OSDN Git Service

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