OSDN Git Service

2964952241a6e60dc861633edbae45437973eb53
[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, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
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 "cp-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "diagnostic.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "convert.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "params.h"
44
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48                                     tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree, 
51                                           tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *);
58 static bool casts_away_constness (tree, tree);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
63                               tsubst_flags_t);
64
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66    does not have an incomplete type.  (That includes void types.)
67    Returns error_mark_node if the VALUE does not have
68    complete type when this function returns.  */
69
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
72 {
73   tree type;
74
75   if (processing_template_decl || value == error_mark_node)
76     return value;
77
78   if (TREE_CODE (value) == OVERLOAD)
79     type = unknown_type_node;
80   else
81     type = TREE_TYPE (value);
82
83   if (type == error_mark_node)
84     return error_mark_node;
85
86   /* First, detect a valid value with a complete type.  */
87   if (COMPLETE_TYPE_P (type))
88     return value;
89
90   if (complete_type_or_maybe_complain (type, value, complain))
91     return value;
92   else
93     return error_mark_node;
94 }
95
96 tree
97 require_complete_type (tree value)
98 {
99   return require_complete_type_sfinae (value, tf_warning_or_error);
100 }
101
102 /* Try to complete TYPE, if it is incomplete.  For example, if TYPE is
103    a template instantiation, do the instantiation.  Returns TYPE,
104    whether or not it could be completed, unless something goes
105    horribly wrong, in which case the error_mark_node is returned.  */
106
107 tree
108 complete_type (tree type)
109 {
110   if (type == NULL_TREE)
111     /* Rather than crash, we return something sure to cause an error
112        at some point.  */
113     return error_mark_node;
114
115   if (type == error_mark_node || COMPLETE_TYPE_P (type))
116     ;
117   else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
118     {
119       tree t = complete_type (TREE_TYPE (type));
120       unsigned int needs_constructing, has_nontrivial_dtor;
121       if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122         layout_type (type);
123       needs_constructing
124         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125       has_nontrivial_dtor
126         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
128         {
129           TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
131         }
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.
141    Returns NULL_TREE if the type cannot be made complete.  */
142
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
145 {
146   type = complete_type (type);
147   if (type == error_mark_node)
148     /* We already issued an error.  */
149     return NULL_TREE;
150   else if (!COMPLETE_TYPE_P (type))
151     {
152       if (complain & tf_error)
153         cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154       return NULL_TREE;
155     }
156   else
157     return type;
158 }
159
160 tree
161 complete_type_or_else (tree type, tree value)
162 {
163   return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
164 }
165
166 /* Return truthvalue of whether type of EXP is instantiated.  */
167
168 int
169 type_unknown_p (const_tree exp)
170 {
171   return (TREE_CODE (exp) == TREE_LIST
172           || TREE_TYPE (exp) == unknown_type_node);
173 }
174
175 \f
176 /* Return the common type of two parameter lists.
177    We assume that comptypes has already been done and returned 1;
178    if that isn't so, this may crash.
179
180    As an optimization, free the space we allocate if the parameter
181    lists are already common.  */
182
183 static tree
184 commonparms (tree p1, tree p2)
185 {
186   tree oldargs = p1, newargs, n;
187   int i, len;
188   int any_change = 0;
189
190   len = list_length (p1);
191   newargs = tree_last (p1);
192
193   if (newargs == void_list_node)
194     i = 1;
195   else
196     {
197       i = 0;
198       newargs = 0;
199     }
200
201   for (; i < len; i++)
202     newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
203
204   n = newargs;
205
206   for (i = 0; p1;
207        p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
208     {
209       if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
210         {
211           TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212           any_change = 1;
213         }
214       else if (! TREE_PURPOSE (p1))
215         {
216           if (TREE_PURPOSE (p2))
217             {
218               TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219               any_change = 1;
220             }
221         }
222       else
223         {
224           if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225             any_change = 1;
226           TREE_PURPOSE (n) = TREE_PURPOSE (p2);
227         }
228       if (TREE_VALUE (p1) != TREE_VALUE (p2))
229         {
230           any_change = 1;
231           TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
232         }
233       else
234         TREE_VALUE (n) = TREE_VALUE (p1);
235     }
236   if (! any_change)
237     return oldargs;
238
239   return newargs;
240 }
241
242 /* Given a type, perhaps copied for a typedef,
243    find the "original" version of it.  */
244 static tree
245 original_type (tree t)
246 {
247   int quals = cp_type_quals (t);
248   while (t != error_mark_node
249          && TYPE_NAME (t) != NULL_TREE)
250     {
251       tree x = TYPE_NAME (t);
252       if (TREE_CODE (x) != TYPE_DECL)
253         break;
254       x = DECL_ORIGINAL_TYPE (x);
255       if (x == NULL_TREE)
256         break;
257       t = x;
258     }
259   return cp_build_qualified_type (t, quals);
260 }
261
262 /* Return the common type for two arithmetic types T1 and T2 under the
263    usual arithmetic conversions.  The default conversions have already
264    been applied, and enumerated types converted to their compatible
265    integer types.  */
266
267 static tree
268 cp_common_type (tree t1, tree t2)
269 {
270   enum tree_code code1 = TREE_CODE (t1);
271   enum tree_code code2 = TREE_CODE (t2);
272   tree attributes;
273
274
275   /* In what follows, we slightly generalize the rules given in [expr] so
276      as to deal with `long long' and `complex'.  First, merge the
277      attributes.  */
278   attributes = (*targetm.merge_type_attributes) (t1, t2);
279
280   if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
281     {
282       if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283         return build_type_attribute_variant (t1, attributes);
284       else
285         return NULL_TREE;
286     }
287
288   /* FIXME: Attributes.  */
289   gcc_assert (ARITHMETIC_TYPE_P (t1)
290               || TREE_CODE (t1) == VECTOR_TYPE
291               || UNSCOPED_ENUM_P (t1));
292   gcc_assert (ARITHMETIC_TYPE_P (t2)
293               || TREE_CODE (t2) == VECTOR_TYPE
294               || UNSCOPED_ENUM_P (t2));
295
296   /* If one type is complex, form the common type of the non-complex
297      components, then make that complex.  Use T1 or T2 if it is the
298      required type.  */
299   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
300     {
301       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303       tree subtype
304         = type_after_usual_arithmetic_conversions (subtype1, subtype2);
305
306       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307         return build_type_attribute_variant (t1, attributes);
308       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309         return build_type_attribute_variant (t2, attributes);
310       else
311         return build_type_attribute_variant (build_complex_type (subtype),
312                                              attributes);
313     }
314
315   if (code1 == VECTOR_TYPE)
316     {
317       /* When we get here we should have two vectors of the same size.
318          Just prefer the unsigned one if present.  */
319       if (TYPE_UNSIGNED (t1))
320         return build_type_attribute_variant (t1, attributes);
321       else
322         return build_type_attribute_variant (t2, attributes);
323     }
324
325   /* If only one is real, use it as the result.  */
326   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327     return build_type_attribute_variant (t1, attributes);
328   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329     return build_type_attribute_variant (t2, attributes);
330
331   /* Both real or both integers; use the one with greater precision.  */
332   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333     return build_type_attribute_variant (t1, attributes);
334   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335     return build_type_attribute_variant (t2, attributes);
336
337   /* The types are the same; no need to do anything fancy.  */
338   if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339     return build_type_attribute_variant (t1, attributes);
340
341   if (code1 != REAL_TYPE)
342     {
343       /* If one is unsigned long long, then convert the other to unsigned
344          long long.  */
345       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346           || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347         return build_type_attribute_variant (long_long_unsigned_type_node,
348                                              attributes);
349       /* If one is a long long, and the other is an unsigned long, and
350          long long can represent all the values of an unsigned long, then
351          convert to a long long.  Otherwise, convert to an unsigned long
352          long.  Otherwise, if either operand is long long, convert the
353          other to long long.
354
355          Since we're here, we know the TYPE_PRECISION is the same;
356          therefore converting to long long cannot represent all the values
357          of an unsigned long, so we choose unsigned long long in that
358          case.  */
359       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360           || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
361         {
362           tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363                     ? long_long_unsigned_type_node
364                     : long_long_integer_type_node);
365           return build_type_attribute_variant (t, attributes);
366         }
367       if (int128_integer_type_node != NULL_TREE
368           && (same_type_p (TYPE_MAIN_VARIANT (t1),
369                            int128_integer_type_node)
370               || same_type_p (TYPE_MAIN_VARIANT (t2),
371                               int128_integer_type_node)))
372         {
373           tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374                     ? int128_unsigned_type_node
375                     : int128_integer_type_node);
376           return build_type_attribute_variant (t, attributes);
377         }
378
379       /* Go through the same procedure, but for longs.  */
380       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381           || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382         return build_type_attribute_variant (long_unsigned_type_node,
383                                              attributes);
384       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385           || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
386         {
387           tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388                     ? long_unsigned_type_node : long_integer_type_node);
389           return build_type_attribute_variant (t, attributes);
390         }
391       /* Otherwise prefer the unsigned one.  */
392       if (TYPE_UNSIGNED (t1))
393         return build_type_attribute_variant (t1, attributes);
394       else
395         return build_type_attribute_variant (t2, attributes);
396     }
397   else
398     {
399       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400           || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401         return build_type_attribute_variant (long_double_type_node,
402                                              attributes);
403       if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404           || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405         return build_type_attribute_variant (double_type_node,
406                                              attributes);
407       if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408           || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409         return build_type_attribute_variant (float_type_node,
410                                              attributes);
411
412       /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413          the standard C++ floating-point types.  Logic earlier in this
414          function has already eliminated the possibility that
415          TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416          compelling reason to choose one or the other.  */
417       return build_type_attribute_variant (t1, attributes);
418     }
419 }
420
421 /* T1 and T2 are arithmetic or enumeration types.  Return the type
422    that will result from the "usual arithmetic conversions" on T1 and
423    T2 as described in [expr].  */
424
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
427 {
428   gcc_assert (ARITHMETIC_TYPE_P (t1)
429               || TREE_CODE (t1) == VECTOR_TYPE
430               || UNSCOPED_ENUM_P (t1));
431   gcc_assert (ARITHMETIC_TYPE_P (t2)
432               || TREE_CODE (t2) == VECTOR_TYPE
433               || UNSCOPED_ENUM_P (t2));
434
435   /* Perform the integral promotions.  We do not promote real types here.  */
436   if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437       && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
438     {
439       t1 = type_promotes_to (t1);
440       t2 = type_promotes_to (t2);
441     }
442
443   return cp_common_type (t1, t2);
444 }
445
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448                          composite_pointer_operation operation)
449 {
450   switch (operation)
451     {
452     case CPO_COMPARISON:
453       emit_diagnostic (kind, input_location, 0,
454                        "comparison between "
455                        "distinct pointer types %qT and %qT lacks a cast",
456                        t1, t2);
457       break;
458     case CPO_CONVERSION:
459       emit_diagnostic (kind, input_location, 0,
460                        "conversion between "
461                        "distinct pointer types %qT and %qT lacks a cast",
462                        t1, t2);
463       break;
464     case CPO_CONDITIONAL_EXPR:
465       emit_diagnostic (kind, input_location, 0,
466                        "conditional expression between "
467                        "distinct pointer types %qT and %qT lacks a cast",
468                        t1, t2);
469       break;
470     default:
471       gcc_unreachable ();
472     }
473 }
474
475 /* Subroutine of composite_pointer_type to implement the recursive
476    case.  See that function for documentation of the parameters.  */
477
478 static tree
479 composite_pointer_type_r (tree t1, tree t2, 
480                           composite_pointer_operation operation,
481                           tsubst_flags_t complain)
482 {
483   tree pointee1;
484   tree pointee2;
485   tree result_type;
486   tree attributes;
487
488   /* Determine the types pointed to by T1 and T2.  */
489   if (TREE_CODE (t1) == POINTER_TYPE)
490     {
491       pointee1 = TREE_TYPE (t1);
492       pointee2 = TREE_TYPE (t2);
493     }
494   else
495     {
496       pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497       pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
498     }
499
500   /* [expr.rel]
501
502      Otherwise, the composite pointer type is a pointer type
503      similar (_conv.qual_) to the type of one of the operands,
504      with a cv-qualification signature (_conv.qual_) that is the
505      union of the cv-qualification signatures of the operand
506      types.  */
507   if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508     result_type = pointee1;
509   else if ((TREE_CODE (pointee1) == POINTER_TYPE
510             && TREE_CODE (pointee2) == POINTER_TYPE)
511            || (TYPE_PTR_TO_MEMBER_P (pointee1)
512                && TYPE_PTR_TO_MEMBER_P (pointee2)))
513     {
514       result_type = composite_pointer_type_r (pointee1, pointee2, operation,
515                                               complain);
516       if (result_type == error_mark_node)
517         return error_mark_node;
518     }
519   else
520     {
521       if (complain & tf_error)
522         composite_pointer_error (DK_PERMERROR, t1, t2, operation);
523       else
524         return error_mark_node;
525       result_type = void_type_node;
526     }
527   result_type = cp_build_qualified_type (result_type,
528                                          (cp_type_quals (pointee1)
529                                           | cp_type_quals (pointee2)));
530   /* If the original types were pointers to members, so is the
531      result.  */
532   if (TYPE_PTR_TO_MEMBER_P (t1))
533     {
534       if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
535                         TYPE_PTRMEM_CLASS_TYPE (t2)))
536         {
537           if (complain & tf_error)
538             composite_pointer_error (DK_PERMERROR, t1, t2, operation);
539           else
540             return error_mark_node;
541         }
542       result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
543                                        result_type);
544     }
545   else
546     result_type = build_pointer_type (result_type);
547
548   /* Merge the attributes.  */
549   attributes = (*targetm.merge_type_attributes) (t1, t2);
550   return build_type_attribute_variant (result_type, attributes);
551 }
552
553 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
554    ARG1 and ARG2 are the values with those types.  The OPERATION is to
555    describe the operation between the pointer types,
556    in case an error occurs.
557
558    This routine also implements the computation of a common type for
559    pointers-to-members as per [expr.eq].  */
560
561 tree
562 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
563                         composite_pointer_operation operation, 
564                         tsubst_flags_t complain)
565 {
566   tree class1;
567   tree class2;
568
569   /* [expr.rel]
570
571      If one operand is a null pointer constant, the composite pointer
572      type is the type of the other operand.  */
573   if (null_ptr_cst_p (arg1))
574     return t2;
575   if (null_ptr_cst_p (arg2))
576     return t1;
577
578   /* We have:
579
580        [expr.rel]
581
582        If one of the operands has type "pointer to cv1 void*", then
583        the other has type "pointer to cv2T", and the composite pointer
584        type is "pointer to cv12 void", where cv12 is the union of cv1
585        and cv2.
586
587     If either type is a pointer to void, make sure it is T1.  */
588   if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
589     {
590       tree t;
591       t = t1;
592       t1 = t2;
593       t2 = t;
594     }
595
596   /* Now, if T1 is a pointer to void, merge the qualifiers.  */
597   if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
598     {
599       tree attributes;
600       tree result_type;
601
602       if (TYPE_PTRFN_P (t2) && (complain & tf_error))
603         {
604           switch (operation)
605               {
606               case CPO_COMPARISON:
607                 pedwarn (input_location, OPT_pedantic, 
608                          "ISO C++ forbids comparison between "
609                          "pointer of type %<void *%> and pointer-to-function");
610                 break;
611               case CPO_CONVERSION:
612                 pedwarn (input_location, OPT_pedantic,
613                          "ISO C++ forbids conversion between "
614                          "pointer of type %<void *%> and pointer-to-function");
615                 break;
616               case CPO_CONDITIONAL_EXPR:
617                 pedwarn (input_location, OPT_pedantic,
618                          "ISO C++ forbids conditional expression between "
619                          "pointer of type %<void *%> and pointer-to-function");
620                 break;
621               default:
622                 gcc_unreachable ();
623               }
624         }
625       result_type
626         = cp_build_qualified_type (void_type_node,
627                                    (cp_type_quals (TREE_TYPE (t1))
628                                     | cp_type_quals (TREE_TYPE (t2))));
629       result_type = build_pointer_type (result_type);
630       /* Merge the attributes.  */
631       attributes = (*targetm.merge_type_attributes) (t1, t2);
632       return build_type_attribute_variant (result_type, attributes);
633     }
634
635   if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
636       && TREE_CODE (t2) == POINTER_TYPE)
637     {
638       if (objc_have_common_type (t1, t2, -3, NULL_TREE))
639         return objc_common_type (t1, t2);
640     }
641
642   /* [expr.eq] permits the application of a pointer conversion to
643      bring the pointers to a common type.  */
644   if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
645       && CLASS_TYPE_P (TREE_TYPE (t1))
646       && CLASS_TYPE_P (TREE_TYPE (t2))
647       && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
648                                                      TREE_TYPE (t2)))
649     {
650       class1 = TREE_TYPE (t1);
651       class2 = TREE_TYPE (t2);
652
653       if (DERIVED_FROM_P (class1, class2))
654         t2 = (build_pointer_type
655               (cp_build_qualified_type (class1, cp_type_quals (class2))));
656       else if (DERIVED_FROM_P (class2, class1))
657         t1 = (build_pointer_type
658               (cp_build_qualified_type (class2, cp_type_quals (class1))));
659       else
660         {
661           if (complain & tf_error)
662             composite_pointer_error (DK_ERROR, t1, t2, operation);
663           return error_mark_node;
664         }
665     }
666   /* [expr.eq] permits the application of a pointer-to-member
667      conversion to change the class type of one of the types.  */
668   else if (TYPE_PTR_TO_MEMBER_P (t1)
669            && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
670                             TYPE_PTRMEM_CLASS_TYPE (t2)))
671     {
672       class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
673       class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
674
675       if (DERIVED_FROM_P (class1, class2))
676         t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
677       else if (DERIVED_FROM_P (class2, class1))
678         t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
679       else
680         {
681           if (complain & tf_error)
682             switch (operation)
683               {
684               case CPO_COMPARISON:
685                 error ("comparison between distinct "
686                        "pointer-to-member types %qT and %qT lacks a cast",
687                        t1, t2);
688                 break;
689               case CPO_CONVERSION:
690                 error ("conversion between distinct "
691                        "pointer-to-member types %qT and %qT lacks a cast",
692                        t1, t2);
693                 break;
694               case CPO_CONDITIONAL_EXPR:
695                 error ("conditional expression between distinct "
696                        "pointer-to-member types %qT and %qT lacks a cast",
697                        t1, t2);
698                 break;
699               default:
700                 gcc_unreachable ();
701               }
702           return error_mark_node;
703         }
704     }
705
706   return composite_pointer_type_r (t1, t2, operation, complain);
707 }
708
709 /* Return the merged type of two types.
710    We assume that comptypes has already been done and returned 1;
711    if that isn't so, this may crash.
712
713    This just combines attributes and default arguments; any other
714    differences would cause the two types to compare unalike.  */
715
716 tree
717 merge_types (tree t1, tree t2)
718 {
719   enum tree_code code1;
720   enum tree_code code2;
721   tree attributes;
722
723   /* Save time if the two types are the same.  */
724   if (t1 == t2)
725     return t1;
726   if (original_type (t1) == original_type (t2))
727     return t1;
728
729   /* If one type is nonsense, use the other.  */
730   if (t1 == error_mark_node)
731     return t2;
732   if (t2 == error_mark_node)
733     return t1;
734
735   /* Merge the attributes.  */
736   attributes = (*targetm.merge_type_attributes) (t1, t2);
737
738   if (TYPE_PTRMEMFUNC_P (t1))
739     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
740   if (TYPE_PTRMEMFUNC_P (t2))
741     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
742
743   code1 = TREE_CODE (t1);
744   code2 = TREE_CODE (t2);
745   if (code1 != code2)
746     {
747       gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
748       if (code1 == TYPENAME_TYPE)
749         {
750           t1 = resolve_typename_type (t1, /*only_current_p=*/true);
751           code1 = TREE_CODE (t1);
752         }
753       else
754         {
755           t2 = resolve_typename_type (t2, /*only_current_p=*/true);
756           code2 = TREE_CODE (t2);
757         }
758     }
759
760   switch (code1)
761     {
762     case POINTER_TYPE:
763     case REFERENCE_TYPE:
764       /* For two pointers, do this recursively on the target type.  */
765       {
766         tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
767         int quals = cp_type_quals (t1);
768
769         if (code1 == POINTER_TYPE)
770           t1 = build_pointer_type (target);
771         else
772           t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
773         t1 = build_type_attribute_variant (t1, attributes);
774         t1 = cp_build_qualified_type (t1, quals);
775
776         if (TREE_CODE (target) == METHOD_TYPE)
777           t1 = build_ptrmemfunc_type (t1);
778
779         return t1;
780       }
781
782     case OFFSET_TYPE:
783       {
784         int quals;
785         tree pointee;
786         quals = cp_type_quals (t1);
787         pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
788                                TYPE_PTRMEM_POINTED_TO_TYPE (t2));
789         t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
790                                 pointee);
791         t1 = cp_build_qualified_type (t1, quals);
792         break;
793       }
794
795     case ARRAY_TYPE:
796       {
797         tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798         /* Save space: see if the result is identical to one of the args.  */
799         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
800           return build_type_attribute_variant (t1, attributes);
801         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
802           return build_type_attribute_variant (t2, attributes);
803         /* Merge the element types, and have a size if either arg has one.  */
804         t1 = build_cplus_array_type
805           (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
806         break;
807       }
808
809     case FUNCTION_TYPE:
810       /* Function types: prefer the one that specified arg types.
811          If both do, merge the arg types.  Also merge the return types.  */
812       {
813         tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814         tree p1 = TYPE_ARG_TYPES (t1);
815         tree p2 = TYPE_ARG_TYPES (t2);
816         tree parms;
817         tree rval, raises;
818
819         /* Save space: see if the result is identical to one of the args.  */
820         if (valtype == TREE_TYPE (t1) && ! p2)
821           return cp_build_type_attribute_variant (t1, attributes);
822         if (valtype == TREE_TYPE (t2) && ! p1)
823           return cp_build_type_attribute_variant (t2, attributes);
824
825         /* Simple way if one arg fails to specify argument types.  */
826         if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
827           parms = p2;
828         else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
829           parms = p1;
830         else
831           parms = commonparms (p1, p2);
832
833         rval = build_function_type (valtype, parms);
834         gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
835         rval = apply_memfn_quals (rval, type_memfn_quals (t1));
836         raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
837                                              TYPE_RAISES_EXCEPTIONS (t2),
838                                              NULL_TREE);
839         t1 = build_exception_variant (rval, raises);
840         break;
841       }
842
843     case METHOD_TYPE:
844       {
845         /* Get this value the long way, since TYPE_METHOD_BASETYPE
846            is just the main variant of this.  */
847         tree basetype = class_of_this_parm (t2);
848         tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
849                                                   TYPE_RAISES_EXCEPTIONS (t2),
850                                                   NULL_TREE);
851         tree t3;
852
853         /* If this was a member function type, get back to the
854            original type of type member function (i.e., without
855            the class instance variable up front.  */
856         t1 = build_function_type (TREE_TYPE (t1),
857                                   TREE_CHAIN (TYPE_ARG_TYPES (t1)));
858         t2 = build_function_type (TREE_TYPE (t2),
859                                   TREE_CHAIN (TYPE_ARG_TYPES (t2)));
860         t3 = merge_types (t1, t2);
861         t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
862                                          TYPE_ARG_TYPES (t3));
863         t1 = build_exception_variant (t3, raises);
864         break;
865       }
866
867     case TYPENAME_TYPE:
868       /* There is no need to merge attributes into a TYPENAME_TYPE.
869          When the type is instantiated it will have whatever
870          attributes result from the instantiation.  */
871       return t1;
872
873     default:;
874     }
875
876   if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
877     return t1;
878   else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
879     return t2;
880   else
881     return cp_build_type_attribute_variant (t1, attributes);
882 }
883
884 /* Return the ARRAY_TYPE type without its domain.  */
885
886 tree
887 strip_array_domain (tree type)
888 {
889   tree t2;
890   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
891   if (TYPE_DOMAIN (type) == NULL_TREE)
892     return type;
893   t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
894   return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
895 }
896
897 /* Wrapper around cp_common_type that is used by c-common.c and other
898    front end optimizations that remove promotions.  
899
900    Return the common type for two arithmetic types T1 and T2 under the
901    usual arithmetic conversions.  The default conversions have already
902    been applied, and enumerated types converted to their compatible
903    integer types.  */
904
905 tree
906 common_type (tree t1, tree t2)
907 {
908   /* If one type is nonsense, use the other  */
909   if (t1 == error_mark_node)
910     return t2;
911   if (t2 == error_mark_node)
912     return t1;
913
914   return cp_common_type (t1, t2);
915 }
916
917 /* Return the common type of two pointer types T1 and T2.  This is the
918    type for the result of most arithmetic operations if the operands
919    have the given two types.
920  
921    We assume that comp_target_types has already been done and returned
922    nonzero; if that isn't so, this may crash.  */
923
924 tree
925 common_pointer_type (tree t1, tree t2)
926 {
927   gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
928               || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
929               || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
930
931   return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
932                                  CPO_CONVERSION, tf_warning_or_error);
933 }
934 \f
935 /* Compare two exception specifier types for exactness or subsetness, if
936    allowed. Returns false for mismatch, true for match (same, or
937    derived and !exact).
938
939    [except.spec] "If a class X ... objects of class X or any class publicly
940    and unambiguously derived from X. Similarly, if a pointer type Y * ...
941    exceptions of type Y * or that are pointers to any type publicly and
942    unambiguously derived from Y. Otherwise a function only allows exceptions
943    that have the same type ..."
944    This does not mention cv qualifiers and is different to what throw
945    [except.throw] and catch [except.catch] will do. They will ignore the
946    top level cv qualifiers, and allow qualifiers in the pointer to class
947    example.
948
949    We implement the letter of the standard.  */
950
951 static bool
952 comp_except_types (tree a, tree b, bool exact)
953 {
954   if (same_type_p (a, b))
955     return true;
956   else if (!exact)
957     {
958       if (cp_type_quals (a) || cp_type_quals (b))
959         return false;
960
961       if (TREE_CODE (a) == POINTER_TYPE
962           && TREE_CODE (b) == POINTER_TYPE)
963         {
964           a = TREE_TYPE (a);
965           b = TREE_TYPE (b);
966           if (cp_type_quals (a) || cp_type_quals (b))
967             return false;
968         }
969
970       if (TREE_CODE (a) != RECORD_TYPE
971           || TREE_CODE (b) != RECORD_TYPE)
972         return false;
973
974       if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
975         return true;
976     }
977   return false;
978 }
979
980 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
981    If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
982    If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
983    If EXACT is ce_exact, the specs must be exactly the same. Exception lists
984    are unordered, but we've already filtered out duplicates. Most lists will
985    be in order, we should try to make use of that.  */
986
987 bool
988 comp_except_specs (const_tree t1, const_tree t2, int exact)
989 {
990   const_tree probe;
991   const_tree base;
992   int  length = 0;
993
994   if (t1 == t2)
995     return true;
996
997   /* First handle noexcept.  */
998   if (exact < ce_exact)
999     {
1000       /* noexcept(false) is compatible with no exception-specification,
1001          and stricter than any spec.  */
1002       if (t1 == noexcept_false_spec)
1003         return t2 == NULL_TREE || exact == ce_derived;
1004       /* Even a derived noexcept(false) is compatible with no
1005          exception-specification.  */
1006       if (t2 == noexcept_false_spec)
1007         return t1 == NULL_TREE;
1008
1009       /* Otherwise, if we aren't looking for an exact match, noexcept is
1010          equivalent to throw().  */
1011       if (t1 == noexcept_true_spec)
1012         t1 = empty_except_spec;
1013       if (t2 == noexcept_true_spec)
1014         t2 = empty_except_spec;
1015     }
1016
1017   /* If any noexcept is left, it is only comparable to itself;
1018      either we're looking for an exact match or we're redeclaring a
1019      template with dependent noexcept.  */
1020   if ((t1 && TREE_PURPOSE (t1))
1021       || (t2 && TREE_PURPOSE (t2)))
1022     return (t1 && t2
1023             && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1024
1025   if (t1 == NULL_TREE)                     /* T1 is ...  */
1026     return t2 == NULL_TREE || exact == ce_derived;
1027   if (!TREE_VALUE (t1))                    /* t1 is EMPTY */
1028     return t2 != NULL_TREE && !TREE_VALUE (t2);
1029   if (t2 == NULL_TREE)                     /* T2 is ...  */
1030     return false;
1031   if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1032     return exact == ce_derived;
1033
1034   /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1035      Count how many we find, to determine exactness. For exact matching and
1036      ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1037      O(nm).  */
1038   for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1039     {
1040       for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1041         {
1042           tree a = TREE_VALUE (probe);
1043           tree b = TREE_VALUE (t2);
1044
1045           if (comp_except_types (a, b, exact))
1046             {
1047               if (probe == base && exact > ce_derived)
1048                 base = TREE_CHAIN (probe);
1049               length++;
1050               break;
1051             }
1052         }
1053       if (probe == NULL_TREE)
1054         return false;
1055     }
1056   return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1057 }
1058
1059 /* Compare the array types T1 and T2.  ALLOW_REDECLARATION is true if
1060    [] can match [size].  */
1061
1062 static bool
1063 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1064 {
1065   tree d1;
1066   tree d2;
1067   tree max1, max2;
1068
1069   if (t1 == t2)
1070     return true;
1071
1072   /* The type of the array elements must be the same.  */
1073   if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1074     return false;
1075
1076   d1 = TYPE_DOMAIN (t1);
1077   d2 = TYPE_DOMAIN (t2);
1078
1079   if (d1 == d2)
1080     return true;
1081
1082   /* If one of the arrays is dimensionless, and the other has a
1083      dimension, they are of different types.  However, it is valid to
1084      write:
1085
1086        extern int a[];
1087        int a[3];
1088
1089      by [basic.link]:
1090
1091        declarations for an array object can specify
1092        array types that differ by the presence or absence of a major
1093        array bound (_dcl.array_).  */
1094   if (!d1 || !d2)
1095     return allow_redeclaration;
1096
1097   /* Check that the dimensions are the same.  */
1098
1099   if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1100     return false;
1101   max1 = TYPE_MAX_VALUE (d1);
1102   max2 = TYPE_MAX_VALUE (d2);
1103   if (processing_template_decl && !abi_version_at_least (2)
1104       && !value_dependent_expression_p (max1)
1105       && !value_dependent_expression_p (max2))
1106     {
1107       /* With abi-1 we do not fold non-dependent array bounds, (and
1108          consequently mangle them incorrectly).  We must therefore
1109          fold them here, to verify the domains have the same
1110          value.  */
1111       max1 = fold (max1);
1112       max2 = fold (max2);
1113     }
1114
1115   if (!cp_tree_equal (max1, max2))
1116     return false;
1117
1118   return true;
1119 }
1120
1121 /* Compare the relative position of T1 and T2 into their respective
1122    template parameter list.
1123    T1 and T2 must be template parameter types.
1124    Return TRUE if T1 and T2 have the same position, FALSE otherwise.  */
1125
1126 static bool
1127 comp_template_parms_position (tree t1, tree t2)
1128 {
1129   tree index1, index2;
1130   gcc_assert (t1 && t2
1131               && TREE_CODE (t1) == TREE_CODE (t2)
1132               && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1133                   || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1134                   || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1135
1136   index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1137   index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1138
1139   /* If T1 and T2 belong to template parm lists of different size,
1140      let's assume they are different.  */
1141   if (TEMPLATE_PARM_NUM_SIBLINGS (index1)
1142       != TEMPLATE_PARM_NUM_SIBLINGS (index2))
1143     return false;
1144
1145   /* Then compare their relative position.  */
1146   if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1147       || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1148       || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1149           != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1150     return false;
1151
1152   return true;
1153 }
1154
1155 /* Subroutine in comptypes.  */
1156
1157 static bool
1158 structural_comptypes (tree t1, tree t2, int strict)
1159 {
1160   if (t1 == t2)
1161     return true;
1162
1163   /* Suppress errors caused by previously reported errors.  */
1164   if (t1 == error_mark_node || t2 == error_mark_node)
1165     return false;
1166
1167   gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1168
1169   /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1170      current instantiation.  */
1171   if (TREE_CODE (t1) == TYPENAME_TYPE)
1172     t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1173
1174   if (TREE_CODE (t2) == TYPENAME_TYPE)
1175     t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1176
1177   if (TYPE_PTRMEMFUNC_P (t1))
1178     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1179   if (TYPE_PTRMEMFUNC_P (t2))
1180     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1181
1182   /* Different classes of types can't be compatible.  */
1183   if (TREE_CODE (t1) != TREE_CODE (t2))
1184     return false;
1185
1186   /* Qualifiers must match.  For array types, we will check when we
1187      recur on the array element types.  */
1188   if (TREE_CODE (t1) != ARRAY_TYPE
1189       && cp_type_quals (t1) != cp_type_quals (t2))
1190     return false;
1191   if (TREE_CODE (t1) == FUNCTION_TYPE
1192       && type_memfn_quals (t1) != type_memfn_quals (t2))
1193     return false;
1194   if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1195     return false;
1196
1197   /* Allow for two different type nodes which have essentially the same
1198      definition.  Note that we already checked for equality of the type
1199      qualifiers (just above).  */
1200
1201   if (TREE_CODE (t1) != ARRAY_TYPE
1202       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1203     return true;
1204
1205
1206   /* Compare the types.  Break out if they could be the same.  */
1207   switch (TREE_CODE (t1))
1208     {
1209     case VOID_TYPE:
1210     case BOOLEAN_TYPE:
1211       /* All void and bool types are the same.  */
1212       break;
1213
1214     case INTEGER_TYPE:
1215     case FIXED_POINT_TYPE:
1216     case REAL_TYPE:
1217       /* With these nodes, we can't determine type equivalence by
1218          looking at what is stored in the nodes themselves, because
1219          two nodes might have different TYPE_MAIN_VARIANTs but still
1220          represent the same type.  For example, wchar_t and int could
1221          have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1222          TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1223          and are distinct types. On the other hand, int and the
1224          following typedef
1225
1226            typedef int INT __attribute((may_alias));
1227
1228          have identical properties, different TYPE_MAIN_VARIANTs, but
1229          represent the same type.  The canonical type system keeps
1230          track of equivalence in this case, so we fall back on it.  */
1231       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1232
1233     case TEMPLATE_TEMPLATE_PARM:
1234     case BOUND_TEMPLATE_TEMPLATE_PARM:
1235       if (!comp_template_parms_position (t1, t2))
1236         return false;
1237       if (!comp_template_parms
1238           (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1239            DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1240         return false;
1241       if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1242         break;
1243       /* Don't check inheritance.  */
1244       strict = COMPARE_STRICT;
1245       /* Fall through.  */
1246
1247     case RECORD_TYPE:
1248     case UNION_TYPE:
1249       if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1250           && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1251               || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1252           && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1253         break;
1254
1255       if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1256         break;
1257       else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1258         break;
1259
1260       return false;
1261
1262     case OFFSET_TYPE:
1263       if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1264                       strict & ~COMPARE_REDECLARATION))
1265         return false;
1266       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1267         return false;
1268       break;
1269
1270     case REFERENCE_TYPE:
1271       if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1272         return false;
1273       /* fall through to checks for pointer types */
1274
1275     case POINTER_TYPE:
1276       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1277           || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1278           || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1279         return false;
1280       break;
1281
1282     case METHOD_TYPE:
1283     case FUNCTION_TYPE:
1284       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1285         return false;
1286       if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1287         return false;
1288       break;
1289
1290     case ARRAY_TYPE:
1291       /* Target types must match incl. qualifiers.  */
1292       if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1293         return false;
1294       break;
1295
1296     case TEMPLATE_TYPE_PARM:
1297       /* If T1 and T2 don't have the same relative position in their
1298          template parameters set, they can't be equal.  */
1299       if (!comp_template_parms_position (t1, t2))
1300         return false;
1301       break;
1302
1303     case TYPENAME_TYPE:
1304       if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1305                           TYPENAME_TYPE_FULLNAME (t2)))
1306         return false;
1307       /* Qualifiers don't matter on scopes.  */
1308       if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1309                                                       TYPE_CONTEXT (t2)))
1310         return false;
1311       break;
1312
1313     case UNBOUND_CLASS_TEMPLATE:
1314       if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1315         return false;
1316       if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1317         return false;
1318       break;
1319
1320     case COMPLEX_TYPE:
1321       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1322         return false;
1323       break;
1324
1325     case VECTOR_TYPE:
1326       if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1327           || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1328         return false;
1329       break;
1330
1331     case TYPE_PACK_EXPANSION:
1332       return same_type_p (PACK_EXPANSION_PATTERN (t1), 
1333                           PACK_EXPANSION_PATTERN (t2));
1334
1335     case DECLTYPE_TYPE:
1336       if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1337           != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1338           || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1339               != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1340           || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1341               != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1342           || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), 
1343                              DECLTYPE_TYPE_EXPR (t2)))
1344         return false;
1345       break;
1346
1347     case UNDERLYING_TYPE:
1348       return same_type_p (UNDERLYING_TYPE_TYPE (t1), 
1349                           UNDERLYING_TYPE_TYPE (t2));
1350
1351     default:
1352       return false;
1353     }
1354
1355   /* If we get here, we know that from a target independent POV the
1356      types are the same.  Make sure the target attributes are also
1357      the same.  */
1358   return comp_type_attributes (t1, t2);
1359 }
1360
1361 /* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
1362    is a bitwise-or of the COMPARE_* flags.  */
1363
1364 bool
1365 comptypes (tree t1, tree t2, int strict)
1366 {
1367   if (strict == COMPARE_STRICT)
1368     {
1369       if (t1 == t2)
1370         return true;
1371
1372       if (t1 == error_mark_node || t2 == error_mark_node)
1373         return false;
1374
1375       if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1376         /* At least one of the types requires structural equality, so
1377            perform a deep check. */
1378         return structural_comptypes (t1, t2, strict);
1379
1380 #ifdef ENABLE_CHECKING
1381       if (USE_CANONICAL_TYPES)
1382         {
1383           bool result = structural_comptypes (t1, t2, strict);
1384           
1385           if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1386             /* The two types are structurally equivalent, but their
1387                canonical types were different. This is a failure of the
1388                canonical type propagation code.*/
1389             internal_error 
1390               ("canonical types differ for identical types %T and %T", 
1391                t1, t2);
1392           else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1393             /* Two types are structurally different, but the canonical
1394                types are the same. This means we were over-eager in
1395                assigning canonical types. */
1396             internal_error 
1397               ("same canonical type node for different types %T and %T",
1398                t1, t2);
1399           
1400           return result;
1401         }
1402 #else
1403       if (USE_CANONICAL_TYPES)
1404         return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1405 #endif
1406       else
1407         return structural_comptypes (t1, t2, strict);
1408     }
1409   else if (strict == COMPARE_STRUCTURAL)
1410     return structural_comptypes (t1, t2, COMPARE_STRICT);
1411   else
1412     return structural_comptypes (t1, t2, strict);
1413 }
1414
1415 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1416    top-level qualifiers.  */
1417
1418 bool
1419 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1420 {
1421   if (type1 == error_mark_node || type2 == error_mark_node)
1422     return false;
1423
1424   return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1425 }
1426
1427 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
1428
1429 bool
1430 at_least_as_qualified_p (const_tree type1, const_tree type2)
1431 {
1432   int q1 = cp_type_quals (type1);
1433   int q2 = cp_type_quals (type2);
1434
1435   /* All qualifiers for TYPE2 must also appear in TYPE1.  */
1436   return (q1 & q2) == q2;
1437 }
1438
1439 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1440    more cv-qualified that TYPE1, and 0 otherwise.  */
1441
1442 int
1443 comp_cv_qualification (const_tree type1, const_tree type2)
1444 {
1445   int q1 = cp_type_quals (type1);
1446   int q2 = cp_type_quals (type2);
1447
1448   if (q1 == q2)
1449     return 0;
1450
1451   if ((q1 & q2) == q2)
1452     return 1;
1453   else if ((q1 & q2) == q1)
1454     return -1;
1455
1456   return 0;
1457 }
1458
1459 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1460    subset of the cv-qualification signature of TYPE2, and the types
1461    are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
1462
1463 int
1464 comp_cv_qual_signature (tree type1, tree type2)
1465 {
1466   if (comp_ptr_ttypes_real (type2, type1, -1))
1467     return 1;
1468   else if (comp_ptr_ttypes_real (type1, type2, -1))
1469     return -1;
1470   else
1471     return 0;
1472 }
1473 \f
1474 /* Subroutines of `comptypes'.  */
1475
1476 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1477    equivalent in the sense that functions with those parameter types
1478    can have equivalent types.  The two lists must be equivalent,
1479    element by element.  */
1480
1481 bool
1482 compparms (const_tree parms1, const_tree parms2)
1483 {
1484   const_tree t1, t2;
1485
1486   /* An unspecified parmlist matches any specified parmlist
1487      whose argument types don't need default promotions.  */
1488
1489   for (t1 = parms1, t2 = parms2;
1490        t1 || t2;
1491        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1492     {
1493       /* If one parmlist is shorter than the other,
1494          they fail to match.  */
1495       if (!t1 || !t2)
1496         return false;
1497       if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1498         return false;
1499     }
1500   return true;
1501 }
1502
1503 \f
1504 /* Process a sizeof or alignof expression where the operand is a
1505    type.  */
1506
1507 tree
1508 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1509 {
1510   tree value;
1511   bool dependent_p;
1512
1513   gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1514   if (type == error_mark_node)
1515     return error_mark_node;
1516
1517   type = non_reference (type);
1518   if (TREE_CODE (type) == METHOD_TYPE)
1519     {
1520       if (complain)
1521         pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, 
1522                  "invalid application of %qs to a member function", 
1523                  operator_name_info[(int) op].name);
1524       value = size_one_node;
1525     }
1526
1527   dependent_p = dependent_type_p (type);
1528   if (!dependent_p)
1529     complete_type (type);
1530   if (dependent_p
1531       /* VLA types will have a non-constant size.  In the body of an
1532          uninstantiated template, we don't need to try to compute the
1533          value, because the sizeof expression is not an integral
1534          constant expression in that case.  And, if we do try to
1535          compute the value, we'll likely end up with SAVE_EXPRs, which
1536          the template substitution machinery does not expect to see.  */
1537       || (processing_template_decl 
1538           && COMPLETE_TYPE_P (type)
1539           && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1540     {
1541       value = build_min (op, size_type_node, type);
1542       TREE_READONLY (value) = 1;
1543       return value;
1544     }
1545
1546   return c_sizeof_or_alignof_type (input_location, complete_type (type),
1547                                    op == SIZEOF_EXPR,
1548                                    complain);
1549 }
1550
1551 /* Return the size of the type, without producing any warnings for
1552    types whose size cannot be taken.  This routine should be used only
1553    in some other routine that has already produced a diagnostic about
1554    using the size of such a type.  */
1555 tree 
1556 cxx_sizeof_nowarn (tree type)
1557 {
1558   if (TREE_CODE (type) == FUNCTION_TYPE
1559       || TREE_CODE (type) == VOID_TYPE
1560       || TREE_CODE (type) == ERROR_MARK)
1561     return size_one_node;
1562   else if (!COMPLETE_TYPE_P (type))
1563     return size_zero_node;
1564   else
1565     return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1566 }
1567
1568 /* Process a sizeof expression where the operand is an expression.  */
1569
1570 static tree
1571 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1572 {
1573   if (e == error_mark_node)
1574     return error_mark_node;
1575
1576   if (processing_template_decl)
1577     {
1578       e = build_min (SIZEOF_EXPR, size_type_node, e);
1579       TREE_SIDE_EFFECTS (e) = 0;
1580       TREE_READONLY (e) = 1;
1581
1582       return e;
1583     }
1584
1585   /* To get the size of a static data member declared as an array of
1586      unknown bound, we need to instantiate it.  */
1587   if (TREE_CODE (e) == VAR_DECL
1588       && VAR_HAD_UNKNOWN_BOUND (e)
1589       && DECL_TEMPLATE_INSTANTIATION (e))
1590     instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1591
1592   e = mark_type_use (e);
1593
1594   if (TREE_CODE (e) == COMPONENT_REF
1595       && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1596       && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1597     {
1598       if (complain & tf_error)
1599         error ("invalid application of %<sizeof%> to a bit-field");
1600       else
1601         return error_mark_node;
1602       e = char_type_node;
1603     }
1604   else if (is_overloaded_fn (e))
1605     {
1606       if (complain & tf_error)
1607         permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1608                    "function type");
1609       else
1610         return error_mark_node;
1611       e = char_type_node;
1612     }
1613   else if (type_unknown_p (e))
1614     {
1615       if (complain & tf_error)
1616         cxx_incomplete_type_error (e, TREE_TYPE (e));
1617       else
1618         return error_mark_node;
1619       e = char_type_node;
1620     }
1621   else
1622     e = TREE_TYPE (e);
1623
1624   return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1625 }
1626
1627 /* Implement the __alignof keyword: Return the minimum required
1628    alignment of E, measured in bytes.  For VAR_DECL's and
1629    FIELD_DECL's return DECL_ALIGN (which can be set from an
1630    "aligned" __attribute__ specification).  */
1631
1632 static tree
1633 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1634 {
1635   tree t;
1636
1637   if (e == error_mark_node)
1638     return error_mark_node;
1639
1640   if (processing_template_decl)
1641     {
1642       e = build_min (ALIGNOF_EXPR, size_type_node, e);
1643       TREE_SIDE_EFFECTS (e) = 0;
1644       TREE_READONLY (e) = 1;
1645
1646       return e;
1647     }
1648
1649   e = mark_type_use (e);
1650
1651   if (TREE_CODE (e) == VAR_DECL)
1652     t = size_int (DECL_ALIGN_UNIT (e));
1653   else if (TREE_CODE (e) == COMPONENT_REF
1654            && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1655            && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1656     {
1657       if (complain & tf_error)
1658         error ("invalid application of %<__alignof%> to a bit-field");
1659       else
1660         return error_mark_node;
1661       t = size_one_node;
1662     }
1663   else if (TREE_CODE (e) == COMPONENT_REF
1664            && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1665     t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1666   else if (is_overloaded_fn (e))
1667     {
1668       if (complain & tf_error)
1669         permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1670                    "function type");
1671       else
1672         return error_mark_node;
1673       if (TREE_CODE (e) == FUNCTION_DECL)
1674         t = size_int (DECL_ALIGN_UNIT (e));
1675       else
1676         t = size_one_node;
1677     }
1678   else if (type_unknown_p (e))
1679     {
1680       if (complain & tf_error)
1681         cxx_incomplete_type_error (e, TREE_TYPE (e));
1682       else
1683         return error_mark_node;
1684       t = size_one_node;
1685     }
1686   else
1687     return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, 
1688                                        complain & tf_error);
1689
1690   return fold_convert (size_type_node, t);
1691 }
1692
1693 /* Process a sizeof or alignof expression E with code OP where the operand
1694    is an expression.  */
1695
1696 tree
1697 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1698 {
1699   if (op == SIZEOF_EXPR)
1700     return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1701   else
1702     return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1703 }
1704 \f
1705 /* EXPR is being used in a context that is not a function call.
1706    Enforce:
1707
1708      [expr.ref]
1709
1710      The expression can be used only as the left-hand operand of a
1711      member function call.
1712
1713      [expr.mptr.operator]
1714
1715      If the result of .* or ->* is a function, then that result can be
1716      used only as the operand for the function call operator ().
1717
1718    by issuing an error message if appropriate.  Returns true iff EXPR
1719    violates these rules.  */
1720
1721 bool
1722 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1723 {
1724   if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1725     {
1726       if (complain & tf_error)
1727         error ("invalid use of non-static member function");
1728       return true;
1729     }
1730   return false;
1731 }
1732
1733 /* If EXP is a reference to a bitfield, and the type of EXP does not
1734    match the declared type of the bitfield, return the declared type
1735    of the bitfield.  Otherwise, return NULL_TREE.  */
1736
1737 tree
1738 is_bitfield_expr_with_lowered_type (const_tree exp)
1739 {
1740   switch (TREE_CODE (exp))
1741     {
1742     case COND_EXPR:
1743       if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1744                                                ? TREE_OPERAND (exp, 1)
1745                                                : TREE_OPERAND (exp, 0)))
1746         return NULL_TREE;
1747       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1748
1749     case COMPOUND_EXPR:
1750       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1751
1752     case MODIFY_EXPR:
1753     case SAVE_EXPR:
1754       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1755
1756     case COMPONENT_REF:
1757       {
1758         tree field;
1759         
1760         field = TREE_OPERAND (exp, 1);
1761         if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1762           return NULL_TREE;
1763         if (same_type_ignoring_top_level_qualifiers_p
1764             (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1765           return NULL_TREE;
1766         return DECL_BIT_FIELD_TYPE (field);
1767       }
1768
1769     CASE_CONVERT:
1770       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1771           == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1772         return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1773       /* Fallthrough.  */
1774
1775     default:
1776       return NULL_TREE;
1777     }
1778 }
1779
1780 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1781    bitfield with a lowered type, the type of EXP is returned, rather
1782    than NULL_TREE.  */
1783
1784 tree
1785 unlowered_expr_type (const_tree exp)
1786 {
1787   tree type;
1788   tree etype = TREE_TYPE (exp);
1789
1790   type = is_bitfield_expr_with_lowered_type (exp);
1791   if (type)
1792     type = cp_build_qualified_type (type, cp_type_quals (etype));
1793   else
1794     type = etype;
1795
1796   return type;
1797 }
1798
1799 /* Perform the conversions in [expr] that apply when an lvalue appears
1800    in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1801    function-to-pointer conversions.  In addition, manifest constants
1802    are replaced by their values, and bitfield references are converted
1803    to their declared types. Note that this function does not perform the
1804    lvalue-to-rvalue conversion for class types. If you need that conversion
1805    to for class types, then you probably need to use force_rvalue.
1806
1807    Although the returned value is being used as an rvalue, this
1808    function does not wrap the returned expression in a
1809    NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1810    that the return value is no longer an lvalue.  */
1811
1812 tree
1813 decay_conversion (tree exp)
1814 {
1815   tree type;
1816   enum tree_code code;
1817
1818   type = TREE_TYPE (exp);
1819   if (type == error_mark_node)
1820     return error_mark_node;
1821
1822   exp = mark_rvalue_use (exp);
1823
1824   exp = resolve_nondeduced_context (exp);
1825   if (type_unknown_p (exp))
1826     {
1827       cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1828       return error_mark_node;
1829     }
1830
1831   /* FIXME remove? at least need to remember that this isn't really a
1832      constant expression if EXP isn't decl_constant_var_p, like with
1833      C_MAYBE_CONST_EXPR.  */
1834   exp = decl_constant_value_safe (exp);
1835   if (error_operand_p (exp))
1836     return error_mark_node;
1837
1838   if (NULLPTR_TYPE_P (type))
1839     return nullptr_node;
1840
1841   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1842      Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
1843   code = TREE_CODE (type);
1844   if (code == VOID_TYPE)
1845     {
1846       error ("void value not ignored as it ought to be");
1847       return error_mark_node;
1848     }
1849   if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1850     return error_mark_node;
1851   if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1852     return cp_build_addr_expr (exp, tf_warning_or_error);
1853   if (code == ARRAY_TYPE)
1854     {
1855       tree adr;
1856       tree ptrtype;
1857
1858       if (TREE_CODE (exp) == INDIRECT_REF)
1859         return build_nop (build_pointer_type (TREE_TYPE (type)),
1860                           TREE_OPERAND (exp, 0));
1861
1862       if (TREE_CODE (exp) == COMPOUND_EXPR)
1863         {
1864           tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1865           return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1866                          TREE_OPERAND (exp, 0), op1);
1867         }
1868
1869       if (!lvalue_p (exp)
1870           && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1871         {
1872           error ("invalid use of non-lvalue array");
1873           return error_mark_node;
1874         }
1875
1876       ptrtype = build_pointer_type (TREE_TYPE (type));
1877
1878       if (TREE_CODE (exp) == VAR_DECL)
1879         {
1880           if (!cxx_mark_addressable (exp))
1881             return error_mark_node;
1882           adr = build_nop (ptrtype, build_address (exp));
1883           return adr;
1884         }
1885       /* This way is better for a COMPONENT_REF since it can
1886          simplify the offset for a component.  */
1887       adr = cp_build_addr_expr (exp, tf_warning_or_error);
1888       return cp_convert (ptrtype, adr);
1889     }
1890
1891   /* If a bitfield is used in a context where integral promotion
1892      applies, then the caller is expected to have used
1893      default_conversion.  That function promotes bitfields correctly
1894      before calling this function.  At this point, if we have a
1895      bitfield referenced, we may assume that is not subject to
1896      promotion, and that, therefore, the type of the resulting rvalue
1897      is the declared type of the bitfield.  */
1898   exp = convert_bitfield_to_declared_type (exp);
1899
1900   /* We do not call rvalue() here because we do not want to wrap EXP
1901      in a NON_LVALUE_EXPR.  */
1902
1903   /* [basic.lval]
1904
1905      Non-class rvalues always have cv-unqualified types.  */
1906   type = TREE_TYPE (exp);
1907   if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1908     exp = build_nop (cv_unqualified (type), exp);
1909
1910   return exp;
1911 }
1912
1913 /* Perform preparatory conversions, as part of the "usual arithmetic
1914    conversions".  In particular, as per [expr]:
1915
1916      Whenever an lvalue expression appears as an operand of an
1917      operator that expects the rvalue for that operand, the
1918      lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1919      standard conversions are applied to convert the expression to an
1920      rvalue.
1921
1922    In addition, we perform integral promotions here, as those are
1923    applied to both operands to a binary operator before determining
1924    what additional conversions should apply.  */
1925
1926 tree
1927 default_conversion (tree exp)
1928 {
1929   /* Check for target-specific promotions.  */
1930   tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1931   if (promoted_type)
1932     exp = cp_convert (promoted_type, exp);
1933   /* Perform the integral promotions first so that bitfield
1934      expressions (which may promote to "int", even if the bitfield is
1935      declared "unsigned") are promoted correctly.  */
1936   else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1937     exp = perform_integral_promotions (exp);
1938   /* Perform the other conversions.  */
1939   exp = decay_conversion (exp);
1940
1941   return exp;
1942 }
1943
1944 /* EXPR is an expression with an integral or enumeration type.
1945    Perform the integral promotions in [conv.prom], and return the
1946    converted value.  */
1947
1948 tree
1949 perform_integral_promotions (tree expr)
1950 {
1951   tree type;
1952   tree promoted_type;
1953
1954   expr = mark_rvalue_use (expr);
1955
1956   /* [conv.prom]
1957
1958      If the bitfield has an enumerated type, it is treated as any
1959      other value of that type for promotion purposes.  */
1960   type = is_bitfield_expr_with_lowered_type (expr);
1961   if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1962     type = TREE_TYPE (expr);
1963   gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1964   /* Scoped enums don't promote.  */
1965   if (SCOPED_ENUM_P (type))
1966     return expr;
1967   promoted_type = type_promotes_to (type);
1968   if (type != promoted_type)
1969     expr = cp_convert (promoted_type, expr);
1970   return expr;
1971 }
1972
1973 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1974    decay_conversion to one.  */
1975
1976 int
1977 string_conv_p (const_tree totype, const_tree exp, int warn)
1978 {
1979   tree t;
1980
1981   if (TREE_CODE (totype) != POINTER_TYPE)
1982     return 0;
1983
1984   t = TREE_TYPE (totype);
1985   if (!same_type_p (t, char_type_node)
1986       && !same_type_p (t, char16_type_node)
1987       && !same_type_p (t, char32_type_node)
1988       && !same_type_p (t, wchar_type_node))
1989     return 0;
1990
1991   if (TREE_CODE (exp) == STRING_CST)
1992     {
1993       /* Make sure that we don't try to convert between char and wide chars.  */
1994       if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1995         return 0;
1996     }
1997   else
1998     {
1999       /* Is this a string constant which has decayed to 'const char *'?  */
2000       t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2001       if (!same_type_p (TREE_TYPE (exp), t))
2002         return 0;
2003       STRIP_NOPS (exp);
2004       if (TREE_CODE (exp) != ADDR_EXPR
2005           || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2006         return 0;
2007     }
2008
2009   /* This warning is not very useful, as it complains about printf.  */
2010   if (warn)
2011     warning (OPT_Wwrite_strings,
2012              "deprecated conversion from string constant to %qT",
2013              totype);
2014
2015   return 1;
2016 }
2017
2018 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2019    can, for example, use as an lvalue.  This code used to be in
2020    unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2021    expressions, where we're dealing with aggregates.  But now it's again only
2022    called from unary_complex_lvalue.  The case (in particular) that led to
2023    this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2024    get it there.  */
2025
2026 static tree
2027 rationalize_conditional_expr (enum tree_code code, tree t,
2028                               tsubst_flags_t complain)
2029 {
2030   /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2031      the first operand is always the one to be used if both operands
2032      are equal, so we know what conditional expression this used to be.  */
2033   if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2034     {
2035       tree op0 = TREE_OPERAND (t, 0);
2036       tree op1 = TREE_OPERAND (t, 1);
2037
2038       /* The following code is incorrect if either operand side-effects.  */
2039       gcc_assert (!TREE_SIDE_EFFECTS (op0)
2040                   && !TREE_SIDE_EFFECTS (op1));
2041       return
2042         build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2043                                                     ? LE_EXPR : GE_EXPR),
2044                                                    op0, TREE_CODE (op0),
2045                                                    op1, TREE_CODE (op1),
2046                                                    /*overload=*/NULL,
2047                                                    complain),
2048                                 cp_build_unary_op (code, op0, 0, complain),
2049                                 cp_build_unary_op (code, op1, 0, complain),
2050                                 complain);
2051     }
2052
2053   return
2054     build_conditional_expr (TREE_OPERAND (t, 0),
2055                             cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2056                                                complain),
2057                             cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2058                                                complain),
2059                             complain);
2060 }
2061
2062 /* Given the TYPE of an anonymous union field inside T, return the
2063    FIELD_DECL for the field.  If not found return NULL_TREE.  Because
2064    anonymous unions can nest, we must also search all anonymous unions
2065    that are directly reachable.  */
2066
2067 tree
2068 lookup_anon_field (tree t, tree type)
2069 {
2070   tree field;
2071
2072   for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2073     {
2074       if (TREE_STATIC (field))
2075         continue;
2076       if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2077         continue;
2078
2079       /* If we find it directly, return the field.  */
2080       if (DECL_NAME (field) == NULL_TREE
2081           && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2082         {
2083           return field;
2084         }
2085
2086       /* Otherwise, it could be nested, search harder.  */
2087       if (DECL_NAME (field) == NULL_TREE
2088           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2089         {
2090           tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2091           if (subfield)
2092             return subfield;
2093         }
2094     }
2095   return NULL_TREE;
2096 }
2097
2098 /* Build an expression representing OBJECT.MEMBER.  OBJECT is an
2099    expression; MEMBER is a DECL or baselink.  If ACCESS_PATH is
2100    non-NULL, it indicates the path to the base used to name MEMBER.
2101    If PRESERVE_REFERENCE is true, the expression returned will have
2102    REFERENCE_TYPE if the MEMBER does.  Otherwise, the expression
2103    returned will have the type referred to by the reference.
2104
2105    This function does not perform access control; that is either done
2106    earlier by the parser when the name of MEMBER is resolved to MEMBER
2107    itself, or later when overload resolution selects one of the
2108    functions indicated by MEMBER.  */
2109
2110 tree
2111 build_class_member_access_expr (tree object, tree member,
2112                                 tree access_path, bool preserve_reference,
2113                                 tsubst_flags_t complain)
2114 {
2115   tree object_type;
2116   tree member_scope;
2117   tree result = NULL_TREE;
2118
2119   if (error_operand_p (object) || error_operand_p (member))
2120     return error_mark_node;
2121
2122   gcc_assert (DECL_P (member) || BASELINK_P (member));
2123
2124   /* [expr.ref]
2125
2126      The type of the first expression shall be "class object" (of a
2127      complete type).  */
2128   object_type = TREE_TYPE (object);
2129   if (!currently_open_class (object_type)
2130       && !complete_type_or_maybe_complain (object_type, object, complain))
2131     return error_mark_node;
2132   if (!CLASS_TYPE_P (object_type))
2133     {
2134       if (complain & tf_error)
2135         {
2136           if (POINTER_TYPE_P (object_type)
2137               && CLASS_TYPE_P (TREE_TYPE (object_type)))
2138             error ("request for member %qD in %qE, which is of pointer "
2139                    "type %qT (maybe you meant to use %<->%> ?)",
2140                    member, object, object_type);
2141           else
2142             error ("request for member %qD in %qE, which is of non-class "
2143                    "type %qT", member, object, object_type);
2144         }
2145       return error_mark_node;
2146     }
2147
2148   /* The standard does not seem to actually say that MEMBER must be a
2149      member of OBJECT_TYPE.  However, that is clearly what is
2150      intended.  */
2151   if (DECL_P (member))
2152     {
2153       member_scope = DECL_CLASS_CONTEXT (member);
2154       mark_used (member);
2155       if (TREE_DEPRECATED (member))
2156         warn_deprecated_use (member, NULL_TREE);
2157     }
2158   else
2159     member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2160   /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2161      presently be the anonymous union.  Go outwards until we find a
2162      type related to OBJECT_TYPE.  */
2163   while (ANON_AGGR_TYPE_P (member_scope)
2164          && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2165                                                         object_type))
2166     member_scope = TYPE_CONTEXT (member_scope);
2167   if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2168     {
2169       if (complain & tf_error)
2170         {
2171           if (TREE_CODE (member) == FIELD_DECL)
2172             error ("invalid use of nonstatic data member %qE", member);
2173           else
2174             error ("%qD is not a member of %qT", member, object_type);
2175         }
2176       return error_mark_node;
2177     }
2178
2179   /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2180      `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only an lvalue
2181      in the front end; only _DECLs and _REFs are lvalues in the back end.  */
2182   {
2183     tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2184     if (temp)
2185       object = cp_build_indirect_ref (temp, RO_NULL, complain);
2186   }
2187
2188   /* In [expr.ref], there is an explicit list of the valid choices for
2189      MEMBER.  We check for each of those cases here.  */
2190   if (TREE_CODE (member) == VAR_DECL)
2191     {
2192       /* A static data member.  */
2193       result = member;
2194       mark_exp_read (object);
2195       /* If OBJECT has side-effects, they are supposed to occur.  */
2196       if (TREE_SIDE_EFFECTS (object))
2197         result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2198     }
2199   else if (TREE_CODE (member) == FIELD_DECL)
2200     {
2201       /* A non-static data member.  */
2202       bool null_object_p;
2203       int type_quals;
2204       tree member_type;
2205
2206       null_object_p = (TREE_CODE (object) == INDIRECT_REF
2207                        && integer_zerop (TREE_OPERAND (object, 0)));
2208
2209       /* Convert OBJECT to the type of MEMBER.  */
2210       if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2211                         TYPE_MAIN_VARIANT (member_scope)))
2212         {
2213           tree binfo;
2214           base_kind kind;
2215
2216           binfo = lookup_base (access_path ? access_path : object_type,
2217                                member_scope, ba_unique,  &kind);
2218           if (binfo == error_mark_node)
2219             return error_mark_node;
2220
2221           /* It is invalid to try to get to a virtual base of a
2222              NULL object.  The most common cause is invalid use of
2223              offsetof macro.  */
2224           if (null_object_p && kind == bk_via_virtual)
2225             {
2226               if (complain & tf_error)
2227                 {
2228                   error ("invalid access to non-static data member %qD of "
2229                          "NULL object",
2230                          member);
2231                   error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2232                 }
2233               return error_mark_node;
2234             }
2235
2236           /* Convert to the base.  */
2237           object = build_base_path (PLUS_EXPR, object, binfo,
2238                                     /*nonnull=*/1, complain);
2239           /* If we found the base successfully then we should be able
2240              to convert to it successfully.  */
2241           gcc_assert (object != error_mark_node);
2242         }
2243
2244       /* Complain about other invalid uses of offsetof, even though they will
2245          give the right answer.  Note that we complain whether or not they
2246          actually used the offsetof macro, since there's no way to know at this
2247          point.  So we just give a warning, instead of a pedwarn.  */
2248       /* Do not produce this warning for base class field references, because
2249          we know for a fact that didn't come from offsetof.  This does occur
2250          in various testsuite cases where a null object is passed where a
2251          vtable access is required.  */
2252       if (null_object_p && warn_invalid_offsetof
2253           && CLASSTYPE_NON_STD_LAYOUT (object_type)
2254           && !DECL_FIELD_IS_BASE (member)
2255           && cp_unevaluated_operand == 0
2256           && (complain & tf_warning))
2257         {
2258           warning (OPT_Winvalid_offsetof, 
2259                    "invalid access to non-static data member %qD "
2260                    " of NULL object", member);
2261           warning (OPT_Winvalid_offsetof, 
2262                    "(perhaps the %<offsetof%> macro was used incorrectly)");
2263         }
2264
2265       /* If MEMBER is from an anonymous aggregate, we have converted
2266          OBJECT so that it refers to the class containing the
2267          anonymous union.  Generate a reference to the anonymous union
2268          itself, and recur to find MEMBER.  */
2269       if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2270           /* When this code is called from build_field_call, the
2271              object already has the type of the anonymous union.
2272              That is because the COMPONENT_REF was already
2273              constructed, and was then disassembled before calling
2274              build_field_call.  After the function-call code is
2275              cleaned up, this waste can be eliminated.  */
2276           && (!same_type_ignoring_top_level_qualifiers_p
2277               (TREE_TYPE (object), DECL_CONTEXT (member))))
2278         {
2279           tree anonymous_union;
2280
2281           anonymous_union = lookup_anon_field (TREE_TYPE (object),
2282                                                DECL_CONTEXT (member));
2283           object = build_class_member_access_expr (object,
2284                                                    anonymous_union,
2285                                                    /*access_path=*/NULL_TREE,
2286                                                    preserve_reference,
2287                                                    complain);
2288         }
2289
2290       /* Compute the type of the field, as described in [expr.ref].  */
2291       type_quals = TYPE_UNQUALIFIED;
2292       member_type = TREE_TYPE (member);
2293       if (TREE_CODE (member_type) != REFERENCE_TYPE)
2294         {
2295           type_quals = (cp_type_quals (member_type)
2296                         | cp_type_quals (object_type));
2297
2298           /* A field is const (volatile) if the enclosing object, or the
2299              field itself, is const (volatile).  But, a mutable field is
2300              not const, even within a const object.  */
2301           if (DECL_MUTABLE_P (member))
2302             type_quals &= ~TYPE_QUAL_CONST;
2303           member_type = cp_build_qualified_type (member_type, type_quals);
2304         }
2305
2306       result = build3 (COMPONENT_REF, member_type, object, member,
2307                        NULL_TREE);
2308       result = fold_if_not_in_template (result);
2309
2310       /* Mark the expression const or volatile, as appropriate.  Even
2311          though we've dealt with the type above, we still have to mark the
2312          expression itself.  */
2313       if (type_quals & TYPE_QUAL_CONST)
2314         TREE_READONLY (result) = 1;
2315       if (type_quals & TYPE_QUAL_VOLATILE)
2316         TREE_THIS_VOLATILE (result) = 1;
2317     }
2318   else if (BASELINK_P (member))
2319     {
2320       /* The member is a (possibly overloaded) member function.  */
2321       tree functions;
2322       tree type;
2323
2324       /* If the MEMBER is exactly one static member function, then we
2325          know the type of the expression.  Otherwise, we must wait
2326          until overload resolution has been performed.  */
2327       functions = BASELINK_FUNCTIONS (member);
2328       if (TREE_CODE (functions) == FUNCTION_DECL
2329           && DECL_STATIC_FUNCTION_P (functions))
2330         type = TREE_TYPE (functions);
2331       else
2332         type = unknown_type_node;
2333       /* Note that we do not convert OBJECT to the BASELINK_BINFO
2334          base.  That will happen when the function is called.  */
2335       result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2336     }
2337   else if (TREE_CODE (member) == CONST_DECL)
2338     {
2339       /* The member is an enumerator.  */
2340       result = member;
2341       /* If OBJECT has side-effects, they are supposed to occur.  */
2342       if (TREE_SIDE_EFFECTS (object))
2343         result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2344                          object, result);
2345     }
2346   else
2347     {
2348       if (complain & tf_error)
2349         error ("invalid use of %qD", member);
2350       return error_mark_node;
2351     }
2352
2353   if (!preserve_reference)
2354     /* [expr.ref]
2355
2356        If E2 is declared to have type "reference to T", then ... the
2357        type of E1.E2 is T.  */
2358     result = convert_from_reference (result);
2359
2360   return result;
2361 }
2362
2363 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2364    SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type.  */
2365
2366 static tree
2367 lookup_destructor (tree object, tree scope, tree dtor_name)
2368 {
2369   tree object_type = TREE_TYPE (object);
2370   tree dtor_type = TREE_OPERAND (dtor_name, 0);
2371   tree expr;
2372
2373   if (scope && !check_dtor_name (scope, dtor_type))
2374     {
2375       error ("qualified type %qT does not match destructor name ~%qT",
2376              scope, dtor_type);
2377       return error_mark_node;
2378     }
2379   if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2380     {
2381       /* In a template, names we can't find a match for are still accepted
2382          destructor names, and we check them here.  */
2383       if (check_dtor_name (object_type, dtor_type))
2384         dtor_type = object_type;
2385       else
2386         {
2387           error ("object type %qT does not match destructor name ~%qT",
2388                  object_type, dtor_type);
2389           return error_mark_node;
2390         }
2391       
2392     }
2393   else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2394     {
2395       error ("the type being destroyed is %qT, but the destructor refers to %qT",
2396              TYPE_MAIN_VARIANT (object_type), dtor_type);
2397       return error_mark_node;
2398     }
2399   expr = lookup_member (dtor_type, complete_dtor_identifier,
2400                         /*protect=*/1, /*want_type=*/false,
2401                         tf_warning_or_error);
2402   expr = (adjust_result_of_qualified_name_lookup
2403           (expr, dtor_type, object_type));
2404   return expr;
2405 }
2406
2407 /* An expression of the form "A::template B" has been resolved to
2408    DECL.  Issue a diagnostic if B is not a template or template
2409    specialization.  */
2410
2411 void
2412 check_template_keyword (tree decl)
2413 {
2414   /* The standard says:
2415
2416       [temp.names]
2417
2418       If a name prefixed by the keyword template is not a member
2419       template, the program is ill-formed.
2420
2421      DR 228 removed the restriction that the template be a member
2422      template.
2423
2424      DR 96, if accepted would add the further restriction that explicit
2425      template arguments must be provided if the template keyword is
2426      used, but, as of 2005-10-16, that DR is still in "drafting".  If
2427      this DR is accepted, then the semantic checks here can be
2428      simplified, as the entity named must in fact be a template
2429      specialization, rather than, as at present, a set of overloaded
2430      functions containing at least one template function.  */
2431   if (TREE_CODE (decl) != TEMPLATE_DECL
2432       && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2433     {
2434       if (!is_overloaded_fn (decl))
2435         permerror (input_location, "%qD is not a template", decl);
2436       else
2437         {
2438           tree fns;
2439           fns = decl;
2440           if (BASELINK_P (fns))
2441             fns = BASELINK_FUNCTIONS (fns);
2442           while (fns)
2443             {
2444               tree fn = OVL_CURRENT (fns);
2445               if (TREE_CODE (fn) == TEMPLATE_DECL
2446                   || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2447                 break;
2448               if (TREE_CODE (fn) == FUNCTION_DECL
2449                   && DECL_USE_TEMPLATE (fn)
2450                   && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2451                 break;
2452               fns = OVL_NEXT (fns);
2453             }
2454           if (!fns)
2455             permerror (input_location, "%qD is not a template", decl);
2456         }
2457     }
2458 }
2459
2460 /* This function is called by the parser to process a class member
2461    access expression of the form OBJECT.NAME.  NAME is a node used by
2462    the parser to represent a name; it is not yet a DECL.  It may,
2463    however, be a BASELINK where the BASELINK_FUNCTIONS is a
2464    TEMPLATE_ID_EXPR.  Templates must be looked up by the parser, and
2465    there is no reason to do the lookup twice, so the parser keeps the
2466    BASELINK.  TEMPLATE_P is true iff NAME was explicitly declared to
2467    be a template via the use of the "A::template B" syntax.  */
2468
2469 tree
2470 finish_class_member_access_expr (tree object, tree name, bool template_p,
2471                                  tsubst_flags_t complain)
2472 {
2473   tree expr;
2474   tree object_type;
2475   tree member;
2476   tree access_path = NULL_TREE;
2477   tree orig_object = object;
2478   tree orig_name = name;
2479
2480   if (object == error_mark_node || name == error_mark_node)
2481     return error_mark_node;
2482
2483   /* If OBJECT is an ObjC class instance, we must obey ObjC access rules.  */
2484   if (!objc_is_public (object, name))
2485     return error_mark_node;
2486
2487   object_type = TREE_TYPE (object);
2488
2489   if (processing_template_decl)
2490     {
2491       if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME.  */
2492           dependent_type_p (object_type)
2493           /* If NAME is just an IDENTIFIER_NODE, then the expression
2494              is dependent.  */
2495           || TREE_CODE (object) == IDENTIFIER_NODE
2496           /* If NAME is "f<args>", where either 'f' or 'args' is
2497              dependent, then the expression is dependent.  */
2498           || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2499               && dependent_template_id_p (TREE_OPERAND (name, 0),
2500                                           TREE_OPERAND (name, 1)))
2501           /* If NAME is "T::X" where "T" is dependent, then the
2502              expression is dependent.  */
2503           || (TREE_CODE (name) == SCOPE_REF
2504               && TYPE_P (TREE_OPERAND (name, 0))
2505               && dependent_type_p (TREE_OPERAND (name, 0))))
2506         return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2507       object = build_non_dependent_expr (object);
2508     }
2509   else if (c_dialect_objc ()
2510            && TREE_CODE (name) == IDENTIFIER_NODE
2511            && (expr = objc_maybe_build_component_ref (object, name)))
2512     return expr;
2513     
2514   /* [expr.ref]
2515
2516      The type of the first expression shall be "class object" (of a
2517      complete type).  */
2518   if (!currently_open_class (object_type)
2519       && !complete_type_or_maybe_complain (object_type, object, complain))
2520     return error_mark_node;
2521   if (!CLASS_TYPE_P (object_type))
2522     {
2523       if (complain & tf_error)
2524         {
2525           if (POINTER_TYPE_P (object_type)
2526               && CLASS_TYPE_P (TREE_TYPE (object_type)))
2527             error ("request for member %qD in %qE, which is of pointer "
2528                    "type %qT (maybe you meant to use %<->%> ?)",
2529                    name, object, object_type);
2530           else
2531             error ("request for member %qD in %qE, which is of non-class "
2532                    "type %qT", name, object, object_type);
2533         }
2534       return error_mark_node;
2535     }
2536
2537   if (BASELINK_P (name))
2538     /* A member function that has already been looked up.  */
2539     member = name;
2540   else
2541     {
2542       bool is_template_id = false;
2543       tree template_args = NULL_TREE;
2544       tree scope;
2545
2546       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2547         {
2548           is_template_id = true;
2549           template_args = TREE_OPERAND (name, 1);
2550           name = TREE_OPERAND (name, 0);
2551
2552           if (TREE_CODE (name) == OVERLOAD)
2553             name = DECL_NAME (get_first_fn (name));
2554           else if (DECL_P (name))
2555             name = DECL_NAME (name);
2556         }
2557
2558       if (TREE_CODE (name) == SCOPE_REF)
2559         {
2560           /* A qualified name.  The qualifying class or namespace `S'
2561              has already been looked up; it is either a TYPE or a
2562              NAMESPACE_DECL.  */
2563           scope = TREE_OPERAND (name, 0);
2564           name = TREE_OPERAND (name, 1);
2565
2566           /* If SCOPE is a namespace, then the qualified name does not
2567              name a member of OBJECT_TYPE.  */
2568           if (TREE_CODE (scope) == NAMESPACE_DECL)
2569             {
2570               if (complain & tf_error)
2571                 error ("%<%D::%D%> is not a member of %qT",
2572                        scope, name, object_type);
2573               return error_mark_node;
2574             }
2575
2576           gcc_assert (CLASS_TYPE_P (scope));
2577           gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2578                       || TREE_CODE (name) == BIT_NOT_EXPR);
2579
2580           if (constructor_name_p (name, scope))
2581             {
2582               if (complain & tf_error)
2583                 error ("cannot call constructor %<%T::%D%> directly",
2584                        scope, name);
2585               return error_mark_node;
2586             }
2587
2588           /* Find the base of OBJECT_TYPE corresponding to SCOPE.  */
2589           access_path = lookup_base (object_type, scope, ba_check, NULL);
2590           if (access_path == error_mark_node)
2591             return error_mark_node;
2592           if (!access_path)
2593             {
2594               if (complain & tf_error)
2595                 error ("%qT is not a base of %qT", scope, object_type);
2596               return error_mark_node;
2597             }
2598         }
2599       else
2600         {
2601           scope = NULL_TREE;
2602           access_path = object_type;
2603         }
2604
2605       if (TREE_CODE (name) == BIT_NOT_EXPR)
2606         member = lookup_destructor (object, scope, name);
2607       else
2608         {
2609           /* Look up the member.  */
2610           member = lookup_member (access_path, name, /*protect=*/1,
2611                                   /*want_type=*/false, complain);
2612           if (member == NULL_TREE)
2613             {
2614               if (complain & tf_error)
2615                 error ("%qD has no member named %qE",
2616                        TREE_CODE (access_path) == TREE_BINFO
2617                        ? TREE_TYPE (access_path) : object_type, name);
2618               return error_mark_node;
2619             }
2620           if (member == error_mark_node)
2621             return error_mark_node;
2622         }
2623
2624       if (is_template_id)
2625         {
2626           tree templ = member;
2627
2628           if (BASELINK_P (templ))
2629             templ = lookup_template_function (templ, template_args);
2630           else
2631             {
2632               if (complain & tf_error)
2633                 error ("%qD is not a member template function", name);
2634               return error_mark_node;
2635             }
2636         }
2637     }
2638
2639   if (TREE_DEPRECATED (member))
2640     warn_deprecated_use (member, NULL_TREE);
2641
2642   if (template_p)
2643     check_template_keyword (member);
2644
2645   expr = build_class_member_access_expr (object, member, access_path,
2646                                          /*preserve_reference=*/false,
2647                                          complain);
2648   if (processing_template_decl && expr != error_mark_node)
2649     {
2650       if (BASELINK_P (member))
2651         {
2652           if (TREE_CODE (orig_name) == SCOPE_REF)
2653             BASELINK_QUALIFIED_P (member) = 1;
2654           orig_name = member;
2655         }
2656       return build_min_non_dep (COMPONENT_REF, expr,
2657                                 orig_object, orig_name,
2658                                 NULL_TREE);
2659     }
2660
2661   return expr;
2662 }
2663
2664 /* Return an expression for the MEMBER_NAME field in the internal
2665    representation of PTRMEM, a pointer-to-member function.  (Each
2666    pointer-to-member function type gets its own RECORD_TYPE so it is
2667    more convenient to access the fields by name than by FIELD_DECL.)
2668    This routine converts the NAME to a FIELD_DECL and then creates the
2669    node for the complete expression.  */
2670
2671 tree
2672 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2673 {
2674   tree ptrmem_type;
2675   tree member;
2676   tree member_type;
2677
2678   /* This code is a stripped down version of
2679      build_class_member_access_expr.  It does not work to use that
2680      routine directly because it expects the object to be of class
2681      type.  */
2682   ptrmem_type = TREE_TYPE (ptrmem);
2683   gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2684   member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2685                           /*want_type=*/false, tf_warning_or_error);
2686   member_type = cp_build_qualified_type (TREE_TYPE (member),
2687                                          cp_type_quals (ptrmem_type));
2688   return fold_build3_loc (input_location,
2689                       COMPONENT_REF, member_type,
2690                       ptrmem, member, NULL_TREE);
2691 }
2692
2693 /* Given an expression PTR for a pointer, return an expression
2694    for the value pointed to.
2695    ERRORSTRING is the name of the operator to appear in error messages.
2696
2697    This function may need to overload OPERATOR_FNNAME.
2698    Must also handle REFERENCE_TYPEs for C++.  */
2699
2700 tree
2701 build_x_indirect_ref (tree expr, ref_operator errorstring, 
2702                       tsubst_flags_t complain)
2703 {
2704   tree orig_expr = expr;
2705   tree rval;
2706
2707   if (processing_template_decl)
2708     {
2709       /* Retain the type if we know the operand is a pointer.  */
2710       if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2711         return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2712       if (type_dependent_expression_p (expr))
2713         return build_min_nt (INDIRECT_REF, expr);
2714       expr = build_non_dependent_expr (expr);
2715     }
2716
2717   rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2718                        NULL_TREE, /*overload=*/NULL, complain);
2719   if (!rval)
2720     rval = cp_build_indirect_ref (expr, errorstring, complain);
2721
2722   if (processing_template_decl && rval != error_mark_node)
2723     return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2724   else
2725     return rval;
2726 }
2727
2728 /* Helper function called from c-common.  */
2729 tree
2730 build_indirect_ref (location_t loc ATTRIBUTE_UNUSED,
2731                     tree ptr, ref_operator errorstring)
2732 {
2733   return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2734 }
2735
2736 tree
2737 cp_build_indirect_ref (tree ptr, ref_operator errorstring, 
2738                        tsubst_flags_t complain)
2739 {
2740   tree pointer, type;
2741
2742   if (ptr == error_mark_node)
2743     return error_mark_node;
2744
2745   if (ptr == current_class_ptr)
2746     return current_class_ref;
2747
2748   pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2749              ? ptr : decay_conversion (ptr));
2750   type = TREE_TYPE (pointer);
2751
2752   if (POINTER_TYPE_P (type))
2753     {
2754       /* [expr.unary.op]
2755
2756          If the type of the expression is "pointer to T," the type
2757          of  the  result  is  "T."  */
2758       tree t = TREE_TYPE (type);
2759
2760       if (CONVERT_EXPR_P (ptr)
2761           || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2762         {
2763           /* If a warning is issued, mark it to avoid duplicates from
2764              the backend.  This only needs to be done at
2765              warn_strict_aliasing > 2.  */
2766           if (warn_strict_aliasing > 2)
2767             if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2768                                          type, TREE_OPERAND (ptr, 0)))
2769               TREE_NO_WARNING (ptr) = 1;
2770         }
2771
2772       if (VOID_TYPE_P (t))
2773         {
2774           /* A pointer to incomplete type (other than cv void) can be
2775              dereferenced [expr.unary.op]/1  */
2776           if (complain & tf_error)
2777             error ("%qT is not a pointer-to-object type", type);
2778           return error_mark_node;
2779         }
2780       else if (TREE_CODE (pointer) == ADDR_EXPR
2781                && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2782         /* The POINTER was something like `&x'.  We simplify `*&x' to
2783            `x'.  */
2784         return TREE_OPERAND (pointer, 0);
2785       else
2786         {
2787           tree ref = build1 (INDIRECT_REF, t, pointer);
2788
2789           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2790              so that we get the proper error message if the result is used
2791              to assign to.  Also, &* is supposed to be a no-op.  */
2792           TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2793           TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2794           TREE_SIDE_EFFECTS (ref)
2795             = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2796           return ref;
2797         }
2798     }
2799   else if (!(complain & tf_error))
2800     /* Don't emit any errors; we'll just return ERROR_MARK_NODE later.  */
2801     ;
2802   /* `pointer' won't be an error_mark_node if we were given a
2803      pointer to member, so it's cool to check for this here.  */
2804   else if (TYPE_PTR_TO_MEMBER_P (type))
2805     switch (errorstring)
2806       {
2807          case RO_ARRAY_INDEXING:
2808            error ("invalid use of array indexing on pointer to member");
2809            break;
2810          case RO_UNARY_STAR:
2811            error ("invalid use of unary %<*%> on pointer to member");
2812            break;
2813          case RO_IMPLICIT_CONVERSION:
2814            error ("invalid use of implicit conversion on pointer to member");
2815            break;
2816          default:
2817            gcc_unreachable ();
2818       }
2819   else if (pointer != error_mark_node)
2820     invalid_indirection_error (input_location, type, errorstring);
2821
2822   return error_mark_node;
2823 }
2824
2825 /* This handles expressions of the form "a[i]", which denotes
2826    an array reference.
2827
2828    This is logically equivalent in C to *(a+i), but we may do it differently.
2829    If A is a variable or a member, we generate a primitive ARRAY_REF.
2830    This avoids forcing the array out of registers, and can work on
2831    arrays that are not lvalues (for example, members of structures returned
2832    by functions).
2833
2834    If INDEX is of some user-defined type, it must be converted to
2835    integer type.  Otherwise, to make a compatible PLUS_EXPR, it
2836    will inherit the type of the array, which will be some pointer type.
2837    
2838    LOC is the location to use in building the array reference.  */
2839
2840 tree
2841 cp_build_array_ref (location_t loc, tree array, tree idx,
2842                     tsubst_flags_t complain)
2843 {
2844   tree ret;
2845
2846   if (idx == 0)
2847     {
2848       if (complain & tf_error)
2849         error_at (loc, "subscript missing in array reference");
2850       return error_mark_node;
2851     }
2852
2853   if (TREE_TYPE (array) == error_mark_node
2854       || TREE_TYPE (idx) == error_mark_node)
2855     return error_mark_node;
2856
2857   /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2858      inside it.  */
2859   switch (TREE_CODE (array))
2860     {
2861     case COMPOUND_EXPR:
2862       {
2863         tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2864                                          complain);
2865         ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2866                       TREE_OPERAND (array, 0), value);
2867         SET_EXPR_LOCATION (ret, loc);
2868         return ret;
2869       }
2870
2871     case COND_EXPR:
2872       ret = build_conditional_expr
2873               (TREE_OPERAND (array, 0),
2874                cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2875                                    complain),
2876                cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2877                                    complain),
2878                tf_warning_or_error);
2879       protected_set_expr_location (ret, loc);
2880       return ret;
2881
2882     default:
2883       break;
2884     }
2885
2886   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2887     {
2888       tree rval, type;
2889
2890       warn_array_subscript_with_type_char (idx);
2891
2892       if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2893         {
2894           if (complain & tf_error)
2895             error_at (loc, "array subscript is not an integer");
2896           return error_mark_node;
2897         }
2898
2899       /* Apply integral promotions *after* noticing character types.
2900          (It is unclear why we do these promotions -- the standard
2901          does not say that we should.  In fact, the natural thing would
2902          seem to be to convert IDX to ptrdiff_t; we're performing
2903          pointer arithmetic.)  */
2904       idx = perform_integral_promotions (idx);
2905
2906       /* An array that is indexed by a non-constant
2907          cannot be stored in a register; we must be able to do
2908          address arithmetic on its address.
2909          Likewise an array of elements of variable size.  */
2910       if (TREE_CODE (idx) != INTEGER_CST
2911           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2912               && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2913                   != INTEGER_CST)))
2914         {
2915           if (!cxx_mark_addressable (array))
2916             return error_mark_node;
2917         }
2918
2919       /* An array that is indexed by a constant value which is not within
2920          the array bounds cannot be stored in a register either; because we
2921          would get a crash in store_bit_field/extract_bit_field when trying
2922          to access a non-existent part of the register.  */
2923       if (TREE_CODE (idx) == INTEGER_CST
2924           && TYPE_DOMAIN (TREE_TYPE (array))
2925           && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2926         {
2927           if (!cxx_mark_addressable (array))
2928             return error_mark_node;
2929         }
2930
2931       if (!lvalue_p (array) && (complain & tf_error))
2932         pedwarn (loc, OPT_pedantic, 
2933                  "ISO C++ forbids subscripting non-lvalue array");
2934
2935       /* Note in C++ it is valid to subscript a `register' array, since
2936          it is valid to take the address of something with that
2937          storage specification.  */
2938       if (extra_warnings)
2939         {
2940           tree foo = array;
2941           while (TREE_CODE (foo) == COMPONENT_REF)
2942             foo = TREE_OPERAND (foo, 0);
2943           if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2944               && (complain & tf_warning))
2945             warning_at (loc, OPT_Wextra,
2946                         "subscripting array declared %<register%>");
2947         }
2948
2949       type = TREE_TYPE (TREE_TYPE (array));
2950       rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2951       /* Array ref is const/volatile if the array elements are
2952          or if the array is..  */
2953       TREE_READONLY (rval)
2954         |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2955       TREE_SIDE_EFFECTS (rval)
2956         |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2957       TREE_THIS_VOLATILE (rval)
2958         |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2959       ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2960                                           complain);
2961       protected_set_expr_location (ret, loc);
2962       return ret;
2963     }
2964
2965   {
2966     tree ar = default_conversion (array);
2967     tree ind = default_conversion (idx);
2968
2969     /* Put the integer in IND to simplify error checking.  */
2970     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2971       {
2972         tree temp = ar;
2973         ar = ind;
2974         ind = temp;
2975       }
2976
2977     if (ar == error_mark_node)
2978       return ar;
2979
2980     if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2981       {
2982         if (complain & tf_error)
2983           error_at (loc, "subscripted value is neither array nor pointer");
2984         return error_mark_node;
2985       }
2986     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2987       {
2988         if (complain & tf_error)
2989           error_at (loc, "array subscript is not an integer");
2990         return error_mark_node;
2991       }
2992
2993     warn_array_subscript_with_type_char (idx);
2994
2995     ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
2996                                                      PLUS_EXPR, ar, ind,
2997                                                      complain),
2998                                  RO_ARRAY_INDEXING,
2999                                  complain);
3000     protected_set_expr_location (ret, loc);
3001     return ret;
3002   }
3003 }
3004
3005 /* Entry point for Obj-C++.  */
3006
3007 tree
3008 build_array_ref (location_t loc, tree array, tree idx)
3009 {
3010   return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3011 }
3012 \f
3013 /* Resolve a pointer to member function.  INSTANCE is the object
3014    instance to use, if the member points to a virtual member.
3015
3016    This used to avoid checking for virtual functions if basetype
3017    has no virtual functions, according to an earlier ANSI draft.
3018    With the final ISO C++ rules, such an optimization is
3019    incorrect: A pointer to a derived member can be static_cast
3020    to pointer-to-base-member, as long as the dynamic object
3021    later has the right member.  */
3022
3023 tree
3024 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
3025 {
3026   if (TREE_CODE (function) == OFFSET_REF)
3027     function = TREE_OPERAND (function, 1);
3028
3029   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3030     {
3031       tree idx, delta, e1, e2, e3, vtbl, basetype;
3032       tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3033
3034       tree instance_ptr = *instance_ptrptr;
3035       tree instance_save_expr = 0;
3036       if (instance_ptr == error_mark_node)
3037         {
3038           if (TREE_CODE (function) == PTRMEM_CST)
3039             {
3040               /* Extracting the function address from a pmf is only
3041                  allowed with -Wno-pmf-conversions. It only works for
3042                  pmf constants.  */
3043               e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
3044               e1 = convert (fntype, e1);
3045               return e1;
3046             }
3047           else
3048             {
3049               error ("object missing in use of %qE", function);
3050               return error_mark_node;
3051             }
3052         }
3053
3054       if (TREE_SIDE_EFFECTS (instance_ptr))
3055         instance_ptr = instance_save_expr = save_expr (instance_ptr);
3056
3057       if (TREE_SIDE_EFFECTS (function))
3058         function = save_expr (function);
3059
3060       /* Start by extracting all the information from the PMF itself.  */
3061       e3 = pfn_from_ptrmemfunc (function);
3062       delta = delta_from_ptrmemfunc (function);
3063       idx = build1 (NOP_EXPR, vtable_index_type, e3);
3064       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3065         {
3066         case ptrmemfunc_vbit_in_pfn:
3067           e1 = cp_build_binary_op (input_location,
3068                                    BIT_AND_EXPR, idx, integer_one_node,
3069                                    tf_warning_or_error);
3070           idx = cp_build_binary_op (input_location,
3071                                     MINUS_EXPR, idx, integer_one_node,
3072                                     tf_warning_or_error);
3073           break;
3074
3075         case ptrmemfunc_vbit_in_delta:
3076           e1 = cp_build_binary_op (input_location,
3077                                    BIT_AND_EXPR, delta, integer_one_node,
3078                                    tf_warning_or_error);
3079           delta = cp_build_binary_op (input_location,
3080                                       RSHIFT_EXPR, delta, integer_one_node,
3081                                       tf_warning_or_error);
3082           break;
3083
3084         default:
3085           gcc_unreachable ();
3086         }
3087
3088       /* Convert down to the right base before using the instance.  A
3089          special case is that in a pointer to member of class C, C may
3090          be incomplete.  In that case, the function will of course be
3091          a member of C, and no conversion is required.  In fact,
3092          lookup_base will fail in that case, because incomplete
3093          classes do not have BINFOs.  */
3094       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3095       if (!same_type_ignoring_top_level_qualifiers_p
3096           (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3097         {
3098           basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3099                                   basetype, ba_check, NULL);
3100           instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3101                                           1, tf_warning_or_error);
3102           if (instance_ptr == error_mark_node)
3103             return error_mark_node;
3104         }
3105       /* ...and then the delta in the PMF.  */
3106       instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3107
3108       /* Hand back the adjusted 'this' argument to our caller.  */
3109       *instance_ptrptr = instance_ptr;
3110
3111       /* Next extract the vtable pointer from the object.  */
3112       vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3113                      instance_ptr);
3114       vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3115       /* If the object is not dynamic the access invokes undefined
3116          behavior.  As it is not executed in this case silence the
3117          spurious warnings it may provoke.  */
3118       TREE_NO_WARNING (vtbl) = 1;
3119
3120       /* Finally, extract the function pointer from the vtable.  */
3121       e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3122       e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3123       TREE_CONSTANT (e2) = 1;
3124
3125       /* When using function descriptors, the address of the
3126          vtable entry is treated as a function pointer.  */
3127       if (TARGET_VTABLE_USES_DESCRIPTORS)
3128         e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3129                      cp_build_addr_expr (e2, tf_warning_or_error));
3130
3131       e2 = fold_convert (TREE_TYPE (e3), e2);
3132       e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3133
3134       /* Make sure this doesn't get evaluated first inside one of the
3135          branches of the COND_EXPR.  */
3136       if (instance_save_expr)
3137         e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3138                      instance_save_expr, e1);
3139
3140       function = e1;
3141     }
3142   return function;
3143 }
3144
3145 /* Used by the C-common bits.  */
3146 tree
3147 build_function_call (location_t loc ATTRIBUTE_UNUSED, 
3148                      tree function, tree params)
3149 {
3150   return cp_build_function_call (function, params, tf_warning_or_error);
3151 }
3152
3153 /* Used by the C-common bits.  */
3154 tree
3155 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3156                          tree function, VEC(tree,gc) *params,
3157                          VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3158 {
3159   VEC(tree,gc) *orig_params = params;
3160   tree ret = cp_build_function_call_vec (function, &params,
3161                                          tf_warning_or_error);
3162
3163   /* cp_build_function_call_vec can reallocate PARAMS by adding
3164      default arguments.  That should never happen here.  Verify
3165      that.  */
3166   gcc_assert (params == orig_params);
3167
3168   return ret;
3169 }
3170
3171 /* Build a function call using a tree list of arguments.  */
3172
3173 tree
3174 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3175 {
3176   VEC(tree,gc) *vec;
3177   tree ret;
3178
3179   vec = make_tree_vector ();
3180   for (; params != NULL_TREE; params = TREE_CHAIN (params))
3181     VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3182   ret = cp_build_function_call_vec (function, &vec, complain);
3183   release_tree_vector (vec);
3184   return ret;
3185 }
3186
3187 /* Build a function call using varargs.  */
3188
3189 tree
3190 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3191 {
3192   VEC(tree,gc) *vec;
3193   va_list args;
3194   tree ret, t;
3195
3196   vec = make_tree_vector ();
3197   va_start (args, complain);
3198   for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3199     VEC_safe_push (tree, gc, vec, t);
3200   va_end (args);
3201   ret = cp_build_function_call_vec (function, &vec, complain);
3202   release_tree_vector (vec);
3203   return ret;
3204 }
3205
3206 /* Build a function call using a vector of arguments.  PARAMS may be
3207    NULL if there are no parameters.  This changes the contents of
3208    PARAMS.  */
3209
3210 tree
3211 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3212                             tsubst_flags_t complain)
3213 {
3214   tree fntype, fndecl;
3215   int is_method;
3216   tree original = function;
3217   int nargs;
3218   tree *argarray;
3219   tree parm_types;
3220   VEC(tree,gc) *allocated = NULL;
3221   tree ret;
3222
3223   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3224      expressions, like those used for ObjC messenger dispatches.  */
3225   if (params != NULL && !VEC_empty (tree, *params))
3226     function = objc_rewrite_function_call (function,
3227                                            VEC_index (tree, *params, 0));
3228
3229   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3230      Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
3231   if (TREE_CODE (function) == NOP_EXPR
3232       && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3233     function = TREE_OPERAND (function, 0);
3234
3235   if (TREE_CODE (function) == FUNCTION_DECL)
3236     {
3237       mark_used (function);
3238       fndecl = function;
3239
3240       /* Convert anything with function type to a pointer-to-function.  */
3241       if (DECL_MAIN_P (function) && (complain & tf_error))
3242         pedwarn (input_location, OPT_pedantic, 
3243                  "ISO C++ forbids calling %<::main%> from within program");
3244
3245       function = build_addr_func (function);
3246     }
3247   else
3248     {
3249       fndecl = NULL_TREE;
3250
3251       function = build_addr_func (function);
3252     }
3253
3254   if (function == error_mark_node)
3255     return error_mark_node;
3256
3257   fntype = TREE_TYPE (function);
3258
3259   if (TYPE_PTRMEMFUNC_P (fntype))
3260     {
3261       if (complain & tf_error)
3262         error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3263                "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3264                original, original);
3265       return error_mark_node;
3266     }
3267
3268   is_method = (TREE_CODE (fntype) == POINTER_TYPE
3269                && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3270
3271   if (!((TREE_CODE (fntype) == POINTER_TYPE
3272          && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3273         || is_method
3274         || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3275     {
3276       if (complain & tf_error)
3277         error ("%qE cannot be used as a function", original);
3278       return error_mark_node;
3279     }
3280
3281   /* fntype now gets the type of function pointed to.  */
3282   fntype = TREE_TYPE (fntype);
3283   parm_types = TYPE_ARG_TYPES (fntype);
3284
3285   if (params == NULL)
3286     {
3287       allocated = make_tree_vector ();
3288       params = &allocated;
3289     }
3290
3291   nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3292                              complain);
3293   if (nargs < 0)
3294     return error_mark_node;
3295
3296   argarray = VEC_address (tree, *params);
3297
3298   /* Check for errors in format strings and inappropriately
3299      null parameters.  */
3300   check_function_arguments (fntype, nargs, argarray);
3301
3302   ret = build_cxx_call (function, nargs, argarray);
3303
3304   if (allocated != NULL)
3305     release_tree_vector (allocated);
3306
3307   return ret;
3308 }
3309 \f
3310 /* Subroutine of convert_arguments.
3311    Warn about wrong number of args are genereted. */
3312
3313 static void
3314 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3315 {
3316   if (fndecl)
3317     {
3318       if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3319         {
3320           if (DECL_NAME (fndecl) == NULL_TREE
3321               || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3322             error_at (loc,
3323                       too_many_p
3324                       ? G_("too many arguments to constructor %q#D")
3325                       : G_("too few arguments to constructor %q#D"),
3326                       fndecl);
3327           else
3328             error_at (loc,
3329                       too_many_p
3330                       ? G_("too many arguments to member function %q#D")
3331                       : G_("too few arguments to member function %q#D"),
3332                       fndecl);
3333         }
3334       else
3335         error_at (loc,
3336                   too_many_p
3337                   ? G_("too many arguments to function %q#D")
3338                   : G_("too few arguments to function %q#D"),
3339                   fndecl);
3340       inform (DECL_SOURCE_LOCATION (fndecl),
3341               "declared here");
3342     }
3343   else
3344     {
3345       if (c_dialect_objc ()  &&  objc_message_selector ())
3346         error_at (loc,
3347                   too_many_p 
3348                   ? G_("too many arguments to method %q#D")
3349                   : G_("too few arguments to method %q#D"),
3350                   objc_message_selector ());
3351       else
3352         error_at (loc, too_many_p ? G_("too many arguments to function")
3353                                   : G_("too few arguments to function"));
3354     }
3355 }
3356
3357 /* Convert the actual parameter expressions in the list VALUES to the
3358    types in the list TYPELIST.  The converted expressions are stored
3359    back in the VALUES vector.
3360    If parmdecls is exhausted, or when an element has NULL as its type,
3361    perform the default conversions.
3362
3363    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
3364
3365    This is also where warnings about wrong number of args are generated.
3366
3367    Returns the actual number of arguments processed (which might be less
3368    than the length of the vector), or -1 on error.
3369
3370    In C++, unspecified trailing parameters can be filled in with their
3371    default arguments, if such were specified.  Do so here.  */
3372
3373 static int
3374 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3375                    int flags, tsubst_flags_t complain)
3376 {
3377   tree typetail;
3378   unsigned int i;
3379
3380   /* Argument passing is always copy-initialization.  */
3381   flags |= LOOKUP_ONLYCONVERTING;
3382
3383   for (i = 0, typetail = typelist;
3384        i < VEC_length (tree, *values);
3385        i++)
3386     {
3387       tree type = typetail ? TREE_VALUE (typetail) : 0;
3388       tree val = VEC_index (tree, *values, i);
3389
3390       if (val == error_mark_node || type == error_mark_node)
3391         return -1;
3392
3393       if (type == void_type_node)
3394         {
3395           if (complain & tf_error)
3396             {
3397               warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3398               return i;
3399             }
3400           else
3401             return -1;
3402         }
3403
3404       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3405          Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
3406       if (TREE_CODE (val) == NOP_EXPR
3407           && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3408           && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3409         val = TREE_OPERAND (val, 0);
3410
3411       if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3412         {
3413           if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3414               || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3415               || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3416             val = decay_conversion (val);
3417         }
3418
3419       if (val == error_mark_node)
3420         return -1;
3421
3422       if (type != 0)
3423         {
3424           /* Formal parm type is specified by a function prototype.  */
3425           tree parmval;
3426
3427           if (!COMPLETE_TYPE_P (complete_type (type)))
3428             {
3429               if (complain & tf_error)
3430                 {
3431                   if (fndecl)
3432                     error ("parameter %P of %qD has incomplete type %qT",
3433                            i, fndecl, type);
3434                   else
3435                     error ("parameter %P has incomplete type %qT", i, type);
3436                 }
3437               parmval = error_mark_node;
3438             }
3439           else
3440             {
3441               parmval = convert_for_initialization
3442                 (NULL_TREE, type, val, flags,
3443                  ICR_ARGPASS, fndecl, i, complain);
3444               parmval = convert_for_arg_passing (type, parmval);
3445             }
3446
3447           if (parmval == error_mark_node)
3448             return -1;
3449
3450           VEC_replace (tree, *values, i, parmval);
3451         }
3452       else
3453         {
3454           if (fndecl && DECL_BUILT_IN (fndecl)
3455               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3456             /* Don't do ellipsis conversion for __built_in_constant_p
3457                as this will result in spurious errors for non-trivial
3458                types.  */
3459             val = require_complete_type_sfinae (val, complain);
3460           else
3461             val = convert_arg_to_ellipsis (val);
3462
3463           VEC_replace (tree, *values, i, val);
3464         }
3465
3466       if (typetail)
3467         typetail = TREE_CHAIN (typetail);
3468     }
3469
3470   if (typetail != 0 && typetail != void_list_node)
3471     {
3472       /* See if there are default arguments that can be used.  Because
3473          we hold default arguments in the FUNCTION_TYPE (which is so
3474          wrong), we can see default parameters here from deduced
3475          contexts (and via typeof) for indirect function calls.
3476          Fortunately we know whether we have a function decl to
3477          provide default arguments in a language conformant
3478          manner.  */
3479       if (fndecl && TREE_PURPOSE (typetail)
3480           && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3481         {
3482           for (; typetail != void_list_node; ++i)
3483             {
3484               tree parmval
3485                 = convert_default_arg (TREE_VALUE (typetail),
3486                                        TREE_PURPOSE (typetail),
3487                                        fndecl, i);
3488
3489               if (parmval == error_mark_node)
3490                 return -1;
3491
3492               VEC_safe_push (tree, gc, *values, parmval);
3493               typetail = TREE_CHAIN (typetail);
3494               /* ends with `...'.  */
3495               if (typetail == NULL_TREE)
3496                 break;
3497             }
3498         }
3499       else
3500         {
3501           if (complain & tf_error)
3502             warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3503           return -1;
3504         }
3505     }
3506
3507   return (int) i;
3508 }
3509 \f
3510 /* Build a binary-operation expression, after performing default
3511    conversions on the operands.  CODE is the kind of expression to
3512    build.  ARG1 and ARG2 are the arguments.  ARG1_CODE and ARG2_CODE
3513    are the tree codes which correspond to ARG1 and ARG2 when issuing
3514    warnings about possibly misplaced parentheses.  They may differ
3515    from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3516    folding (e.g., if the parser sees "a | 1 + 1", it may call this
3517    routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3518    To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3519    ARG2_CODE as ERROR_MARK.  */
3520
3521 tree
3522 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3523                    tree arg2, enum tree_code arg2_code, tree *overload,
3524                    tsubst_flags_t complain)
3525 {
3526   tree orig_arg1;
3527   tree orig_arg2;
3528   tree expr;
3529
3530   orig_arg1 = arg1;
3531   orig_arg2 = arg2;
3532
3533   if (processing_template_decl)
3534     {
3535       if (type_dependent_expression_p (arg1)
3536           || type_dependent_expression_p (arg2))
3537         return build_min_nt (code, arg1, arg2);
3538       arg1 = build_non_dependent_expr (arg1);
3539       arg2 = build_non_dependent_expr (arg2);
3540     }
3541
3542   if (code == DOTSTAR_EXPR)
3543     expr = build_m_component_ref (arg1, arg2);
3544   else
3545     expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3546                          overload, complain);
3547
3548   /* Check for cases such as x+y<<z which users are likely to
3549      misinterpret.  But don't warn about obj << x + y, since that is a
3550      common idiom for I/O.  */
3551   if (warn_parentheses
3552       && (complain & tf_warning)
3553       && !processing_template_decl
3554       && !error_operand_p (arg1)
3555       && !error_operand_p (arg2)
3556       && (code != LSHIFT_EXPR
3557           || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3558     warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3559
3560   if (processing_template_decl && expr != error_mark_node)
3561     return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3562
3563   return expr;
3564 }
3565
3566 /* Build and return an ARRAY_REF expression.  */
3567
3568 tree
3569 build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
3570 {
3571   tree orig_arg1 = arg1;
3572   tree orig_arg2 = arg2;
3573   tree expr;
3574
3575   if (processing_template_decl)
3576     {
3577       if (type_dependent_expression_p (arg1)
3578           || type_dependent_expression_p (arg2))
3579         return build_min_nt (ARRAY_REF, arg1, arg2,
3580                              NULL_TREE, NULL_TREE);
3581       arg1 = build_non_dependent_expr (arg1);
3582       arg2 = build_non_dependent_expr (arg2);
3583     }
3584
3585   expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3586                        /*overload=*/NULL, complain);
3587
3588   if (processing_template_decl && expr != error_mark_node)
3589     return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3590                               NULL_TREE, NULL_TREE);
3591   return expr;
3592 }
3593
3594 /* For the c-common bits.  */
3595 tree
3596 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3597                  int convert_p ATTRIBUTE_UNUSED)
3598 {
3599   return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3600 }
3601
3602
3603 /* Build a binary-operation expression without default conversions.
3604    CODE is the kind of expression to build.
3605    LOCATION is the location_t of the operator in the source code.
3606    This function differs from `build' in several ways:
3607    the data type of the result is computed and recorded in it,
3608    warnings are generated if arg data types are invalid,
3609    special handling for addition and subtraction of pointers is known,
3610    and some optimization is done (operations on narrow ints
3611    are done in the narrower type when that gives the same result).
3612    Constant folding is also done before the result is returned.
3613
3614    Note that the operands will never have enumeral types
3615    because either they have just had the default conversions performed
3616    or they have both just been converted to some other type in which
3617    the arithmetic is to be done.
3618
3619    C++: must do special pointer arithmetic when implementing
3620    multiple inheritance, and deal with pointer to member functions.  */
3621
3622 tree
3623 cp_build_binary_op (location_t location,
3624                     enum tree_code code, tree orig_op0, tree orig_op1,
3625                     tsubst_flags_t complain)
3626 {
3627   tree op0, op1;
3628   enum tree_code code0, code1;
3629   tree type0, type1;
3630   const char *invalid_op_diag;
3631
3632   /* Expression code to give to the expression when it is built.
3633      Normally this is CODE, which is what the caller asked for,
3634      but in some special cases we change it.  */
3635   enum tree_code resultcode = code;
3636
3637   /* Data type in which the computation is to be performed.
3638      In the simplest cases this is the common type of the arguments.  */
3639   tree result_type = NULL;
3640
3641   /* Nonzero means operands have already been type-converted
3642      in whatever way is necessary.
3643      Zero means they need to be converted to RESULT_TYPE.  */
3644   int converted = 0;
3645
3646   /* Nonzero means create the expression with this type, rather than
3647      RESULT_TYPE.  */
3648   tree build_type = 0;
3649
3650   /* Nonzero means after finally constructing the expression
3651      convert it to this type.  */
3652   tree final_type = 0;
3653
3654   tree result;
3655
3656   /* Nonzero if this is an operation like MIN or MAX which can
3657      safely be computed in short if both args are promoted shorts.
3658      Also implies COMMON.
3659      -1 indicates a bitwise operation; this makes a difference
3660      in the exact conditions for when it is safe to do the operation
3661      in a narrower mode.  */
3662   int shorten = 0;
3663
3664   /* Nonzero if this is a comparison operation;
3665      if both args are promoted shorts, compare the original shorts.
3666      Also implies COMMON.  */
3667   int short_compare = 0;
3668
3669   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
3670   int common = 0;
3671
3672   /* True if both operands have arithmetic type.  */
3673   bool arithmetic_types_p;
3674
3675   /* Apply default conversions.  */
3676   op0 = orig_op0;
3677   op1 = orig_op1;
3678
3679   if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3680       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3681       || code == TRUTH_XOR_EXPR)
3682     {
3683       if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3684         op0 = decay_conversion (op0);
3685       if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3686         op1 = decay_conversion (op1);
3687     }
3688   else
3689     {
3690       if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3691         op0 = default_conversion (op0);
3692       if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3693         op1 = default_conversion (op1);
3694     }
3695
3696   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3697   STRIP_TYPE_NOPS (op0);
3698   STRIP_TYPE_NOPS (op1);
3699
3700   /* DTRT if one side is an overloaded function, but complain about it.  */
3701   if (type_unknown_p (op0))
3702     {
3703       tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3704       if (t != error_mark_node)
3705         {
3706           if (complain & tf_error)
3707             permerror (input_location, "assuming cast to type %qT from overloaded function",
3708                        TREE_TYPE (t));
3709           op0 = t;
3710         }
3711     }
3712   if (type_unknown_p (op1))
3713     {
3714       tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3715       if (t != error_mark_node)
3716         {
3717           if (complain & tf_error)
3718             permerror (input_location, "assuming cast to type %qT from overloaded function",
3719                        TREE_TYPE (t));
3720           op1 = t;
3721         }
3722     }
3723
3724   type0 = TREE_TYPE (op0);
3725   type1 = TREE_TYPE (op1);
3726
3727   /* The expression codes of the data types of the arguments tell us
3728      whether the arguments are integers, floating, pointers, etc.  */
3729   code0 = TREE_CODE (type0);
3730   code1 = TREE_CODE (type1);
3731
3732   /* If an error was already reported for one of the arguments,
3733      avoid reporting another error.  */
3734   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3735     return error_mark_node;
3736
3737   if ((invalid_op_diag
3738        = targetm.invalid_binary_op (code, type0, type1)))
3739     {
3740       error (invalid_op_diag);
3741       return error_mark_node;
3742     }
3743
3744   /* Issue warnings about peculiar, but valid, uses of NULL.  */
3745   if ((orig_op0 == null_node || orig_op1 == null_node)
3746       /* It's reasonable to use pointer values as operands of &&
3747          and ||, so NULL is no exception.  */
3748       && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR 
3749       && ( /* Both are NULL (or 0) and the operation was not a
3750               comparison or a pointer subtraction.  */
3751           (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) 
3752            && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR) 
3753           /* Or if one of OP0 or OP1 is neither a pointer nor NULL.  */
3754           || (!null_ptr_cst_p (orig_op0)
3755               && !TYPE_PTR_P (type0) && !TYPE_PTR_TO_MEMBER_P (type0))
3756           || (!null_ptr_cst_p (orig_op1) 
3757               && !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
3758       && (complain & tf_warning))
3759     /* Some sort of arithmetic operation involving NULL was
3760        performed.  */
3761     warning (OPT_Wpointer_arith, "NULL used in arithmetic");
3762
3763   switch (code)
3764     {
3765     case MINUS_EXPR:
3766       /* Subtraction of two similar pointers.
3767          We must subtract them as integers, then divide by object size.  */
3768       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3769           && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3770                                                         TREE_TYPE (type1)))
3771         return pointer_diff (op0, op1, common_pointer_type (type0, type1));
3772       /* In all other cases except pointer - int, the usual arithmetic
3773          rules apply.  */
3774       else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3775         {
3776           common = 1;
3777           break;
3778         }
3779       /* The pointer - int case is just like pointer + int; fall
3780          through.  */
3781     case PLUS_EXPR:
3782       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3783           && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3784         {
3785           tree ptr_operand;
3786           tree int_operand;
3787           ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3788           int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3789           if (processing_template_decl)
3790             {
3791               result_type = TREE_TYPE (ptr_operand);
3792               break;
3793             }
3794           return cp_pointer_int_sum (code,
3795                                      ptr_operand, 
3796                                      int_operand);
3797         }
3798       common = 1;
3799       break;
3800
3801     case MULT_EXPR:
3802       common = 1;
3803       break;
3804
3805     case TRUNC_DIV_EXPR:
3806     case CEIL_DIV_EXPR:
3807     case FLOOR_DIV_EXPR:
3808     case ROUND_DIV_EXPR:
3809     case EXACT_DIV_EXPR:
3810       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3811            || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3812           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3813               || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3814         {
3815           enum tree_code tcode0 = code0, tcode1 = code1;
3816
3817           warn_for_div_by_zero (location, op1);
3818
3819           if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3820             tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3821           if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3822             tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3823
3824           if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3825             resultcode = RDIV_EXPR;
3826           else
3827             /* When dividing two signed integers, we have to promote to int.
3828                unless we divide by a constant != -1.  Note that default
3829                conversion will have been performed on the operands at this
3830                point, so we have to dig out the original type to find out if
3831                it was unsigned.  */
3832             shorten = ((TREE_CODE (op0) == NOP_EXPR
3833                         && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3834                        || (TREE_CODE (op1) == INTEGER_CST
3835                            && ! integer_all_onesp (op1)));
3836
3837           common = 1;
3838         }
3839       break;
3840
3841     case BIT_AND_EXPR:
3842     case BIT_IOR_EXPR:
3843     case BIT_XOR_EXPR:
3844       if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3845           || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3846               && !VECTOR_FLOAT_TYPE_P (type0)
3847               && !VECTOR_FLOAT_TYPE_P (type1)))
3848         shorten = -1;
3849       break;
3850
3851     case TRUNC_MOD_EXPR:
3852     case FLOOR_MOD_EXPR:
3853       warn_for_div_by_zero (location, op1);
3854
3855       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3856           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3857           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3858         common = 1;
3859       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3860         {
3861           /* Although it would be tempting to shorten always here, that loses
3862              on some targets, since the modulo instruction is undefined if the
3863              quotient can't be represented in the computation mode.  We shorten
3864              only if unsigned or if dividing by something we know != -1.  */
3865           shorten = ((TREE_CODE (op0) == NOP_EXPR
3866                       && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3867                      || (TREE_CODE (op1) == INTEGER_CST
3868                          && ! integer_all_onesp (op1)));
3869           common = 1;
3870         }
3871       break;
3872
3873     case TRUTH_ANDIF_EXPR:
3874     case TRUTH_ORIF_EXPR:
3875     case TRUTH_AND_EXPR:
3876     case TRUTH_OR_EXPR:
3877       result_type = boolean_type_node;
3878       break;
3879
3880       /* Shift operations: result has same type as first operand;
3881          always convert second operand to int.
3882          Also set SHORT_SHIFT if shifting rightward.  */
3883
3884     case RSHIFT_EXPR:
3885       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3886         {
3887           result_type = type0;
3888           if (TREE_CODE (op1) == INTEGER_CST)
3889             {
3890               if (tree_int_cst_lt (op1, integer_zero_node))
3891                 {
3892                   if ((complain & tf_warning)
3893                       && c_inhibit_evaluation_warnings == 0)
3894                     warning (0, "right shift count is negative");
3895                 }
3896               else
3897                 {
3898                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
3899                       && (complain & tf_warning)
3900                       && c_inhibit_evaluation_warnings == 0)
3901                     warning (0, "right shift count >= width of type");
3902                 }
3903             }
3904           /* Convert the shift-count to an integer, regardless of
3905              size of value being shifted.  */
3906           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3907             op1 = cp_convert (integer_type_node, op1);
3908           /* Avoid converting op1 to result_type later.  */
3909           converted = 1;
3910         }
3911       break;
3912
3913     case LSHIFT_EXPR:
3914       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3915         {
3916           result_type = type0;
3917           if (TREE_CODE (op1) == INTEGER_CST)
3918             {
3919               if (tree_int_cst_lt (op1, integer_zero_node))
3920                 {
3921                   if ((complain & tf_warning)
3922                       && c_inhibit_evaluation_warnings == 0)
3923                     warning (0, "left shift count is negative");
3924                 }
3925               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3926                 {
3927                   if ((complain & tf_warning)
3928                       && c_inhibit_evaluation_warnings == 0)
3929                     warning (0, "left shift count >= width of type");
3930                 }
3931             }
3932           /* Convert the shift-count to an integer, regardless of
3933              size of value being shifted.  */
3934           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3935             op1 = cp_convert (integer_type_node, op1);
3936           /* Avoid converting op1 to result_type later.  */
3937           converted = 1;
3938         }
3939       break;
3940
3941     case RROTATE_EXPR:
3942     case LROTATE_EXPR:
3943       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3944         {
3945           result_type = type0;
3946           if (TREE_CODE (op1) == INTEGER_CST)
3947             {
3948               if (tree_int_cst_lt (op1, integer_zero_node))
3949                 {
3950                   if (complain & tf_warning)
3951                     warning (0, (code == LROTATE_EXPR)
3952                                   ? G_("left rotate count is negative")
3953                                   : G_("right rotate count is negative"));
3954                 }
3955               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3956                 {
3957                   if (complain & tf_warning)
3958                     warning (0, (code == LROTATE_EXPR) 
3959                                   ? G_("left rotate count >= width of type")
3960                                   : G_("right rotate count >= width of type"));
3961                 }
3962             }
3963           /* Convert the shift-count to an integer, regardless of
3964              size of value being shifted.  */
3965           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3966             op1 = cp_convert (integer_type_node, op1);
3967         }
3968       break;
3969
3970     case EQ_EXPR:
3971     case NE_EXPR:
3972       if ((complain & tf_warning)
3973           && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
3974         warning (OPT_Wfloat_equal,
3975                  "comparing floating point with == or != is unsafe");
3976       if ((complain & tf_warning)
3977           && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3978               || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
3979         warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
3980
3981       build_type = boolean_type_node;
3982       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3983            || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
3984           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3985               || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
3986         short_compare = 1;
3987       else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3988                || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3989         result_type = composite_pointer_type (type0, type1, op0, op1,
3990                                               CPO_COMPARISON, complain);
3991       else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3992                && null_ptr_cst_p (op1))
3993         {
3994           if (TREE_CODE (op0) == ADDR_EXPR
3995               && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
3996             {
3997               if (complain & tf_warning)
3998                 warning (OPT_Waddress, "the address of %qD will never be NULL",
3999                          TREE_OPERAND (op0, 0));
4000             }
4001           result_type = type0;
4002         }
4003       else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
4004                && null_ptr_cst_p (op0))
4005         {
4006           if (TREE_CODE (op1) == ADDR_EXPR 
4007               && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4008             {
4009               if (complain & tf_warning)
4010                 warning (OPT_Waddress, "the address of %qD will never be NULL",
4011                          TREE_OPERAND (op1, 0));
4012             }
4013           result_type = type1;
4014         }
4015       else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4016         /* One of the operands must be of nullptr_t type.  */
4017         result_type = TREE_TYPE (nullptr_node);
4018       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4019         {
4020           result_type = type0;
4021           if (complain & tf_error) 
4022             permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4023           else
4024             return error_mark_node;
4025         }
4026       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4027         {
4028           result_type = type1;
4029           if (complain & tf_error)
4030             permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4031           else
4032             return error_mark_node;
4033         }
4034       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4035         {
4036           if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4037               == ptrmemfunc_vbit_in_delta)
4038             {
4039               tree pfn0 = pfn_from_ptrmemfunc (op0);
4040               tree delta0 = delta_from_ptrmemfunc (op0);
4041               tree e1 = cp_build_binary_op (location,
4042                                             EQ_EXPR,
4043                                             pfn0,       
4044                                             build_zero_cst (TREE_TYPE (pfn0)),
4045                                             complain);
4046               tree e2 = cp_build_binary_op (location,
4047                                             BIT_AND_EXPR, 
4048                                             delta0,
4049                                             integer_one_node,
4050                                             complain);
4051               e2 = cp_build_binary_op (location,
4052                                        EQ_EXPR, e2, integer_zero_node,
4053                                        complain);
4054               op0 = cp_build_binary_op (location,
4055                                         TRUTH_ANDIF_EXPR, e1, e2,
4056                                         complain);
4057               op1 = cp_convert (TREE_TYPE (op0), integer_one_node); 
4058             }
4059           else 
4060             {
4061               op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4062               op1 = cp_convert (TREE_TYPE (op0), op1);
4063             }
4064           result_type = TREE_TYPE (op0);
4065         }
4066       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4067         return cp_build_binary_op (location, code, op1, op0, complain);
4068       else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4069         {
4070           tree type;
4071           /* E will be the final comparison.  */
4072           tree e;
4073           /* E1 and E2 are for scratch.  */
4074           tree e1;
4075           tree e2;
4076           tree pfn0;
4077           tree pfn1;
4078           tree delta0;
4079           tree delta1;
4080
4081           type = composite_pointer_type (type0, type1, op0, op1, 
4082                                          CPO_COMPARISON, complain);
4083
4084           if (!same_type_p (TREE_TYPE (op0), type))
4085             op0 = cp_convert_and_check (type, op0);
4086           if (!same_type_p (TREE_TYPE (op1), type))
4087             op1 = cp_convert_and_check (type, op1);
4088
4089           if (op0 == error_mark_node || op1 == error_mark_node)
4090             return error_mark_node;
4091
4092           if (TREE_SIDE_EFFECTS (op0))
4093             op0 = save_expr (op0);
4094           if (TREE_SIDE_EFFECTS (op1))
4095             op1 = save_expr (op1);
4096
4097           pfn0 = pfn_from_ptrmemfunc (op0);
4098           pfn1 = pfn_from_ptrmemfunc (op1);
4099           delta0 = delta_from_ptrmemfunc (op0);
4100           delta1 = delta_from_ptrmemfunc (op1);
4101           if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4102               == ptrmemfunc_vbit_in_delta)
4103             {
4104               /* We generate:
4105
4106                  (op0.pfn == op1.pfn
4107                   && ((op0.delta == op1.delta)
4108                        || (!op0.pfn && op0.delta & 1 == 0 
4109                            && op1.delta & 1 == 0))
4110
4111                  The reason for the `!op0.pfn' bit is that a NULL
4112                  pointer-to-member is any member with a zero PFN and
4113                  LSB of the DELTA field is 0.  */
4114
4115               e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4116                                        delta0, 
4117                                        integer_one_node,
4118                                        complain);
4119               e1 = cp_build_binary_op (location,
4120                                        EQ_EXPR, e1, integer_zero_node,
4121                                        complain);
4122               e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4123                                        delta1,
4124                                        integer_one_node,
4125                                        complain);
4126               e2 = cp_build_binary_op (location,
4127                                        EQ_EXPR, e2, integer_zero_node,
4128                                        complain);
4129               e1 = cp_build_binary_op (location,
4130                                        TRUTH_ANDIF_EXPR, e2, e1,
4131                                        complain);
4132               e2 = cp_build_binary_op (location, EQ_EXPR,
4133                                        pfn0,
4134                                        build_zero_cst (TREE_TYPE (pfn0)),
4135                                        complain);
4136               e2 = cp_build_binary_op (location,
4137                                        TRUTH_ANDIF_EXPR, e2, e1, complain);
4138               e1 = cp_build_binary_op (location,
4139                                        EQ_EXPR, delta0, delta1, complain);
4140               e1 = cp_build_binary_op (location,
4141                                        TRUTH_ORIF_EXPR, e1, e2, complain);
4142             }
4143           else
4144             {
4145               /* We generate:
4146
4147                  (op0.pfn == op1.pfn
4148                  && (!op0.pfn || op0.delta == op1.delta))
4149
4150                  The reason for the `!op0.pfn' bit is that a NULL
4151                  pointer-to-member is any member with a zero PFN; the
4152                  DELTA field is unspecified.  */
4153  
4154               e1 = cp_build_binary_op (location,
4155                                        EQ_EXPR, delta0, delta1, complain);
4156               e2 = cp_build_binary_op (location,
4157                                        EQ_EXPR,
4158                                        pfn0,
4159                                        build_zero_cst (TREE_TYPE (pfn0)),
4160                                        complain);
4161               e1 = cp_build_binary_op (location,
4162                                        TRUTH_ORIF_EXPR, e1, e2, complain);
4163             }
4164           e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4165           e = cp_build_binary_op (location,
4166                                   TRUTH_ANDIF_EXPR, e2, e1, complain);
4167           if (code == EQ_EXPR)
4168             return e;
4169           return cp_build_binary_op (location,
4170                                      EQ_EXPR, e, integer_zero_node, complain);
4171         }
4172       else
4173         {
4174           gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4175                       || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4176                                        type1));
4177           gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4178                       || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4179                                        type0));
4180         }
4181
4182       break;
4183
4184     case MAX_EXPR:
4185     case MIN_EXPR:
4186       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4187            && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4188         shorten = 1;
4189       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4190         result_type = composite_pointer_type (type0, type1, op0, op1,
4191                                               CPO_COMPARISON, complain);
4192       break;
4193
4194     case LE_EXPR:
4195     case GE_EXPR:
4196     case LT_EXPR:
4197     case GT_EXPR:
4198       if (TREE_CODE (orig_op0) == STRING_CST
4199           || TREE_CODE (orig_op1) == STRING_CST)
4200         {
4201           if (complain & tf_warning)
4202             warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4203         }
4204
4205       build_type = boolean_type_node;
4206       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4207            || code0 == ENUMERAL_TYPE)
4208            && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4209                || code1 == ENUMERAL_TYPE))
4210         short_compare = 1;
4211       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4212         result_type = composite_pointer_type (type0, type1, op0, op1,
4213                                               CPO_COMPARISON, complain);
4214       else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4215         {
4216           result_type = type0;
4217           if (extra_warnings && (complain & tf_warning))
4218             warning (OPT_Wextra,
4219                      "ordered comparison of pointer with integer zero");
4220         }
4221       else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4222         {
4223           result_type = type1;
4224           if (extra_warnings && (complain & tf_warning))
4225             warning (OPT_Wextra,
4226                      "ordered comparison of pointer with integer zero");
4227         }
4228       else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4229         /* One of the operands must be of nullptr_t type.  */
4230         result_type = TREE_TYPE (nullptr_node);
4231       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4232         {
4233           result_type = type0;
4234           if (complain & tf_error)
4235             permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4236           else
4237             return error_mark_node;
4238         }
4239       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4240         {
4241           result_type = type1;
4242           if (complain & tf_error)
4243             permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4244           else
4245             return error_mark_node;
4246         }
4247       break;
4248
4249     case UNORDERED_EXPR:
4250     case ORDERED_EXPR:
4251     case UNLT_EXPR:
4252     case UNLE_EXPR:
4253     case UNGT_EXPR:
4254     case UNGE_EXPR:
4255     case UNEQ_EXPR:
4256       build_type = integer_type_node;
4257       if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4258         {
4259           if (complain & tf_error)
4260             error ("unordered comparison on non-floating point argument");
4261           return error_mark_node;
4262         }
4263       common = 1;
4264       break;
4265
4266     default:
4267       break;
4268     }
4269
4270   if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4271         || code0 == ENUMERAL_TYPE)
4272        && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4273            || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4274     arithmetic_types_p = 1;
4275   else
4276     {
4277       arithmetic_types_p = 0;
4278       /* Vector arithmetic is only allowed when both sides are vectors.  */
4279       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4280         {
4281           if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4282               || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4283                                                         TREE_TYPE (type1)))
4284             {
4285               binary_op_error (location, code, type0, type1);
4286               return error_mark_node;
4287             }
4288           arithmetic_types_p = 1;
4289         }
4290     }
4291   /* Determine the RESULT_TYPE, if it is not already known.  */
4292   if (!result_type
4293       && arithmetic_types_p
4294       && (shorten || common || short_compare))
4295     {
4296       result_type = cp_common_type (type0, type1);
4297       do_warn_double_promotion (result_type, type0, type1,
4298                                 "implicit conversion from %qT to %qT "
4299                                 "to match other operand of binary "
4300                                 "expression",
4301                                 location);
4302     }
4303
4304   if (!result_type)
4305     {
4306       if (complain & tf_error)
4307         error ("invalid operands of types %qT and %qT to binary %qO",
4308                TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4309       return error_mark_node;
4310     }
4311
4312   /* If we're in a template, the only thing we need to know is the
4313      RESULT_TYPE.  */
4314   if (processing_template_decl)
4315     {
4316       /* Since the middle-end checks the type when doing a build2, we
4317          need to build the tree in pieces.  This built tree will never
4318          get out of the front-end as we replace it when instantiating
4319          the template.  */
4320       tree tmp = build2 (resultcode,
4321                          build_type ? build_type : result_type,
4322                          NULL_TREE, op1);
4323       TREE_OPERAND (tmp, 0) = op0;
4324       return tmp;
4325     }
4326
4327   if (arithmetic_types_p)
4328     {
4329       bool first_complex = (code0 == COMPLEX_TYPE);
4330       bool second_complex = (code1 == COMPLEX_TYPE);
4331       int none_complex = (!first_complex && !second_complex);
4332
4333       /* Adapted from patch for c/24581.  */
4334       if (first_complex != second_complex
4335           && (code == PLUS_EXPR
4336               || code == MINUS_EXPR
4337               || code == MULT_EXPR
4338               || (code == TRUNC_DIV_EXPR && first_complex))
4339           && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4340           && flag_signed_zeros)
4341         {
4342           /* An operation on mixed real/complex operands must be
4343              handled specially, but the language-independent code can
4344              more easily optimize the plain complex arithmetic if
4345              -fno-signed-zeros.  */
4346           tree real_type = TREE_TYPE (result_type);
4347           tree real, imag;
4348           if (first_complex)
4349             {
4350               if (TREE_TYPE (op0) != result_type)
4351                 op0 = cp_convert_and_check (result_type, op0);
4352               if (TREE_TYPE (op1) != real_type)
4353                 op1 = cp_convert_and_check (real_type, op1);
4354             }
4355           else
4356             {
4357               if (TREE_TYPE (op0) != real_type)
4358                 op0 = cp_convert_and_check (real_type, op0);
4359               if (TREE_TYPE (op1) != result_type)
4360                 op1 = cp_convert_and_check (result_type, op1);
4361             }
4362           if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4363             return error_mark_node;
4364           if (first_complex)
4365             {
4366               op0 = save_expr (op0);
4367               real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4368               imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4369               switch (code)
4370                 {
4371                 case MULT_EXPR:
4372                 case TRUNC_DIV_EXPR:
4373                   op1 = save_expr (op1);
4374                   imag = build2 (resultcode, real_type, imag, op1);
4375                   /* Fall through.  */
4376                 case PLUS_EXPR:
4377                 case MINUS_EXPR:
4378                   real = build2 (resultcode, real_type, real, op1);
4379                   break;
4380                 default:
4381                   gcc_unreachable();
4382                 }
4383             }
4384           else
4385             {
4386               op1 = save_expr (op1);
4387               real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4388               imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4389               switch (code)
4390                 {
4391                 case MULT_EXPR:
4392                   op0 = save_expr (op0);
4393                   imag = build2 (resultcode, real_type, op0, imag);
4394                   /* Fall through.  */
4395                 case PLUS_EXPR:
4396                   real = build2 (resultcode, real_type, op0, real);
4397                   break;
4398                 case MINUS_EXPR:
4399                   real = build2 (resultcode, real_type, op0, real);
4400                   imag = build1 (NEGATE_EXPR, real_type, imag);
4401                   break;
4402                 default:
4403                   gcc_unreachable();
4404                 }
4405             }
4406           real = fold_if_not_in_template (real);
4407           imag = fold_if_not_in_template (imag);
4408           result = build2 (COMPLEX_EXPR, result_type, real, imag);
4409           result = fold_if_not_in_template (result);
4410           return result;
4411         }
4412
4413       /* For certain operations (which identify themselves by shorten != 0)
4414          if both args were extended from the same smaller type,
4415          do the arithmetic in that type and then extend.
4416
4417          shorten !=0 and !=1 indicates a bitwise operation.
4418          For them, this optimization is safe only if
4419          both args are zero-extended or both are sign-extended.
4420          Otherwise, we might change the result.
4421          E.g., (short)-1 | (unsigned short)-1 is (int)-1
4422          but calculated in (unsigned short) it would be (unsigned short)-1.  */
4423
4424       if (shorten && none_complex)
4425         {
4426           final_type = result_type;
4427           result_type = shorten_binary_op (result_type, op0, op1, 
4428                                            shorten == -1);
4429         }
4430
4431       /* Comparison operations are shortened too but differently.
4432          They identify themselves by setting short_compare = 1.  */
4433
4434       if (short_compare)
4435         {
4436           /* Don't write &op0, etc., because that would prevent op0
4437              from being kept in a register.
4438              Instead, make copies of the our local variables and
4439              pass the copies by reference, then copy them back afterward.  */
4440           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4441           enum tree_code xresultcode = resultcode;
4442           tree val
4443             = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4444           if (val != 0)
4445             return cp_convert (boolean_type_node, val);
4446           op0 = xop0, op1 = xop1;
4447           converted = 1;
4448           resultcode = xresultcode;
4449         }
4450
4451       if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4452           && warn_sign_compare
4453           && !TREE_NO_WARNING (orig_op0)
4454           && !TREE_NO_WARNING (orig_op1)
4455           /* Do not warn until the template is instantiated; we cannot
4456              bound the ranges of the arguments until that point.  */
4457           && !processing_template_decl
4458           && (complain & tf_warning)
4459           && c_inhibit_evaluation_warnings == 0)
4460         {
4461           warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1, 
4462                                  result_type, resultcode);
4463         }
4464     }
4465
4466   /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4467      Then the expression will be built.
4468      It will be given type FINAL_TYPE if that is nonzero;
4469      otherwise, it will be given type RESULT_TYPE.  */
4470   if (! converted)
4471     {
4472       if (TREE_TYPE (op0) != result_type)
4473         op0 = cp_convert_and_check (result_type, op0);
4474       if (TREE_TYPE (op1) != result_type)
4475         op1 = cp_convert_and_check (result_type, op1);
4476
4477       if (op0 == error_mark_node || op1 == error_mark_node)
4478         return error_mark_node;
4479     }
4480
4481   if (build_type == NULL_TREE)
4482     build_type = result_type;
4483
4484   result = build2 (resultcode, build_type, op0, op1);
4485   result = fold_if_not_in_template (result);
4486   if (final_type != 0)
4487     result = cp_convert (final_type, result);
4488
4489   if (TREE_OVERFLOW_P (result) 
4490       && !TREE_OVERFLOW_P (op0) 
4491       && !TREE_OVERFLOW_P (op1))
4492     overflow_warning (location, result);
4493
4494   return result;
4495 }
4496 \f
4497 /* Return a tree for the sum or difference (RESULTCODE says which)
4498    of pointer PTROP and integer INTOP.  */
4499
4500 static tree
4501 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4502 {
4503   tree res_type = TREE_TYPE (ptrop);
4504
4505   /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4506      in certain circumstance (when it's valid to do so).  So we need
4507      to make sure it's complete.  We don't need to check here, if we
4508      can actually complete it at all, as those checks will be done in
4509      pointer_int_sum() anyway.  */
4510   complete_type (TREE_TYPE (res_type));
4511
4512   return pointer_int_sum (input_location, resultcode, ptrop,
4513                           fold_if_not_in_template (intop));
4514 }
4515
4516 /* Return a tree for the difference of pointers OP0 and OP1.
4517    The resulting tree has type int.  */
4518
4519 static tree
4520 pointer_diff (tree op0, tree op1, tree ptrtype)
4521 {
4522   tree result;
4523   tree restype = ptrdiff_type_node;
4524   tree target_type = TREE_TYPE (ptrtype);
4525
4526   if (!complete_type_or_else (target_type, NULL_TREE))
4527     return error_mark_node;
4528
4529   if (TREE_CODE (target_type) == VOID_TYPE)
4530     permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction");
4531   if (TREE_CODE (target_type) == FUNCTION_TYPE)
4532     permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction");
4533   if (TREE_CODE (target_type) == METHOD_TYPE)
4534     permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction");
4535
4536   /* First do the subtraction as integers;
4537      then drop through to build the divide operator.  */
4538
4539   op0 = cp_build_binary_op (input_location,
4540                             MINUS_EXPR,
4541                             cp_convert (restype, op0),
4542                             cp_convert (restype, op1),
4543                             tf_warning_or_error);
4544
4545   /* This generates an error if op1 is a pointer to an incomplete type.  */
4546   if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4547     error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4548
4549   op1 = (TYPE_PTROB_P (ptrtype)
4550          ? size_in_bytes (target_type)
4551          : integer_one_node);
4552
4553   /* Do the division.  */
4554
4555   result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4556   return fold_if_not_in_template (result);
4557 }
4558 \f
4559 /* Construct and perhaps optimize a tree representation
4560    for a unary operation.  CODE, a tree_code, specifies the operation
4561    and XARG is the operand.  */
4562
4563 tree
4564 build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain)
4565 {
4566   tree orig_expr = xarg;
4567   tree exp;
4568   int ptrmem = 0;
4569
4570   if (processing_template_decl)
4571     {
4572       if (type_dependent_expression_p (xarg))
4573         return build_min_nt (code, xarg, NULL_TREE);
4574
4575       xarg = build_non_dependent_expr (xarg);
4576     }
4577
4578   exp = NULL_TREE;
4579
4580   /* [expr.unary.op] says:
4581
4582        The address of an object of incomplete type can be taken.
4583
4584      (And is just the ordinary address operator, not an overloaded
4585      "operator &".)  However, if the type is a template
4586      specialization, we must complete the type at this point so that
4587      an overloaded "operator &" will be available if required.  */
4588   if (code == ADDR_EXPR
4589       && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4590       && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4591            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4592           || (TREE_CODE (xarg) == OFFSET_REF)))
4593     /* Don't look for a function.  */;
4594   else
4595     exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
4596                         /*overload=*/NULL, complain);
4597   if (!exp && code == ADDR_EXPR)
4598     {
4599       if (is_overloaded_fn (xarg))
4600         {
4601           tree fn = get_first_fn (xarg);
4602           if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4603             {
4604               error (DECL_CONSTRUCTOR_P (fn)
4605                      ? G_("taking address of constructor %qE")
4606                      : G_("taking address of destructor %qE"),
4607                      xarg);
4608               return error_mark_node;
4609             }
4610         }
4611
4612       /* A pointer to member-function can be formed only by saying
4613          &X::mf.  */
4614       if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4615           && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4616         {
4617           if (TREE_CODE (xarg) != OFFSET_REF
4618               || !TYPE_P (TREE_OPERAND (xarg, 0)))
4619             {
4620               error ("invalid use of %qE to form a pointer-to-member-function",
4621                      xarg);
4622               if (TREE_CODE (xarg) != OFFSET_REF)
4623                 inform (input_location, "  a qualified-id is required");
4624               return error_mark_node;
4625             }
4626           else
4627             {
4628               error ("parentheses around %qE cannot be used to form a"
4629                      " pointer-to-member-function",
4630                      xarg);
4631               PTRMEM_OK_P (xarg) = 1;
4632             }
4633         }
4634
4635       if (TREE_CODE (xarg) == OFFSET_REF)
4636         {
4637           ptrmem = PTRMEM_OK_P (xarg);
4638
4639           if (!ptrmem && !flag_ms_extensions
4640               && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4641             {
4642               /* A single non-static member, make sure we don't allow a
4643                  pointer-to-member.  */
4644               xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4645                              TREE_OPERAND (xarg, 0),
4646                              ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4647               PTRMEM_OK_P (xarg) = ptrmem;
4648             }
4649         }
4650
4651       exp = cp_build_addr_expr_strict (xarg, complain);
4652     }
4653
4654   if (processing_template_decl && exp != error_mark_node)
4655     exp = build_min_non_dep (code, exp, orig_expr,
4656                              /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4657   if (TREE_CODE (exp) == ADDR_EXPR)
4658     PTRMEM_OK_P (exp) = ptrmem;
4659   return exp;
4660 }
4661
4662 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4663    constants, where a null value is represented by an INTEGER_CST of
4664    -1.  */
4665
4666 tree
4667 cp_truthvalue_conversion (tree expr)
4668 {
4669   tree type = TREE_TYPE (expr);
4670   if (TYPE_PTRMEM_P (type))
4671     return build_binary_op (EXPR_LOCATION (expr),
4672                             NE_EXPR, expr, nullptr_node, 1);
4673   else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4674     {
4675       /* With -Wzero-as-null-pointer-constant do not warn for an
4676          'if (p)' or a 'while (!p)', where p is a pointer.  */
4677       tree ret;
4678       ++c_inhibit_evaluation_warnings;
4679       ret = c_common_truthvalue_conversion (input_location, expr);
4680       --c_inhibit_evaluation_warnings;
4681       return ret;
4682     }
4683   else
4684     return c_common_truthvalue_conversion (input_location, expr);
4685 }
4686
4687 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  */
4688
4689 tree
4690 condition_conversion (tree expr)
4691 {
4692   tree t;
4693   if (processing_template_decl)
4694     return expr;
4695   t = perform_implicit_conversion_flags (boolean_type_node, expr,
4696                                          tf_warning_or_error, LOOKUP_NORMAL);
4697   t = fold_build_cleanup_point_expr (boolean_type_node, t);
4698   return t;
4699 }
4700
4701 /* Returns the address of T.  This function will fold away
4702    ADDR_EXPR of INDIRECT_REF.  */
4703
4704 tree
4705 build_address (tree t)
4706 {
4707   if (error_operand_p (t) || !cxx_mark_addressable (t))
4708     return error_mark_node;
4709   t = build_fold_addr_expr (t);
4710   if (TREE_CODE (t) != ADDR_EXPR)
4711     t = rvalue (t);
4712   return t;
4713 }
4714
4715 /* Returns the address of T with type TYPE.  */
4716
4717 tree
4718 build_typed_address (tree t, tree type)
4719 {
4720   if (error_operand_p (t) || !cxx_mark_addressable (t))
4721     return error_mark_node;
4722   t = build_fold_addr_expr_with_type (t, type);
4723   if (TREE_CODE (t) != ADDR_EXPR)
4724     t = rvalue (t);
4725   return t;
4726 }
4727
4728 /* Return a NOP_EXPR converting EXPR to TYPE.  */
4729
4730 tree
4731 build_nop (tree type, tree expr)
4732 {
4733   if (type == error_mark_node || error_operand_p (expr))
4734     return expr;
4735   return build1 (NOP_EXPR, type, expr);
4736 }
4737
4738 /* Take the address of ARG, whatever that means under C++ semantics.
4739    If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4740    and class rvalues as well.
4741
4742    Nothing should call this function directly; instead, callers should use
4743    cp_build_addr_expr or cp_build_addr_expr_strict.  */
4744
4745 static tree
4746 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4747 {
4748   tree argtype;
4749   tree val;
4750
4751   if (!arg || error_operand_p (arg))
4752     return error_mark_node;
4753
4754   arg = mark_lvalue_use (arg);
4755   argtype = lvalue_type (arg);
4756
4757   gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4758               || !IDENTIFIER_OPNAME_P (arg));
4759
4760   if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4761       && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4762     {
4763       /* They're trying to take the address of a unique non-static
4764          member function.  This is ill-formed (except in MS-land),
4765          but let's try to DTRT.
4766          Note: We only handle unique functions here because we don't
4767          want to complain if there's a static overload; non-unique
4768          cases will be handled by instantiate_type.  But we need to
4769          handle this case here to allow casts on the resulting PMF.
4770          We could defer this in non-MS mode, but it's easier to give
4771          a useful error here.  */
4772
4773       /* Inside constant member functions, the `this' pointer
4774          contains an extra const qualifier.  TYPE_MAIN_VARIANT
4775          is used here to remove this const from the diagnostics
4776          and the created OFFSET_REF.  */
4777       tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4778       tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4779       mark_used (fn);
4780
4781       if (! flag_ms_extensions)
4782         {
4783           tree name = DECL_NAME (fn);
4784           if (!(complain & tf_error))
4785             return error_mark_node;
4786           else if (current_class_type
4787                    && TREE_OPERAND (arg, 0) == current_class_ref)
4788             /* An expression like &memfn.  */
4789             permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4790                        " or parenthesized non-static member function to form"
4791                        " a pointer to member function.  Say %<&%T::%D%>",
4792                        base, name);
4793           else
4794             permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4795                        " function to form a pointer to member function."
4796                        "  Say %<&%T::%D%>",
4797                        base, name);
4798         }
4799       arg = build_offset_ref (base, fn, /*address_p=*/true);
4800     }
4801
4802   /* Uninstantiated types are all functions.  Taking the
4803      address of a function is a no-op, so just return the
4804      argument.  */
4805   if (type_unknown_p (arg))
4806     return build1 (ADDR_EXPR, unknown_type_node, arg);
4807
4808   if (TREE_CODE (arg) == OFFSET_REF)
4809     /* We want a pointer to member; bypass all the code for actually taking
4810        the address of something.  */
4811     goto offset_ref;
4812
4813   /* Anything not already handled and not a true memory reference
4814      is an error.  */
4815   if (TREE_CODE (argtype) != FUNCTION_TYPE
4816       && TREE_CODE (argtype) != METHOD_TYPE)
4817     {
4818       cp_lvalue_kind kind = lvalue_kind (arg);
4819       if (kind == clk_none)
4820         {
4821           if (complain & tf_error)
4822             lvalue_error (input_location, lv_addressof);
4823           return error_mark_node;
4824         }
4825       if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
4826         {
4827           if (!(complain & tf_error))
4828             return error_mark_node;
4829           if (kind & clk_class)
4830             /* Make this a permerror because we used to accept it.  */
4831             permerror (input_location, "taking address of temporary");
4832           else
4833             error ("taking address of xvalue (rvalue reference)");
4834         }
4835     }
4836
4837   if (TREE_CODE (argtype) == REFERENCE_TYPE)
4838     {
4839       tree type = build_pointer_type (TREE_TYPE (argtype));
4840       arg = build1 (CONVERT_EXPR, type, arg);
4841       return arg;
4842     }
4843   else if (pedantic && DECL_MAIN_P (arg))
4844     {
4845       /* ARM $3.4 */
4846       /* Apparently a lot of autoconf scripts for C++ packages do this,
4847          so only complain if -pedantic.  */
4848       if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
4849         pedwarn (input_location, OPT_pedantic,
4850                  "ISO C++ forbids taking address of function %<::main%>");
4851       else if (flag_pedantic_errors)
4852         return error_mark_node;
4853     }
4854
4855   /* Let &* cancel out to simplify resulting code.  */
4856   if (TREE_CODE (arg) == INDIRECT_REF)
4857     {
4858       /* We don't need to have `current_class_ptr' wrapped in a
4859          NON_LVALUE_EXPR node.  */
4860       if (arg == current_class_ref)
4861         return current_class_ptr;
4862
4863       arg = TREE_OPERAND (arg, 0);
4864       if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4865         {
4866           tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4867           arg = build1 (CONVERT_EXPR, type, arg);
4868         }
4869       else
4870         /* Don't let this be an lvalue.  */
4871         arg = rvalue (arg);
4872       return arg;
4873     }
4874
4875   /* ??? Cope with user tricks that amount to offsetof.  */
4876   if (TREE_CODE (argtype) != FUNCTION_TYPE
4877       && TREE_CODE (argtype) != METHOD_TYPE
4878       && argtype != unknown_type_node
4879       && (val = get_base_address (arg))
4880       && COMPLETE_TYPE_P (TREE_TYPE (val))
4881       && TREE_CODE (val) == INDIRECT_REF
4882       && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4883     {
4884       tree type = build_pointer_type (argtype);
4885       return fold_convert (type, fold_offsetof_1 (arg));
4886     }
4887
4888   /* Handle complex lvalues (when permitted)
4889      by reduction to simpler cases.  */
4890   val = unary_complex_lvalue (ADDR_EXPR, arg);
4891   if (val != 0)
4892     return val;
4893
4894   switch (TREE_CODE (arg))
4895     {
4896     CASE_CONVERT:
4897     case FLOAT_EXPR:
4898     case FIX_TRUNC_EXPR:
4899       /* Even if we're not being pedantic, we cannot allow this
4900          extension when we're instantiating in a SFINAE
4901          context.  */
4902       if (! lvalue_p (arg) && complain == tf_none)
4903         {
4904           if (complain & tf_error)
4905             permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4906           else
4907             return error_mark_node;
4908         }
4909       break;
4910
4911     case BASELINK:
4912       arg = BASELINK_FUNCTIONS (arg);
4913       /* Fall through.  */
4914
4915     case OVERLOAD:
4916       arg = OVL_CURRENT (arg);
4917       break;
4918
4919     case OFFSET_REF:
4920     offset_ref:
4921       /* Turn a reference to a non-static data member into a
4922          pointer-to-member.  */
4923       {
4924         tree type;
4925         tree t;
4926
4927         gcc_assert (PTRMEM_OK_P (arg));
4928
4929         t = TREE_OPERAND (arg, 1);
4930         if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4931           {
4932             if (complain & tf_error)
4933               error ("cannot create pointer to reference member %qD", t);
4934             return error_mark_node;
4935           }
4936
4937         type = build_ptrmem_type (context_for_name_lookup (t),
4938                                   TREE_TYPE (t));
4939         t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4940         return t;
4941       }
4942
4943     default:
4944       break;
4945     }
4946
4947   if (argtype != error_mark_node)
4948     argtype = build_pointer_type (argtype);
4949
4950   /* In a template, we are processing a non-dependent expression
4951      so we can just form an ADDR_EXPR with the correct type.  */
4952   if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
4953     {
4954       val = build_address (arg);
4955       if (TREE_CODE (arg) == OFFSET_REF)
4956         PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4957     }
4958   else if (BASELINK_P (TREE_OPERAND (arg, 1)))
4959     {
4960       tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4961
4962       /* We can only get here with a single static member
4963          function.  */
4964       gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4965                   && DECL_STATIC_FUNCTION_P (fn));
4966       mark_used (fn);
4967       val = build_address (fn);
4968       if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4969         /* Do not lose object's side effects.  */
4970         val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
4971                       TREE_OPERAND (arg, 0), val);
4972     }
4973   else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4974     {
4975       if (complain & tf_error)
4976         error ("attempt to take address of bit-field structure member %qD",
4977                TREE_OPERAND (arg, 1));
4978       return error_mark_node;
4979     }
4980   else
4981     {
4982       tree object = TREE_OPERAND (arg, 0);
4983       tree field = TREE_OPERAND (arg, 1);
4984       gcc_assert (same_type_ignoring_top_level_qualifiers_p
4985                   (TREE_TYPE (object), decl_type_context (field)));
4986       val = build_address (arg);
4987     }
4988
4989   if (TREE_CODE (argtype) == POINTER_TYPE
4990       && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4991     {
4992       build_ptrmemfunc_type (argtype);
4993       val = build_ptrmemfunc (argtype, val, 0,
4994                               /*c_cast_p=*/false,
4995                               tf_warning_or_error);
4996     }
4997
4998   return val;
4999 }
5000
5001 /* Take the address of ARG if it has one, even if it's an rvalue.  */
5002
5003 tree
5004 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5005 {
5006   return cp_build_addr_expr_1 (arg, 0, complain);
5007 }
5008
5009 /* Take the address of ARG, but only if it's an lvalue.  */
5010
5011 tree
5012 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5013 {
5014   return cp_build_addr_expr_1 (arg, 1, complain);
5015 }
5016
5017 /* C++: Must handle pointers to members.
5018
5019    Perhaps type instantiation should be extended to handle conversion
5020    from aggregates to types we don't yet know we want?  (Or are those
5021    cases typically errors which should be reported?)
5022
5023    NOCONVERT nonzero suppresses the default promotions
5024    (such as from short to int).  */
5025
5026 tree
5027 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert, 
5028                    tsubst_flags_t complain)
5029 {
5030   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
5031   tree arg = xarg;
5032   tree argtype = 0;
5033   const char *errstring = NULL;
5034   tree val;
5035   const char *invalid_op_diag;
5036
5037   if (!arg || error_operand_p (arg))
5038     return error_mark_node;
5039
5040   if ((invalid_op_diag
5041        = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5042                                     ? CONVERT_EXPR
5043                                     : code),
5044                                    TREE_TYPE (xarg))))
5045     {
5046       error (invalid_op_diag);
5047       return error_mark_node;
5048     }
5049
5050   switch (code)
5051     {
5052     case UNARY_PLUS_EXPR:
5053     case NEGATE_EXPR:
5054       {
5055         int flags = WANT_ARITH | WANT_ENUM;
5056         /* Unary plus (but not unary minus) is allowed on pointers.  */
5057         if (code == UNARY_PLUS_EXPR)
5058           flags |= WANT_POINTER;
5059         arg = build_expr_type_conversion (flags, arg, true);
5060         if (!arg)
5061           errstring = (code == NEGATE_EXPR
5062                        ? _("wrong type argument to unary minus")
5063                        : _("wrong type argument to unary plus"));
5064         else
5065           {
5066             if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5067               arg = perform_integral_promotions (arg);
5068
5069             /* Make sure the result is not an lvalue: a unary plus or minus
5070                expression is always a rvalue.  */
5071             arg = rvalue (arg);
5072           }
5073       }
5074       break;
5075
5076     case BIT_NOT_EXPR:
5077       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5078         {
5079           code = CONJ_EXPR;
5080           if (!noconvert)
5081             arg = default_conversion (arg);
5082         }
5083       else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5084                                                    | WANT_VECTOR_OR_COMPLEX,
5085                                                    arg, true)))
5086         errstring = _("wrong type argument to bit-complement");
5087       else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5088         arg = perform_integral_promotions (arg);
5089       break;
5090
5091     case ABS_EXPR:
5092       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5093         errstring = _("wrong type argument to abs");
5094       else if (!noconvert)
5095         arg = default_conversion (arg);
5096       break;
5097
5098     case CONJ_EXPR:
5099       /* Conjugating a real value is a no-op, but allow it anyway.  */
5100       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5101         errstring = _("wrong type argument to conjugation");
5102       else if (!noconvert)
5103         arg = default_conversion (arg);
5104       break;
5105
5106     case TRUTH_NOT_EXPR:
5107       arg = perform_implicit_conversion (boolean_type_node, arg,
5108                                          complain);
5109       val = invert_truthvalue_loc (input_location, arg);
5110       if (arg != error_mark_node)
5111         return val;
5112       errstring = _("in argument to unary !");
5113       break;
5114
5115     case NOP_EXPR:
5116       break;
5117
5118     case REALPART_EXPR:
5119     case IMAGPART_EXPR:
5120       arg = build_real_imag_expr (input_location, code, arg);
5121       if (arg == error_mark_node)
5122         return arg;
5123       else
5124         return fold_if_not_in_template (arg);
5125
5126     case PREINCREMENT_EXPR:
5127     case POSTINCREMENT_EXPR:
5128     case PREDECREMENT_EXPR:
5129     case POSTDECREMENT_EXPR:
5130       /* Handle complex lvalues (when permitted)
5131          by reduction to simpler cases.  */
5132
5133       val = unary_complex_lvalue (code, arg);
5134       if (val != 0)
5135         return val;
5136
5137       arg = mark_lvalue_use (arg);
5138
5139       /* Increment or decrement the real part of the value,
5140          and don't change the imaginary part.  */
5141       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5142         {
5143           tree real, imag;
5144
5145           arg = stabilize_reference (arg);
5146           real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5147           imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5148           real = cp_build_unary_op (code, real, 1, complain);
5149           if (real == error_mark_node || imag == error_mark_node)
5150             return error_mark_node;
5151           return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5152                          real, imag);
5153         }
5154
5155       /* Report invalid types.  */
5156
5157       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5158                                               arg, true)))
5159         {
5160           if (code == PREINCREMENT_EXPR)
5161             errstring = _("no pre-increment operator for type");
5162           else if (code == POSTINCREMENT_EXPR)
5163             errstring = _("no post-increment operator for type");
5164           else if (code == PREDECREMENT_EXPR)
5165             errstring = _("no pre-decrement operator for type");
5166           else
5167             errstring = _("no post-decrement operator for type");
5168           break;
5169         }
5170       else if (arg == error_mark_node)
5171         return error_mark_node;
5172
5173       /* Report something read-only.  */
5174
5175       if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5176           || TREE_READONLY (arg)) 
5177         {
5178           if (complain & tf_error)
5179             cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5180                                       || code == POSTINCREMENT_EXPR)
5181                                      ? lv_increment : lv_decrement));
5182           else
5183             return error_mark_node;
5184         }
5185
5186       {
5187         tree inc;
5188         tree declared_type = unlowered_expr_type (arg);
5189
5190         argtype = TREE_TYPE (arg);
5191
5192         /* ARM $5.2.5 last annotation says this should be forbidden.  */
5193         if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5194           {
5195             if (complain & tf_error)
5196               permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5197                          ? G_("ISO C++ forbids incrementing an enum")
5198                          : G_("ISO C++ forbids decrementing an enum"));
5199             else
5200               return error_mark_node;
5201           }
5202
5203         /* Compute the increment.  */
5204
5205         if (TREE_CODE (argtype) == POINTER_TYPE)
5206           {
5207             tree type = complete_type (TREE_TYPE (argtype));
5208
5209             if (!COMPLETE_OR_VOID_TYPE_P (type))
5210               {
5211                 if (complain & tf_error)
5212                   error (((code == PREINCREMENT_EXPR
5213                            || code == POSTINCREMENT_EXPR))
5214                          ? G_("cannot increment a pointer to incomplete type %qT")
5215                          : G_("cannot decrement a pointer to incomplete type %qT"),
5216                          TREE_TYPE (argtype));
5217                 else
5218                   return error_mark_node;
5219               }
5220             else if ((pedantic || warn_pointer_arith)
5221                      && !TYPE_PTROB_P (argtype)) 
5222               {
5223                 if (complain & tf_error)
5224                   permerror (input_location, (code == PREINCREMENT_EXPR
5225                               || code == POSTINCREMENT_EXPR)
5226                              ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5227                              : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5228                              argtype);
5229                 else
5230                   return error_mark_node;
5231               }
5232
5233             inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5234           }
5235         else
5236           inc = integer_one_node;
5237
5238         inc = cp_convert (argtype, inc);
5239
5240         /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5241            need to ask Objective-C to build the increment or decrement
5242            expression for it.  */
5243         if (objc_is_property_ref (arg))
5244           return objc_build_incr_expr_for_property_ref (input_location, code, 
5245                                                         arg, inc);      
5246
5247         /* Complain about anything else that is not a true lvalue.  */
5248         if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5249                                     || code == POSTINCREMENT_EXPR)
5250                                    ? lv_increment : lv_decrement),
5251                              complain))
5252           return error_mark_node;
5253
5254         /* Forbid using -- on `bool'.  */
5255         if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5256           {
5257             if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5258               {
5259                 if (complain & tf_error)
5260                   error ("invalid use of Boolean expression as operand "
5261                          "to %<operator--%>");
5262                 return error_mark_node;
5263               }
5264             val = boolean_increment (code, arg);
5265           }
5266         else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5267           /* An rvalue has no cv-qualifiers.  */
5268           val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5269         else
5270           val = build2 (code, TREE_TYPE (arg), arg, inc);
5271
5272         TREE_SIDE_EFFECTS (val) = 1;
5273         return val;
5274       }
5275
5276     case ADDR_EXPR:
5277       /* Note that this operation never does default_conversion
5278          regardless of NOCONVERT.  */
5279       return cp_build_addr_expr (arg, complain);
5280
5281     default:
5282       break;
5283     }
5284
5285   if (!errstring)
5286     {
5287       if (argtype == 0)
5288         argtype = TREE_TYPE (arg);
5289       return fold_if_not_in_template (build1 (code, argtype, arg));
5290     }
5291
5292   if (complain & tf_error)
5293     error ("%s", errstring);
5294   return error_mark_node;
5295 }
5296
5297 /* Hook for the c-common bits that build a unary op.  */
5298 tree
5299 build_unary_op (location_t location ATTRIBUTE_UNUSED,
5300                 enum tree_code code, tree xarg, int noconvert)
5301 {
5302   return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5303 }
5304
5305 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5306    for certain kinds of expressions which are not really lvalues
5307    but which we can accept as lvalues.
5308
5309    If ARG is not a kind of expression we can handle, return
5310    NULL_TREE.  */
5311
5312 tree
5313 unary_complex_lvalue (enum tree_code code, tree arg)
5314 {
5315   /* Inside a template, making these kinds of adjustments is
5316      pointless; we are only concerned with the type of the
5317      expression.  */
5318   if (processing_template_decl)
5319     return NULL_TREE;
5320
5321   /* Handle (a, b) used as an "lvalue".  */
5322   if (TREE_CODE (arg) == COMPOUND_EXPR)
5323     {
5324       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5325                                             tf_warning_or_error);
5326       return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5327                      TREE_OPERAND (arg, 0), real_result);
5328     }
5329
5330   /* Handle (a ? b : c) used as an "lvalue".  */
5331   if (TREE_CODE (arg) == COND_EXPR
5332       || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5333     return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5334
5335   /* Handle (a = b), (++a), and (--a) used as an "lvalue".  */
5336   if (TREE_CODE (arg) == MODIFY_EXPR
5337       || TREE_CODE (arg) == PREINCREMENT_EXPR
5338       || TREE_CODE (arg) == PREDECREMENT_EXPR)
5339     {
5340       tree lvalue = TREE_OPERAND (arg, 0);
5341       if (TREE_SIDE_EFFECTS (lvalue))
5342         {
5343           lvalue = stabilize_reference (lvalue);
5344           arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5345                         lvalue, TREE_OPERAND (arg, 1));
5346         }
5347       return unary_complex_lvalue
5348         (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5349     }
5350
5351   if (code != ADDR_EXPR)
5352     return NULL_TREE;
5353
5354   /* Handle (a = b) used as an "lvalue" for `&'.  */
5355   if (TREE_CODE (arg) == MODIFY_EXPR
5356       || TREE_CODE (arg) == INIT_EXPR)
5357     {
5358       tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5359                                             tf_warning_or_error);
5360       arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5361                     arg, real_result);
5362       TREE_NO_WARNING (arg) = 1;
5363       return arg;
5364     }
5365
5366   if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5367       || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5368       || TREE_CODE (arg) == OFFSET_REF)
5369     return NULL_TREE;
5370
5371   /* We permit compiler to make function calls returning
5372      objects of aggregate type look like lvalues.  */
5373   {
5374     tree targ = arg;
5375
5376     if (TREE_CODE (targ) == SAVE_EXPR)
5377       targ = TREE_OPERAND (targ, 0);
5378
5379     if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5380       {
5381         if (TREE_CODE (arg) == SAVE_EXPR)
5382           targ = arg;
5383         else
5384           targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5385         return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5386       }
5387
5388     if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5389       return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5390                      TREE_OPERAND (targ, 0), current_function_decl, NULL);
5391   }
5392
5393   /* Don't let anything else be handled specially.  */
5394   return NULL_TREE;
5395 }
5396 \f
5397 /* Mark EXP saying that we need to be able to take the
5398    address of it; it should not be allocated in a register.
5399    Value is true if successful.
5400
5401    C++: we do not allow `current_class_ptr' to be addressable.  */
5402
5403 bool
5404 cxx_mark_addressable (tree exp)
5405 {
5406   tree x = exp;
5407
5408   while (1)
5409     switch (TREE_CODE (x))
5410       {
5411       case ADDR_EXPR:
5412       case COMPONENT_REF:
5413       case ARRAY_REF:
5414       case REALPART_EXPR:
5415       case IMAGPART_EXPR:
5416         x = TREE_OPERAND (x, 0);
5417         break;
5418
5419       case PARM_DECL:
5420         if (x == current_class_ptr)
5421           {
5422             error ("cannot take the address of %<this%>, which is an rvalue expression");
5423             TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later.  */
5424             return true;
5425           }
5426         /* Fall through.  */
5427
5428       case VAR_DECL:
5429         /* Caller should not be trying to mark initialized
5430            constant fields addressable.  */
5431         gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5432                     || DECL_IN_AGGR_P (x) == 0
5433                     || TREE_STATIC (x)
5434                     || DECL_EXTERNAL (x));
5435         /* Fall through.  */
5436
5437       case RESULT_DECL:
5438         if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5439             && !DECL_ARTIFICIAL (x))
5440           {
5441             if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5442               {
5443                 error
5444                   ("address of explicit register variable %qD requested", x);
5445                 return false;
5446               }
5447             else if (extra_warnings)
5448               warning
5449                 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5450           }
5451         TREE_ADDRESSABLE (x) = 1;
5452         return true;
5453
5454       case CONST_DECL:
5455       case FUNCTION_DECL:
5456         TREE_ADDRESSABLE (x) = 1;
5457         return true;
5458
5459       case CONSTRUCTOR:
5460         TREE_ADDRESSABLE (x) = 1;
5461         return true;
5462
5463       case TARGET_EXPR:
5464         TREE_ADDRESSABLE (x) = 1;
5465         cxx_mark_addressable (TREE_OPERAND (x, 0));
5466         return true;
5467
5468       default:
5469         return true;
5470     }
5471 }
5472 \f
5473 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
5474
5475 tree
5476 build_x_conditional_expr (tree ifexp, tree op1, tree op2, 
5477                           tsubst_flags_t complain)
5478 {
5479   tree orig_ifexp = ifexp;
5480   tree orig_op1 = op1;
5481   tree orig_op2 = op2;
5482   tree expr;
5483
5484   if (processing_template_decl)
5485     {
5486       /* The standard says that the expression is type-dependent if
5487          IFEXP is type-dependent, even though the eventual type of the
5488          expression doesn't dependent on IFEXP.  */
5489       if (type_dependent_expression_p (ifexp)
5490           /* As a GNU extension, the middle operand may be omitted.  */
5491           || (op1 && type_dependent_expression_p (op1))
5492           || type_dependent_expression_p (op2))
5493         return build_min_nt (COND_EXPR, ifexp, op1, op2);
5494       ifexp = build_non_dependent_expr (ifexp);
5495       if (op1)
5496         op1 = build_non_dependent_expr (op1);
5497       op2 = build_non_dependent_expr (op2);
5498     }
5499
5500   expr = build_conditional_expr (ifexp, op1, op2, complain);
5501   if (processing_template_decl && expr != error_mark_node)
5502     {
5503       tree min = build_min_non_dep (COND_EXPR, expr,
5504                                     orig_ifexp, orig_op1, orig_op2);
5505       /* Remember that the result is an lvalue or xvalue.  */
5506       if (lvalue_or_rvalue_with_address_p (expr)
5507           && !lvalue_or_rvalue_with_address_p (min))
5508         TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5509                                                    !real_lvalue_p (expr));
5510       expr = convert_from_reference (min);
5511     }
5512   return expr;
5513 }
5514 \f
5515 /* Given a list of expressions, return a compound expression
5516    that performs them all and returns the value of the last of them.  */
5517
5518 tree
5519 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5520                                  tsubst_flags_t complain)
5521 {
5522   tree expr = TREE_VALUE (list);
5523
5524   if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5525       && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5526     {
5527       if (complain & tf_error)
5528         pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5529                  "non-class type must not be parenthesized");
5530       else
5531         return error_mark_node;
5532     }
5533
5534   if (TREE_CHAIN (list))
5535     {
5536       if (complain & tf_error)
5537         switch (exp)
5538           {
5539           case ELK_INIT:
5540             permerror (input_location, "expression list treated as compound "
5541                                        "expression in initializer");
5542             break;
5543           case ELK_MEM_INIT:
5544             permerror (input_location, "expression list treated as compound "
5545                                        "expression in mem-initializer");
5546             break;
5547           case ELK_FUNC_CAST:
5548             permerror (input_location, "expression list treated as compound "
5549                                        "expression in functional cast");
5550             break;
5551           default:
5552             gcc_unreachable ();
5553           }
5554       else
5555         return error_mark_node;
5556
5557       for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5558         expr = build_x_compound_expr (expr, TREE_VALUE (list), 
5559                                       complain);
5560     }
5561
5562   return expr;
5563 }
5564
5565 /* Like build_x_compound_expr_from_list, but using a VEC.  */
5566
5567 tree
5568 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg)
5569 {
5570   if (VEC_empty (tree, vec))
5571     return NULL_TREE;
5572   else if (VEC_length (tree, vec) == 1)
5573     return VEC_index (tree, vec, 0);
5574   else
5575     {
5576       tree expr;
5577       unsigned int ix;
5578       tree t;
5579
5580       if (msg != NULL)
5581         permerror (input_location,
5582                    "%s expression list treated as compound expression",
5583                    msg);
5584
5585       expr = VEC_index (tree, vec, 0);
5586       for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5587         expr = build_x_compound_expr (expr, t, tf_warning_or_error);
5588
5589       return expr;
5590     }
5591 }
5592
5593 /* Handle overloading of the ',' operator when needed.  */
5594
5595 tree
5596 build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain)
5597 {
5598   tree result;
5599   tree orig_op1 = op1;
5600   tree orig_op2 = op2;
5601
5602   if (processing_template_decl)
5603     {
5604       if (type_dependent_expression_p (op1)
5605           || type_dependent_expression_p (op2))
5606         return build_min_nt (COMPOUND_EXPR, op1, op2);
5607       op1 = build_non_dependent_expr (op1);
5608       op2 = build_non_dependent_expr (op2);
5609     }
5610
5611   result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
5612                          /*overload=*/NULL, complain);
5613   if (!result)
5614     result = cp_build_compound_expr (op1, op2, complain);
5615
5616   if (processing_template_decl && result != error_mark_node)
5617     return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5618
5619   return result;
5620 }
5621
5622 /* Like cp_build_compound_expr, but for the c-common bits.  */
5623
5624 tree
5625 build_compound_expr (location_t loc ATTRIBUTE_UNUSED, tree lhs, tree rhs)
5626 {
5627   return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5628 }
5629
5630 /* Build a compound expression.  */
5631
5632 tree
5633 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5634 {
5635   lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5636
5637   if (lhs == error_mark_node || rhs == error_mark_node)
5638     return error_mark_node;
5639
5640   if (TREE_CODE (rhs) == TARGET_EXPR)
5641     {
5642       /* If the rhs is a TARGET_EXPR, then build the compound
5643          expression inside the target_expr's initializer. This
5644          helps the compiler to eliminate unnecessary temporaries.  */
5645       tree init = TREE_OPERAND (rhs, 1);
5646
5647       init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5648       TREE_OPERAND (rhs, 1) = init;
5649
5650       return rhs;
5651     }
5652
5653   if (type_unknown_p (rhs))
5654     {
5655       error ("no context to resolve type of %qE", rhs);
5656       return error_mark_node;
5657     }
5658   
5659   return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5660 }
5661
5662 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5663    casts away constness.  CAST gives the type of cast.  Returns true
5664    if the cast is ill-formed, false if it is well-formed.
5665
5666    ??? This function warns for casting away any qualifier not just
5667    const.  We would like to specify exactly what qualifiers are casted
5668    away.
5669 */
5670
5671 static bool
5672 check_for_casting_away_constness (tree src_type, tree dest_type,
5673                                   enum tree_code cast, tsubst_flags_t complain)
5674 {
5675   /* C-style casts are allowed to cast away constness.  With
5676      WARN_CAST_QUAL, we still want to issue a warning.  */
5677   if (cast == CAST_EXPR && !warn_cast_qual)
5678     return false;
5679   
5680   if (!casts_away_constness (src_type, dest_type))
5681     return false;
5682
5683   switch (cast)
5684     {
5685     case CAST_EXPR:
5686       if (complain & tf_warning)
5687         warning (OPT_Wcast_qual,
5688                  "cast from type %qT to type %qT casts away qualifiers",
5689                  src_type, dest_type);
5690       return false;
5691       
5692     case STATIC_CAST_EXPR:
5693       if (complain & tf_error)
5694         error ("static_cast from type %qT to type %qT casts away qualifiers",
5695                src_type, dest_type);
5696       return true;
5697       
5698     case REINTERPRET_CAST_EXPR:
5699       if (complain & tf_error)
5700         error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5701                src_type, dest_type);
5702       return true;
5703
5704     default:
5705       gcc_unreachable();
5706     }
5707 }
5708
5709 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5710    (another pointer-to-member type in the same hierarchy) and return
5711    the converted expression.  If ALLOW_INVERSE_P is permitted, a
5712    pointer-to-derived may be converted to pointer-to-base; otherwise,
5713    only the other direction is permitted.  If C_CAST_P is true, this
5714    conversion is taking place as part of a C-style cast.  */
5715
5716 tree
5717 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5718                 bool c_cast_p, tsubst_flags_t complain)
5719 {
5720   if (TYPE_PTRMEM_P (type))
5721     {
5722       tree delta;
5723
5724       if (TREE_CODE (expr) == PTRMEM_CST)
5725         expr = cplus_expand_constant (expr);
5726       delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5727                                     TYPE_PTRMEM_CLASS_TYPE (type),
5728                                     allow_inverse_p,
5729                                     c_cast_p, complain);
5730       if (delta == error_mark_node)
5731         return error_mark_node;
5732
5733       if (!integer_zerop (delta))
5734         {
5735           tree cond, op1, op2;
5736
5737           cond = cp_build_binary_op (input_location,
5738                                      EQ_EXPR,
5739                                      expr,
5740                                      build_int_cst (TREE_TYPE (expr), -1),
5741                                      tf_warning_or_error);
5742           op1 = build_nop (ptrdiff_type_node, expr);
5743           op2 = cp_build_binary_op (input_location,
5744                                     PLUS_EXPR, op1, delta,
5745                                     tf_warning_or_error);
5746
5747           expr = fold_build3_loc (input_location,
5748                               COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5749                          
5750         }
5751
5752       return build_nop (type, expr);
5753     }
5754   else
5755     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5756                              allow_inverse_p, c_cast_p, complain);
5757 }
5758
5759 /* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
5760    this static_cast is being attempted as one of the possible casts
5761    allowed by a C-style cast.  (In that case, accessibility of base
5762    classes is not considered, and it is OK to cast away
5763    constness.)  Return the result of the cast.  *VALID_P is set to
5764    indicate whether or not the cast was valid.  */
5765
5766 static tree
5767 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5768                      bool *valid_p, tsubst_flags_t complain)
5769 {
5770   tree intype;
5771   tree result;
5772
5773   /* Assume the cast is valid.  */
5774   *valid_p = true;
5775
5776   intype = TREE_TYPE (expr);
5777
5778   /* Save casted types in the function's used types hash table.  */
5779   used_types_insert (type);
5780
5781   /* [expr.static.cast]
5782
5783      An lvalue of type "cv1 B", where B is a class type, can be cast
5784      to type "reference to cv2 D", where D is a class derived (clause
5785      _class.derived_) from B, if a valid standard conversion from
5786      "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5787      same cv-qualification as, or greater cv-qualification than, cv1,
5788      and B is not a virtual base class of D.  */
5789   /* We check this case before checking the validity of "TYPE t =
5790      EXPR;" below because for this case:
5791
5792        struct B {};
5793        struct D : public B { D(const B&); };
5794        extern B& b;
5795        void f() { static_cast<const D&>(b); }
5796
5797      we want to avoid constructing a new D.  The standard is not
5798      completely clear about this issue, but our interpretation is
5799      consistent with other compilers.  */
5800   if (TREE_CODE (type) == REFERENCE_TYPE
5801       && CLASS_TYPE_P (TREE_TYPE (type))
5802       && CLASS_TYPE_P (intype)
5803       && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
5804       && DERIVED_FROM_P (intype, TREE_TYPE (type))
5805       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5806                       build_pointer_type (TYPE_MAIN_VARIANT
5807                                           (TREE_TYPE (type))))
5808       && (c_cast_p
5809           || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5810     {
5811       tree base;
5812
5813       /* There is a standard conversion from "D*" to "B*" even if "B"
5814          is ambiguous or inaccessible.  If this is really a
5815          static_cast, then we check both for inaccessibility and
5816          ambiguity.  However, if this is a static_cast being performed
5817          because the user wrote a C-style cast, then accessibility is
5818          not considered.  */
5819       base = lookup_base (TREE_TYPE (type), intype,
5820                           c_cast_p ? ba_unique : ba_check,
5821                           NULL);
5822
5823       /* Convert from "B*" to "D*".  This function will check that "B"
5824          is not a virtual base of "D".  */
5825       expr = build_base_path (MINUS_EXPR, build_address (expr),
5826                               base, /*nonnull=*/false, complain);
5827       /* Convert the pointer to a reference -- but then remember that
5828          there are no expressions with reference type in C++.
5829
5830          We call rvalue so that there's an actual tree code
5831          (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
5832          is a variable with the same type, the conversion would get folded
5833          away, leaving just the variable and causing lvalue_kind to give
5834          the wrong answer.  */
5835       return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
5836     }
5837
5838   /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
5839      cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)."  */
5840   if (TREE_CODE (type) == REFERENCE_TYPE
5841       && TYPE_REF_IS_RVALUE (type)
5842       && lvalue_or_rvalue_with_address_p (expr)
5843       && reference_related_p (TREE_TYPE (type), intype)
5844       && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5845     {
5846       expr = build_typed_address (expr, type);
5847       return convert_from_reference (expr);
5848     }
5849
5850   /* Resolve overloaded address here rather than once in
5851      implicit_conversion and again in the inverse code below.  */
5852   if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
5853     {
5854       expr = instantiate_type (type, expr, complain);
5855       intype = TREE_TYPE (expr);
5856     }
5857
5858   /* [expr.static.cast]
5859
5860      An expression e can be explicitly converted to a type T using a
5861      static_cast of the form static_cast<T>(e) if the declaration T
5862      t(e);" is well-formed, for some invented temporary variable
5863      t.  */
5864   result = perform_direct_initialization_if_possible (type, expr,
5865                                                       c_cast_p, complain);
5866   if (result)
5867     {
5868       result = convert_from_reference (result);
5869
5870       /* [expr.static.cast]
5871
5872          If T is a reference type, the result is an lvalue; otherwise,
5873          the result is an rvalue.  */
5874       if (TREE_CODE (type) != REFERENCE_TYPE)
5875         result = rvalue (result);
5876       return result;
5877     }
5878
5879   /* [expr.static.cast]
5880
5881      Any expression can be explicitly converted to type cv void.  */
5882   if (TREE_CODE (type) == VOID_TYPE)
5883     return convert_to_void (expr, ICV_CAST, complain);
5884
5885   /* [expr.static.cast]
5886
5887      The inverse of any standard conversion sequence (clause _conv_),
5888      other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
5889      (_conv.array_), function-to-pointer (_conv.func_), and boolean
5890      (_conv.bool_) conversions, can be performed explicitly using
5891      static_cast subject to the restriction that the explicit
5892      conversion does not cast away constness (_expr.const.cast_), and
5893      the following additional rules for specific cases:  */
5894   /* For reference, the conversions not excluded are: integral
5895      promotions, floating point promotion, integral conversions,
5896      floating point conversions, floating-integral conversions,
5897      pointer conversions, and pointer to member conversions.  */
5898   /* DR 128
5899
5900      A value of integral _or enumeration_ type can be explicitly
5901      converted to an enumeration type.  */
5902   /* The effect of all that is that any conversion between any two
5903      types which are integral, floating, or enumeration types can be
5904      performed.  */
5905   if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5906        || SCALAR_FLOAT_TYPE_P (type))
5907       && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
5908           || SCALAR_FLOAT_TYPE_P (intype)))
5909     return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
5910
5911   if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
5912       && CLASS_TYPE_P (TREE_TYPE (type))
5913       && CLASS_TYPE_P (TREE_TYPE (intype))
5914       && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
5915                                           (TREE_TYPE (intype))),
5916                       build_pointer_type (TYPE_MAIN_VARIANT
5917                                           (TREE_TYPE (type)))))
5918     {
5919       tree base;
5920
5921       if (!c_cast_p
5922           && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
5923                                                complain))
5924         return error_mark_node;
5925       base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5926                           c_cast_p ? ba_unique : ba_check,
5927                           NULL);
5928       expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
5929                               complain);
5930       return cp_fold_convert(type, expr);
5931     }
5932
5933   if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5934       || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5935     {
5936       tree c1;
5937       tree c2;
5938       tree t1;
5939       tree t2;
5940
5941       c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
5942       c2 = TYPE_PTRMEM_CLASS_TYPE (type);
5943
5944       if (TYPE_PTRMEM_P (type))
5945         {
5946           t1 = (build_ptrmem_type
5947                 (c1,
5948                  TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
5949           t2 = (build_ptrmem_type
5950                 (c2,
5951                  TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
5952         }
5953       else
5954         {
5955           t1 = intype;
5956           t2 = type;
5957         }
5958       if (can_convert (t1, t2) || can_convert (t2, t1))
5959         {
5960           if (!c_cast_p
5961               && check_for_casting_away_constness (intype, type,
5962                                                    STATIC_CAST_EXPR,
5963                                                    complain))
5964             return error_mark_node;
5965           return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
5966                                  c_cast_p, complain);
5967         }
5968     }
5969
5970   /* [expr.static.cast]
5971
5972      An rvalue of type "pointer to cv void" can be explicitly
5973      converted to a pointer to object type.  A value of type pointer
5974      to object converted to "pointer to cv void" and back to the
5975      original pointer type will have its original value.  */
5976   if (TREE_CODE (intype) == POINTER_TYPE
5977       && VOID_TYPE_P (TREE_TYPE (intype))
5978       && TYPE_PTROB_P (type))
5979     {
5980       if (!c_cast_p
5981           && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
5982                                                complain))
5983         return error_mark_node;
5984       return build_nop (type, expr);
5985     }
5986
5987   *valid_p = false;
5988   return error_mark_node;
5989 }
5990
5991 /* Return an expression representing static_cast<TYPE>(EXPR).  */
5992
5993 tree
5994 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
5995 {
5996   tree result;
5997   bool valid_p;
5998
5999   if (type == error_mark_node || expr == error_mark_node)
6000     return error_mark_node;
6001
6002   if (processing_template_decl)
6003     {
6004       expr = build_min (STATIC_CAST_EXPR, type, expr);
6005       /* We don't know if it will or will not have side effects.  */
6006       TREE_SIDE_EFFECTS (expr) = 1;
6007       return convert_from_reference (expr);
6008     }
6009
6010   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6011      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
6012   if (TREE_CODE (type) != REFERENCE_TYPE
6013       && TREE_CODE (expr) == NOP_EXPR
6014       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6015     expr = TREE_OPERAND (expr, 0);
6016
6017   result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6018                                 complain);
6019   if (valid_p)
6020     return result;
6021
6022   if (complain & tf_error)
6023     error ("invalid static_cast from type %qT to type %qT",
6024            TREE_TYPE (expr), type);
6025   return error_mark_node;
6026 }
6027
6028 /* EXPR is an expression with member function or pointer-to-member
6029    function type.  TYPE is a pointer type.  Converting EXPR to TYPE is
6030    not permitted by ISO C++, but we accept it in some modes.  If we
6031    are not in one of those modes, issue a diagnostic.  Return the
6032    converted expression.  */
6033
6034 tree
6035 convert_member_func_to_ptr (tree type, tree expr)
6036 {
6037   tree intype;
6038   tree decl;
6039
6040   intype = TREE_TYPE (expr);
6041   gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6042               || TREE_CODE (intype) == METHOD_TYPE);
6043
6044   if (pedantic || warn_pmf2ptr)
6045     pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpmf_conversions,
6046              "converting from %qT to %qT", intype, type);
6047
6048   if (TREE_CODE (intype) == METHOD_TYPE)
6049     expr = build_addr_func (expr);
6050   else if (TREE_CODE (expr) == PTRMEM_CST)
6051     expr = build_address (PTRMEM_CST_MEMBER (expr));
6052   else
6053     {
6054       decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6055       decl = build_address (decl);
6056       expr = get_member_function_from_ptrfunc (&decl, expr);
6057     }
6058
6059   return build_nop (type, expr);
6060 }
6061
6062 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6063    If C_CAST_P is true, this reinterpret cast is being done as part of
6064    a C-style cast.  If VALID_P is non-NULL, *VALID_P is set to
6065    indicate whether or not reinterpret_cast was valid.  */
6066
6067 static tree
6068 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6069                           bool *valid_p, tsubst_flags_t complain)
6070 {
6071   tree intype;
6072
6073   /* Assume the cast is invalid.  */
6074   if (valid_p)
6075     *valid_p = true;
6076
6077   if (type == error_mark_node || error_operand_p (expr))
6078     return error_mark_node;
6079
6080   intype = TREE_TYPE (expr);
6081
6082   /* Save casted types in the function's used types hash table.  */
6083   used_types_insert (type);
6084
6085   /* [expr.reinterpret.cast]
6086      An lvalue expression of type T1 can be cast to the type
6087      "reference to T2" if an expression of type "pointer to T1" can be
6088      explicitly converted to the type "pointer to T2" using a
6089      reinterpret_cast.  */
6090   if (TREE_CODE (type) == REFERENCE_TYPE)
6091     {
6092       if (! real_lvalue_p (expr))
6093         {
6094           if (complain & tf_error)
6095             error ("invalid cast of an rvalue expression of type "
6096                    "%qT to type %qT",
6097                    intype, type);
6098           return error_mark_node;
6099         }
6100
6101       /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6102          "B" are related class types; the reinterpret_cast does not
6103          adjust the pointer.  */
6104       if (TYPE_PTR_P (intype)
6105           && (complain & tf_warning)
6106           && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6107                          COMPARE_BASE | COMPARE_DERIVED)))
6108         warning (0, "casting %qT to %qT does not dereference pointer",
6109                  intype, type);
6110
6111       expr = cp_build_addr_expr (expr, complain);
6112
6113       if (warn_strict_aliasing > 2)
6114         strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6115
6116       if (expr != error_mark_node)
6117         expr = build_reinterpret_cast_1
6118           (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6119            valid_p, complain);
6120       if (expr != error_mark_node)
6121         /* cp_build_indirect_ref isn't right for rvalue refs.  */
6122         expr = convert_from_reference (fold_convert (type, expr));
6123       return expr;
6124     }
6125
6126   /* As a G++ extension, we consider conversions from member
6127      functions, and pointers to member functions to
6128      pointer-to-function and pointer-to-void types.  If
6129      -Wno-pmf-conversions has not been specified,
6130      convert_member_func_to_ptr will issue an error message.  */
6131   if ((TYPE_PTRMEMFUNC_P (intype)
6132        || TREE_CODE (intype) == METHOD_TYPE)
6133       && TYPE_PTR_P (type)
6134       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6135           || VOID_TYPE_P (TREE_TYPE (type))))
6136     return convert_member_func_to_ptr (type, expr);
6137
6138   /* If the cast is not to a reference type, the lvalue-to-rvalue,
6139      array-to-pointer, and function-to-pointer conversions are
6140      performed.  */
6141   expr = decay_conversion (expr);
6142
6143   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6144      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
6145   if (TREE_CODE (expr) == NOP_EXPR
6146       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6147     expr = TREE_OPERAND (expr, 0);
6148
6149   if (error_operand_p (expr))
6150     return error_mark_node;
6151
6152   intype = TREE_TYPE (expr);
6153
6154   /* [expr.reinterpret.cast]
6155      A pointer can be converted to any integral type large enough to
6156      hold it. ... A value of type std::nullptr_t can be converted to
6157      an integral type; the conversion has the same meaning and
6158      validity as a conversion of (void*)0 to the integral type.  */
6159   if (CP_INTEGRAL_TYPE_P (type)
6160       && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6161     {
6162       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6163         {
6164           if (complain & tf_error)
6165             permerror (input_location, "cast from %qT to %qT loses precision",
6166                        intype, type);
6167           else
6168             return error_mark_node;
6169         }
6170       if (NULLPTR_TYPE_P (intype))
6171         return build_int_cst (type, 0);
6172     }
6173   /* [expr.reinterpret.cast]
6174      A value of integral or enumeration type can be explicitly
6175      converted to a pointer.  */
6176   else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6177     /* OK */
6178     ;
6179   else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6180            || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6181     return fold_if_not_in_template (build_nop (type, expr));
6182   else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
6183            || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6184     {
6185       tree sexpr = expr;
6186
6187       if (!c_cast_p
6188           && check_for_casting_away_constness (intype, type,
6189                                                REINTERPRET_CAST_EXPR,
6190                                                complain))
6191         return error_mark_node;
6192       /* Warn about possible alignment problems.  */
6193       if (STRICT_ALIGNMENT && warn_cast_align
6194           && (complain & tf_warning)
6195           && !VOID_TYPE_P (type)
6196           && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6197           && COMPLETE_TYPE_P (TREE_TYPE (type))
6198           && COMPLETE_TYPE_P (TREE_TYPE (intype))
6199           && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6200         warning (OPT_Wcast_align, "cast from %qT to %qT "
6201                  "increases required alignment of target type", intype, type);
6202
6203       /* We need to strip nops here, because the front end likes to
6204          create (int *)&a for array-to-pointer decay, instead of &a[0].  */
6205       STRIP_NOPS (sexpr);
6206       if (warn_strict_aliasing <= 2)
6207         strict_aliasing_warning (intype, type, sexpr);
6208
6209       return fold_if_not_in_template (build_nop (type, expr));
6210     }
6211   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6212            || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6213     {
6214       if (pedantic && (complain & tf_warning))
6215         /* Only issue a warning, as we have always supported this
6216            where possible, and it is necessary in some cases.  DR 195
6217            addresses this issue, but as of 2004/10/26 is still in
6218            drafting.  */
6219         warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6220       return fold_if_not_in_template (build_nop (type, expr));
6221     }
6222   else if (TREE_CODE (type) == VECTOR_TYPE)
6223     return fold_if_not_in_template (convert_to_vector (type, expr));
6224   else if (TREE_CODE (intype) == VECTOR_TYPE
6225            && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6226     return fold_if_not_in_template (convert_to_integer (type, expr));
6227   else
6228     {
6229       if (valid_p)
6230         *valid_p = false;
6231       if (complain & tf_error)
6232         error ("invalid cast from type %qT to type %qT", intype, type);
6233       return error_mark_node;
6234     }
6235
6236   return cp_convert (type, expr);
6237 }
6238
6239 tree
6240 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6241 {
6242   if (type == error_mark_node || expr == error_mark_node)
6243     return error_mark_node;
6244
6245   if (processing_template_decl)
6246     {
6247       tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6248
6249       if (!TREE_SIDE_EFFECTS (t)
6250           && type_dependent_expression_p (expr))
6251         /* There might turn out to be side effects inside expr.  */
6252         TREE_SIDE_EFFECTS (t) = 1;
6253       return convert_from_reference (t);
6254     }
6255
6256   return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6257                                    /*valid_p=*/NULL, complain);
6258 }
6259
6260 /* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
6261    return an appropriate expression.  Otherwise, return
6262    error_mark_node.  If the cast is not valid, and COMPLAIN is true,
6263    then a diagnostic will be issued.  If VALID_P is non-NULL, we are
6264    performing a C-style cast, its value upon return will indicate
6265    whether or not the conversion succeeded.  */
6266
6267 static tree
6268 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6269                     bool *valid_p)
6270 {
6271   tree src_type;
6272   tree reference_type;
6273
6274   /* Callers are responsible for handling error_mark_node as a
6275      destination type.  */
6276   gcc_assert (dst_type != error_mark_node);
6277   /* In a template, callers should be building syntactic
6278      representations of casts, not using this machinery.  */
6279   gcc_assert (!processing_template_decl);
6280
6281   /* Assume the conversion is invalid.  */
6282   if (valid_p)
6283     *valid_p = false;
6284
6285   if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
6286     {
6287       if (complain & tf_error)
6288         error ("invalid use of const_cast with type %qT, "
6289                "which is not a pointer, "
6290                "reference, nor a pointer-to-data-member type", dst_type);
6291       return error_mark_node;
6292     }
6293
6294   if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6295     {
6296       if (complain & tf_error)
6297         error ("invalid use of const_cast with type %qT, which is a pointer "
6298                "or reference to a function type", dst_type);
6299       return error_mark_node;
6300     }
6301
6302   /* Save casted types in the function's used types hash table.  */
6303   used_types_insert (dst_type);
6304
6305   src_type = TREE_TYPE (expr);
6306   /* Expressions do not really have reference types.  */
6307   if (TREE_CODE (src_type) == REFERENCE_TYPE)
6308     src_type = TREE_TYPE (src_type);
6309
6310   /* [expr.const.cast]
6311
6312      For two object types T1 and T2, if a pointer to T1 can be explicitly
6313      converted to the type "pointer to T2" using a const_cast, then the
6314      following conversions can also be made:
6315
6316      -- an lvalue of type T1 can be explicitly converted to an lvalue of
6317      type T2 using the cast const_cast<T2&>;
6318
6319      -- a glvalue of type T1 can be explicitly converted to an xvalue of
6320      type T2 using the cast const_cast<T2&&>; and
6321
6322      -- if T1 is a class type, a prvalue of type T1 can be explicitly
6323      converted to an xvalue of type T2 using the cast const_cast<T2&&>.  */
6324
6325   if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6326     {
6327       reference_type = dst_type;
6328       if (!TYPE_REF_IS_RVALUE (dst_type)
6329           ? real_lvalue_p (expr)
6330           : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6331              ? lvalue_p (expr)
6332              : lvalue_or_rvalue_with_address_p (expr)))
6333         /* OK.  */;
6334       else
6335         {
6336           if (complain & tf_error)
6337             error ("invalid const_cast of an rvalue of type %qT to type %qT",
6338                    src_type, dst_type);
6339           return error_mark_node;
6340         }
6341       dst_type = build_pointer_type (TREE_TYPE (dst_type));
6342       src_type = build_pointer_type (src_type);
6343     }
6344   else
6345     {
6346       reference_type = NULL_TREE;
6347       /* If the destination type is not a reference type, the
6348          lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6349          conversions are performed.  */
6350       src_type = type_decays_to (src_type);
6351       if (src_type == error_mark_node)
6352         return error_mark_node;
6353     }
6354
6355   if (TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
6356     {
6357       if (comp_ptr_ttypes_const (dst_type, src_type))
6358         {
6359           if (valid_p)
6360             {
6361               *valid_p = true;
6362               /* This cast is actually a C-style cast.  Issue a warning if
6363                  the user is making a potentially unsafe cast.  */
6364               check_for_casting_away_constness (src_type, dst_type,
6365                                                 CAST_EXPR, complain);
6366             }
6367           if (reference_type)
6368             {
6369               expr = cp_build_addr_expr (expr, complain);
6370               expr = build_nop (reference_type, expr);
6371               return convert_from_reference (expr);
6372             }
6373           else
6374             {
6375               expr = decay_conversion (expr);
6376               /* build_c_cast puts on a NOP_EXPR to make the result not an
6377                  lvalue.  Strip such NOP_EXPRs if VALUE is being used in
6378                  non-lvalue context.  */
6379               if (TREE_CODE (expr) == NOP_EXPR
6380                   && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6381                 expr = TREE_OPERAND (expr, 0);
6382               return build_nop (dst_type, expr);
6383             }
6384         }
6385       else if (valid_p
6386                && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6387                                             TREE_TYPE (src_type)))
6388         check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6389                                           complain);
6390     }
6391
6392   if (complain & tf_error)
6393     error ("invalid const_cast from type %qT to type %qT",
6394            src_type, dst_type);
6395   return error_mark_node;
6396 }
6397
6398 tree
6399 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6400 {
6401   if (type == error_mark_node || error_operand_p (expr))
6402     return error_mark_node;
6403
6404   if (processing_template_decl)
6405     {
6406       tree t = build_min (CONST_CAST_EXPR, type, expr);
6407
6408       if (!TREE_SIDE_EFFECTS (t)
6409           && type_dependent_expression_p (expr))
6410         /* There might turn out to be side effects inside expr.  */
6411         TREE_SIDE_EFFECTS (t) = 1;
6412       return convert_from_reference (t);
6413     }
6414
6415   return build_const_cast_1 (type, expr, complain,
6416                              /*valid_p=*/NULL);
6417 }
6418
6419 /* Like cp_build_c_cast, but for the c-common bits.  */
6420
6421 tree
6422 build_c_cast (location_t loc ATTRIBUTE_UNUSED, tree type, tree expr)
6423 {
6424   return cp_build_c_cast (type, expr, tf_warning_or_error);
6425 }
6426
6427 /* Build an expression representing an explicit C-style cast to type
6428    TYPE of expression EXPR.  */
6429
6430 tree
6431 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6432 {
6433   tree value = expr;
6434   tree result;
6435   bool valid_p;
6436
6437   if (type == error_mark_node || error_operand_p (expr))
6438     return error_mark_node;
6439
6440   if (processing_template_decl)
6441     {
6442       tree t = build_min (CAST_EXPR, type,
6443                           tree_cons (NULL_TREE, value, NULL_TREE));
6444       /* We don't know if it will or will not have side effects.  */
6445       TREE_SIDE_EFFECTS (t) = 1;
6446       return convert_from_reference (t);
6447     }
6448
6449   /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6450      'Class') should always be retained, because this information aids
6451      in method lookup.  */
6452   if (objc_is_object_ptr (type)
6453       && objc_is_object_ptr (TREE_TYPE (expr)))
6454     return build_nop (type, expr);
6455
6456   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6457      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
6458   if (TREE_CODE (type) != REFERENCE_TYPE
6459       && TREE_CODE (value) == NOP_EXPR
6460       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6461     value = TREE_OPERAND (value, 0);
6462
6463   if (TREE_CODE (type) == ARRAY_TYPE)
6464     {
6465       /* Allow casting from T1* to T2[] because Cfront allows it.
6466          NIHCL uses it. It is not valid ISO C++ however.  */
6467       if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6468         {
6469           if (complain & tf_error)
6470             permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6471           else
6472             return error_mark_node;
6473           type = build_pointer_type (TREE_TYPE (type));
6474         }
6475       else
6476         {
6477           if (complain & tf_error)
6478             error ("ISO C++ forbids casting to an array type %qT", type);
6479           return error_mark_node;
6480         }
6481     }
6482
6483   if (TREE_CODE (type) == FUNCTION_TYPE
6484       || TREE_CODE (type) == METHOD_TYPE)
6485     {
6486       if (complain & tf_error)
6487         error ("invalid cast to function type %qT", type);
6488       return error_mark_node;
6489     }
6490
6491   if (TREE_CODE (type) == POINTER_TYPE
6492       && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6493       /* Casting to an integer of smaller size is an error detected elsewhere.  */
6494       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6495       /* Don't warn about converting any constant.  */
6496       && !TREE_CONSTANT (value))
6497     warning_at (input_location, OPT_Wint_to_pointer_cast, 
6498                 "cast to pointer from integer of different size");
6499
6500   /* A C-style cast can be a const_cast.  */
6501   result = build_const_cast_1 (type, value, complain & tf_warning,
6502                                &valid_p);
6503   if (valid_p)
6504     return result;
6505
6506   /* Or a static cast.  */
6507   result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6508                                 &valid_p, complain);
6509   /* Or a reinterpret_cast.  */
6510   if (!valid_p)
6511     result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6512                                        &valid_p, complain);
6513   /* The static_cast or reinterpret_cast may be followed by a
6514      const_cast.  */
6515   if (valid_p
6516       /* A valid cast may result in errors if, for example, a
6517          conversion to am ambiguous base class is required.  */
6518       && !error_operand_p (result))
6519     {
6520       tree result_type;
6521
6522       /* Non-class rvalues always have cv-unqualified type.  */
6523       if (!CLASS_TYPE_P (type))
6524         type = TYPE_MAIN_VARIANT (type);
6525       result_type = TREE_TYPE (result);
6526       if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6527         result_type = TYPE_MAIN_VARIANT (result_type);
6528       /* If the type of RESULT does not match TYPE, perform a
6529          const_cast to make it match.  If the static_cast or
6530          reinterpret_cast succeeded, we will differ by at most
6531          cv-qualification, so the follow-on const_cast is guaranteed
6532          to succeed.  */
6533       if (!same_type_p (non_reference (type), non_reference (result_type)))
6534         {
6535           result = build_const_cast_1 (type, result, false, &valid_p);
6536           gcc_assert (valid_p);
6537         }
6538       return result;
6539     }
6540
6541   return error_mark_node;
6542 }
6543 \f
6544 /* For use from the C common bits.  */
6545 tree
6546 build_modify_expr (location_t location ATTRIBUTE_UNUSED,
6547                    tree lhs, tree lhs_origtype ATTRIBUTE_UNUSED,
6548                    enum tree_code modifycode, 
6549                    location_t rhs_location ATTRIBUTE_UNUSED, tree rhs,
6550                    tree rhs_origtype ATTRIBUTE_UNUSED)
6551 {
6552   return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6553 }
6554
6555 /* Build an assignment expression of lvalue LHS from value RHS.
6556    MODIFYCODE is the code for a binary operator that we use
6557    to combine the old value of LHS with RHS to get the new value.
6558    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6559
6560    C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
6561
6562 tree
6563 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6564                       tsubst_flags_t complain)
6565 {
6566   tree result;
6567   tree newrhs = rhs;
6568   tree lhstype = TREE_TYPE (lhs);
6569   tree olhstype = lhstype;
6570   bool plain_assign = (modifycode == NOP_EXPR);
6571
6572   /* Avoid duplicate error messages from operands that had errors.  */
6573   if (error_operand_p (lhs) || error_operand_p (rhs))
6574     return error_mark_node;
6575
6576   /* Handle control structure constructs used as "lvalues".  */
6577   switch (TREE_CODE (lhs))
6578     {
6579       /* Handle --foo = 5; as these are valid constructs in C++.  */
6580     case PREDECREMENT_EXPR:
6581     case PREINCREMENT_EXPR:
6582       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6583         lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6584                       stabilize_reference (TREE_OPERAND (lhs, 0)),
6585                       TREE_OPERAND (lhs, 1));
6586       newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6587                                      modifycode, rhs, complain);
6588       if (newrhs == error_mark_node)
6589         return error_mark_node;
6590       return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6591
6592       /* Handle (a, b) used as an "lvalue".  */
6593     case COMPOUND_EXPR:
6594       newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6595                                      modifycode, rhs, complain);
6596       if (newrhs == error_mark_node)
6597         return error_mark_node;
6598       return build2 (COMPOUND_EXPR, lhstype,
6599                      TREE_OPERAND (lhs, 0), newrhs);
6600
6601     case MODIFY_EXPR:
6602       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6603         lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6604                       stabilize_reference (TREE_OPERAND (lhs, 0)),
6605                       TREE_OPERAND (lhs, 1));
6606       newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6607                                      complain);
6608       if (newrhs == error_mark_node)
6609         return error_mark_node;
6610       return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6611
6612     case MIN_EXPR:
6613     case MAX_EXPR:
6614       /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6615          when neither operand has side-effects.  */
6616       if (!lvalue_or_else (lhs, lv_assign, complain))
6617         return error_mark_node;
6618
6619       gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6620                   && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6621
6622       lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6623                     build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6624                             boolean_type_node,
6625                             TREE_OPERAND (lhs, 0),
6626                             TREE_OPERAND (lhs, 1)),
6627                     TREE_OPERAND (lhs, 0),
6628                     TREE_OPERAND (lhs, 1));
6629       /* Fall through.  */
6630
6631       /* Handle (a ? b : c) used as an "lvalue".  */
6632     case COND_EXPR:
6633       {
6634         /* Produce (a ? (b = rhs) : (c = rhs))
6635            except that the RHS goes through a save-expr
6636            so the code to compute it is only emitted once.  */
6637         tree cond;
6638         tree preeval = NULL_TREE;
6639
6640         if (VOID_TYPE_P (TREE_TYPE (rhs)))
6641           {
6642             if (complain & tf_error)
6643               error ("void value not ignored as it ought to be");
6644             return error_mark_node;
6645           }
6646
6647         rhs = stabilize_expr (rhs, &preeval);
6648
6649         /* Check this here to avoid odd errors when trying to convert
6650            a throw to the type of the COND_EXPR.  */
6651         if (!lvalue_or_else (lhs, lv_assign, complain))
6652           return error_mark_node;
6653
6654         cond = build_conditional_expr
6655           (TREE_OPERAND (lhs, 0),
6656            cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6657                                  modifycode, rhs, complain),
6658            cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6659                                  modifycode, rhs, complain),
6660            complain);
6661
6662         if (cond == error_mark_node)
6663           return cond;
6664         /* Make sure the code to compute the rhs comes out
6665            before the split.  */
6666         if (preeval)
6667           cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6668         return cond;
6669       }
6670
6671     default:
6672       break;
6673     }
6674
6675   if (modifycode == INIT_EXPR)
6676     {
6677       if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6678         /* Do the default thing.  */;
6679       else if (TREE_CODE (rhs) == CONSTRUCTOR)
6680         {
6681           /* Compound literal.  */
6682           if (! same_type_p (TREE_TYPE (rhs), lhstype))
6683             /* Call convert to generate an error; see PR 11063.  */
6684             rhs = convert (lhstype, rhs);
6685           result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6686           TREE_SIDE_EFFECTS (result) = 1;
6687           return result;
6688         }
6689       else if (! MAYBE_CLASS_TYPE_P (lhstype))
6690         /* Do the default thing.  */;
6691       else
6692         {
6693           VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6694           result = build_special_member_call (lhs, complete_ctor_identifier,
6695                                               &rhs_vec, lhstype, LOOKUP_NORMAL,
6696                                               complain);
6697           release_tree_vector (rhs_vec);
6698           if (result == NULL_TREE)
6699             return error_mark_node;
6700           return result;
6701         }
6702     }
6703   else
6704     {
6705       lhs = require_complete_type_sfinae (lhs, complain);
6706       if (lhs == error_mark_node)
6707         return error_mark_node;
6708
6709       if (modifycode == NOP_EXPR)
6710         {
6711           if (c_dialect_objc ())
6712             {
6713               result = objc_maybe_build_modify_expr (lhs, rhs);
6714               if (result)
6715                 return result;
6716             }
6717
6718           /* `operator=' is not an inheritable operator.  */
6719           if (! MAYBE_CLASS_TYPE_P (lhstype))
6720             /* Do the default thing.  */;
6721           else
6722             {
6723               result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
6724                                      lhs, rhs, make_node (NOP_EXPR),
6725                                      /*overload=*/NULL,
6726                                      complain);
6727               if (result == NULL_TREE)
6728                 return error_mark_node;
6729               return result;
6730             }
6731           lhstype = olhstype;
6732         }
6733       else
6734         {
6735           tree init = NULL_TREE;
6736
6737           /* A binary op has been requested.  Combine the old LHS
6738              value with the RHS producing the value we should actually
6739              store into the LHS.  */
6740           gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
6741                          && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
6742                         || MAYBE_CLASS_TYPE_P (lhstype)));
6743
6744           /* Preevaluate the RHS to make sure its evaluation is complete
6745              before the lvalue-to-rvalue conversion of the LHS:
6746
6747              [expr.ass] With respect to an indeterminately-sequenced
6748              function call, the operation of a compound assignment is a
6749              single evaluation. [ Note: Therefore, a function call shall
6750              not intervene between the lvalue-to-rvalue conversion and the
6751              side effect associated with any single compound assignment
6752              operator. -- end note ]  */
6753           lhs = stabilize_reference (lhs);
6754           if (TREE_SIDE_EFFECTS (rhs))
6755             rhs = mark_rvalue_use (rhs);
6756           rhs = stabilize_expr (rhs, &init);
6757           newrhs = cp_build_binary_op (input_location,
6758                                        modifycode, lhs, rhs,
6759                                        complain);
6760           if (newrhs == error_mark_node)
6761             {
6762               if (complain & tf_error)
6763                 error ("  in evaluation of %<%Q(%#T, %#T)%>", modifycode,
6764                        TREE_TYPE (lhs), TREE_TYPE (rhs));
6765               return error_mark_node;
6766             }
6767
6768           if (init)
6769             newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
6770
6771           /* Now it looks like a plain assignment.  */
6772           modifycode = NOP_EXPR;
6773           if (c_dialect_objc ())
6774             {
6775               result = objc_maybe_build_modify_expr (lhs, newrhs);
6776               if (result)
6777                 return result;
6778             }
6779         }
6780       gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
6781       gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
6782     }
6783
6784   /* The left-hand side must be an lvalue.  */
6785   if (!lvalue_or_else (lhs, lv_assign, complain))
6786     return error_mark_node;
6787
6788   /* Warn about modifying something that is `const'.  Don't warn if
6789      this is initialization.  */
6790   if (modifycode != INIT_EXPR
6791       && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6792           /* Functions are not modifiable, even though they are
6793              lvalues.  */
6794           || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6795           || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
6796           /* If it's an aggregate and any field is const, then it is
6797              effectively const.  */
6798           || (CLASS_TYPE_P (lhstype)
6799               && C_TYPE_FIELDS_READONLY (lhstype))))
6800     {
6801       if (complain & tf_error)
6802         cxx_readonly_error (lhs, lv_assign);
6803       else
6804         return error_mark_node;
6805     }
6806
6807   /* If storing into a structure or union member, it may have been given a
6808      lowered bitfield type.  We need to convert to the declared type first,
6809      so retrieve it now.  */
6810
6811   olhstype = unlowered_expr_type (lhs);
6812
6813   /* Convert new value to destination type.  */
6814
6815   if (TREE_CODE (lhstype) == ARRAY_TYPE)
6816     {
6817       int from_array;
6818
6819       if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
6820         {
6821           if (modifycode != INIT_EXPR)
6822             {
6823               if (complain & tf_error)
6824                 error ("assigning to an array from an initializer list");
6825               return error_mark_node;
6826             }
6827           if (check_array_initializer (lhs, lhstype, newrhs))
6828             return error_mark_node;
6829           newrhs = digest_init (lhstype, newrhs, complain);
6830           if (newrhs == error_mark_node)
6831             return error_mark_node;
6832         }
6833
6834       else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
6835                                      TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
6836         {
6837           if (complain & tf_error)
6838             error ("incompatible types in assignment of %qT to %qT",
6839                    TREE_TYPE (rhs), lhstype);
6840           return error_mark_node;
6841         }
6842
6843       /* Allow array assignment in compiler-generated code.  */
6844       else if (!current_function_decl
6845                || !DECL_DEFAULTED_FN (current_function_decl))
6846         {
6847           /* This routine is used for both initialization and assignment.
6848              Make sure the diagnostic message differentiates the context.  */
6849           if (complain & tf_error)
6850             {
6851               if (modifycode == INIT_EXPR)
6852                 error ("array used as initializer");
6853               else
6854                 error ("invalid array assignment");
6855             }
6856           return error_mark_node;
6857         }
6858
6859       from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6860                    ? 1 + (modifycode != INIT_EXPR): 0;
6861       return build_vec_init (lhs, NULL_TREE, newrhs,
6862                              /*explicit_value_init_p=*/false,
6863                              from_array, complain);
6864     }
6865
6866   if (modifycode == INIT_EXPR)
6867     /* Calls with INIT_EXPR are all direct-initialization, so don't set
6868        LOOKUP_ONLYCONVERTING.  */
6869     newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
6870                                          ICR_INIT, NULL_TREE, 0,
6871                                          complain);
6872   else
6873     newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
6874                                      NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
6875
6876   if (!same_type_p (lhstype, olhstype))
6877     newrhs = cp_convert_and_check (lhstype, newrhs);
6878
6879   if (modifycode != INIT_EXPR)
6880     {
6881       if (TREE_CODE (newrhs) == CALL_EXPR
6882           && TYPE_NEEDS_CONSTRUCTING (lhstype))
6883         newrhs = build_cplus_new (lhstype, newrhs, complain);
6884
6885       /* Can't initialize directly from a TARGET_EXPR, since that would
6886          cause the lhs to be constructed twice, and possibly result in
6887          accidental self-initialization.  So we force the TARGET_EXPR to be
6888          expanded without a target.  */
6889       if (TREE_CODE (newrhs) == TARGET_EXPR)
6890         newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6891                          TREE_OPERAND (newrhs, 0));
6892     }
6893
6894   if (newrhs == error_mark_node)
6895     return error_mark_node;
6896
6897   if (c_dialect_objc () && flag_objc_gc)
6898     {
6899       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6900
6901       if (result)
6902         return result;
6903     }
6904
6905   result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6906                    lhstype, lhs, newrhs);
6907
6908   TREE_SIDE_EFFECTS (result) = 1;
6909   if (!plain_assign)
6910     TREE_NO_WARNING (result) = 1;
6911
6912   return result;
6913 }
6914
6915 tree
6916 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6917                      tsubst_flags_t complain)
6918 {
6919   if (processing_template_decl)
6920     return build_min_nt (MODOP_EXPR, lhs,
6921                          build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6922
6923   if (modifycode != NOP_EXPR)
6924     {
6925       tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6926                                 make_node (modifycode),
6927                                 /*overload=*/NULL,
6928                                 complain);
6929       if (rval)
6930         {
6931           TREE_NO_WARNING (rval) = 1;
6932           return rval;
6933         }
6934     }
6935   return cp_build_modify_expr (lhs, modifycode, rhs, complain);
6936 }
6937
6938 /* Helper function for get_delta_difference which assumes FROM is a base
6939    class of TO.  Returns a delta for the conversion of pointer-to-member
6940    of FROM to pointer-to-member of TO.  If the conversion is invalid and 
6941    tf_error is not set in COMPLAIN returns error_mark_node, otherwise
6942    returns zero.  If FROM is not a base class of TO, returns NULL_TREE.
6943    If C_CAST_P is true, this conversion is taking place as part of a 
6944    C-style cast.  */
6945
6946 static tree
6947 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
6948                         tsubst_flags_t complain)
6949 {
6950   tree binfo;
6951   base_kind kind;
6952   base_access access = c_cast_p ? ba_unique : ba_check;
6953
6954   /* Note: ba_quiet does not distinguish between access control and
6955      ambiguity.  */
6956   if (!(complain & tf_error))
6957     access |= ba_quiet;
6958
6959   binfo = lookup_base (to, from, access, &kind);
6960
6961   if (kind == bk_inaccessible || kind == bk_ambig)
6962     {
6963       if (!(complain & tf_error))
6964         return error_mark_node;
6965
6966       error ("   in pointer to member function conversion");
6967       return size_zero_node;
6968     }
6969   else if (binfo)
6970     {
6971       if (kind != bk_via_virtual)
6972         return BINFO_OFFSET (binfo);
6973       else
6974         /* FROM is a virtual base class of TO.  Issue an error or warning
6975            depending on whether or not this is a reinterpret cast.  */
6976         {
6977           if (!(complain & tf_error))
6978             return error_mark_node;
6979
6980           error ("pointer to member conversion via virtual base %qT",
6981                  BINFO_TYPE (binfo_from_vbase (binfo)));
6982
6983           return size_zero_node;
6984         }
6985       }
6986   else
6987     return NULL_TREE;
6988 }
6989
6990 /* Get difference in deltas for different pointer to member function
6991    types.  If the conversion is invalid and tf_error is not set in
6992    COMPLAIN, returns error_mark_node, otherwise returns an integer
6993    constant of type PTRDIFF_TYPE_NODE and its value is zero if the
6994    conversion is invalid.  If ALLOW_INVERSE_P is true, then allow reverse
6995    conversions as well.  If C_CAST_P is true this conversion is taking
6996    place as part of a C-style cast.
6997
6998    Note that the naming of FROM and TO is kind of backwards; the return
6999    value is what we add to a TO in order to get a FROM.  They are named
7000    this way because we call this function to find out how to convert from
7001    a pointer to member of FROM to a pointer to member of TO.  */
7002
7003 static tree
7004 get_delta_difference (tree from, tree to,
7005                       bool allow_inverse_p,
7006                       bool c_cast_p, tsubst_flags_t complain)
7007 {
7008   tree result;
7009
7010   if (same_type_ignoring_top_level_qualifiers_p (from, to))
7011     /* Pointer to member of incomplete class is permitted*/
7012     result = size_zero_node;
7013   else
7014     result = get_delta_difference_1 (from, to, c_cast_p, complain);
7015
7016   if (result == error_mark_node)
7017     return error_mark_node;
7018
7019   if (!result)
7020   {
7021     if (!allow_inverse_p)
7022       {
7023         if (!(complain & tf_error))
7024           return error_mark_node;
7025
7026         error_not_base_type (from, to);
7027         error ("   in pointer to member conversion");
7028         result = size_zero_node;
7029       }
7030     else
7031       {
7032         result = get_delta_difference_1 (to, from, c_cast_p, complain);
7033
7034         if (result == error_mark_node)
7035           return error_mark_node;
7036
7037         if (result)
7038           result = size_diffop_loc (input_location,
7039                                     size_zero_node, result);
7040         else
7041           {
7042             if (!(complain & tf_error))
7043               return error_mark_node;
7044
7045             error_not_base_type (from, to);
7046             error ("   in pointer to member conversion");
7047             result = size_zero_node;
7048           }
7049       }
7050   }
7051
7052   return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7053                                                       result));
7054 }
7055
7056 /* Return a constructor for the pointer-to-member-function TYPE using
7057    the other components as specified.  */
7058
7059 tree
7060 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7061 {
7062   tree u = NULL_TREE;
7063   tree delta_field;
7064   tree pfn_field;
7065   VEC(constructor_elt, gc) *v;
7066
7067   /* Pull the FIELD_DECLs out of the type.  */
7068   pfn_field = TYPE_FIELDS (type);
7069   delta_field = DECL_CHAIN (pfn_field);
7070
7071   /* Make sure DELTA has the type we want.  */
7072   delta = convert_and_check (delta_type_node, delta);
7073
7074   /* Convert to the correct target type if necessary.  */
7075   pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7076
7077   /* Finish creating the initializer.  */
7078   v = VEC_alloc(constructor_elt, gc, 2);
7079   CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7080   CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7081   u = build_constructor (type, v);
7082   TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7083   TREE_STATIC (u) = (TREE_CONSTANT (u)
7084                      && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7085                          != NULL_TREE)
7086                      && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7087                          != NULL_TREE));
7088   return u;
7089 }
7090
7091 /* Build a constructor for a pointer to member function.  It can be
7092    used to initialize global variables, local variable, or used
7093    as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
7094    want to be.
7095
7096    If FORCE is nonzero, then force this conversion, even if
7097    we would rather not do it.  Usually set when using an explicit
7098    cast.  A C-style cast is being processed iff C_CAST_P is true.
7099
7100    Return error_mark_node, if something goes wrong.  */
7101
7102 tree
7103 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7104                   tsubst_flags_t complain)
7105 {
7106   tree fn;
7107   tree pfn_type;
7108   tree to_type;
7109
7110   if (error_operand_p (pfn))
7111     return error_mark_node;
7112
7113   pfn_type = TREE_TYPE (pfn);
7114   to_type = build_ptrmemfunc_type (type);
7115
7116   /* Handle multiple conversions of pointer to member functions.  */
7117   if (TYPE_PTRMEMFUNC_P (pfn_type))
7118     {
7119       tree delta = NULL_TREE;
7120       tree npfn = NULL_TREE;
7121       tree n;
7122
7123       if (!force
7124           && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
7125         error ("invalid conversion to type %qT from type %qT",
7126                to_type, pfn_type);
7127
7128       n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7129                                 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7130                                 force,
7131                                 c_cast_p, complain);
7132       if (n == error_mark_node)
7133         return error_mark_node;
7134
7135       /* We don't have to do any conversion to convert a
7136          pointer-to-member to its own type.  But, we don't want to
7137          just return a PTRMEM_CST if there's an explicit cast; that
7138          cast should make the expression an invalid template argument.  */
7139       if (TREE_CODE (pfn) != PTRMEM_CST)
7140         {
7141           if (same_type_p (to_type, pfn_type))
7142             return pfn;
7143           else if (integer_zerop (n))
7144             return build_reinterpret_cast (to_type, pfn, 
7145                                            tf_warning_or_error);
7146         }
7147
7148       if (TREE_SIDE_EFFECTS (pfn))
7149         pfn = save_expr (pfn);
7150
7151       /* Obtain the function pointer and the current DELTA.  */
7152       if (TREE_CODE (pfn) == PTRMEM_CST)
7153         expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7154       else
7155         {
7156           npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7157           delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7158         }
7159
7160       /* Just adjust the DELTA field.  */
7161       gcc_assert  (same_type_ignoring_top_level_qualifiers_p
7162                    (TREE_TYPE (delta), ptrdiff_type_node));
7163       if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7164         n = cp_build_binary_op (input_location,
7165                                 LSHIFT_EXPR, n, integer_one_node,
7166                                 tf_warning_or_error);
7167       delta = cp_build_binary_op (input_location,
7168                                   PLUS_EXPR, delta, n, tf_warning_or_error);
7169       return build_ptrmemfunc1 (to_type, delta, npfn);
7170     }
7171
7172   /* Handle null pointer to member function conversions.  */
7173   if (null_ptr_cst_p (pfn))
7174     {
7175       pfn = build_c_cast (input_location, type, nullptr_node);
7176       return build_ptrmemfunc1 (to_type,
7177                                 integer_zero_node,
7178                                 pfn);
7179     }
7180
7181   if (type_unknown_p (pfn))
7182     return instantiate_type (type, pfn, tf_warning_or_error);
7183
7184   fn = TREE_OPERAND (pfn, 0);
7185   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7186               /* In a template, we will have preserved the
7187                  OFFSET_REF.  */
7188               || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7189   return make_ptrmem_cst (to_type, fn);
7190 }
7191
7192 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7193    given by CST.
7194
7195    ??? There is no consistency as to the types returned for the above
7196    values.  Some code acts as if it were a sizetype and some as if it were
7197    integer_type_node.  */
7198
7199 void
7200 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7201 {
7202   tree type = TREE_TYPE (cst);
7203   tree fn = PTRMEM_CST_MEMBER (cst);
7204   tree ptr_class, fn_class;
7205
7206   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7207
7208   /* The class that the function belongs to.  */
7209   fn_class = DECL_CONTEXT (fn);
7210
7211   /* The class that we're creating a pointer to member of.  */
7212   ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7213
7214   /* First, calculate the adjustment to the function's class.  */
7215   *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7216                                  /*c_cast_p=*/0, tf_warning_or_error);
7217
7218   if (!DECL_VIRTUAL_P (fn))
7219     *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
7220   else
7221     {
7222       /* If we're dealing with a virtual function, we have to adjust 'this'
7223          again, to point to the base which provides the vtable entry for
7224          fn; the call will do the opposite adjustment.  */
7225       tree orig_class = DECL_CONTEXT (fn);
7226       tree binfo = binfo_or_else (orig_class, fn_class);
7227       *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7228                        *delta, BINFO_OFFSET (binfo));
7229       *delta = fold_if_not_in_template (*delta);
7230
7231       /* We set PFN to the vtable offset at which the function can be
7232          found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7233          case delta is shifted left, and then incremented).  */
7234       *pfn = DECL_VINDEX (fn);
7235       *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7236                      TYPE_SIZE_UNIT (vtable_entry_type));
7237       *pfn = fold_if_not_in_template (*pfn);
7238
7239       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7240         {
7241         case ptrmemfunc_vbit_in_pfn:
7242           *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7243                          integer_one_node);
7244           *pfn = fold_if_not_in_template (*pfn);
7245           break;
7246
7247         case ptrmemfunc_vbit_in_delta:
7248           *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7249                            *delta, integer_one_node);
7250           *delta = fold_if_not_in_template (*delta);
7251           *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7252                            *delta, integer_one_node);
7253           *delta = fold_if_not_in_template (*delta);
7254           break;
7255
7256         default:
7257           gcc_unreachable ();
7258         }
7259
7260       *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7261       *pfn = fold_if_not_in_template (*pfn);
7262     }
7263 }
7264
7265 /* Return an expression for PFN from the pointer-to-member function
7266    given by T.  */
7267
7268 static tree
7269 pfn_from_ptrmemfunc (tree t)
7270 {
7271   if (TREE_CODE (t) == PTRMEM_CST)
7272     {
7273       tree delta;
7274       tree pfn;
7275
7276       expand_ptrmemfunc_cst (t, &delta, &pfn);
7277       if (pfn)
7278         return pfn;
7279     }
7280
7281   return build_ptrmemfunc_access_expr (t, pfn_identifier);
7282 }
7283
7284 /* Return an expression for DELTA from the pointer-to-member function
7285    given by T.  */
7286
7287 static tree
7288 delta_from_ptrmemfunc (tree t)
7289 {
7290   if (TREE_CODE (t) == PTRMEM_CST)
7291     {
7292       tree delta;
7293       tree pfn;
7294
7295       expand_ptrmemfunc_cst (t, &delta, &pfn);
7296       if (delta)
7297         return delta;
7298     }
7299
7300   return build_ptrmemfunc_access_expr (t, delta_identifier);
7301 }
7302
7303 /* Convert value RHS to type TYPE as preparation for an assignment to
7304    an lvalue of type TYPE.  ERRTYPE indicates what kind of error the
7305    implicit conversion is.  If FNDECL is non-NULL, we are doing the
7306    conversion in order to pass the PARMNUMth argument of FNDECL.
7307    If FNDECL is NULL, we are doing the conversion in function pointer
7308    argument passing, conversion in initialization, etc. */
7309
7310 static tree
7311 convert_for_assignment (tree type, tree rhs,
7312                         impl_conv_rhs errtype, tree fndecl, int parmnum,
7313                         tsubst_flags_t complain, int flags)
7314 {
7315   tree rhstype;
7316   enum tree_code coder;
7317
7318   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
7319   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7320     rhs = TREE_OPERAND (rhs, 0);
7321
7322   rhstype = TREE_TYPE (rhs);
7323   coder = TREE_CODE (rhstype);
7324
7325   if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7326       && vector_types_convertible_p (type, rhstype, true))
7327     {
7328       rhs = mark_rvalue_use (rhs);
7329       return convert (type, rhs);
7330     }
7331
7332   if (rhs == error_mark_node || rhstype == error_mark_node)
7333     return error_mark_node;
7334   if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7335     return error_mark_node;
7336
7337   /* The RHS of an assignment cannot have void type.  */
7338   if (coder == VOID_TYPE)
7339     {
7340       if (complain & tf_error)
7341         error ("void value not ignored as it ought to be");
7342       return error_mark_node;
7343     }
7344
7345   /* Simplify the RHS if possible.  */
7346   if (TREE_CODE (rhs) == CONST_DECL)
7347     rhs = DECL_INITIAL (rhs);
7348
7349   if (c_dialect_objc ())
7350     {
7351       int parmno;
7352       tree selector;
7353       tree rname = fndecl;
7354
7355       switch (errtype)
7356         {
7357           case ICR_ASSIGN:
7358             parmno = -1;
7359             break;
7360           case ICR_INIT:
7361             parmno = -2;
7362             break;
7363           default:
7364             selector = objc_message_selector ();
7365             parmno = parmnum;
7366             if (selector && parmno > 1)
7367               {
7368                 rname = selector;
7369                 parmno -= 1;
7370               }
7371         }
7372
7373       if (objc_compare_types (type, rhstype, parmno, rname))
7374         {
7375           rhs = mark_rvalue_use (rhs);
7376           return convert (type, rhs);
7377         }
7378     }
7379
7380   /* [expr.ass]
7381
7382      The expression is implicitly converted (clause _conv_) to the
7383      cv-unqualified type of the left operand.
7384
7385      We allow bad conversions here because by the time we get to this point
7386      we are committed to doing the conversion.  If we end up doing a bad
7387      conversion, convert_like will complain.  */
7388   if (!can_convert_arg_bad (type, rhstype, rhs, flags))
7389     {
7390       /* When -Wno-pmf-conversions is use, we just silently allow
7391          conversions from pointers-to-members to plain pointers.  If
7392          the conversion doesn't work, cp_convert will complain.  */
7393       if (!warn_pmf2ptr
7394           && TYPE_PTR_P (type)
7395           && TYPE_PTRMEMFUNC_P (rhstype))
7396         rhs = cp_convert (strip_top_quals (type), rhs);
7397       else
7398         {
7399           if (complain & tf_error)
7400             {
7401               /* If the right-hand side has unknown type, then it is an
7402                  overloaded function.  Call instantiate_type to get error
7403                  messages.  */
7404               if (rhstype == unknown_type_node)
7405                 instantiate_type (type, rhs, tf_warning_or_error);
7406               else if (fndecl)
7407                 error ("cannot convert %qT to %qT for argument %qP to %qD",
7408                        rhstype, type, parmnum, fndecl);
7409               else
7410                 switch (errtype)
7411                   {
7412                     case ICR_DEFAULT_ARGUMENT:
7413                       error ("cannot convert %qT to %qT in default argument",
7414                              rhstype, type);
7415                       break;
7416                     case ICR_ARGPASS:
7417                       error ("cannot convert %qT to %qT in argument passing",
7418                              rhstype, type);
7419                       break;
7420                     case ICR_CONVERTING:
7421                       error ("cannot convert %qT to %qT",
7422                              rhstype, type);
7423                       break;
7424                     case ICR_INIT:
7425                       error ("cannot convert %qT to %qT in initialization",
7426                              rhstype, type);
7427                       break;
7428                     case ICR_RETURN:
7429                       error ("cannot convert %qT to %qT in return",
7430                              rhstype, type);
7431                       break;
7432                     case ICR_ASSIGN:
7433                       error ("cannot convert %qT to %qT in assignment",
7434                              rhstype, type);
7435                       break;
7436                     default:
7437                       gcc_unreachable();
7438                   }
7439             }
7440           return error_mark_node;
7441         }
7442     }
7443   if (warn_missing_format_attribute)
7444     {
7445       const enum tree_code codel = TREE_CODE (type);
7446       if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7447           && coder == codel
7448           && check_missing_format_attribute (type, rhstype)
7449           && (complain & tf_warning))
7450         switch (errtype)
7451           {
7452             case ICR_ARGPASS:
7453             case ICR_DEFAULT_ARGUMENT:
7454               if (fndecl)
7455                 warning (OPT_Wmissing_format_attribute,
7456                          "parameter %qP of %qD might be a candidate "
7457                          "for a format attribute", parmnum, fndecl);
7458               else
7459                 warning (OPT_Wmissing_format_attribute,
7460                          "parameter might be a candidate "
7461                          "for a format attribute");
7462               break;
7463             case ICR_CONVERTING:
7464               warning (OPT_Wmissing_format_attribute,
7465                        "target of conversion might be a candidate "
7466                        "for a format attribute");
7467               break;
7468             case ICR_INIT:
7469               warning (OPT_Wmissing_format_attribute,
7470                        "target of initialization might be a candidate "
7471                        "for a format attribute");
7472               break;
7473             case ICR_RETURN:
7474               warning (OPT_Wmissing_format_attribute,
7475                        "return type might be a candidate "
7476                        "for a format attribute");
7477               break;
7478             case ICR_ASSIGN:
7479               warning (OPT_Wmissing_format_attribute,
7480                        "left-hand side of assignment might be a candidate "
7481                        "for a format attribute");
7482               break;
7483             default:
7484               gcc_unreachable();
7485           }
7486     }
7487
7488   /* If -Wparentheses, warn about a = b = c when a has type bool and b
7489      does not.  */
7490   if (warn_parentheses
7491       && TREE_CODE (type) == BOOLEAN_TYPE
7492       && TREE_CODE (rhs) == MODIFY_EXPR
7493       && !TREE_NO_WARNING (rhs)
7494       && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7495       && (complain & tf_warning))
7496     {
7497       location_t loc = EXPR_LOC_OR_HERE (rhs);
7498
7499       warning_at (loc, OPT_Wparentheses,
7500                   "suggest parentheses around assignment used as truth value");
7501       TREE_NO_WARNING (rhs) = 1;
7502     }
7503
7504   return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7505                                             complain, flags);
7506 }
7507
7508 /* Convert RHS to be of type TYPE.
7509    If EXP is nonzero, it is the target of the initialization.
7510    ERRTYPE indicates what kind of error the implicit conversion is.
7511
7512    Two major differences between the behavior of
7513    `convert_for_assignment' and `convert_for_initialization'
7514    are that references are bashed in the former, while
7515    copied in the latter, and aggregates are assigned in
7516    the former (operator=) while initialized in the
7517    latter (X(X&)).
7518
7519    If using constructor make sure no conversion operator exists, if one does
7520    exist, an ambiguity exists.
7521
7522    If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything.  */
7523
7524 tree
7525 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7526                             impl_conv_rhs errtype, tree fndecl, int parmnum,
7527                             tsubst_flags_t complain)
7528 {
7529   enum tree_code codel = TREE_CODE (type);
7530   tree rhstype;
7531   enum tree_code coder;
7532
7533   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7534      Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
7535   if (TREE_CODE (rhs) == NOP_EXPR
7536       && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7537       && codel != REFERENCE_TYPE)
7538     rhs = TREE_OPERAND (rhs, 0);
7539
7540   if (type == error_mark_node
7541       || rhs == error_mark_node
7542       || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7543     return error_mark_node;
7544
7545   if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7546        && TREE_CODE (type) != ARRAY_TYPE
7547        && (TREE_CODE (type) != REFERENCE_TYPE
7548            || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7549       || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7550           && (TREE_CODE (type) != REFERENCE_TYPE
7551               || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7552       || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7553     rhs = decay_conversion (rhs);
7554
7555   rhstype = TREE_TYPE (rhs);
7556   coder = TREE_CODE (rhstype);
7557
7558   if (coder == ERROR_MARK)
7559     return error_mark_node;
7560
7561   /* We accept references to incomplete types, so we can
7562      return here before checking if RHS is of complete type.  */
7563
7564   if (codel == REFERENCE_TYPE)
7565     {
7566       /* This should eventually happen in convert_arguments.  */
7567       int savew = 0, savee = 0;
7568
7569       if (fndecl)
7570         savew = warningcount, savee = errorcount;
7571       rhs = initialize_reference (type, rhs, flags, complain);
7572       if (fndecl)
7573         {
7574           if (warningcount > savew)
7575             warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7576           else if (errorcount > savee)
7577             error ("in passing argument %P of %q+D", parmnum, fndecl);
7578         }
7579       return rhs;
7580     }
7581
7582   if (exp != 0)
7583     exp = require_complete_type_sfinae (exp, complain);
7584   if (exp == error_mark_node)
7585     return error_mark_node;
7586
7587   rhstype = non_reference (rhstype);
7588
7589   type = complete_type (type);
7590
7591   if (DIRECT_INIT_EXPR_P (type, rhs))
7592     /* Don't try to do copy-initialization if we already have
7593        direct-initialization.  */
7594     return rhs;
7595
7596   if (MAYBE_CLASS_TYPE_P (type))
7597     return perform_implicit_conversion_flags (type, rhs, complain, flags);
7598
7599   return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7600                                  complain, flags);
7601 }
7602 \f
7603 /* If RETVAL is the address of, or a reference to, a local variable or
7604    temporary give an appropriate warning.  */
7605
7606 static void
7607 maybe_warn_about_returning_address_of_local (tree retval)
7608 {
7609   tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7610   tree whats_returned = retval;
7611
7612   for (;;)
7613     {
7614       if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7615         whats_returned = TREE_OPERAND (whats_returned, 1);
7616       else if (CONVERT_EXPR_P (whats_returned)
7617                || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7618         whats_returned = TREE_OPERAND (whats_returned, 0);
7619       else
7620         break;
7621     }
7622
7623   if (TREE_CODE (whats_returned) != ADDR_EXPR)
7624     return;
7625   whats_returned = TREE_OPERAND (whats_returned, 0);
7626
7627   if (TREE_CODE (valtype) == REFERENCE_TYPE)
7628     {
7629       if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7630           || TREE_CODE (whats_returned) == TARGET_EXPR)
7631         {
7632           warning (0, "returning reference to temporary");
7633           return;
7634         }
7635       if (TREE_CODE (whats_returned) == VAR_DECL
7636           && DECL_NAME (whats_returned)
7637           && TEMP_NAME_P (DECL_NAME (whats_returned)))
7638         {
7639           warning (0, "reference to non-lvalue returned");
7640           return;
7641         }
7642     }
7643
7644   while (TREE_CODE (whats_returned) == COMPONENT_REF
7645          || TREE_CODE (whats_returned) == ARRAY_REF)
7646     whats_returned = TREE_OPERAND (whats_returned, 0);
7647
7648   if (DECL_P (whats_returned)
7649       && DECL_NAME (whats_returned)
7650       && DECL_FUNCTION_SCOPE_P (whats_returned)
7651       && !(TREE_STATIC (whats_returned)
7652            || TREE_PUBLIC (whats_returned)))
7653     {
7654       if (TREE_CODE (valtype) == REFERENCE_TYPE)
7655         warning (0, "reference to local variable %q+D returned",
7656                  whats_returned);
7657       else
7658         warning (0, "address of local variable %q+D returned",
7659                  whats_returned);
7660       return;
7661     }
7662 }
7663
7664 /* Check that returning RETVAL from the current function is valid.
7665    Return an expression explicitly showing all conversions required to
7666    change RETVAL into the function return type, and to assign it to
7667    the DECL_RESULT for the function.  Set *NO_WARNING to true if
7668    code reaches end of non-void function warning shouldn't be issued
7669    on this RETURN_EXPR.  */
7670
7671 tree
7672 check_return_expr (tree retval, bool *no_warning)
7673 {
7674   tree result;
7675   /* The type actually returned by the function, after any
7676      promotions.  */
7677   tree valtype;
7678   int fn_returns_value_p;
7679   bool named_return_value_okay_p;
7680
7681   *no_warning = false;
7682
7683   /* A `volatile' function is one that isn't supposed to return, ever.
7684      (This is a G++ extension, used to get better code for functions
7685      that call the `volatile' function.)  */
7686   if (TREE_THIS_VOLATILE (current_function_decl))
7687     warning (0, "function declared %<noreturn%> has a %<return%> statement");
7688
7689   /* Check for various simple errors.  */
7690   if (DECL_DESTRUCTOR_P (current_function_decl))
7691     {
7692       if (retval)
7693         error ("returning a value from a destructor");
7694       return NULL_TREE;
7695     }
7696   else if (DECL_CONSTRUCTOR_P (current_function_decl))
7697     {
7698       if (in_function_try_handler)
7699         /* If a return statement appears in a handler of the
7700            function-try-block of a constructor, the program is ill-formed.  */
7701         error ("cannot return from a handler of a function-try-block of a constructor");
7702       else if (retval)
7703         /* You can't return a value from a constructor.  */
7704         error ("returning a value from a constructor");
7705       return NULL_TREE;
7706     }
7707
7708   /* As an extension, deduce lambda return type from a return statement
7709      anywhere in the body.  */
7710   if (retval && LAMBDA_FUNCTION_P (current_function_decl))
7711     {
7712       tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7713       if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
7714         {
7715           tree type = lambda_return_type (retval);
7716           tree oldtype = LAMBDA_EXPR_RETURN_TYPE (lambda);
7717
7718           if (oldtype == NULL_TREE)
7719             apply_lambda_return_type (lambda, type);
7720           /* If one of the answers is type-dependent, we can't do any
7721              better until instantiation time.  */
7722           else if (oldtype == dependent_lambda_return_type_node)
7723             /* Leave it.  */;
7724           else if (type == dependent_lambda_return_type_node)
7725             apply_lambda_return_type (lambda, type);
7726           else if (!same_type_p (type, oldtype))
7727             error ("inconsistent types %qT and %qT deduced for "
7728                    "lambda return type", type, oldtype);
7729         }
7730     }
7731
7732   if (processing_template_decl)
7733     {
7734       current_function_returns_value = 1;
7735       if (check_for_bare_parameter_packs (retval))
7736         retval = error_mark_node;
7737       return retval;
7738     }
7739
7740   /* When no explicit return-value is given in a function with a named
7741      return value, the named return value is used.  */
7742   result = DECL_RESULT (current_function_decl);
7743   valtype = TREE_TYPE (result);
7744   gcc_assert (valtype != NULL_TREE);
7745   fn_returns_value_p = !VOID_TYPE_P (valtype);
7746   if (!retval && DECL_NAME (result) && fn_returns_value_p)
7747     retval = result;
7748
7749   /* Check for a return statement with no return value in a function
7750      that's supposed to return a value.  */
7751   if (!retval && fn_returns_value_p)
7752     {
7753       permerror (input_location, "return-statement with no value, in function returning %qT",
7754                  valtype);
7755       /* Clear this, so finish_function won't say that we reach the
7756          end of a non-void function (which we don't, we gave a
7757          return!).  */
7758       current_function_returns_null = 0;
7759       /* And signal caller that TREE_NO_WARNING should be set on the
7760          RETURN_EXPR to avoid control reaches end of non-void function
7761          warnings in tree-cfg.c.  */
7762       *no_warning = true;
7763     }
7764   /* Check for a return statement with a value in a function that
7765      isn't supposed to return a value.  */
7766   else if (retval && !fn_returns_value_p)
7767     {
7768       if (VOID_TYPE_P (TREE_TYPE (retval)))
7769         /* You can return a `void' value from a function of `void'
7770            type.  In that case, we have to evaluate the expression for
7771            its side-effects.  */
7772           finish_expr_stmt (retval);
7773       else
7774         permerror (input_location, "return-statement with a value, in function "
7775                    "returning 'void'");
7776       current_function_returns_null = 1;
7777
7778       /* There's really no value to return, after all.  */
7779       return NULL_TREE;
7780     }
7781   else if (!retval)
7782     /* Remember that this function can sometimes return without a
7783        value.  */
7784     current_function_returns_null = 1;
7785   else
7786     /* Remember that this function did return a value.  */
7787     current_function_returns_value = 1;
7788
7789   /* Check for erroneous operands -- but after giving ourselves a
7790      chance to provide an error about returning a value from a void
7791      function.  */
7792   if (error_operand_p (retval))
7793     {
7794       current_function_return_value = error_mark_node;
7795       return error_mark_node;
7796     }
7797
7798   /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
7799   if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
7800        || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
7801       && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
7802       && ! flag_check_new
7803       && retval && null_ptr_cst_p (retval))
7804     warning (0, "%<operator new%> must not return NULL unless it is "
7805              "declared %<throw()%> (or -fcheck-new is in effect)");
7806
7807   /* Effective C++ rule 15.  See also start_function.  */
7808   if (warn_ecpp
7809       && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
7810     {
7811       bool warn = true;
7812
7813       /* The function return type must be a reference to the current
7814         class.  */
7815       if (TREE_CODE (valtype) == REFERENCE_TYPE
7816           && same_type_ignoring_top_level_qualifiers_p
7817               (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
7818         {
7819           /* Returning '*this' is obviously OK.  */
7820           if (retval == current_class_ref)
7821             warn = false;
7822           /* If we are calling a function whose return type is the same of
7823              the current class reference, it is ok.  */
7824           else if (TREE_CODE (retval) == INDIRECT_REF
7825                    && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
7826             warn = false;
7827         }
7828
7829       if (warn)
7830         warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
7831     }
7832
7833   /* The fabled Named Return Value optimization, as per [class.copy]/15:
7834
7835      [...]      For  a function with a class return type, if the expression
7836      in the return statement is the name of a local  object,  and  the  cv-
7837      unqualified  type  of  the  local  object  is the same as the function
7838      return type, an implementation is permitted to omit creating the  tem-
7839      porary  object  to  hold  the function return value [...]
7840
7841      So, if this is a value-returning function that always returns the same
7842      local variable, remember it.
7843
7844      It might be nice to be more flexible, and choose the first suitable
7845      variable even if the function sometimes returns something else, but
7846      then we run the risk of clobbering the variable we chose if the other
7847      returned expression uses the chosen variable somehow.  And people expect
7848      this restriction, anyway.  (jason 2000-11-19)
7849
7850      See finish_function and finalize_nrv for the rest of this optimization.  */
7851
7852   named_return_value_okay_p = 
7853     (retval != NULL_TREE
7854      /* Must be a local, automatic variable.  */
7855      && TREE_CODE (retval) == VAR_DECL
7856      && DECL_CONTEXT (retval) == current_function_decl
7857      && ! TREE_STATIC (retval)
7858      && ! DECL_ANON_UNION_VAR_P (retval)
7859      && (DECL_ALIGN (retval)
7860          >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
7861      /* The cv-unqualified type of the returned value must be the
7862         same as the cv-unqualified return type of the
7863         function.  */
7864      && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7865                      (TYPE_MAIN_VARIANT
7866                       (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7867      /* And the returned value must be non-volatile.  */
7868      && ! TYPE_VOLATILE (TREE_TYPE (retval)));
7869      
7870   if (fn_returns_value_p && flag_elide_constructors)
7871     {
7872       if (named_return_value_okay_p
7873           && (current_function_return_value == NULL_TREE
7874               || current_function_return_value == retval))
7875         current_function_return_value = retval;
7876       else
7877         current_function_return_value = error_mark_node;
7878     }
7879
7880   /* We don't need to do any conversions when there's nothing being
7881      returned.  */
7882   if (!retval)
7883     return NULL_TREE;
7884
7885   /* Do any required conversions.  */
7886   if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
7887     /* No conversions are required.  */
7888     ;
7889   else
7890     {
7891       /* The type the function is declared to return.  */
7892       tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7893       int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
7894
7895       /* The functype's return type will have been set to void, if it
7896          was an incomplete type.  Just treat this as 'return;' */
7897       if (VOID_TYPE_P (functype))
7898         return error_mark_node;
7899
7900       /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
7901          treated as an rvalue for the purposes of overload resolution to
7902          favor move constructors over copy constructors.
7903
7904          Note that these conditions are similar to, but not as strict as,
7905          the conditions for the named return value optimization.  */
7906       if ((cxx_dialect != cxx98)
7907           && (TREE_CODE (retval) == VAR_DECL
7908               || TREE_CODE (retval) == PARM_DECL)
7909           && DECL_CONTEXT (retval) == current_function_decl
7910           && !TREE_STATIC (retval)
7911           && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7912                           (TYPE_MAIN_VARIANT
7913                            (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7914           /* This is only interesting for class type.  */
7915           && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
7916         flags = flags | LOOKUP_PREFER_RVALUE;
7917
7918       /* First convert the value to the function's return type, then
7919          to the type of return value's location to handle the
7920          case that functype is smaller than the valtype.  */
7921       retval = convert_for_initialization
7922         (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
7923          tf_warning_or_error);
7924       retval = convert (valtype, retval);
7925
7926       /* If the conversion failed, treat this just like `return;'.  */
7927       if (retval == error_mark_node)
7928         return retval;
7929       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
7930       else if (! cfun->returns_struct
7931                && TREE_CODE (retval) == TARGET_EXPR
7932                && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7933         retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7934                          TREE_OPERAND (retval, 0));
7935       else
7936         maybe_warn_about_returning_address_of_local (retval);
7937     }
7938
7939   /* Actually copy the value returned into the appropriate location.  */
7940   if (retval && retval != result)
7941     retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
7942
7943   return retval;
7944 }
7945
7946 \f
7947 /* Returns nonzero if the pointer-type FROM can be converted to the
7948    pointer-type TO via a qualification conversion.  If CONSTP is -1,
7949    then we return nonzero if the pointers are similar, and the
7950    cv-qualification signature of FROM is a proper subset of that of TO.
7951
7952    If CONSTP is positive, then all outer pointers have been
7953    const-qualified.  */
7954
7955 static int
7956 comp_ptr_ttypes_real (tree to, tree from, int constp)
7957 {
7958   bool to_more_cv_qualified = false;
7959   bool is_opaque_pointer = false;
7960
7961   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7962     {
7963       if (TREE_CODE (to) != TREE_CODE (from))
7964         return 0;
7965
7966       if (TREE_CODE (from) == OFFSET_TYPE
7967           && !same_type_p (TYPE_OFFSET_BASETYPE (from),
7968                            TYPE_OFFSET_BASETYPE (to)))
7969         return 0;
7970
7971       /* Const and volatile mean something different for function types,
7972          so the usual checks are not appropriate.  */
7973       if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7974         {
7975           if (!at_least_as_qualified_p (to, from))
7976             return 0;
7977
7978           if (!at_least_as_qualified_p (from, to))
7979             {
7980               if (constp == 0)
7981                 return 0;
7982               to_more_cv_qualified = true;
7983             }
7984
7985           if (constp > 0)
7986             constp &= TYPE_READONLY (to);
7987         }
7988
7989       if (TREE_CODE (to) == VECTOR_TYPE)
7990         is_opaque_pointer = vector_targets_convertible_p (to, from);
7991
7992       if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
7993         return ((constp >= 0 || to_more_cv_qualified)
7994                 && (is_opaque_pointer
7995                     || same_type_ignoring_top_level_qualifiers_p (to, from)));
7996     }
7997 }
7998
7999 /* When comparing, say, char ** to char const **, this function takes
8000    the 'char *' and 'char const *'.  Do not pass non-pointer/reference
8001    types to this function.  */
8002
8003 int
8004 comp_ptr_ttypes (tree to, tree from)
8005 {
8006   return comp_ptr_ttypes_real (to, from, 1);
8007 }
8008
8009 /* Returns true iff FNTYPE is a non-class type that involves
8010    error_mark_node.  We can get FUNCTION_TYPE with buried error_mark_node
8011    if a parameter type is ill-formed.  */
8012
8013 bool
8014 error_type_p (const_tree type)
8015 {
8016   tree t;
8017
8018   switch (TREE_CODE (type))
8019     {
8020     case ERROR_MARK:
8021       return true;
8022
8023     case POINTER_TYPE:
8024     case REFERENCE_TYPE:
8025     case OFFSET_TYPE:
8026       return error_type_p (TREE_TYPE (type));
8027
8028     case FUNCTION_TYPE:
8029     case METHOD_TYPE:
8030       if (error_type_p (TREE_TYPE (type)))
8031         return true;
8032       for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8033         if (error_type_p (TREE_VALUE (t)))
8034           return true;
8035       return false;
8036
8037     case RECORD_TYPE:
8038       if (TYPE_PTRMEMFUNC_P (type))
8039         return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8040       return false;
8041
8042     default:
8043       return false;
8044     }
8045 }
8046
8047 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8048    type or inheritance-related types, regardless of cv-quals.  */
8049
8050 int
8051 ptr_reasonably_similar (const_tree to, const_tree from)
8052 {
8053   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8054     {
8055       /* Any target type is similar enough to void.  */
8056       if (TREE_CODE (to) == VOID_TYPE)
8057         return !error_type_p (from);
8058       if (TREE_CODE (from) == VOID_TYPE)
8059         return !error_type_p (to);
8060
8061       if (TREE_CODE (to) != TREE_CODE (from))
8062         return 0;
8063
8064       if (TREE_CODE (from) == OFFSET_TYPE
8065           && comptypes (TYPE_OFFSET_BASETYPE (to),
8066                         TYPE_OFFSET_BASETYPE (from),
8067                         COMPARE_BASE | COMPARE_DERIVED))
8068         continue;
8069
8070       if (TREE_CODE (to) == VECTOR_TYPE
8071           && vector_types_convertible_p (to, from, false))
8072         return 1;
8073
8074       if (TREE_CODE (to) == INTEGER_TYPE
8075           && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8076         return 1;
8077
8078       if (TREE_CODE (to) == FUNCTION_TYPE)
8079         return !error_type_p (to) && !error_type_p (from);
8080
8081       if (TREE_CODE (to) != POINTER_TYPE)
8082         return comptypes
8083           (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8084            COMPARE_BASE | COMPARE_DERIVED);
8085     }
8086 }
8087
8088 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8089    pointer-to-member types) are the same, ignoring cv-qualification at
8090    all levels.  */
8091
8092 bool
8093 comp_ptr_ttypes_const (tree to, tree from)
8094 {
8095   bool is_opaque_pointer = false;
8096
8097   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8098     {
8099       if (TREE_CODE (to) != TREE_CODE (from))
8100         return false;
8101
8102       if (TREE_CODE (from) == OFFSET_TYPE
8103           && same_type_p (TYPE_OFFSET_BASETYPE (from),
8104                           TYPE_OFFSET_BASETYPE (to)))
8105           continue;
8106
8107       if (TREE_CODE (to) == VECTOR_TYPE)
8108         is_opaque_pointer = vector_targets_convertible_p (to, from);
8109
8110       if (TREE_CODE (to) != POINTER_TYPE)
8111         return (is_opaque_pointer
8112                 || same_type_ignoring_top_level_qualifiers_p (to, from));
8113     }
8114 }
8115
8116 /* Returns the type qualifiers for this type, including the qualifiers on the
8117    elements for an array type.  */
8118
8119 int
8120 cp_type_quals (const_tree type)
8121 {
8122   int quals;
8123   /* This CONST_CAST is okay because strip_array_types returns its
8124      argument unmodified and we assign it to a const_tree.  */
8125   type = strip_array_types (CONST_CAST_TREE (type));
8126   if (type == error_mark_node
8127       /* Quals on a FUNCTION_TYPE are memfn quals.  */
8128       || TREE_CODE (type) == FUNCTION_TYPE)
8129     return TYPE_UNQUALIFIED;
8130   quals = TYPE_QUALS (type);
8131   /* METHOD and REFERENCE_TYPEs should never have quals.  */
8132   gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8133                && TREE_CODE (type) != REFERENCE_TYPE)
8134               || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8135                   == TYPE_UNQUALIFIED));
8136   return quals;
8137 }
8138
8139 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8140    METHOD_TYPE.  */
8141
8142 int
8143 type_memfn_quals (const_tree type)
8144 {
8145   if (TREE_CODE (type) == FUNCTION_TYPE)
8146     return TYPE_QUALS (type);
8147   else if (TREE_CODE (type) == METHOD_TYPE)
8148     return cp_type_quals (class_of_this_parm (type));
8149   else
8150     gcc_unreachable ();
8151 }
8152
8153 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8154    MEMFN_QUALS.  */
8155
8156 tree
8157 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8158 {
8159   /* Could handle METHOD_TYPE here if necessary.  */
8160   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8161   if (TYPE_QUALS (type) == memfn_quals)
8162     return type;
8163   /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8164      complex.  */
8165   return build_qualified_type (type, memfn_quals);
8166 }
8167
8168 /* Returns nonzero if TYPE is const or volatile.  */
8169
8170 bool
8171 cv_qualified_p (const_tree type)
8172 {
8173   int quals = cp_type_quals (type);
8174   return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8175 }
8176
8177 /* Returns nonzero if the TYPE contains a mutable member.  */
8178
8179 bool
8180 cp_has_mutable_p (const_tree type)
8181 {
8182   /* This CONST_CAST is okay because strip_array_types returns its
8183      argument unmodified and we assign it to a const_tree.  */
8184   type = strip_array_types (CONST_CAST_TREE(type));
8185
8186   return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8187 }
8188
8189 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8190    TYPE_QUALS.  For a VAR_DECL, this may be an optimistic
8191    approximation.  In particular, consider:
8192
8193      int f();
8194      struct S { int i; };
8195      const S s = { f(); }
8196
8197    Here, we will make "s" as TREE_READONLY (because it is declared
8198    "const") -- only to reverse ourselves upon seeing that the
8199    initializer is non-constant.  */
8200
8201 void
8202 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8203 {
8204   tree type = TREE_TYPE (decl);
8205
8206   if (type == error_mark_node)
8207     return;
8208
8209   if (TREE_CODE (decl) == TYPE_DECL)
8210     return;
8211
8212   gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8213                 && type_quals != TYPE_UNQUALIFIED));
8214
8215   /* Avoid setting TREE_READONLY incorrectly.  */
8216   /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8217      constructor can produce constant init, so rely on cp_finish_decl to
8218      clear TREE_READONLY if the variable has non-constant init.  */
8219
8220   /* If the type has a mutable component, that component might be
8221      modified.  */
8222   if (TYPE_HAS_MUTABLE_P (type))
8223     type_quals &= ~TYPE_QUAL_CONST;
8224
8225   c_apply_type_quals_to_decl (type_quals, decl);
8226 }
8227
8228 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
8229    exemplar types such that casting T1 to T2 is casting away constness
8230    if and only if there is no implicit conversion from T1 to T2.  */
8231
8232 static void
8233 casts_away_constness_r (tree *t1, tree *t2)
8234 {
8235   int quals1;
8236   int quals2;
8237
8238   /* [expr.const.cast]
8239
8240      For multi-level pointer to members and multi-level mixed pointers
8241      and pointers to members (conv.qual), the "member" aspect of a
8242      pointer to member level is ignored when determining if a const
8243      cv-qualifier has been cast away.  */
8244   /* [expr.const.cast]
8245
8246      For  two  pointer types:
8247
8248             X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
8249             X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
8250             K is min(N,M)
8251
8252      casting from X1 to X2 casts away constness if, for a non-pointer
8253      type T there does not exist an implicit conversion (clause
8254      _conv_) from:
8255
8256             Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8257
8258      to
8259
8260             Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
8261   if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
8262       || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
8263     {
8264       *t1 = cp_build_qualified_type (void_type_node,
8265                                      cp_type_quals (*t1));
8266       *t2 = cp_build_qualified_type (void_type_node,
8267                                      cp_type_quals (*t2));
8268       return;
8269     }
8270
8271   quals1 = cp_type_quals (*t1);
8272   quals2 = cp_type_quals (*t2);
8273
8274   if (TYPE_PTRMEM_P (*t1))
8275     *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8276   else
8277     *t1 = TREE_TYPE (*t1);
8278   if (TYPE_PTRMEM_P (*t2))
8279     *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8280   else
8281     *t2 = TREE_TYPE (*t2);
8282
8283   casts_away_constness_r (t1, t2);
8284   *t1 = build_pointer_type (*t1);
8285   *t2 = build_pointer_type (*t2);
8286   *t1 = cp_build_qualified_type (*t1, quals1);
8287   *t2 = cp_build_qualified_type (*t2, quals2);
8288 }
8289
8290 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8291    constness.  
8292
8293    ??? This function returns non-zero if casting away qualifiers not
8294    just const.  We would like to return to the caller exactly which
8295    qualifiers are casted away to give more accurate diagnostics.
8296 */
8297
8298 static bool
8299 casts_away_constness (tree t1, tree t2)
8300 {
8301   if (TREE_CODE (t2) == REFERENCE_TYPE)
8302     {
8303       /* [expr.const.cast]
8304
8305          Casting from an lvalue of type T1 to an lvalue of type T2
8306          using a reference cast casts away constness if a cast from an
8307          rvalue of type "pointer to T1" to the type "pointer to T2"
8308          casts away constness.  */
8309       t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8310       return casts_away_constness (build_pointer_type (t1),
8311                                    build_pointer_type (TREE_TYPE (t2)));
8312     }
8313
8314   if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
8315     /* [expr.const.cast]
8316
8317        Casting from an rvalue of type "pointer to data member of X
8318        of type T1" to the type "pointer to data member of Y of type
8319        T2" casts away constness if a cast from an rvalue of type
8320        "pointer to T1" to the type "pointer to T2" casts away
8321        constness.  */
8322     return casts_away_constness
8323       (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8324        build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
8325
8326   /* Casting away constness is only something that makes sense for
8327      pointer or reference types.  */
8328   if (TREE_CODE (t1) != POINTER_TYPE
8329       || TREE_CODE (t2) != POINTER_TYPE)
8330     return false;
8331
8332   /* Top-level qualifiers don't matter.  */
8333   t1 = TYPE_MAIN_VARIANT (t1);
8334   t2 = TYPE_MAIN_VARIANT (t2);
8335   casts_away_constness_r (&t1, &t2);
8336   if (!can_convert (t2, t1))
8337     return true;
8338
8339   return false;
8340 }
8341
8342 /* If T is a REFERENCE_TYPE return the type to which T refers.
8343    Otherwise, return T itself.  */
8344
8345 tree
8346 non_reference (tree t)
8347 {
8348   if (t && TREE_CODE (t) == REFERENCE_TYPE)
8349     t = TREE_TYPE (t);
8350   return t;
8351 }
8352
8353
8354 /* Return nonzero if REF is an lvalue valid for this language;
8355    otherwise, print an error message and return zero.  USE says
8356    how the lvalue is being used and so selects the error message.  */
8357
8358 int
8359 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8360 {
8361   cp_lvalue_kind kind = lvalue_kind (ref);
8362
8363   if (kind == clk_none)
8364     {
8365       if (complain & tf_error)
8366         lvalue_error (input_location, use);
8367       return 0;
8368     }
8369   else if (kind & (clk_rvalueref|clk_class))
8370     {
8371       if (!(complain & tf_error))
8372         return 0;
8373       if (kind & clk_class)
8374         /* Make this a permerror because we used to accept it.  */
8375         permerror (input_location, "using temporary as lvalue");
8376       else
8377         error ("using xvalue (rvalue reference) as lvalue");
8378     }
8379   return 1;
8380 }
8381
8382 /* Return true if a user-defined literal operator is a raw operator.  */
8383
8384 bool
8385 check_raw_literal_operator (const_tree decl)
8386 {
8387   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8388   tree argtype;
8389   int arity;
8390   bool maybe_raw_p = false;
8391
8392   /* Count the number and type of arguments and check for ellipsis.  */
8393   for (argtype = argtypes, arity = 0;
8394        argtype && argtype != void_list_node;
8395        ++arity, argtype = TREE_CHAIN (argtype))
8396     {
8397       tree t = TREE_VALUE (argtype);
8398
8399       if (same_type_p (t, const_string_type_node))
8400         maybe_raw_p = true;
8401     }
8402   if (!argtype)
8403     return false; /* Found ellipsis.  */
8404
8405   if (!maybe_raw_p || arity != 1)
8406     return false;
8407
8408   return true;
8409 }
8410
8411
8412 /* Return true if a user-defined literal operator has one of the allowed
8413    argument types.  */
8414
8415 bool
8416 check_literal_operator_args (const_tree decl,
8417                              bool *long_long_unsigned_p, bool *long_double_p)
8418 {
8419   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8420   if (processing_template_decl)
8421     return (argtypes == NULL_TREE
8422             || same_type_p (TREE_VALUE (argtypes), void_type_node));
8423   else
8424     {
8425       tree argtype;
8426       int arity;
8427       int max_arity = 2;
8428       bool found_string_p = false;
8429       bool maybe_raw_p = false;
8430       bool found_size_p = false;
8431
8432       *long_long_unsigned_p = false;
8433       *long_double_p = false;
8434
8435       /* Count the number and type of arguments and check for ellipsis.  */
8436       for (argtype = argtypes, arity = 0;
8437            argtype && argtype != void_list_node;
8438            argtype = TREE_CHAIN (argtype))
8439         {
8440           tree t = TREE_VALUE (argtype);
8441           ++arity;
8442
8443           if (TREE_CODE (t) == POINTER_TYPE)
8444             {
8445               t = TREE_TYPE (t);
8446               if (cp_type_quals (t) != TYPE_QUAL_CONST)
8447                 return false;
8448               t = TYPE_MAIN_VARIANT (t);
8449               if (same_type_p (t, char_type_node))
8450                 {
8451                   found_string_p = true;
8452                   maybe_raw_p = true;
8453                 }
8454               else if (same_type_p (t, wchar_type_node))
8455                 found_string_p = true;
8456               else if (same_type_p (t, char16_type_node))
8457                 found_string_p = true;
8458               else if (same_type_p (t, char32_type_node))
8459                 found_string_p = true;
8460               else
8461                 return false;
8462             }
8463           else if (same_type_p (t, size_type_node))
8464             {
8465               if (!found_string_p)
8466                 return false;
8467               found_size_p = true;
8468             }
8469           else if (same_type_p (t, long_long_unsigned_type_node))
8470             {
8471               max_arity = 1;
8472               *long_long_unsigned_p = true;
8473             }
8474           else if (same_type_p (t, long_double_type_node))
8475             {
8476               max_arity = 1;
8477               *long_double_p = true;
8478             }
8479           else if (same_type_p (t, char_type_node))
8480             max_arity = 1;
8481           else if (same_type_p (t, wchar_type_node))
8482             max_arity = 1;
8483           else if (same_type_p (t, char16_type_node))
8484             max_arity = 1;
8485           else if (same_type_p (t, char32_type_node))
8486             max_arity = 1;
8487           else
8488             return false;
8489         }
8490       if (!argtype)
8491         return false; /* Found ellipsis.  */
8492
8493       if (arity > max_arity)
8494         return false;
8495
8496       if (found_string_p && !maybe_raw_p && !found_size_p)
8497         return false;
8498
8499       return true;
8500     }
8501 }
8502