OSDN Git Service

* typeck.c (composite_pointer_error): New function.
[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    Free Software Foundation, Inc.
5    Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23
24 /* This file is part of the C++ front end.
25    It contains routines to build C++ expressions given their operands,
26    including computing the types of the result, C and C++ specific error
27    checks, and some optimization.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "diagnostic.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "convert.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "params.h"
44
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48                                     tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree, 
51                                           tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *);
58 static bool casts_away_constness (tree, tree);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
63                               tsubst_flags_t);
64
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66    does not have an incomplete type.  (That includes void types.)
67    Returns error_mark_node if the VALUE does not have
68    complete type when this function returns.  */
69
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
72 {
73   tree type;
74
75   if (processing_template_decl || value == error_mark_node)
76     return value;
77
78   if (TREE_CODE (value) == OVERLOAD)
79     type = unknown_type_node;
80   else
81     type = TREE_TYPE (value);
82
83   if (type == error_mark_node)
84     return error_mark_node;
85
86   /* First, detect a valid value with a complete type.  */
87   if (COMPLETE_TYPE_P (type))
88     return value;
89
90   if (complete_type_or_maybe_complain (type, value, complain))
91     return value;
92   else
93     return error_mark_node;
94 }
95
96 tree
97 require_complete_type (tree value)
98 {
99   return require_complete_type_sfinae (value, tf_warning_or_error);
100 }
101
102 /* Try to complete TYPE, if it is incomplete.  For example, if TYPE is
103    a template instantiation, do the instantiation.  Returns TYPE,
104    whether or not it could be completed, unless something goes
105    horribly wrong, in which case the error_mark_node is returned.  */
106
107 tree
108 complete_type (tree type)
109 {
110   if (type == NULL_TREE)
111     /* Rather than crash, we return something sure to cause an error
112        at some point.  */
113     return error_mark_node;
114
115   if (type == error_mark_node || COMPLETE_TYPE_P (type))
116     ;
117   else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
118     {
119       tree t = complete_type (TREE_TYPE (type));
120       unsigned int needs_constructing, has_nontrivial_dtor;
121       if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122         layout_type (type);
123       needs_constructing
124         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125       has_nontrivial_dtor
126         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
128         {
129           TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
131         }
132     }
133   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134     instantiate_class_template (TYPE_MAIN_VARIANT (type));
135
136   return type;
137 }
138
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140    VALUE is used for informative diagnostics.
141    Returns NULL_TREE if the type cannot be made complete.  */
142
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
145 {
146   type = complete_type (type);
147   if (type == error_mark_node)
148     /* We already issued an error.  */
149     return NULL_TREE;
150   else if (!COMPLETE_TYPE_P (type))
151     {
152       if (complain & tf_error)
153         cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154       return NULL_TREE;
155     }
156   else
157     return type;
158 }
159
160 tree
161 complete_type_or_else (tree type, tree value)
162 {
163   return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
164 }
165
166 /* Return truthvalue of whether type of EXP is instantiated.  */
167
168 int
169 type_unknown_p (const_tree exp)
170 {
171   return (TREE_CODE (exp) == TREE_LIST
172           || TREE_TYPE (exp) == unknown_type_node);
173 }
174
175 \f
176 /* Return the common type of two parameter lists.
177    We assume that comptypes has already been done and returned 1;
178    if that isn't so, this may crash.
179
180    As an optimization, free the space we allocate if the parameter
181    lists are already common.  */
182
183 static tree
184 commonparms (tree p1, tree p2)
185 {
186   tree oldargs = p1, newargs, n;
187   int i, len;
188   int any_change = 0;
189
190   len = list_length (p1);
191   newargs = tree_last (p1);
192
193   if (newargs == void_list_node)
194     i = 1;
195   else
196     {
197       i = 0;
198       newargs = 0;
199     }
200
201   for (; i < len; i++)
202     newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
203
204   n = newargs;
205
206   for (i = 0; p1;
207        p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
208     {
209       if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
210         {
211           TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212           any_change = 1;
213         }
214       else if (! TREE_PURPOSE (p1))
215         {
216           if (TREE_PURPOSE (p2))
217             {
218               TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219               any_change = 1;
220             }
221         }
222       else
223         {
224           if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225             any_change = 1;
226           TREE_PURPOSE (n) = TREE_PURPOSE (p2);
227         }
228       if (TREE_VALUE (p1) != TREE_VALUE (p2))
229         {
230           any_change = 1;
231           TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
232         }
233       else
234         TREE_VALUE (n) = TREE_VALUE (p1);
235     }
236   if (! any_change)
237     return oldargs;
238
239   return newargs;
240 }
241
242 /* Given a type, perhaps copied for a typedef,
243    find the "original" version of it.  */
244 static tree
245 original_type (tree t)
246 {
247   int quals = cp_type_quals (t);
248   while (t != error_mark_node
249          && TYPE_NAME (t) != NULL_TREE)
250     {
251       tree x = TYPE_NAME (t);
252       if (TREE_CODE (x) != TYPE_DECL)
253         break;
254       x = DECL_ORIGINAL_TYPE (x);
255       if (x == NULL_TREE)
256         break;
257       t = x;
258     }
259   return cp_build_qualified_type (t, quals);
260 }
261
262 /* Return the common type for two arithmetic types T1 and T2 under the
263    usual arithmetic conversions.  The default conversions have already
264    been applied, and enumerated types converted to their compatible
265    integer types.  */
266
267 static tree
268 cp_common_type (tree t1, tree t2)
269 {
270   enum tree_code code1 = TREE_CODE (t1);
271   enum tree_code code2 = TREE_CODE (t2);
272   tree attributes;
273
274
275   /* In what follows, we slightly generalize the rules given in [expr] so
276      as to deal with `long long' and `complex'.  First, merge the
277      attributes.  */
278   attributes = (*targetm.merge_type_attributes) (t1, t2);
279
280   if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
281     {
282       if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283         return build_type_attribute_variant (t1, attributes);
284       else
285         return NULL_TREE;
286     }
287
288   /* FIXME: Attributes.  */
289   gcc_assert (ARITHMETIC_TYPE_P (t1)
290               || TREE_CODE (t1) == VECTOR_TYPE
291               || UNSCOPED_ENUM_P (t1));
292   gcc_assert (ARITHMETIC_TYPE_P (t2)
293               || TREE_CODE (t2) == VECTOR_TYPE
294               || UNSCOPED_ENUM_P (t2));
295
296   /* If one type is complex, form the common type of the non-complex
297      components, then make that complex.  Use T1 or T2 if it is the
298      required type.  */
299   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
300     {
301       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303       tree subtype
304         = type_after_usual_arithmetic_conversions (subtype1, subtype2);
305
306       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307         return build_type_attribute_variant (t1, attributes);
308       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309         return build_type_attribute_variant (t2, attributes);
310       else
311         return build_type_attribute_variant (build_complex_type (subtype),
312                                              attributes);
313     }
314
315   if (code1 == VECTOR_TYPE)
316     {
317       /* When we get here we should have two vectors of the same size.
318          Just prefer the unsigned one if present.  */
319       if (TYPE_UNSIGNED (t1))
320         return build_type_attribute_variant (t1, attributes);
321       else
322         return build_type_attribute_variant (t2, attributes);
323     }
324
325   /* If only one is real, use it as the result.  */
326   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327     return build_type_attribute_variant (t1, attributes);
328   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329     return build_type_attribute_variant (t2, attributes);
330
331   /* Both real or both integers; use the one with greater precision.  */
332   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333     return build_type_attribute_variant (t1, attributes);
334   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335     return build_type_attribute_variant (t2, attributes);
336
337   /* The types are the same; no need to do anything fancy.  */
338   if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339     return build_type_attribute_variant (t1, attributes);
340
341   if (code1 != REAL_TYPE)
342     {
343       /* If one is unsigned long long, then convert the other to unsigned
344          long long.  */
345       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346           || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347         return build_type_attribute_variant (long_long_unsigned_type_node,
348                                              attributes);
349       /* If one is a long long, and the other is an unsigned long, and
350          long long can represent all the values of an unsigned long, then
351          convert to a long long.  Otherwise, convert to an unsigned long
352          long.  Otherwise, if either operand is long long, convert the
353          other to long long.
354
355          Since we're here, we know the TYPE_PRECISION is the same;
356          therefore converting to long long cannot represent all the values
357          of an unsigned long, so we choose unsigned long long in that
358          case.  */
359       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360           || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
361         {
362           tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363                     ? long_long_unsigned_type_node
364                     : long_long_integer_type_node);
365           return build_type_attribute_variant (t, attributes);
366         }
367       if (int128_integer_type_node != NULL_TREE
368           && (same_type_p (TYPE_MAIN_VARIANT (t1),
369                            int128_integer_type_node)
370               || same_type_p (TYPE_MAIN_VARIANT (t2),
371                               int128_integer_type_node)))
372         {
373           tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374                     ? int128_unsigned_type_node
375                     : int128_integer_type_node);
376           return build_type_attribute_variant (t, attributes);
377         }
378
379       /* Go through the same procedure, but for longs.  */
380       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381           || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382         return build_type_attribute_variant (long_unsigned_type_node,
383                                              attributes);
384       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385           || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
386         {
387           tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388                     ? long_unsigned_type_node : long_integer_type_node);
389           return build_type_attribute_variant (t, attributes);
390         }
391       /* Otherwise prefer the unsigned one.  */
392       if (TYPE_UNSIGNED (t1))
393         return build_type_attribute_variant (t1, attributes);
394       else
395         return build_type_attribute_variant (t2, attributes);
396     }
397   else
398     {
399       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400           || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401         return build_type_attribute_variant (long_double_type_node,
402                                              attributes);
403       if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404           || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405         return build_type_attribute_variant (double_type_node,
406                                              attributes);
407       if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408           || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409         return build_type_attribute_variant (float_type_node,
410                                              attributes);
411
412       /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413          the standard C++ floating-point types.  Logic earlier in this
414          function has already eliminated the possibility that
415          TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416          compelling reason to choose one or the other.  */
417       return build_type_attribute_variant (t1, attributes);
418     }
419 }
420
421 /* T1 and T2 are arithmetic or enumeration types.  Return the type
422    that will result from the "usual arithmetic conversions" on T1 and
423    T2 as described in [expr].  */
424
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
427 {
428   gcc_assert (ARITHMETIC_TYPE_P (t1)
429               || TREE_CODE (t1) == VECTOR_TYPE
430               || UNSCOPED_ENUM_P (t1));
431   gcc_assert (ARITHMETIC_TYPE_P (t2)
432               || TREE_CODE (t2) == VECTOR_TYPE
433               || UNSCOPED_ENUM_P (t2));
434
435   /* Perform the integral promotions.  We do not promote real types here.  */
436   if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437       && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
438     {
439       t1 = type_promotes_to (t1);
440       t2 = type_promotes_to (t2);
441     }
442
443   return cp_common_type (t1, t2);
444 }
445
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448                          composite_pointer_operation operation)
449 {
450   switch (operation)
451     {
452     case CPO_COMPARISON:
453       emit_diagnostic (kind, input_location, 0,
454                        "comparison between "
455                        "distinct pointer types %qT and %qT lacks a cast",
456                        t1, t2);
457       break;
458     case CPO_CONVERSION:
459       emit_diagnostic (kind, input_location, 0,
460                        "conversion between "
461                        "distinct pointer types %qT and %qT lacks a cast",
462                        t1, t2);
463       break;
464     case CPO_CONDITIONAL_EXPR:
465       emit_diagnostic (kind, input_location, 0,
466                        "conditional expression between "
467                        "distinct pointer types %qT and %qT lacks a cast",
468                        t1, t2);
469       break;
470     default:
471       gcc_unreachable ();
472     }
473 }
474
475 /* Subroutine of composite_pointer_type to implement the recursive
476    case.  See that function for documentation of the parameters.  */
477
478 static tree
479 composite_pointer_type_r (tree t1, tree t2, 
480                           composite_pointer_operation operation,
481                           tsubst_flags_t complain)
482 {
483   tree pointee1;
484   tree pointee2;
485   tree result_type;
486   tree attributes;
487
488   /* Determine the types pointed to by T1 and T2.  */
489   if (TREE_CODE (t1) == POINTER_TYPE)
490     {
491       pointee1 = TREE_TYPE (t1);
492       pointee2 = TREE_TYPE (t2);
493     }
494   else
495     {
496       pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497       pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
498     }
499
500   /* [expr.rel]
501
502      Otherwise, the composite pointer type is a pointer type
503      similar (_conv.qual_) to the type of one of the operands,
504      with a cv-qualification signature (_conv.qual_) that is the
505      union of the cv-qualification signatures of the operand
506      types.  */
507   if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508     result_type = pointee1;
509   else if ((TREE_CODE (pointee1) == POINTER_TYPE
510             && TREE_CODE (pointee2) == POINTER_TYPE)
511            || (TYPE_PTR_TO_MEMBER_P (pointee1)
512                && TYPE_PTR_TO_MEMBER_P (pointee2)))
513     result_type = composite_pointer_type_r (pointee1, pointee2, operation,
514                                             complain);
515   else
516     {
517       if (complain & tf_error)
518         composite_pointer_error (DK_PERMERROR, t1, t2, operation);
519
520       result_type = void_type_node;
521     }
522   result_type = cp_build_qualified_type (result_type,
523                                          (cp_type_quals (pointee1)
524                                           | cp_type_quals (pointee2)));
525   /* If the original types were pointers to members, so is the
526      result.  */
527   if (TYPE_PTR_TO_MEMBER_P (t1))
528     {
529       if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
530                         TYPE_PTRMEM_CLASS_TYPE (t2))
531           && (complain & tf_error))
532         composite_pointer_error (DK_PERMERROR, t1, t2, operation);
533       result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
534                                        result_type);
535     }
536   else
537     result_type = build_pointer_type (result_type);
538
539   /* Merge the attributes.  */
540   attributes = (*targetm.merge_type_attributes) (t1, t2);
541   return build_type_attribute_variant (result_type, attributes);
542 }
543
544 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
545    ARG1 and ARG2 are the values with those types.  The OPERATION is to
546    describe the operation between the pointer types,
547    in case an error occurs.
548
549    This routine also implements the computation of a common type for
550    pointers-to-members as per [expr.eq].  */
551
552 tree
553 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
554                         composite_pointer_operation operation, 
555                         tsubst_flags_t complain)
556 {
557   tree class1;
558   tree class2;
559
560   /* [expr.rel]
561
562      If one operand is a null pointer constant, the composite pointer
563      type is the type of the other operand.  */
564   if (null_ptr_cst_p (arg1))
565     return t2;
566   if (null_ptr_cst_p (arg2))
567     return t1;
568
569   /* We have:
570
571        [expr.rel]
572
573        If one of the operands has type "pointer to cv1 void*", then
574        the other has type "pointer to cv2T", and the composite pointer
575        type is "pointer to cv12 void", where cv12 is the union of cv1
576        and cv2.
577
578     If either type is a pointer to void, make sure it is T1.  */
579   if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
580     {
581       tree t;
582       t = t1;
583       t1 = t2;
584       t2 = t;
585     }
586
587   /* Now, if T1 is a pointer to void, merge the qualifiers.  */
588   if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
589     {
590       tree attributes;
591       tree result_type;
592
593       if (TYPE_PTRFN_P (t2) && (complain & tf_error))
594         {
595           switch (operation)
596               {
597               case CPO_COMPARISON:
598                 pedwarn (input_location, OPT_pedantic, 
599                          "ISO C++ forbids comparison between "
600                          "pointer of type %<void *%> and pointer-to-function");
601                 break;
602               case CPO_CONVERSION:
603                 pedwarn (input_location, OPT_pedantic,
604                          "ISO C++ forbids conversion between "
605                          "pointer of type %<void *%> and pointer-to-function");
606                 break;
607               case CPO_CONDITIONAL_EXPR:
608                 pedwarn (input_location, OPT_pedantic,
609                          "ISO C++ forbids conditional expression between "
610                          "pointer of type %<void *%> and pointer-to-function");
611                 break;
612               default:
613                 gcc_unreachable ();
614               }
615         }
616       result_type
617         = cp_build_qualified_type (void_type_node,
618                                    (cp_type_quals (TREE_TYPE (t1))
619                                     | cp_type_quals (TREE_TYPE (t2))));
620       result_type = build_pointer_type (result_type);
621       /* Merge the attributes.  */
622       attributes = (*targetm.merge_type_attributes) (t1, t2);
623       return build_type_attribute_variant (result_type, attributes);
624     }
625
626   if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
627       && TREE_CODE (t2) == POINTER_TYPE)
628     {
629       if (objc_have_common_type (t1, t2, -3, NULL_TREE))
630         return objc_common_type (t1, t2);
631     }
632
633   /* [expr.eq] permits the application of a pointer conversion to
634      bring the pointers to a common type.  */
635   if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
636       && CLASS_TYPE_P (TREE_TYPE (t1))
637       && CLASS_TYPE_P (TREE_TYPE (t2))
638       && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
639                                                      TREE_TYPE (t2)))
640     {
641       class1 = TREE_TYPE (t1);
642       class2 = TREE_TYPE (t2);
643
644       if (DERIVED_FROM_P (class1, class2))
645         t2 = (build_pointer_type
646               (cp_build_qualified_type (class1, cp_type_quals (class2))));
647       else if (DERIVED_FROM_P (class2, class1))
648         t1 = (build_pointer_type
649               (cp_build_qualified_type (class2, cp_type_quals (class1))));
650       else
651         {
652           if (complain & tf_error)
653             composite_pointer_error (DK_ERROR, t1, t2, operation);
654           return error_mark_node;
655         }
656     }
657   /* [expr.eq] permits the application of a pointer-to-member
658      conversion to change the class type of one of the types.  */
659   else if (TYPE_PTR_TO_MEMBER_P (t1)
660            && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
661                             TYPE_PTRMEM_CLASS_TYPE (t2)))
662     {
663       class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
664       class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
665
666       if (DERIVED_FROM_P (class1, class2))
667         t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
668       else if (DERIVED_FROM_P (class2, class1))
669         t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
670       else
671         {
672           if (complain & tf_error)
673             switch (operation)
674               {
675               case CPO_COMPARISON:
676                 error ("comparison between distinct "
677                        "pointer-to-member types %qT and %qT lacks a cast",
678                        t1, t2);
679                 break;
680               case CPO_CONVERSION:
681                 error ("conversion between distinct "
682                        "pointer-to-member types %qT and %qT lacks a cast",
683                        t1, t2);
684                 break;
685               case CPO_CONDITIONAL_EXPR:
686                 error ("conditional expression between distinct "
687                        "pointer-to-member types %qT and %qT lacks a cast",
688                        t1, t2);
689                 break;
690               default:
691                 gcc_unreachable ();
692               }
693           return error_mark_node;
694         }
695     }
696
697   return composite_pointer_type_r (t1, t2, operation, complain);
698 }
699
700 /* Return the merged type of two types.
701    We assume that comptypes has already been done and returned 1;
702    if that isn't so, this may crash.
703
704    This just combines attributes and default arguments; any other
705    differences would cause the two types to compare unalike.  */
706
707 tree
708 merge_types (tree t1, tree t2)
709 {
710   enum tree_code code1;
711   enum tree_code code2;
712   tree attributes;
713
714   /* Save time if the two types are the same.  */
715   if (t1 == t2)
716     return t1;
717   if (original_type (t1) == original_type (t2))
718     return t1;
719
720   /* If one type is nonsense, use the other.  */
721   if (t1 == error_mark_node)
722     return t2;
723   if (t2 == error_mark_node)
724     return t1;
725
726   /* Merge the attributes.  */
727   attributes = (*targetm.merge_type_attributes) (t1, t2);
728
729   if (TYPE_PTRMEMFUNC_P (t1))
730     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
731   if (TYPE_PTRMEMFUNC_P (t2))
732     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
733
734   code1 = TREE_CODE (t1);
735   code2 = TREE_CODE (t2);
736   if (code1 != code2)
737     {
738       gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
739       if (code1 == TYPENAME_TYPE)
740         {
741           t1 = resolve_typename_type (t1, /*only_current_p=*/true);
742           code1 = TREE_CODE (t1);
743         }
744       else
745         {
746           t2 = resolve_typename_type (t2, /*only_current_p=*/true);
747           code2 = TREE_CODE (t2);
748         }
749     }
750
751   switch (code1)
752     {
753     case POINTER_TYPE:
754     case REFERENCE_TYPE:
755       /* For two pointers, do this recursively on the target type.  */
756       {
757         tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
758         int quals = cp_type_quals (t1);
759
760         if (code1 == POINTER_TYPE)
761           t1 = build_pointer_type (target);
762         else
763           t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
764         t1 = build_type_attribute_variant (t1, attributes);
765         t1 = cp_build_qualified_type (t1, quals);
766
767         if (TREE_CODE (target) == METHOD_TYPE)
768           t1 = build_ptrmemfunc_type (t1);
769
770         return t1;
771       }
772
773     case OFFSET_TYPE:
774       {
775         int quals;
776         tree pointee;
777         quals = cp_type_quals (t1);
778         pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
779                                TYPE_PTRMEM_POINTED_TO_TYPE (t2));
780         t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
781                                 pointee);
782         t1 = cp_build_qualified_type (t1, quals);
783         break;
784       }
785
786     case ARRAY_TYPE:
787       {
788         tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
789         /* Save space: see if the result is identical to one of the args.  */
790         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
791           return build_type_attribute_variant (t1, attributes);
792         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
793           return build_type_attribute_variant (t2, attributes);
794         /* Merge the element types, and have a size if either arg has one.  */
795         t1 = build_cplus_array_type
796           (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
797         break;
798       }
799
800     case FUNCTION_TYPE:
801       /* Function types: prefer the one that specified arg types.
802          If both do, merge the arg types.  Also merge the return types.  */
803       {
804         tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
805         tree p1 = TYPE_ARG_TYPES (t1);
806         tree p2 = TYPE_ARG_TYPES (t2);
807         tree parms;
808         tree rval, raises;
809
810         /* Save space: see if the result is identical to one of the args.  */
811         if (valtype == TREE_TYPE (t1) && ! p2)
812           return cp_build_type_attribute_variant (t1, attributes);
813         if (valtype == TREE_TYPE (t2) && ! p1)
814           return cp_build_type_attribute_variant (t2, attributes);
815
816         /* Simple way if one arg fails to specify argument types.  */
817         if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
818           parms = p2;
819         else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
820           parms = p1;
821         else
822           parms = commonparms (p1, p2);
823
824         rval = build_function_type (valtype, parms);
825         gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
826         rval = apply_memfn_quals (rval, type_memfn_quals (t1));
827         raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
828                                              TYPE_RAISES_EXCEPTIONS (t2));
829         t1 = build_exception_variant (rval, raises);
830         break;
831       }
832
833     case METHOD_TYPE:
834       {
835         /* Get this value the long way, since TYPE_METHOD_BASETYPE
836            is just the main variant of this.  */
837         tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
838         tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
839                                                   TYPE_RAISES_EXCEPTIONS (t2));
840         tree t3;
841
842         /* If this was a member function type, get back to the
843            original type of type member function (i.e., without
844            the class instance variable up front.  */
845         t1 = build_function_type (TREE_TYPE (t1),
846                                   TREE_CHAIN (TYPE_ARG_TYPES (t1)));
847         t2 = build_function_type (TREE_TYPE (t2),
848                                   TREE_CHAIN (TYPE_ARG_TYPES (t2)));
849         t3 = merge_types (t1, t2);
850         t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
851                                          TYPE_ARG_TYPES (t3));
852         t1 = build_exception_variant (t3, raises);
853         break;
854       }
855
856     case TYPENAME_TYPE:
857       /* There is no need to merge attributes into a TYPENAME_TYPE.
858          When the type is instantiated it will have whatever
859          attributes result from the instantiation.  */
860       return t1;
861
862     default:;
863     }
864
865   if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
866     return t1;
867   else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
868     return t2;
869   else
870     return cp_build_type_attribute_variant (t1, attributes);
871 }
872
873 /* Return the ARRAY_TYPE type without its domain.  */
874
875 tree
876 strip_array_domain (tree type)
877 {
878   tree t2;
879   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
880   if (TYPE_DOMAIN (type) == NULL_TREE)
881     return type;
882   t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
883   return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
884 }
885
886 /* Wrapper around cp_common_type that is used by c-common.c and other
887    front end optimizations that remove promotions.  
888
889    Return the common type for two arithmetic types T1 and T2 under the
890    usual arithmetic conversions.  The default conversions have already
891    been applied, and enumerated types converted to their compatible
892    integer types.  */
893
894 tree
895 common_type (tree t1, tree t2)
896 {
897   /* If one type is nonsense, use the other  */
898   if (t1 == error_mark_node)
899     return t2;
900   if (t2 == error_mark_node)
901     return t1;
902
903   return cp_common_type (t1, t2);
904 }
905
906 /* Return the common type of two pointer types T1 and T2.  This is the
907    type for the result of most arithmetic operations if the operands
908    have the given two types.
909  
910    We assume that comp_target_types has already been done and returned
911    nonzero; if that isn't so, this may crash.  */
912
913 tree
914 common_pointer_type (tree t1, tree t2)
915 {
916   gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
917               || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
918               || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
919
920   return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
921                                  CPO_CONVERSION, tf_warning_or_error);
922 }
923 \f
924 /* Compare two exception specifier types for exactness or subsetness, if
925    allowed. Returns false for mismatch, true for match (same, or
926    derived and !exact).
927
928    [except.spec] "If a class X ... objects of class X or any class publicly
929    and unambiguously derived from X. Similarly, if a pointer type Y * ...
930    exceptions of type Y * or that are pointers to any type publicly and
931    unambiguously derived from Y. Otherwise a function only allows exceptions
932    that have the same type ..."
933    This does not mention cv qualifiers and is different to what throw
934    [except.throw] and catch [except.catch] will do. They will ignore the
935    top level cv qualifiers, and allow qualifiers in the pointer to class
936    example.
937
938    We implement the letter of the standard.  */
939
940 static bool
941 comp_except_types (tree a, tree b, bool exact)
942 {
943   if (same_type_p (a, b))
944     return true;
945   else if (!exact)
946     {
947       if (cp_type_quals (a) || cp_type_quals (b))
948         return false;
949
950       if (TREE_CODE (a) == POINTER_TYPE
951           && TREE_CODE (b) == POINTER_TYPE)
952         {
953           a = TREE_TYPE (a);
954           b = TREE_TYPE (b);
955           if (cp_type_quals (a) || cp_type_quals (b))
956             return false;
957         }
958
959       if (TREE_CODE (a) != RECORD_TYPE
960           || TREE_CODE (b) != RECORD_TYPE)
961         return false;
962
963       if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
964         return true;
965     }
966   return false;
967 }
968
969 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
970    If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
971    If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
972    If EXACT is ce_exact, the specs must be exactly the same. Exception lists
973    are unordered, but we've already filtered out duplicates. Most lists will
974    be in order, we should try to make use of that.  */
975
976 bool
977 comp_except_specs (const_tree t1, const_tree t2, int exact)
978 {
979   const_tree probe;
980   const_tree base;
981   int  length = 0;
982
983   if (t1 == t2)
984     return true;
985
986   /* First handle noexcept.  */
987   if (exact < ce_exact)
988     {
989       /* noexcept(false) is compatible with any throwing dynamic-exc-spec
990          and stricter than any spec.  */
991       if (t1 == noexcept_false_spec)
992         return !nothrow_spec_p (t2) || exact == ce_derived;
993       /* Even a derived noexcept(false) is compatible with a throwing
994          dynamic spec.  */
995       if (t2 == noexcept_false_spec)
996         return !nothrow_spec_p (t1);
997
998       /* Otherwise, if we aren't looking for an exact match, noexcept is
999          equivalent to throw().  */
1000       if (t1 == noexcept_true_spec)
1001         t1 = empty_except_spec;
1002       if (t2 == noexcept_true_spec)
1003         t2 = empty_except_spec;
1004     }
1005
1006   /* If any noexcept is left, it is only comparable to itself;
1007      either we're looking for an exact match or we're redeclaring a
1008      template with dependent noexcept.  */
1009   if ((t1 && TREE_PURPOSE (t1))
1010       || (t2 && TREE_PURPOSE (t2)))
1011     return (t1 && t2
1012             && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1013
1014   if (t1 == NULL_TREE)                     /* T1 is ...  */
1015     return t2 == NULL_TREE || exact == ce_derived;
1016   if (!TREE_VALUE (t1))                    /* t1 is EMPTY */
1017     return t2 != NULL_TREE && !TREE_VALUE (t2);
1018   if (t2 == NULL_TREE)                     /* T2 is ...  */
1019     return false;
1020   if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1021     return exact == ce_derived;
1022
1023   /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1024      Count how many we find, to determine exactness. For exact matching and
1025      ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1026      O(nm).  */
1027   for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1028     {
1029       for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1030         {
1031           tree a = TREE_VALUE (probe);
1032           tree b = TREE_VALUE (t2);
1033
1034           if (comp_except_types (a, b, exact))
1035             {
1036               if (probe == base && exact > ce_derived)
1037                 base = TREE_CHAIN (probe);
1038               length++;
1039               break;
1040             }
1041         }
1042       if (probe == NULL_TREE)
1043         return false;
1044     }
1045   return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1046 }
1047
1048 /* Compare the array types T1 and T2.  ALLOW_REDECLARATION is true if
1049    [] can match [size].  */
1050
1051 static bool
1052 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1053 {
1054   tree d1;
1055   tree d2;
1056   tree max1, max2;
1057
1058   if (t1 == t2)
1059     return true;
1060
1061   /* The type of the array elements must be the same.  */
1062   if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1063     return false;
1064
1065   d1 = TYPE_DOMAIN (t1);
1066   d2 = TYPE_DOMAIN (t2);
1067
1068   if (d1 == d2)
1069     return true;
1070
1071   /* If one of the arrays is dimensionless, and the other has a
1072      dimension, they are of different types.  However, it is valid to
1073      write:
1074
1075        extern int a[];
1076        int a[3];
1077
1078      by [basic.link]:
1079
1080        declarations for an array object can specify
1081        array types that differ by the presence or absence of a major
1082        array bound (_dcl.array_).  */
1083   if (!d1 || !d2)
1084     return allow_redeclaration;
1085
1086   /* Check that the dimensions are the same.  */
1087
1088   if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1089     return false;
1090   max1 = TYPE_MAX_VALUE (d1);
1091   max2 = TYPE_MAX_VALUE (d2);
1092   if (processing_template_decl && !abi_version_at_least (2)
1093       && !value_dependent_expression_p (max1)
1094       && !value_dependent_expression_p (max2))
1095     {
1096       /* With abi-1 we do not fold non-dependent array bounds, (and
1097          consequently mangle them incorrectly).  We must therefore
1098          fold them here, to verify the domains have the same
1099          value.  */
1100       max1 = fold (max1);
1101       max2 = fold (max2);
1102     }
1103
1104   if (!cp_tree_equal (max1, max2))
1105     return false;
1106
1107   return true;
1108 }
1109
1110 /* Compare the relative position of T1 and T2 into their respective
1111    template parameter list.
1112    T1 and T2 must be template parameter types.
1113    Return TRUE if T1 and T2 have the same position, FALSE otherwise.  */
1114
1115 static bool
1116 comp_template_parms_position (tree t1, tree t2)
1117 {
1118   tree index1, index2;
1119   gcc_assert (t1 && t2
1120               && TREE_CODE (t1) == TREE_CODE (t2)
1121               && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1122                   || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1123                   || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1124
1125   index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1126   index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1127
1128   /* If T1 and T2 belong to template parm lists of different size,
1129      let's assume they are different.  */
1130   if (TEMPLATE_PARM_NUM_SIBLINGS (index1)
1131       != TEMPLATE_PARM_NUM_SIBLINGS (index2))
1132     return false;
1133
1134   /* Then compare their relative position.  */
1135   if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1136       || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1137       || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1138           != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1139     return false;
1140
1141   return true;
1142 }
1143
1144 /* Subroutine in comptypes.  */
1145
1146 static bool
1147 structural_comptypes (tree t1, tree t2, int strict)
1148 {
1149   if (t1 == t2)
1150     return true;
1151
1152   /* Suppress errors caused by previously reported errors.  */
1153   if (t1 == error_mark_node || t2 == error_mark_node)
1154     return false;
1155
1156   gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1157
1158   /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1159      current instantiation.  */
1160   if (TREE_CODE (t1) == TYPENAME_TYPE)
1161     t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1162
1163   if (TREE_CODE (t2) == TYPENAME_TYPE)
1164     t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1165
1166   if (TYPE_PTRMEMFUNC_P (t1))
1167     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1168   if (TYPE_PTRMEMFUNC_P (t2))
1169     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1170
1171   /* Different classes of types can't be compatible.  */
1172   if (TREE_CODE (t1) != TREE_CODE (t2))
1173     return false;
1174
1175   /* Qualifiers must match.  For array types, we will check when we
1176      recur on the array element types.  */
1177   if (TREE_CODE (t1) != ARRAY_TYPE
1178       && cp_type_quals (t1) != cp_type_quals (t2))
1179     return false;
1180   if (TREE_CODE (t1) == FUNCTION_TYPE
1181       && type_memfn_quals (t1) != type_memfn_quals (t2))
1182     return false;
1183   if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1184     return false;
1185
1186   /* Allow for two different type nodes which have essentially the same
1187      definition.  Note that we already checked for equality of the type
1188      qualifiers (just above).  */
1189
1190   if (TREE_CODE (t1) != ARRAY_TYPE
1191       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1192     return true;
1193
1194
1195   /* Compare the types.  Break out if they could be the same.  */
1196   switch (TREE_CODE (t1))
1197     {
1198     case VOID_TYPE:
1199     case BOOLEAN_TYPE:
1200       /* All void and bool types are the same.  */
1201       break;
1202
1203     case INTEGER_TYPE:
1204     case FIXED_POINT_TYPE:
1205     case REAL_TYPE:
1206       /* With these nodes, we can't determine type equivalence by
1207          looking at what is stored in the nodes themselves, because
1208          two nodes might have different TYPE_MAIN_VARIANTs but still
1209          represent the same type.  For example, wchar_t and int could
1210          have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1211          TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1212          and are distinct types. On the other hand, int and the
1213          following typedef
1214
1215            typedef int INT __attribute((may_alias));
1216
1217          have identical properties, different TYPE_MAIN_VARIANTs, but
1218          represent the same type.  The canonical type system keeps
1219          track of equivalence in this case, so we fall back on it.  */
1220       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1221
1222     case TEMPLATE_TEMPLATE_PARM:
1223     case BOUND_TEMPLATE_TEMPLATE_PARM:
1224       if (!comp_template_parms_position (t1, t2))
1225         return false;
1226       if (!comp_template_parms
1227           (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1228            DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1229         return false;
1230       if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1231         break;
1232       /* Don't check inheritance.  */
1233       strict = COMPARE_STRICT;
1234       /* Fall through.  */
1235
1236     case RECORD_TYPE:
1237     case UNION_TYPE:
1238       if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1239           && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1240               || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1241           && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1242         break;
1243
1244       if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1245         break;
1246       else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1247         break;
1248
1249       return false;
1250
1251     case OFFSET_TYPE:
1252       if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1253                       strict & ~COMPARE_REDECLARATION))
1254         return false;
1255       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1256         return false;
1257       break;
1258
1259     case REFERENCE_TYPE:
1260       if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1261         return false;
1262       /* fall through to checks for pointer types */
1263
1264     case POINTER_TYPE:
1265       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1266           || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1267           || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1268         return false;
1269       break;
1270
1271     case METHOD_TYPE:
1272     case FUNCTION_TYPE:
1273       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1274         return false;
1275       if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1276         return false;
1277       break;
1278
1279     case ARRAY_TYPE:
1280       /* Target types must match incl. qualifiers.  */
1281       if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1282         return false;
1283       break;
1284
1285     case TEMPLATE_TYPE_PARM:
1286       /* If T1 and T2 don't have the same relative position in their
1287          template parameters set, they can't be equal.  */
1288       if (!comp_template_parms_position (t1, t2))
1289         return false;
1290       break;
1291
1292     case TYPENAME_TYPE:
1293       if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1294                           TYPENAME_TYPE_FULLNAME (t2)))
1295         return false;
1296       if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1297         return false;
1298       break;
1299
1300     case UNBOUND_CLASS_TEMPLATE:
1301       if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1302         return false;
1303       if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1304         return false;
1305       break;
1306
1307     case COMPLEX_TYPE:
1308       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1309         return false;
1310       break;
1311
1312     case VECTOR_TYPE:
1313       if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1314           || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1315         return false;
1316       break;
1317
1318     case TYPE_PACK_EXPANSION:
1319       return same_type_p (PACK_EXPANSION_PATTERN (t1), 
1320                           PACK_EXPANSION_PATTERN (t2));
1321
1322     case DECLTYPE_TYPE:
1323       if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1324           != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1325           || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1326               != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1327           || (DECLTYPE_FOR_LAMBDA_RETURN (t1)
1328               != DECLTYPE_FOR_LAMBDA_RETURN (t2))
1329           || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), 
1330                              DECLTYPE_TYPE_EXPR (t2)))
1331         return false;
1332       break;
1333
1334     default:
1335       return false;
1336     }
1337
1338   /* If we get here, we know that from a target independent POV the
1339      types are the same.  Make sure the target attributes are also
1340      the same.  */
1341   return targetm.comp_type_attributes (t1, t2);
1342 }
1343
1344 /* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
1345    is a bitwise-or of the COMPARE_* flags.  */
1346
1347 bool
1348 comptypes (tree t1, tree t2, int strict)
1349 {
1350   if (strict == COMPARE_STRICT)
1351     {
1352       if (t1 == t2)
1353         return true;
1354
1355       if (t1 == error_mark_node || t2 == error_mark_node)
1356         return false;
1357
1358       if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1359         /* At least one of the types requires structural equality, so
1360            perform a deep check. */
1361         return structural_comptypes (t1, t2, strict);
1362
1363 #ifdef ENABLE_CHECKING
1364       if (USE_CANONICAL_TYPES)
1365         {
1366           bool result = structural_comptypes (t1, t2, strict);
1367           
1368           if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1369             /* The two types are structurally equivalent, but their
1370                canonical types were different. This is a failure of the
1371                canonical type propagation code.*/
1372             internal_error 
1373               ("canonical types differ for identical types %T and %T", 
1374                t1, t2);
1375           else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1376             /* Two types are structurally different, but the canonical
1377                types are the same. This means we were over-eager in
1378                assigning canonical types. */
1379             internal_error 
1380               ("same canonical type node for different types %T and %T",
1381                t1, t2);
1382           
1383           return result;
1384         }
1385 #else
1386       if (USE_CANONICAL_TYPES)
1387         return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1388 #endif
1389       else
1390         return structural_comptypes (t1, t2, strict);
1391     }
1392   else if (strict == COMPARE_STRUCTURAL)
1393     return structural_comptypes (t1, t2, COMPARE_STRICT);
1394   else
1395     return structural_comptypes (t1, t2, strict);
1396 }
1397
1398 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1399    top-level qualifiers.  */
1400
1401 bool
1402 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1403 {
1404   if (type1 == error_mark_node || type2 == error_mark_node)
1405     return false;
1406
1407   return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1408 }
1409
1410 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
1411
1412 bool
1413 at_least_as_qualified_p (const_tree type1, const_tree type2)
1414 {
1415   int q1 = cp_type_quals (type1);
1416   int q2 = cp_type_quals (type2);
1417
1418   /* All qualifiers for TYPE2 must also appear in TYPE1.  */
1419   return (q1 & q2) == q2;
1420 }
1421
1422 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1423    more cv-qualified that TYPE1, and 0 otherwise.  */
1424
1425 int
1426 comp_cv_qualification (const_tree type1, const_tree type2)
1427 {
1428   int q1 = cp_type_quals (type1);
1429   int q2 = cp_type_quals (type2);
1430
1431   if (q1 == q2)
1432     return 0;
1433
1434   if ((q1 & q2) == q2)
1435     return 1;
1436   else if ((q1 & q2) == q1)
1437     return -1;
1438
1439   return 0;
1440 }
1441
1442 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1443    subset of the cv-qualification signature of TYPE2, and the types
1444    are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
1445
1446 int
1447 comp_cv_qual_signature (tree type1, tree type2)
1448 {
1449   if (comp_ptr_ttypes_real (type2, type1, -1))
1450     return 1;
1451   else if (comp_ptr_ttypes_real (type1, type2, -1))
1452     return -1;
1453   else
1454     return 0;
1455 }
1456 \f
1457 /* Subroutines of `comptypes'.  */
1458
1459 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1460    equivalent in the sense that functions with those parameter types
1461    can have equivalent types.  The two lists must be equivalent,
1462    element by element.  */
1463
1464 bool
1465 compparms (const_tree parms1, const_tree parms2)
1466 {
1467   const_tree t1, t2;
1468
1469   /* An unspecified parmlist matches any specified parmlist
1470      whose argument types don't need default promotions.  */
1471
1472   for (t1 = parms1, t2 = parms2;
1473        t1 || t2;
1474        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1475     {
1476       /* If one parmlist is shorter than the other,
1477          they fail to match.  */
1478       if (!t1 || !t2)
1479         return false;
1480       if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1481         return false;
1482     }
1483   return true;
1484 }
1485
1486 \f
1487 /* Process a sizeof or alignof expression where the operand is a
1488    type.  */
1489
1490 tree
1491 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1492 {
1493   tree value;
1494   bool dependent_p;
1495
1496   gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1497   if (type == error_mark_node)
1498     return error_mark_node;
1499
1500   type = non_reference (type);
1501   if (TREE_CODE (type) == METHOD_TYPE)
1502     {
1503       if (complain)
1504         pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, 
1505                  "invalid application of %qs to a member function", 
1506                  operator_name_info[(int) op].name);
1507       value = size_one_node;
1508     }
1509
1510   dependent_p = dependent_type_p (type);
1511   if (!dependent_p)
1512     complete_type (type);
1513   if (dependent_p
1514       /* VLA types will have a non-constant size.  In the body of an
1515          uninstantiated template, we don't need to try to compute the
1516          value, because the sizeof expression is not an integral
1517          constant expression in that case.  And, if we do try to
1518          compute the value, we'll likely end up with SAVE_EXPRs, which
1519          the template substitution machinery does not expect to see.  */
1520       || (processing_template_decl 
1521           && COMPLETE_TYPE_P (type)
1522           && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1523     {
1524       value = build_min (op, size_type_node, type);
1525       TREE_READONLY (value) = 1;
1526       return value;
1527     }
1528
1529   return c_sizeof_or_alignof_type (input_location, complete_type (type),
1530                                    op == SIZEOF_EXPR,
1531                                    complain);
1532 }
1533
1534 /* Return the size of the type, without producing any warnings for
1535    types whose size cannot be taken.  This routine should be used only
1536    in some other routine that has already produced a diagnostic about
1537    using the size of such a type.  */
1538 tree 
1539 cxx_sizeof_nowarn (tree type)
1540 {
1541   if (TREE_CODE (type) == FUNCTION_TYPE
1542       || TREE_CODE (type) == VOID_TYPE
1543       || TREE_CODE (type) == ERROR_MARK)
1544     return size_one_node;
1545   else if (!COMPLETE_TYPE_P (type))
1546     return size_zero_node;
1547   else
1548     return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1549 }
1550
1551 /* Process a sizeof expression where the operand is an expression.  */
1552
1553 static tree
1554 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1555 {
1556   if (e == error_mark_node)
1557     return error_mark_node;
1558
1559   if (processing_template_decl)
1560     {
1561       e = build_min (SIZEOF_EXPR, size_type_node, e);
1562       TREE_SIDE_EFFECTS (e) = 0;
1563       TREE_READONLY (e) = 1;
1564
1565       return e;
1566     }
1567
1568   /* To get the size of a static data member declared as an array of
1569      unknown bound, we need to instantiate it.  */
1570   if (TREE_CODE (e) == VAR_DECL
1571       && VAR_HAD_UNKNOWN_BOUND (e)
1572       && DECL_TEMPLATE_INSTANTIATION (e))
1573     instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1574
1575   e = mark_type_use (e);
1576
1577   if (TREE_CODE (e) == COMPONENT_REF
1578       && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1579       && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1580     {
1581       if (complain & tf_error)
1582         error ("invalid application of %<sizeof%> to a bit-field");
1583       else
1584         return error_mark_node;
1585       e = char_type_node;
1586     }
1587   else if (is_overloaded_fn (e))
1588     {
1589       if (complain & tf_error)
1590         permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1591                    "function type");
1592       else
1593         return error_mark_node;
1594       e = char_type_node;
1595     }
1596   else if (type_unknown_p (e))
1597     {
1598       if (complain & tf_error)
1599         cxx_incomplete_type_error (e, TREE_TYPE (e));
1600       else
1601         return error_mark_node;
1602       e = char_type_node;
1603     }
1604   else
1605     e = TREE_TYPE (e);
1606
1607   return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1608 }
1609
1610 /* Implement the __alignof keyword: Return the minimum required
1611    alignment of E, measured in bytes.  For VAR_DECL's and
1612    FIELD_DECL's return DECL_ALIGN (which can be set from an
1613    "aligned" __attribute__ specification).  */
1614
1615 static tree
1616 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1617 {
1618   tree t;
1619
1620   if (e == error_mark_node)
1621     return error_mark_node;
1622
1623   if (processing_template_decl)
1624     {
1625       e = build_min (ALIGNOF_EXPR, size_type_node, e);
1626       TREE_SIDE_EFFECTS (e) = 0;
1627       TREE_READONLY (e) = 1;
1628
1629       return e;
1630     }
1631
1632   e = mark_type_use (e);
1633
1634   if (TREE_CODE (e) == VAR_DECL)
1635     t = size_int (DECL_ALIGN_UNIT (e));
1636   else if (TREE_CODE (e) == COMPONENT_REF
1637            && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1638            && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1639     {
1640       if (complain & tf_error)
1641         error ("invalid application of %<__alignof%> to a bit-field");
1642       else
1643         return error_mark_node;
1644       t = size_one_node;
1645     }
1646   else if (TREE_CODE (e) == COMPONENT_REF
1647            && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1648     t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1649   else if (is_overloaded_fn (e))
1650     {
1651       if (complain & tf_error)
1652         permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1653                    "function type");
1654       else
1655         return error_mark_node;
1656       if (TREE_CODE (e) == FUNCTION_DECL)
1657         t = size_int (DECL_ALIGN_UNIT (e));
1658       else
1659         t = size_one_node;
1660     }
1661   else if (type_unknown_p (e))
1662     {
1663       if (complain & tf_error)
1664         cxx_incomplete_type_error (e, TREE_TYPE (e));
1665       else
1666         return error_mark_node;
1667       t = size_one_node;
1668     }
1669   else
1670     return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, 
1671                                        complain & tf_error);
1672
1673   return fold_convert (size_type_node, t);
1674 }
1675
1676 /* Process a sizeof or alignof expression E with code OP where the operand
1677    is an expression.  */
1678
1679 tree
1680 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1681 {
1682   if (op == SIZEOF_EXPR)
1683     return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1684   else
1685     return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1686 }
1687 \f
1688 /* EXPR is being used in a context that is not a function call.
1689    Enforce:
1690
1691      [expr.ref]
1692
1693      The expression can be used only as the left-hand operand of a
1694      member function call.
1695
1696      [expr.mptr.operator]
1697
1698      If the result of .* or ->* is a function, then that result can be
1699      used only as the operand for the function call operator ().
1700
1701    by issuing an error message if appropriate.  Returns true iff EXPR
1702    violates these rules.  */
1703
1704 bool
1705 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1706 {
1707   if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1708     {
1709       if (complain & tf_error)
1710         error ("invalid use of non-static member function");
1711       return true;
1712     }
1713   return false;
1714 }
1715
1716 /* If EXP is a reference to a bitfield, and the type of EXP does not
1717    match the declared type of the bitfield, return the declared type
1718    of the bitfield.  Otherwise, return NULL_TREE.  */
1719
1720 tree
1721 is_bitfield_expr_with_lowered_type (const_tree exp)
1722 {
1723   switch (TREE_CODE (exp))
1724     {
1725     case COND_EXPR:
1726       if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1727                                                ? TREE_OPERAND (exp, 1)
1728                                                : TREE_OPERAND (exp, 0)))
1729         return NULL_TREE;
1730       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1731
1732     case COMPOUND_EXPR:
1733       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1734
1735     case MODIFY_EXPR:
1736     case SAVE_EXPR:
1737       return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1738
1739     case COMPONENT_REF:
1740       {
1741         tree field;
1742         
1743         field = TREE_OPERAND (exp, 1);
1744         if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1745           return NULL_TREE;
1746         if (same_type_ignoring_top_level_qualifiers_p
1747             (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1748           return NULL_TREE;
1749         return DECL_BIT_FIELD_TYPE (field);
1750       }
1751
1752     CASE_CONVERT:
1753       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1754           == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1755         return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1756       /* Fallthrough.  */
1757
1758     default:
1759       return NULL_TREE;
1760     }
1761 }
1762
1763 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1764    bitfield with a lowered type, the type of EXP is returned, rather
1765    than NULL_TREE.  */
1766
1767 tree
1768 unlowered_expr_type (const_tree exp)
1769 {
1770   tree type;
1771
1772   type = is_bitfield_expr_with_lowered_type (exp);
1773   if (!type)
1774     type = TREE_TYPE (exp);
1775
1776   return type;
1777 }
1778
1779 /* Perform the conversions in [expr] that apply when an lvalue appears
1780    in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1781    function-to-pointer conversions.  In addition, manifest constants
1782    are replaced by their values, and bitfield references are converted
1783    to their declared types. Note that this function does not perform the
1784    lvalue-to-rvalue conversion for class types. If you need that conversion
1785    to for class types, then you probably need to use force_rvalue.
1786
1787    Although the returned value is being used as an rvalue, this
1788    function does not wrap the returned expression in a
1789    NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1790    that the return value is no longer an lvalue.  */
1791
1792 tree
1793 decay_conversion (tree exp)
1794 {
1795   tree type;
1796   enum tree_code code;
1797
1798   type = TREE_TYPE (exp);
1799   if (type == error_mark_node)
1800     return error_mark_node;
1801
1802   exp = mark_rvalue_use (exp);
1803
1804   exp = resolve_nondeduced_context (exp);
1805   if (type_unknown_p (exp))
1806     {
1807       cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1808       return error_mark_node;
1809     }
1810
1811   /* FIXME remove? at least need to remember that this isn't really a
1812      constant expression if EXP isn't decl_constant_var_p, like with
1813      C_MAYBE_CONST_EXPR.  */
1814   exp = decl_constant_value (exp);
1815   if (error_operand_p (exp))
1816     return error_mark_node;
1817
1818   if (NULLPTR_TYPE_P (type))
1819     return nullptr_node;
1820
1821   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1822      Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
1823   code = TREE_CODE (type);
1824   if (code == VOID_TYPE)
1825     {
1826       error ("void value not ignored as it ought to be");
1827       return error_mark_node;
1828     }
1829   if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1830     return error_mark_node;
1831   if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1832     return cp_build_addr_expr (exp, tf_warning_or_error);
1833   if (code == ARRAY_TYPE)
1834     {
1835       tree adr;
1836       tree ptrtype;
1837
1838       if (TREE_CODE (exp) == INDIRECT_REF)
1839         return build_nop (build_pointer_type (TREE_TYPE (type)),
1840                           TREE_OPERAND (exp, 0));
1841
1842       if (TREE_CODE (exp) == COMPOUND_EXPR)
1843         {
1844           tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1845           return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1846                          TREE_OPERAND (exp, 0), op1);
1847         }
1848
1849       if (!lvalue_p (exp)
1850           && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1851         {
1852           error ("invalid use of non-lvalue array");
1853           return error_mark_node;
1854         }
1855
1856       ptrtype = build_pointer_type (TREE_TYPE (type));
1857
1858       if (TREE_CODE (exp) == VAR_DECL)
1859         {
1860           if (!cxx_mark_addressable (exp))
1861             return error_mark_node;
1862           adr = build_nop (ptrtype, build_address (exp));
1863           return adr;
1864         }
1865       /* This way is better for a COMPONENT_REF since it can
1866          simplify the offset for a component.  */
1867       adr = cp_build_addr_expr (exp, tf_warning_or_error);
1868       return cp_convert (ptrtype, adr);
1869     }
1870
1871   /* If a bitfield is used in a context where integral promotion
1872      applies, then the caller is expected to have used
1873      default_conversion.  That function promotes bitfields correctly
1874      before calling this function.  At this point, if we have a
1875      bitfield referenced, we may assume that is not subject to
1876      promotion, and that, therefore, the type of the resulting rvalue
1877      is the declared type of the bitfield.  */
1878   exp = convert_bitfield_to_declared_type (exp);
1879
1880   /* We do not call rvalue() here because we do not want to wrap EXP
1881      in a NON_LVALUE_EXPR.  */
1882
1883   /* [basic.lval]
1884
1885      Non-class rvalues always have cv-unqualified types.  */
1886   type = TREE_TYPE (exp);
1887   if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1888     exp = build_nop (cv_unqualified (type), exp);
1889
1890   return exp;
1891 }
1892
1893 /* Perform preparatory conversions, as part of the "usual arithmetic
1894    conversions".  In particular, as per [expr]:
1895
1896      Whenever an lvalue expression appears as an operand of an
1897      operator that expects the rvalue for that operand, the
1898      lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1899      standard conversions are applied to convert the expression to an
1900      rvalue.
1901
1902    In addition, we perform integral promotions here, as those are
1903    applied to both operands to a binary operator before determining
1904    what additional conversions should apply.  */
1905
1906 tree
1907 default_conversion (tree exp)
1908 {
1909   /* Check for target-specific promotions.  */
1910   tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1911   if (promoted_type)
1912     exp = cp_convert (promoted_type, exp);
1913   /* Perform the integral promotions first so that bitfield
1914      expressions (which may promote to "int", even if the bitfield is
1915      declared "unsigned") are promoted correctly.  */
1916   else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1917     exp = perform_integral_promotions (exp);
1918   /* Perform the other conversions.  */
1919   exp = decay_conversion (exp);
1920
1921   return exp;
1922 }
1923
1924 /* EXPR is an expression with an integral or enumeration type.
1925    Perform the integral promotions in [conv.prom], and return the
1926    converted value.  */
1927
1928 tree
1929 perform_integral_promotions (tree expr)
1930 {
1931   tree type;
1932   tree promoted_type;
1933
1934   expr = mark_rvalue_use (expr);
1935
1936   /* [conv.prom]
1937
1938      If the bitfield has an enumerated type, it is treated as any
1939      other value of that type for promotion purposes.  */
1940   type = is_bitfield_expr_with_lowered_type (expr);
1941   if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1942     type = TREE_TYPE (expr);
1943   gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1944   promoted_type = type_promotes_to (type);
1945   if (type != promoted_type)
1946     expr = cp_convert (promoted_type, expr);
1947   return expr;
1948 }
1949
1950 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1951    decay_conversion to one.  */
1952
1953 int
1954 string_conv_p (const_tree totype, const_tree exp, int warn)
1955 {
1956   tree t;
1957
1958   if (TREE_CODE (totype) != POINTER_TYPE)
1959     return 0;
1960
1961   t = TREE_TYPE (totype);
1962   if (!same_type_p (t, char_type_node)
1963       && !same_type_p (t, char16_type_node)
1964       && !same_type_p (t, char32_type_node)
1965       && !same_type_p (t, wchar_type_node))
1966     return 0;
1967
1968   if (TREE_CODE (exp) == STRING_CST)
1969     {
1970       /* Make sure that we don't try to convert between char and wide chars.  */
1971       if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1972         return 0;
1973     }
1974   else
1975     {
1976       /* Is this a string constant which has decayed to 'const char *'?  */
1977       t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
1978       if (!same_type_p (TREE_TYPE (exp), t))
1979         return 0;
1980       STRIP_NOPS (exp);
1981       if (TREE_CODE (exp) != ADDR_EXPR
1982           || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1983         return 0;
1984     }
1985
1986   /* This warning is not very useful, as it complains about printf.  */
1987   if (warn)
1988     warning (OPT_Wwrite_strings,
1989              "deprecated conversion from string constant to %qT",
1990              totype);
1991
1992   return 1;
1993 }
1994
1995 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1996    can, for example, use as an lvalue.  This code used to be in
1997    unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1998    expressions, where we're dealing with aggregates.  But now it's again only
1999    called from unary_complex_lvalue.  The case (in particular) that led to
2000    this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2001    get it there.  */
2002
2003 static tree
2004 rationalize_conditional_expr (enum tree_code code, tree t,
2005                               tsubst_flags_t complain)
2006 {
2007   /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2008      the first operand is always the one to be used if both operands
2009      are equal, so we know what conditional expression this used to be.  */
2010   if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2011     {
2012       tree op0 = TREE_OPERAND (t, 0);
2013       tree op1 = TREE_OPERAND (t, 1);
2014
2015       /* The following code is incorrect if either operand side-effects.  */
2016       gcc_assert (!TREE_SIDE_EFFECTS (op0)
2017                   && !TREE_SIDE_EFFECTS (op1));
2018       return
2019         build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2020                                                     ? LE_EXPR : GE_EXPR),
2021                                                    op0, TREE_CODE (op0),
2022                                                    op1, TREE_CODE (op1),
2023                                                    /*overloaded_p=*/NULL,
2024                                                    complain),
2025                                 cp_build_unary_op (code, op0, 0, complain),
2026                                 cp_build_unary_op (code, op1, 0, complain),
2027                                 complain);
2028     }
2029
2030   return
2031     build_conditional_expr (TREE_OPERAND (t, 0),
2032                             cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2033                                                complain),
2034                             cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2035                                                complain),
2036                             complain);
2037 }
2038
2039 /* Given the TYPE of an anonymous union field inside T, return the
2040    FIELD_DECL for the field.  If not found return NULL_TREE.  Because
2041    anonymous unions can nest, we must also search all anonymous unions
2042    that are directly reachable.  */
2043
2044 tree
2045 lookup_anon_field (tree t, tree type)
2046 {
2047   tree field;
2048
2049   for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2050     {
2051       if (TREE_STATIC (field))
2052         continue;
2053       if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2054         continue;
2055
2056       /* If we find it directly, return the field.  */
2057       if (DECL_NAME (field) == NULL_TREE
2058           && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2059         {
2060           return field;
2061         }
2062
2063       /* Otherwise, it could be nested, search harder.  */
2064       if (DECL_NAME (field) == NULL_TREE
2065           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2066         {
2067           tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2068           if (subfield)
2069             return subfield;
2070         }
2071     }
2072   return NULL_TREE;
2073 }
2074
2075 /* Build an expression representing OBJECT.MEMBER.  OBJECT is an
2076    expression; MEMBER is a DECL or baselink.  If ACCESS_PATH is
2077    non-NULL, it indicates the path to the base used to name MEMBER.
2078    If PRESERVE_REFERENCE is true, the expression returned will have
2079    REFERENCE_TYPE if the MEMBER does.  Otherwise, the expression
2080    returned will have the type referred to by the reference.
2081
2082    This function does not perform access control; that is either done
2083    earlier by the parser when the name of MEMBER is resolved to MEMBER
2084    itself, or later when overload resolution selects one of the
2085    functions indicated by MEMBER.  */
2086
2087 tree
2088 build_class_member_access_expr (tree object, tree member,
2089                                 tree access_path, bool preserve_reference,
2090                                 tsubst_flags_t complain)
2091 {
2092   tree object_type;
2093   tree member_scope;
2094   tree result = NULL_TREE;
2095
2096   if (error_operand_p (object) || error_operand_p (member))
2097     return error_mark_node;
2098
2099   gcc_assert (DECL_P (member) || BASELINK_P (member));
2100
2101   /* [expr.ref]
2102
2103      The type of the first expression shall be "class object" (of a
2104      complete type).  */
2105   object_type = TREE_TYPE (object);
2106   if (!currently_open_class (object_type)
2107       && !complete_type_or_maybe_complain (object_type, object, complain))
2108     return error_mark_node;
2109   if (!CLASS_TYPE_P (object_type))
2110     {
2111       if (complain & tf_error)
2112         error ("request for member %qD in %qE, which is of non-class type %qT",
2113                member, object, object_type);
2114       return error_mark_node;
2115     }
2116
2117   /* The standard does not seem to actually say that MEMBER must be a
2118      member of OBJECT_TYPE.  However, that is clearly what is
2119      intended.  */
2120   if (DECL_P (member))
2121     {
2122       member_scope = DECL_CLASS_CONTEXT (member);
2123       mark_used (member);
2124       if (TREE_DEPRECATED (member))
2125         warn_deprecated_use (member, NULL_TREE);
2126     }
2127   else
2128     member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2129   /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2130      presently be the anonymous union.  Go outwards until we find a
2131      type related to OBJECT_TYPE.  */
2132   while (ANON_AGGR_TYPE_P (member_scope)
2133          && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2134                                                         object_type))
2135     member_scope = TYPE_CONTEXT (member_scope);
2136   if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2137     {
2138       if (complain & tf_error)
2139         {
2140           if (TREE_CODE (member) == FIELD_DECL)
2141             error ("invalid use of nonstatic data member %qE", member);
2142           else
2143             error ("%qD is not a member of %qT", member, object_type);
2144         }
2145       return error_mark_node;
2146     }
2147
2148   /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2149      `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only an lvalue
2150      in the front end; only _DECLs and _REFs are lvalues in the back end.  */
2151   {
2152     tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2153     if (temp)
2154       object = cp_build_indirect_ref (temp, RO_NULL, complain);
2155   }
2156
2157   /* In [expr.ref], there is an explicit list of the valid choices for
2158      MEMBER.  We check for each of those cases here.  */
2159   if (TREE_CODE (member) == VAR_DECL)
2160     {
2161       /* A static data member.  */
2162       result = member;
2163       mark_exp_read (object);
2164       /* If OBJECT has side-effects, they are supposed to occur.  */
2165       if (TREE_SIDE_EFFECTS (object))
2166         result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2167     }
2168   else if (TREE_CODE (member) == FIELD_DECL)
2169     {
2170       /* A non-static data member.  */
2171       bool null_object_p;
2172       int type_quals;
2173       tree member_type;
2174
2175       null_object_p = (TREE_CODE (object) == INDIRECT_REF
2176                        && integer_zerop (TREE_OPERAND (object, 0)));
2177
2178       /* Convert OBJECT to the type of MEMBER.  */
2179       if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2180                         TYPE_MAIN_VARIANT (member_scope)))
2181         {
2182           tree binfo;
2183           base_kind kind;
2184
2185           binfo = lookup_base (access_path ? access_path : object_type,
2186                                member_scope, ba_unique,  &kind);
2187           if (binfo == error_mark_node)
2188             return error_mark_node;
2189
2190           /* It is invalid to try to get to a virtual base of a
2191              NULL object.  The most common cause is invalid use of
2192              offsetof macro.  */
2193           if (null_object_p && kind == bk_via_virtual)
2194             {
2195               if (complain & tf_error)
2196                 {
2197                   error ("invalid access to non-static data member %qD of "
2198                          "NULL object",
2199                          member);
2200                   error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2201                 }
2202               return error_mark_node;
2203             }
2204
2205           /* Convert to the base.  */
2206           object = build_base_path (PLUS_EXPR, object, binfo,
2207                                     /*nonnull=*/1);
2208           /* If we found the base successfully then we should be able
2209              to convert to it successfully.  */
2210           gcc_assert (object != error_mark_node);
2211         }
2212
2213       /* Complain about other invalid uses of offsetof, even though they will
2214          give the right answer.  Note that we complain whether or not they
2215          actually used the offsetof macro, since there's no way to know at this
2216          point.  So we just give a warning, instead of a pedwarn.  */
2217       /* Do not produce this warning for base class field references, because
2218          we know for a fact that didn't come from offsetof.  This does occur
2219          in various testsuite cases where a null object is passed where a
2220          vtable access is required.  */
2221       if (null_object_p && warn_invalid_offsetof
2222           && CLASSTYPE_NON_STD_LAYOUT (object_type)
2223           && !DECL_FIELD_IS_BASE (member)
2224           && cp_unevaluated_operand == 0
2225           && (complain & tf_warning))
2226         {
2227           warning (OPT_Winvalid_offsetof, 
2228                    "invalid access to non-static data member %qD "
2229                    " of NULL object", member);
2230           warning (OPT_Winvalid_offsetof, 
2231                    "(perhaps the %<offsetof%> macro was used incorrectly)");
2232         }
2233
2234       /* If MEMBER is from an anonymous aggregate, we have converted
2235          OBJECT so that it refers to the class containing the
2236          anonymous union.  Generate a reference to the anonymous union
2237          itself, and recur to find MEMBER.  */
2238       if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2239           /* When this code is called from build_field_call, the
2240              object already has the type of the anonymous union.
2241              That is because the COMPONENT_REF was already
2242              constructed, and was then disassembled before calling
2243              build_field_call.  After the function-call code is
2244              cleaned up, this waste can be eliminated.  */
2245           && (!same_type_ignoring_top_level_qualifiers_p
2246               (TREE_TYPE (object), DECL_CONTEXT (member))))
2247         {
2248           tree anonymous_union;
2249
2250           anonymous_union = lookup_anon_field (TREE_TYPE (object),
2251                                                DECL_CONTEXT (member));
2252           object = build_class_member_access_expr (object,
2253                                                    anonymous_union,
2254                                                    /*access_path=*/NULL_TREE,
2255                                                    preserve_reference,
2256                                                    complain);
2257         }
2258
2259       /* Compute the type of the field, as described in [expr.ref].  */
2260       type_quals = TYPE_UNQUALIFIED;
2261       member_type = TREE_TYPE (member);
2262       if (TREE_CODE (member_type) != REFERENCE_TYPE)
2263         {
2264           type_quals = (cp_type_quals (member_type)
2265                         | cp_type_quals (object_type));
2266
2267           /* A field is const (volatile) if the enclosing object, or the
2268              field itself, is const (volatile).  But, a mutable field is
2269              not const, even within a const object.  */
2270           if (DECL_MUTABLE_P (member))
2271             type_quals &= ~TYPE_QUAL_CONST;
2272           member_type = cp_build_qualified_type (member_type, type_quals);
2273         }
2274
2275       result = build3 (COMPONENT_REF, member_type, object, member,
2276                        NULL_TREE);
2277       result = fold_if_not_in_template (result);
2278
2279       /* Mark the expression const or volatile, as appropriate.  Even
2280          though we've dealt with the type above, we still have to mark the
2281          expression itself.  */
2282       if (type_quals & TYPE_QUAL_CONST)
2283         TREE_READONLY (result) = 1;
2284       if (type_quals & TYPE_QUAL_VOLATILE)
2285         TREE_THIS_VOLATILE (result) = 1;
2286     }
2287   else if (BASELINK_P (member))
2288     {
2289       /* The member is a (possibly overloaded) member function.  */
2290       tree functions;
2291       tree type;
2292
2293       /* If the MEMBER is exactly one static member function, then we
2294          know the type of the expression.  Otherwise, we must wait
2295          until overload resolution has been performed.  */
2296       functions = BASELINK_FUNCTIONS (member);
2297       if (TREE_CODE (functions) == FUNCTION_DECL
2298           && DECL_STATIC_FUNCTION_P (functions))
2299         type = TREE_TYPE (functions);
2300       else
2301         type = unknown_type_node;
2302       /* Note that we do not convert OBJECT to the BASELINK_BINFO
2303          base.  That will happen when the function is called.  */
2304       result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2305     }
2306   else if (TREE_CODE (member) == CONST_DECL)
2307     {
2308       /* The member is an enumerator.  */
2309       result = member;
2310       /* If OBJECT has side-effects, they are supposed to occur.  */
2311       if (TREE_SIDE_EFFECTS (object))
2312         result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2313                          object, result);
2314     }
2315   else
2316     {
2317       if (complain & tf_error)
2318         error ("invalid use of %qD", member);
2319       return error_mark_node;
2320     }
2321
2322   if (!preserve_reference)
2323     /* [expr.ref]
2324
2325        If E2 is declared to have type "reference to T", then ... the
2326        type of E1.E2 is T.  */
2327     result = convert_from_reference (result);
2328
2329   return result;
2330 }
2331
2332 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2333    SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type.  */
2334
2335 static tree
2336 lookup_destructor (tree object, tree scope, tree dtor_name)
2337 {
2338   tree object_type = TREE_TYPE (object);
2339   tree dtor_type = TREE_OPERAND (dtor_name, 0);
2340   tree expr;
2341
2342   if (scope && !check_dtor_name (scope, dtor_type))
2343     {
2344       error ("qualified type %qT does not match destructor name ~%qT",
2345              scope, dtor_type);
2346       return error_mark_node;
2347     }
2348   if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2349     {
2350       /* In a template, names we can't find a match for are still accepted
2351          destructor names, and we check them here.  */
2352       if (check_dtor_name (object_type, dtor_type))
2353         dtor_type = object_type;
2354       else
2355         {
2356           error ("object type %qT does not match destructor name ~%qT",
2357                  object_type, dtor_type);
2358           return error_mark_node;
2359         }
2360       
2361     }
2362   else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2363     {
2364       error ("the type being destroyed is %qT, but the destructor refers to %qT",
2365              TYPE_MAIN_VARIANT (object_type), dtor_type);
2366       return error_mark_node;
2367     }
2368   expr = lookup_member (dtor_type, complete_dtor_identifier,
2369                         /*protect=*/1, /*want_type=*/false);
2370   expr = (adjust_result_of_qualified_name_lookup
2371           (expr, dtor_type, object_type));
2372   return expr;
2373 }
2374
2375 /* An expression of the form "A::template B" has been resolved to
2376    DECL.  Issue a diagnostic if B is not a template or template
2377    specialization.  */
2378
2379 void
2380 check_template_keyword (tree decl)
2381 {
2382   /* The standard says:
2383
2384       [temp.names]
2385
2386       If a name prefixed by the keyword template is not a member
2387       template, the program is ill-formed.
2388
2389      DR 228 removed the restriction that the template be a member
2390      template.
2391
2392      DR 96, if accepted would add the further restriction that explicit
2393      template arguments must be provided if the template keyword is
2394      used, but, as of 2005-10-16, that DR is still in "drafting".  If
2395      this DR is accepted, then the semantic checks here can be
2396      simplified, as the entity named must in fact be a template
2397      specialization, rather than, as at present, a set of overloaded
2398      functions containing at least one template function.  */
2399   if (TREE_CODE (decl) != TEMPLATE_DECL
2400       && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2401     {
2402       if (!is_overloaded_fn (decl))
2403         permerror (input_location, "%qD is not a template", decl);
2404       else
2405         {
2406           tree fns;
2407           fns = decl;
2408           if (BASELINK_P (fns))
2409             fns = BASELINK_FUNCTIONS (fns);
2410           while (fns)
2411             {
2412               tree fn = OVL_CURRENT (fns);
2413               if (TREE_CODE (fn) == TEMPLATE_DECL
2414                   || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2415                 break;
2416               if (TREE_CODE (fn) == FUNCTION_DECL
2417                   && DECL_USE_TEMPLATE (fn)
2418                   && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2419                 break;
2420               fns = OVL_NEXT (fns);
2421             }
2422           if (!fns)
2423             permerror (input_location, "%qD is not a template", decl);
2424         }
2425     }
2426 }
2427
2428 /* This function is called by the parser to process a class member
2429    access expression of the form OBJECT.NAME.  NAME is a node used by
2430    the parser to represent a name; it is not yet a DECL.  It may,
2431    however, be a BASELINK where the BASELINK_FUNCTIONS is a
2432    TEMPLATE_ID_EXPR.  Templates must be looked up by the parser, and
2433    there is no reason to do the lookup twice, so the parser keeps the
2434    BASELINK.  TEMPLATE_P is true iff NAME was explicitly declared to
2435    be a template via the use of the "A::template B" syntax.  */
2436
2437 tree
2438 finish_class_member_access_expr (tree object, tree name, bool template_p,
2439                                  tsubst_flags_t complain)
2440 {
2441   tree expr;
2442   tree object_type;
2443   tree member;
2444   tree access_path = NULL_TREE;
2445   tree orig_object = object;
2446   tree orig_name = name;
2447
2448   if (object == error_mark_node || name == error_mark_node)
2449     return error_mark_node;
2450
2451   /* If OBJECT is an ObjC class instance, we must obey ObjC access rules.  */
2452   if (!objc_is_public (object, name))
2453     return error_mark_node;
2454
2455   object_type = TREE_TYPE (object);
2456
2457   if (processing_template_decl)
2458     {
2459       if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME.  */
2460           dependent_type_p (object_type)
2461           /* If NAME is just an IDENTIFIER_NODE, then the expression
2462              is dependent.  */
2463           || TREE_CODE (object) == IDENTIFIER_NODE
2464           /* If NAME is "f<args>", where either 'f' or 'args' is
2465              dependent, then the expression is dependent.  */
2466           || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2467               && dependent_template_id_p (TREE_OPERAND (name, 0),
2468                                           TREE_OPERAND (name, 1)))
2469           /* If NAME is "T::X" where "T" is dependent, then the
2470              expression is dependent.  */
2471           || (TREE_CODE (name) == SCOPE_REF
2472               && TYPE_P (TREE_OPERAND (name, 0))
2473               && dependent_type_p (TREE_OPERAND (name, 0))))
2474         return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2475       object = build_non_dependent_expr (object);
2476     }
2477   else if (c_dialect_objc ()
2478            && TREE_CODE (name) == IDENTIFIER_NODE
2479            && (expr = objc_maybe_build_component_ref (object, name)))
2480     return expr;
2481     
2482   /* [expr.ref]
2483
2484      The type of the first expression shall be "class object" (of a
2485      complete type).  */
2486   if (!currently_open_class (object_type)
2487       && !complete_type_or_maybe_complain (object_type, object, complain))
2488     return error_mark_node;
2489   if (!CLASS_TYPE_P (object_type))
2490     {
2491       if (complain & tf_error)
2492         error ("request for member %qD in %qE, which is of non-class type %qT",
2493                name, object, object_type);
2494       return error_mark_node;
2495     }
2496
2497   if (BASELINK_P (name))
2498     /* A member function that has already been looked up.  */
2499     member = name;
2500   else
2501     {
2502       bool is_template_id = false;
2503       tree template_args = NULL_TREE;
2504       tree scope;
2505
2506       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2507         {
2508           is_template_id = true;
2509           template_args = TREE_OPERAND (name, 1);
2510           name = TREE_OPERAND (name, 0);
2511
2512           if (TREE_CODE (name) == OVERLOAD)
2513             name = DECL_NAME (get_first_fn (name));
2514           else if (DECL_P (name))
2515             name = DECL_NAME (name);
2516         }
2517
2518       if (TREE_CODE (name) == SCOPE_REF)
2519         {
2520           /* A qualified name.  The qualifying class or namespace `S'
2521              has already been looked up; it is either a TYPE or a
2522              NAMESPACE_DECL.  */
2523           scope = TREE_OPERAND (name, 0);
2524           name = TREE_OPERAND (name, 1);
2525
2526           /* If SCOPE is a namespace, then the qualified name does not
2527              name a member of OBJECT_TYPE.  */
2528           if (TREE_CODE (scope) == NAMESPACE_DECL)
2529             {
2530               if (complain & tf_error)
2531                 error ("%<%D::%D%> is not a member of %qT",
2532                        scope, name, object_type);
2533               return error_mark_node;
2534             }
2535
2536           gcc_assert (CLASS_TYPE_P (scope));
2537           gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2538                       || TREE_CODE (name) == BIT_NOT_EXPR);
2539
2540           if (constructor_name_p (name, scope))
2541             {
2542               if (complain & tf_error)
2543                 error ("cannot call constructor %<%T::%D%> directly",
2544                        scope, name);
2545               return error_mark_node;
2546             }
2547
2548           /* Find the base of OBJECT_TYPE corresponding to SCOPE.  */
2549           access_path = lookup_base (object_type, scope, ba_check, NULL);
2550           if (access_path == error_mark_node)
2551             return error_mark_node;
2552           if (!access_path)
2553             {
2554               if (complain & tf_error)
2555                 error ("%qT is not a base of %qT", scope, object_type);
2556               return error_mark_node;
2557             }
2558         }
2559       else
2560         {
2561           scope = NULL_TREE;
2562           access_path = object_type;
2563         }
2564
2565       if (TREE_CODE (name) == BIT_NOT_EXPR)
2566         member = lookup_destructor (object, scope, name);
2567       else
2568         {
2569           /* Look up the member.  */
2570           member = lookup_member (access_path, name, /*protect=*/1,
2571                                   /*want_type=*/false);
2572           if (member == NULL_TREE)
2573             {
2574               if (complain & tf_error)
2575                 error ("%qD has no member named %qE", object_type, name);
2576               return error_mark_node;
2577             }
2578           if (member == error_mark_node)
2579             return error_mark_node;
2580         }
2581
2582       if (is_template_id)
2583         {
2584           tree templ = member;
2585
2586           if (BASELINK_P (templ))
2587             templ = lookup_template_function (templ, template_args);
2588           else
2589             {
2590               if (complain & tf_error)
2591                 error ("%qD is not a member template function", name);
2592               return error_mark_node;
2593             }
2594         }
2595     }
2596
2597   if (TREE_DEPRECATED (member))
2598     warn_deprecated_use (member, NULL_TREE);
2599
2600   if (template_p)
2601     check_template_keyword (member);
2602
2603   expr = build_class_member_access_expr (object, member, access_path,
2604                                          /*preserve_reference=*/false,
2605                                          complain);
2606   if (processing_template_decl && expr != error_mark_node)
2607     {
2608       if (BASELINK_P (member))
2609         {
2610           if (TREE_CODE (orig_name) == SCOPE_REF)
2611             BASELINK_QUALIFIED_P (member) = 1;
2612           orig_name = member;
2613         }
2614       return build_min_non_dep (COMPONENT_REF, expr,
2615                                 orig_object, orig_name,
2616                                 NULL_TREE);
2617     }
2618
2619   return expr;
2620 }
2621
2622 /* Return an expression for the MEMBER_NAME field in the internal
2623    representation of PTRMEM, a pointer-to-member function.  (Each
2624    pointer-to-member function type gets its own RECORD_TYPE so it is
2625    more convenient to access the fields by name than by FIELD_DECL.)
2626    This routine converts the NAME to a FIELD_DECL and then creates the
2627    node for the complete expression.  */
2628
2629 tree
2630 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2631 {
2632   tree ptrmem_type;
2633   tree member;
2634   tree member_type;
2635
2636   /* This code is a stripped down version of
2637      build_class_member_access_expr.  It does not work to use that
2638      routine directly because it expects the object to be of class
2639      type.  */
2640   ptrmem_type = TREE_TYPE (ptrmem);
2641   gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2642   member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2643                           /*want_type=*/false);
2644   member_type = cp_build_qualified_type (TREE_TYPE (member),
2645                                          cp_type_quals (ptrmem_type));
2646   return fold_build3_loc (input_location,
2647                       COMPONENT_REF, member_type,
2648                       ptrmem, member, NULL_TREE);
2649 }
2650
2651 /* Given an expression PTR for a pointer, return an expression
2652    for the value pointed to.
2653    ERRORSTRING is the name of the operator to appear in error messages.
2654
2655    This function may need to overload OPERATOR_FNNAME.
2656    Must also handle REFERENCE_TYPEs for C++.  */
2657
2658 tree
2659 build_x_indirect_ref (tree expr, ref_operator errorstring, 
2660                       tsubst_flags_t complain)
2661 {
2662   tree orig_expr = expr;
2663   tree rval;
2664
2665   if (processing_template_decl)
2666     {
2667       /* Retain the type if we know the operand is a pointer so that
2668          describable_type doesn't make auto deduction break.  */
2669       if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2670         return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2671       if (type_dependent_expression_p (expr))
2672         return build_min_nt (INDIRECT_REF, expr);
2673       expr = build_non_dependent_expr (expr);
2674     }
2675
2676   rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2677                        NULL_TREE, /*overloaded_p=*/NULL, complain);
2678   if (!rval)
2679     rval = cp_build_indirect_ref (expr, errorstring, complain);
2680
2681   if (processing_template_decl && rval != error_mark_node)
2682     return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2683   else
2684     return rval;
2685 }
2686
2687 /* Helper function called from c-common.  */
2688 tree
2689 build_indirect_ref (location_t loc __attribute__ ((__unused__)),
2690                     tree ptr, ref_operator errorstring)
2691 {
2692   return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2693 }
2694
2695 tree
2696 cp_build_indirect_ref (tree ptr, ref_operator errorstring, 
2697                        tsubst_flags_t complain)
2698 {
2699   tree pointer, type;
2700
2701   if (ptr == error_mark_node)
2702     return error_mark_node;
2703
2704   if (ptr == current_class_ptr)
2705     return current_class_ref;
2706
2707   pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2708              ? ptr : decay_conversion (ptr));
2709   type = TREE_TYPE (pointer);
2710
2711   if (POINTER_TYPE_P (type))
2712     {
2713       /* [expr.unary.op]
2714
2715          If the type of the expression is "pointer to T," the type
2716          of  the  result  is  "T."  */
2717       tree t = TREE_TYPE (type);
2718
2719       if (CONVERT_EXPR_P (ptr)
2720           || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2721         {
2722           /* If a warning is issued, mark it to avoid duplicates from
2723              the backend.  This only needs to be done at
2724              warn_strict_aliasing > 2.  */
2725           if (warn_strict_aliasing > 2)
2726             if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2727                                          type, TREE_OPERAND (ptr, 0)))
2728               TREE_NO_WARNING (ptr) = 1;
2729         }
2730
2731       if (VOID_TYPE_P (t))
2732         {
2733           /* A pointer to incomplete type (other than cv void) can be
2734              dereferenced [expr.unary.op]/1  */
2735           if (complain & tf_error)
2736             error ("%qT is not a pointer-to-object type", type);
2737           return error_mark_node;
2738         }
2739       else if (TREE_CODE (pointer) == ADDR_EXPR
2740                && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2741         /* The POINTER was something like `&x'.  We simplify `*&x' to
2742            `x'.  */
2743         return TREE_OPERAND (pointer, 0);
2744       else
2745         {
2746           tree ref = build1 (INDIRECT_REF, t, pointer);
2747
2748           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2749              so that we get the proper error message if the result is used
2750              to assign to.  Also, &* is supposed to be a no-op.  */
2751           TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2752           TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2753           TREE_SIDE_EFFECTS (ref)
2754             = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2755           return ref;
2756         }
2757     }
2758   else if (!(complain & tf_error))
2759     /* Don't emit any errors; we'll just return ERROR_MARK_NODE later.  */
2760     ;
2761   /* `pointer' won't be an error_mark_node if we were given a
2762      pointer to member, so it's cool to check for this here.  */
2763   else if (TYPE_PTR_TO_MEMBER_P (type))
2764     switch (errorstring)
2765       {
2766          case RO_ARRAY_INDEXING:
2767            error ("invalid use of array indexing on pointer to member");
2768            break;
2769          case RO_UNARY_STAR:
2770            error ("invalid use of unary %<*%> on pointer to member");
2771            break;
2772          case RO_IMPLICIT_CONVERSION:
2773            error ("invalid use of implicit conversion on pointer to member");
2774            break;
2775          default:
2776            gcc_unreachable ();
2777       }
2778   else if (pointer != error_mark_node)
2779     switch (errorstring)
2780       {
2781          case RO_NULL:
2782            error ("invalid type argument");
2783            break;
2784          case RO_ARRAY_INDEXING:
2785            error ("invalid type argument of array indexing");
2786            break;
2787          case RO_UNARY_STAR:
2788            error ("invalid type argument of unary %<*%>");
2789            break;
2790          case RO_IMPLICIT_CONVERSION:
2791            error ("invalid type argument of implicit conversion");
2792            break;
2793          default:
2794            gcc_unreachable ();
2795       }
2796   return error_mark_node;
2797 }
2798
2799 /* This handles expressions of the form "a[i]", which denotes
2800    an array reference.
2801
2802    This is logically equivalent in C to *(a+i), but we may do it differently.
2803    If A is a variable or a member, we generate a primitive ARRAY_REF.
2804    This avoids forcing the array out of registers, and can work on
2805    arrays that are not lvalues (for example, members of structures returned
2806    by functions).
2807
2808    If INDEX is of some user-defined type, it must be converted to
2809    integer type.  Otherwise, to make a compatible PLUS_EXPR, it
2810    will inherit the type of the array, which will be some pointer type.
2811    
2812    LOC is the location to use in building the array reference.  */
2813
2814 tree
2815 cp_build_array_ref (location_t loc, tree array, tree idx,
2816                     tsubst_flags_t complain)
2817 {
2818   tree ret;
2819
2820   if (idx == 0)
2821     {
2822       if (complain & tf_error)
2823         error_at (loc, "subscript missing in array reference");
2824       return error_mark_node;
2825     }
2826
2827   if (TREE_TYPE (array) == error_mark_node
2828       || TREE_TYPE (idx) == error_mark_node)
2829     return error_mark_node;
2830
2831   /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2832      inside it.  */
2833   switch (TREE_CODE (array))
2834     {
2835     case COMPOUND_EXPR:
2836       {
2837         tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2838                                          complain);
2839         ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2840                       TREE_OPERAND (array, 0), value);
2841         SET_EXPR_LOCATION (ret, loc);
2842         return ret;
2843       }
2844
2845     case COND_EXPR:
2846       ret = build_conditional_expr
2847               (TREE_OPERAND (array, 0),
2848                cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2849                                    complain),
2850                cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2851                                    complain),
2852                tf_warning_or_error);
2853       protected_set_expr_location (ret, loc);
2854       return ret;
2855
2856     default:
2857       break;
2858     }
2859
2860   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2861     {
2862       tree rval, type;
2863
2864       warn_array_subscript_with_type_char (idx);
2865
2866       if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2867         {
2868           if (complain & tf_error)
2869             error_at (loc, "array subscript is not an integer");
2870           return error_mark_node;
2871         }
2872
2873       /* Apply integral promotions *after* noticing character types.
2874          (It is unclear why we do these promotions -- the standard
2875          does not say that we should.  In fact, the natural thing would
2876          seem to be to convert IDX to ptrdiff_t; we're performing
2877          pointer arithmetic.)  */
2878       idx = perform_integral_promotions (idx);
2879
2880       /* An array that is indexed by a non-constant
2881          cannot be stored in a register; we must be able to do
2882          address arithmetic on its address.
2883          Likewise an array of elements of variable size.  */
2884       if (TREE_CODE (idx) != INTEGER_CST
2885           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2886               && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2887                   != INTEGER_CST)))
2888         {
2889           if (!cxx_mark_addressable (array))
2890             return error_mark_node;
2891         }
2892
2893       /* An array that is indexed by a constant value which is not within
2894          the array bounds cannot be stored in a register either; because we
2895          would get a crash in store_bit_field/extract_bit_field when trying
2896          to access a non-existent part of the register.  */
2897       if (TREE_CODE (idx) == INTEGER_CST
2898           && TYPE_DOMAIN (TREE_TYPE (array))
2899           && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2900         {
2901           if (!cxx_mark_addressable (array))
2902             return error_mark_node;
2903         }
2904
2905       if (!lvalue_p (array) && (complain & tf_error))
2906         pedwarn (loc, OPT_pedantic, 
2907                  "ISO C++ forbids subscripting non-lvalue array");
2908
2909       /* Note in C++ it is valid to subscript a `register' array, since
2910          it is valid to take the address of something with that
2911          storage specification.  */
2912       if (extra_warnings)
2913         {
2914           tree foo = array;
2915           while (TREE_CODE (foo) == COMPONENT_REF)
2916             foo = TREE_OPERAND (foo, 0);
2917           if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2918               && (complain & tf_warning))
2919             warning_at (loc, OPT_Wextra,
2920                         "subscripting array declared %<register%>");
2921         }
2922
2923       type = TREE_TYPE (TREE_TYPE (array));
2924       rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2925       /* Array ref is const/volatile if the array elements are
2926          or if the array is..  */
2927       TREE_READONLY (rval)
2928         |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2929       TREE_SIDE_EFFECTS (rval)
2930         |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2931       TREE_THIS_VOLATILE (rval)
2932         |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2933       ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2934                                           complain);
2935       protected_set_expr_location (ret, loc);
2936       return ret;
2937     }
2938
2939   {
2940     tree ar = default_conversion (array);
2941     tree ind = default_conversion (idx);
2942
2943     /* Put the integer in IND to simplify error checking.  */
2944     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2945       {
2946         tree temp = ar;
2947         ar = ind;
2948         ind = temp;
2949       }
2950
2951     if (ar == error_mark_node)
2952       return ar;
2953
2954     if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2955       {
2956         if (complain & tf_error)
2957           error_at (loc, "subscripted value is neither array nor pointer");
2958         return error_mark_node;
2959       }
2960     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2961       {
2962         if (complain & tf_error)
2963           error_at (loc, "array subscript is not an integer");
2964         return error_mark_node;
2965       }
2966
2967     warn_array_subscript_with_type_char (idx);
2968
2969     ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
2970                                                      PLUS_EXPR, ar, ind,
2971                                                      complain),
2972                                  RO_ARRAY_INDEXING,
2973                                  complain);
2974     protected_set_expr_location (ret, loc);
2975     return ret;
2976   }
2977 }
2978
2979 /* Entry point for Obj-C++.  */
2980
2981 tree
2982 build_array_ref (location_t loc, tree array, tree idx)
2983 {
2984   return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
2985 }
2986 \f
2987 /* Resolve a pointer to member function.  INSTANCE is the object
2988    instance to use, if the member points to a virtual member.
2989
2990    This used to avoid checking for virtual functions if basetype
2991    has no virtual functions, according to an earlier ANSI draft.
2992    With the final ISO C++ rules, such an optimization is
2993    incorrect: A pointer to a derived member can be static_cast
2994    to pointer-to-base-member, as long as the dynamic object
2995    later has the right member.  */
2996
2997 tree
2998 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2999 {
3000   if (TREE_CODE (function) == OFFSET_REF)
3001     function = TREE_OPERAND (function, 1);
3002
3003   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3004     {
3005       tree idx, delta, e1, e2, e3, vtbl, basetype;
3006       tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3007
3008       tree instance_ptr = *instance_ptrptr;
3009       tree instance_save_expr = 0;
3010       if (instance_ptr == error_mark_node)
3011         {
3012           if (TREE_CODE (function) == PTRMEM_CST)
3013             {
3014               /* Extracting the function address from a pmf is only
3015                  allowed with -Wno-pmf-conversions. It only works for
3016                  pmf constants.  */
3017               e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
3018               e1 = convert (fntype, e1);
3019               return e1;
3020             }
3021           else
3022             {
3023               error ("object missing in use of %qE", function);
3024               return error_mark_node;
3025             }
3026         }
3027
3028       if (TREE_SIDE_EFFECTS (instance_ptr))
3029         instance_ptr = instance_save_expr = save_expr (instance_ptr);
3030
3031       if (TREE_SIDE_EFFECTS (function))
3032         function = save_expr (function);
3033
3034       /* Start by extracting all the information from the PMF itself.  */
3035       e3 = pfn_from_ptrmemfunc (function);
3036       delta = delta_from_ptrmemfunc (function);
3037       idx = build1 (NOP_EXPR, vtable_index_type, e3);
3038       switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3039         {
3040         case ptrmemfunc_vbit_in_pfn:
3041           e1 = cp_build_binary_op (input_location,
3042                                    BIT_AND_EXPR, idx, integer_one_node,
3043                                    tf_warning_or_error);
3044           idx = cp_build_binary_op (input_location,
3045                                     MINUS_EXPR, idx, integer_one_node,
3046                                     tf_warning_or_error);
3047           break;
3048
3049         case ptrmemfunc_vbit_in_delta:
3050           e1 = cp_build_binary_op (input_location,
3051                                    BIT_AND_EXPR, delta, integer_one_node,
3052                                    tf_warning_or_error);
3053           delta = cp_build_binary_op (input_location,
3054                                       RSHIFT_EXPR, delta, integer_one_node,
3055                                       tf_warning_or_error);
3056           break;
3057
3058         default:
3059           gcc_unreachable ();
3060         }
3061
3062       /* Convert down to the right base before using the instance.  A
3063          special case is that in a pointer to member of class C, C may
3064          be incomplete.  In that case, the function will of course be
3065          a member of C, and no conversion is required.  In fact,
3066          lookup_base will fail in that case, because incomplete
3067          classes do not have BINFOs.  */
3068       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3069       if (!same_type_ignoring_top_level_qualifiers_p
3070           (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3071         {
3072           basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3073                                   basetype, ba_check, NULL);
3074           instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3075                                           1);
3076           if (instance_ptr == error_mark_node)
3077             return error_mark_node;
3078         }
3079       /* ...and then the delta in the PMF.  */
3080       instance_ptr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (instance_ptr),
3081                              instance_ptr, fold_convert (sizetype, delta));
3082
3083       /* Hand back the adjusted 'this' argument to our caller.  */
3084       *instance_ptrptr = instance_ptr;
3085
3086       /* Next extract the vtable pointer from the object.  */
3087       vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3088                      instance_ptr);
3089       vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3090       /* If the object is not dynamic the access invokes undefined
3091          behavior.  As it is not executed in this case silence the
3092          spurious warnings it may provoke.  */
3093       TREE_NO_WARNING (vtbl) = 1;
3094
3095       /* Finally, extract the function pointer from the vtable.  */
3096       e2 = fold_build2_loc (input_location,
3097                         POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
3098                         fold_convert (sizetype, idx));
3099       e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3100       TREE_CONSTANT (e2) = 1;
3101
3102       /* When using function descriptors, the address of the
3103          vtable entry is treated as a function pointer.  */
3104       if (TARGET_VTABLE_USES_DESCRIPTORS)
3105         e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3106                      cp_build_addr_expr (e2, tf_warning_or_error));
3107
3108       e2 = fold_convert (TREE_TYPE (e3), e2);
3109       e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3110
3111       /* Make sure this doesn't get evaluated first inside one of the
3112          branches of the COND_EXPR.  */
3113       if (instance_save_expr)
3114         e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3115                      instance_save_expr, e1);
3116
3117       function = e1;
3118     }
3119   return function;
3120 }
3121
3122 /* Used by the C-common bits.  */
3123 tree
3124 build_function_call (location_t loc ATTRIBUTE_UNUSED, 
3125                      tree function, tree params)
3126 {
3127   return cp_build_function_call (function, params, tf_warning_or_error);
3128 }
3129
3130 /* Used by the C-common bits.  */
3131 tree
3132 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3133                          tree function, VEC(tree,gc) *params,
3134                          VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3135 {
3136   VEC(tree,gc) *orig_params = params;
3137   tree ret = cp_build_function_call_vec (function, &params,
3138                                          tf_warning_or_error);
3139
3140   /* cp_build_function_call_vec can reallocate PARAMS by adding
3141      default arguments.  That should never happen here.  Verify
3142      that.  */
3143   gcc_assert (params == orig_params);
3144
3145   return ret;
3146 }
3147
3148 /* Build a function call using a tree list of arguments.  */
3149
3150 tree
3151 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3152 {
3153   VEC(tree,gc) *vec;
3154   tree ret;
3155
3156   vec = make_tree_vector ();
3157   for (; params != NULL_TREE; params = TREE_CHAIN (params))
3158     VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3159   ret = cp_build_function_call_vec (function, &vec, complain);
3160   release_tree_vector (vec);
3161   return ret;
3162 }
3163
3164 /* Build a function call using varargs.  */
3165
3166 tree
3167 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3168 {
3169   VEC(tree,gc) *vec;
3170   va_list args;
3171   tree ret, t;
3172
3173   vec = make_tree_vector ();
3174   va_start (args, complain);
3175   for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3176     VEC_safe_push (tree, gc, vec, t);
3177   va_end (args);
3178   ret = cp_build_function_call_vec (function, &vec, complain);
3179   release_tree_vector (vec);
3180   return ret;
3181 }
3182
3183 /* Build a function call using a vector of arguments.  PARAMS may be
3184    NULL if there are no parameters.  This changes the contents of
3185    PARAMS.  */
3186
3187 tree
3188 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3189                             tsubst_flags_t complain)
3190 {
3191   tree fntype, fndecl;
3192   int is_method;
3193   tree original = function;
3194   int nargs;
3195   tree *argarray;
3196   tree parm_types;
3197   VEC(tree,gc) *allocated = NULL;
3198   tree ret;
3199
3200   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3201      expressions, like those used for ObjC messenger dispatches.  */
3202   if (params != NULL && !VEC_empty (tree, *params))
3203     function = objc_rewrite_function_call (function,
3204                                            VEC_index (tree, *params, 0));
3205
3206   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3207      Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
3208   if (TREE_CODE (function) == NOP_EXPR
3209       && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3210     function = TREE_OPERAND (function, 0);
3211
3212   if (TREE_CODE (function) == FUNCTION_DECL)
3213     {
3214       mark_used (function);
3215       fndecl = function;
3216
3217       /* Convert anything with function type to a pointer-to-function.  */
3218       if (DECL_MAIN_P (function) && (complain & tf_error))
3219         pedwarn (input_location, OPT_pedantic, 
3220                  "ISO C++ forbids calling %<::main%> from within program");
3221
3222       function = build_addr_func (function);
3223     }
3224   else
3225     {
3226       fndecl = NULL_TREE;
3227
3228       function = build_addr_func (function);
3229     }
3230
3231   if (function == error_mark_node)
3232     return error_mark_node;
3233
3234   fntype = TREE_TYPE (function);
3235
3236   if (TYPE_PTRMEMFUNC_P (fntype))
3237     {
3238       if (complain & tf_error)
3239         error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3240                "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3241                original, original);
3242       return error_mark_node;
3243     }
3244
3245   is_method = (TREE_CODE (fntype) == POINTER_TYPE
3246                && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3247
3248   if (!((TREE_CODE (fntype) == POINTER_TYPE
3249          && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3250         || is_method
3251         || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3252     {
3253       if (complain & tf_error)
3254         error ("%qE cannot be used as a function", original);
3255       return error_mark_node;
3256     }
3257
3258   /* fntype now gets the type of function pointed to.  */
3259   fntype = TREE_TYPE (fntype);
3260   parm_types = TYPE_ARG_TYPES (fntype);
3261
3262   if (params == NULL)
3263     {
3264       allocated = make_tree_vector ();
3265       params = &allocated;
3266     }
3267
3268   nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3269                              complain);
3270   if (nargs < 0)
3271     return error_mark_node;
3272
3273   argarray = VEC_address (tree, *params);
3274
3275   /* Check for errors in format strings and inappropriately
3276      null parameters.  */
3277   check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
3278                             parm_types);
3279
3280   ret = build_cxx_call (function, nargs, argarray);
3281
3282   if (allocated != NULL)
3283     release_tree_vector (allocated);
3284
3285   return ret;
3286 }
3287 \f
3288 /* Subroutine of convert_arguments.
3289    Warn about wrong number of args are genereted. */
3290
3291 static void
3292 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3293 {
3294   if (fndecl)
3295     {
3296       if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3297         {
3298           if (DECL_NAME (fndecl) == NULL_TREE
3299               || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3300             error_at (loc,
3301                       too_many_p
3302                       ? G_("too many arguments to constructor %q#D")
3303                       : G_("too few arguments to constructor %q#D"),
3304                       fndecl);
3305           else
3306             error_at (loc,
3307                       too_many_p
3308                       ? G_("too many arguments to member function %q#D")
3309                       : G_("too few arguments to member function %q#D"),
3310                       fndecl);
3311         }
3312       else
3313         error_at (loc,
3314                   too_many_p
3315                   ? G_("too many arguments to function %q#D")
3316                   : G_("too few arguments to function %q#D"),
3317                   fndecl);
3318       inform (DECL_SOURCE_LOCATION (fndecl),
3319               "declared here");
3320     }
3321   else
3322     {
3323       if (c_dialect_objc ()  &&  objc_message_selector ())
3324         error_at (loc,
3325                   too_many_p 
3326                   ? G_("too many arguments to method %q#D")
3327                   : G_("too few arguments to method %q#D"),
3328                   objc_message_selector ());
3329       else
3330         error_at (loc, too_many_p ? G_("too many arguments to function")
3331                                   : G_("too few arguments to function"));
3332     }
3333 }
3334
3335 /* Convert the actual parameter expressions in the list VALUES to the
3336    types in the list TYPELIST.  The converted expressions are stored
3337    back in the VALUES vector.
3338    If parmdecls is exhausted, or when an element has NULL as its type,
3339    perform the default conversions.
3340
3341    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
3342
3343    This is also where warnings about wrong number of args are generated.
3344
3345    Returns the actual number of arguments processed (which might be less
3346    than the length of the vector), or -1 on error.
3347
3348    In C++, unspecified trailing parameters can be filled in with their
3349    default arguments, if such were specified.  Do so here.  */
3350
3351 static int
3352 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3353                    int flags, tsubst_flags_t complain)
3354 {
3355   tree typetail;
3356   unsigned int i;
3357
3358   /* Argument passing is always copy-initialization.  */
3359   flags |= LOOKUP_ONLYCONVERTING;
3360
3361   for (i = 0, typetail = typelist;
3362        i < VEC_length (tree, *values);
3363        i++)
3364     {
3365       tree type = typetail ? TREE_VALUE (typetail) : 0;
3366       tree val = VEC_index (tree, *values, i);
3367
3368       if (val == error_mark_node || type == error_mark_node)
3369         return -1;
3370
3371       if (type == void_type_node)
3372         {
3373           if (complain & tf_error)
3374             {
3375               warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3376               return i;
3377             }
3378           else
3379             return -1;
3380         }
3381
3382       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3383          Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
3384       if (TREE_CODE (val) == NOP_EXPR
3385           && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3386           && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3387         val = TREE_OPERAND (val, 0);
3388
3389       if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3390         {
3391           if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3392               || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3393               || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3394             val = decay_conversion (val);
3395         }
3396
3397       if (val == error_mark_node)
3398         return -1;
3399
3400       if (type != 0)
3401         {
3402           /* Formal parm type is specified by a function prototype.  */
3403           tree parmval;
3404
3405           if (!COMPLETE_TYPE_P (complete_type (type)))
3406             {
3407               if (complain & tf_error)
3408                 {
3409                   if (fndecl)
3410                     error ("parameter %P of %qD has incomplete type %qT",
3411                            i, fndecl, type);
3412                   else
3413                     error ("parameter %P has incomplete type %qT", i, type);
3414                 }
3415               parmval = error_mark_node;
3416             }
3417           else
3418             {
3419               parmval = convert_for_initialization
3420                 (NULL_TREE, type, val, flags,
3421                  ICR_ARGPASS, fndecl, i, complain);
3422               parmval = convert_for_arg_passing (type, parmval);
3423             }
3424
3425           if (parmval == error_mark_node)
3426             return -1;
3427
3428           VEC_replace (tree, *values, i, parmval);
3429         }
3430       else
3431         {
3432           if (fndecl && DECL_BUILT_IN (fndecl)
3433               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3434             /* Don't do ellipsis conversion for __built_in_constant_p
3435                as this will result in spurious errors for non-trivial
3436                types.  */
3437             val = require_complete_type_sfinae (val, complain);
3438           else
3439             val = convert_arg_to_ellipsis (val);
3440
3441           VEC_replace (tree, *values, i, val);
3442         }
3443
3444       if (typetail)
3445         typetail = TREE_CHAIN (typetail);
3446     }
3447
3448   if (typetail != 0 && typetail != void_list_node)
3449     {
3450       /* See if there are default arguments that can be used.  Because
3451          we hold default arguments in the FUNCTION_TYPE (which is so
3452          wrong), we can see default parameters here from deduced
3453          contexts (and via typeof) for indirect function calls.
3454          Fortunately we know whether we have a function decl to
3455          provide default arguments in a language conformant
3456          manner.  */
3457       if (fndecl && TREE_PURPOSE (typetail)
3458           && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3459         {
3460           for (; typetail != void_list_node; ++i)
3461             {
3462               tree parmval
3463                 = convert_default_arg (TREE_VALUE (typetail),
3464                                        TREE_PURPOSE (typetail),
3465                                        fndecl, i);
3466
3467               if (parmval == error_mark_node)
3468                 return -1;
3469
3470               VEC_safe_push (tree, gc, *values, parmval);
3471               typetail = TREE_CHAIN (typetail);
3472               /* ends with `...'.  */
3473               if (typetail == NULL_TREE)