SemaOverload.cpp revision d28b42862bc627f4fc1430b4a1919b304800dc1c
13ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch//===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===//
2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//                     The LLVM Compiler Infrastructure
4b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch//
5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// This file is distributed under the University of Illinois Open Source
6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// License. See LICENSE.TXT for details.
7b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch//
8014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch//===----------------------------------------------------------------------===//
9014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch//
10014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// This file provides Sema routines for C++ overloading.
11b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
12b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//===----------------------------------------------------------------------===//
13b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
14b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch#include "Sema.h"
15b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch#include "SemaInherit.h"
16b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch#include "clang/Basic/Diagnostic.h"
17b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "clang/Lex/Preprocessor.h"
18b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "clang/AST/ASTContext.h"
19b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "clang/AST/Expr.h"
20b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "clang/AST/ExprCXX.h"
21b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch#include "clang/AST/TypeOrdering.h"
22b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch#include "clang/Basic/PartialDiagnostic.h"
23b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch#include "llvm/ADT/SmallPtrSet.h"
24b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "llvm/ADT/STLExtras.h"
25b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "llvm/Support/Compiler.h"
26b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include <algorithm>
27b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include <cstdio>
28b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
29b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochnamespace clang {
30b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
31b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// GetConversionCategory - Retrieve the implicit conversion
32b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// category corresponding to the given implicit conversion kind.
33b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochImplicitConversionCategory
34b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochGetConversionCategory(ImplicitConversionKind Kind) {
35b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  static const ImplicitConversionCategory
36b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Category[(int)ICK_Num_Conversion_Kinds] = {
37b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Identity,
38b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Lvalue_Transformation,
39014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ICC_Lvalue_Transformation,
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Lvalue_Transformation,
41b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Qualification_Adjustment,
42b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Promotion,
43b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Promotion,
44b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Promotion,
45b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
48b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
49b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
53b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion,
54b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICC_Conversion
55b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  };
56b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Category[(int)Kind];
57b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
58b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// GetConversionRank - Retrieve the implicit conversion rank
60b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// corresponding to the given implicit conversion kind.
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) {
62b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  static const ImplicitConversionRank
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Rank[(int)ICK_Num_Conversion_Kinds] = {
64b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Exact_Match,
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Exact_Match,
66b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Exact_Match,
67b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Exact_Match,
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Exact_Match,
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Promotion,
70b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Promotion,
71b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Promotion,
72b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
73537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch    ICR_Conversion,
74b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
76b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
77b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
78b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
79b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
80b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion,
81b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICR_Conversion
82014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  };
83b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Rank[(int)Kind];
84b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
85b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
86b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// GetImplicitConversionName - Return the name of this kind of
87b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// implicit conversion.
88b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochconst char* GetImplicitConversionName(ImplicitConversionKind Kind) {
89b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  static const char* Name[(int)ICK_Num_Conversion_Kinds] = {
90b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "No conversion",
91b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Lvalue-to-rvalue",
92b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Array-to-pointer",
93b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Function-to-pointer",
94b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Qualification",
95b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Integral promotion",
96b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Floating point promotion",
97b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Complex promotion",
98b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Integral conversion",
99b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Floating conversion",
100b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Complex conversion",
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Floating-integral conversion",
102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Complex-real conversion",
103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Pointer conversion",
10421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    "Pointer-to-member conversion",
105b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    "Boolean conversion",
1061b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    "Compatible-types conversion",
1071b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    "Derived-to-base conversion"
108b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  };
109b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Name[Kind];
110b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
111b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1121b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// StandardConversionSequence - Set the standard conversion
1131b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// sequence to the identity conversion.
11421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdochvoid StandardConversionSequence::setAsIdentityConversion() {
115b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  First = ICK_Identity;
116014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Second = ICK_Identity;
117b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Third = ICK_Identity;
118b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Deprecated = false;
119b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ReferenceBinding = false;
120b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DirectBinding = false;
121b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  RRefBinding = false;
122b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CopyConstructor = 0;
123b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
124b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
125b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// getRank - Retrieve the rank of this standard conversion sequence
126b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the
127b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// implicit conversions.
128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochImplicitConversionRank StandardConversionSequence::getRank() const {
129b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ImplicitConversionRank Rank = ICR_Exact_Match;
130b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if  (GetConversionRank(First) > Rank)
131b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Rank = GetConversionRank(First);
132b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if  (GetConversionRank(Second) > Rank)
133b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Rank = GetConversionRank(Second);
134b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if  (GetConversionRank(Third) > Rank)
135b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Rank = GetConversionRank(Third);
136b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Rank;
137b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
138b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
139b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// isPointerConversionToBool - Determines whether this conversion is
140b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// a conversion of a pointer or pointer-to-member to bool. This is
141b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// used as part of the ranking of standard conversion sequences
142b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// (C++ 13.3.3.2p4).
143b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool StandardConversionSequence::isPointerConversionToBool() const
144b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch{
145b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType FromType = QualType::getFromOpaquePtr(FromTypePtr);
146b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType ToType = QualType::getFromOpaquePtr(ToTypePtr);
147b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
148b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Note that FromType has not necessarily been transformed by the
149b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // array-to-pointer or function-to-pointer implicit conversions, so
150b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // check for their presence as well as checking whether FromType is
151b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // a pointer.
152b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (ToType->isBooleanType() &&
153b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      (FromType->isPointerType() || FromType->isBlockPointerType() ||
154b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch       First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer))
155b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
1563ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
157b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return false;
158958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
159014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
160014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// isPointerConversionToVoidPointer - Determines whether this
161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// conversion is a conversion of a pointer to a void pointer. This is
162958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier/// used as part of the ranking of standard conversion sequences (C++
163958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier/// 13.3.3.2p4).
164958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernierbool
165b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochStandardConversionSequence::
166b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochisPointerConversionToVoidPointer(ASTContext& Context) const
167b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch{
168b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType FromType = QualType::getFromOpaquePtr(FromTypePtr);
169b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType ToType = QualType::getFromOpaquePtr(ToTypePtr);
170b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
171b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Note that FromType has not necessarily been transformed by the
172b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // array-to-pointer implicit conversion, so check for its presence
173b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // and redo the conversion to get a pointer.
174b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (First == ICK_Array_To_Pointer)
175b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FromType = Context.getArrayDecayedType(FromType);
176b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1771e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (Second == ICK_Pointer_Conversion)
1781e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    if (const PointerType* ToPtrType = ToType->getAs<PointerType>())
179b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return ToPtrType->getPointeeType()->isVoidType();
180b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
181b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return false;
182b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
183b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
184b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// DebugPrint - Print this standard conversion sequence to standard
185b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// error. Useful for debugging overloading issues.
186b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochvoid StandardConversionSequence::DebugPrint() const {
1873ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  bool PrintedSomething = false;
1883ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  if (First != ICK_Identity) {
189b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    fprintf(stderr, "%s", GetImplicitConversionName(First));
190257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    PrintedSomething = true;
191257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  }
192257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
193b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (Second != ICK_Identity) {
194257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    if (PrintedSomething) {
195257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      fprintf(stderr, " -> ");
196257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    }
197257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    fprintf(stderr, "%s", GetImplicitConversionName(Second));
198257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
199257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    if (CopyConstructor) {
200257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      fprintf(stderr, " (by copy constructor)");
201257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    } else if (DirectBinding) {
202257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      fprintf(stderr, " (direct reference binding)");
203257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    } else if (ReferenceBinding) {
204257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      fprintf(stderr, " (reference binding)");
205257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    }
206257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    PrintedSomething = true;
207257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  }
208257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
209b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (Third != ICK_Identity) {
210b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (PrintedSomething) {
211b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      fprintf(stderr, " -> ");
212b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
213b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    fprintf(stderr, "%s", GetImplicitConversionName(Third));
214b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    PrintedSomething = true;
2151e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  }
2161e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
2171e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (!PrintedSomething) {
218b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    fprintf(stderr, "No conversions required");
219b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
220b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
221b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
222b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// DebugPrint - Print this user-defined conversion sequence to standard
223b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// error. Useful for debugging overloading issues.
224b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdochvoid UserDefinedConversionSequence::DebugPrint() const {
225b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (Before.First || Before.Second || Before.Third) {
226b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Before.DebugPrint();
227b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    fprintf(stderr, " -> ");
2281e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  }
2291b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str());
2301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  if (After.First || After.Second || After.Third) {
2311b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    fprintf(stderr, " -> ");
2321b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    After.DebugPrint();
2331b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  }
2341b268ca467c924004286c97bac133db489cf43d0Ben Murdoch}
2351b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
2361e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// DebugPrint - Print this implicit conversion sequence to standard
237b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// error. Useful for debugging overloading issues.
238b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid ImplicitConversionSequence::DebugPrint() const {
239b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  switch (ConversionKind) {
240b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  case StandardConversion:
241b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    fprintf(stderr, "Standard conversion: ");
2421e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    Standard.DebugPrint();
2431e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    break;
244b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  case UserDefinedConversion:
2451e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    fprintf(stderr, "User-defined conversion: ");
246b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    UserDefined.DebugPrint();
247b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    break;
2481e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  case EllipsisConversion:
2491e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    fprintf(stderr, "Ellipsis conversion");
2501e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    break;
2511e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  case BadConversion:
252b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    fprintf(stderr, "Bad conversion");
253b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    break;
2541e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  }
2551e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
2561e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  fprintf(stderr, "\n");
2571e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block}
258b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
259b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// IsOverload - Determine whether the given New declaration is an
260b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// overload of the Old declaration. This routine returns false if New
261b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// and Old cannot be overloaded, e.g., if they are functions with the
262b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// same signature (C++ 1.3.10) or if the Old declaration isn't a
263b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// function (or overload set). When it does return false and Old is an
264b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// OverloadedFunctionDecl, MatchedDecl will be set to point to the
265b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// FunctionDecl that New cannot be overloaded with.
266b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
267b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Example: Given the following input:
268b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
269b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//   void f(int, float); // #1
2701b268ca467c924004286c97bac133db489cf43d0Ben Murdoch//   void f(int, int); // #2
2711b268ca467c924004286c97bac133db489cf43d0Ben Murdoch//   int f(int, int); // #3
272b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
2731e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block// When we process #1, there is no previous declaration of "f",
274b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// so IsOverload will not be used.
275b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch//
276b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// When we process #2, Old is a FunctionDecl for #1.  By comparing the
277b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// parameter types, we see that #1 and #2 are overloaded (since they
278b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// have different signatures), so this routine returns false;
279b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// MatchedDecl is unchanged.
2801e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block//
281b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// When we process #3, Old is an OverloadedFunctionDecl containing #1
282b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// and #2. We compare the signatures of #3 to #1 (they're overloaded,
283b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch// so we do nothing) and then #3 to #2. Since the signatures of #3 and
284b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch// #2 are identical (return types of functions are not part of the
285b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch// signature), IsOverload returns false and MatchedDecl will be set to
286014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// point to the FunctionDecl for #2.
287b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochbool
288014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochSema::IsOverload(FunctionDecl *New, Decl* OldD,
289b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                 OverloadedFunctionDecl::function_iterator& MatchedDecl)
290b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch{
291b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) {
292b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Is this new function an overload of every function in the
293b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // overload set?
294b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
295b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                           FuncEnd = Ovl->function_end();
296b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    for (; Func != FuncEnd; ++Func) {
297b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (!IsOverload(New, *Func, MatchedDecl)) {
298b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        MatchedDecl = Func;
299b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return false;
300b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      }
3013fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    }
3023fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch
303b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // This function overloads every function in the overload set.
304b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
305b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (FunctionTemplateDecl *Old = dyn_cast<FunctionTemplateDecl>(OldD))
306014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return IsOverload(New, Old->getTemplatedDecl(), MatchedDecl);
307014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) {
308b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate();
309014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate();
310014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
311b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // C++ [temp.fct]p2:
312b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   A function template can be overloaded with other function templates
313b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   and with normal (non-template) functions.
314b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if ((OldTemplate == 0) != (NewTemplate == 0))
315b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      return true;
316257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
317b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Is the function New an overload of the function Old?
318b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    QualType OldQType = Context.getCanonicalType(Old->getType());
319b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    QualType NewQType = Context.getCanonicalType(New->getType());
320b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
321b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Compare the signatures (C++ 1.3.10) of the two functions to
322b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // determine whether they are overloads. If we find any mismatch
323257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    // in the signature, they are overloads.
324014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
325014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // If either of these functions is a K&R-style function (no
326257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    // prototype), then we consider them to have matching signatures.
327b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) ||
328257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        isa<FunctionNoProtoType>(NewQType.getTypePtr()))
329257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      return false;
330b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
331b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType);
332b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType);
333b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
334b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // The signature of a function includes the types of its
335b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // parameters (C++ 1.3.10), which includes the presence or absence
336b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // of the ellipsis; see C++ DR 357).
337b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (OldQType != NewQType &&
338b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        (OldType->getNumArgs() != NewType->getNumArgs() ||
339b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch         OldType->isVariadic() != NewType->isVariadic() ||
340b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch         !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(),
341b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                     NewType->arg_type_begin())))
342b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      return true;
343b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
344b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // C++ [temp.over.link]p4:
345b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   The signature of a function template consists of its function
346b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   signature, its return type and its template parameter list. The names
347b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   of the template parameters are significant only for establishing the
348b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   relationship between the template parameters and the rest of the
349b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   signature.
350b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //
351b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // We check the return type and template parameter lists for function
352b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // templates first; the remaining checks follow.
353b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (NewTemplate &&
354b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(),
355b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         OldTemplate->getTemplateParameters(),
356b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         false, false, SourceLocation()) ||
357b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch         OldType->getResultType() != NewType->getResultType()))
358b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      return true;
359b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
360b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // If the function is a class member, its signature includes the
361014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // cv-qualifiers (if any) on the function itself.
362257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    //
363257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    // As part of this, also check whether one of the member functions
364257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    // is static, in which case they are not overloads (C++
365014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // 13.1p2). While not part of the definition of the signature,
366b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // this check is important to determine whether these functions
367b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // can be overloaded.
368b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
369257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
370257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    if (OldMethod && NewMethod &&
371257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        !OldMethod->isStatic() && !NewMethod->isStatic() &&
372257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers())
373014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return true;
374b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
375b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // The signatures match; this is not an overload.
376b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return false;
377014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  } else {
378b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // (C++ 13p1):
379014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //   Only function declarations can be overloaded; object and type
380014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //   declarations cannot be overloaded.
381b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
382b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
383014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
384b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
385b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// TryImplicitConversion - Attempt to perform an implicit conversion
386b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// from the given expression (Expr) to the given type (ToType). This
387b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// function returns an implicit conversion sequence that can be used
388b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// to perform the initialization. Given
389b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch///
390b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch///   void f(float f);
391b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch///   void g(int i) { f(i); }
392b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch///
393b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// this routine would produce an implicit conversion sequence to
394014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// describe the initialization of f from i, which will be a standard
395014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// conversion sequence containing an lvalue-to-rvalue conversion (C++
396014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// 4.1) followed by a floating-integral conversion (C++ 4.9).
397014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch//
398014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// Note that this routine only determines how the conversion can be
399014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// performed; it does not actually perform the conversion. As such,
400014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// it will not produce any diagnostics if no conversion is available,
401b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// but will instead return an implicit conversion sequence of kind
402b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// "BadConversion".
403b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch///
404b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// If @p SuppressUserConversions, then user-defined conversions are
405b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// not permitted.
406014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// If @p AllowExplicit, then explicit user-defined conversions are
407b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// permitted.
408b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// If @p ForceRValue, then overloading is performed as if From was an rvalue,
409b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// no matter its actual lvalueness.
410b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochImplicitConversionSequence
411b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochSema::TryImplicitConversion(Expr* From, QualType ToType,
412b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                            bool SuppressUserConversions,
413014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                            bool AllowExplicit, bool ForceRValue)
414b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch{
415b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ImplicitConversionSequence ICS;
416b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (IsStandardConversion(From, ToType, ICS.Standard))
417b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
418b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else if (getLangOptions().CPlusPlus &&
419b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch           IsUserDefinedConversion(From, ToType, ICS.UserDefined,
420b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   !SuppressUserConversions, AllowExplicit,
421b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   ForceRValue)) {
422014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion;
423b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // C++ [over.ics.user]p4:
424014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //   A conversion of an expression of class type to the same class
425b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   type is given Exact Match rank, and a conversion of an
426b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   expression of class type to a base class of that type is
427b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   given Conversion rank, in spite of the fact that a copy
428b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   constructor (i.e., a user-defined conversion function) is
429b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   called for those cases.
430014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (CXXConstructorDecl *Constructor
431b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) {
432b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      QualType FromCanon
433b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        = Context.getCanonicalType(From->getType().getUnqualifiedType());
434b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType();
435014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) {
436b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        // Turn this into a "standard" conversion sequence, so that it
437b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        // gets ranked with standard conversion sequences.
438014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
439b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        ICS.Standard.setAsIdentityConversion();
440b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr();
441b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr();
442b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        ICS.Standard.CopyConstructor = Constructor;
443b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        if (ToCanon != FromCanon)
444b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          ICS.Standard.Second = ICK_Derived_To_Base;
445b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      }
446b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
447b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
448b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // C++ [over.best.ics]p4:
449b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   However, when considering the argument of a user-defined
450b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   conversion function that is a candidate by 13.3.1.3 when
451b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   invoked for the copying of the temporary in the second step
452b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or
453b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   13.3.1.6 in all cases, only standard conversion sequences and
454014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //   ellipsis conversion sequences are allowed.
455b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (SuppressUserConversions &&
456014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion)
457b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
458b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else
459b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
460b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
461014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return ICS;
462b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
463014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
464b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// IsStandardConversion - Determines whether there is a standard
465b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the
466b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// expression From to the type ToType. Standard conversion sequences
467b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// only consider non-class types; for conversions that involve class
4681e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// types, use TryImplicitConversion. If a conversion exists, SCS will
469b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// contain the standard conversion sequence required to perform this
470b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// conversion and this routine will return true. Otherwise, this
471b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// routine will return false and the value of SCS is unspecified.
472b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool
473014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochSema::IsStandardConversion(Expr* From, QualType ToType,
474b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                           StandardConversionSequence &SCS)
4753fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch{
4763fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  QualType FromType = From->getType();
477b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
478b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Standard conversions (C++ [conv])
479b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  SCS.setAsIdentityConversion();
480b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  SCS.Deprecated = false;
481b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  SCS.IncompatibleObjC = false;
482b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  SCS.FromTypePtr = FromType.getAsOpaquePtr();
483b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  SCS.CopyConstructor = 0;
484b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
485b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // There are no standard conversions for class types in C++, so
486b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // abort early. When overloading in C, however, we do permit
487b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (FromType->isRecordType() || ToType->isRecordType()) {
488b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (getLangOptions().CPlusPlus)
489b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return false;
490b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
491b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // When we're overloading in C, we allow, as standard conversions,
492b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
493b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
494b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // The first conversion can be an lvalue-to-rvalue conversion,
495b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // array-to-pointer conversion, or function-to-pointer conversion
496b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // (C++ 4p1).
497b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
498b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Lvalue-to-rvalue conversion (C++ 4.1):
499b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   An lvalue (3.10) of a non-function, non-array type T can be
500b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   converted to an rvalue.
501b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Expr::isLvalueResult argIsLvalue = From->isLvalue(Context);
502b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (argIsLvalue == Expr::LV_Valid &&
5033fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch      !FromType->isFunctionType() && !FromType->isArrayType() &&
5043fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch      Context.getCanonicalType(FromType) != Context.OverloadTy) {
5053fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    SCS.First = ICK_Lvalue_To_Rvalue;
506b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
507b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // If T is a non-class type, the type of the rvalue is the
508b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // cv-unqualified version of T. Otherwise, the type of the rvalue
509b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // is T (C++ 4.1p1). C++ can't get here with class types; in C, we
510b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // just strip the qualifiers because they don't matter.
511b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
512014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // FIXME: Doesn't see through to qualifiers behind a typedef!
5133ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    FromType = FromType.getUnqualifiedType();
5143ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  } else if (FromType->isArrayType()) {
5153ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // Array-to-pointer conversion (C++ 4.2)
5163ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    SCS.First = ICK_Array_To_Pointer;
5173ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
5183ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // An lvalue or rvalue of type "array of N T" or "array of unknown
5193ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // bound of T" can be converted to an rvalue of type "pointer to
5203ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // T" (C++ 4.2p1).
5213ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    FromType = Context.getArrayDecayedType(FromType);
5223ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
5233ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    if (IsStringLiteralToNonConstPointerConversion(From, ToType)) {
524b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // This conversion is deprecated. (C++ D.4).
525b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      SCS.Deprecated = true;
526b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
527b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // For the purpose of ranking in overload resolution
5283ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // (13.3.3.1.1), this conversion is considered an
5293ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // array-to-pointer conversion followed by a qualification
5303ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // conversion (4.4). (C++ 4.2p2)
531014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      SCS.Second = ICK_Identity;
532b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      SCS.Third = ICK_Qualification;
533b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      SCS.ToTypePtr = ToType.getAsOpaquePtr();
534b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      return true;
535b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
5363ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) {
537b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // Function-to-pointer conversion (C++ 4.3).
538b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    SCS.First = ICK_Function_To_Pointer;
539b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
540b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // An lvalue of function type T can be converted to an rvalue of
541b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // type "pointer to T." The result is a pointer to the
542b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // function. (C++ 4.3p1).
543b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    FromType = Context.getPointerType(FromType);
544b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  } else if (FunctionDecl *Fn
545b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch             = ResolveAddressOfOverloadedFunction(From, ToType, false)) {
546b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // Address of overloaded function (C++ [over.over]).
547b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.First = ICK_Function_To_Pointer;
548b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
5491b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    // We were able to resolve the address of the overloaded function,
550b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // so we can convert to the type of that function.
551b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FromType = Fn->getType();
552b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (ToType->isLValueReferenceType())
553014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      FromType = Context.getLValueReferenceType(FromType);
554b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    else if (ToType->isRValueReferenceType())
555b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      FromType = Context.getRValueReferenceType(FromType);
556b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    else if (ToType->isMemberPointerType()) {
557b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      // Resolve address only succeeds if both sides are member pointers,
558b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      // but it doesn't have to be the same class. See DR 247.
559b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      // Note that this means that the type of &Derived::fn can be
560b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // Ret (Base::*)(Args) if the fn overload actually found is from the
561b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      // base class, even if it was brought into the derived class via a
562b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      // using declaration. The standard isn't clear on this issue at all.
563b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
564b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      FromType = Context.getMemberPointerType(FromType,
565b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                    Context.getTypeDeclType(M->getParent()).getTypePtr());
566b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    } else
567014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      FromType = Context.getPointerType(FromType);
568b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else {
569b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // We don't require any conversions for the first step.
570b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    SCS.First = ICK_Identity;
571014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
572b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
573b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // The second conversion can be an integral promotion, floating
574b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // point promotion, integral conversion, floating point conversion,
575b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // floating-integral conversion, pointer conversion,
576b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // pointer-to-member conversion, or boolean conversion (C++ 4p1).
577b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // For overloading in C, this can also be a "compatible-type"
578b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // conversion.
579b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  bool IncompatibleObjC = false;
580b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (Context.hasSameUnqualifiedType(FromType, ToType)) {
581b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // The unqualified versions of the types are the same: there's no
582b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // conversion to do.
583014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    SCS.Second = ICK_Identity;
584b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else if (IsIntegralPromotion(From, FromType, ToType)) {
585b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Integral promotion (C++ 4.5).
586b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Integral_Promotion;
587b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FromType = ToType.getUnqualifiedType();
588b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else if (IsFloatingPointPromotion(FromType, ToType)) {
589b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Floating point promotion (C++ 4.6).
590014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    SCS.Second = ICK_Floating_Promotion;
591b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FromType = ToType.getUnqualifiedType();
592b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (IsComplexPromotion(FromType, ToType)) {
593b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Complex promotion (Clang extension)
594b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Complex_Promotion;
595b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType = ToType.getUnqualifiedType();
596014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) &&
597b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch           (ToType->isIntegralType() && !ToType->isEnumeralType())) {
598b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Integral conversions (C++ 4.7).
599b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // FIXME: isIntegralType shouldn't be true for enums in C++.
600b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Integral_Conversion;
601b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    FromType = ToType.getUnqualifiedType();
602b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else if (FromType->isFloatingType() && ToType->isFloatingType()) {
603b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Floating point conversions (C++ 4.8).
604b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Floating_Conversion;
605b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType = ToType.getUnqualifiedType();
606b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (FromType->isComplexType() && ToType->isComplexType()) {
607b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Complex conversions (C99 6.3.1.6)
608b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Complex_Conversion;
609b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType = ToType.getUnqualifiedType();
610b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if ((FromType->isFloatingType() &&
611b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch              ToType->isIntegralType() && (!ToType->isBooleanType() &&
612b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                           !ToType->isEnumeralType())) ||
613b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch             ((FromType->isIntegralType() || FromType->isEnumeralType()) &&
614014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch              ToType->isFloatingType())) {
615b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Floating-integral conversions (C++ 4.9).
616b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // FIXME: isIntegralType shouldn't be true for enums in C++.
617b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Floating_Integral;
618b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType = ToType.getUnqualifiedType();
619b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if ((FromType->isComplexType() && ToType->isArithmeticType()) ||
620b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch             (ToType->isComplexType() && FromType->isArithmeticType())) {
621b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Complex-real conversions (C99 6.3.1.7)
622b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Complex_Real;
623b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType = ToType.getUnqualifiedType();
624b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  } else if (IsPointerConversion(From, FromType, ToType, FromType,
625b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                 IncompatibleObjC)) {
626b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Pointer conversions (C++ 4.10).
627b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Pointer_Conversion;
628b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.IncompatibleObjC = IncompatibleObjC;
629b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (IsMemberPointerConversion(From, FromType, ToType, FromType)) {
630b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Pointer to member conversions (4.11).
631b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Pointer_Member;
632b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (ToType->isBooleanType() &&
633b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch             (FromType->isArithmeticType() ||
634b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch              FromType->isEnumeralType() ||
635b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch              FromType->isPointerType() ||
636b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch              FromType->isBlockPointerType() ||
637b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch              FromType->isMemberPointerType() ||
638b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch              FromType->isNullPtrType())) {
639014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Boolean conversions (C++ 4.12).
640b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    SCS.Second = ICK_Boolean_Conversion;
641b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType = Context.BoolTy;
642b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  } else if (!getLangOptions().CPlusPlus &&
643b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch             Context.typesAreCompatible(ToType, FromType)) {
644b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // Compatible conversions (Clang extension for C function overloading)
645b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    SCS.Second = ICK_Compatible_Conversion;
646b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else {
647b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // No second conversion required.
648b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SCS.Second = ICK_Identity;
649b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
650b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
651b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType CanonFrom;
652b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType CanonTo;
653b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // The third conversion can be a qualification conversion (C++ 4p1).
654b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (IsQualificationConversion(FromType, ToType)) {
655b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    SCS.Third = ICK_Qualification;
656014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    FromType = ToType;
657b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    CanonFrom = Context.getCanonicalType(FromType);
658b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    CanonTo = Context.getCanonicalType(ToType);
659b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else {
660b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // No conversion required
661b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    SCS.Third = ICK_Identity;
662b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
663b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // C++ [over.best.ics]p6:
664b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   [...] Any difference in top-level cv-qualification is
665b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   subsumed by the initialization itself and does not constitute
666b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   a conversion. [...]
667b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    CanonFrom = Context.getCanonicalType(FromType);
668b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    CanonTo = Context.getCanonicalType(ToType);
669b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() &&
670b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) {
671b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      FromType = ToType;
672b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      CanonFrom = CanonTo;
673b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
674014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
675b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
676b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // If we have not converted the argument type to the parameter type,
677b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // this is a bad conversion sequence.
678b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (CanonFrom != CanonTo)
679b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
680b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
681b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  SCS.ToTypePtr = FromType.getAsOpaquePtr();
682b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return true;
683b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
684b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
685b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// IsIntegralPromotion - Determines whether the conversion from the
686b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// expression From (whose potentially-adjusted type is FromType) to
687b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// ToType is an integral promotion (C++ 4.5). If so, returns true and
688b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// sets PromotedType to the promoted type.
689b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType)
6903fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch{
691b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const BuiltinType *To = ToType->getAsBuiltinType();
692b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // All integers are built-in.
693b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (!To) {
694b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
695b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
696b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
697b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // An rvalue of type char, signed char, unsigned char, short int, or
698b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // unsigned short int can be converted to an rvalue of type int if
699014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // int can represent all the values of the source type; otherwise,
7003ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // the source rvalue can be converted to an rvalue of type unsigned
701b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // int (C++ 4.5p1).
702b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) {
703b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (// We can promote any signed, promotable integer type to an int
7043ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        (FromType->isSignedIntegerType() ||
7053ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch         // We can promote any unsigned integer type whose size is
7063ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch         // less than int to an int.
707b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch         (!FromType->isSignedIntegerType() &&
708b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) {
7093ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      return To->getKind() == BuiltinType::Int;
7103ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    }
711b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
712b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return To->getKind() == BuiltinType::UInt;
7133ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  }
7143ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
7153ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2)
716014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // can be converted to an rvalue of the first of the following types
717b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // that can represent all the values of its underlying type: int,
718b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // unsigned int, long, or unsigned long (C++ 4.5p2).
719b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if ((FromType->isEnumeralType() || FromType->isWideCharType())
720b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      && ToType->isIntegerType()) {
721b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // Determine whether the type we're converting from is signed or
722b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // unsigned.
723b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    bool FromIsSigned;
724b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    uint64_t FromSize = Context.getTypeSize(FromType);
725b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (const EnumType *FromEnumType = FromType->getAsEnumType()) {
726b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType();
727b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      FromIsSigned = UnderlyingType->isSignedIntegerType();
728b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    } else {
729b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now.
730b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      FromIsSigned = true;
731b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
732b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
733b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // The types we'll try to promote to, in the appropriate
734b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // order. Try each of these types.
735014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    QualType PromoteTypes[6] = {
736257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      Context.IntTy, Context.UnsignedIntTy,
737b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      Context.LongTy, Context.UnsignedLongTy ,
738b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      Context.LongLongTy, Context.UnsignedLongLongTy
739b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    };
740b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    for (int Idx = 0; Idx < 6; ++Idx) {
741b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]);
742b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (FromSize < ToSize ||
743b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          (FromSize == ToSize &&
744b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch           FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) {
745b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // We found the type that we can promote to. If this is the
746b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // type we wanted, we have a promotion. Otherwise, no
747257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        // promotion.
748257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        return Context.getCanonicalType(ToType).getUnqualifiedType()
749b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType();
750b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      }
751b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
752b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
753b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
754b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // An rvalue for an integral bit-field (9.6) can be converted to an
755b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // rvalue of type int if int can represent all the values of the
756b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // bit-field; otherwise, it can be converted to unsigned int if
757b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // unsigned int can represent all the values of the bit-field. If
758b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // the bit-field is larger yet, no integral promotion applies to
759b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // it. If the bit-field has an enumerated type, it is treated as any
760b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // other value of that type for promotion purposes (C++ 4.5p3).
761b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // FIXME: We should delay checking of bit-fields until we actually perform the
762b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // conversion.
763014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  using llvm::APSInt;
764b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (From)
765b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (FieldDecl *MemberDecl = From->getBitField()) {
766b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      APSInt BitWidth;
767b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (FromType->isIntegralType() && !FromType->isEnumeralType() &&
768b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch          MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) {
769b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned());
770b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        ToSize = Context.getTypeSize(ToType);
771b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
772b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // Are we promoting to an int from a bitfield that fits in an int?
773b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        if (BitWidth < ToSize ||
774b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            (FromType->isSignedIntegerType() && BitWidth <= ToSize)) {
775b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          return To->getKind() == BuiltinType::Int;
776b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        }
777b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
778b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        // Are we promoting to an unsigned int from an unsigned bitfield
779b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        // that fits into an unsigned int?
780014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) {
781b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          return To->getKind() == BuiltinType::UInt;
782b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        }
783b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
784b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return false;
785b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      }
786b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    }
787b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
788b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // An rvalue of type bool can be converted to an rvalue of type int,
789b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // with false becoming zero and true becoming one (C++ 4.5p4).
790b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) {
791b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return true;
792b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
793b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
794b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return false;
795b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
796b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
797014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// IsFloatingPointPromotion - Determines whether the conversion from
7983ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// FromType to ToType is a floating point promotion (C++ 4.6). If so,
799b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// returns true and sets PromotedType to the promoted type.
800b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType)
801b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch{
8023ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  /// An rvalue of type float can be converted to an rvalue of type
8033ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  /// double. (C++ 4.6p1).
804b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType())
805b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) {
806b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (FromBuiltin->getKind() == BuiltinType::Float &&
807b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          ToBuiltin->getKind() == BuiltinType::Double)
808b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return true;
809b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
810b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // C99 6.3.1.5p1:
811b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      //   When a float is promoted to double or long double, or a
812b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      //   double is promoted to long double [...].
813b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (!getLangOptions().CPlusPlus &&
814b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          (FromBuiltin->getKind() == BuiltinType::Float ||
8153ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch           FromBuiltin->getKind() == BuiltinType::Double) &&
816014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          (ToBuiltin->getKind() == BuiltinType::LongDouble))
8173ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        return true;
8183ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    }
8191b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
8201b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  return false;
8211b268ca467c924004286c97bac133db489cf43d0Ben Murdoch}
8221b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
8231b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// \brief Determine if a conversion is a complex promotion.
8241b268ca467c924004286c97bac133db489cf43d0Ben Murdoch///
8253ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// A complex promotion is defined as a complex -> complex conversion
8261b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// where the conversion between the underlying real types is a
8271b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// floating-point or integral promotion.
8281b268ca467c924004286c97bac133db489cf43d0Ben Murdochbool Sema::IsComplexPromotion(QualType FromType, QualType ToType) {
8291b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  const ComplexType *FromComplex = FromType->getAsComplexType();
8301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  if (!FromComplex)
8311b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    return false;
832b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
8331b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  const ComplexType *ToComplex = ToType->getAsComplexType();
834b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (!ToComplex)
835b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
836b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
8371b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  return IsFloatingPointPromotion(FromComplex->getElementType(),
838b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                  ToComplex->getElementType()) ||
839b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    IsIntegralPromotion(0, FromComplex->getElementType(),
840b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                        ToComplex->getElementType());
8411b268ca467c924004286c97bac133db489cf43d0Ben Murdoch}
8421b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
8431b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// BuildSimilarlyQualifiedPointerType - In a pointer conversion from
8441b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// the pointer type FromPtr to a pointer to type ToPointee, with the
8451b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// same type qualifiers as FromPtr has on its pointee type. ToType,
8461b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// if non-empty, will be a pointer to ToType that may or may not have
8471b268ca467c924004286c97bac133db489cf43d0Ben Murdoch/// the right set of qualifiers on its pointee.
8481b268ca467c924004286c97bac133db489cf43d0Ben Murdochstatic QualType
8491b268ca467c924004286c97bac133db489cf43d0Ben MurdochBuildSimilarlyQualifiedPointerType(const PointerType *FromPtr,
8501b268ca467c924004286c97bac133db489cf43d0Ben Murdoch                                   QualType ToPointee, QualType ToType,
851b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                   ASTContext &Context) {
8521b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType());
8531b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  QualType CanonToPointee = Context.getCanonicalType(ToPointee);
854257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  unsigned Quals = CanonFromPointee.getCVRQualifiers();
8551b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
856257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // Exact qualifier match -> return the pointer type we're converting to.
857257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  if (CanonToPointee.getCVRQualifiers() == Quals) {
858257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    // ToType is exactly what we need. Return it.
859257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    if (ToType.getTypePtr())
860b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return ToType;
861b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
862257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    // Build a pointer to ToPointee. It has the right qualifiers
8631b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    // already.
864b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return Context.getPointerType(ToPointee);
865257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  }
866257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
867257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // Just build a canonical type that has the right qualifiers.
868014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Context.getPointerType(CanonToPointee.getQualifiedType(Quals));
8693ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch}
870b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
8713ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// IsPointerConversion - Determines whether the conversion of the
872b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// expression From, which has the (possibly adjusted) type FromType,
8733ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// can be converted to the type ToType via a pointer conversion (C++
874b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// 4.10). If so, returns true and places the converted type (that
8753ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// might differ from ToType in its cv-qualifiers at some level) into
8763ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// ConvertedType.
8773ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch///
878014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// This routine also supports conversions to and from block pointers
879b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// and conversions with Objective-C's 'id', 'id<protocols...>', and
880b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// pointers to interfaces. FIXME: Once we've determined the
881b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// appropriate overloading rules for Objective-C, we may want to
882b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// split the Objective-C checks into a different routine; however,
883b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// GCC seems to consider all of these conversions to be pointer
884b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// conversions, so for now they live here. IncompatibleObjC will be
885b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// set if the conversion is an allowed Objective-C conversion that
886b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// should result in a warning.
887b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdochbool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType,
888b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                               QualType& ConvertedType,
889b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                               bool &IncompatibleObjC)
890b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch{
891b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  IncompatibleObjC = false;
892b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC))
893014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return true;
894e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
895b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Conversion from a null pointer constant to any Objective-C pointer type.
896e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (ToType->isObjCObjectPointerType() &&
897e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      From->isNullPointerConstant(Context)) {
898e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    ConvertedType = ToType;
899b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
900b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
901b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
902e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  // Blocks: Block pointers can be converted to void*.
903e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (FromType->isBlockPointerType() && ToType->isPointerType() &&
904e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) {
905014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ConvertedType = ToType;
906b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return true;
907b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
908b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // Blocks: A null pointer constant can be converted to a block
909b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // pointer type.
910b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (ToType->isBlockPointerType() && From->isNullPointerConstant(Context)) {
911b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ConvertedType = ToType;
912b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
913b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
914b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
915b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // If the left-hand-side is nullptr_t, the right side can be a null
91621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  // pointer constant.
91721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  if (ToType->isNullPtrType() && From->isNullPointerConstant(Context)) {
91821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    ConvertedType = ToType;
91921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    return true;
92021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  }
921b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
92221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  const PointerType* ToTypePtr = ToType->getAs<PointerType>();
92321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  if (!ToTypePtr)
92421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    return false;
92521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
9261e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // A null pointer constant can be converted to a pointer type (C++ 4.10p1).
92721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  if (From->isNullPointerConstant(Context)) {
92821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    ConvertedType = ToType;
92921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    return true;
93021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  }
93121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
93221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  // Beyond this point, both types need to be pointers.
93321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  const PointerType *FromTypePtr = FromType->getAs<PointerType>();
93421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  if (!FromTypePtr)
93521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    return false;
93621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
9371e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  QualType FromPointeeType = FromTypePtr->getPointeeType();
938b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType ToPointeeType = ToTypePtr->getPointeeType();
939b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
940b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // An rvalue of type "pointer to cv T," where T is an object type,
9411e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // can be converted to an rvalue of type "pointer to cv void" (C++
9421e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // 4.10p2).
9431e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) {
944014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
945b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                                       ToPointeeType,
946b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                                       ToType, Context);
947b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    return true;
948b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  }
949b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
950b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // When we're overloading in C, we allow a special kind of pointer
951b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // conversion for compatible-but-not-identical pointee types.
952b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (!getLangOptions().CPlusPlus &&
953b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      Context.typesAreCompatible(FromPointeeType, ToPointeeType)) {
954b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
955b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                                       ToPointeeType,
956014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                                       ToType, Context);
957b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return true;
958b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
959b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
960b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // C++ [conv.ptr]p3:
961b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //
962b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   An rvalue of type "pointer to cv D," where D is a class type,
963b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   can be converted to an rvalue of type "pointer to cv B," where
964b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   B is a base class (clause 10) of D. If B is an inaccessible
965b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   (clause 11) or ambiguous (10.2) base class of D, a program that
966b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   necessitates this conversion is ill-formed. The result of the
967b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   conversion is a pointer to the base class sub-object of the
968b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   derived class object. The null pointer value is converted to
969b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   the null pointer value of the destination type.
970014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //
971b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Note that we do not check for ambiguity or inaccessibility
972b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // here. That is handled by CheckPointerConversion.
973b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (getLangOptions().CPlusPlus &&
974b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      FromPointeeType->isRecordType() && ToPointeeType->isRecordType() &&
975b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      IsDerivedFrom(FromPointeeType, ToPointeeType)) {
976b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
977b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                                       ToPointeeType,
978b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                                       ToType, Context);
979b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
980b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
981b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
982b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return false;
983b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
984014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
985b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// isObjCPointerConversion - Determines whether this is an
986b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// Objective-C pointer conversion. Subroutine of IsPointerConversion,
987b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// with the same arguments and return values.
988b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool Sema::isObjCPointerConversion(QualType FromType, QualType ToType,
989b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   QualType& ConvertedType,
990b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   bool &IncompatibleObjC) {
991b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (!getLangOptions().ObjC1)
992b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
993b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
994b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // First, we handle all conversions on ObjC object pointer types.
995b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const ObjCObjectPointerType* ToObjCPtr = ToType->getAsObjCObjectPointerType();
996b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const ObjCObjectPointerType *FromObjCPtr =
997014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    FromType->getAsObjCObjectPointerType();
998b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
999b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (ToObjCPtr && FromObjCPtr) {
1000b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Objective C++: We're able to convert between "id" or "Class" and a
1001b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // pointer to any interface (in both directions).
1002b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) {
1003b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ConvertedType = ToType;
1004b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return true;
1005b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1006b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Conversions with Objective-C's id<...>.
1007b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if ((FromObjCPtr->isObjCQualifiedIdType() ||
1008b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch         ToObjCPtr->isObjCQualifiedIdType()) &&
1009b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType,
1010014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                                  /*compare=*/false)) {
1011b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ConvertedType = ToType;
1012b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return true;
1013b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1014014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Objective C++: We're able to convert from a pointer to an
1015b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // interface to a pointer to a different interface.
1016b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) {
1017b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ConvertedType = ToType;
1018b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return true;
1019b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1020b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1021b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) {
1022b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // Okay: this is some kind of implicit downcast of Objective-C
1023b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // interfaces, which is permitted. However, we're going to
1024b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // complain about it.
1025014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      IncompatibleObjC = true;
1026b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ConvertedType = FromType;
1027b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return true;
1028b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1029014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
1030b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Beyond this point, both types need to be C pointers or block pointers.
1031b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType ToPointeeType;
1032b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const PointerType *ToCPtr = ToType->getAs<PointerType>())
1033b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ToPointeeType = ToCPtr->getPointeeType();
1034b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>())
1035b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ToPointeeType = ToBlockPtr->getPointeeType();
1036b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else
1037b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
1038b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1039b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType FromPointeeType;
1040b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const PointerType *FromCPtr = FromType->getAs<PointerType>())
1041b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromPointeeType = FromCPtr->getPointeeType();
1042b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>())
1043014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    FromPointeeType = FromBlockPtr->getPointeeType();
1044b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else
1045b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
1046b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1047014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // If we have pointers to pointers, recursively check whether this
1048b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // is an Objective-C conversion.
1049b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() &&
1050b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType,
1051b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                              IncompatibleObjC)) {
1052b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // We always complain about this conversion.
1053b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    IncompatibleObjC = true;
1054b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ConvertedType = ToType;
1055014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return true;
1056b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1057b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // If we have pointers to functions or blocks, check whether the only
1058b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // differences in the argument and result types are in Objective-C
1059b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // pointer conversions. If so, we permit the conversion (but
1060b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // complain about it).
1061b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const FunctionProtoType *FromFunctionType
1062b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    = FromPointeeType->getAsFunctionProtoType();
1063014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const FunctionProtoType *ToFunctionType
1064b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    = ToPointeeType->getAsFunctionProtoType();
1065b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (FromFunctionType && ToFunctionType) {
1066b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // If the function types are exactly the same, this isn't an
1067b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Objective-C pointer conversion.
1068b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (Context.getCanonicalType(FromPointeeType)
1069014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          == Context.getCanonicalType(ToPointeeType))
1070b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return false;
1071b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1072b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Perform the quick checks that will tell us whether these
1073b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // function types are obviously different.
1074b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() ||
1075b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        FromFunctionType->isVariadic() != ToFunctionType->isVariadic() ||
1076b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals())
1077b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return false;
1078b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1079b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    bool HasObjCConversion = false;
1080b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (Context.getCanonicalType(FromFunctionType->getResultType())
1081b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          == Context.getCanonicalType(ToFunctionType->getResultType())) {
1082b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // Okay, the types match exactly. Nothing to do.
1083014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    } else if (isObjCPointerConversion(FromFunctionType->getResultType(),
1084b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                       ToFunctionType->getResultType(),
1085b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                       ConvertedType, IncompatibleObjC)) {
1086b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // Okay, we have an Objective-C pointer conversion.
1087014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      HasObjCConversion = true;
1088b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    } else {
1089b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // Function types are too different. Abort.
1090b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return false;
1091b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1092b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1093b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Check argument types.
1094b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
1095b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch         ArgIdx != NumArgs; ++ArgIdx) {
1096b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      QualType FromArgType = FromFunctionType->getArgType(ArgIdx);
1097b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      QualType ToArgType = ToFunctionType->getArgType(ArgIdx);
1098b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (Context.getCanonicalType(FromArgType)
1099b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch            == Context.getCanonicalType(ToArgType)) {
1100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        // Okay, the types match exactly. Nothing to do.
1101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      } else if (isObjCPointerConversion(FromArgType, ToArgType,
1102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                         ConvertedType, IncompatibleObjC)) {
1103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // Okay, we have an Objective-C pointer conversion.
1104b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        HasObjCConversion = true;
1105b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      } else {
1106b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // Argument types are too different. Abort.
1107b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return false;
1108b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      }
1109b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1110b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1111014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (HasObjCConversion) {
1112b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // We had an Objective-C conversion. Allow this pointer
1113b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // conversion, but complain about it.
1114b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ConvertedType = ToType;
1115014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      IncompatibleObjC = true;
1116b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return true;
1117b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1118b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1119b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1120b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return false;
1121b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
1122b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1123b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CheckPointerConversion - Check the pointer conversion from the
1124b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// expression From to the type ToType. This routine checks for
1125b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// ambiguous or inaccessible derived-to-base pointer
1126b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// conversions for which IsPointerConversion has already returned
1127b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// true. It returns true and produces a diagnostic if there was an
1128b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// error, or returns false otherwise.
1129b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool Sema::CheckPointerConversion(Expr *From, QualType ToType) {
1130b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType FromType = From->getType();
1131014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1132b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const PointerType *FromPtrType = FromType->getAs<PointerType>())
1133b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) {
1134b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      QualType FromPointeeType = FromPtrType->getPointeeType(),
1135014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch               ToPointeeType   = ToPtrType->getPointeeType();
1136b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1137b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (FromPointeeType->isRecordType() &&
1138b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          ToPointeeType->isRecordType()) {
1139b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // We must have a derived-to-base conversion. Check an
1140b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // ambiguous or inaccessible conversion.
1141b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType,
1142b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                            From->getExprLoc(),
1143b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                            From->getSourceRange());
11443fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch      }
1145b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
1146b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const ObjCObjectPointerType *FromPtrType =
1147b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        FromType->getAsObjCObjectPointerType())
1148b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (const ObjCObjectPointerType *ToPtrType =
1149b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          ToType->getAsObjCObjectPointerType()) {
1150b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // Objective-C++ conversions are always okay.
1151014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // FIXME: We should have a different class of conversions for the
1152b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // Objective-C++ implicit conversions.
1153014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType())
1154014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        return false;
1155014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1156014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
1157014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return false;
1158b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch}
1159086aeeaae12517475c22695a200be45495516549Ben Murdoch
1160014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// IsMemberPointerConversion - Determines whether the conversion of the
1161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// expression From, which has the (possibly adjusted) type FromType, can be
1162014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// converted to the type ToType via a member pointer conversion (C++ 4.11).
1163b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// If so, returns true and places the converted type (that might differ from
1164014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// ToType in its cv-qualifiers at some level) into ConvertedType.
1165014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochbool Sema::IsMemberPointerConversion(Expr *From, QualType FromType,
1166014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                     QualType ToType, QualType &ConvertedType)
1167086aeeaae12517475c22695a200be45495516549Ben Murdoch{
1168086aeeaae12517475c22695a200be45495516549Ben Murdoch  const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>();
1169086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (!ToTypePtr)
1170014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return false;
1171b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1172b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // A null pointer constant can be converted to a member pointer (C++ 4.11p1)
1173b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (From->isNullPointerConstant(Context)) {
1174b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    ConvertedType = ToType;
1175b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    return true;
1176b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
1177b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1178b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // Otherwise, both types have to be member pointers.
1179b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>();
1180b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (!FromTypePtr)
1181b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
1182b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1183b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // A pointer to member of B can be converted to a pointer to member of D,
1184b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // where D is derived from B (C++ 4.11p2).
1185014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  QualType FromClass(FromTypePtr->getClass(), 0);
1186b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType ToClass(ToTypePtr->getClass(), 0);
11873ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // FIXME: What happens when these are dependent? Is this function even called?
1188b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1189b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (IsDerivedFrom(ToClass, FromClass)) {
1190b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(),
1191b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                                 ToClass.getTypePtr());
1192b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
1193b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1194b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1195b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return false;
11963ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch}
1197b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1198b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CheckMemberPointerConversion - Check the member pointer conversion from the
1199b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// expression From to the type ToType. This routine checks for ambiguous or
1200b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions
1201b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// for which IsMemberPointerConversion has already returned true. It returns
1202014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// true and produces a diagnostic if there was an error, or returns false
1203b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// otherwise.
1204b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdochbool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType,
1205b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                        CastExpr::CastKind &Kind) {
1206b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  QualType FromType = From->getType();
1207b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>();
1208b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (!FromPtrType) {
1209b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // This must be a null pointer to member pointer conversion
1210b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    assert(From->isNullPointerConstant(Context) &&
1211b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch           "Expr must be null pointer constant!");
1212b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    Kind = CastExpr::CK_NullToMemberPointer;
1213b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return false;
1214b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
1215b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1216b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>();
1217b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assert(ToPtrType && "No member pointer cast has a target type "
1218b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                      "that is not a member pointer.");
1219b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1220b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType FromClass = QualType(FromPtrType->getClass(), 0);
1221b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType ToClass   = QualType(ToPtrType->getClass(), 0);
1222b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1223b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // FIXME: What about dependent types?
1224014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  assert(FromClass->isRecordType() && "Pointer into non-class.");
1225b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  assert(ToClass->isRecordType() && "Pointer into non-class.");
1226b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1227b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
1228b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                  /*DetectVirtual=*/true);
1229b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths);
1230b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  assert(DerivationOkay &&
1231b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch         "Should not have been called if derivation isn't OK.");
1232b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  (void)DerivationOkay;
1233b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1234b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (Paths.isAmbiguous(Context.getCanonicalType(FromClass).
1235b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                  getUnqualifiedType())) {
1236b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Derivation is ambiguous. Redo the check to find the exact paths.
1237b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    Paths.clear();
1238b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    Paths.setRecordingPaths(true);
1239014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths);
1240b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    assert(StillOkay && "Derivation changed due to quantum fluctuation.");
1241b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    (void)StillOkay;
12421e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
1243b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths);
12441e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv)
1245b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange();
1246b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return true;
1247b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
1248014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1249b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const RecordType *VBase = Paths.getDetectedVirtual()) {
1250b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual)
1251b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      << FromClass << ToClass << QualType(VBase, 0)
1252b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      << From->getSourceRange();
1253b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return true;
1254b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1255b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1256b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Must be a base to derived member conversion.
1257014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Kind = CastExpr::CK_BaseToDerivedMemberPointer;
1258b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return false;
12591e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block}
12601e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
12611e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// IsQualificationConversion - Determines whether the conversion from
1262b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// an rvalue of type FromType to ToType is a qualification conversion
1263b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// (C++ 4.4).
1264b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool
1265b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochSema::IsQualificationConversion(QualType FromType, QualType ToType)
12661e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block{
1267b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  FromType = Context.getCanonicalType(FromType);
1268014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ToType = Context.getCanonicalType(ToType);
1269b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1270b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // If FromType and ToType are the same type, this is not a
1271b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // qualification conversion.
1272014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (FromType == ToType)
1273b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return false;
1274b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
12751e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // (C++ 4.4p4):
1276b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  //   A conversion can add cv-qualifiers at levels other than the first
1277b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   in multi-level pointers, subject to the following rules: [...]
1278b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  bool PreviousToQualsIncludeConst = true;
1279b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  bool UnwrappedAnyPointer = false;
1280b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  while (UnwrapSimilarPointerTypes(FromType, ToType)) {
1281b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Within each iteration of the loop, we check the qualifiers to
1282b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // determine if this still looks like a qualification
1283014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // conversion. Then, if all is well, we unwrap one more level of
1284b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // pointers or pointers-to-members and do it all again
1285b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // until there are no more pointers or pointers-to-members left to
1286b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // unwrap.
1287b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    UnwrappedAnyPointer = true;
1288b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1289b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   -- for every j > 0, if const is in cv 1,j then const is in cv
1290b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //      2,j, and similarly for volatile.
1291b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (!ToType.isAtLeastAsQualifiedAs(FromType))
1292b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      return false;
1293b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1294014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //   -- if the cv 1,j and cv 2,j are different, then const is in
1295b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      every cv for 0 < k < j.
1296b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers()
1297b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        && !PreviousToQualsIncludeConst)
1298b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return false;
1299b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1300b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Keep track of whether all prior cv-qualifiers in the "to" type
1301b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // include const.
1302b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    PreviousToQualsIncludeConst
1303b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      = PreviousToQualsIncludeConst && ToType.isConstQualified();
1304b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1305b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1306b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // We are left with FromType and ToType being the pointee types
1307014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // after unwrapping the original FromType and ToType the same number
1308b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // of types. If we unwrapped any pointers, and if FromType and
1309b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // ToType have the same unqualified type (since we checked
1310b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // qualifiers above), then this is a qualification conversion.
1311014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return UnwrappedAnyPointer &&
1312b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    FromType.getUnqualifiedType() == ToType.getUnqualifiedType();
1313b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
1314b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1315b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// \brief Given a function template or function, extract the function template
1316b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// declaration (if any) and the underlying function declaration.
1317b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate<typename T>
1318b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdochstatic void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function,
1319b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   FunctionTemplateDecl *&FunctionTemplate) {
1320b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig);
13219fac840a46e8b7e26894f4792ba26dde14c56b04Steve Block  if (FunctionTemplate)
1322b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Function = cast<T>(FunctionTemplate->getTemplatedDecl());
1323b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  else
1324b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    Function = cast<T>(Orig);
1325b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
1326014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1327b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1328b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// Determines whether there is a user-defined conversion sequence
1329b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// (C++ [over.ics.user]) that converts expression From to the type
1330b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// ToType. If such a conversion exists, User will contain the
1331b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// user-defined conversion sequence that performs such a conversion
1332b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// and this routine will return true. Otherwise, this routine returns
1333b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// false and User is unspecified.
1334b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch///
13353fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// \param AllowConversionFunctions true if the conversion should
1336b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// consider conversion functions at all. If false, only constructors
1337b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// will be considered.
1338b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch///
1339b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// \param AllowExplicit  true if the conversion should consider C++0x
1340b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// "explicit" conversion functions as well as non-explicit conversion
1341014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// functions (C++0x [class.conv.fct]p2).
1342b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch///
1343b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// \param ForceRValue  true if the expression should be treated as an rvalue
1344b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// for overload resolution.
1345b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool Sema::IsUserDefinedConversion(Expr *From, QualType ToType,
1346b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   UserDefinedConversionSequence& User,
1347b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   bool AllowConversionFunctions,
1348b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   bool AllowExplicit, bool ForceRValue)
1349b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch{
1350b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  OverloadCandidateSet CandidateSet;
1351b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) {
1352b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (CXXRecordDecl *ToRecordDecl
1353b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) {
1354b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // C++ [over.match.ctor]p1:
1355b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      //   When objects of class type are direct-initialized (8.5), or
1356b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      //   copy-initialized from an expression of the same or a
1357b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      //   derived class type (8.5), overload resolution selects the
1358b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      //   constructor. [...] For copy-initialization, the candidate
1359b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      //   functions are all the converting constructors (12.3.1) of
1360b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      //   that class. The argument list is the expression-list within
1361b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      //   the parentheses of the initializer.
1362014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      DeclarationName ConstructorName
1363b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        = Context.DeclarationNames.getCXXConstructorName(
1364b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                          Context.getCanonicalType(ToType).getUnqualifiedType());
1365b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      DeclContext::lookup_iterator Con, ConEnd;
1366b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      for (llvm::tie(Con, ConEnd)
1367b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch             = ToRecordDecl->lookup(ConstructorName);
1368b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch           Con != ConEnd; ++Con) {
1369b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        // Find the constructor (which may be a template).
1370b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        CXXConstructorDecl *Constructor = 0;
1371b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        FunctionTemplateDecl *ConstructorTmpl
1372b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          = dyn_cast<FunctionTemplateDecl>(*Con);
1373b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        if (ConstructorTmpl)
1374b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          Constructor
1375b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch            = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl());
1376b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        else
1377b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          Constructor = cast<CXXConstructorDecl>(*Con);
1378b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1379b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        if (!Constructor->isInvalidDecl() &&
1380b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            Constructor->isConvertingConstructor()) {
1381b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          if (ConstructorTmpl)
1382014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From,
1383b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         1, CandidateSet,
1384b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                         /*SuppressUserConversions=*/true,
1385b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                         ForceRValue);
1386b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch          else
1387b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch            AddOverloadCandidate(Constructor, &From, 1, CandidateSet,
1388b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                 /*SuppressUserConversions=*/true, ForceRValue);
1389b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        }
1390b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      }
1391b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1392b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1393b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1394b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (!AllowConversionFunctions) {
1395b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // Don't allow any conversion functions to enter the overload set.
1396b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else if (RequireCompleteType(From->getLocStart(), From->getType(),
1397014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                 PDiag(0)
13983ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                   << From->getSourceRange())) {
1399b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // No conversion functions from incomplete types.
1400b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (const RecordType *FromRecordType
1401b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch               = From->getType()->getAs<RecordType>()) {
14023ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    if (CXXRecordDecl *FromRecordDecl
14033ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch          = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) {
1404b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // Add all of the conversion functions as candidates.
1405b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // FIXME: Look for conversions in base classes!
1406b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      OverloadedFunctionDecl *Conversions
1407b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        = FromRecordDecl->getConversionFunctions();
1408b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      for (OverloadedFunctionDecl::function_iterator Func
14093ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch             = Conversions->function_begin();
14103ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch           Func != Conversions->function_end(); ++Func) {
14113ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        CXXConversionDecl *Conv;
1412014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        FunctionTemplateDecl *ConvTemplate;
1413b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        GetFunctionAndTemplate(*Func, Conv, ConvTemplate);
1414b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        if (ConvTemplate)
1415b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch          Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
1416b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        else
1417b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch          Conv = dyn_cast<CXXConversionDecl>(*Func);
1418b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1419b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        if (AllowExplicit || !Conv->isExplicit()) {
1420b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          if (ConvTemplate)
1421b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch            AddTemplateConversionCandidate(ConvTemplate, From, ToType,
1422b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                           CandidateSet);
1423b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          else
1424b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            AddConversionCandidate(Conv, From, ToType, CandidateSet);
1425014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        }
1426014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
1427014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
1428b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
1429b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1430b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  OverloadCandidateSet::iterator Best;
1431b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) {
1432b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    case OR_Success:
1433b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // Record the standard conversion we used and the conversion function.
1434014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (CXXConstructorDecl *Constructor
1435b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            = dyn_cast<CXXConstructorDecl>(Best->Function)) {
14363fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        // C++ [over.ics.user]p1:
14373fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        //   If the user-defined conversion is specified by a
14383fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        //   constructor (12.3.1), the initial standard conversion
14393fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        //   sequence converts the source type to the type required by
1440b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        //   the argument of the constructor.
14413fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        //
14423fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        // FIXME: What about ellipsis conversions?
14433fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        QualType ThisType = Constructor->getThisType(Context);
1444b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        User.Before = Best->Conversions[0].Standard;
1445b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        User.ConversionFunction = Constructor;
14463fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        User.After.setAsIdentityConversion();
14473fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch        User.After.FromTypePtr
14483fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch          = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr();
1449014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        User.After.ToTypePtr = ToType.getAsOpaquePtr();
1450b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return true;
1451014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      } else if (CXXConversionDecl *Conversion
1452014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                   = dyn_cast<CXXConversionDecl>(Best->Function)) {
1453014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        // C++ [over.ics.user]p1:
1454b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //
1455014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        //   [...] If the user-defined conversion is specified by a
1456014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        //   conversion function (12.3.2), the initial standard
1457b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        //   conversion sequence converts the source type to the
1458b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        //   implicit object parameter of the conversion function.
1459b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        User.Before = Best->Conversions[0].Standard;
1460b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        User.ConversionFunction = Conversion;
1461b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1462014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        // C++ [over.ics.user]p2:
1463b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        //   The second standard conversion sequence converts the
1464b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //   result of the user-defined conversion to the target type
1465b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //   for the sequence. Since an implicit conversion sequence
1466b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //   is an initialization, the special rules for
1467b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        //   initialization by user-defined conversion apply when
1468b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //   selecting the best user-defined conversion for a
1469b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //   user-defined conversion sequence (see 13.3.3 and
1470b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        //   13.3.3.1).
1471b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        User.After = Best->FinalConversion;
1472b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return true;
1473b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      } else {
1474b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        assert(false && "Not a constructor or conversion function?");
1475b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return false;
1476b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      }
1477b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1478b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    case OR_No_Viable_Function:
1479b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    case OR_Deleted:
1480b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // No conversion here! We're done.
1481b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      return false;
1482b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1483b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    case OR_Ambiguous:
1484b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // FIXME: See C++ [over.best.ics]p10 for the handling of
1485b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // ambiguous conversion sequences.
1486014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return false;
1487b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
1488b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1489b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  return false;
1490b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch}
1491b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1492b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CompareImplicitConversionSequences - Compare two implicit
1493b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// conversion sequences to determine whether one is better than the
1494b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// other or if they are indistinguishable (C++ 13.3.3.2).
1495b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochImplicitConversionSequence::CompareKind
149644f0eee88ff00398ff7f715fab053374d808c90dSteve BlockSema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1,
149744f0eee88ff00398ff7f715fab053374d808c90dSteve Block                                         const ImplicitConversionSequence& ICS2)
149844f0eee88ff00398ff7f715fab053374d808c90dSteve Block{
1499014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // (C++ 13.3.3.2p2): When comparing the basic forms of implicit
150044f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // conversion sequences (as defined in 13.3.3.1)
1501b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   -- a standard conversion sequence (13.3.3.1.1) is a better
15023fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  //      conversion sequence than a user-defined conversion sequence or
15033fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  //      an ellipsis conversion sequence, and
1504b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   -- a user-defined conversion sequence (13.3.3.1.2) is a better
150544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  //      conversion sequence than an ellipsis conversion sequence
150644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  //      (13.3.3.1.3).
15073fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  //
15083fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  if (ICS1.ConversionKind < ICS2.ConversionKind)
1509b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ImplicitConversionSequence::Better;
1510b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  else if (ICS2.ConversionKind < ICS1.ConversionKind)
1511b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return ImplicitConversionSequence::Worse;
1512b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1513b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Two implicit conversion sequences of the same form are
1514b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // indistinguishable conversion sequences unless one of the
1515b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // following rules apply: (C++ 13.3.3.2p3):
1516b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion)
1517b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard);
1518014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  else if (ICS1.ConversionKind ==
15199fac840a46e8b7e26894f4792ba26dde14c56b04Steve Block             ImplicitConversionSequence::UserDefinedConversion) {
1520b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // User-defined conversion sequence U1 is a better conversion
1521b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // sequence than another user-defined conversion sequence U2 if
1522b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // they contain the same user-defined conversion function or
1523b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // constructor and if the second standard conversion sequence of
15249fac840a46e8b7e26894f4792ba26dde14c56b04Steve Block    // U1 is better than the second standard conversion sequence of
1525b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // U2 (C++ 13.3.3.2p3).
1526b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (ICS1.UserDefined.ConversionFunction ==
1527b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          ICS2.UserDefined.ConversionFunction)
1528b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      return CompareStandardConversionSequences(ICS1.UserDefined.After,
1529b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                                ICS2.UserDefined.After);
1530b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
1531b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1532b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return ImplicitConversionSequence::Indistinguishable;
1533014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
15341e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
1535b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CompareStandardConversionSequences - Compare two standard
1536b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// conversion sequences to determine whether one is better than the
15371e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// other or if they are indistinguishable (C++ 13.3.3.2p3).
1538b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochImplicitConversionSequence::CompareKind
15391e0659c275bb392c045087af4f6b0d7565cb3d77Steve BlockSema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1,
15401e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block                                         const StandardConversionSequence& SCS2)
15411e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block{
1542014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Standard conversion sequence S1 is a better conversion sequence
1543b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // than standard conversion sequence S2 if (C++ 13.3.3.2p3):
1544014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1545b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //  -- S1 is a proper subsequence of S2 (comparing the conversion
1546b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //     sequences in the canonical form defined by 13.3.3.1.1,
1547014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //     excluding any Lvalue Transformation; the identity conversion
1548b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //     sequence is considered to be a subsequence of any
1549b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //     non-identity conversion sequence) or, if not that,
1550b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third)
1551014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Neither is a proper subsequence of the other. Do nothing.
1552b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ;
1553b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) ||
15543fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch           (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) ||
1555b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch           (SCS1.Second == ICK_Identity &&
1556b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch            SCS1.Third == ICK_Identity))
1557b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // SCS1 is a proper subsequence of SCS2.
1558b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return ImplicitConversionSequence::Better;
1559b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) ||
1560b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch           (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) ||
1561b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch           (SCS2.Second == ICK_Identity &&
1562014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            SCS2.Third == ICK_Identity))
1563b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // SCS2 is a proper subsequence of SCS1.
1564b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ImplicitConversionSequence::Worse;
1565b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
156644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  //  -- the rank of S1 is better than the rank of S2 (by the rules
1567b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  //     defined below), or, if not that,
1568b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ImplicitConversionRank Rank1 = SCS1.getRank();
1569b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ImplicitConversionRank Rank2 = SCS2.getRank();
1570b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (Rank1 < Rank2)
1571b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ImplicitConversionSequence::Better;
1572b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  else if (Rank2 < Rank1)
1573b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ImplicitConversionSequence::Worse;
1574b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1575b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // (C++ 13.3.3.2p4): Two conversion sequences with the same rank
1576b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // are indistinguishable unless one of the following rules
1577014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // applies:
1578014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1579b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   A conversion that is not a conversion of a pointer, or
1580b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   pointer to member, to bool is better than another conversion
1581b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   that is such a conversion.
1582014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool())
15831e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    return SCS2.isPointerConversionToBool()
1584b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch             ? ImplicitConversionSequence::Better
1585b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch             : ImplicitConversionSequence::Worse;
15861e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
15871e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // C++ [over.ics.rank]p4b2:
15881e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  //
1589b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   If class B is derived directly or indirectly from class A,
15901e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  //   conversion of B* to A* is better than conversion of B* to
15911e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  //   void*, and conversion of A* to void* is better than conversion
15921e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  //   of B* to void*.
15931e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  bool SCS1ConvertsToVoid
15941e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    = SCS1.isPointerConversionToVoidPointer(Context);
1595b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  bool SCS2ConvertsToVoid
1596b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    = SCS2.isPointerConversionToVoidPointer(Context);
1597b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) {
1598b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Exactly one of the conversion sequences is a conversion to
15991e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    // a void pointer; it's the worse conversion.
16001e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better
160121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch                              : ImplicitConversionSequence::Worse;
1602b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) {
160321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    // Neither conversion sequence converts to a void pointer; compare
16048b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch    // their derived-to-base conversions.
1605b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (ImplicitConversionSequence::CompareKind DerivedCK
16068b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch          = CompareDerivedToBaseConversions(SCS1, SCS2))
16078b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch      return DerivedCK;
1608b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) {
1609b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Both conversion sequences are conversions to void
1610b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // pointers. Compare the source types to determine if there's an
16118b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch    // inheritance relationship in their sources.
16128b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch    QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr);
16138b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch    QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr);
16148b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch
1615014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Adjust the types we're converting from via the array-to-pointer
1616b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // conversion, if we need to.
1617b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (SCS1.First == ICK_Array_To_Pointer)
1618b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      FromType1 = Context.getArrayDecayedType(FromType1);
1619014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (SCS2.First == ICK_Array_To_Pointer)
1620b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      FromType2 = Context.getArrayDecayedType(FromType2);
16211e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
16221e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    QualType FromPointee1
16231e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block      = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType();
16241e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    QualType FromPointee2
1625b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType();
1626b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1627b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    if (IsDerivedFrom(FromPointee2, FromPointee1))
1628b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      return ImplicitConversionSequence::Better;
1629b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    else if (IsDerivedFrom(FromPointee1, FromPointee2))
1630b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      return ImplicitConversionSequence::Worse;
1631b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1632014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Objective-C++: If one interface is more specific than the
1633b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // other, it is the better one.
1634b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType();
1635b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType();
1636014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (FromIface1 && FromIface1) {
16371e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block      if (Context.canAssignObjCInterfaces(FromIface2, FromIface1))
16381e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block        return ImplicitConversionSequence::Better;
16391e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block      else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2))
16401e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block        return ImplicitConversionSequence::Worse;
16411e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    }
16421e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  }
16431e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
1644b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Compare based on qualification conversions (C++ 13.3.3.2p3,
1645b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // bullet 3).
1646b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (ImplicitConversionSequence::CompareKind QualCK
1647b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        = CompareQualificationConversions(SCS1, SCS2))
16481e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    return QualCK;
16491e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
16501e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) {
16511e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    // C++0x [over.ics.rank]p3b4:
16521e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    //   -- S1 and S2 are reference bindings (8.5.3) and neither refers to an
1653014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //      implicit object parameter of a non-static member function declared
16541e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    //      without a ref-qualifier, and S1 binds an rvalue reference to an
16551e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    //      rvalue and S2 binds an lvalue reference.
16561e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    // FIXME: We don't know if we're dealing with the implicit object parameter,
1657014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // or if the member function in this case has a ref qualifier.
1658b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    // (Of course, we don't have ref qualifiers yet.)
1659b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    if (SCS1.RRefBinding != SCS2.RRefBinding)
1660b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      return SCS1.RRefBinding ? ImplicitConversionSequence::Better
1661b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                              : ImplicitConversionSequence::Worse;
1662b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1663b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // C++ [over.ics.rank]p3b4:
1664b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   -- S1 and S2 are reference bindings (8.5.3), and the types to
1665b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      which the references refer are the same type except for
1666b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      top-level cv-qualifiers, and the type to which the reference
1667b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      initialized by S2 refers is more cv-qualified than the type
1668b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      to which the reference initialized by S1 refers.
1669014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr);
16703ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr);
1671b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    T1 = Context.getCanonicalType(T1);
1672b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    T2 = Context.getCanonicalType(T2);
1673b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) {
1674b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (T2.isMoreQualifiedThan(T1))
1675b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Better;
1676b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      else if (T1.isMoreQualifiedThan(T2))
1677b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Worse;
1678b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
16793fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  }
16803fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch
16813fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  return ImplicitConversionSequence::Indistinguishable;
1682014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
16831e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
1684b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CompareQualificationConversions - Compares two standard conversion
1685b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// sequences to determine whether they can be ranked based on their
1686b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// qualification conversions (C++ 13.3.3.2p3 bullet 3).
1687b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochImplicitConversionSequence::CompareKind
1688b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochSema::CompareQualificationConversions(const StandardConversionSequence& SCS1,
1689b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                      const StandardConversionSequence& SCS2)
1690b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch{
1691b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // C++ 13.3.3.2p3:
1692014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //  -- S1 and S2 differ only in their qualification conversion and
1693b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //     yield similar types T1 and T2 (C++ 4.4), respectively, and the
1694b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //     cv-qualification signature of type T1 is a proper subset of
1695b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //     the cv-qualification signature of type T2, and S1 is not the
16961e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  //     deprecated string literal array-to-pointer conversion (4.2).
16971e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second ||
16981e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block      SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification)
1699014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return ImplicitConversionSequence::Indistinguishable;
1700b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1701b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // FIXME: the example in the standard doesn't use a qualification
1702b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // conversion (!)
1703b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr);
17041e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr);
17051e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  T1 = Context.getCanonicalType(T1);
1706b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  T2 = Context.getCanonicalType(T2);
1707b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1708b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // If the types are the same, we won't learn anything by unwrapped
1709014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // them.
17101e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (T1.getUnqualifiedType() == T2.getUnqualifiedType())
1711b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ImplicitConversionSequence::Indistinguishable;
17121e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
17131e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  ImplicitConversionSequence::CompareKind Result
17141e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    = ImplicitConversionSequence::Indistinguishable;
1715014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  while (UnwrapSimilarPointerTypes(T1, T2)) {
1716b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Within each iteration of the loop, we check the qualifiers to
1717b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // determine if this still looks like a qualification
1718b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // conversion. Then, if all is well, we unwrap one more level of
1719b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // pointers or pointers-to-members and do it all again
1720b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // until there are no more pointers or pointers-to-members left
1721b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // to unwrap. This essentially mimics what
1722014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // IsQualificationConversion does, but here we're checking for a
1723b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // strict subset of qualifiers.
1724b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (T1.getCVRQualifiers() == T2.getCVRQualifiers())
1725b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // The qualifiers are the same, so this doesn't tell us anything
1726b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // about how the sequences rank.
1727b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ;
1728b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    else if (T2.isMoreQualifiedThan(T1)) {
1729014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // T1 has fewer qualifiers, so it could be the better sequence.
17303ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      if (Result == ImplicitConversionSequence::Worse)
17313ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        // Neither has qualifiers that are a subset of the other's
17323ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        // qualifiers.
17333ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        return ImplicitConversionSequence::Indistinguishable;
17343ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
1735b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      Result = ImplicitConversionSequence::Better;
1736b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    } else if (T1.isMoreQualifiedThan(T2)) {
17373ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // T2 has fewer qualifiers, so it could be the better sequence.
17383ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      if (Result == ImplicitConversionSequence::Better)
17393ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        // Neither has qualifiers that are a subset of the other's
17403ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        // qualifiers.
17413ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        return ImplicitConversionSequence::Indistinguishable;
1742014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1743b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      Result = ImplicitConversionSequence::Worse;
1744b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    } else {
1745b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // Qualifiers are disjoint.
1746014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ImplicitConversionSequence::Indistinguishable;
1747014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
1748014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1749014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // If the types after this point are equivalent, we're done.
1750014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (T1.getUnqualifiedType() == T2.getUnqualifiedType())
1751014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      break;
1752b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
17531e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
17541e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // Check that the winning standard conversion sequence isn't using
1755b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // the deprecated string literal array to pointer conversion.
1756b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  switch (Result) {
1757b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  case ImplicitConversionSequence::Better:
1758b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (SCS1.Deprecated)
1759014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      Result = ImplicitConversionSequence::Indistinguishable;
1760014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    break;
1761014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1762014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  case ImplicitConversionSequence::Indistinguishable:
1763b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    break;
1764b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1765b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  case ImplicitConversionSequence::Worse:
1766014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (SCS2.Deprecated)
1767b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      Result = ImplicitConversionSequence::Indistinguishable;
1768b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    break;
1769b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1770b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1771b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Result;
1772b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
1773014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1774014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// CompareDerivedToBaseConversions - Compares two standard conversion
1775b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// sequences to determine whether they can be ranked based on their
1776014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// various kinds of derived-to-base conversions (C++
1777014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// [over.ics.rank]p4b3).  As part of these checks, we also look at
1778b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// conversions between Objective-C interface types.
1779b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochImplicitConversionSequence::CompareKind
1780b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochSema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1,
1781014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                      const StandardConversionSequence& SCS2) {
1782257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr);
1783257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr);
1784257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr);
1785257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr);
1786257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
1787257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // Adjust the types we're converting from via the array-to-pointer
1788257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // conversion, if we need to.
1789257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  if (SCS1.First == ICK_Array_To_Pointer)
1790257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch    FromType1 = Context.getArrayDecayedType(FromType1);
1791b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (SCS2.First == ICK_Array_To_Pointer)
1792b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    FromType2 = Context.getArrayDecayedType(FromType2);
1793b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1794014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Canonicalize all of the types.
1795257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  FromType1 = Context.getCanonicalType(FromType1);
1796257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  ToType1 = Context.getCanonicalType(ToType1);
1797257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  FromType2 = Context.getCanonicalType(FromType2);
1798257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  ToType2 = Context.getCanonicalType(ToType2);
1799257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
1800014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // C++ [over.ics.rank]p4b3:
1801b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  //
1802b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   If class B is derived directly or indirectly from class A and
18031e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  //   class C is derived directly or indirectly from B,
1804b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //
18051e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // For Objective-C, we let A, B, and C also be Objective-C
18061e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  // interfaces.
18071e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
1808b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Compare based on pointer conversions.
1809b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (SCS1.Second == ICK_Pointer_Conversion &&
1810b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      SCS2.Second == ICK_Pointer_Conversion &&
1811b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      /*FIXME: Remove if Objective-C id conversions get their own rank*/
1812b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      FromType1->isPointerType() && FromType2->isPointerType() &&
1813014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      ToType1->isPointerType() && ToType2->isPointerType()) {
1814b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    QualType FromPointee1
18153ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType();
1816b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    QualType ToPointee1
1817b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType();
1818b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    QualType FromPointee2
1819014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType();
1820b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    QualType ToPointee2
1821b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType();
18221e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
18231e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType();
18241e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType();
18251e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType();
1826b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType();
1827b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1828b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   -- conversion of C* to B* is better than conversion of C* to A*,
1829b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) {
1830014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (IsDerivedFrom(ToPointee1, ToPointee2))
1831b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Better;
1832b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      else if (IsDerivedFrom(ToPointee2, ToPointee1))
1833b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return ImplicitConversionSequence::Worse;
1834b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1835b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (ToIface1 && ToIface2) {
1836b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        if (Context.canAssignObjCInterfaces(ToIface2, ToIface1))
1837b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          return ImplicitConversionSequence::Better;
1838b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2))
1839b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch          return ImplicitConversionSequence::Worse;
1840014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
1841b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
1842b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1843b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   -- conversion of B* to A* is better than conversion of C* to A*,
1844b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) {
1845b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      if (IsDerivedFrom(FromPointee2, FromPointee1))
1846b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Better;
1847b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      else if (IsDerivedFrom(FromPointee1, FromPointee2))
1848b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Worse;
1849b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1850b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      if (FromIface1 && FromIface2) {
1851b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        if (Context.canAssignObjCInterfaces(FromIface1, FromIface2))
1852014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          return ImplicitConversionSequence::Better;
1853b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1))
1854b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch          return ImplicitConversionSequence::Worse;
1855b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      }
18563fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    }
1857b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
1858b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1859b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Compare based on reference bindings.
1860b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (SCS1.ReferenceBinding && SCS2.ReferenceBinding &&
1861b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      SCS1.Second == ICK_Derived_To_Base) {
1862b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   -- binding of an expression of type C to a reference of type
1863b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      B& is better than binding an expression of type C to a
1864014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //      reference of type A&,
1865b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() &&
1866b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) {
1867b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      if (IsDerivedFrom(ToType1, ToType2))
1868b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Better;
1869b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      else if (IsDerivedFrom(ToType2, ToType1))
1870b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return ImplicitConversionSequence::Worse;
1871b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1872b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1873b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   -- binding of an expression of type B to a reference of type
1874b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //      A& is better than binding an expression of type C to a
1875b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //      reference of type A&,
1876b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() &&
1877b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) {
1878014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (IsDerivedFrom(FromType2, FromType1))
1879b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return ImplicitConversionSequence::Better;
1880b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      else if (IsDerivedFrom(FromType1, FromType2))
1881b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        return ImplicitConversionSequence::Worse;
1882b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1883b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  }
1884b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1885b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1886b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // FIXME: conversion of A::* to B::* is better than conversion of
1887b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // A::* to C::*,
1888b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1889b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // FIXME: conversion of B::* to C::* is better than conversion of
1890b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // A::* to C::*, and
1891b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1892014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (SCS1.CopyConstructor && SCS2.CopyConstructor &&
1893b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      SCS1.Second == ICK_Derived_To_Base) {
18941e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    //   -- conversion of C to B is better than conversion of C to A,
1895b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() &&
1896b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) {
1897b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      if (IsDerivedFrom(ToType1, ToType2))
1898b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return ImplicitConversionSequence::Better;
1899b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      else if (IsDerivedFrom(ToType2, ToType1))
1900b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return ImplicitConversionSequence::Worse;
1901b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
1902b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1903b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    //   -- conversion of B to A is better than conversion of C to A.
1904b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() &&
1905b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) {
1906b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      if (IsDerivedFrom(FromType2, FromType1))
1907b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return ImplicitConversionSequence::Better;
1908014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      else if (IsDerivedFrom(FromType1, FromType2))
1909b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return ImplicitConversionSequence::Worse;
1910b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    }
1911b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  }
1912b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1913b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  return ImplicitConversionSequence::Indistinguishable;
1914b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
1915b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1916b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// TryCopyInitialization - Try to copy-initialize a value of type
1917b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// ToType from the expression From. Return the implicit conversion
1918b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// sequence required to pass this argument, which may be a bad
19193fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// conversion sequence (meaning that the argument cannot be passed to
1920b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// a parameter of this type). If @p SuppressUserConversions, then we
1921b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// do not permit any user-defined conversion sequences. If @p ForceRValue,
1922b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// then we treat @p From as an rvalue, even if it is an lvalue.
1923b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochImplicitConversionSequence
1924b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochSema::TryCopyInitialization(Expr *From, QualType ToType,
1925014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                            bool SuppressUserConversions, bool ForceRValue) {
1926b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (ToType->isReferenceType()) {
1927b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ImplicitConversionSequence ICS;
1928b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions,
1929b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                       /*AllowExplicit=*/false, ForceRValue);
1930b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ICS;
1931b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } else {
1932b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return TryImplicitConversion(From, ToType, SuppressUserConversions,
1933b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                 ForceRValue);
1934b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
1935b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
1936b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1937b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// PerformCopyInitialization - Copy-initialize an object of type @p ToType with
1938b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// the expression @p From. Returns true (and emits a diagnostic) if there was
1939014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// an error, returns false if the initialization succeeded. Elidable should
1940b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// be true when the copy may be elided (C++ 12.8p15). Overload resolution works
1941b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// differently in C++0x for this case.
1942b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochbool Sema::PerformCopyInitialization(Expr *&From, QualType ToType,
1943b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                     const char* Flavor, bool Elidable) {
1944b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (!getLangOptions().CPlusPlus) {
1945b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // In C, argument passing is the same as performing an assignment.
1946b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    QualType FromType = From->getType();
1947b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1948b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    AssignConvertType ConvTy =
1949b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      CheckSingleAssignmentConstraints(ToType, From);
1950b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (ConvTy != Compatible &&
1951b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible)
1952b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      ConvTy = Compatible;
1953b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1954b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType,
1955b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                    FromType, From, Flavor);
1956014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
1957b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1958b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (ToType->isReferenceType())
1959b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    return CheckReferenceInit(From, ToType);
1960b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1961b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (!PerformImplicitConversion(From, ToType, Flavor,
1962b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                 /*AllowExplicit=*/false, Elidable))
1963b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
1964b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1965b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Diag(From->getSourceRange().getBegin(),
1966b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch              diag::err_typecheck_convert_incompatible)
1967b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    << ToType << From->getType() << Flavor << From->getSourceRange();
1968b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
1969014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1970b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// TryObjectArgumentInitialization - Try to initialize the object
19713ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// parameter of the given member function (@c Method) from the
1972b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// expression @p From.
19733ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochImplicitConversionSequence
1974b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben MurdochSema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) {
1975b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType ClassType = Context.getTypeDeclType(Method->getParent());
1976b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  unsigned MethodQuals = Method->getTypeQualifiers();
1977b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals);
1978b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1979b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Set up the conversion sequence as a "bad" conversion, to allow us
19807d3e7fc4b65010eabe860313ee0c64f50843f6e3Ben Murdoch  // to exit early.
1981b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ImplicitConversionSequence ICS;
1982b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ICS.Standard.setAsIdentityConversion();
1983b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
1984014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
1985b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // We need to have an object of class type.
1986b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  QualType FromType = From->getType();
1987b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (const PointerType *PT = FromType->getAs<PointerType>())
1988b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    FromType = PT->getPointeeType();
1989b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
1990b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  assert(FromType->isRecordType());
1991b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1992b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // The implicit object parmeter is has the type "reference to cv X",
1993b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // where X is the class of which the function is a member
1994b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // (C++ [over.match.funcs]p4). However, when finding an implicit
1995b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // conversion sequence for the argument, we are not allowed to
1996b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // create temporaries or perform user-defined conversions
1997b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // (C++ [over.match.funcs]p5). We perform a simplified version of
1998b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // reference binding here, that allows class rvalues to bind to
1999b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // non-constant references.
2000b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2001b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // First check the qualifiers. We don't care about lvalue-vs-rvalue
2002014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // with the implicit object parameter (C++ [over.match.funcs]p5).
2003b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType FromTypeCanon = Context.getCanonicalType(FromType);
2004b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() &&
2005b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      !ImplicitParamType.isAtLeastAsQualifiedAs(FromType))
2006b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ICS;
2007b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2008b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // Check that we have either the same type or a derived type. It
2009b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // affects the conversion rank.
2010e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  QualType ClassTypeCanon = Context.getCanonicalType(ClassType);
2011b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType())
2012b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    ICS.Standard.Second = ICK_Identity;
2013b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  else if (IsDerivedFrom(FromType, ClassType))
2014b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    ICS.Standard.Second = ICK_Derived_To_Base;
2015b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  else
2016b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return ICS;
2017b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2018b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Success. Mark this as a reference binding.
2019b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
2020b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr();
2021b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr();
2022014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ICS.Standard.ReferenceBinding = true;
2023b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ICS.Standard.DirectBinding = true;
2024b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ICS.Standard.RRefBinding = false;
2025b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return ICS;
2026014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
2027b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2028014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// PerformObjectArgumentInitialization - Perform initialization of
2029014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// the implicit object parameter for the given Method with the given
20301e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// expression.
20311e0659c275bb392c045087af4f6b0d7565cb3d77Steve Blockbool
20321e0659c275bb392c045087af4f6b0d7565cb3d77Steve BlockSema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) {
2033014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  QualType FromRecordType, DestType;
2034014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  QualType ImplicitParamRecordType  =
20351e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    Method->getThisType(Context)->getAs<PointerType>()->getPointeeType();
2036b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
20371e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (const PointerType *PT = From->getType()->getAs<PointerType>()) {
20381e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    FromRecordType = PT->getPointeeType();
20391e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    DestType = Method->getThisType(Context);
2040014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  } else {
2041014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    FromRecordType = From->getType();
2042b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    DestType = ImplicitParamRecordType;
2043b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
2044b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2045b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ImplicitConversionSequence ICS
2046014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    = TryObjectArgumentInitialization(From, Method);
20471e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion)
2048014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return Diag(From->getSourceRange().getBegin(),
2049b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                diag::err_implicit_object_parameter_init)
2050b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch       << ImplicitParamRecordType << FromRecordType << From->getSourceRange();
2051b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2052014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (ICS.Standard.Second == ICK_Derived_To_Base &&
2053b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      CheckDerivedToBaseConversion(FromRecordType,
2054014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                   ImplicitParamRecordType,
2055014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                   From->getSourceRange().getBegin(),
2056b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                   From->getSourceRange()))
2057b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    return true;
2058b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
2059014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase,
2060b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                    /*isLvalue=*/true);
2061b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return false;
2062b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
2063b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
20643fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// TryContextuallyConvertToBool - Attempt to contextually convert the
2065b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// expression From to bool (C++0x [conv]p3).
2066e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) {
2067e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  return TryImplicitConversion(From, Context.BoolTy, false, true);
2068014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
2069589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch
20703fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// PerformContextuallyConvertToBool - Perform a contextual conversion
207144f0eee88ff00398ff7f715fab053374d808c90dSteve Block/// of the expression From to bool (C++0x [conv]p3).
2072b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochbool Sema::PerformContextuallyConvertToBool(Expr *&From) {
2073b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From);
2074b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting"))
2075b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return false;
2076014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
2077b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return Diag(From->getSourceRange().getBegin(),
2078b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch              diag::err_typecheck_bool_condition)
2079b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    << From->getType() << From->getSourceRange();
2080b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
2081b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2082014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// AddOverloadCandidate - Adds the given function to the set of
2083b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// candidate functions, using the given function call arguments.  If
2084014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// @p SuppressUserConversions, then don't allow user-defined
2085014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// conversions via constructors or conversion operators.
20861e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// If @p ForceRValue, treat all arguments as rvalues. This is a slightly
20871e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// hacky way to implement the overloading rules for elidable copy
20881e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// initialization in C++0x (C++0x 12.8p15).
20891e0659c275bb392c045087af4f6b0d7565cb3d77Steve Blockvoid
2090014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochSema::AddOverloadCandidate(FunctionDecl *Function,
2091014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                           Expr **Args, unsigned NumArgs,
20921e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block                           OverloadCandidateSet& CandidateSet,
2093b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                           bool SuppressUserConversions,
20941e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block                           bool ForceRValue)
20951e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block{
20961e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  const FunctionProtoType* Proto
20971e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block    = dyn_cast<FunctionProtoType>(Function->getType()->getAsFunctionType());
2098014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  assert(Proto && "Functions without a prototype cannot be overloaded");
2099014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  assert(!isa<CXXConversionDecl>(Function) &&
2100b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch         "Use AddConversionCandidate for conversion functions");
2101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assert(!Function->getDescribedFunctionTemplate() &&
2102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch         "Use AddTemplateOverloadCandidate for function templates");
2103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2104014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) {
2105b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (!isa<CXXConstructorDecl>(Method)) {
2106014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // If we get here, it's because we're calling a member function
21073ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // that is named without a member access expression (e.g.,
21083ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // "this->f") that was either written explicitly or created
21093ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // implicitly. This can happen with a qualified call to a member
2110014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // function, e.g., X::f(). We use a NULL object as the implied
21113ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      // object argument (C++ [over.call.func]p3).
21123ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet,
2113b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                         SuppressUserConversions, ForceRValue);
21143ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      return;
2115b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    }
21163ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // We treat a constructor like a non-member function, since its object
2117b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // argument doesn't participate in overload resolution.
21183ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  }
2119b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
21203ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
21213ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // Add this candidate
2122b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CandidateSet.push_back(OverloadCandidate());
2123b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  OverloadCandidate& Candidate = CandidateSet.back();
2124b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Function = Function;
2125b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Viable = true;
2126b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.IsSurrogate = false;
21273ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Candidate.IgnoreObjectArgument = false;
21283ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
21293ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  unsigned NumArgsInProto = Proto->getNumArgs();
21303ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
2131014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // (C++ 13.3.2p2): A candidate function having fewer than m
2132b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // parameters is viable only if it has an ellipsis in its parameter
2133b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // list (8.3.5).
2134b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (NumArgs > NumArgsInProto && !Proto->isVariadic()) {
2135b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Candidate.Viable = false;
2136b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return;
2137b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
2138b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2139b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // (C++ 13.3.2p2): A candidate function having more than m parameters
2140b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // is viable only if the (m+1)st parameter has a default argument
2141b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // (8.3.6). For the purposes of overload resolution, the
2142014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // parameter list is truncated on the right, so that there are
2143b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // exactly m parameters.
2144b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  unsigned MinRequiredArgs = Function->getMinRequiredArguments();
2145b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (NumArgs < MinRequiredArgs) {
2146b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Not enough arguments.
2147b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Candidate.Viable = false;
2148b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return;
21493ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  }
21503ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
2151b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Determine the implicit conversion sequences for each of the
2152b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // arguments.
2153b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Conversions.resize(NumArgs);
2154b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
21558b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch    if (ArgIdx < NumArgsInProto) {
21568b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch      // (C++ 13.3.2p3): for F to be a viable function, there shall
21578b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch      // exist for each argument an implicit conversion sequence
2158014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // (13.3.3.1) that converts that argument to the corresponding
2159014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // parameter of F.
2160014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      QualType ParamType = Proto->getArgType(ArgIdx);
2161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      Candidate.Conversions[ArgIdx]
2162014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        = TryCopyInitialization(Args[ArgIdx], ParamType,
2163014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                SuppressUserConversions, ForceRValue);
2164014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (Candidate.Conversions[ArgIdx].ConversionKind
2165014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            == ImplicitConversionSequence::BadConversion) {
2166014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        Candidate.Viable = false;
2167014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        break;
2168014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
2169014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    } else {
2170014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // (C++ 13.3.2p2): For the purposes of overload resolution, any
2171014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // argument for which there is no corresponding parameter is
2172014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // considered to ""match the ellipsis" (C+ 13.3.3.1.3).
2173014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      Candidate.Conversions[ArgIdx].ConversionKind
2174014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        = ImplicitConversionSequence::EllipsisConversion;
2175014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
2176014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
2177014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
2178014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
2179014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// \brief Add all of the function declarations in the given function set to
2180014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// the overload canddiate set.
2181257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochvoid Sema::AddFunctionCandidates(const FunctionSet &Functions,
21823fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch                                 Expr **Args, unsigned NumArgs,
21833fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch                                 OverloadCandidateSet& CandidateSet,
21843fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch                                 bool SuppressUserConversions) {
21853fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  for (FunctionSet::const_iterator F = Functions.begin(),
2186257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                FEnd = Functions.end();
2187257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch       F != FEnd; ++F) {
21883fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F))
21893fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch      AddOverloadCandidate(FD, Args, NumArgs, CandidateSet,
21903fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch                           SuppressUserConversions);
2191b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    else
2192b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*F),
2193b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   /*FIXME: explicit args */false, 0, 0,
2194257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                   Args, NumArgs, CandidateSet,
2195257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                   SuppressUserConversions);
2196257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  }
2197014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
21981e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block
21993fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// AddMethodCandidate - Adds the given C++ member function to the set
22003fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// of candidate functions, using the given function call arguments
22013fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// and the object argument (@c Object). For example, in a call
22023fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain
22031e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// both @c a1 and @c a2. If @p SuppressUserConversions, then don't
22041e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block/// allow user-defined conversions via constructors or conversion
22053fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// operators. If @p ForceRValue, treat all arguments as rvalues. This is
22063fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// a slightly hacky way to implement the overloading rules for elidable copy
22073fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch/// initialization in C++0x (C++0x 12.8p15).
2208b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochvoid
2209b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochSema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object,
2210b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                         Expr **Args, unsigned NumArgs,
22111e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block                         OverloadCandidateSet& CandidateSet,
22121e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block                         bool SuppressUserConversions, bool ForceRValue)
22131e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block{
2214014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const FunctionProtoType* Proto
221544f0eee88ff00398ff7f715fab053374d808c90dSteve Block    = dyn_cast<FunctionProtoType>(Method->getType()->getAsFunctionType());
22163fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  assert(Proto && "Methods without a prototype cannot be overloaded");
22173fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  assert(!isa<CXXConversionDecl>(Method) &&
22183fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch         "Use AddConversionCandidate for conversion functions");
221944f0eee88ff00398ff7f715fab053374d808c90dSteve Block  assert(!isa<CXXConstructorDecl>(Method) &&
222044f0eee88ff00398ff7f715fab053374d808c90dSteve Block         "Use AddOverloadCandidate for constructors");
22213fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch
22223fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  // Add this candidate
22231e0659c275bb392c045087af4f6b0d7565cb3d77Steve Block  CandidateSet.push_back(OverloadCandidate());
2224b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  OverloadCandidate& Candidate = CandidateSet.back();
2225b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Function = Method;
2226b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.IsSurrogate = false;
2227b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.IgnoreObjectArgument = false;
2228b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2229014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  unsigned NumArgsInProto = Proto->getNumArgs();
2230b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2231b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // (C++ 13.3.2p2): A candidate function having fewer than m
2232b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // parameters is viable only if it has an ellipsis in its parameter
2233b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // list (8.3.5).
2234b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (NumArgs > NumArgsInProto && !Proto->isVariadic()) {
22353ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    Candidate.Viable = false;
22363ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    return;
2237b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
2238b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2239b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // (C++ 13.3.2p2): A candidate function having more than m parameters
2240b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // is viable only if the (m+1)st parameter has a default argument
2241b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // (8.3.6). For the purposes of overload resolution, the
2242014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // parameter list is truncated on the right, so that there are
2243014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // exactly m parameters.
2244014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  unsigned MinRequiredArgs = Method->getMinRequiredArguments();
2245014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (NumArgs < MinRequiredArgs) {
2246014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Not enough arguments.
2247014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Candidate.Viable = false;
2248014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return;
2249014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
2250014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
2251014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Candidate.Viable = true;
2252014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Candidate.Conversions.resize(NumArgs + 1);
2253014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
2254014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (Method->isStatic() || !Object)
2255014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // The implicit object argument is ignored.
2256014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Candidate.IgnoreObjectArgument = true;
2257014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  else {
2258014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Determine the implicit conversion sequence for the object
2259014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // parameter.
2260b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method);
2261b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    if (Candidate.Conversions[0].ConversionKind
2262b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch          == ImplicitConversionSequence::BadConversion) {
2263b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      Candidate.Viable = false;
2264b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      return;
2265b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
2266b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
2267b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2268b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Determine the implicit conversion sequences for each of the
2269b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // arguments.
2270b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
2271b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    if (ArgIdx < NumArgsInProto) {
2272b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // (C++ 13.3.2p3): for F to be a viable function, there shall
2273b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // exist for each argument an implicit conversion sequence
2274014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // (13.3.3.1) that converts that argument to the corresponding
2275b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // parameter of F.
2276b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      QualType ParamType = Proto->getArgType(ArgIdx);
2277b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      Candidate.Conversions[ArgIdx + 1]
2278b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        = TryCopyInitialization(Args[ArgIdx], ParamType,
2279b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                SuppressUserConversions, ForceRValue);
2280b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (Candidate.Conversions[ArgIdx + 1].ConversionKind
2281b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            == ImplicitConversionSequence::BadConversion) {
2282b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        Candidate.Viable = false;
2283b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        break;
2284b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      }
2285b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    } else {
2286b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // (C++ 13.3.2p2): For the purposes of overload resolution, any
2287014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // argument for which there is no corresponding parameter is
2288b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      // considered to ""match the ellipsis" (C+ 13.3.3.1.3).
228944f0eee88ff00398ff7f715fab053374d808c90dSteve Block      Candidate.Conversions[ArgIdx + 1].ConversionKind
2290b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch        = ImplicitConversionSequence::EllipsisConversion;
2291b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    }
2292b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
2293b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
2294b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
229544f0eee88ff00398ff7f715fab053374d808c90dSteve Block/// \brief Add a C++ member function template as a candidate to the candidate
229644f0eee88ff00398ff7f715fab053374d808c90dSteve Block/// set, using template argument deduction to produce an appropriate member
2297b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// function template specialization.
229844f0eee88ff00398ff7f715fab053374d808c90dSteve Blockvoid
2299014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochSema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl,
2300257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 bool HasExplicitTemplateArgs,
2301257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 const TemplateArgument *ExplicitTemplateArgs,
2302257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 unsigned NumExplicitTemplateArgs,
2303257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 Expr *Object, Expr **Args, unsigned NumArgs,
2304257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 OverloadCandidateSet& CandidateSet,
2305257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 bool SuppressUserConversions,
2306257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                 bool ForceRValue) {
2307257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // C++ [over.match.funcs]p7:
2308257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   In each case where a candidate is a function template, candidate
2309257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   function template specializations are generated using template argument
2310257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   deduction (14.8.3, 14.8.2). Those candidates are then handled as
2311014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   candidate functions in the usual way.113) A given name can refer to one
2312257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   or more function templates and also to a set of overloaded non-template
2313257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   functions. In such a case, the candidate functions generated from each
2314257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   function template are combined with the set of non-template candidate
2315257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  //   functions.
2316257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  TemplateDeductionInfo Info(Context);
2317257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  FunctionDecl *Specialization = 0;
2318257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  if (TemplateDeductionResult Result
2319257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs,
2320257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                ExplicitTemplateArgs, NumExplicitTemplateArgs,
2321257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                                Args, NumArgs, Specialization, Info)) {
2322257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        // FIXME: Record what happened with template argument deduction, so
2323014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        // that we can give the user a beautiful diagnostic.
2324257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch        (void)Result;
2325b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        return;
2326257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      }
2327b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2328257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // Add the function template specialization produced by template argument
2329257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  // deduction as a candidate.
2330257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  assert(Specialization && "Missing member function template specialization?");
2331b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assert(isa<CXXMethodDecl>(Specialization) &&
2332257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch         "Specialization is not a member function?");
2333257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs,
2334257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch                     CandidateSet, SuppressUserConversions, ForceRValue);
2335257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch}
2336257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
2337014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch/// \brief Add a C++ function template specialization as a candidate
233844f0eee88ff00398ff7f715fab053374d808c90dSteve Block/// in the candidate set, using template argument deduction to produce
233944f0eee88ff00398ff7f715fab053374d808c90dSteve Block/// an appropriate function template specialization.
234044f0eee88ff00398ff7f715fab053374d808c90dSteve Blockvoid
2341b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochSema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate,
2342b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                   bool HasExplicitTemplateArgs,
2343b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                 const TemplateArgument *ExplicitTemplateArgs,
23443ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                   unsigned NumExplicitTemplateArgs,
2345b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   Expr **Args, unsigned NumArgs,
2346b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                   OverloadCandidateSet& CandidateSet,
23473ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                   bool SuppressUserConversions,
23483ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                   bool ForceRValue) {
23493ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // C++ [over.match.funcs]p7:
2350014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   In each case where a candidate is a function template, candidate
23513ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  //   function template specializations are generated using template argument
2352b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   deduction (14.8.3, 14.8.2). Those candidates are then handled as
2353b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   candidate functions in the usual way.113) A given name can refer to one
23543ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  //   or more function templates and also to a set of overloaded non-template
23553ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  //   functions. In such a case, the candidate functions generated from each
2356b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //   function template are combined with the set of non-template candidate
23573ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  //   functions.
2358b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TemplateDeductionInfo Info(Context);
2359b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  FunctionDecl *Specialization = 0;
23603ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  if (TemplateDeductionResult Result
23613ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs,
23623ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                  ExplicitTemplateArgs, NumExplicitTemplateArgs,
2363014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                  Args, NumArgs, Specialization, Info)) {
2364592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch    // FIXME: Record what happened with template argument deduction, so
2365b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // that we can give the user a beautiful diagnostic.
2366592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch    (void)Result;
2367b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    return;
2368b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
2369592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch
2370592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch  // Add the function template specialization produced by template argument
2371592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch  // deduction as a candidate.
2372b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assert(Specialization && "Missing function template specialization?");
2373b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet,
2374592a9fc1d8ea420377a2e7efd0600e20b058be2bBen Murdoch                       SuppressUserConversions, ForceRValue);
2375b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
2376b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2377b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// AddConversionCandidate - Add a C++ conversion function as a
2378b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// candidate in the candidate set (C++ [over.match.conv],
2379537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// C++ [over.match.copy]). From is the expression we're converting from,
2380537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// and ToType is the type that we're eventually trying to convert to
2381537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// (which may or may not be the same type as the type that the
2382537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch/// conversion function produces).
2383537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdochvoid
2384537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben MurdochSema::AddConversionCandidate(CXXConversionDecl *Conversion,
2385537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch                             Expr *From, QualType ToType,
2386537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch                             OverloadCandidateSet& CandidateSet) {
2387537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch  assert(!Conversion->getDescribedFunctionTemplate() &&
2388537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch         "Conversion function templates use AddTemplateConversionCandidate");
2389537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch
2390537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch  // Add this candidate
2391537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch  CandidateSet.push_back(OverloadCandidate());
2392b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  OverloadCandidate& Candidate = CandidateSet.back();
2393014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Candidate.Function = Conversion;
2394b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.IsSurrogate = false;
23953fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  Candidate.IgnoreObjectArgument = false;
23963fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  Candidate.FinalConversion.setAsIdentityConversion();
23973fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  Candidate.FinalConversion.FromTypePtr
2398b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    = Conversion->getConversionType().getAsOpaquePtr();
2399b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr();
2400b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2401b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Determine the implicit conversion sequence for the implicit
2402b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // object parameter.
2403b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.Viable = true;
2404b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.Conversions.resize(1);
2405b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion);
2406b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2407014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (Candidate.Conversions[0].ConversionKind
2408b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      == ImplicitConversionSequence::BadConversion) {
2409b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    Candidate.Viable = false;
2410b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    return;
2411b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  }
2412b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2413b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // To determine what the conversion from the result of calling the
2414b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // conversion function to the type we're eventually trying to
2415b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // convert to (ToType), we need to synthesize a call to the
24163fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  // conversion function and attempt copy initialization from it. This
2417b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // makes sure that we get the right semantics with respect to
2418b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // lvalues/rvalues and the type. Fortunately, we can allocate this
2419b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // call on the stack and we don't need its arguments to be
2420014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // well-formed.
2421b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  DeclRefExpr ConversionRef(Conversion, Conversion->getType(),
2422b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                            SourceLocation());
2423b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()),
2424014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                CastExpr::CK_Unknown,
2425b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                &ConversionRef, false);
2426014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
2427b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Note that it is safe to allocate CallExpr on the stack here because
2428b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // there are 0 arguments (i.e., nothing is allocated using ASTContext's
2429b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // allocator).
2430b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  CallExpr Call(Context, &ConversionFn, 0, 0,
2431014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                Conversion->getConversionType().getNonReferenceType(),
2432b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                SourceLocation());
24333fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  ImplicitConversionSequence ICS =
24343fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    TryCopyInitialization(&Call, ToType,
24353fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch                          /*SuppressUserConversions=*/true,
24363fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch                          /*ForceRValue=*/false);
24373fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch
24383fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  switch (ICS.ConversionKind) {
2439b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  case ImplicitConversionSequence::StandardConversion:
24403fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    Candidate.FinalConversion = ICS.Standard;
24413fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    break;
24423fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch
24433fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch  case ImplicitConversionSequence::BadConversion:
24443fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    Candidate.Viable = false;
24453fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch    break;
2446b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2447b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  default:
2448b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    assert(false &&
2449014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch           "Can only end up with a standard conversion sequence or failure");
24503ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  }
24513ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch}
24523ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
24533ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// \brief Adds a conversion function template specialization
24543ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// candidate to the overload set, using template argument deduction
24553ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// to deduce the template arguments of the conversion function
24563ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// template from the type that we are converting to (C++
24573ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// [temp.deduct.conv]).
24583ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvoid
24593ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochSema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate,
24603ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                     Expr *From, QualType ToType,
24613ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                     OverloadCandidateSet &CandidateSet) {
24623ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) &&
2463014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch         "Only conversion function templates permitted here");
24643ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
24653ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  TemplateDeductionInfo Info(Context);
24663ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CXXConversionDecl *Specialization = 0;
24673ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  if (TemplateDeductionResult Result
24683ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        = DeduceTemplateArguments(FunctionTemplate, ToType,
24693ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                  Specialization, Info)) {
24703ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // FIXME: Record what happened with template argument deduction, so
24713ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    // that we can give the user a beautiful diagnostic.
24723ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    (void)Result;
24733ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    return;
24743ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  }
24753ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
24763ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // Add the conversion function template specialization produced by
24773ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // template argument deduction as a candidate.
24783ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  assert(Specialization && "Missing function template specialization?");
2479014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  AddConversionCandidate(Specialization, From, ToType, CandidateSet);
24803ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch}
24813ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
24823ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// AddSurrogateCandidate - Adds a "surrogate" candidate function that
24833ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// converts the given @c Object to a function pointer via the
24843ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// conversion function @c Conversion, and then attempts to call it
24853ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// with the given arguments (C++ [over.call.object]p2-4). Proto is
24863ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch/// the type of function that we'll eventually be calling.
24873ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvoid Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion,
24883ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                 const FunctionProtoType *Proto,
24893ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                 Expr *Object, Expr **Args, unsigned NumArgs,
24903ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                                 OverloadCandidateSet& CandidateSet) {
24913ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CandidateSet.push_back(OverloadCandidate());
24923ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  OverloadCandidate& Candidate = CandidateSet.back();
2493014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Candidate.Function = 0;
24943ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Candidate.Surrogate = Conversion;
24953ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Candidate.Viable = true;
24963ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Candidate.IsSurrogate = true;
24973ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Candidate.IgnoreObjectArgument = false;
24983ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Candidate.Conversions.resize(NumArgs + 1);
24993ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
25003ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // Determine the implicit conversion sequence for the implicit
25013ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // object parameter.
25023ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  ImplicitConversionSequence ObjectInit
25033ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    = TryObjectArgumentInitialization(Object, Conversion);
25043ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) {
25053ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    Candidate.Viable = false;
25063ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    return;
2507b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  }
2508014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
2509b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // The first conversion is actually a user-defined conversion whose
2510b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // first conversion is ObjectInit's standard conversion (which is
2511b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // effectively a reference binding). Record it as such.
2512b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Conversions[0].ConversionKind
2513b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    = ImplicitConversionSequence::UserDefinedConversion;
2514b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard;
2515b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion;
2516b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Conversions[0].UserDefined.After
2517b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    = Candidate.Conversions[0].UserDefined.Before;
2518b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion();
2519b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2520b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Find the
2521b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  unsigned NumArgsInProto = Proto->getNumArgs();
2522b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2523b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // (C++ 13.3.2p2): A candidate function having fewer than m
2524014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // parameters is viable only if it has an ellipsis in its parameter
2525b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // list (8.3.5).
2526e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (NumArgs > NumArgsInProto && !Proto->isVariadic()) {
2527b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Candidate.Viable = false;
2528b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return;
2529b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
2530b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2531b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Function types don't have any default arguments, so just check if
2532b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // we have enough arguments.
2533b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (NumArgs < NumArgsInProto) {
2534b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    // Not enough arguments.
2535b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    Candidate.Viable = false;
2536b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    return;
2537b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
2538b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2539b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Determine the implicit conversion sequences for each of the
2540b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // arguments.
2541b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
2542b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if (ArgIdx < NumArgsInProto) {
2543b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // (C++ 13.3.2p3): for F to be a viable function, there shall
2544b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // exist for each argument an implicit conversion sequence
2545b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // (13.3.3.1) that converts that argument to the corresponding
254621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      // parameter of F.
254721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      QualType ParamType = Proto->getArgType(ArgIdx);
2548b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      Candidate.Conversions[ArgIdx + 1]
2549b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        = TryCopyInitialization(Args[ArgIdx], ParamType,
2550b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                /*SuppressUserConversions=*/false,
2551b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                /*ForceRValue=*/false);
2552b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if (Candidate.Conversions[ArgIdx + 1].ConversionKind
2553b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch            == ImplicitConversionSequence::BadConversion) {
2554b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        Candidate.Viable = false;
2555b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        break;
2556b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      }
2557b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    } else {
2558b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // (C++ 13.3.2p2): For the purposes of overload resolution, any
2559b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // argument for which there is no corresponding parameter is
2560b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      // considered to ""match the ellipsis" (C+ 13.3.3.1.3).
2561b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      Candidate.Conversions[ArgIdx + 1].ConversionKind
2562b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        = ImplicitConversionSequence::EllipsisConversion;
2563b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
2564b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
2565b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
2566b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2567b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// FIXME: This will eventually be removed, once we've migrated all of the
2568b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch// operator overloading logic over to the scheme used by binary operators, which
2569b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch// works for template instantiation.
2570b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochvoid Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S,
2571b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                 SourceLocation OpLoc,
2572b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                 Expr **Args, unsigned NumArgs,
2573b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                 OverloadCandidateSet& CandidateSet,
2574b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                 SourceRange OpRange) {
2575b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2576b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  FunctionSet Functions;
2577b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2578b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType T1 = Args[0]->getType();
2579b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType T2;
2580b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (NumArgs > 1)
2581b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    T2 = Args[1]->getType();
2582b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
2583b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
2584b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (S)
2585b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    LookupOverloadedOperatorName(Op, S, T1, T2, Functions);
2586b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  ArgumentDependentLookup(OpName, Args, NumArgs, Functions);
2587b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet);
2588b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange);
2589b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet);
2590b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch}
2591b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
2592b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// \brief Add overload candidates for overloaded operators that are
2593b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// member functions.
2594b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch///
2595b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// Add the overloaded operator candidates that are member functions
2596b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// for the operator Op that was used in an operator expression such
2597b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// as "x Op y". , Args/NumArgs provides the operator arguments, and
2598b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// CandidateSet will store the added overload candidates. (C++
2599b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch/// [over.match.oper]).
2600b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochvoid Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op,
2601b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                       SourceLocation OpLoc,
2602b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                       Expr **Args, unsigned NumArgs,
2603b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                       OverloadCandidateSet& CandidateSet,
2604b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                                       SourceRange OpRange) {
2605b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
2606b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
2607b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  // C++ [over.match.oper]p3:
2608014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   For a unary operator @ with an operand of a type whose
2609b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //   cv-unqualified version is T1, and for a binary operator @ with
2610b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //   a left operand of a type whose cv-unqualified version is T1 and
2611b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //   a right operand of a type whose cv-unqualified version is T2,
2612b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //   three sets of candidate functions, designated member
2613b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  //   candidates, non-member candidates and built-in candidates, are
2614b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  //   constructed as follows:
2615b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType T1 = Args[0]->getType();
2616b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  QualType T2;
2617b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (NumArgs > 1)
2618b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    T2 = Args[1]->getType();
2619b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
2620b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //     -- If T1 is a class type, the set of member candidates is the
2621b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //        result of the qualified lookup of T1::operator@
2622b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //        (13.3.1.1.1); otherwise, the set of member candidates is
2623b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  //        empty.
2624b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // FIXME: Lookup in base classes, too!
2625b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  if (const RecordType *T1Rec = T1->getAs<RecordType>()) {
2626b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    DeclContext::lookup_const_iterator Oper, OperEnd;
262769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch    for (llvm::tie(Oper, OperEnd) = T1Rec->getDecl()->lookup(OpName);
262869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch         Oper != OperEnd; ++Oper)
2629b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0],
263069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                         Args+1, NumArgs - 1, CandidateSet,
263169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                         /*SuppressUserConversions=*/false);
2632b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
2633b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch}
2634b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2635b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// AddBuiltinCandidate - Add a candidate for a built-in
2636b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// operator. ResultTy and ParamTys are the result and parameter types
2637b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/// of the built-in candidate, respectively. Args and NumArgs are the
263869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch/// arguments being passed to the candidate. IsAssignmentOperator
263969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch/// should be true when this built-in candidate is an assignment
264069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch/// operator. NumContextualBoolArguments is the number of arguments
2641b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// (at the beginning of the argument list) that will be contextually
2642b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch/// converted to bool.
2643b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdochvoid Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys,
2644b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                               Expr **Args, unsigned NumArgs,
2645b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                               OverloadCandidateSet& CandidateSet,
2646b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                               bool IsAssignmentOperator,
2647b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                               unsigned NumContextualBoolArguments) {
2648b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Add this candidate
2649b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  CandidateSet.push_back(OverloadCandidate());
2650b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  OverloadCandidate& Candidate = CandidateSet.back();
2651b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.Function = 0;
2652b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.IsSurrogate = false;
2653b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.IgnoreObjectArgument = false;
2654b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Candidate.BuiltinTypes.ResultTy = ResultTy;
2655b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
2656b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx];
2657b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
2658b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Determine the implicit conversion sequences for each of the
2659b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // arguments.
2660b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.Viable = true;
2661b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  Candidate.Conversions.resize(NumArgs);
2662b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
2663b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // C++ [over.match.oper]p4:
2664b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   For the built-in assignment operators, conversions of the
2665b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //   left operand are restricted as follows:
2666b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //     -- no temporaries are introduced to hold the left operand, and
2667b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //     -- no user-defined conversions are applied to the left
2668b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    //        operand to achieve a type match with the left-most
2669014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //        parameter of a built-in candidate.
2670014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    //
2671b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    // We block these conversions by turning off user-defined
2672014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // conversions, since that is the only way that initialization of
2673    // a reference to a non-class type can occur from something that
2674    // is not of the same type.
2675    if (ArgIdx < NumContextualBoolArguments) {
2676      assert(ParamTys[ArgIdx] == Context.BoolTy &&
2677             "Contextual conversion to bool requires bool type");
2678      Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]);
2679    } else {
2680      Candidate.Conversions[ArgIdx]
2681        = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx],
2682                                ArgIdx == 0 && IsAssignmentOperator,
2683                                /*ForceRValue=*/false);
2684    }
2685    if (Candidate.Conversions[ArgIdx].ConversionKind
2686        == ImplicitConversionSequence::BadConversion) {
2687      Candidate.Viable = false;
2688      break;
2689    }
2690  }
2691}
2692
2693/// BuiltinCandidateTypeSet - A set of types that will be used for the
2694/// candidate operator functions for built-in operators (C++
2695/// [over.built]). The types are separated into pointer types and
2696/// enumeration types.
2697class BuiltinCandidateTypeSet  {
2698  /// TypeSet - A set of types.
2699  typedef llvm::SmallPtrSet<QualType, 8> TypeSet;
2700
2701  /// PointerTypes - The set of pointer types that will be used in the
2702  /// built-in candidates.
2703  TypeSet PointerTypes;
2704
2705  /// MemberPointerTypes - The set of member pointer types that will be
2706  /// used in the built-in candidates.
2707  TypeSet MemberPointerTypes;
2708
2709  /// EnumerationTypes - The set of enumeration types that will be
2710  /// used in the built-in candidates.
2711  TypeSet EnumerationTypes;
2712
2713  /// Sema - The semantic analysis instance where we are building the
2714  /// candidate type set.
2715  Sema &SemaRef;
2716
2717  /// Context - The AST context in which we will build the type sets.
2718  ASTContext &Context;
2719
2720  bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty);
2721  bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty);
2722
2723public:
2724  /// iterator - Iterates through the types that are part of the set.
2725  typedef TypeSet::iterator iterator;
2726
2727  BuiltinCandidateTypeSet(Sema &SemaRef)
2728    : SemaRef(SemaRef), Context(SemaRef.Context) { }
2729
2730  void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions,
2731                             bool AllowExplicitConversions);
2732
2733  /// pointer_begin - First pointer type found;
2734  iterator pointer_begin() { return PointerTypes.begin(); }
2735
2736  /// pointer_end - Past the last pointer type found;
2737  iterator pointer_end() { return PointerTypes.end(); }
2738
2739  /// member_pointer_begin - First member pointer type found;
2740  iterator member_pointer_begin() { return MemberPointerTypes.begin(); }
2741
2742  /// member_pointer_end - Past the last member pointer type found;
2743  iterator member_pointer_end() { return MemberPointerTypes.end(); }
2744
2745  /// enumeration_begin - First enumeration type found;
2746  iterator enumeration_begin() { return EnumerationTypes.begin(); }
2747
2748  /// enumeration_end - Past the last enumeration type found;
2749  iterator enumeration_end() { return EnumerationTypes.end(); }
2750};
2751
2752/// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to
2753/// the set of pointer types along with any more-qualified variants of
2754/// that type. For example, if @p Ty is "int const *", this routine
2755/// will add "int const *", "int const volatile *", "int const
2756/// restrict *", and "int const volatile restrict *" to the set of
2757/// pointer types. Returns true if the add of @p Ty itself succeeded,
2758/// false otherwise.
2759bool
2760BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) {
2761  // Insert this type.
2762  if (!PointerTypes.insert(Ty))
2763    return false;
2764
2765  if (const PointerType *PointerTy = Ty->getAs<PointerType>()) {
2766    QualType PointeeTy = PointerTy->getPointeeType();
2767    // FIXME: Optimize this so that we don't keep trying to add the same types.
2768
2769    // FIXME: Do we have to add CVR qualifiers at *all* levels to deal with all
2770    // pointer conversions that don't cast away constness?
2771    if (!PointeeTy.isConstQualified())
2772      AddPointerWithMoreQualifiedTypeVariants
2773        (Context.getPointerType(PointeeTy.withConst()));
2774    if (!PointeeTy.isVolatileQualified())
2775      AddPointerWithMoreQualifiedTypeVariants
2776        (Context.getPointerType(PointeeTy.withVolatile()));
2777    if (!PointeeTy.isRestrictQualified())
2778      AddPointerWithMoreQualifiedTypeVariants
2779        (Context.getPointerType(PointeeTy.withRestrict()));
2780  }
2781
2782  return true;
2783}
2784
2785/// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty
2786/// to the set of pointer types along with any more-qualified variants of
2787/// that type. For example, if @p Ty is "int const *", this routine
2788/// will add "int const *", "int const volatile *", "int const
2789/// restrict *", and "int const volatile restrict *" to the set of
2790/// pointer types. Returns true if the add of @p Ty itself succeeded,
2791/// false otherwise.
2792bool
2793BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants(
2794    QualType Ty) {
2795  // Insert this type.
2796  if (!MemberPointerTypes.insert(Ty))
2797    return false;
2798
2799  if (const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>()) {
2800    QualType PointeeTy = PointerTy->getPointeeType();
2801    const Type *ClassTy = PointerTy->getClass();
2802    // FIXME: Optimize this so that we don't keep trying to add the same types.
2803
2804    if (!PointeeTy.isConstQualified())
2805      AddMemberPointerWithMoreQualifiedTypeVariants
2806        (Context.getMemberPointerType(PointeeTy.withConst(), ClassTy));
2807    if (!PointeeTy.isVolatileQualified())
2808      AddMemberPointerWithMoreQualifiedTypeVariants
2809        (Context.getMemberPointerType(PointeeTy.withVolatile(), ClassTy));
2810    if (!PointeeTy.isRestrictQualified())
2811      AddMemberPointerWithMoreQualifiedTypeVariants
2812        (Context.getMemberPointerType(PointeeTy.withRestrict(), ClassTy));
2813  }
2814
2815  return true;
2816}
2817
2818/// AddTypesConvertedFrom - Add each of the types to which the type @p
2819/// Ty can be implicit converted to the given set of @p Types. We're
2820/// primarily interested in pointer types and enumeration types. We also
2821/// take member pointer types, for the conditional operator.
2822/// AllowUserConversions is true if we should look at the conversion
2823/// functions of a class type, and AllowExplicitConversions if we
2824/// should also include the explicit conversion functions of a class
2825/// type.
2826void
2827BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty,
2828                                               bool AllowUserConversions,
2829                                               bool AllowExplicitConversions) {
2830  // Only deal with canonical types.
2831  Ty = Context.getCanonicalType(Ty);
2832
2833  // Look through reference types; they aren't part of the type of an
2834  // expression for the purposes of conversions.
2835  if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>())
2836    Ty = RefTy->getPointeeType();
2837
2838  // We don't care about qualifiers on the type.
2839  Ty = Ty.getUnqualifiedType();
2840
2841  if (const PointerType *PointerTy = Ty->getAs<PointerType>()) {
2842    QualType PointeeTy = PointerTy->getPointeeType();
2843
2844    // Insert our type, and its more-qualified variants, into the set
2845    // of types.
2846    if (!AddPointerWithMoreQualifiedTypeVariants(Ty))
2847      return;
2848
2849    // Add 'cv void*' to our set of types.
2850    if (!Ty->isVoidType()) {
2851      QualType QualVoid
2852        = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers());
2853      AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid));
2854    }
2855
2856    // If this is a pointer to a class type, add pointers to its bases
2857    // (with the same level of cv-qualification as the original
2858    // derived class, of course).
2859    if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) {
2860      CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl());
2861      for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin();
2862           Base != ClassDecl->bases_end(); ++Base) {
2863        QualType BaseTy = Context.getCanonicalType(Base->getType());
2864        BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers());
2865
2866        // Add the pointer type, recursively, so that we get all of
2867        // the indirect base classes, too.
2868        AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false);
2869      }
2870    }
2871  } else if (Ty->isMemberPointerType()) {
2872    // Member pointers are far easier, since the pointee can't be converted.
2873    if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty))
2874      return;
2875  } else if (Ty->isEnumeralType()) {
2876    EnumerationTypes.insert(Ty);
2877  } else if (AllowUserConversions) {
2878    if (const RecordType *TyRec = Ty->getAs<RecordType>()) {
2879      if (SemaRef.RequireCompleteType(SourceLocation(), Ty, 0)) {
2880        // No conversion functions in incomplete types.
2881        return;
2882      }
2883
2884      CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl());
2885      // FIXME: Visit conversion functions in the base classes, too.
2886      OverloadedFunctionDecl *Conversions
2887        = ClassDecl->getConversionFunctions();
2888      for (OverloadedFunctionDecl::function_iterator Func
2889             = Conversions->function_begin();
2890           Func != Conversions->function_end(); ++Func) {
2891        CXXConversionDecl *Conv;
2892        FunctionTemplateDecl *ConvTemplate;
2893        GetFunctionAndTemplate(*Func, Conv, ConvTemplate);
2894
2895        // Skip conversion function templates; they don't tell us anything
2896        // about which builtin types we can convert to.
2897        if (ConvTemplate)
2898          continue;
2899
2900        if (AllowExplicitConversions || !Conv->isExplicit())
2901          AddTypesConvertedFrom(Conv->getConversionType(), false, false);
2902      }
2903    }
2904  }
2905}
2906
2907/// \brief Helper function for AddBuiltinOperatorCandidates() that adds
2908/// the volatile- and non-volatile-qualified assignment operators for the
2909/// given type to the candidate set.
2910static void AddBuiltinAssignmentOperatorCandidates(Sema &S,
2911                                                   QualType T,
2912                                                   Expr **Args,
2913                                                   unsigned NumArgs,
2914                                    OverloadCandidateSet &CandidateSet) {
2915  QualType ParamTypes[2];
2916
2917  // T& operator=(T&, T)
2918  ParamTypes[0] = S.Context.getLValueReferenceType(T);
2919  ParamTypes[1] = T;
2920  S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet,
2921                        /*IsAssignmentOperator=*/true);
2922
2923  if (!S.Context.getCanonicalType(T).isVolatileQualified()) {
2924    // volatile T& operator=(volatile T&, T)
2925    ParamTypes[0] = S.Context.getLValueReferenceType(T.withVolatile());
2926    ParamTypes[1] = T;
2927    S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet,
2928                          /*IsAssignmentOperator=*/true);
2929  }
2930}
2931
2932/// AddBuiltinOperatorCandidates - Add the appropriate built-in
2933/// operator overloads to the candidate set (C++ [over.built]), based
2934/// on the operator @p Op and the arguments given. For example, if the
2935/// operator is a binary '+', this routine might add "int
2936/// operator+(int, int)" to cover integer addition.
2937void
2938Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op,
2939                                   Expr **Args, unsigned NumArgs,
2940                                   OverloadCandidateSet& CandidateSet) {
2941  // The set of "promoted arithmetic types", which are the arithmetic
2942  // types are that preserved by promotion (C++ [over.built]p2). Note
2943  // that the first few of these types are the promoted integral
2944  // types; these types need to be first.
2945  // FIXME: What about complex?
2946  const unsigned FirstIntegralType = 0;
2947  const unsigned LastIntegralType = 13;
2948  const unsigned FirstPromotedIntegralType = 7,
2949                 LastPromotedIntegralType = 13;
2950  const unsigned FirstPromotedArithmeticType = 7,
2951                 LastPromotedArithmeticType = 16;
2952  const unsigned NumArithmeticTypes = 16;
2953  QualType ArithmeticTypes[NumArithmeticTypes] = {
2954    Context.BoolTy, Context.CharTy, Context.WCharTy,
2955// FIXME:   Context.Char16Ty, Context.Char32Ty,
2956    Context.SignedCharTy, Context.ShortTy,
2957    Context.UnsignedCharTy, Context.UnsignedShortTy,
2958    Context.IntTy, Context.LongTy, Context.LongLongTy,
2959    Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy,
2960    Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy
2961  };
2962
2963  // Find all of the types that the arguments can convert to, but only
2964  // if the operator we're looking at has built-in operator candidates
2965  // that make use of these types.
2966  BuiltinCandidateTypeSet CandidateTypes(*this);
2967  if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual ||
2968      Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual ||
2969      Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal ||
2970      Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript ||
2971      Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus ||
2972      (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) {
2973    for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
2974      CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(),
2975                                           true,
2976                                           (Op == OO_Exclaim ||
2977                                            Op == OO_AmpAmp ||
2978                                            Op == OO_PipePipe));
2979  }
2980
2981  bool isComparison = false;
2982  switch (Op) {
2983  case OO_None:
2984  case NUM_OVERLOADED_OPERATORS:
2985    assert(false && "Expected an overloaded operator");
2986    break;
2987
2988  case OO_Star: // '*' is either unary or binary
2989    if (NumArgs == 1)
2990      goto UnaryStar;
2991    else
2992      goto BinaryStar;
2993    break;
2994
2995  case OO_Plus: // '+' is either unary or binary
2996    if (NumArgs == 1)
2997      goto UnaryPlus;
2998    else
2999      goto BinaryPlus;
3000    break;
3001
3002  case OO_Minus: // '-' is either unary or binary
3003    if (NumArgs == 1)
3004      goto UnaryMinus;
3005    else
3006      goto BinaryMinus;
3007    break;
3008
3009  case OO_Amp: // '&' is either unary or binary
3010    if (NumArgs == 1)
3011      goto UnaryAmp;
3012    else
3013      goto BinaryAmp;
3014
3015  case OO_PlusPlus:
3016  case OO_MinusMinus:
3017    // C++ [over.built]p3:
3018    //
3019    //   For every pair (T, VQ), where T is an arithmetic type, and VQ
3020    //   is either volatile or empty, there exist candidate operator
3021    //   functions of the form
3022    //
3023    //       VQ T&      operator++(VQ T&);
3024    //       T          operator++(VQ T&, int);
3025    //
3026    // C++ [over.built]p4:
3027    //
3028    //   For every pair (T, VQ), where T is an arithmetic type other
3029    //   than bool, and VQ is either volatile or empty, there exist
3030    //   candidate operator functions of the form
3031    //
3032    //       VQ T&      operator--(VQ T&);
3033    //       T          operator--(VQ T&, int);
3034    for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1);
3035         Arith < NumArithmeticTypes; ++Arith) {
3036      QualType ArithTy = ArithmeticTypes[Arith];
3037      QualType ParamTypes[2]
3038        = { Context.getLValueReferenceType(ArithTy), Context.IntTy };
3039
3040      // Non-volatile version.
3041      if (NumArgs == 1)
3042        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
3043      else
3044        AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet);
3045
3046      // Volatile version
3047      ParamTypes[0] = Context.getLValueReferenceType(ArithTy.withVolatile());
3048      if (NumArgs == 1)
3049        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
3050      else
3051        AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet);
3052    }
3053
3054    // C++ [over.built]p5:
3055    //
3056    //   For every pair (T, VQ), where T is a cv-qualified or
3057    //   cv-unqualified object type, and VQ is either volatile or
3058    //   empty, there exist candidate operator functions of the form
3059    //
3060    //       T*VQ&      operator++(T*VQ&);
3061    //       T*VQ&      operator--(T*VQ&);
3062    //       T*         operator++(T*VQ&, int);
3063    //       T*         operator--(T*VQ&, int);
3064    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
3065         Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3066      // Skip pointer types that aren't pointers to object types.
3067      if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType())
3068        continue;
3069
3070      QualType ParamTypes[2] = {
3071        Context.getLValueReferenceType(*Ptr), Context.IntTy
3072      };
3073
3074      // Without volatile
3075      if (NumArgs == 1)
3076        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
3077      else
3078        AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
3079
3080      if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) {
3081        // With volatile
3082        ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile());
3083        if (NumArgs == 1)
3084          AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
3085        else
3086          AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
3087      }
3088    }
3089    break;
3090
3091  UnaryStar:
3092    // C++ [over.built]p6:
3093    //   For every cv-qualified or cv-unqualified object type T, there
3094    //   exist candidate operator functions of the form
3095    //
3096    //       T&         operator*(T*);
3097    //
3098    // C++ [over.built]p7:
3099    //   For every function type T, there exist candidate operator
3100    //   functions of the form
3101    //       T&         operator*(T*);
3102    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
3103         Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3104      QualType ParamTy = *Ptr;
3105      QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType();
3106      AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy),
3107                          &ParamTy, Args, 1, CandidateSet);
3108    }
3109    break;
3110
3111  UnaryPlus:
3112    // C++ [over.built]p8:
3113    //   For every type T, there exist candidate operator functions of
3114    //   the form
3115    //
3116    //       T*         operator+(T*);
3117    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
3118         Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3119      QualType ParamTy = *Ptr;
3120      AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet);
3121    }
3122
3123    // Fall through
3124
3125  UnaryMinus:
3126    // C++ [over.built]p9:
3127    //  For every promoted arithmetic type T, there exist candidate
3128    //  operator functions of the form
3129    //
3130    //       T         operator+(T);
3131    //       T         operator-(T);
3132    for (unsigned Arith = FirstPromotedArithmeticType;
3133         Arith < LastPromotedArithmeticType; ++Arith) {
3134      QualType ArithTy = ArithmeticTypes[Arith];
3135      AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet);
3136    }
3137    break;
3138
3139  case OO_Tilde:
3140    // C++ [over.built]p10:
3141    //   For every promoted integral type T, there exist candidate
3142    //   operator functions of the form
3143    //
3144    //        T         operator~(T);
3145    for (unsigned Int = FirstPromotedIntegralType;
3146         Int < LastPromotedIntegralType; ++Int) {
3147      QualType IntTy = ArithmeticTypes[Int];
3148      AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet);
3149    }
3150    break;
3151
3152  case OO_New:
3153  case OO_Delete:
3154  case OO_Array_New:
3155  case OO_Array_Delete:
3156  case OO_Call:
3157    assert(false && "Special operators don't use AddBuiltinOperatorCandidates");
3158    break;
3159
3160  case OO_Comma:
3161  UnaryAmp:
3162  case OO_Arrow:
3163    // C++ [over.match.oper]p3:
3164    //   -- For the operator ',', the unary operator '&', or the
3165    //      operator '->', the built-in candidates set is empty.
3166    break;
3167
3168  case OO_EqualEqual:
3169  case OO_ExclaimEqual:
3170    // C++ [over.match.oper]p16:
3171    //   For every pointer to member type T, there exist candidate operator
3172    //   functions of the form
3173    //
3174    //        bool operator==(T,T);
3175    //        bool operator!=(T,T);
3176    for (BuiltinCandidateTypeSet::iterator
3177           MemPtr = CandidateTypes.member_pointer_begin(),
3178           MemPtrEnd = CandidateTypes.member_pointer_end();
3179         MemPtr != MemPtrEnd;
3180         ++MemPtr) {
3181      QualType ParamTypes[2] = { *MemPtr, *MemPtr };
3182      AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet);
3183    }
3184
3185    // Fall through
3186
3187  case OO_Less:
3188  case OO_Greater:
3189  case OO_LessEqual:
3190  case OO_GreaterEqual:
3191    // C++ [over.built]p15:
3192    //
3193    //   For every pointer or enumeration type T, there exist
3194    //   candidate operator functions of the form
3195    //
3196    //        bool       operator<(T, T);
3197    //        bool       operator>(T, T);
3198    //        bool       operator<=(T, T);
3199    //        bool       operator>=(T, T);
3200    //        bool       operator==(T, T);
3201    //        bool       operator!=(T, T);
3202    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
3203         Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3204      QualType ParamTypes[2] = { *Ptr, *Ptr };
3205      AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet);
3206    }
3207    for (BuiltinCandidateTypeSet::iterator Enum
3208           = CandidateTypes.enumeration_begin();
3209         Enum != CandidateTypes.enumeration_end(); ++Enum) {
3210      QualType ParamTypes[2] = { *Enum, *Enum };
3211      AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet);
3212    }
3213
3214    // Fall through.
3215    isComparison = true;
3216
3217  BinaryPlus:
3218  BinaryMinus:
3219    if (!isComparison) {
3220      // We didn't fall through, so we must have OO_Plus or OO_Minus.
3221
3222      // C++ [over.built]p13:
3223      //
3224      //   For every cv-qualified or cv-unqualified object type T
3225      //   there exist candidate operator functions of the form
3226      //
3227      //      T*         operator+(T*, ptrdiff_t);
3228      //      T&         operator[](T*, ptrdiff_t);    [BELOW]
3229      //      T*         operator-(T*, ptrdiff_t);
3230      //      T*         operator+(ptrdiff_t, T*);
3231      //      T&         operator[](ptrdiff_t, T*);    [BELOW]
3232      //
3233      // C++ [over.built]p14:
3234      //
3235      //   For every T, where T is a pointer to object type, there
3236      //   exist candidate operator functions of the form
3237      //
3238      //      ptrdiff_t  operator-(T, T);
3239      for (BuiltinCandidateTypeSet::iterator Ptr
3240             = CandidateTypes.pointer_begin();
3241           Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3242        QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() };
3243
3244        // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t)
3245        AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
3246
3247        if (Op == OO_Plus) {
3248          // T* operator+(ptrdiff_t, T*);
3249          ParamTypes[0] = ParamTypes[1];
3250          ParamTypes[1] = *Ptr;
3251          AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
3252        } else {
3253          // ptrdiff_t operator-(T, T);
3254          ParamTypes[1] = *Ptr;
3255          AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes,
3256                              Args, 2, CandidateSet);
3257        }
3258      }
3259    }
3260    // Fall through
3261
3262  case OO_Slash:
3263  BinaryStar:
3264  Conditional:
3265    // C++ [over.built]p12:
3266    //
3267    //   For every pair of promoted arithmetic types L and R, there
3268    //   exist candidate operator functions of the form
3269    //
3270    //        LR         operator*(L, R);
3271    //        LR         operator/(L, R);
3272    //        LR         operator+(L, R);
3273    //        LR         operator-(L, R);
3274    //        bool       operator<(L, R);
3275    //        bool       operator>(L, R);
3276    //        bool       operator<=(L, R);
3277    //        bool       operator>=(L, R);
3278    //        bool       operator==(L, R);
3279    //        bool       operator!=(L, R);
3280    //
3281    //   where LR is the result of the usual arithmetic conversions
3282    //   between types L and R.
3283    //
3284    // C++ [over.built]p24:
3285    //
3286    //   For every pair of promoted arithmetic types L and R, there exist
3287    //   candidate operator functions of the form
3288    //
3289    //        LR       operator?(bool, L, R);
3290    //
3291    //   where LR is the result of the usual arithmetic conversions
3292    //   between types L and R.
3293    // Our candidates ignore the first parameter.
3294    for (unsigned Left = FirstPromotedArithmeticType;
3295         Left < LastPromotedArithmeticType; ++Left) {
3296      for (unsigned Right = FirstPromotedArithmeticType;
3297           Right < LastPromotedArithmeticType; ++Right) {
3298        QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] };
3299        QualType Result
3300          = isComparison
3301          ? Context.BoolTy
3302          : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]);
3303        AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet);
3304      }
3305    }
3306    break;
3307
3308  case OO_Percent:
3309  BinaryAmp:
3310  case OO_Caret:
3311  case OO_Pipe:
3312  case OO_LessLess:
3313  case OO_GreaterGreater:
3314    // C++ [over.built]p17:
3315    //
3316    //   For every pair of promoted integral types L and R, there
3317    //   exist candidate operator functions of the form
3318    //
3319    //      LR         operator%(L, R);
3320    //      LR         operator&(L, R);
3321    //      LR         operator^(L, R);
3322    //      LR         operator|(L, R);
3323    //      L          operator<<(L, R);
3324    //      L          operator>>(L, R);
3325    //
3326    //   where LR is the result of the usual arithmetic conversions
3327    //   between types L and R.
3328    for (unsigned Left = FirstPromotedIntegralType;
3329         Left < LastPromotedIntegralType; ++Left) {
3330      for (unsigned Right = FirstPromotedIntegralType;
3331           Right < LastPromotedIntegralType; ++Right) {
3332        QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] };
3333        QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater)
3334            ? LandR[0]
3335            : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]);
3336        AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet);
3337      }
3338    }
3339    break;
3340
3341  case OO_Equal:
3342    // C++ [over.built]p20:
3343    //
3344    //   For every pair (T, VQ), where T is an enumeration or
3345    //   pointer to member type and VQ is either volatile or
3346    //   empty, there exist candidate operator functions of the form
3347    //
3348    //        VQ T&      operator=(VQ T&, T);
3349    for (BuiltinCandidateTypeSet::iterator
3350           Enum = CandidateTypes.enumeration_begin(),
3351           EnumEnd = CandidateTypes.enumeration_end();
3352         Enum != EnumEnd; ++Enum)
3353      AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2,
3354                                             CandidateSet);
3355    for (BuiltinCandidateTypeSet::iterator
3356           MemPtr = CandidateTypes.member_pointer_begin(),
3357         MemPtrEnd = CandidateTypes.member_pointer_end();
3358         MemPtr != MemPtrEnd; ++MemPtr)
3359      AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2,
3360                                             CandidateSet);
3361      // Fall through.
3362
3363  case OO_PlusEqual:
3364  case OO_MinusEqual:
3365    // C++ [over.built]p19:
3366    //
3367    //   For every pair (T, VQ), where T is any type and VQ is either
3368    //   volatile or empty, there exist candidate operator functions
3369    //   of the form
3370    //
3371    //        T*VQ&      operator=(T*VQ&, T*);
3372    //
3373    // C++ [over.built]p21:
3374    //
3375    //   For every pair (T, VQ), where T is a cv-qualified or
3376    //   cv-unqualified object type and VQ is either volatile or
3377    //   empty, there exist candidate operator functions of the form
3378    //
3379    //        T*VQ&      operator+=(T*VQ&, ptrdiff_t);
3380    //        T*VQ&      operator-=(T*VQ&, ptrdiff_t);
3381    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
3382         Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3383      QualType ParamTypes[2];
3384      ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType();
3385
3386      // non-volatile version
3387      ParamTypes[0] = Context.getLValueReferenceType(*Ptr);
3388      AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet,
3389                          /*IsAssigmentOperator=*/Op == OO_Equal);
3390
3391      if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) {
3392        // volatile version
3393        ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile());
3394        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet,
3395                            /*IsAssigmentOperator=*/Op == OO_Equal);
3396      }
3397    }
3398    // Fall through.
3399
3400  case OO_StarEqual:
3401  case OO_SlashEqual:
3402    // C++ [over.built]p18:
3403    //
3404    //   For every triple (L, VQ, R), where L is an arithmetic type,
3405    //   VQ is either volatile or empty, and R is a promoted
3406    //   arithmetic type, there exist candidate operator functions of
3407    //   the form
3408    //
3409    //        VQ L&      operator=(VQ L&, R);
3410    //        VQ L&      operator*=(VQ L&, R);
3411    //        VQ L&      operator/=(VQ L&, R);
3412    //        VQ L&      operator+=(VQ L&, R);
3413    //        VQ L&      operator-=(VQ L&, R);
3414    for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) {
3415      for (unsigned Right = FirstPromotedArithmeticType;
3416           Right < LastPromotedArithmeticType; ++Right) {
3417        QualType ParamTypes[2];
3418        ParamTypes[1] = ArithmeticTypes[Right];
3419
3420        // Add this built-in operator as a candidate (VQ is empty).
3421        ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]);
3422        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet,
3423                            /*IsAssigmentOperator=*/Op == OO_Equal);
3424
3425        // Add this built-in operator as a candidate (VQ is 'volatile').
3426        ParamTypes[0] = ArithmeticTypes[Left].withVolatile();
3427        ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]);
3428        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet,
3429                            /*IsAssigmentOperator=*/Op == OO_Equal);
3430      }
3431    }
3432    break;
3433
3434  case OO_PercentEqual:
3435  case OO_LessLessEqual:
3436  case OO_GreaterGreaterEqual:
3437  case OO_AmpEqual:
3438  case OO_CaretEqual:
3439  case OO_PipeEqual:
3440    // C++ [over.built]p22:
3441    //
3442    //   For every triple (L, VQ, R), where L is an integral type, VQ
3443    //   is either volatile or empty, and R is a promoted integral
3444    //   type, there exist candidate operator functions of the form
3445    //
3446    //        VQ L&       operator%=(VQ L&, R);
3447    //        VQ L&       operator<<=(VQ L&, R);
3448    //        VQ L&       operator>>=(VQ L&, R);
3449    //        VQ L&       operator&=(VQ L&, R);
3450    //        VQ L&       operator^=(VQ L&, R);
3451    //        VQ L&       operator|=(VQ L&, R);
3452    for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) {
3453      for (unsigned Right = FirstPromotedIntegralType;
3454           Right < LastPromotedIntegralType; ++Right) {
3455        QualType ParamTypes[2];
3456        ParamTypes[1] = ArithmeticTypes[Right];
3457
3458        // Add this built-in operator as a candidate (VQ is empty).
3459        ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]);
3460        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
3461
3462        // Add this built-in operator as a candidate (VQ is 'volatile').
3463        ParamTypes[0] = ArithmeticTypes[Left];
3464        ParamTypes[0].addVolatile();
3465        ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]);
3466        AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
3467      }
3468    }
3469    break;
3470
3471  case OO_Exclaim: {
3472    // C++ [over.operator]p23:
3473    //
3474    //   There also exist candidate operator functions of the form
3475    //
3476    //        bool        operator!(bool);
3477    //        bool        operator&&(bool, bool);     [BELOW]
3478    //        bool        operator||(bool, bool);     [BELOW]
3479    QualType ParamTy = Context.BoolTy;
3480    AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet,
3481                        /*IsAssignmentOperator=*/false,
3482                        /*NumContextualBoolArguments=*/1);
3483    break;
3484  }
3485
3486  case OO_AmpAmp:
3487  case OO_PipePipe: {
3488    // C++ [over.operator]p23:
3489    //
3490    //   There also exist candidate operator functions of the form
3491    //
3492    //        bool        operator!(bool);            [ABOVE]
3493    //        bool        operator&&(bool, bool);
3494    //        bool        operator||(bool, bool);
3495    QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy };
3496    AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet,
3497                        /*IsAssignmentOperator=*/false,
3498                        /*NumContextualBoolArguments=*/2);
3499    break;
3500  }
3501
3502  case OO_Subscript:
3503    // C++ [over.built]p13:
3504    //
3505    //   For every cv-qualified or cv-unqualified object type T there
3506    //   exist candidate operator functions of the form
3507    //
3508    //        T*         operator+(T*, ptrdiff_t);     [ABOVE]
3509    //        T&         operator[](T*, ptrdiff_t);
3510    //        T*         operator-(T*, ptrdiff_t);     [ABOVE]
3511    //        T*         operator+(ptrdiff_t, T*);     [ABOVE]
3512    //        T&         operator[](ptrdiff_t, T*);
3513    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
3514         Ptr != CandidateTypes.pointer_end(); ++Ptr) {
3515      QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() };
3516      QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType();
3517      QualType ResultTy = Context.getLValueReferenceType(PointeeType);
3518
3519      // T& operator[](T*, ptrdiff_t)
3520      AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet);
3521
3522      // T& operator[](ptrdiff_t, T*);
3523      ParamTypes[0] = ParamTypes[1];
3524      ParamTypes[1] = *Ptr;
3525      AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet);
3526    }
3527    break;
3528
3529  case OO_ArrowStar:
3530    // FIXME: No support for pointer-to-members yet.
3531    break;
3532
3533  case OO_Conditional:
3534    // Note that we don't consider the first argument, since it has been
3535    // contextually converted to bool long ago. The candidates below are
3536    // therefore added as binary.
3537    //
3538    // C++ [over.built]p24:
3539    //   For every type T, where T is a pointer or pointer-to-member type,
3540    //   there exist candidate operator functions of the form
3541    //
3542    //        T        operator?(bool, T, T);
3543    //
3544    for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(),
3545         E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) {
3546      QualType ParamTypes[2] = { *Ptr, *Ptr };
3547      AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
3548    }
3549    for (BuiltinCandidateTypeSet::iterator Ptr =
3550           CandidateTypes.member_pointer_begin(),
3551         E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) {
3552      QualType ParamTypes[2] = { *Ptr, *Ptr };
3553      AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
3554    }
3555    goto Conditional;
3556  }
3557}
3558
3559/// \brief Add function candidates found via argument-dependent lookup
3560/// to the set of overloading candidates.
3561///
3562/// This routine performs argument-dependent name lookup based on the
3563/// given function name (which may also be an operator name) and adds
3564/// all of the overload candidates found by ADL to the overload
3565/// candidate set (C++ [basic.lookup.argdep]).
3566void
3567Sema::AddArgumentDependentLookupCandidates(DeclarationName Name,
3568                                           Expr **Args, unsigned NumArgs,
3569                                           OverloadCandidateSet& CandidateSet) {
3570  FunctionSet Functions;
3571
3572  // Record all of the function candidates that we've already
3573  // added to the overload set, so that we don't add those same
3574  // candidates a second time.
3575  for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3576                                   CandEnd = CandidateSet.end();
3577       Cand != CandEnd; ++Cand)
3578    if (Cand->Function) {
3579      Functions.insert(Cand->Function);
3580      if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate())
3581        Functions.insert(FunTmpl);
3582    }
3583
3584  ArgumentDependentLookup(Name, Args, NumArgs, Functions);
3585
3586  // Erase all of the candidates we already knew about.
3587  // FIXME: This is suboptimal. Is there a better way?
3588  for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3589                                   CandEnd = CandidateSet.end();
3590       Cand != CandEnd; ++Cand)
3591    if (Cand->Function) {
3592      Functions.erase(Cand->Function);
3593      if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate())
3594        Functions.erase(FunTmpl);
3595    }
3596
3597  // For each of the ADL candidates we found, add it to the overload
3598  // set.
3599  for (FunctionSet::iterator Func = Functions.begin(),
3600                          FuncEnd = Functions.end();
3601       Func != FuncEnd; ++Func) {
3602    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func))
3603      AddOverloadCandidate(FD, Args, NumArgs, CandidateSet);
3604    else
3605      AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func),
3606                                   /*FIXME: explicit args */false, 0, 0,
3607                                   Args, NumArgs, CandidateSet);
3608  }
3609}
3610
3611/// isBetterOverloadCandidate - Determines whether the first overload
3612/// candidate is a better candidate than the second (C++ 13.3.3p1).
3613bool
3614Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1,
3615                                const OverloadCandidate& Cand2)
3616{
3617  // Define viable functions to be better candidates than non-viable
3618  // functions.
3619  if (!Cand2.Viable)
3620    return Cand1.Viable;
3621  else if (!Cand1.Viable)
3622    return false;
3623
3624  // C++ [over.match.best]p1:
3625  //
3626  //   -- if F is a static member function, ICS1(F) is defined such
3627  //      that ICS1(F) is neither better nor worse than ICS1(G) for
3628  //      any function G, and, symmetrically, ICS1(G) is neither
3629  //      better nor worse than ICS1(F).
3630  unsigned StartArg = 0;
3631  if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument)
3632    StartArg = 1;
3633
3634  // C++ [over.match.best]p1:
3635  //   A viable function F1 is defined to be a better function than another
3636  //   viable function F2 if for all arguments i, ICSi(F1) is not a worse
3637  //   conversion sequence than ICSi(F2), and then...
3638  unsigned NumArgs = Cand1.Conversions.size();
3639  assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch");
3640  bool HasBetterConversion = false;
3641  for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) {
3642    switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx],
3643                                               Cand2.Conversions[ArgIdx])) {
3644    case ImplicitConversionSequence::Better:
3645      // Cand1 has a better conversion sequence.
3646      HasBetterConversion = true;
3647      break;
3648
3649    case ImplicitConversionSequence::Worse:
3650      // Cand1 can't be better than Cand2.
3651      return false;
3652
3653    case ImplicitConversionSequence::Indistinguishable:
3654      // Do nothing.
3655      break;
3656    }
3657  }
3658
3659  //    -- for some argument j, ICSj(F1) is a better conversion sequence than
3660  //       ICSj(F2), or, if not that,
3661  if (HasBetterConversion)
3662    return true;
3663
3664  //     - F1 is a non-template function and F2 is a function template
3665  //       specialization, or, if not that,
3666  if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() &&
3667      Cand2.Function && Cand2.Function->getPrimaryTemplate())
3668    return true;
3669
3670  //   -- F1 and F2 are function template specializations, and the function
3671  //      template for F1 is more specialized than the template for F2
3672  //      according to the partial ordering rules described in 14.5.5.2, or,
3673  //      if not that,
3674  if (Cand1.Function && Cand1.Function->getPrimaryTemplate() &&
3675      Cand2.Function && Cand2.Function->getPrimaryTemplate())
3676    if (FunctionTemplateDecl *BetterTemplate
3677          = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(),
3678                                       Cand2.Function->getPrimaryTemplate(),
3679                                       true))
3680      return BetterTemplate == Cand1.Function->getPrimaryTemplate();
3681
3682  //   -- the context is an initialization by user-defined conversion
3683  //      (see 8.5, 13.3.1.5) and the standard conversion sequence
3684  //      from the return type of F1 to the destination type (i.e.,
3685  //      the type of the entity being initialized) is a better
3686  //      conversion sequence than the standard conversion sequence
3687  //      from the return type of F2 to the destination type.
3688  if (Cand1.Function && Cand2.Function &&
3689      isa<CXXConversionDecl>(Cand1.Function) &&
3690      isa<CXXConversionDecl>(Cand2.Function)) {
3691    switch (CompareStandardConversionSequences(Cand1.FinalConversion,
3692                                               Cand2.FinalConversion)) {
3693    case ImplicitConversionSequence::Better:
3694      // Cand1 has a better conversion sequence.
3695      return true;
3696
3697    case ImplicitConversionSequence::Worse:
3698      // Cand1 can't be better than Cand2.
3699      return false;
3700
3701    case ImplicitConversionSequence::Indistinguishable:
3702      // Do nothing
3703      break;
3704    }
3705  }
3706
3707  return false;
3708}
3709
3710/// \brief Computes the best viable function (C++ 13.3.3)
3711/// within an overload candidate set.
3712///
3713/// \param CandidateSet the set of candidate functions.
3714///
3715/// \param Loc the location of the function name (or operator symbol) for
3716/// which overload resolution occurs.
3717///
3718/// \param Best f overload resolution was successful or found a deleted
3719/// function, Best points to the candidate function found.
3720///
3721/// \returns The result of overload resolution.
3722Sema::OverloadingResult
3723Sema::BestViableFunction(OverloadCandidateSet& CandidateSet,
3724                         SourceLocation Loc,
3725                         OverloadCandidateSet::iterator& Best)
3726{
3727  // Find the best viable function.
3728  Best = CandidateSet.end();
3729  for (OverloadCandidateSet::iterator Cand = CandidateSet.begin();
3730       Cand != CandidateSet.end(); ++Cand) {
3731    if (Cand->Viable) {
3732      if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best))
3733        Best = Cand;
3734    }
3735  }
3736
3737  // If we didn't find any viable functions, abort.
3738  if (Best == CandidateSet.end())
3739    return OR_No_Viable_Function;
3740
3741  // Make sure that this function is better than every other viable
3742  // function. If not, we have an ambiguity.
3743  for (OverloadCandidateSet::iterator Cand = CandidateSet.begin();
3744       Cand != CandidateSet.end(); ++Cand) {
3745    if (Cand->Viable &&
3746        Cand != Best &&
3747        !isBetterOverloadCandidate(*Best, *Cand)) {
3748      Best = CandidateSet.end();
3749      return OR_Ambiguous;
3750    }
3751  }
3752
3753  // Best is the best viable function.
3754  if (Best->Function &&
3755      (Best->Function->isDeleted() ||
3756       Best->Function->getAttr<UnavailableAttr>()))
3757    return OR_Deleted;
3758
3759  // C++ [basic.def.odr]p2:
3760  //   An overloaded function is used if it is selected by overload resolution
3761  //   when referred to from a potentially-evaluated expression. [Note: this
3762  //   covers calls to named functions (5.2.2), operator overloading
3763  //   (clause 13), user-defined conversions (12.3.2), allocation function for
3764  //   placement new (5.3.4), as well as non-default initialization (8.5).
3765  if (Best->Function)
3766    MarkDeclarationReferenced(Loc, Best->Function);
3767  return OR_Success;
3768}
3769
3770/// PrintOverloadCandidates - When overload resolution fails, prints
3771/// diagnostic messages containing the candidates in the candidate
3772/// set. If OnlyViable is true, only viable candidates will be printed.
3773void
3774Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet,
3775                              bool OnlyViable)
3776{
3777  OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3778                             LastCand = CandidateSet.end();
3779  for (; Cand != LastCand; ++Cand) {
3780    if (Cand->Viable || !OnlyViable) {
3781      if (Cand->Function) {
3782        if (Cand->Function->isDeleted() ||
3783            Cand->Function->getAttr<UnavailableAttr>()) {
3784          // Deleted or "unavailable" function.
3785          Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted)
3786            << Cand->Function->isDeleted();
3787        } else {
3788          // Normal function
3789          // FIXME: Give a better reason!
3790          Diag(Cand->Function->getLocation(), diag::err_ovl_candidate);
3791        }
3792      } else if (Cand->IsSurrogate) {
3793        // Desugar the type of the surrogate down to a function type,
3794        // retaining as many typedefs as possible while still showing
3795        // the function type (and, therefore, its parameter types).
3796        QualType FnType = Cand->Surrogate->getConversionType();
3797        bool isLValueReference = false;
3798        bool isRValueReference = false;
3799        bool isPointer = false;
3800        if (const LValueReferenceType *FnTypeRef =
3801              FnType->getAs<LValueReferenceType>()) {
3802          FnType = FnTypeRef->getPointeeType();
3803          isLValueReference = true;
3804        } else if (const RValueReferenceType *FnTypeRef =
3805                     FnType->getAs<RValueReferenceType>()) {
3806          FnType = FnTypeRef->getPointeeType();
3807          isRValueReference = true;
3808        }
3809        if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) {
3810          FnType = FnTypePtr->getPointeeType();
3811          isPointer = true;
3812        }
3813        // Desugar down to a function type.
3814        FnType = QualType(FnType->getAsFunctionType(), 0);
3815        // Reconstruct the pointer/reference as appropriate.
3816        if (isPointer) FnType = Context.getPointerType(FnType);
3817        if (isRValueReference) FnType = Context.getRValueReferenceType(FnType);
3818        if (isLValueReference) FnType = Context.getLValueReferenceType(FnType);
3819
3820        Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand)
3821          << FnType;
3822      } else {
3823        // FIXME: We need to get the identifier in here
3824        // FIXME: Do we want the error message to point at the operator?
3825        // (built-ins won't have a location)
3826        QualType FnType
3827          = Context.getFunctionType(Cand->BuiltinTypes.ResultTy,
3828                                    Cand->BuiltinTypes.ParamTypes,
3829                                    Cand->Conversions.size(),
3830                                    false, 0);
3831
3832        Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType;
3833      }
3834    }
3835  }
3836}
3837
3838/// ResolveAddressOfOverloadedFunction - Try to resolve the address of
3839/// an overloaded function (C++ [over.over]), where @p From is an
3840/// expression with overloaded function type and @p ToType is the type
3841/// we're trying to resolve to. For example:
3842///
3843/// @code
3844/// int f(double);
3845/// int f(int);
3846///
3847/// int (*pfd)(double) = f; // selects f(double)
3848/// @endcode
3849///
3850/// This routine returns the resulting FunctionDecl if it could be
3851/// resolved, and NULL otherwise. When @p Complain is true, this
3852/// routine will emit diagnostics if there is an error.
3853FunctionDecl *
3854Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType,
3855                                         bool Complain) {
3856  QualType FunctionType = ToType;
3857  bool IsMember = false;
3858  if (const PointerType *ToTypePtr = ToType->getAs<PointerType>())
3859    FunctionType = ToTypePtr->getPointeeType();
3860  else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>())
3861    FunctionType = ToTypeRef->getPointeeType();
3862  else if (const MemberPointerType *MemTypePtr =
3863                    ToType->getAs<MemberPointerType>()) {
3864    FunctionType = MemTypePtr->getPointeeType();
3865    IsMember = true;
3866  }
3867
3868  // We only look at pointers or references to functions.
3869  FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType();
3870  if (!FunctionType->isFunctionType())
3871    return 0;
3872
3873  // Find the actual overloaded function declaration.
3874  OverloadedFunctionDecl *Ovl = 0;
3875
3876  // C++ [over.over]p1:
3877  //   [...] [Note: any redundant set of parentheses surrounding the
3878  //   overloaded function name is ignored (5.1). ]
3879  Expr *OvlExpr = From->IgnoreParens();
3880
3881  // C++ [over.over]p1:
3882  //   [...] The overloaded function name can be preceded by the &
3883  //   operator.
3884  if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) {
3885    if (UnOp->getOpcode() == UnaryOperator::AddrOf)
3886      OvlExpr = UnOp->getSubExpr()->IgnoreParens();
3887  }
3888
3889  // Try to dig out the overloaded function.
3890  FunctionTemplateDecl *FunctionTemplate = 0;
3891  if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) {
3892    Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl());
3893    FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl());
3894  }
3895
3896  // If there's no overloaded function declaration or function template,
3897  // we're done.
3898  if (!Ovl && !FunctionTemplate)
3899    return 0;
3900
3901  OverloadIterator Fun;
3902  if (Ovl)
3903    Fun = Ovl;
3904  else
3905    Fun = FunctionTemplate;
3906
3907  // Look through all of the overloaded functions, searching for one
3908  // whose type matches exactly.
3909  llvm::SmallPtrSet<FunctionDecl *, 4> Matches;
3910
3911  bool FoundNonTemplateFunction = false;
3912  for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) {
3913    // C++ [over.over]p3:
3914    //   Non-member functions and static member functions match
3915    //   targets of type "pointer-to-function" or "reference-to-function."
3916    //   Nonstatic member functions match targets of
3917    //   type "pointer-to-member-function."
3918    // Note that according to DR 247, the containing class does not matter.
3919
3920    if (FunctionTemplateDecl *FunctionTemplate
3921          = dyn_cast<FunctionTemplateDecl>(*Fun)) {
3922      if (CXXMethodDecl *Method
3923            = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) {
3924        // Skip non-static function templates when converting to pointer, and
3925        // static when converting to member pointer.
3926        if (Method->isStatic() == IsMember)
3927          continue;
3928      } else if (IsMember)
3929        continue;
3930
3931      // C++ [over.over]p2:
3932      //   If the name is a function template, template argument deduction is
3933      //   done (14.8.2.2), and if the argument deduction succeeds, the
3934      //   resulting template argument list is used to generate a single
3935      //   function template specialization, which is added to the set of
3936      //   overloaded functions considered.
3937      FunctionDecl *Specialization = 0;
3938      TemplateDeductionInfo Info(Context);
3939      if (TemplateDeductionResult Result
3940            = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false,
3941                                      /*FIXME:*/0, /*FIXME:*/0,
3942                                      FunctionType, Specialization, Info)) {
3943        // FIXME: make a note of the failed deduction for diagnostics.
3944        (void)Result;
3945      } else {
3946        assert(FunctionType
3947                 == Context.getCanonicalType(Specialization->getType()));
3948        Matches.insert(
3949                cast<FunctionDecl>(Specialization->getCanonicalDecl()));
3950      }
3951    }
3952
3953    if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) {
3954      // Skip non-static functions when converting to pointer, and static
3955      // when converting to member pointer.
3956      if (Method->isStatic() == IsMember)
3957        continue;
3958    } else if (IsMember)
3959      continue;
3960
3961    if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) {
3962      if (FunctionType == Context.getCanonicalType(FunDecl->getType())) {
3963        Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl()));
3964        FoundNonTemplateFunction = true;
3965      }
3966    }
3967  }
3968
3969  // If there were 0 or 1 matches, we're done.
3970  if (Matches.empty())
3971    return 0;
3972  else if (Matches.size() == 1)
3973    return *Matches.begin();
3974
3975  // C++ [over.over]p4:
3976  //   If more than one function is selected, [...]
3977  llvm::SmallVector<FunctionDecl *, 4> RemainingMatches;
3978  typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter;
3979  if (FoundNonTemplateFunction) {
3980    //   [...] any function template specializations in the set are
3981    //   eliminated if the set also contains a non-template function, [...]
3982    for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M)
3983      if ((*M)->getPrimaryTemplate() == 0)
3984        RemainingMatches.push_back(*M);
3985  } else {
3986    //   [...] and any given function template specialization F1 is
3987    //   eliminated if the set contains a second function template
3988    //   specialization whose function template is more specialized
3989    //   than the function template of F1 according to the partial
3990    //   ordering rules of 14.5.5.2.
3991
3992    // The algorithm specified above is quadratic. We instead use a
3993    // two-pass algorithm (similar to the one used to identify the
3994    // best viable function in an overload set) that identifies the
3995    // best function template (if it exists).
3996    MatchIter Best = Matches.begin();
3997    MatchIter M = Best, MEnd = Matches.end();
3998    // Find the most specialized function.
3999    for (++M; M != MEnd; ++M)
4000      if (getMoreSpecializedTemplate((*M)->getPrimaryTemplate(),
4001                                     (*Best)->getPrimaryTemplate(),
4002                                     false)
4003            == (*M)->getPrimaryTemplate())
4004        Best = M;
4005
4006    // Determine whether this function template is more specialized
4007    // that all of the others.
4008    bool Ambiguous = false;
4009    for (M = Matches.begin(); M != MEnd; ++M) {
4010      if (M != Best &&
4011          getMoreSpecializedTemplate((*M)->getPrimaryTemplate(),
4012                                     (*Best)->getPrimaryTemplate(),
4013                                     false)
4014           != (*Best)->getPrimaryTemplate()) {
4015        Ambiguous = true;
4016        break;
4017      }
4018    }
4019
4020    // If one function template was more specialized than all of the
4021    // others, return it.
4022    if (!Ambiguous)
4023      return *Best;
4024
4025    // We could not find a most-specialized function template, which
4026    // is equivalent to having a set of function templates with more
4027    // than one such template. So, we place all of the function
4028    // templates into the set of remaining matches and produce a
4029    // diagnostic below. FIXME: we could perform the quadratic
4030    // algorithm here, pruning the result set to limit the number of
4031    // candidates output later.
4032     RemainingMatches.append(Matches.begin(), Matches.end());
4033  }
4034
4035  // [...] After such eliminations, if any, there shall remain exactly one
4036  // selected function.
4037  if (RemainingMatches.size() == 1)
4038    return RemainingMatches.front();
4039
4040  // FIXME: We should probably return the same thing that BestViableFunction
4041  // returns (even if we issue the diagnostics here).
4042  Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous)
4043    << RemainingMatches[0]->getDeclName();
4044  for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I)
4045    Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate);
4046  return 0;
4047}
4048
4049/// ResolveOverloadedCallFn - Given the call expression that calls Fn
4050/// (which eventually refers to the declaration Func) and the call
4051/// arguments Args/NumArgs, attempt to resolve the function call down
4052/// to a specific function. If overload resolution succeeds, returns
4053/// the function declaration produced by overload
4054/// resolution. Otherwise, emits diagnostics, deletes all of the
4055/// arguments and Fn, and returns NULL.
4056FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee,
4057                                            DeclarationName UnqualifiedName,
4058                                            bool HasExplicitTemplateArgs,
4059                                 const TemplateArgument *ExplicitTemplateArgs,
4060                                            unsigned NumExplicitTemplateArgs,
4061                                            SourceLocation LParenLoc,
4062                                            Expr **Args, unsigned NumArgs,
4063                                            SourceLocation *CommaLocs,
4064                                            SourceLocation RParenLoc,
4065                                            bool &ArgumentDependentLookup) {
4066  OverloadCandidateSet CandidateSet;
4067
4068  // Add the functions denoted by Callee to the set of candidate
4069  // functions. While we're doing so, track whether argument-dependent
4070  // lookup still applies, per:
4071  //
4072  // C++0x [basic.lookup.argdep]p3:
4073  //   Let X be the lookup set produced by unqualified lookup (3.4.1)
4074  //   and let Y be the lookup set produced by argument dependent
4075  //   lookup (defined as follows). If X contains
4076  //
4077  //     -- a declaration of a class member, or
4078  //
4079  //     -- a block-scope function declaration that is not a
4080  //        using-declaration, or
4081  //
4082  //     -- a declaration that is neither a function or a function
4083  //        template
4084  //
4085  //   then Y is empty.
4086  if (OverloadedFunctionDecl *Ovl
4087        = dyn_cast_or_null<OverloadedFunctionDecl>(Callee)) {
4088    for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
4089                                                FuncEnd = Ovl->function_end();
4090         Func != FuncEnd; ++Func) {
4091      DeclContext *Ctx = 0;
4092      if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Func)) {
4093        if (HasExplicitTemplateArgs)
4094          continue;
4095
4096        AddOverloadCandidate(FunDecl, Args, NumArgs, CandidateSet);
4097        Ctx = FunDecl->getDeclContext();
4098      } else {
4099        FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*Func);
4100        AddTemplateOverloadCandidate(FunTmpl, HasExplicitTemplateArgs,
4101                                     ExplicitTemplateArgs,
4102                                     NumExplicitTemplateArgs,
4103                                     Args, NumArgs, CandidateSet);
4104        Ctx = FunTmpl->getDeclContext();
4105      }
4106
4107
4108      if (Ctx->isRecord() || Ctx->isFunctionOrMethod())
4109        ArgumentDependentLookup = false;
4110    }
4111  } else if (FunctionDecl *Func = dyn_cast_or_null<FunctionDecl>(Callee)) {
4112    assert(!HasExplicitTemplateArgs && "Explicit template arguments?");
4113    AddOverloadCandidate(Func, Args, NumArgs, CandidateSet);
4114
4115    if (Func->getDeclContext()->isRecord() ||
4116        Func->getDeclContext()->isFunctionOrMethod())
4117      ArgumentDependentLookup = false;
4118  } else if (FunctionTemplateDecl *FuncTemplate
4119               = dyn_cast_or_null<FunctionTemplateDecl>(Callee)) {
4120    AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs,
4121                                 ExplicitTemplateArgs,
4122                                 NumExplicitTemplateArgs,
4123                                 Args, NumArgs, CandidateSet);
4124
4125    if (FuncTemplate->getDeclContext()->isRecord())
4126      ArgumentDependentLookup = false;
4127  }
4128
4129  if (Callee)
4130    UnqualifiedName = Callee->getDeclName();
4131
4132  // FIXME: Pass explicit template arguments through for ADL
4133  if (ArgumentDependentLookup)
4134    AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs,
4135                                         CandidateSet);
4136
4137  OverloadCandidateSet::iterator Best;
4138  switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) {
4139  case OR_Success:
4140    return Best->Function;
4141
4142  case OR_No_Viable_Function:
4143    Diag(Fn->getSourceRange().getBegin(),
4144         diag::err_ovl_no_viable_function_in_call)
4145      << UnqualifiedName << Fn->getSourceRange();
4146    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
4147    break;
4148
4149  case OR_Ambiguous:
4150    Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call)
4151      << UnqualifiedName << Fn->getSourceRange();
4152    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4153    break;
4154
4155  case OR_Deleted:
4156    Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call)
4157      << Best->Function->isDeleted()
4158      << UnqualifiedName
4159      << Fn->getSourceRange();
4160    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4161    break;
4162  }
4163
4164  // Overload resolution failed. Destroy all of the subexpressions and
4165  // return NULL.
4166  Fn->Destroy(Context);
4167  for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
4168    Args[Arg]->Destroy(Context);
4169  return 0;
4170}
4171
4172/// \brief Create a unary operation that may resolve to an overloaded
4173/// operator.
4174///
4175/// \param OpLoc The location of the operator itself (e.g., '*').
4176///
4177/// \param OpcIn The UnaryOperator::Opcode that describes this
4178/// operator.
4179///
4180/// \param Functions The set of non-member functions that will be
4181/// considered by overload resolution. The caller needs to build this
4182/// set based on the context using, e.g.,
4183/// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This
4184/// set should not contain any member functions; those will be added
4185/// by CreateOverloadedUnaryOp().
4186///
4187/// \param input The input argument.
4188Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc,
4189                                                     unsigned OpcIn,
4190                                                     FunctionSet &Functions,
4191                                                     ExprArg input) {
4192  UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn);
4193  Expr *Input = (Expr *)input.get();
4194
4195  OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc);
4196  assert(Op != OO_None && "Invalid opcode for overloaded unary operator");
4197  DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
4198
4199  Expr *Args[2] = { Input, 0 };
4200  unsigned NumArgs = 1;
4201
4202  // For post-increment and post-decrement, add the implicit '0' as
4203  // the second argument, so that we know this is a post-increment or
4204  // post-decrement.
4205  if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) {
4206    llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false);
4207    Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy,
4208                                           SourceLocation());
4209    NumArgs = 2;
4210  }
4211
4212  if (Input->isTypeDependent()) {
4213    OverloadedFunctionDecl *Overloads
4214      = OverloadedFunctionDecl::Create(Context, CurContext, OpName);
4215    for (FunctionSet::iterator Func = Functions.begin(),
4216                            FuncEnd = Functions.end();
4217         Func != FuncEnd; ++Func)
4218      Overloads->addOverload(*Func);
4219
4220    DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy,
4221                                                OpLoc, false, false);
4222
4223    input.release();
4224    return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn,
4225                                                   &Args[0], NumArgs,
4226                                                   Context.DependentTy,
4227                                                   OpLoc));
4228  }
4229
4230  // Build an empty overload set.
4231  OverloadCandidateSet CandidateSet;
4232
4233  // Add the candidates from the given function set.
4234  AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false);
4235
4236  // Add operator candidates that are member functions.
4237  AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet);
4238
4239  // Add builtin operator candidates.
4240  AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet);
4241
4242  // Perform overload resolution.
4243  OverloadCandidateSet::iterator Best;
4244  switch (BestViableFunction(CandidateSet, OpLoc, Best)) {
4245  case OR_Success: {
4246    // We found a built-in operator or an overloaded operator.
4247    FunctionDecl *FnDecl = Best->Function;
4248
4249    if (FnDecl) {
4250      // We matched an overloaded operator. Build a call to that
4251      // operator.
4252
4253      // Convert the arguments.
4254      if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) {
4255        if (PerformObjectArgumentInitialization(Input, Method))
4256          return ExprError();
4257      } else {
4258        // Convert the arguments.
4259        if (PerformCopyInitialization(Input,
4260                                      FnDecl->getParamDecl(0)->getType(),
4261                                      "passing"))
4262          return ExprError();
4263      }
4264
4265      // Determine the result type
4266      QualType ResultTy
4267        = FnDecl->getType()->getAsFunctionType()->getResultType();
4268      ResultTy = ResultTy.getNonReferenceType();
4269
4270      // Build the actual expression node.
4271      Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(),
4272                                               SourceLocation());
4273      UsualUnaryConversions(FnExpr);
4274
4275      input.release();
4276
4277      Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr,
4278                                                   &Input, 1, ResultTy, OpLoc);
4279      return MaybeBindToTemporary(CE);
4280    } else {
4281      // We matched a built-in operator. Convert the arguments, then
4282      // break out so that we will build the appropriate built-in
4283      // operator node.
4284        if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0],
4285                                      Best->Conversions[0], "passing"))
4286          return ExprError();
4287
4288        break;
4289      }
4290    }
4291
4292    case OR_No_Viable_Function:
4293      // No viable function; fall through to handling this as a
4294      // built-in operator, which will produce an error message for us.
4295      break;
4296
4297    case OR_Ambiguous:
4298      Diag(OpLoc,  diag::err_ovl_ambiguous_oper)
4299          << UnaryOperator::getOpcodeStr(Opc)
4300          << Input->getSourceRange();
4301      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4302      return ExprError();
4303
4304    case OR_Deleted:
4305      Diag(OpLoc, diag::err_ovl_deleted_oper)
4306        << Best->Function->isDeleted()
4307        << UnaryOperator::getOpcodeStr(Opc)
4308        << Input->getSourceRange();
4309      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4310      return ExprError();
4311    }
4312
4313  // Either we found no viable overloaded operator or we matched a
4314  // built-in operator. In either case, fall through to trying to
4315  // build a built-in operation.
4316  input.release();
4317  return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input));
4318}
4319
4320/// \brief Create a binary operation that may resolve to an overloaded
4321/// operator.
4322///
4323/// \param OpLoc The location of the operator itself (e.g., '+').
4324///
4325/// \param OpcIn The BinaryOperator::Opcode that describes this
4326/// operator.
4327///
4328/// \param Functions The set of non-member functions that will be
4329/// considered by overload resolution. The caller needs to build this
4330/// set based on the context using, e.g.,
4331/// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This
4332/// set should not contain any member functions; those will be added
4333/// by CreateOverloadedBinOp().
4334///
4335/// \param LHS Left-hand argument.
4336/// \param RHS Right-hand argument.
4337Sema::OwningExprResult
4338Sema::CreateOverloadedBinOp(SourceLocation OpLoc,
4339                            unsigned OpcIn,
4340                            FunctionSet &Functions,
4341                            Expr *LHS, Expr *RHS) {
4342  Expr *Args[2] = { LHS, RHS };
4343  LHS=RHS=0; //Please use only Args instead of LHS/RHS couple
4344
4345  BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn);
4346  OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc);
4347  DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
4348
4349  // If either side is type-dependent, create an appropriate dependent
4350  // expression.
4351  if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) {
4352    // .* cannot be overloaded.
4353    if (Opc == BinaryOperator::PtrMemD)
4354      return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc,
4355                                                Context.DependentTy, OpLoc));
4356
4357    OverloadedFunctionDecl *Overloads
4358      = OverloadedFunctionDecl::Create(Context, CurContext, OpName);
4359    for (FunctionSet::iterator Func = Functions.begin(),
4360                            FuncEnd = Functions.end();
4361         Func != FuncEnd; ++Func)
4362      Overloads->addOverload(*Func);
4363
4364    DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy,
4365                                                OpLoc, false, false);
4366
4367    return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn,
4368                                                   Args, 2,
4369                                                   Context.DependentTy,
4370                                                   OpLoc));
4371  }
4372
4373  // If this is the .* operator, which is not overloadable, just
4374  // create a built-in binary operator.
4375  if (Opc == BinaryOperator::PtrMemD)
4376    return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]);
4377
4378  // If this is one of the assignment operators, we only perform
4379  // overload resolution if the left-hand side is a class or
4380  // enumeration type (C++ [expr.ass]p3).
4381  if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign &&
4382      !Args[0]->getType()->isOverloadableType())
4383    return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]);
4384
4385  // Build an empty overload set.
4386  OverloadCandidateSet CandidateSet;
4387
4388  // Add the candidates from the given function set.
4389  AddFunctionCandidates(Functions, Args, 2, CandidateSet, false);
4390
4391  // Add operator candidates that are member functions.
4392  AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet);
4393
4394  // Add builtin operator candidates.
4395  AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet);
4396
4397  // Perform overload resolution.
4398  OverloadCandidateSet::iterator Best;
4399  switch (BestViableFunction(CandidateSet, OpLoc, Best)) {
4400    case OR_Success: {
4401      // We found a built-in operator or an overloaded operator.
4402      FunctionDecl *FnDecl = Best->Function;
4403
4404      if (FnDecl) {
4405        // We matched an overloaded operator. Build a call to that
4406        // operator.
4407
4408        // Convert the arguments.
4409        if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) {
4410          if (PerformObjectArgumentInitialization(Args[0], Method) ||
4411              PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(),
4412                                        "passing"))
4413            return ExprError();
4414        } else {
4415          // Convert the arguments.
4416          if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(),
4417                                        "passing") ||
4418              PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(),
4419                                        "passing"))
4420            return ExprError();
4421        }
4422
4423        // Determine the result type
4424        QualType ResultTy
4425          = FnDecl->getType()->getAsFunctionType()->getResultType();
4426        ResultTy = ResultTy.getNonReferenceType();
4427
4428        // Build the actual expression node.
4429        Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(),
4430                                                 OpLoc);
4431        UsualUnaryConversions(FnExpr);
4432
4433        Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr,
4434                                                     Args, 2, ResultTy, OpLoc);
4435        return MaybeBindToTemporary(CE);
4436      } else {
4437        // We matched a built-in operator. Convert the arguments, then
4438        // break out so that we will build the appropriate built-in
4439        // operator node.
4440        if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0],
4441                                      Best->Conversions[0], "passing") ||
4442            PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1],
4443                                      Best->Conversions[1], "passing"))
4444          return ExprError();
4445
4446        break;
4447      }
4448    }
4449
4450    case OR_No_Viable_Function:
4451      // For class as left operand for assignment or compound assigment operator
4452      // do not fall through to handling in built-in, but report that no overloaded
4453      // assignment operator found
4454      if (Args[0]->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) {
4455        Diag(OpLoc,  diag::err_ovl_no_viable_oper)
4456             << BinaryOperator::getOpcodeStr(Opc)
4457             << Args[0]->getSourceRange() << Args[1]->getSourceRange();
4458        return ExprError();
4459      }
4460      // No viable function; fall through to handling this as a
4461      // built-in operator, which will produce an error message for us.
4462      break;
4463
4464    case OR_Ambiguous:
4465      Diag(OpLoc,  diag::err_ovl_ambiguous_oper)
4466          << BinaryOperator::getOpcodeStr(Opc)
4467          << Args[0]->getSourceRange() << Args[1]->getSourceRange();
4468      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4469      return ExprError();
4470
4471    case OR_Deleted:
4472      Diag(OpLoc, diag::err_ovl_deleted_oper)
4473        << Best->Function->isDeleted()
4474        << BinaryOperator::getOpcodeStr(Opc)
4475        << Args[0]->getSourceRange() << Args[1]->getSourceRange();
4476      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4477      return ExprError();
4478    }
4479
4480  // Either we found no viable overloaded operator or we matched a
4481  // built-in operator. In either case, try to build a built-in
4482  // operation.
4483  return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]);
4484}
4485
4486/// BuildCallToMemberFunction - Build a call to a member
4487/// function. MemExpr is the expression that refers to the member
4488/// function (and includes the object parameter), Args/NumArgs are the
4489/// arguments to the function call (not including the object
4490/// parameter). The caller needs to validate that the member
4491/// expression refers to a member function or an overloaded member
4492/// function.
4493Sema::ExprResult
4494Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE,
4495                                SourceLocation LParenLoc, Expr **Args,
4496                                unsigned NumArgs, SourceLocation *CommaLocs,
4497                                SourceLocation RParenLoc) {
4498  // Dig out the member expression. This holds both the object
4499  // argument and the member function we're referring to.
4500  MemberExpr *MemExpr = 0;
4501  if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE))
4502    MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr());
4503  else
4504    MemExpr = dyn_cast<MemberExpr>(MemExprE);
4505  assert(MemExpr && "Building member call without member expression");
4506
4507  // Extract the object argument.
4508  Expr *ObjectArg = MemExpr->getBase();
4509
4510  CXXMethodDecl *Method = 0;
4511  if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) ||
4512      isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) {
4513    // Add overload candidates
4514    OverloadCandidateSet CandidateSet;
4515    DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName();
4516
4517    for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd;
4518         Func != FuncEnd; ++Func) {
4519      if ((Method = dyn_cast<CXXMethodDecl>(*Func)))
4520        AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet,
4521                           /*SuppressUserConversions=*/false);
4522      else
4523        AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func),
4524                                   /*FIXME:*/false, /*FIXME:*/0,
4525                                   /*FIXME:*/0, ObjectArg, Args, NumArgs,
4526                                   CandidateSet,
4527                                   /*SuppressUsedConversions=*/false);
4528    }
4529
4530    OverloadCandidateSet::iterator Best;
4531    switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) {
4532    case OR_Success:
4533      Method = cast<CXXMethodDecl>(Best->Function);
4534      break;
4535
4536    case OR_No_Viable_Function:
4537      Diag(MemExpr->getSourceRange().getBegin(),
4538           diag::err_ovl_no_viable_member_function_in_call)
4539        << DeclName << MemExprE->getSourceRange();
4540      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
4541      // FIXME: Leaking incoming expressions!
4542      return true;
4543
4544    case OR_Ambiguous:
4545      Diag(MemExpr->getSourceRange().getBegin(),
4546           diag::err_ovl_ambiguous_member_call)
4547        << DeclName << MemExprE->getSourceRange();
4548      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
4549      // FIXME: Leaking incoming expressions!
4550      return true;
4551
4552    case OR_Deleted:
4553      Diag(MemExpr->getSourceRange().getBegin(),
4554           diag::err_ovl_deleted_member_call)
4555        << Best->Function->isDeleted()
4556        << DeclName << MemExprE->getSourceRange();
4557      PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
4558      // FIXME: Leaking incoming expressions!
4559      return true;
4560    }
4561
4562    FixOverloadedFunctionReference(MemExpr, Method);
4563  } else {
4564    Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl());
4565  }
4566
4567  assert(Method && "Member call to something that isn't a method?");
4568  ExprOwningPtr<CXXMemberCallExpr>
4569    TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args,
4570                                                  NumArgs,
4571                                  Method->getResultType().getNonReferenceType(),
4572                                  RParenLoc));
4573
4574  // Convert the object argument (for a non-static member function call).
4575  if (!Method->isStatic() &&
4576      PerformObjectArgumentInitialization(ObjectArg, Method))
4577    return true;
4578  MemExpr->setBase(ObjectArg);
4579
4580  // Convert the rest of the arguments
4581  const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType());
4582  if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs,
4583                              RParenLoc))
4584    return true;
4585
4586  if (CheckFunctionCall(Method, TheCall.get()))
4587    return true;
4588
4589  return MaybeBindToTemporary(TheCall.release()).release();
4590}
4591
4592/// BuildCallToObjectOfClassType - Build a call to an object of class
4593/// type (C++ [over.call.object]), which can end up invoking an
4594/// overloaded function call operator (@c operator()) or performing a
4595/// user-defined conversion on the object argument.
4596Sema::ExprResult
4597Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object,
4598                                   SourceLocation LParenLoc,
4599                                   Expr **Args, unsigned NumArgs,
4600                                   SourceLocation *CommaLocs,
4601                                   SourceLocation RParenLoc) {
4602  assert(Object->getType()->isRecordType() && "Requires object type argument");
4603  const RecordType *Record = Object->getType()->getAs<RecordType>();
4604
4605  // C++ [over.call.object]p1:
4606  //  If the primary-expression E in the function call syntax
4607  //  evaluates to a class object of type "cv T", then the set of
4608  //  candidate functions includes at least the function call
4609  //  operators of T. The function call operators of T are obtained by
4610  //  ordinary lookup of the name operator() in the context of
4611  //  (E).operator().
4612  OverloadCandidateSet CandidateSet;
4613  DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call);
4614  DeclContext::lookup_const_iterator Oper, OperEnd;
4615  for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName);
4616       Oper != OperEnd; ++Oper)
4617    AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs,
4618                       CandidateSet, /*SuppressUserConversions=*/false);
4619
4620  // C++ [over.call.object]p2:
4621  //   In addition, for each conversion function declared in T of the
4622  //   form
4623  //
4624  //        operator conversion-type-id () cv-qualifier;
4625  //
4626  //   where cv-qualifier is the same cv-qualification as, or a
4627  //   greater cv-qualification than, cv, and where conversion-type-id
4628  //   denotes the type "pointer to function of (P1,...,Pn) returning
4629  //   R", or the type "reference to pointer to function of
4630  //   (P1,...,Pn) returning R", or the type "reference to function
4631  //   of (P1,...,Pn) returning R", a surrogate call function [...]
4632  //   is also considered as a candidate function. Similarly,
4633  //   surrogate call functions are added to the set of candidate
4634  //   functions for each conversion function declared in an
4635  //   accessible base class provided the function is not hidden
4636  //   within T by another intervening declaration.
4637
4638  if (!RequireCompleteType(SourceLocation(), Object->getType(), 0)) {
4639    // FIXME: Look in base classes for more conversion operators!
4640    OverloadedFunctionDecl *Conversions
4641      = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
4642    for (OverloadedFunctionDecl::function_iterator
4643           Func = Conversions->function_begin(),
4644           FuncEnd = Conversions->function_end();
4645         Func != FuncEnd; ++Func) {
4646      CXXConversionDecl *Conv;
4647      FunctionTemplateDecl *ConvTemplate;
4648      GetFunctionAndTemplate(*Func, Conv, ConvTemplate);
4649
4650      // Skip over templated conversion functions; they aren't
4651      // surrogates.
4652      if (ConvTemplate)
4653        continue;
4654
4655      // Strip the reference type (if any) and then the pointer type (if
4656      // any) to get down to what might be a function type.
4657      QualType ConvType = Conv->getConversionType().getNonReferenceType();
4658      if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>())
4659        ConvType = ConvPtrType->getPointeeType();
4660
4661      if (const FunctionProtoType *Proto = ConvType->getAsFunctionProtoType())
4662        AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet);
4663    }
4664  }
4665
4666  // Perform overload resolution.
4667  OverloadCandidateSet::iterator Best;
4668  switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) {
4669  case OR_Success:
4670    // Overload resolution succeeded; we'll build the appropriate call
4671    // below.
4672    break;
4673
4674  case OR_No_Viable_Function:
4675    Diag(Object->getSourceRange().getBegin(),
4676         diag::err_ovl_no_viable_object_call)
4677      << Object->getType() << Object->getSourceRange();
4678    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
4679    break;
4680
4681  case OR_Ambiguous:
4682    Diag(Object->getSourceRange().getBegin(),
4683         diag::err_ovl_ambiguous_object_call)
4684      << Object->getType() << Object->getSourceRange();
4685    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4686    break;
4687
4688  case OR_Deleted:
4689    Diag(Object->getSourceRange().getBegin(),
4690         diag::err_ovl_deleted_object_call)
4691      << Best->Function->isDeleted()
4692      << Object->getType() << Object->getSourceRange();
4693    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4694    break;
4695  }
4696
4697  if (Best == CandidateSet.end()) {
4698    // We had an error; delete all of the subexpressions and return
4699    // the error.
4700    Object->Destroy(Context);
4701    for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
4702      Args[ArgIdx]->Destroy(Context);
4703    return true;
4704  }
4705
4706  if (Best->Function == 0) {
4707    // Since there is no function declaration, this is one of the
4708    // surrogate candidates. Dig out the conversion function.
4709    CXXConversionDecl *Conv
4710      = cast<CXXConversionDecl>(
4711                         Best->Conversions[0].UserDefined.ConversionFunction);
4712
4713    // We selected one of the surrogate functions that converts the
4714    // object parameter to a function pointer. Perform the conversion
4715    // on the object argument, then let ActOnCallExpr finish the job.
4716    // FIXME: Represent the user-defined conversion in the AST!
4717    ImpCastExprToType(Object,
4718                      Conv->getConversionType().getNonReferenceType(),
4719                      CastExpr::CK_Unknown,
4720                      Conv->getConversionType()->isLValueReferenceType());
4721    return ActOnCallExpr(S, ExprArg(*this, Object), LParenLoc,
4722                         MultiExprArg(*this, (ExprTy**)Args, NumArgs),
4723                         CommaLocs, RParenLoc).release();
4724  }
4725
4726  // We found an overloaded operator(). Build a CXXOperatorCallExpr
4727  // that calls this method, using Object for the implicit object
4728  // parameter and passing along the remaining arguments.
4729  CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function);
4730  const FunctionProtoType *Proto = Method->getType()->getAsFunctionProtoType();
4731
4732  unsigned NumArgsInProto = Proto->getNumArgs();
4733  unsigned NumArgsToCheck = NumArgs;
4734
4735  // Build the full argument list for the method call (the
4736  // implicit object parameter is placed at the beginning of the
4737  // list).
4738  Expr **MethodArgs;
4739  if (NumArgs < NumArgsInProto) {
4740    NumArgsToCheck = NumArgsInProto;
4741    MethodArgs = new Expr*[NumArgsInProto + 1];
4742  } else {
4743    MethodArgs = new Expr*[NumArgs + 1];
4744  }
4745  MethodArgs[0] = Object;
4746  for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
4747    MethodArgs[ArgIdx + 1] = Args[ArgIdx];
4748
4749  Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(),
4750                                          SourceLocation());
4751  UsualUnaryConversions(NewFn);
4752
4753  // Once we've built TheCall, all of the expressions are properly
4754  // owned.
4755  QualType ResultTy = Method->getResultType().getNonReferenceType();
4756  ExprOwningPtr<CXXOperatorCallExpr>
4757    TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn,
4758                                                    MethodArgs, NumArgs + 1,
4759                                                    ResultTy, RParenLoc));
4760  delete [] MethodArgs;
4761
4762  // We may have default arguments. If so, we need to allocate more
4763  // slots in the call for them.
4764  if (NumArgs < NumArgsInProto)
4765    TheCall->setNumArgs(Context, NumArgsInProto + 1);
4766  else if (NumArgs > NumArgsInProto)
4767    NumArgsToCheck = NumArgsInProto;
4768
4769  bool IsError = false;
4770
4771  // Initialize the implicit object parameter.
4772  IsError |= PerformObjectArgumentInitialization(Object, Method);
4773  TheCall->setArg(0, Object);
4774
4775
4776  // Check the argument types.
4777  for (unsigned i = 0; i != NumArgsToCheck; i++) {
4778    Expr *Arg;
4779    if (i < NumArgs) {
4780      Arg = Args[i];
4781
4782      // Pass the argument.
4783      QualType ProtoArgType = Proto->getArgType(i);
4784      IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing");
4785    } else {
4786      Arg = CXXDefaultArgExpr::Create(Context, Method->getParamDecl(i));
4787    }
4788
4789    TheCall->setArg(i + 1, Arg);
4790  }
4791
4792  // If this is a variadic call, handle args passed through "...".
4793  if (Proto->isVariadic()) {
4794    // Promote the arguments (C99 6.5.2.2p7).
4795    for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
4796      Expr *Arg = Args[i];
4797      IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod);
4798      TheCall->setArg(i + 1, Arg);
4799    }
4800  }
4801
4802  if (IsError) return true;
4803
4804  if (CheckFunctionCall(Method, TheCall.get()))
4805    return true;
4806
4807  return MaybeBindToTemporary(TheCall.release()).release();
4808}
4809
4810/// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator->
4811///  (if one exists), where @c Base is an expression of class type and
4812/// @c Member is the name of the member we're trying to find.
4813Sema::OwningExprResult
4814Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) {
4815  Expr *Base = static_cast<Expr *>(BaseIn.get());
4816  assert(Base->getType()->isRecordType() && "left-hand side must have class type");
4817
4818  // C++ [over.ref]p1:
4819  //
4820  //   [...] An expression x->m is interpreted as (x.operator->())->m
4821  //   for a class object x of type T if T::operator->() exists and if
4822  //   the operator is selected as the best match function by the
4823  //   overload resolution mechanism (13.3).
4824  // FIXME: look in base classes.
4825  DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow);
4826  OverloadCandidateSet CandidateSet;
4827  const RecordType *BaseRecord = Base->getType()->getAs<RecordType>();
4828
4829  DeclContext::lookup_const_iterator Oper, OperEnd;
4830  for (llvm::tie(Oper, OperEnd)
4831         = BaseRecord->getDecl()->lookup(OpName); Oper != OperEnd; ++Oper)
4832    AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet,
4833                       /*SuppressUserConversions=*/false);
4834
4835  // Perform overload resolution.
4836  OverloadCandidateSet::iterator Best;
4837  switch (BestViableFunction(CandidateSet, OpLoc, Best)) {
4838  case OR_Success:
4839    // Overload resolution succeeded; we'll build the call below.
4840    break;
4841
4842  case OR_No_Viable_Function:
4843    if (CandidateSet.empty())
4844      Diag(OpLoc, diag::err_typecheck_member_reference_arrow)
4845        << Base->getType() << Base->getSourceRange();
4846    else
4847      Diag(OpLoc, diag::err_ovl_no_viable_oper)
4848        << "operator->" << Base->getSourceRange();
4849    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
4850    return ExprError();
4851
4852  case OR_Ambiguous:
4853    Diag(OpLoc,  diag::err_ovl_ambiguous_oper)
4854      << "operator->" << Base->getSourceRange();
4855    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4856    return ExprError();
4857
4858  case OR_Deleted:
4859    Diag(OpLoc,  diag::err_ovl_deleted_oper)
4860      << Best->Function->isDeleted()
4861      << "operator->" << Base->getSourceRange();
4862    PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
4863    return ExprError();
4864  }
4865
4866  // Convert the object parameter.
4867  CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function);
4868  if (PerformObjectArgumentInitialization(Base, Method))
4869    return ExprError();
4870
4871  // No concerns about early exits now.
4872  BaseIn.release();
4873
4874  // Build the operator call.
4875  Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(),
4876                                           SourceLocation());
4877  UsualUnaryConversions(FnExpr);
4878  Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1,
4879                                 Method->getResultType().getNonReferenceType(),
4880                                 OpLoc);
4881  return Owned(Base);
4882}
4883
4884/// FixOverloadedFunctionReference - E is an expression that refers to
4885/// a C++ overloaded function (possibly with some parentheses and
4886/// perhaps a '&' around it). We have resolved the overloaded function
4887/// to the function declaration Fn, so patch up the expression E to
4888/// refer (possibly indirectly) to Fn.
4889void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) {
4890  if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
4891    FixOverloadedFunctionReference(PE->getSubExpr(), Fn);
4892    E->setType(PE->getSubExpr()->getType());
4893  } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) {
4894    assert(UnOp->getOpcode() == UnaryOperator::AddrOf &&
4895           "Can only take the address of an overloaded function");
4896    if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) {
4897      if (Method->isStatic()) {
4898        // Do nothing: static member functions aren't any different
4899        // from non-member functions.
4900      } else if (QualifiedDeclRefExpr *DRE
4901                 = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) {
4902        // We have taken the address of a pointer to member
4903        // function. Perform the computation here so that we get the
4904        // appropriate pointer to member type.
4905        DRE->setDecl(Fn);
4906        DRE->setType(Fn->getType());
4907        QualType ClassType
4908          = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext()));
4909        E->setType(Context.getMemberPointerType(Fn->getType(),
4910                                                ClassType.getTypePtr()));
4911        return;
4912      }
4913    }
4914    FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn);
4915    E->setType(Context.getPointerType(UnOp->getSubExpr()->getType()));
4916  } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) {
4917    assert((isa<OverloadedFunctionDecl>(DR->getDecl()) ||
4918            isa<FunctionTemplateDecl>(DR->getDecl())) &&
4919           "Expected overloaded function or function template");
4920    DR->setDecl(Fn);
4921    E->setType(Fn->getType());
4922  } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) {
4923    MemExpr->setMemberDecl(Fn);
4924    E->setType(Fn->getType());
4925  } else {
4926    assert(false && "Invalid reference to overloaded function");
4927  }
4928}
4929
4930} // end namespace clang
4931