blob: 836534951326bb9353b9e10f477415d2e0e4ea45 [file] [log] [blame]
Douglas Gregord2baafd2008-10-21 16:13:35 +00001//===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides Sema routines for C++ overloading.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Douglas Gregorbb461502008-10-24 04:54:22 +000015#include "SemaInherit.h"
Douglas Gregord2baafd2008-10-21 16:13:35 +000016#include "clang/Basic/Diagnostic.h"
Douglas Gregor70d26122008-11-12 17:17:38 +000017#include "clang/Lex/Preprocessor.h"
Douglas Gregord2baafd2008-10-21 16:13:35 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Expr.h"
Douglas Gregor10f3c502008-11-19 21:05:33 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor70d26122008-11-12 17:17:38 +000021#include "clang/AST/TypeOrdering.h"
Douglas Gregor3d4492e2008-11-13 20:12:29 +000022#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregord2baafd2008-10-21 16:13:35 +000023#include "llvm/Support/Compiler.h"
24#include <algorithm>
25
26namespace clang {
27
28/// GetConversionCategory - Retrieve the implicit conversion
29/// category corresponding to the given implicit conversion kind.
30ImplicitConversionCategory
31GetConversionCategory(ImplicitConversionKind Kind) {
32 static const ImplicitConversionCategory
33 Category[(int)ICK_Num_Conversion_Kinds] = {
34 ICC_Identity,
35 ICC_Lvalue_Transformation,
36 ICC_Lvalue_Transformation,
37 ICC_Lvalue_Transformation,
38 ICC_Qualification_Adjustment,
39 ICC_Promotion,
40 ICC_Promotion,
41 ICC_Conversion,
42 ICC_Conversion,
43 ICC_Conversion,
44 ICC_Conversion,
45 ICC_Conversion,
Douglas Gregor2aecd1f2008-10-29 02:00:59 +000046 ICC_Conversion,
Douglas Gregord2baafd2008-10-21 16:13:35 +000047 ICC_Conversion
48 };
49 return Category[(int)Kind];
50}
51
52/// GetConversionRank - Retrieve the implicit conversion rank
53/// corresponding to the given implicit conversion kind.
54ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) {
55 static const ImplicitConversionRank
56 Rank[(int)ICK_Num_Conversion_Kinds] = {
57 ICR_Exact_Match,
58 ICR_Exact_Match,
59 ICR_Exact_Match,
60 ICR_Exact_Match,
61 ICR_Exact_Match,
62 ICR_Promotion,
63 ICR_Promotion,
64 ICR_Conversion,
65 ICR_Conversion,
66 ICR_Conversion,
67 ICR_Conversion,
68 ICR_Conversion,
Douglas Gregor2aecd1f2008-10-29 02:00:59 +000069 ICR_Conversion,
Douglas Gregord2baafd2008-10-21 16:13:35 +000070 ICR_Conversion
71 };
72 return Rank[(int)Kind];
73}
74
75/// GetImplicitConversionName - Return the name of this kind of
76/// implicit conversion.
77const char* GetImplicitConversionName(ImplicitConversionKind Kind) {
78 static const char* Name[(int)ICK_Num_Conversion_Kinds] = {
79 "No conversion",
80 "Lvalue-to-rvalue",
81 "Array-to-pointer",
82 "Function-to-pointer",
83 "Qualification",
84 "Integral promotion",
85 "Floating point promotion",
86 "Integral conversion",
87 "Floating conversion",
88 "Floating-integral conversion",
89 "Pointer conversion",
90 "Pointer-to-member conversion",
Douglas Gregor2aecd1f2008-10-29 02:00:59 +000091 "Boolean conversion",
92 "Derived-to-base conversion"
Douglas Gregord2baafd2008-10-21 16:13:35 +000093 };
94 return Name[Kind];
95}
96
Douglas Gregorb72e9da2008-10-31 16:23:19 +000097/// StandardConversionSequence - Set the standard conversion
98/// sequence to the identity conversion.
99void StandardConversionSequence::setAsIdentityConversion() {
100 First = ICK_Identity;
101 Second = ICK_Identity;
102 Third = ICK_Identity;
103 Deprecated = false;
104 ReferenceBinding = false;
105 DirectBinding = false;
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000106 CopyConstructor = 0;
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000107}
108
Douglas Gregord2baafd2008-10-21 16:13:35 +0000109/// getRank - Retrieve the rank of this standard conversion sequence
110/// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the
111/// implicit conversions.
112ImplicitConversionRank StandardConversionSequence::getRank() const {
113 ImplicitConversionRank Rank = ICR_Exact_Match;
114 if (GetConversionRank(First) > Rank)
115 Rank = GetConversionRank(First);
116 if (GetConversionRank(Second) > Rank)
117 Rank = GetConversionRank(Second);
118 if (GetConversionRank(Third) > Rank)
119 Rank = GetConversionRank(Third);
120 return Rank;
121}
122
123/// isPointerConversionToBool - Determines whether this conversion is
124/// a conversion of a pointer or pointer-to-member to bool. This is
125/// used as part of the ranking of standard conversion sequences
126/// (C++ 13.3.3.2p4).
127bool StandardConversionSequence::isPointerConversionToBool() const
128{
129 QualType FromType = QualType::getFromOpaquePtr(FromTypePtr);
130 QualType ToType = QualType::getFromOpaquePtr(ToTypePtr);
131
132 // Note that FromType has not necessarily been transformed by the
133 // array-to-pointer or function-to-pointer implicit conversions, so
134 // check for their presence as well as checking whether FromType is
135 // a pointer.
136 if (ToType->isBooleanType() &&
137 (FromType->isPointerType() ||
138 First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer))
139 return true;
140
141 return false;
142}
143
Douglas Gregor14046502008-10-23 00:40:37 +0000144/// isPointerConversionToVoidPointer - Determines whether this
145/// conversion is a conversion of a pointer to a void pointer. This is
146/// used as part of the ranking of standard conversion sequences (C++
147/// 13.3.3.2p4).
148bool
149StandardConversionSequence::
150isPointerConversionToVoidPointer(ASTContext& Context) const
151{
152 QualType FromType = QualType::getFromOpaquePtr(FromTypePtr);
153 QualType ToType = QualType::getFromOpaquePtr(ToTypePtr);
154
155 // Note that FromType has not necessarily been transformed by the
156 // array-to-pointer implicit conversion, so check for its presence
157 // and redo the conversion to get a pointer.
158 if (First == ICK_Array_To_Pointer)
159 FromType = Context.getArrayDecayedType(FromType);
160
161 if (Second == ICK_Pointer_Conversion)
162 if (const PointerType* ToPtrType = ToType->getAsPointerType())
163 return ToPtrType->getPointeeType()->isVoidType();
164
165 return false;
166}
167
Douglas Gregord2baafd2008-10-21 16:13:35 +0000168/// DebugPrint - Print this standard conversion sequence to standard
169/// error. Useful for debugging overloading issues.
170void StandardConversionSequence::DebugPrint() const {
171 bool PrintedSomething = false;
172 if (First != ICK_Identity) {
173 fprintf(stderr, "%s", GetImplicitConversionName(First));
174 PrintedSomething = true;
175 }
176
177 if (Second != ICK_Identity) {
178 if (PrintedSomething) {
179 fprintf(stderr, " -> ");
180 }
181 fprintf(stderr, "%s", GetImplicitConversionName(Second));
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000182
183 if (CopyConstructor) {
184 fprintf(stderr, " (by copy constructor)");
185 } else if (DirectBinding) {
186 fprintf(stderr, " (direct reference binding)");
187 } else if (ReferenceBinding) {
188 fprintf(stderr, " (reference binding)");
189 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000190 PrintedSomething = true;
191 }
192
193 if (Third != ICK_Identity) {
194 if (PrintedSomething) {
195 fprintf(stderr, " -> ");
196 }
197 fprintf(stderr, "%s", GetImplicitConversionName(Third));
198 PrintedSomething = true;
199 }
200
201 if (!PrintedSomething) {
202 fprintf(stderr, "No conversions required");
203 }
204}
205
206/// DebugPrint - Print this user-defined conversion sequence to standard
207/// error. Useful for debugging overloading issues.
208void UserDefinedConversionSequence::DebugPrint() const {
209 if (Before.First || Before.Second || Before.Third) {
210 Before.DebugPrint();
211 fprintf(stderr, " -> ");
212 }
Chris Lattner271d4c22008-11-24 05:29:24 +0000213 fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str());
Douglas Gregord2baafd2008-10-21 16:13:35 +0000214 if (After.First || After.Second || After.Third) {
215 fprintf(stderr, " -> ");
216 After.DebugPrint();
217 }
218}
219
220/// DebugPrint - Print this implicit conversion sequence to standard
221/// error. Useful for debugging overloading issues.
222void ImplicitConversionSequence::DebugPrint() const {
223 switch (ConversionKind) {
224 case StandardConversion:
225 fprintf(stderr, "Standard conversion: ");
226 Standard.DebugPrint();
227 break;
228 case UserDefinedConversion:
229 fprintf(stderr, "User-defined conversion: ");
230 UserDefined.DebugPrint();
231 break;
232 case EllipsisConversion:
233 fprintf(stderr, "Ellipsis conversion");
234 break;
235 case BadConversion:
236 fprintf(stderr, "Bad conversion");
237 break;
238 }
239
240 fprintf(stderr, "\n");
241}
242
243// IsOverload - Determine whether the given New declaration is an
244// overload of the Old declaration. This routine returns false if New
245// and Old cannot be overloaded, e.g., if they are functions with the
246// same signature (C++ 1.3.10) or if the Old declaration isn't a
247// function (or overload set). When it does return false and Old is an
248// OverloadedFunctionDecl, MatchedDecl will be set to point to the
249// FunctionDecl that New cannot be overloaded with.
250//
251// Example: Given the following input:
252//
253// void f(int, float); // #1
254// void f(int, int); // #2
255// int f(int, int); // #3
256//
257// When we process #1, there is no previous declaration of "f",
258// so IsOverload will not be used.
259//
260// When we process #2, Old is a FunctionDecl for #1. By comparing the
261// parameter types, we see that #1 and #2 are overloaded (since they
262// have different signatures), so this routine returns false;
263// MatchedDecl is unchanged.
264//
265// When we process #3, Old is an OverloadedFunctionDecl containing #1
266// and #2. We compare the signatures of #3 to #1 (they're overloaded,
267// so we do nothing) and then #3 to #2. Since the signatures of #3 and
268// #2 are identical (return types of functions are not part of the
269// signature), IsOverload returns false and MatchedDecl will be set to
270// point to the FunctionDecl for #2.
271bool
272Sema::IsOverload(FunctionDecl *New, Decl* OldD,
273 OverloadedFunctionDecl::function_iterator& MatchedDecl)
274{
275 if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) {
276 // Is this new function an overload of every function in the
277 // overload set?
278 OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
279 FuncEnd = Ovl->function_end();
280 for (; Func != FuncEnd; ++Func) {
281 if (!IsOverload(New, *Func, MatchedDecl)) {
282 MatchedDecl = Func;
283 return false;
284 }
285 }
286
287 // This function overloads every function in the overload set.
288 return true;
289 } else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) {
290 // Is the function New an overload of the function Old?
291 QualType OldQType = Context.getCanonicalType(Old->getType());
292 QualType NewQType = Context.getCanonicalType(New->getType());
293
294 // Compare the signatures (C++ 1.3.10) of the two functions to
295 // determine whether they are overloads. If we find any mismatch
296 // in the signature, they are overloads.
297
298 // If either of these functions is a K&R-style function (no
299 // prototype), then we consider them to have matching signatures.
300 if (isa<FunctionTypeNoProto>(OldQType.getTypePtr()) ||
301 isa<FunctionTypeNoProto>(NewQType.getTypePtr()))
302 return false;
303
304 FunctionTypeProto* OldType = cast<FunctionTypeProto>(OldQType.getTypePtr());
305 FunctionTypeProto* NewType = cast<FunctionTypeProto>(NewQType.getTypePtr());
306
307 // The signature of a function includes the types of its
308 // parameters (C++ 1.3.10), which includes the presence or absence
309 // of the ellipsis; see C++ DR 357).
310 if (OldQType != NewQType &&
311 (OldType->getNumArgs() != NewType->getNumArgs() ||
312 OldType->isVariadic() != NewType->isVariadic() ||
313 !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(),
314 NewType->arg_type_begin())))
315 return true;
316
317 // If the function is a class member, its signature includes the
318 // cv-qualifiers (if any) on the function itself.
319 //
320 // As part of this, also check whether one of the member functions
321 // is static, in which case they are not overloads (C++
322 // 13.1p2). While not part of the definition of the signature,
323 // this check is important to determine whether these functions
324 // can be overloaded.
325 CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
326 CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
327 if (OldMethod && NewMethod &&
328 !OldMethod->isStatic() && !NewMethod->isStatic() &&
Douglas Gregora7b56a32008-11-21 15:36:28 +0000329 OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers())
Douglas Gregord2baafd2008-10-21 16:13:35 +0000330 return true;
331
332 // The signatures match; this is not an overload.
333 return false;
334 } else {
335 // (C++ 13p1):
336 // Only function declarations can be overloaded; object and type
337 // declarations cannot be overloaded.
338 return false;
339 }
340}
341
Douglas Gregor81c29152008-10-29 00:13:59 +0000342/// TryImplicitConversion - Attempt to perform an implicit conversion
343/// from the given expression (Expr) to the given type (ToType). This
344/// function returns an implicit conversion sequence that can be used
345/// to perform the initialization. Given
Douglas Gregord2baafd2008-10-21 16:13:35 +0000346///
347/// void f(float f);
348/// void g(int i) { f(i); }
349///
350/// this routine would produce an implicit conversion sequence to
351/// describe the initialization of f from i, which will be a standard
352/// conversion sequence containing an lvalue-to-rvalue conversion (C++
353/// 4.1) followed by a floating-integral conversion (C++ 4.9).
354//
355/// Note that this routine only determines how the conversion can be
356/// performed; it does not actually perform the conversion. As such,
357/// it will not produce any diagnostics if no conversion is available,
358/// but will instead return an implicit conversion sequence of kind
359/// "BadConversion".
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000360///
361/// If @p SuppressUserConversions, then user-defined conversions are
362/// not permitted.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000363ImplicitConversionSequence
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000364Sema::TryImplicitConversion(Expr* From, QualType ToType,
365 bool SuppressUserConversions)
Douglas Gregord2baafd2008-10-21 16:13:35 +0000366{
367 ImplicitConversionSequence ICS;
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000368 if (IsStandardConversion(From, ToType, ICS.Standard))
369 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000370 else if (!SuppressUserConversions &&
371 IsUserDefinedConversion(From, ToType, ICS.UserDefined)) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000372 ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion;
Douglas Gregore640ab62008-11-03 17:51:48 +0000373 // C++ [over.ics.user]p4:
374 // A conversion of an expression of class type to the same class
375 // type is given Exact Match rank, and a conversion of an
376 // expression of class type to a base class of that type is
377 // given Conversion rank, in spite of the fact that a copy
378 // constructor (i.e., a user-defined conversion function) is
379 // called for those cases.
380 if (CXXConstructorDecl *Constructor
381 = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) {
382 if (Constructor->isCopyConstructor(Context)) {
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000383 // Turn this into a "standard" conversion sequence, so that it
384 // gets ranked with standard conversion sequences.
Douglas Gregore640ab62008-11-03 17:51:48 +0000385 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
386 ICS.Standard.setAsIdentityConversion();
387 ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr();
388 ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr();
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000389 ICS.Standard.CopyConstructor = Constructor;
Douglas Gregore640ab62008-11-03 17:51:48 +0000390 if (IsDerivedFrom(From->getType().getUnqualifiedType(),
391 ToType.getUnqualifiedType()))
392 ICS.Standard.Second = ICK_Derived_To_Base;
393 }
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000394 }
Douglas Gregore640ab62008-11-03 17:51:48 +0000395 } else
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000396 ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000397
398 return ICS;
399}
400
401/// IsStandardConversion - Determines whether there is a standard
402/// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the
403/// expression From to the type ToType. Standard conversion sequences
404/// only consider non-class types; for conversions that involve class
405/// types, use TryImplicitConversion. If a conversion exists, SCS will
406/// contain the standard conversion sequence required to perform this
407/// conversion and this routine will return true. Otherwise, this
408/// routine will return false and the value of SCS is unspecified.
409bool
410Sema::IsStandardConversion(Expr* From, QualType ToType,
411 StandardConversionSequence &SCS)
412{
Douglas Gregord2baafd2008-10-21 16:13:35 +0000413 QualType FromType = From->getType();
414
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000415 // There are no standard conversions for class types, so abort early.
416 if (FromType->isRecordType() || ToType->isRecordType())
417 return false;
418
419 // Standard conversions (C++ [conv])
Douglas Gregor70d26122008-11-12 17:17:38 +0000420 SCS.setAsIdentityConversion();
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000421 SCS.Deprecated = false;
Douglas Gregor6fd35572008-12-19 17:40:08 +0000422 SCS.IncompatibleObjC = false;
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000423 SCS.FromTypePtr = FromType.getAsOpaquePtr();
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000424 SCS.CopyConstructor = 0;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000425
426 // The first conversion can be an lvalue-to-rvalue conversion,
427 // array-to-pointer conversion, or function-to-pointer conversion
428 // (C++ 4p1).
429
430 // Lvalue-to-rvalue conversion (C++ 4.1):
431 // An lvalue (3.10) of a non-function, non-array type T can be
432 // converted to an rvalue.
433 Expr::isLvalueResult argIsLvalue = From->isLvalue(Context);
434 if (argIsLvalue == Expr::LV_Valid &&
Douglas Gregor45014fd2008-11-10 20:40:00 +0000435 !FromType->isFunctionType() && !FromType->isArrayType() &&
436 !FromType->isOverloadType()) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000437 SCS.First = ICK_Lvalue_To_Rvalue;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000438
439 // If T is a non-class type, the type of the rvalue is the
440 // cv-unqualified version of T. Otherwise, the type of the rvalue
441 // is T (C++ 4.1p1).
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000442 FromType = FromType.getUnqualifiedType();
Douglas Gregord2baafd2008-10-21 16:13:35 +0000443 }
444 // Array-to-pointer conversion (C++ 4.2)
445 else if (FromType->isArrayType()) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000446 SCS.First = ICK_Array_To_Pointer;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000447
448 // An lvalue or rvalue of type "array of N T" or "array of unknown
449 // bound of T" can be converted to an rvalue of type "pointer to
450 // T" (C++ 4.2p1).
451 FromType = Context.getArrayDecayedType(FromType);
452
453 if (IsStringLiteralToNonConstPointerConversion(From, ToType)) {
454 // This conversion is deprecated. (C++ D.4).
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000455 SCS.Deprecated = true;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000456
457 // For the purpose of ranking in overload resolution
458 // (13.3.3.1.1), this conversion is considered an
459 // array-to-pointer conversion followed by a qualification
460 // conversion (4.4). (C++ 4.2p2)
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000461 SCS.Second = ICK_Identity;
462 SCS.Third = ICK_Qualification;
463 SCS.ToTypePtr = ToType.getAsOpaquePtr();
464 return true;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000465 }
466 }
467 // Function-to-pointer conversion (C++ 4.3).
468 else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000469 SCS.First = ICK_Function_To_Pointer;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000470
471 // An lvalue of function type T can be converted to an rvalue of
472 // type "pointer to T." The result is a pointer to the
473 // function. (C++ 4.3p1).
474 FromType = Context.getPointerType(FromType);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000475 }
Douglas Gregor45014fd2008-11-10 20:40:00 +0000476 // Address of overloaded function (C++ [over.over]).
477 else if (FunctionDecl *Fn
478 = ResolveAddressOfOverloadedFunction(From, ToType, false)) {
479 SCS.First = ICK_Function_To_Pointer;
480
481 // We were able to resolve the address of the overloaded function,
482 // so we can convert to the type of that function.
483 FromType = Fn->getType();
484 if (ToType->isReferenceType())
485 FromType = Context.getReferenceType(FromType);
486 else
487 FromType = Context.getPointerType(FromType);
488 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000489 // We don't require any conversions for the first step.
490 else {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000491 SCS.First = ICK_Identity;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000492 }
493
494 // The second conversion can be an integral promotion, floating
495 // point promotion, integral conversion, floating point conversion,
496 // floating-integral conversion, pointer conversion,
497 // pointer-to-member conversion, or boolean conversion (C++ 4p1).
Douglas Gregor6fd35572008-12-19 17:40:08 +0000498 bool IncompatibleObjC = false;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000499 if (Context.getCanonicalType(FromType).getUnqualifiedType() ==
500 Context.getCanonicalType(ToType).getUnqualifiedType()) {
501 // The unqualified versions of the types are the same: there's no
502 // conversion to do.
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000503 SCS.Second = ICK_Identity;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000504 }
505 // Integral promotion (C++ 4.5).
506 else if (IsIntegralPromotion(From, FromType, ToType)) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000507 SCS.Second = ICK_Integral_Promotion;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000508 FromType = ToType.getUnqualifiedType();
509 }
510 // Floating point promotion (C++ 4.6).
511 else if (IsFloatingPointPromotion(FromType, ToType)) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000512 SCS.Second = ICK_Floating_Promotion;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000513 FromType = ToType.getUnqualifiedType();
514 }
515 // Integral conversions (C++ 4.7).
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000516 // FIXME: isIntegralType shouldn't be true for enums in C++.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000517 else if ((FromType->isIntegralType() || FromType->isEnumeralType()) &&
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000518 (ToType->isIntegralType() && !ToType->isEnumeralType())) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000519 SCS.Second = ICK_Integral_Conversion;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000520 FromType = ToType.getUnqualifiedType();
521 }
522 // Floating point conversions (C++ 4.8).
523 else if (FromType->isFloatingType() && ToType->isFloatingType()) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000524 SCS.Second = ICK_Floating_Conversion;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000525 FromType = ToType.getUnqualifiedType();
526 }
527 // Floating-integral conversions (C++ 4.9).
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000528 // FIXME: isIntegralType shouldn't be true for enums in C++.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000529 else if ((FromType->isFloatingType() &&
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000530 ToType->isIntegralType() && !ToType->isBooleanType() &&
531 !ToType->isEnumeralType()) ||
Douglas Gregord2baafd2008-10-21 16:13:35 +0000532 ((FromType->isIntegralType() || FromType->isEnumeralType()) &&
533 ToType->isFloatingType())) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000534 SCS.Second = ICK_Floating_Integral;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000535 FromType = ToType.getUnqualifiedType();
536 }
537 // Pointer conversions (C++ 4.10).
Douglas Gregor6fd35572008-12-19 17:40:08 +0000538 else if (IsPointerConversion(From, FromType, ToType, FromType,
539 IncompatibleObjC)) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000540 SCS.Second = ICK_Pointer_Conversion;
Douglas Gregor6fd35572008-12-19 17:40:08 +0000541 SCS.IncompatibleObjC = IncompatibleObjC;
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000542 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000543 // FIXME: Pointer to member conversions (4.11).
544 // Boolean conversions (C++ 4.12).
545 // FIXME: pointer-to-member type
546 else if (ToType->isBooleanType() &&
547 (FromType->isArithmeticType() ||
548 FromType->isEnumeralType() ||
549 FromType->isPointerType())) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000550 SCS.Second = ICK_Boolean_Conversion;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000551 FromType = Context.BoolTy;
552 } else {
553 // No second conversion required.
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000554 SCS.Second = ICK_Identity;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000555 }
556
Douglas Gregor81c29152008-10-29 00:13:59 +0000557 QualType CanonFrom;
558 QualType CanonTo;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000559 // The third conversion can be a qualification conversion (C++ 4p1).
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000560 if (IsQualificationConversion(FromType, ToType)) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000561 SCS.Third = ICK_Qualification;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000562 FromType = ToType;
Douglas Gregor81c29152008-10-29 00:13:59 +0000563 CanonFrom = Context.getCanonicalType(FromType);
564 CanonTo = Context.getCanonicalType(ToType);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000565 } else {
566 // No conversion required
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000567 SCS.Third = ICK_Identity;
568
569 // C++ [over.best.ics]p6:
570 // [...] Any difference in top-level cv-qualification is
571 // subsumed by the initialization itself and does not constitute
572 // a conversion. [...]
Douglas Gregor81c29152008-10-29 00:13:59 +0000573 CanonFrom = Context.getCanonicalType(FromType);
574 CanonTo = Context.getCanonicalType(ToType);
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000575 if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() &&
Douglas Gregor81c29152008-10-29 00:13:59 +0000576 CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) {
577 FromType = ToType;
578 CanonFrom = CanonTo;
579 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000580 }
581
582 // If we have not converted the argument type to the parameter type,
583 // this is a bad conversion sequence.
Douglas Gregor81c29152008-10-29 00:13:59 +0000584 if (CanonFrom != CanonTo)
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000585 return false;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000586
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000587 SCS.ToTypePtr = FromType.getAsOpaquePtr();
588 return true;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000589}
590
591/// IsIntegralPromotion - Determines whether the conversion from the
592/// expression From (whose potentially-adjusted type is FromType) to
593/// ToType is an integral promotion (C++ 4.5). If so, returns true and
594/// sets PromotedType to the promoted type.
595bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType)
596{
597 const BuiltinType *To = ToType->getAsBuiltinType();
Sebastian Redl12aee862008-11-04 15:59:10 +0000598 // All integers are built-in.
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000599 if (!To) {
600 return false;
601 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000602
603 // An rvalue of type char, signed char, unsigned char, short int, or
604 // unsigned short int can be converted to an rvalue of type int if
605 // int can represent all the values of the source type; otherwise,
606 // the source rvalue can be converted to an rvalue of type unsigned
607 // int (C++ 4.5p1).
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000608 if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000609 if (// We can promote any signed, promotable integer type to an int
610 (FromType->isSignedIntegerType() ||
611 // We can promote any unsigned integer type whose size is
612 // less than int to an int.
613 (!FromType->isSignedIntegerType() &&
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000614 Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000615 return To->getKind() == BuiltinType::Int;
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000616 }
617
Douglas Gregord2baafd2008-10-21 16:13:35 +0000618 return To->getKind() == BuiltinType::UInt;
619 }
620
621 // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2)
622 // can be converted to an rvalue of the first of the following types
623 // that can represent all the values of its underlying type: int,
624 // unsigned int, long, or unsigned long (C++ 4.5p2).
625 if ((FromType->isEnumeralType() || FromType->isWideCharType())
626 && ToType->isIntegerType()) {
627 // Determine whether the type we're converting from is signed or
628 // unsigned.
629 bool FromIsSigned;
630 uint64_t FromSize = Context.getTypeSize(FromType);
631 if (const EnumType *FromEnumType = FromType->getAsEnumType()) {
632 QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType();
633 FromIsSigned = UnderlyingType->isSignedIntegerType();
634 } else {
635 // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now.
636 FromIsSigned = true;
637 }
638
639 // The types we'll try to promote to, in the appropriate
640 // order. Try each of these types.
Douglas Gregor6b5e34f2008-12-12 02:00:36 +0000641 QualType PromoteTypes[6] = {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000642 Context.IntTy, Context.UnsignedIntTy,
Douglas Gregor6b5e34f2008-12-12 02:00:36 +0000643 Context.LongTy, Context.UnsignedLongTy ,
644 Context.LongLongTy, Context.UnsignedLongLongTy
Douglas Gregord2baafd2008-10-21 16:13:35 +0000645 };
Douglas Gregor6b5e34f2008-12-12 02:00:36 +0000646 for (int Idx = 0; Idx < 6; ++Idx) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000647 uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]);
648 if (FromSize < ToSize ||
649 (FromSize == ToSize &&
650 FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) {
651 // We found the type that we can promote to. If this is the
652 // type we wanted, we have a promotion. Otherwise, no
653 // promotion.
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000654 return Context.getCanonicalType(ToType).getUnqualifiedType()
Douglas Gregord2baafd2008-10-21 16:13:35 +0000655 == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType();
656 }
657 }
658 }
659
660 // An rvalue for an integral bit-field (9.6) can be converted to an
661 // rvalue of type int if int can represent all the values of the
662 // bit-field; otherwise, it can be converted to unsigned int if
663 // unsigned int can represent all the values of the bit-field. If
664 // the bit-field is larger yet, no integral promotion applies to
665 // it. If the bit-field has an enumerated type, it is treated as any
666 // other value of that type for promotion purposes (C++ 4.5p3).
667 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(From)) {
668 using llvm::APSInt;
Douglas Gregor82d44772008-12-20 23:49:58 +0000669 if (FieldDecl *MemberDecl = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) {
670 APSInt BitWidth;
671 if (MemberDecl->isBitField() &&
672 FromType->isIntegralType() && !FromType->isEnumeralType() &&
673 From->isIntegerConstantExpr(BitWidth, Context)) {
674 APSInt ToSize(Context.getTypeSize(ToType));
675
676 // Are we promoting to an int from a bitfield that fits in an int?
677 if (BitWidth < ToSize ||
678 (FromType->isSignedIntegerType() && BitWidth <= ToSize)) {
679 return To->getKind() == BuiltinType::Int;
680 }
681
682 // Are we promoting to an unsigned int from an unsigned bitfield
683 // that fits into an unsigned int?
684 if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) {
685 return To->getKind() == BuiltinType::UInt;
686 }
687
688 return false;
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000689 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000690 }
691 }
692
693 // An rvalue of type bool can be converted to an rvalue of type int,
694 // with false becoming zero and true becoming one (C++ 4.5p4).
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000695 if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000696 return true;
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000697 }
Douglas Gregord2baafd2008-10-21 16:13:35 +0000698
699 return false;
700}
701
702/// IsFloatingPointPromotion - Determines whether the conversion from
703/// FromType to ToType is a floating point promotion (C++ 4.6). If so,
704/// returns true and sets PromotedType to the promoted type.
705bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType)
706{
707 /// An rvalue of type float can be converted to an rvalue of type
708 /// double. (C++ 4.6p1).
709 if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType())
710 if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType())
711 if (FromBuiltin->getKind() == BuiltinType::Float &&
712 ToBuiltin->getKind() == BuiltinType::Double)
713 return true;
714
715 return false;
716}
717
Douglas Gregor24a90a52008-11-26 23:31:11 +0000718/// BuildSimilarlyQualifiedPointerType - In a pointer conversion from
719/// the pointer type FromPtr to a pointer to type ToPointee, with the
720/// same type qualifiers as FromPtr has on its pointee type. ToType,
721/// if non-empty, will be a pointer to ToType that may or may not have
722/// the right set of qualifiers on its pointee.
723static QualType
724BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr,
725 QualType ToPointee, QualType ToType,
726 ASTContext &Context) {
727 QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType());
728 QualType CanonToPointee = Context.getCanonicalType(ToPointee);
729 unsigned Quals = CanonFromPointee.getCVRQualifiers();
730
731 // Exact qualifier match -> return the pointer type we're converting to.
732 if (CanonToPointee.getCVRQualifiers() == Quals) {
733 // ToType is exactly what we need. Return it.
734 if (ToType.getTypePtr())
735 return ToType;
736
737 // Build a pointer to ToPointee. It has the right qualifiers
738 // already.
739 return Context.getPointerType(ToPointee);
740 }
741
742 // Just build a canonical type that has the right qualifiers.
743 return Context.getPointerType(CanonToPointee.getQualifiedType(Quals));
744}
745
Douglas Gregord2baafd2008-10-21 16:13:35 +0000746/// IsPointerConversion - Determines whether the conversion of the
747/// expression From, which has the (possibly adjusted) type FromType,
748/// can be converted to the type ToType via a pointer conversion (C++
749/// 4.10). If so, returns true and places the converted type (that
750/// might differ from ToType in its cv-qualifiers at some level) into
751/// ConvertedType.
Douglas Gregor9036ef72008-11-27 00:15:41 +0000752///
Douglas Gregor3f5a00c2008-11-27 01:19:21 +0000753/// This routine also supports conversions to and from block pointers
754/// and conversions with Objective-C's 'id', 'id<protocols...>', and
755/// pointers to interfaces. FIXME: Once we've determined the
756/// appropriate overloading rules for Objective-C, we may want to
757/// split the Objective-C checks into a different routine; however,
758/// GCC seems to consider all of these conversions to be pointer
Douglas Gregor6fd35572008-12-19 17:40:08 +0000759/// conversions, so for now they live here. IncompatibleObjC will be
760/// set if the conversion is an allowed Objective-C conversion that
761/// should result in a warning.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000762bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType,
Douglas Gregor6fd35572008-12-19 17:40:08 +0000763 QualType& ConvertedType,
764 bool &IncompatibleObjC)
Douglas Gregord2baafd2008-10-21 16:13:35 +0000765{
Douglas Gregor6fd35572008-12-19 17:40:08 +0000766 IncompatibleObjC = false;
Douglas Gregor932778b2008-12-19 19:13:09 +0000767 if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC))
768 return true;
Douglas Gregor6fd35572008-12-19 17:40:08 +0000769
Douglas Gregorf1d75712008-12-22 20:51:52 +0000770 // Conversion from a null pointer constant to any Objective-C pointer type.
771 if (Context.isObjCObjectPointerType(ToType) &&
772 From->isNullPointerConstant(Context)) {
773 ConvertedType = ToType;
774 return true;
775 }
776
Douglas Gregor9036ef72008-11-27 00:15:41 +0000777 // Blocks: Block pointers can be converted to void*.
778 if (FromType->isBlockPointerType() && ToType->isPointerType() &&
779 ToType->getAsPointerType()->getPointeeType()->isVoidType()) {
780 ConvertedType = ToType;
781 return true;
782 }
783 // Blocks: A null pointer constant can be converted to a block
784 // pointer type.
785 if (ToType->isBlockPointerType() && From->isNullPointerConstant(Context)) {
786 ConvertedType = ToType;
787 return true;
788 }
789
Douglas Gregord2baafd2008-10-21 16:13:35 +0000790 const PointerType* ToTypePtr = ToType->getAsPointerType();
791 if (!ToTypePtr)
792 return false;
793
794 // A null pointer constant can be converted to a pointer type (C++ 4.10p1).
795 if (From->isNullPointerConstant(Context)) {
796 ConvertedType = ToType;
797 return true;
798 }
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000799
Douglas Gregor24a90a52008-11-26 23:31:11 +0000800 // Beyond this point, both types need to be pointers.
801 const PointerType *FromTypePtr = FromType->getAsPointerType();
802 if (!FromTypePtr)
803 return false;
804
805 QualType FromPointeeType = FromTypePtr->getPointeeType();
806 QualType ToPointeeType = ToTypePtr->getPointeeType();
807
Douglas Gregord2baafd2008-10-21 16:13:35 +0000808 // An rvalue of type "pointer to cv T," where T is an object type,
809 // can be converted to an rvalue of type "pointer to cv void" (C++
810 // 4.10p2).
Douglas Gregor932778b2008-12-19 19:13:09 +0000811 if (FromPointeeType->isIncompleteOrObjectType() &&
812 ToPointeeType->isVoidType()) {
Douglas Gregor8bb7ad82008-11-27 00:52:49 +0000813 ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
814 ToPointeeType,
Douglas Gregor24a90a52008-11-26 23:31:11 +0000815 ToType, Context);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000816 return true;
817 }
818
Douglas Gregor14046502008-10-23 00:40:37 +0000819 // C++ [conv.ptr]p3:
820 //
821 // An rvalue of type "pointer to cv D," where D is a class type,
822 // can be converted to an rvalue of type "pointer to cv B," where
823 // B is a base class (clause 10) of D. If B is an inaccessible
824 // (clause 11) or ambiguous (10.2) base class of D, a program that
825 // necessitates this conversion is ill-formed. The result of the
826 // conversion is a pointer to the base class sub-object of the
827 // derived class object. The null pointer value is converted to
828 // the null pointer value of the destination type.
829 //
Douglas Gregorbb461502008-10-24 04:54:22 +0000830 // Note that we do not check for ambiguity or inaccessibility
831 // here. That is handled by CheckPointerConversion.
Douglas Gregor24a90a52008-11-26 23:31:11 +0000832 if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() &&
833 IsDerivedFrom(FromPointeeType, ToPointeeType)) {
Douglas Gregor8bb7ad82008-11-27 00:52:49 +0000834 ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
835 ToPointeeType,
Douglas Gregor24a90a52008-11-26 23:31:11 +0000836 ToType, Context);
837 return true;
838 }
Douglas Gregor14046502008-10-23 00:40:37 +0000839
Douglas Gregor932778b2008-12-19 19:13:09 +0000840 return false;
841}
842
843/// isObjCPointerConversion - Determines whether this is an
844/// Objective-C pointer conversion. Subroutine of IsPointerConversion,
845/// with the same arguments and return values.
846bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType,
847 QualType& ConvertedType,
848 bool &IncompatibleObjC) {
849 if (!getLangOptions().ObjC1)
850 return false;
851
852 // Conversions with Objective-C's id<...>.
853 if ((FromType->isObjCQualifiedIdType() || ToType->isObjCQualifiedIdType()) &&
854 ObjCQualifiedIdTypesAreCompatible(ToType, FromType, /*compare=*/false)) {
855 ConvertedType = ToType;
856 return true;
857 }
858
859 const PointerType* ToTypePtr = ToType->getAsPointerType();
860 if (!ToTypePtr)
861 return false;
862
863 // Beyond this point, both types need to be pointers.
864 const PointerType *FromTypePtr = FromType->getAsPointerType();
865 if (!FromTypePtr)
866 return false;
867
868 QualType FromPointeeType = FromTypePtr->getPointeeType();
869 QualType ToPointeeType = ToTypePtr->getPointeeType();
870
Douglas Gregor24a90a52008-11-26 23:31:11 +0000871 // Objective C++: We're able to convert from a pointer to an
872 // interface to a pointer to a different interface.
873 const ObjCInterfaceType* FromIface = FromPointeeType->getAsObjCInterfaceType();
874 const ObjCInterfaceType* ToIface = ToPointeeType->getAsObjCInterfaceType();
875 if (FromIface && ToIface &&
876 Context.canAssignObjCInterfaces(ToIface, FromIface)) {
Douglas Gregor8bb7ad82008-11-27 00:52:49 +0000877 ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
878 ToPointeeType,
Douglas Gregor24a90a52008-11-26 23:31:11 +0000879 ToType, Context);
880 return true;
881 }
882
Douglas Gregor6fd35572008-12-19 17:40:08 +0000883 if (FromIface && ToIface &&
884 Context.canAssignObjCInterfaces(FromIface, ToIface)) {
885 // Okay: this is some kind of implicit downcast of Objective-C
886 // interfaces, which is permitted. However, we're going to
887 // complain about it.
888 IncompatibleObjC = true;
889 ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
890 ToPointeeType,
891 ToType, Context);
892 return true;
893 }
894
Douglas Gregor24a90a52008-11-26 23:31:11 +0000895 // Objective C++: We're able to convert between "id" and a pointer
896 // to any interface (in both directions).
897 if ((FromIface && Context.isObjCIdType(ToPointeeType))
898 || (ToIface && Context.isObjCIdType(FromPointeeType))) {
Douglas Gregor8bb7ad82008-11-27 00:52:49 +0000899 ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr,
900 ToPointeeType,
Douglas Gregor24a90a52008-11-26 23:31:11 +0000901 ToType, Context);
902 return true;
903 }
Douglas Gregor14046502008-10-23 00:40:37 +0000904
Douglas Gregord0c653a2008-12-18 23:43:31 +0000905 // Objective C++: Allow conversions between the Objective-C "id" and
906 // "Class", in either direction.
907 if ((Context.isObjCIdType(FromPointeeType) &&
908 Context.isObjCClassType(ToPointeeType)) ||
909 (Context.isObjCClassType(FromPointeeType) &&
910 Context.isObjCIdType(ToPointeeType))) {
911 ConvertedType = ToType;
912 return true;
913 }
914
Douglas Gregor932778b2008-12-19 19:13:09 +0000915 // If we have pointers to pointers, recursively check whether this
916 // is an Objective-C conversion.
917 if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() &&
918 isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType,
919 IncompatibleObjC)) {
920 // We always complain about this conversion.
921 IncompatibleObjC = true;
922 ConvertedType = ToType;
923 return true;
924 }
925
926 // If we have pointers to functions, check whether the only
927 // differences in the argument and result types are in Objective-C
928 // pointer conversions. If so, we permit the conversion (but
929 // complain about it).
930 const FunctionTypeProto *FromFunctionType
931 = FromPointeeType->getAsFunctionTypeProto();
932 const FunctionTypeProto *ToFunctionType
933 = ToPointeeType->getAsFunctionTypeProto();
934 if (FromFunctionType && ToFunctionType) {
935 // If the function types are exactly the same, this isn't an
936 // Objective-C pointer conversion.
937 if (Context.getCanonicalType(FromPointeeType)
938 == Context.getCanonicalType(ToPointeeType))
939 return false;
940
941 // Perform the quick checks that will tell us whether these
942 // function types are obviously different.
943 if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() ||
944 FromFunctionType->isVariadic() != ToFunctionType->isVariadic() ||
945 FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals())
946 return false;
947
948 bool HasObjCConversion = false;
949 if (Context.getCanonicalType(FromFunctionType->getResultType())
950 == Context.getCanonicalType(ToFunctionType->getResultType())) {
951 // Okay, the types match exactly. Nothing to do.
952 } else if (isObjCPointerConversion(FromFunctionType->getResultType(),
953 ToFunctionType->getResultType(),
954 ConvertedType, IncompatibleObjC)) {
955 // Okay, we have an Objective-C pointer conversion.
956 HasObjCConversion = true;
957 } else {
958 // Function types are too different. Abort.
959 return false;
960 }
961
962 // Check argument types.
963 for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
964 ArgIdx != NumArgs; ++ArgIdx) {
965 QualType FromArgType = FromFunctionType->getArgType(ArgIdx);
966 QualType ToArgType = ToFunctionType->getArgType(ArgIdx);
967 if (Context.getCanonicalType(FromArgType)
968 == Context.getCanonicalType(ToArgType)) {
969 // Okay, the types match exactly. Nothing to do.
970 } else if (isObjCPointerConversion(FromArgType, ToArgType,
971 ConvertedType, IncompatibleObjC)) {
972 // Okay, we have an Objective-C pointer conversion.
973 HasObjCConversion = true;
974 } else {
975 // Argument types are too different. Abort.
976 return false;
977 }
978 }
979
980 if (HasObjCConversion) {
981 // We had an Objective-C conversion. Allow this pointer
982 // conversion, but complain about it.
983 ConvertedType = ToType;
984 IncompatibleObjC = true;
985 return true;
986 }
987 }
988
989 return false;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000990}
991
Douglas Gregorbb461502008-10-24 04:54:22 +0000992/// CheckPointerConversion - Check the pointer conversion from the
993/// expression From to the type ToType. This routine checks for
994/// ambiguous (FIXME: or inaccessible) derived-to-base pointer
995/// conversions for which IsPointerConversion has already returned
996/// true. It returns true and produces a diagnostic if there was an
997/// error, or returns false otherwise.
998bool Sema::CheckPointerConversion(Expr *From, QualType ToType) {
999 QualType FromType = From->getType();
1000
1001 if (const PointerType *FromPtrType = FromType->getAsPointerType())
1002 if (const PointerType *ToPtrType = ToType->getAsPointerType()) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001003 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
1004 /*DetectVirtual=*/false);
Douglas Gregorbb461502008-10-24 04:54:22 +00001005 QualType FromPointeeType = FromPtrType->getPointeeType(),
1006 ToPointeeType = ToPtrType->getPointeeType();
Douglas Gregord0c653a2008-12-18 23:43:31 +00001007
1008 // Objective-C++ conversions are always okay.
1009 // FIXME: We should have a different class of conversions for
1010 // the Objective-C++ implicit conversions.
1011 if (Context.isObjCIdType(FromPointeeType) ||
1012 Context.isObjCIdType(ToPointeeType) ||
1013 Context.isObjCClassType(FromPointeeType) ||
1014 Context.isObjCClassType(ToPointeeType))
1015 return false;
1016
Douglas Gregorbb461502008-10-24 04:54:22 +00001017 if (FromPointeeType->isRecordType() &&
1018 ToPointeeType->isRecordType()) {
1019 // We must have a derived-to-base conversion. Check an
1020 // ambiguous or inaccessible conversion.
Douglas Gregor651d1cc2008-10-24 16:17:19 +00001021 return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType,
1022 From->getExprLoc(),
1023 From->getSourceRange());
Douglas Gregorbb461502008-10-24 04:54:22 +00001024 }
1025 }
1026
1027 return false;
1028}
1029
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001030/// IsQualificationConversion - Determines whether the conversion from
1031/// an rvalue of type FromType to ToType is a qualification conversion
1032/// (C++ 4.4).
1033bool
1034Sema::IsQualificationConversion(QualType FromType, QualType ToType)
1035{
1036 FromType = Context.getCanonicalType(FromType);
1037 ToType = Context.getCanonicalType(ToType);
1038
1039 // If FromType and ToType are the same type, this is not a
1040 // qualification conversion.
1041 if (FromType == ToType)
1042 return false;
1043
1044 // (C++ 4.4p4):
1045 // A conversion can add cv-qualifiers at levels other than the first
1046 // in multi-level pointers, subject to the following rules: [...]
1047 bool PreviousToQualsIncludeConst = true;
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001048 bool UnwrappedAnyPointer = false;
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001049 while (UnwrapSimilarPointerTypes(FromType, ToType)) {
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001050 // Within each iteration of the loop, we check the qualifiers to
1051 // determine if this still looks like a qualification
1052 // conversion. Then, if all is well, we unwrap one more level of
Douglas Gregorabed2172008-10-22 17:49:05 +00001053 // pointers or pointers-to-members and do it all again
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001054 // until there are no more pointers or pointers-to-members left to
1055 // unwrap.
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001056 UnwrappedAnyPointer = true;
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001057
1058 // -- for every j > 0, if const is in cv 1,j then const is in cv
1059 // 2,j, and similarly for volatile.
Douglas Gregore5db4f72008-10-22 00:38:21 +00001060 if (!ToType.isAtLeastAsQualifiedAs(FromType))
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001061 return false;
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001062
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001063 // -- if the cv 1,j and cv 2,j are different, then const is in
1064 // every cv for 0 < k < j.
1065 if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers()
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001066 && !PreviousToQualsIncludeConst)
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001067 return false;
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001068
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001069 // Keep track of whether all prior cv-qualifiers in the "to" type
1070 // include const.
1071 PreviousToQualsIncludeConst
1072 = PreviousToQualsIncludeConst && ToType.isConstQualified();
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001073 }
Douglas Gregor6573cfd2008-10-21 23:43:52 +00001074
1075 // We are left with FromType and ToType being the pointee types
1076 // after unwrapping the original FromType and ToType the same number
1077 // of types. If we unwrapped any pointers, and if FromType and
1078 // ToType have the same unqualified type (since we checked
1079 // qualifiers above), then this is a qualification conversion.
1080 return UnwrappedAnyPointer &&
1081 FromType.getUnqualifiedType() == ToType.getUnqualifiedType();
1082}
1083
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001084/// IsUserDefinedConversion - Determines whether there is a
1085/// user-defined conversion sequence (C++ [over.ics.user]) that
1086/// converts expression From to the type ToType. If such a conversion
1087/// exists, User will contain the user-defined conversion sequence
1088/// that performs such a conversion and this routine will return
1089/// true. Otherwise, this routine returns false and User is
1090/// unspecified.
1091bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType,
1092 UserDefinedConversionSequence& User)
1093{
1094 OverloadCandidateSet CandidateSet;
1095 if (const CXXRecordType *ToRecordType
1096 = dyn_cast_or_null<CXXRecordType>(ToType->getAsRecordType())) {
1097 // C++ [over.match.ctor]p1:
1098 // When objects of class type are direct-initialized (8.5), or
1099 // copy-initialized from an expression of the same or a
1100 // derived class type (8.5), overload resolution selects the
1101 // constructor. [...] For copy-initialization, the candidate
1102 // functions are all the converting constructors (12.3.1) of
1103 // that class. The argument list is the expression-list within
1104 // the parentheses of the initializer.
1105 CXXRecordDecl *ToRecordDecl = ToRecordType->getDecl();
Douglas Gregorb9213832008-12-15 21:24:18 +00001106 DeclarationName ConstructorName
1107 = Context.DeclarationNames.getCXXConstructorName(
1108 Context.getCanonicalType(ToType));
1109 DeclContext::lookup_result Lookup
1110 = ToRecordDecl->lookup(Context, ConstructorName);
1111 if (Lookup.first == Lookup.second)
1112 /* No constructors. FIXME: Implicit copy constructor? */;
1113 else if (OverloadedFunctionDecl *Constructors
1114 = dyn_cast<OverloadedFunctionDecl>(*Lookup.first)) {
1115 for (OverloadedFunctionDecl::function_const_iterator func
1116 = Constructors->function_begin();
1117 func != Constructors->function_end(); ++func) {
1118 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*func);
1119 if (Constructor->isConvertingConstructor())
1120 AddOverloadCandidate(Constructor, &From, 1, CandidateSet,
1121 /*SuppressUserConversions=*/true);
1122 }
1123 } else if (CXXConstructorDecl *Constructor
1124 = dyn_cast<CXXConstructorDecl>(*Lookup.first)) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001125 if (Constructor->isConvertingConstructor())
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001126 AddOverloadCandidate(Constructor, &From, 1, CandidateSet,
1127 /*SuppressUserConversions=*/true);
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001128 }
1129 }
1130
Douglas Gregor60714f92008-11-07 22:36:19 +00001131 if (const CXXRecordType *FromRecordType
1132 = dyn_cast_or_null<CXXRecordType>(From->getType()->getAsRecordType())) {
1133 // Add all of the conversion functions as candidates.
1134 // FIXME: Look for conversions in base classes!
1135 CXXRecordDecl *FromRecordDecl = FromRecordType->getDecl();
1136 OverloadedFunctionDecl *Conversions
1137 = FromRecordDecl->getConversionFunctions();
1138 for (OverloadedFunctionDecl::function_iterator Func
1139 = Conversions->function_begin();
1140 Func != Conversions->function_end(); ++Func) {
1141 CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
1142 AddConversionCandidate(Conv, From, ToType, CandidateSet);
1143 }
1144 }
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001145
1146 OverloadCandidateSet::iterator Best;
1147 switch (BestViableFunction(CandidateSet, Best)) {
1148 case OR_Success:
1149 // Record the standard conversion we used and the conversion function.
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001150 if (CXXConstructorDecl *Constructor
1151 = dyn_cast<CXXConstructorDecl>(Best->Function)) {
1152 // C++ [over.ics.user]p1:
1153 // If the user-defined conversion is specified by a
1154 // constructor (12.3.1), the initial standard conversion
1155 // sequence converts the source type to the type required by
1156 // the argument of the constructor.
1157 //
1158 // FIXME: What about ellipsis conversions?
1159 QualType ThisType = Constructor->getThisType(Context);
1160 User.Before = Best->Conversions[0].Standard;
1161 User.ConversionFunction = Constructor;
1162 User.After.setAsIdentityConversion();
1163 User.After.FromTypePtr
1164 = ThisType->getAsPointerType()->getPointeeType().getAsOpaquePtr();
1165 User.After.ToTypePtr = ToType.getAsOpaquePtr();
1166 return true;
Douglas Gregor60714f92008-11-07 22:36:19 +00001167 } else if (CXXConversionDecl *Conversion
1168 = dyn_cast<CXXConversionDecl>(Best->Function)) {
1169 // C++ [over.ics.user]p1:
1170 //
1171 // [...] If the user-defined conversion is specified by a
1172 // conversion function (12.3.2), the initial standard
1173 // conversion sequence converts the source type to the
1174 // implicit object parameter of the conversion function.
1175 User.Before = Best->Conversions[0].Standard;
1176 User.ConversionFunction = Conversion;
1177
1178 // C++ [over.ics.user]p2:
1179 // The second standard conversion sequence converts the
1180 // result of the user-defined conversion to the target type
1181 // for the sequence. Since an implicit conversion sequence
1182 // is an initialization, the special rules for
1183 // initialization by user-defined conversion apply when
1184 // selecting the best user-defined conversion for a
1185 // user-defined conversion sequence (see 13.3.3 and
1186 // 13.3.3.1).
1187 User.After = Best->FinalConversion;
1188 return true;
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001189 } else {
Douglas Gregor60714f92008-11-07 22:36:19 +00001190 assert(false && "Not a constructor or conversion function?");
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001191 return false;
1192 }
1193
1194 case OR_No_Viable_Function:
1195 // No conversion here! We're done.
1196 return false;
1197
1198 case OR_Ambiguous:
1199 // FIXME: See C++ [over.best.ics]p10 for the handling of
1200 // ambiguous conversion sequences.
1201 return false;
1202 }
1203
1204 return false;
1205}
1206
Douglas Gregord2baafd2008-10-21 16:13:35 +00001207/// CompareImplicitConversionSequences - Compare two implicit
1208/// conversion sequences to determine whether one is better than the
1209/// other or if they are indistinguishable (C++ 13.3.3.2).
1210ImplicitConversionSequence::CompareKind
1211Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1,
1212 const ImplicitConversionSequence& ICS2)
1213{
1214 // (C++ 13.3.3.2p2): When comparing the basic forms of implicit
1215 // conversion sequences (as defined in 13.3.3.1)
1216 // -- a standard conversion sequence (13.3.3.1.1) is a better
1217 // conversion sequence than a user-defined conversion sequence or
1218 // an ellipsis conversion sequence, and
1219 // -- a user-defined conversion sequence (13.3.3.1.2) is a better
1220 // conversion sequence than an ellipsis conversion sequence
1221 // (13.3.3.1.3).
1222 //
1223 if (ICS1.ConversionKind < ICS2.ConversionKind)
1224 return ImplicitConversionSequence::Better;
1225 else if (ICS2.ConversionKind < ICS1.ConversionKind)
1226 return ImplicitConversionSequence::Worse;
1227
1228 // Two implicit conversion sequences of the same form are
1229 // indistinguishable conversion sequences unless one of the
1230 // following rules apply: (C++ 13.3.3.2p3):
1231 if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion)
1232 return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard);
1233 else if (ICS1.ConversionKind ==
1234 ImplicitConversionSequence::UserDefinedConversion) {
1235 // User-defined conversion sequence U1 is a better conversion
1236 // sequence than another user-defined conversion sequence U2 if
1237 // they contain the same user-defined conversion function or
1238 // constructor and if the second standard conversion sequence of
1239 // U1 is better than the second standard conversion sequence of
1240 // U2 (C++ 13.3.3.2p3).
1241 if (ICS1.UserDefined.ConversionFunction ==
1242 ICS2.UserDefined.ConversionFunction)
1243 return CompareStandardConversionSequences(ICS1.UserDefined.After,
1244 ICS2.UserDefined.After);
1245 }
1246
1247 return ImplicitConversionSequence::Indistinguishable;
1248}
1249
1250/// CompareStandardConversionSequences - Compare two standard
1251/// conversion sequences to determine whether one is better than the
1252/// other or if they are indistinguishable (C++ 13.3.3.2p3).
1253ImplicitConversionSequence::CompareKind
1254Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1,
1255 const StandardConversionSequence& SCS2)
1256{
1257 // Standard conversion sequence S1 is a better conversion sequence
1258 // than standard conversion sequence S2 if (C++ 13.3.3.2p3):
1259
1260 // -- S1 is a proper subsequence of S2 (comparing the conversion
1261 // sequences in the canonical form defined by 13.3.3.1.1,
1262 // excluding any Lvalue Transformation; the identity conversion
1263 // sequence is considered to be a subsequence of any
1264 // non-identity conversion sequence) or, if not that,
1265 if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third)
1266 // Neither is a proper subsequence of the other. Do nothing.
1267 ;
1268 else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) ||
1269 (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) ||
1270 (SCS1.Second == ICK_Identity &&
1271 SCS1.Third == ICK_Identity))
1272 // SCS1 is a proper subsequence of SCS2.
1273 return ImplicitConversionSequence::Better;
1274 else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) ||
1275 (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) ||
1276 (SCS2.Second == ICK_Identity &&
1277 SCS2.Third == ICK_Identity))
1278 // SCS2 is a proper subsequence of SCS1.
1279 return ImplicitConversionSequence::Worse;
1280
1281 // -- the rank of S1 is better than the rank of S2 (by the rules
1282 // defined below), or, if not that,
1283 ImplicitConversionRank Rank1 = SCS1.getRank();
1284 ImplicitConversionRank Rank2 = SCS2.getRank();
1285 if (Rank1 < Rank2)
1286 return ImplicitConversionSequence::Better;
1287 else if (Rank2 < Rank1)
1288 return ImplicitConversionSequence::Worse;
Douglas Gregord2baafd2008-10-21 16:13:35 +00001289
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001290 // (C++ 13.3.3.2p4): Two conversion sequences with the same rank
1291 // are indistinguishable unless one of the following rules
1292 // applies:
1293
1294 // A conversion that is not a conversion of a pointer, or
1295 // pointer to member, to bool is better than another conversion
1296 // that is such a conversion.
1297 if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool())
1298 return SCS2.isPointerConversionToBool()
1299 ? ImplicitConversionSequence::Better
1300 : ImplicitConversionSequence::Worse;
1301
Douglas Gregor14046502008-10-23 00:40:37 +00001302 // C++ [over.ics.rank]p4b2:
1303 //
1304 // If class B is derived directly or indirectly from class A,
Douglas Gregor0e343382008-10-29 14:50:44 +00001305 // conversion of B* to A* is better than conversion of B* to
1306 // void*, and conversion of A* to void* is better than conversion
1307 // of B* to void*.
Douglas Gregor14046502008-10-23 00:40:37 +00001308 bool SCS1ConvertsToVoid
1309 = SCS1.isPointerConversionToVoidPointer(Context);
1310 bool SCS2ConvertsToVoid
1311 = SCS2.isPointerConversionToVoidPointer(Context);
Douglas Gregor0e343382008-10-29 14:50:44 +00001312 if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) {
1313 // Exactly one of the conversion sequences is a conversion to
1314 // a void pointer; it's the worse conversion.
Douglas Gregor14046502008-10-23 00:40:37 +00001315 return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better
1316 : ImplicitConversionSequence::Worse;
Douglas Gregor0e343382008-10-29 14:50:44 +00001317 } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) {
1318 // Neither conversion sequence converts to a void pointer; compare
1319 // their derived-to-base conversions.
Douglas Gregor14046502008-10-23 00:40:37 +00001320 if (ImplicitConversionSequence::CompareKind DerivedCK
1321 = CompareDerivedToBaseConversions(SCS1, SCS2))
1322 return DerivedCK;
Douglas Gregor0e343382008-10-29 14:50:44 +00001323 } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) {
1324 // Both conversion sequences are conversions to void
1325 // pointers. Compare the source types to determine if there's an
1326 // inheritance relationship in their sources.
1327 QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr);
1328 QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr);
1329
1330 // Adjust the types we're converting from via the array-to-pointer
1331 // conversion, if we need to.
1332 if (SCS1.First == ICK_Array_To_Pointer)
1333 FromType1 = Context.getArrayDecayedType(FromType1);
1334 if (SCS2.First == ICK_Array_To_Pointer)
1335 FromType2 = Context.getArrayDecayedType(FromType2);
1336
1337 QualType FromPointee1
1338 = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType();
1339 QualType FromPointee2
1340 = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType();
1341
1342 if (IsDerivedFrom(FromPointee2, FromPointee1))
1343 return ImplicitConversionSequence::Better;
1344 else if (IsDerivedFrom(FromPointee1, FromPointee2))
1345 return ImplicitConversionSequence::Worse;
Douglas Gregor24a90a52008-11-26 23:31:11 +00001346
1347 // Objective-C++: If one interface is more specific than the
1348 // other, it is the better one.
1349 const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType();
1350 const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType();
1351 if (FromIface1 && FromIface1) {
1352 if (Context.canAssignObjCInterfaces(FromIface2, FromIface1))
1353 return ImplicitConversionSequence::Better;
1354 else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2))
1355 return ImplicitConversionSequence::Worse;
1356 }
Douglas Gregor0e343382008-10-29 14:50:44 +00001357 }
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001358
1359 // Compare based on qualification conversions (C++ 13.3.3.2p3,
1360 // bullet 3).
Douglas Gregor14046502008-10-23 00:40:37 +00001361 if (ImplicitConversionSequence::CompareKind QualCK
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001362 = CompareQualificationConversions(SCS1, SCS2))
Douglas Gregor14046502008-10-23 00:40:37 +00001363 return QualCK;
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001364
Douglas Gregor0e343382008-10-29 14:50:44 +00001365 // C++ [over.ics.rank]p3b4:
1366 // -- S1 and S2 are reference bindings (8.5.3), and the types to
1367 // which the references refer are the same type except for
1368 // top-level cv-qualifiers, and the type to which the reference
1369 // initialized by S2 refers is more cv-qualified than the type
1370 // to which the reference initialized by S1 refers.
1371 if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) {
1372 QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr);
1373 QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr);
1374 T1 = Context.getCanonicalType(T1);
1375 T2 = Context.getCanonicalType(T2);
1376 if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) {
1377 if (T2.isMoreQualifiedThan(T1))
1378 return ImplicitConversionSequence::Better;
1379 else if (T1.isMoreQualifiedThan(T2))
1380 return ImplicitConversionSequence::Worse;
1381 }
1382 }
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001383
1384 return ImplicitConversionSequence::Indistinguishable;
1385}
1386
1387/// CompareQualificationConversions - Compares two standard conversion
1388/// sequences to determine whether they can be ranked based on their
1389/// qualification conversions (C++ 13.3.3.2p3 bullet 3).
1390ImplicitConversionSequence::CompareKind
1391Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1,
1392 const StandardConversionSequence& SCS2)
1393{
Douglas Gregor4459bbe2008-10-22 15:04:37 +00001394 // C++ 13.3.3.2p3:
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001395 // -- S1 and S2 differ only in their qualification conversion and
1396 // yield similar types T1 and T2 (C++ 4.4), respectively, and the
1397 // cv-qualification signature of type T1 is a proper subset of
1398 // the cv-qualification signature of type T2, and S1 is not the
1399 // deprecated string literal array-to-pointer conversion (4.2).
1400 if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second ||
1401 SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification)
1402 return ImplicitConversionSequence::Indistinguishable;
1403
1404 // FIXME: the example in the standard doesn't use a qualification
1405 // conversion (!)
1406 QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr);
1407 QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr);
1408 T1 = Context.getCanonicalType(T1);
1409 T2 = Context.getCanonicalType(T2);
1410
1411 // If the types are the same, we won't learn anything by unwrapped
1412 // them.
1413 if (T1.getUnqualifiedType() == T2.getUnqualifiedType())
1414 return ImplicitConversionSequence::Indistinguishable;
1415
1416 ImplicitConversionSequence::CompareKind Result
1417 = ImplicitConversionSequence::Indistinguishable;
1418 while (UnwrapSimilarPointerTypes(T1, T2)) {
1419 // Within each iteration of the loop, we check the qualifiers to
1420 // determine if this still looks like a qualification
1421 // conversion. Then, if all is well, we unwrap one more level of
Douglas Gregorabed2172008-10-22 17:49:05 +00001422 // pointers or pointers-to-members and do it all again
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001423 // until there are no more pointers or pointers-to-members left
1424 // to unwrap. This essentially mimics what
1425 // IsQualificationConversion does, but here we're checking for a
1426 // strict subset of qualifiers.
1427 if (T1.getCVRQualifiers() == T2.getCVRQualifiers())
1428 // The qualifiers are the same, so this doesn't tell us anything
1429 // about how the sequences rank.
1430 ;
1431 else if (T2.isMoreQualifiedThan(T1)) {
1432 // T1 has fewer qualifiers, so it could be the better sequence.
1433 if (Result == ImplicitConversionSequence::Worse)
1434 // Neither has qualifiers that are a subset of the other's
1435 // qualifiers.
1436 return ImplicitConversionSequence::Indistinguishable;
1437
1438 Result = ImplicitConversionSequence::Better;
1439 } else if (T1.isMoreQualifiedThan(T2)) {
1440 // T2 has fewer qualifiers, so it could be the better sequence.
1441 if (Result == ImplicitConversionSequence::Better)
1442 // Neither has qualifiers that are a subset of the other's
1443 // qualifiers.
1444 return ImplicitConversionSequence::Indistinguishable;
1445
1446 Result = ImplicitConversionSequence::Worse;
1447 } else {
1448 // Qualifiers are disjoint.
1449 return ImplicitConversionSequence::Indistinguishable;
1450 }
1451
1452 // If the types after this point are equivalent, we're done.
1453 if (T1.getUnqualifiedType() == T2.getUnqualifiedType())
1454 break;
Douglas Gregord2baafd2008-10-21 16:13:35 +00001455 }
1456
Douglas Gregorccc0ccc2008-10-22 14:17:15 +00001457 // Check that the winning standard conversion sequence isn't using
1458 // the deprecated string literal array to pointer conversion.
1459 switch (Result) {
1460 case ImplicitConversionSequence::Better:
1461 if (SCS1.Deprecated)
1462 Result = ImplicitConversionSequence::Indistinguishable;
1463 break;
1464
1465 case ImplicitConversionSequence::Indistinguishable:
1466 break;
1467
1468 case ImplicitConversionSequence::Worse:
1469 if (SCS2.Deprecated)
1470 Result = ImplicitConversionSequence::Indistinguishable;
1471 break;
1472 }
1473
1474 return Result;
Douglas Gregord2baafd2008-10-21 16:13:35 +00001475}
1476
Douglas Gregor14046502008-10-23 00:40:37 +00001477/// CompareDerivedToBaseConversions - Compares two standard conversion
1478/// sequences to determine whether they can be ranked based on their
Douglas Gregor24a90a52008-11-26 23:31:11 +00001479/// various kinds of derived-to-base conversions (C++
1480/// [over.ics.rank]p4b3). As part of these checks, we also look at
1481/// conversions between Objective-C interface types.
Douglas Gregor14046502008-10-23 00:40:37 +00001482ImplicitConversionSequence::CompareKind
1483Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1,
1484 const StandardConversionSequence& SCS2) {
1485 QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr);
1486 QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr);
1487 QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr);
1488 QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr);
1489
1490 // Adjust the types we're converting from via the array-to-pointer
1491 // conversion, if we need to.
1492 if (SCS1.First == ICK_Array_To_Pointer)
1493 FromType1 = Context.getArrayDecayedType(FromType1);
1494 if (SCS2.First == ICK_Array_To_Pointer)
1495 FromType2 = Context.getArrayDecayedType(FromType2);
1496
1497 // Canonicalize all of the types.
1498 FromType1 = Context.getCanonicalType(FromType1);
1499 ToType1 = Context.getCanonicalType(ToType1);
1500 FromType2 = Context.getCanonicalType(FromType2);
1501 ToType2 = Context.getCanonicalType(ToType2);
1502
Douglas Gregor0e343382008-10-29 14:50:44 +00001503 // C++ [over.ics.rank]p4b3:
Douglas Gregor14046502008-10-23 00:40:37 +00001504 //
1505 // If class B is derived directly or indirectly from class A and
1506 // class C is derived directly or indirectly from B,
Douglas Gregor24a90a52008-11-26 23:31:11 +00001507 //
1508 // For Objective-C, we let A, B, and C also be Objective-C
1509 // interfaces.
Douglas Gregor0e343382008-10-29 14:50:44 +00001510
1511 // Compare based on pointer conversions.
Douglas Gregor14046502008-10-23 00:40:37 +00001512 if (SCS1.Second == ICK_Pointer_Conversion &&
Douglas Gregor3f5a00c2008-11-27 01:19:21 +00001513 SCS2.Second == ICK_Pointer_Conversion &&
1514 /*FIXME: Remove if Objective-C id conversions get their own rank*/
1515 FromType1->isPointerType() && FromType2->isPointerType() &&
1516 ToType1->isPointerType() && ToType2->isPointerType()) {
Douglas Gregor14046502008-10-23 00:40:37 +00001517 QualType FromPointee1
1518 = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType();
1519 QualType ToPointee1
1520 = ToType1->getAsPointerType()->getPointeeType().getUnqualifiedType();
1521 QualType FromPointee2
1522 = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType();
1523 QualType ToPointee2
1524 = ToType2->getAsPointerType()->getPointeeType().getUnqualifiedType();
Douglas Gregor24a90a52008-11-26 23:31:11 +00001525
1526 const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType();
1527 const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType();
1528 const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType();
1529 const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType();
1530
Douglas Gregor0e343382008-10-29 14:50:44 +00001531 // -- conversion of C* to B* is better than conversion of C* to A*,
Douglas Gregor14046502008-10-23 00:40:37 +00001532 if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) {
1533 if (IsDerivedFrom(ToPointee1, ToPointee2))
1534 return ImplicitConversionSequence::Better;
1535 else if (IsDerivedFrom(ToPointee2, ToPointee1))
1536 return ImplicitConversionSequence::Worse;
Douglas Gregor24a90a52008-11-26 23:31:11 +00001537
1538 if (ToIface1 && ToIface2) {
1539 if (Context.canAssignObjCInterfaces(ToIface2, ToIface1))
1540 return ImplicitConversionSequence::Better;
1541 else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2))
1542 return ImplicitConversionSequence::Worse;
1543 }
Douglas Gregor14046502008-10-23 00:40:37 +00001544 }
Douglas Gregor0e343382008-10-29 14:50:44 +00001545
1546 // -- conversion of B* to A* is better than conversion of C* to A*,
1547 if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) {
1548 if (IsDerivedFrom(FromPointee2, FromPointee1))
1549 return ImplicitConversionSequence::Better;
1550 else if (IsDerivedFrom(FromPointee1, FromPointee2))
1551 return ImplicitConversionSequence::Worse;
Douglas Gregor24a90a52008-11-26 23:31:11 +00001552
1553 if (FromIface1 && FromIface2) {
1554 if (Context.canAssignObjCInterfaces(FromIface1, FromIface2))
1555 return ImplicitConversionSequence::Better;
1556 else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1))
1557 return ImplicitConversionSequence::Worse;
1558 }
Douglas Gregor0e343382008-10-29 14:50:44 +00001559 }
Douglas Gregor14046502008-10-23 00:40:37 +00001560 }
1561
Douglas Gregor0e343382008-10-29 14:50:44 +00001562 // Compare based on reference bindings.
1563 if (SCS1.ReferenceBinding && SCS2.ReferenceBinding &&
1564 SCS1.Second == ICK_Derived_To_Base) {
1565 // -- binding of an expression of type C to a reference of type
1566 // B& is better than binding an expression of type C to a
1567 // reference of type A&,
1568 if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() &&
1569 ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) {
1570 if (IsDerivedFrom(ToType1, ToType2))
1571 return ImplicitConversionSequence::Better;
1572 else if (IsDerivedFrom(ToType2, ToType1))
1573 return ImplicitConversionSequence::Worse;
1574 }
1575
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001576 // -- binding of an expression of type B to a reference of type
1577 // A& is better than binding an expression of type C to a
1578 // reference of type A&,
Douglas Gregor0e343382008-10-29 14:50:44 +00001579 if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() &&
1580 ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) {
1581 if (IsDerivedFrom(FromType2, FromType1))
1582 return ImplicitConversionSequence::Better;
1583 else if (IsDerivedFrom(FromType1, FromType2))
1584 return ImplicitConversionSequence::Worse;
1585 }
1586 }
1587
1588
1589 // FIXME: conversion of A::* to B::* is better than conversion of
1590 // A::* to C::*,
1591
1592 // FIXME: conversion of B::* to C::* is better than conversion of
1593 // A::* to C::*, and
1594
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001595 if (SCS1.CopyConstructor && SCS2.CopyConstructor &&
1596 SCS1.Second == ICK_Derived_To_Base) {
1597 // -- conversion of C to B is better than conversion of C to A,
1598 if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() &&
1599 ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) {
1600 if (IsDerivedFrom(ToType1, ToType2))
1601 return ImplicitConversionSequence::Better;
1602 else if (IsDerivedFrom(ToType2, ToType1))
1603 return ImplicitConversionSequence::Worse;
1604 }
Douglas Gregor0e343382008-10-29 14:50:44 +00001605
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001606 // -- conversion of B to A is better than conversion of C to A.
1607 if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() &&
1608 ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) {
1609 if (IsDerivedFrom(FromType2, FromType1))
1610 return ImplicitConversionSequence::Better;
1611 else if (IsDerivedFrom(FromType1, FromType2))
1612 return ImplicitConversionSequence::Worse;
1613 }
1614 }
Douglas Gregor0e343382008-10-29 14:50:44 +00001615
Douglas Gregor14046502008-10-23 00:40:37 +00001616 return ImplicitConversionSequence::Indistinguishable;
1617}
1618
Douglas Gregor81c29152008-10-29 00:13:59 +00001619/// TryCopyInitialization - Try to copy-initialize a value of type
1620/// ToType from the expression From. Return the implicit conversion
1621/// sequence required to pass this argument, which may be a bad
1622/// conversion sequence (meaning that the argument cannot be passed to
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001623/// a parameter of this type). If @p SuppressUserConversions, then we
1624/// do not permit any user-defined conversion sequences.
Douglas Gregor81c29152008-10-29 00:13:59 +00001625ImplicitConversionSequence
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001626Sema::TryCopyInitialization(Expr *From, QualType ToType,
1627 bool SuppressUserConversions) {
Douglas Gregor81c29152008-10-29 00:13:59 +00001628 if (!getLangOptions().CPlusPlus) {
Douglas Gregorb72e9da2008-10-31 16:23:19 +00001629 // In C, copy initialization is the same as performing an assignment.
Douglas Gregor81c29152008-10-29 00:13:59 +00001630 AssignConvertType ConvTy =
1631 CheckSingleAssignmentConstraints(ToType, From);
1632 ImplicitConversionSequence ICS;
1633 if (getLangOptions().NoExtensions? ConvTy != Compatible
1634 : ConvTy == Incompatible)
1635 ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
1636 else
1637 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
1638 return ICS;
1639 } else if (ToType->isReferenceType()) {
1640 ImplicitConversionSequence ICS;
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001641 CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions);
Douglas Gregor81c29152008-10-29 00:13:59 +00001642 return ICS;
1643 } else {
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001644 return TryImplicitConversion(From, ToType, SuppressUserConversions);
Douglas Gregor81c29152008-10-29 00:13:59 +00001645 }
1646}
1647
1648/// PerformArgumentPassing - Pass the argument Arg into a parameter of
1649/// type ToType. Returns true (and emits a diagnostic) if there was
1650/// an error, returns false if the initialization succeeded.
1651bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType,
1652 const char* Flavor) {
1653 if (!getLangOptions().CPlusPlus) {
1654 // In C, argument passing is the same as performing an assignment.
1655 QualType FromType = From->getType();
1656 AssignConvertType ConvTy =
1657 CheckSingleAssignmentConstraints(ToType, From);
1658
1659 return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType,
1660 FromType, From, Flavor);
Douglas Gregor81c29152008-10-29 00:13:59 +00001661 }
Chris Lattner271d4c22008-11-24 05:29:24 +00001662
1663 if (ToType->isReferenceType())
1664 return CheckReferenceInit(From, ToType);
1665
Douglas Gregor6fd35572008-12-19 17:40:08 +00001666 if (!PerformImplicitConversion(From, ToType, Flavor))
Chris Lattner271d4c22008-11-24 05:29:24 +00001667 return false;
1668
1669 return Diag(From->getSourceRange().getBegin(),
1670 diag::err_typecheck_convert_incompatible)
1671 << ToType << From->getType() << Flavor << From->getSourceRange();
Douglas Gregor81c29152008-10-29 00:13:59 +00001672}
1673
Douglas Gregor5ed15042008-11-18 23:14:02 +00001674/// TryObjectArgumentInitialization - Try to initialize the object
1675/// parameter of the given member function (@c Method) from the
1676/// expression @p From.
1677ImplicitConversionSequence
1678Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) {
1679 QualType ClassType = Context.getTypeDeclType(Method->getParent());
1680 unsigned MethodQuals = Method->getTypeQualifiers();
1681 QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals);
1682
1683 // Set up the conversion sequence as a "bad" conversion, to allow us
1684 // to exit early.
1685 ImplicitConversionSequence ICS;
1686 ICS.Standard.setAsIdentityConversion();
1687 ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
1688
1689 // We need to have an object of class type.
1690 QualType FromType = From->getType();
1691 if (!FromType->isRecordType())
1692 return ICS;
1693
1694 // The implicit object parmeter is has the type "reference to cv X",
1695 // where X is the class of which the function is a member
1696 // (C++ [over.match.funcs]p4). However, when finding an implicit
1697 // conversion sequence for the argument, we are not allowed to
1698 // create temporaries or perform user-defined conversions
1699 // (C++ [over.match.funcs]p5). We perform a simplified version of
1700 // reference binding here, that allows class rvalues to bind to
1701 // non-constant references.
1702
1703 // First check the qualifiers. We don't care about lvalue-vs-rvalue
1704 // with the implicit object parameter (C++ [over.match.funcs]p5).
1705 QualType FromTypeCanon = Context.getCanonicalType(FromType);
1706 if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() &&
1707 !ImplicitParamType.isAtLeastAsQualifiedAs(FromType))
1708 return ICS;
1709
1710 // Check that we have either the same type or a derived type. It
1711 // affects the conversion rank.
1712 QualType ClassTypeCanon = Context.getCanonicalType(ClassType);
1713 if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType())
1714 ICS.Standard.Second = ICK_Identity;
1715 else if (IsDerivedFrom(FromType, ClassType))
1716 ICS.Standard.Second = ICK_Derived_To_Base;
1717 else
1718 return ICS;
1719
1720 // Success. Mark this as a reference binding.
1721 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
1722 ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr();
1723 ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr();
1724 ICS.Standard.ReferenceBinding = true;
1725 ICS.Standard.DirectBinding = true;
1726 return ICS;
1727}
1728
1729/// PerformObjectArgumentInitialization - Perform initialization of
1730/// the implicit object parameter for the given Method with the given
1731/// expression.
1732bool
1733Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) {
1734 QualType ImplicitParamType
1735 = Method->getThisType(Context)->getAsPointerType()->getPointeeType();
1736 ImplicitConversionSequence ICS
1737 = TryObjectArgumentInitialization(From, Method);
1738 if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion)
1739 return Diag(From->getSourceRange().getBegin(),
Chris Lattner8ba580c2008-11-19 05:08:23 +00001740 diag::err_implicit_object_parameter_init)
Chris Lattner4bfd2232008-11-24 06:25:27 +00001741 << ImplicitParamType << From->getType() << From->getSourceRange();
Douglas Gregor5ed15042008-11-18 23:14:02 +00001742
1743 if (ICS.Standard.Second == ICK_Derived_To_Base &&
1744 CheckDerivedToBaseConversion(From->getType(), ImplicitParamType,
1745 From->getSourceRange().getBegin(),
1746 From->getSourceRange()))
1747 return true;
1748
1749 ImpCastExprToType(From, ImplicitParamType, /*isLvalue=*/true);
1750 return false;
1751}
1752
Douglas Gregord2baafd2008-10-21 16:13:35 +00001753/// AddOverloadCandidate - Adds the given function to the set of
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001754/// candidate functions, using the given function call arguments. If
1755/// @p SuppressUserConversions, then don't allow user-defined
1756/// conversions via constructors or conversion operators.
Douglas Gregord2baafd2008-10-21 16:13:35 +00001757void
1758Sema::AddOverloadCandidate(FunctionDecl *Function,
1759 Expr **Args, unsigned NumArgs,
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001760 OverloadCandidateSet& CandidateSet,
1761 bool SuppressUserConversions)
Douglas Gregord2baafd2008-10-21 16:13:35 +00001762{
1763 const FunctionTypeProto* Proto
1764 = dyn_cast<FunctionTypeProto>(Function->getType()->getAsFunctionType());
1765 assert(Proto && "Functions without a prototype cannot be overloaded");
Douglas Gregor60714f92008-11-07 22:36:19 +00001766 assert(!isa<CXXConversionDecl>(Function) &&
1767 "Use AddConversionCandidate for conversion functions");
Douglas Gregord2baafd2008-10-21 16:13:35 +00001768
Douglas Gregor3257fb52008-12-22 05:46:06 +00001769 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) {
1770 // If we get here, it's because we're calling a member function
1771 // that is named without a member access expression (e.g.,
1772 // "this->f") that was either written explicitly or created
1773 // implicitly. This can happen with a qualified call to a member
1774 // function, e.g., X::f(). We use a NULL object as the implied
1775 // object argument (C++ [over.call.func]p3).
1776 AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet,
1777 SuppressUserConversions);
1778 return;
1779 }
1780
1781
Douglas Gregord2baafd2008-10-21 16:13:35 +00001782 // Add this candidate
1783 CandidateSet.push_back(OverloadCandidate());
1784 OverloadCandidate& Candidate = CandidateSet.back();
1785 Candidate.Function = Function;
Douglas Gregor3257fb52008-12-22 05:46:06 +00001786 Candidate.Viable = true;
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00001787 Candidate.IsSurrogate = false;
Douglas Gregor3257fb52008-12-22 05:46:06 +00001788 Candidate.IgnoreObjectArgument = false;
Douglas Gregord2baafd2008-10-21 16:13:35 +00001789
1790 unsigned NumArgsInProto = Proto->getNumArgs();
1791
1792 // (C++ 13.3.2p2): A candidate function having fewer than m
1793 // parameters is viable only if it has an ellipsis in its parameter
1794 // list (8.3.5).
1795 if (NumArgs > NumArgsInProto && !Proto->isVariadic()) {
1796 Candidate.Viable = false;
1797 return;
1798 }
1799
1800 // (C++ 13.3.2p2): A candidate function having more than m parameters
1801 // is viable only if the (m+1)st parameter has a default argument
1802 // (8.3.6). For the purposes of overload resolution, the
1803 // parameter list is truncated on the right, so that there are
1804 // exactly m parameters.
1805 unsigned MinRequiredArgs = Function->getMinRequiredArguments();
1806 if (NumArgs < MinRequiredArgs) {
1807 // Not enough arguments.
1808 Candidate.Viable = false;
1809 return;
1810 }
1811
1812 // Determine the implicit conversion sequences for each of the
1813 // arguments.
Douglas Gregord2baafd2008-10-21 16:13:35 +00001814 Candidate.Conversions.resize(NumArgs);
1815 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1816 if (ArgIdx < NumArgsInProto) {
1817 // (C++ 13.3.2p3): for F to be a viable function, there shall
1818 // exist for each argument an implicit conversion sequence
1819 // (13.3.3.1) that converts that argument to the corresponding
1820 // parameter of F.
1821 QualType ParamType = Proto->getArgType(ArgIdx);
1822 Candidate.Conversions[ArgIdx]
Douglas Gregora3b34bb2008-11-03 19:09:14 +00001823 = TryCopyInitialization(Args[ArgIdx], ParamType,
1824 SuppressUserConversions);
Douglas Gregord2baafd2008-10-21 16:13:35 +00001825 if (Candidate.Conversions[ArgIdx].ConversionKind
Douglas Gregor5ed15042008-11-18 23:14:02 +00001826 == ImplicitConversionSequence::BadConversion) {
Douglas Gregord2baafd2008-10-21 16:13:35 +00001827 Candidate.Viable = false;
Douglas Gregor5ed15042008-11-18 23:14:02 +00001828 break;
1829 }
Douglas Gregord2baafd2008-10-21 16:13:35 +00001830 } else {
1831 // (C++ 13.3.2p2): For the purposes of overload resolution, any
1832 // argument for which there is no corresponding parameter is
1833 // considered to ""match the ellipsis" (C+ 13.3.3.1.3).
1834 Candidate.Conversions[ArgIdx].ConversionKind
1835 = ImplicitConversionSequence::EllipsisConversion;
1836 }
1837 }
1838}
1839
Douglas Gregor5ed15042008-11-18 23:14:02 +00001840/// AddMethodCandidate - Adds the given C++ member function to the set
1841/// of candidate functions, using the given function call arguments
1842/// and the object argument (@c Object). For example, in a call
1843/// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain
1844/// both @c a1 and @c a2. If @p SuppressUserConversions, then don't
1845/// allow user-defined conversions via constructors or conversion
1846/// operators.
1847void
1848Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object,
1849 Expr **Args, unsigned NumArgs,
1850 OverloadCandidateSet& CandidateSet,
1851 bool SuppressUserConversions)
1852{
1853 const FunctionTypeProto* Proto
1854 = dyn_cast<FunctionTypeProto>(Method->getType()->getAsFunctionType());
1855 assert(Proto && "Methods without a prototype cannot be overloaded");
1856 assert(!isa<CXXConversionDecl>(Method) &&
1857 "Use AddConversionCandidate for conversion functions");
1858
1859 // Add this candidate
1860 CandidateSet.push_back(OverloadCandidate());
1861 OverloadCandidate& Candidate = CandidateSet.back();
1862 Candidate.Function = Method;
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00001863 Candidate.IsSurrogate = false;
Douglas Gregor3257fb52008-12-22 05:46:06 +00001864 Candidate.IgnoreObjectArgument = false;
Douglas Gregor5ed15042008-11-18 23:14:02 +00001865
1866 unsigned NumArgsInProto = Proto->getNumArgs();
1867
1868 // (C++ 13.3.2p2): A candidate function having fewer than m
1869 // parameters is viable only if it has an ellipsis in its parameter
1870 // list (8.3.5).
1871 if (NumArgs > NumArgsInProto && !Proto->isVariadic()) {
1872 Candidate.Viable = false;
1873 return;
1874 }
1875
1876 // (C++ 13.3.2p2): A candidate function having more than m parameters
1877 // is viable only if the (m+1)st parameter has a default argument
1878 // (8.3.6). For the purposes of overload resolution, the
1879 // parameter list is truncated on the right, so that there are
1880 // exactly m parameters.
1881 unsigned MinRequiredArgs = Method->getMinRequiredArguments();
1882 if (NumArgs < MinRequiredArgs) {
1883 // Not enough arguments.
1884 Candidate.Viable = false;
1885 return;
1886 }
1887
1888 Candidate.Viable = true;
1889 Candidate.Conversions.resize(NumArgs + 1);
1890
Douglas Gregor3257fb52008-12-22 05:46:06 +00001891 if (Method->isStatic() || !Object)
1892 // The implicit object argument is ignored.
1893 Candidate.IgnoreObjectArgument = true;
1894 else {
1895 // Determine the implicit conversion sequence for the object
1896 // parameter.
1897 Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method);
1898 if (Candidate.Conversions[0].ConversionKind
1899 == ImplicitConversionSequence::BadConversion) {
1900 Candidate.Viable = false;
1901 return;
1902 }
Douglas Gregor5ed15042008-11-18 23:14:02 +00001903 }
1904
1905 // Determine the implicit conversion sequences for each of the
1906 // arguments.
1907 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1908 if (ArgIdx < NumArgsInProto) {
1909 // (C++ 13.3.2p3): for F to be a viable function, there shall
1910 // exist for each argument an implicit conversion sequence
1911 // (13.3.3.1) that converts that argument to the corresponding
1912 // parameter of F.
1913 QualType ParamType = Proto->getArgType(ArgIdx);
1914 Candidate.Conversions[ArgIdx + 1]
1915 = TryCopyInitialization(Args[ArgIdx], ParamType,
1916 SuppressUserConversions);
1917 if (Candidate.Conversions[ArgIdx + 1].ConversionKind
1918 == ImplicitConversionSequence::BadConversion) {
1919 Candidate.Viable = false;
1920 break;
1921 }
1922 } else {
1923 // (C++ 13.3.2p2): For the purposes of overload resolution, any
1924 // argument for which there is no corresponding parameter is
1925 // considered to ""match the ellipsis" (C+ 13.3.3.1.3).
1926 Candidate.Conversions[ArgIdx + 1].ConversionKind
1927 = ImplicitConversionSequence::EllipsisConversion;
1928 }
1929 }
1930}
1931
Douglas Gregor60714f92008-11-07 22:36:19 +00001932/// AddConversionCandidate - Add a C++ conversion function as a
1933/// candidate in the candidate set (C++ [over.match.conv],
1934/// C++ [over.match.copy]). From is the expression we're converting from,
1935/// and ToType is the type that we're eventually trying to convert to
1936/// (which may or may not be the same type as the type that the
1937/// conversion function produces).
1938void
1939Sema::AddConversionCandidate(CXXConversionDecl *Conversion,
1940 Expr *From, QualType ToType,
1941 OverloadCandidateSet& CandidateSet) {
1942 // Add this candidate
1943 CandidateSet.push_back(OverloadCandidate());
1944 OverloadCandidate& Candidate = CandidateSet.back();
1945 Candidate.Function = Conversion;
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00001946 Candidate.IsSurrogate = false;
Douglas Gregor3257fb52008-12-22 05:46:06 +00001947 Candidate.IgnoreObjectArgument = false;
Douglas Gregor60714f92008-11-07 22:36:19 +00001948 Candidate.FinalConversion.setAsIdentityConversion();
1949 Candidate.FinalConversion.FromTypePtr
1950 = Conversion->getConversionType().getAsOpaquePtr();
1951 Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr();
1952
Douglas Gregor5ed15042008-11-18 23:14:02 +00001953 // Determine the implicit conversion sequence for the implicit
1954 // object parameter.
Douglas Gregor60714f92008-11-07 22:36:19 +00001955 Candidate.Viable = true;
1956 Candidate.Conversions.resize(1);
Douglas Gregor5ed15042008-11-18 23:14:02 +00001957 Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion);
Douglas Gregor60714f92008-11-07 22:36:19 +00001958
Douglas Gregor60714f92008-11-07 22:36:19 +00001959 if (Candidate.Conversions[0].ConversionKind
1960 == ImplicitConversionSequence::BadConversion) {
1961 Candidate.Viable = false;
1962 return;
1963 }
1964
1965 // To determine what the conversion from the result of calling the
1966 // conversion function to the type we're eventually trying to
1967 // convert to (ToType), we need to synthesize a call to the
1968 // conversion function and attempt copy initialization from it. This
1969 // makes sure that we get the right semantics with respect to
1970 // lvalues/rvalues and the type. Fortunately, we can allocate this
1971 // call on the stack and we don't need its arguments to be
1972 // well-formed.
1973 DeclRefExpr ConversionRef(Conversion, Conversion->getType(),
1974 SourceLocation());
1975 ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()),
Douglas Gregor70d26122008-11-12 17:17:38 +00001976 &ConversionRef, false);
Douglas Gregor60714f92008-11-07 22:36:19 +00001977 CallExpr Call(&ConversionFn, 0, 0,
1978 Conversion->getConversionType().getNonReferenceType(),
1979 SourceLocation());
1980 ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true);
1981 switch (ICS.ConversionKind) {
1982 case ImplicitConversionSequence::StandardConversion:
1983 Candidate.FinalConversion = ICS.Standard;
1984 break;
1985
1986 case ImplicitConversionSequence::BadConversion:
1987 Candidate.Viable = false;
1988 break;
1989
1990 default:
1991 assert(false &&
1992 "Can only end up with a standard conversion sequence or failure");
1993 }
1994}
1995
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00001996/// AddSurrogateCandidate - Adds a "surrogate" candidate function that
1997/// converts the given @c Object to a function pointer via the
1998/// conversion function @c Conversion, and then attempts to call it
1999/// with the given arguments (C++ [over.call.object]p2-4). Proto is
2000/// the type of function that we'll eventually be calling.
2001void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion,
2002 const FunctionTypeProto *Proto,
2003 Expr *Object, Expr **Args, unsigned NumArgs,
2004 OverloadCandidateSet& CandidateSet) {
2005 CandidateSet.push_back(OverloadCandidate());
2006 OverloadCandidate& Candidate = CandidateSet.back();
2007 Candidate.Function = 0;
2008 Candidate.Surrogate = Conversion;
2009 Candidate.Viable = true;
2010 Candidate.IsSurrogate = true;
Douglas Gregor3257fb52008-12-22 05:46:06 +00002011 Candidate.IgnoreObjectArgument = false;
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00002012 Candidate.Conversions.resize(NumArgs + 1);
2013
2014 // Determine the implicit conversion sequence for the implicit
2015 // object parameter.
2016 ImplicitConversionSequence ObjectInit
2017 = TryObjectArgumentInitialization(Object, Conversion);
2018 if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) {
2019 Candidate.Viable = false;
2020 return;
2021 }
2022
2023 // The first conversion is actually a user-defined conversion whose
2024 // first conversion is ObjectInit's standard conversion (which is
2025 // effectively a reference binding). Record it as such.
2026 Candidate.Conversions[0].ConversionKind
2027 = ImplicitConversionSequence::UserDefinedConversion;
2028 Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard;
2029 Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion;
2030 Candidate.Conversions[0].UserDefined.After
2031 = Candidate.Conversions[0].UserDefined.Before;
2032 Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion();
2033
2034 // Find the
2035 unsigned NumArgsInProto = Proto->getNumArgs();
2036
2037 // (C++ 13.3.2p2): A candidate function having fewer than m
2038 // parameters is viable only if it has an ellipsis in its parameter
2039 // list (8.3.5).
2040 if (NumArgs > NumArgsInProto && !Proto->isVariadic()) {
2041 Candidate.Viable = false;
2042 return;
2043 }
2044
2045 // Function types don't have any default arguments, so just check if
2046 // we have enough arguments.
2047 if (NumArgs < NumArgsInProto) {
2048 // Not enough arguments.
2049 Candidate.Viable = false;
2050 return;
2051 }
2052
2053 // Determine the implicit conversion sequences for each of the
2054 // arguments.
2055 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
2056 if (ArgIdx < NumArgsInProto) {
2057 // (C++ 13.3.2p3): for F to be a viable function, there shall
2058 // exist for each argument an implicit conversion sequence
2059 // (13.3.3.1) that converts that argument to the corresponding
2060 // parameter of F.
2061 QualType ParamType = Proto->getArgType(ArgIdx);
2062 Candidate.Conversions[ArgIdx + 1]
2063 = TryCopyInitialization(Args[ArgIdx], ParamType,
2064 /*SuppressUserConversions=*/false);
2065 if (Candidate.Conversions[ArgIdx + 1].ConversionKind
2066 == ImplicitConversionSequence::BadConversion) {
2067 Candidate.Viable = false;
2068 break;
2069 }
2070 } else {
2071 // (C++ 13.3.2p2): For the purposes of overload resolution, any
2072 // argument for which there is no corresponding parameter is
2073 // considered to ""match the ellipsis" (C+ 13.3.3.1.3).
2074 Candidate.Conversions[ArgIdx + 1].ConversionKind
2075 = ImplicitConversionSequence::EllipsisConversion;
2076 }
2077 }
2078}
2079
Douglas Gregor849ea9c2008-11-19 03:25:36 +00002080/// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is
2081/// an acceptable non-member overloaded operator for a call whose
2082/// arguments have types T1 (and, if non-empty, T2). This routine
2083/// implements the check in C++ [over.match.oper]p3b2 concerning
2084/// enumeration types.
2085static bool
2086IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn,
2087 QualType T1, QualType T2,
2088 ASTContext &Context) {
2089 if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType()))
2090 return true;
2091
2092 const FunctionTypeProto *Proto = Fn->getType()->getAsFunctionTypeProto();
2093 if (Proto->getNumArgs() < 1)
2094 return false;
2095
2096 if (T1->isEnumeralType()) {
2097 QualType ArgType = Proto->getArgType(0).getNonReferenceType();
2098 if (Context.getCanonicalType(T1).getUnqualifiedType()
2099 == Context.getCanonicalType(ArgType).getUnqualifiedType())
2100 return true;
2101 }
2102
2103 if (Proto->getNumArgs() < 2)
2104 return false;
2105
2106 if (!T2.isNull() && T2->isEnumeralType()) {
2107 QualType ArgType = Proto->getArgType(1).getNonReferenceType();
2108 if (Context.getCanonicalType(T2).getUnqualifiedType()
2109 == Context.getCanonicalType(ArgType).getUnqualifiedType())
2110 return true;
2111 }
2112
2113 return false;
2114}
2115
Douglas Gregor5ed15042008-11-18 23:14:02 +00002116/// AddOperatorCandidates - Add the overloaded operator candidates for
2117/// the operator Op that was used in an operator expression such as "x
2118/// Op y". S is the scope in which the expression occurred (used for
2119/// name lookup of the operator), Args/NumArgs provides the operator
2120/// arguments, and CandidateSet will store the added overload
2121/// candidates. (C++ [over.match.oper]).
2122void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S,
2123 Expr **Args, unsigned NumArgs,
2124 OverloadCandidateSet& CandidateSet) {
2125 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
2126
2127 // C++ [over.match.oper]p3:
2128 // For a unary operator @ with an operand of a type whose
2129 // cv-unqualified version is T1, and for a binary operator @ with
2130 // a left operand of a type whose cv-unqualified version is T1 and
2131 // a right operand of a type whose cv-unqualified version is T2,
2132 // three sets of candidate functions, designated member
2133 // candidates, non-member candidates and built-in candidates, are
2134 // constructed as follows:
2135 QualType T1 = Args[0]->getType();
2136 QualType T2;
2137 if (NumArgs > 1)
2138 T2 = Args[1]->getType();
2139
2140 // -- If T1 is a class type, the set of member candidates is the
2141 // result of the qualified lookup of T1::operator@
2142 // (13.3.1.1.1); otherwise, the set of member candidates is
2143 // empty.
2144 if (const RecordType *T1Rec = T1->getAsRecordType()) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002145 DeclContext::lookup_const_result Lookup
Douglas Gregor39677622008-12-11 20:41:00 +00002146 = T1Rec->getDecl()->lookup(Context, OpName);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002147 NamedDecl *MemberOps = (Lookup.first == Lookup.second)? 0 : *Lookup.first;
Douglas Gregor5ed15042008-11-18 23:14:02 +00002148 if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(MemberOps))
2149 AddMethodCandidate(Method, Args[0], Args+1, NumArgs - 1, CandidateSet,
2150 /*SuppressUserConversions=*/false);
2151 else if (OverloadedFunctionDecl *Ovl
2152 = dyn_cast_or_null<OverloadedFunctionDecl>(MemberOps)) {
2153 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2154 FEnd = Ovl->function_end();
2155 F != FEnd; ++F) {
2156 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*F))
2157 AddMethodCandidate(Method, Args[0], Args+1, NumArgs - 1, CandidateSet,
2158 /*SuppressUserConversions=*/false);
2159 }
2160 }
2161 }
2162
2163 // -- The set of non-member candidates is the result of the
2164 // unqualified lookup of operator@ in the context of the
2165 // expression according to the usual rules for name lookup in
2166 // unqualified function calls (3.4.2) except that all member
2167 // functions are ignored. However, if no operand has a class
2168 // type, only those non-member functions in the lookup set
2169 // that have a first parameter of type T1 or “reference to
2170 // (possibly cv-qualified) T1”, when T1 is an enumeration
2171 // type, or (if there is a right operand) a second parameter
2172 // of type T2 or “reference to (possibly cv-qualified) T2”,
2173 // when T2 is an enumeration type, are candidate functions.
2174 {
2175 NamedDecl *NonMemberOps = 0;
2176 for (IdentifierResolver::iterator I
2177 = IdResolver.begin(OpName, CurContext, true/*LookInParentCtx*/);
2178 I != IdResolver.end(); ++I) {
2179 // We don't need to check the identifier namespace, because
2180 // operator names can only be ordinary identifiers.
2181
2182 // Ignore member functions.
2183 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(*I)) {
2184 if (SD->getDeclContext()->isCXXRecord())
2185 continue;
2186 }
2187
2188 // We found something with this name. We're done.
2189 NonMemberOps = *I;
2190 break;
2191 }
2192
Douglas Gregor849ea9c2008-11-19 03:25:36 +00002193 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(NonMemberOps)) {
2194 if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context))
2195 AddOverloadCandidate(FD, Args, NumArgs, CandidateSet,
2196 /*SuppressUserConversions=*/false);
2197 } else if (OverloadedFunctionDecl *Ovl
2198 = dyn_cast_or_null<OverloadedFunctionDecl>(NonMemberOps)) {
Douglas Gregor5ed15042008-11-18 23:14:02 +00002199 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2200 FEnd = Ovl->function_end();
Douglas Gregor849ea9c2008-11-19 03:25:36 +00002201 F != FEnd; ++F) {
2202 if (IsAcceptableNonMemberOperatorCandidate(*F, T1, T2, Context))
2203 AddOverloadCandidate(*F, Args, NumArgs, CandidateSet,
2204 /*SuppressUserConversions=*/false);
2205 }
Douglas Gregor5ed15042008-11-18 23:14:02 +00002206 }
2207 }
2208
2209 // Add builtin overload candidates (C++ [over.built]).
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002210 AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet);
Douglas Gregor5ed15042008-11-18 23:14:02 +00002211}
2212
Douglas Gregor70d26122008-11-12 17:17:38 +00002213/// AddBuiltinCandidate - Add a candidate for a built-in
2214/// operator. ResultTy and ParamTys are the result and parameter types
2215/// of the built-in candidate, respectively. Args and NumArgs are the
2216/// arguments being passed to the candidate.
2217void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys,
2218 Expr **Args, unsigned NumArgs,
2219 OverloadCandidateSet& CandidateSet) {
2220 // Add this candidate
2221 CandidateSet.push_back(OverloadCandidate());
2222 OverloadCandidate& Candidate = CandidateSet.back();
2223 Candidate.Function = 0;
Douglas Gregor6b5e34f2008-12-12 02:00:36 +00002224 Candidate.IsSurrogate = false;
Douglas Gregor3257fb52008-12-22 05:46:06 +00002225 Candidate.IgnoreObjectArgument = false;
Douglas Gregor70d26122008-11-12 17:17:38 +00002226 Candidate.BuiltinTypes.ResultTy = ResultTy;
2227 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
2228 Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx];
2229
2230 // Determine the implicit conversion sequences for each of the
2231 // arguments.
2232 Candidate.Viable = true;
2233 Candidate.Conversions.resize(NumArgs);
2234 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
2235 Candidate.Conversions[ArgIdx]
2236 = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], false);
2237 if (Candidate.Conversions[ArgIdx].ConversionKind
Douglas Gregor5ed15042008-11-18 23:14:02 +00002238 == ImplicitConversionSequence::BadConversion) {
Douglas Gregor70d26122008-11-12 17:17:38 +00002239 Candidate.Viable = false;
Douglas Gregor5ed15042008-11-18 23:14:02 +00002240 break;
2241 }
Douglas Gregor70d26122008-11-12 17:17:38 +00002242 }
2243}
2244
2245/// BuiltinCandidateTypeSet - A set of types that will be used for the
2246/// candidate operator functions for built-in operators (C++
2247/// [over.built]). The types are separated into pointer types and
2248/// enumeration types.
2249class BuiltinCandidateTypeSet {
2250 /// TypeSet - A set of types.
Douglas Gregor3d4492e2008-11-13 20:12:29 +00002251 typedef llvm::SmallPtrSet<void*, 8> TypeSet;
Douglas Gregor70d26122008-11-12 17:17:38 +00002252
2253 /// PointerTypes - The set of pointer types that will be used in the
2254 /// built-in candidates.
2255 TypeSet PointerTypes;
2256
2257 /// EnumerationTypes - The set of enumeration types that will be
2258 /// used in the built-in candidates.
2259 TypeSet EnumerationTypes;
2260
2261 /// Context - The AST context in which we will build the type sets.
2262 ASTContext &Context;
2263
2264 bool AddWithMoreQualifiedTypeVariants(QualType Ty);
2265
2266public:
2267 /// iterator - Iterates through the types that are part of the set.
Douglas Gregor3d4492e2008-11-13 20:12:29 +00002268 class iterator {
2269 TypeSet::iterator Base;
2270
2271 public:
2272 typedef QualType value_type;
2273 typedef QualType reference;
2274 typedef QualType pointer;
2275 typedef std::ptrdiff_t difference_type;
2276 typedef std::input_iterator_tag iterator_category;
2277
2278 iterator(TypeSet::iterator B) : Base(B) { }
2279
2280 iterator& operator++() {
2281 ++Base;
2282 return *this;
2283 }
2284
2285 iterator operator++(int) {
2286 iterator tmp(*this);
2287 ++(*this);
2288 return tmp;
2289 }
2290
2291 reference operator*() const {
2292 return QualType::getFromOpaquePtr(*Base);
2293 }
2294
2295 pointer operator->() const {
2296 return **this;
2297 }
2298
2299 friend bool operator==(iterator LHS, iterator RHS) {
2300 return LHS.Base == RHS.Base;
2301 }
2302
2303 friend bool operator!=(iterator LHS, iterator RHS) {
2304 return LHS.Base != RHS.Base;
2305 }
2306 };
Douglas Gregor70d26122008-11-12 17:17:38 +00002307
2308 BuiltinCandidateTypeSet(ASTContext &Context) : Context(Context) { }
2309
2310 void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions = true);
2311
2312 /// pointer_begin - First pointer type found;
2313 iterator pointer_begin() { return PointerTypes.begin(); }
2314
2315 /// pointer_end - Last pointer type found;
2316 iterator pointer_end() { return PointerTypes.end(); }
2317
2318 /// enumeration_begin - First enumeration type found;
2319 iterator enumeration_begin() { return EnumerationTypes.begin(); }
2320
2321 /// enumeration_end - Last enumeration type found;
2322 iterator enumeration_end() { return EnumerationTypes.end(); }
2323};
2324
2325/// AddWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to
2326/// the set of pointer types along with any more-qualified variants of
2327/// that type. For example, if @p Ty is "int const *", this routine
2328/// will add "int const *", "int const volatile *", "int const
2329/// restrict *", and "int const volatile restrict *" to the set of
2330/// pointer types. Returns true if the add of @p Ty itself succeeded,
2331/// false otherwise.
2332bool BuiltinCandidateTypeSet::AddWithMoreQualifiedTypeVariants(QualType Ty) {
2333 // Insert this type.
Douglas Gregor3d4492e2008-11-13 20:12:29 +00002334 if (!PointerTypes.insert(Ty.getAsOpaquePtr()))
Douglas Gregor70d26122008-11-12 17:17:38 +00002335 return false;
2336
2337 if (const PointerType *PointerTy = Ty->getAsPointerType()) {
2338 QualType PointeeTy = PointerTy->getPointeeType();
2339 // FIXME: Optimize this so that we don't keep trying to add the same types.
2340
2341 // FIXME: Do we have to add CVR qualifiers at *all* levels to deal
2342 // with all pointer conversions that don't cast away constness?
2343 if (!PointeeTy.isConstQualified())
2344 AddWithMoreQualifiedTypeVariants
2345 (Context.getPointerType(PointeeTy.withConst()));
2346 if (!PointeeTy.isVolatileQualified())
2347 AddWithMoreQualifiedTypeVariants
2348 (Context.getPointerType(PointeeTy.withVolatile()));
2349 if (!PointeeTy.isRestrictQualified())
2350 AddWithMoreQualifiedTypeVariants
2351 (Context.getPointerType(PointeeTy.withRestrict()));
2352 }
2353
2354 return true;
2355}
2356
2357/// AddTypesConvertedFrom - Add each of the types to which the type @p
2358/// Ty can be implicit converted to the given set of @p Types. We're
2359/// primarily interested in pointer types, enumeration types,
2360void BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty,
2361 bool AllowUserConversions) {
2362 // Only deal with canonical types.
2363 Ty = Context.getCanonicalType(Ty);
2364
2365 // Look through reference types; they aren't part of the type of an
2366 // expression for the purposes of conversions.
2367 if (const ReferenceType *RefTy = Ty->getAsReferenceType())
2368 Ty = RefTy->getPointeeType();
2369
2370 // We don't care about qualifiers on the type.
2371 Ty = Ty.getUnqualifiedType();
2372
2373 if (const PointerType *PointerTy = Ty->getAsPointerType()) {
2374 QualType PointeeTy = PointerTy->getPointeeType();
2375
2376 // Insert our type, and its more-qualified variants, into the set
2377 // of types.
2378 if (!AddWithMoreQualifiedTypeVariants(Ty))
2379 return;
2380
2381 // Add 'cv void*' to our set of types.
2382 if (!Ty->isVoidType()) {
2383 QualType QualVoid
2384 = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers());
2385 AddWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid));
2386 }
2387
2388 // If this is a pointer to a class type, add pointers to its bases
2389 // (with the same level of cv-qualification as the original
2390 // derived class, of course).
2391 if (const RecordType *PointeeRec = PointeeTy->getAsRecordType()) {
2392 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl());
2393 for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin();
2394 Base != ClassDecl->bases_end(); ++Base) {
2395 QualType BaseTy = Context.getCanonicalType(Base->getType());
2396 BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers());
2397
2398 // Add the pointer type, recursively, so that we get all of
2399 // the indirect base classes, too.
2400 AddTypesConvertedFrom(Context.getPointerType(BaseTy), false);
2401 }
2402 }
2403 } else if (Ty->isEnumeralType()) {
Douglas Gregor3d4492e2008-11-13 20:12:29 +00002404 EnumerationTypes.insert(Ty.getAsOpaquePtr());
Douglas Gregor70d26122008-11-12 17:17:38 +00002405 } else if (AllowUserConversions) {
2406 if (const RecordType *TyRec = Ty->getAsRecordType()) {
2407 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl());
2408 // FIXME: Visit conversion functions in the base classes, too.
2409 OverloadedFunctionDecl *Conversions
2410 = ClassDecl->getConversionFunctions();
2411 for (OverloadedFunctionDecl::function_iterator Func
2412 = Conversions->function_begin();
2413 Func != Conversions->function_end(); ++Func) {
2414 CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
2415 AddTypesConvertedFrom(Conv->getConversionType(), false);
2416 }
2417 }
2418 }
2419}
2420
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002421/// AddBuiltinOperatorCandidates - Add the appropriate built-in
2422/// operator overloads to the candidate set (C++ [over.built]), based
2423/// on the operator @p Op and the arguments given. For example, if the
2424/// operator is a binary '+', this routine might add "int
2425/// operator+(int, int)" to cover integer addition.
Douglas Gregor70d26122008-11-12 17:17:38 +00002426void
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002427Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op,
2428 Expr **Args, unsigned NumArgs,
2429 OverloadCandidateSet& CandidateSet) {
Douglas Gregor70d26122008-11-12 17:17:38 +00002430 // The set of "promoted arithmetic types", which are the arithmetic
2431 // types are that preserved by promotion (C++ [over.built]p2). Note
2432 // that the first few of these types are the promoted integral
2433 // types; these types need to be first.
2434 // FIXME: What about complex?
2435 const unsigned FirstIntegralType = 0;
2436 const unsigned LastIntegralType = 13;
2437 const unsigned FirstPromotedIntegralType = 7,
2438 LastPromotedIntegralType = 13;
2439 const unsigned FirstPromotedArithmeticType = 7,
2440 LastPromotedArithmeticType = 16;
2441 const unsigned NumArithmeticTypes = 16;
2442 QualType ArithmeticTypes[NumArithmeticTypes] = {
2443 Context.BoolTy, Context.CharTy, Context.WCharTy,
2444 Context.SignedCharTy, Context.ShortTy,
2445 Context.UnsignedCharTy, Context.UnsignedShortTy,
2446 Context.IntTy, Context.LongTy, Context.LongLongTy,
2447 Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy,
2448 Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy
2449 };
2450
2451 // Find all of the types that the arguments can convert to, but only
2452 // if the operator we're looking at has built-in operator candidates
2453 // that make use of these types.
2454 BuiltinCandidateTypeSet CandidateTypes(Context);
2455 if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual ||
2456 Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual ||
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002457 Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal ||
Douglas Gregor70d26122008-11-12 17:17:38 +00002458 Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript ||
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002459 Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus ||
2460 (Op == OO_Star && NumArgs == 1)) {
2461 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
Douglas Gregor70d26122008-11-12 17:17:38 +00002462 CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType());
2463 }
2464
2465 bool isComparison = false;
2466 switch (Op) {
2467 case OO_None:
2468 case NUM_OVERLOADED_OPERATORS:
2469 assert(false && "Expected an overloaded operator");
2470 break;
2471
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002472 case OO_Star: // '*' is either unary or binary
2473 if (NumArgs == 1)
2474 goto UnaryStar;
2475 else
2476 goto BinaryStar;
2477 break;
2478
2479 case OO_Plus: // '+' is either unary or binary
2480 if (NumArgs == 1)
2481 goto UnaryPlus;
2482 else
2483 goto BinaryPlus;
2484 break;
2485
2486 case OO_Minus: // '-' is either unary or binary
2487 if (NumArgs == 1)
2488 goto UnaryMinus;
2489 else
2490 goto BinaryMinus;
2491 break;
2492
2493 case OO_Amp: // '&' is either unary or binary
2494 if (NumArgs == 1)
2495 goto UnaryAmp;
2496 else
2497 goto BinaryAmp;
2498
2499 case OO_PlusPlus:
2500 case OO_MinusMinus:
2501 // C++ [over.built]p3:
2502 //
2503 // For every pair (T, VQ), where T is an arithmetic type, and VQ
2504 // is either volatile or empty, there exist candidate operator
2505 // functions of the form
2506 //
2507 // VQ T& operator++(VQ T&);
2508 // T operator++(VQ T&, int);
2509 //
2510 // C++ [over.built]p4:
2511 //
2512 // For every pair (T, VQ), where T is an arithmetic type other
2513 // than bool, and VQ is either volatile or empty, there exist
2514 // candidate operator functions of the form
2515 //
2516 // VQ T& operator--(VQ T&);
2517 // T operator--(VQ T&, int);
2518 for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1);
2519 Arith < NumArithmeticTypes; ++Arith) {
2520 QualType ArithTy = ArithmeticTypes[Arith];
2521 QualType ParamTypes[2]
2522 = { Context.getReferenceType(ArithTy), Context.IntTy };
2523
2524 // Non-volatile version.
2525 if (NumArgs == 1)
2526 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
2527 else
2528 AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet);
2529
2530 // Volatile version
2531 ParamTypes[0] = Context.getReferenceType(ArithTy.withVolatile());
2532 if (NumArgs == 1)
2533 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
2534 else
2535 AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet);
2536 }
2537
2538 // C++ [over.built]p5:
2539 //
2540 // For every pair (T, VQ), where T is a cv-qualified or
2541 // cv-unqualified object type, and VQ is either volatile or
2542 // empty, there exist candidate operator functions of the form
2543 //
2544 // T*VQ& operator++(T*VQ&);
2545 // T*VQ& operator--(T*VQ&);
2546 // T* operator++(T*VQ&, int);
2547 // T* operator--(T*VQ&, int);
2548 for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
2549 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2550 // Skip pointer types that aren't pointers to object types.
Douglas Gregor24a90a52008-11-26 23:31:11 +00002551 if (!(*Ptr)->getAsPointerType()->getPointeeType()->isIncompleteOrObjectType())
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002552 continue;
2553
2554 QualType ParamTypes[2] = {
2555 Context.getReferenceType(*Ptr), Context.IntTy
2556 };
2557
2558 // Without volatile
2559 if (NumArgs == 1)
2560 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
2561 else
2562 AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
2563
2564 if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) {
2565 // With volatile
2566 ParamTypes[0] = Context.getReferenceType((*Ptr).withVolatile());
2567 if (NumArgs == 1)
2568 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet);
2569 else
2570 AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
2571 }
2572 }
2573 break;
2574
2575 UnaryStar:
2576 // C++ [over.built]p6:
2577 // For every cv-qualified or cv-unqualified object type T, there
2578 // exist candidate operator functions of the form
2579 //
2580 // T& operator*(T*);
2581 //
2582 // C++ [over.built]p7:
2583 // For every function type T, there exist candidate operator
2584 // functions of the form
2585 // T& operator*(T*);
2586 for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
2587 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2588 QualType ParamTy = *Ptr;
2589 QualType PointeeTy = ParamTy->getAsPointerType()->getPointeeType();
2590 AddBuiltinCandidate(Context.getReferenceType(PointeeTy),
2591 &ParamTy, Args, 1, CandidateSet);
2592 }
2593 break;
2594
2595 UnaryPlus:
2596 // C++ [over.built]p8:
2597 // For every type T, there exist candidate operator functions of
2598 // the form
2599 //
2600 // T* operator+(T*);
2601 for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
2602 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2603 QualType ParamTy = *Ptr;
2604 AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet);
2605 }
2606
2607 // Fall through
2608
2609 UnaryMinus:
2610 // C++ [over.built]p9:
2611 // For every promoted arithmetic type T, there exist candidate
2612 // operator functions of the form
2613 //
2614 // T operator+(T);
2615 // T operator-(T);
2616 for (unsigned Arith = FirstPromotedArithmeticType;
2617 Arith < LastPromotedArithmeticType; ++Arith) {
2618 QualType ArithTy = ArithmeticTypes[Arith];
2619 AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet);
2620 }
2621 break;
2622
2623 case OO_Tilde:
2624 // C++ [over.built]p10:
2625 // For every promoted integral type T, there exist candidate
2626 // operator functions of the form
2627 //
2628 // T operator~(T);
2629 for (unsigned Int = FirstPromotedIntegralType;
2630 Int < LastPromotedIntegralType; ++Int) {
2631 QualType IntTy = ArithmeticTypes[Int];
2632 AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet);
2633 }
2634 break;
2635
Douglas Gregor70d26122008-11-12 17:17:38 +00002636 case OO_New:
2637 case OO_Delete:
2638 case OO_Array_New:
2639 case OO_Array_Delete:
Douglas Gregor70d26122008-11-12 17:17:38 +00002640 case OO_Call:
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002641 assert(false && "Special operators don't use AddBuiltinOperatorCandidates");
Douglas Gregor70d26122008-11-12 17:17:38 +00002642 break;
2643
2644 case OO_Comma:
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002645 UnaryAmp:
2646 case OO_Arrow:
Douglas Gregor70d26122008-11-12 17:17:38 +00002647 // C++ [over.match.oper]p3:
2648 // -- For the operator ',', the unary operator '&', or the
2649 // operator '->', the built-in candidates set is empty.
Douglas Gregor70d26122008-11-12 17:17:38 +00002650 break;
2651
2652 case OO_Less:
2653 case OO_Greater:
2654 case OO_LessEqual:
2655 case OO_GreaterEqual:
2656 case OO_EqualEqual:
2657 case OO_ExclaimEqual:
2658 // C++ [over.built]p15:
2659 //
2660 // For every pointer or enumeration type T, there exist
2661 // candidate operator functions of the form
2662 //
2663 // bool operator<(T, T);
2664 // bool operator>(T, T);
2665 // bool operator<=(T, T);
2666 // bool operator>=(T, T);
2667 // bool operator==(T, T);
2668 // bool operator!=(T, T);
2669 for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
2670 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2671 QualType ParamTypes[2] = { *Ptr, *Ptr };
2672 AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet);
2673 }
2674 for (BuiltinCandidateTypeSet::iterator Enum
2675 = CandidateTypes.enumeration_begin();
2676 Enum != CandidateTypes.enumeration_end(); ++Enum) {
2677 QualType ParamTypes[2] = { *Enum, *Enum };
2678 AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet);
2679 }
2680
2681 // Fall through.
2682 isComparison = true;
2683
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002684 BinaryPlus:
2685 BinaryMinus:
Douglas Gregor70d26122008-11-12 17:17:38 +00002686 if (!isComparison) {
2687 // We didn't fall through, so we must have OO_Plus or OO_Minus.
2688
2689 // C++ [over.built]p13:
2690 //
2691 // For every cv-qualified or cv-unqualified object type T
2692 // there exist candidate operator functions of the form
2693 //
2694 // T* operator+(T*, ptrdiff_t);
2695 // T& operator[](T*, ptrdiff_t); [BELOW]
2696 // T* operator-(T*, ptrdiff_t);
2697 // T* operator+(ptrdiff_t, T*);
2698 // T& operator[](ptrdiff_t, T*); [BELOW]
2699 //
2700 // C++ [over.built]p14:
2701 //
2702 // For every T, where T is a pointer to object type, there
2703 // exist candidate operator functions of the form
2704 //
2705 // ptrdiff_t operator-(T, T);
2706 for (BuiltinCandidateTypeSet::iterator Ptr
2707 = CandidateTypes.pointer_begin();
2708 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2709 QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() };
2710
2711 // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t)
2712 AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
2713
2714 if (Op == OO_Plus) {
2715 // T* operator+(ptrdiff_t, T*);
2716 ParamTypes[0] = ParamTypes[1];
2717 ParamTypes[1] = *Ptr;
2718 AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet);
2719 } else {
2720 // ptrdiff_t operator-(T, T);
2721 ParamTypes[1] = *Ptr;
2722 AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes,
2723 Args, 2, CandidateSet);
2724 }
2725 }
2726 }
2727 // Fall through
2728
Douglas Gregor70d26122008-11-12 17:17:38 +00002729 case OO_Slash:
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002730 BinaryStar:
Douglas Gregor70d26122008-11-12 17:17:38 +00002731 // C++ [over.built]p12:
2732 //
2733 // For every pair of promoted arithmetic types L and R, there
2734 // exist candidate operator functions of the form
2735 //
2736 // LR operator*(L, R);
2737 // LR operator/(L, R);
2738 // LR operator+(L, R);
2739 // LR operator-(L, R);
2740 // bool operator<(L, R);
2741 // bool operator>(L, R);
2742 // bool operator<=(L, R);
2743 // bool operator>=(L, R);
2744 // bool operator==(L, R);
2745 // bool operator!=(L, R);
2746 //
2747 // where LR is the result of the usual arithmetic conversions
2748 // between types L and R.
2749 for (unsigned Left = FirstPromotedArithmeticType;
2750 Left < LastPromotedArithmeticType; ++Left) {
2751 for (unsigned Right = FirstPromotedArithmeticType;
2752 Right < LastPromotedArithmeticType; ++Right) {
2753 QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] };
2754 QualType Result
2755 = isComparison? Context.BoolTy
2756 : UsualArithmeticConversionsType(LandR[0], LandR[1]);
2757 AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet);
2758 }
2759 }
2760 break;
2761
2762 case OO_Percent:
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002763 BinaryAmp:
Douglas Gregor70d26122008-11-12 17:17:38 +00002764 case OO_Caret:
2765 case OO_Pipe:
2766 case OO_LessLess:
2767 case OO_GreaterGreater:
2768 // C++ [over.built]p17:
2769 //
2770 // For every pair of promoted integral types L and R, there
2771 // exist candidate operator functions of the form
2772 //
2773 // LR operator%(L, R);
2774 // LR operator&(L, R);
2775 // LR operator^(L, R);
2776 // LR operator|(L, R);
2777 // L operator<<(L, R);
2778 // L operator>>(L, R);
2779 //
2780 // where LR is the result of the usual arithmetic conversions
2781 // between types L and R.
2782 for (unsigned Left = FirstPromotedIntegralType;
2783 Left < LastPromotedIntegralType; ++Left) {
2784 for (unsigned Right = FirstPromotedIntegralType;
2785 Right < LastPromotedIntegralType; ++Right) {
2786 QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] };
2787 QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater)
2788 ? LandR[0]
2789 : UsualArithmeticConversionsType(LandR[0], LandR[1]);
2790 AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet);
2791 }
2792 }
2793 break;
2794
2795 case OO_Equal:
2796 // C++ [over.built]p20:
2797 //
2798 // For every pair (T, VQ), where T is an enumeration or
2799 // (FIXME:) pointer to member type and VQ is either volatile or
2800 // empty, there exist candidate operator functions of the form
2801 //
2802 // VQ T& operator=(VQ T&, T);
2803 for (BuiltinCandidateTypeSet::iterator Enum
2804 = CandidateTypes.enumeration_begin();
2805 Enum != CandidateTypes.enumeration_end(); ++Enum) {
2806 QualType ParamTypes[2];
2807
2808 // T& operator=(T&, T)
2809 ParamTypes[0] = Context.getReferenceType(*Enum);
2810 ParamTypes[1] = *Enum;
2811 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2812
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002813 if (!Context.getCanonicalType(*Enum).isVolatileQualified()) {
2814 // volatile T& operator=(volatile T&, T)
2815 ParamTypes[0] = Context.getReferenceType((*Enum).withVolatile());
2816 ParamTypes[1] = *Enum;
2817 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2818 }
Douglas Gregor70d26122008-11-12 17:17:38 +00002819 }
2820 // Fall through.
2821
2822 case OO_PlusEqual:
2823 case OO_MinusEqual:
2824 // C++ [over.built]p19:
2825 //
2826 // For every pair (T, VQ), where T is any type and VQ is either
2827 // volatile or empty, there exist candidate operator functions
2828 // of the form
2829 //
2830 // T*VQ& operator=(T*VQ&, T*);
2831 //
2832 // C++ [over.built]p21:
2833 //
2834 // For every pair (T, VQ), where T is a cv-qualified or
2835 // cv-unqualified object type and VQ is either volatile or
2836 // empty, there exist candidate operator functions of the form
2837 //
2838 // T*VQ& operator+=(T*VQ&, ptrdiff_t);
2839 // T*VQ& operator-=(T*VQ&, ptrdiff_t);
2840 for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
2841 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2842 QualType ParamTypes[2];
2843 ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType();
2844
2845 // non-volatile version
2846 ParamTypes[0] = Context.getReferenceType(*Ptr);
2847 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2848
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002849 if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) {
2850 // volatile version
2851 ParamTypes[0] = Context.getReferenceType((*Ptr).withVolatile());
2852 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2853 }
Douglas Gregor70d26122008-11-12 17:17:38 +00002854 }
2855 // Fall through.
2856
2857 case OO_StarEqual:
2858 case OO_SlashEqual:
2859 // C++ [over.built]p18:
2860 //
2861 // For every triple (L, VQ, R), where L is an arithmetic type,
2862 // VQ is either volatile or empty, and R is a promoted
2863 // arithmetic type, there exist candidate operator functions of
2864 // the form
2865 //
2866 // VQ L& operator=(VQ L&, R);
2867 // VQ L& operator*=(VQ L&, R);
2868 // VQ L& operator/=(VQ L&, R);
2869 // VQ L& operator+=(VQ L&, R);
2870 // VQ L& operator-=(VQ L&, R);
2871 for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) {
2872 for (unsigned Right = FirstPromotedArithmeticType;
2873 Right < LastPromotedArithmeticType; ++Right) {
2874 QualType ParamTypes[2];
2875 ParamTypes[1] = ArithmeticTypes[Right];
2876
2877 // Add this built-in operator as a candidate (VQ is empty).
2878 ParamTypes[0] = Context.getReferenceType(ArithmeticTypes[Left]);
2879 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2880
2881 // Add this built-in operator as a candidate (VQ is 'volatile').
2882 ParamTypes[0] = ArithmeticTypes[Left].withVolatile();
2883 ParamTypes[0] = Context.getReferenceType(ParamTypes[0]);
2884 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2885 }
2886 }
2887 break;
2888
2889 case OO_PercentEqual:
2890 case OO_LessLessEqual:
2891 case OO_GreaterGreaterEqual:
2892 case OO_AmpEqual:
2893 case OO_CaretEqual:
2894 case OO_PipeEqual:
2895 // C++ [over.built]p22:
2896 //
2897 // For every triple (L, VQ, R), where L is an integral type, VQ
2898 // is either volatile or empty, and R is a promoted integral
2899 // type, there exist candidate operator functions of the form
2900 //
2901 // VQ L& operator%=(VQ L&, R);
2902 // VQ L& operator<<=(VQ L&, R);
2903 // VQ L& operator>>=(VQ L&, R);
2904 // VQ L& operator&=(VQ L&, R);
2905 // VQ L& operator^=(VQ L&, R);
2906 // VQ L& operator|=(VQ L&, R);
2907 for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) {
2908 for (unsigned Right = FirstPromotedIntegralType;
2909 Right < LastPromotedIntegralType; ++Right) {
2910 QualType ParamTypes[2];
2911 ParamTypes[1] = ArithmeticTypes[Right];
2912
2913 // Add this built-in operator as a candidate (VQ is empty).
2914 // FIXME: We should be caching these declarations somewhere,
2915 // rather than re-building them every time.
2916 ParamTypes[0] = Context.getReferenceType(ArithmeticTypes[Left]);
2917 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2918
2919 // Add this built-in operator as a candidate (VQ is 'volatile').
2920 ParamTypes[0] = ArithmeticTypes[Left];
2921 ParamTypes[0].addVolatile();
2922 ParamTypes[0] = Context.getReferenceType(ParamTypes[0]);
2923 AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet);
2924 }
2925 }
2926 break;
2927
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002928 case OO_Exclaim: {
2929 // C++ [over.operator]p23:
2930 //
2931 // There also exist candidate operator functions of the form
2932 //
2933 // bool operator!(bool);
2934 // bool operator&&(bool, bool); [BELOW]
2935 // bool operator||(bool, bool); [BELOW]
2936 QualType ParamTy = Context.BoolTy;
2937 AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet);
2938 break;
2939 }
2940
Douglas Gregor70d26122008-11-12 17:17:38 +00002941 case OO_AmpAmp:
2942 case OO_PipePipe: {
2943 // C++ [over.operator]p23:
2944 //
2945 // There also exist candidate operator functions of the form
2946 //
Douglas Gregor4f6904d2008-11-19 15:42:04 +00002947 // bool operator!(bool); [ABOVE]
Douglas Gregor70d26122008-11-12 17:17:38 +00002948 // bool operator&&(bool, bool);
2949 // bool operator||(bool, bool);
2950 QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy };
2951 AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet);
2952 break;
2953 }
2954
2955 case OO_Subscript:
2956 // C++ [over.built]p13:
2957 //
2958 // For every cv-qualified or cv-unqualified object type T there
2959 // exist candidate operator functions of the form
2960 //
2961 // T* operator+(T*, ptrdiff_t); [ABOVE]
2962 // T& operator[](T*, ptrdiff_t);
2963 // T* operator-(T*, ptrdiff_t); [ABOVE]
2964 // T* operator+(ptrdiff_t, T*); [ABOVE]
2965 // T& operator[](ptrdiff_t, T*);
2966 for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin();
2967 Ptr != CandidateTypes.pointer_end(); ++Ptr) {
2968 QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() };
2969 QualType PointeeType = (*Ptr)->getAsPointerType()->getPointeeType();
2970 QualType ResultTy = Context.getReferenceType(PointeeType);
2971
2972 // T& operator[](T*, ptrdiff_t)
2973 AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet);
2974
2975 // T& operator[](ptrdiff_t, T*);
2976 ParamTypes[0] = ParamTypes[1];
2977 ParamTypes[1] = *Ptr;
2978 AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet);
2979 }
2980 break;
2981
2982 case OO_ArrowStar:
2983 // FIXME: No support for pointer-to-members yet.
2984 break;
2985 }
2986}
2987
Douglas Gregord2baafd2008-10-21 16:13:35 +00002988/// AddOverloadCandidates - Add all of the function overloads in Ovl
2989/// to the candidate set.
2990void
Douglas Gregor5870a952008-11-03 20:45:27 +00002991Sema::AddOverloadCandidates(const OverloadedFunctionDecl *Ovl,
Douglas Gregord2baafd2008-10-21 16:13:35 +00002992 Expr **Args, unsigned NumArgs,
Douglas Gregora3b34bb2008-11-03 19:09:14 +00002993 OverloadCandidateSet& CandidateSet,
2994 bool SuppressUserConversions)
Douglas Gregord2baafd2008-10-21 16:13:35 +00002995{
Douglas Gregor5870a952008-11-03 20:45:27 +00002996 for (OverloadedFunctionDecl::function_const_iterator Func
2997 = Ovl->function_begin();
Douglas Gregord2baafd2008-10-21 16:13:35 +00002998 Func != Ovl->function_end(); ++Func)
Douglas Gregora3b34bb2008-11-03 19:09:14 +00002999 AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet,
3000 SuppressUserConversions);
Douglas Gregord2baafd2008-10-21 16:13:35 +00003001}
3002
3003/// isBetterOverloadCandidate - Determines whether the first overload
3004/// candidate is a better candidate than the second (C++ 13.3.3p1).
3005bool
3006Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1,
3007 const OverloadCandidate& Cand2)
3008{
3009 // Define viable functions to be better candidates than non-viable
3010 // functions.
3011 if (!Cand2.Viable)
3012 return Cand1.Viable;
3013 else if (!Cand1.Viable)
3014 return false;
3015
Douglas Gregor3257fb52008-12-22 05:46:06 +00003016 // C++ [over.match.best]p1:
3017 //
3018 // -- if F is a static member function, ICS1(F) is defined such
3019 // that ICS1(F) is neither better nor worse than ICS1(G) for
3020 // any function G, and, symmetrically, ICS1(G) is neither
3021 // better nor worse than ICS1(F).
3022 unsigned StartArg = 0;
3023 if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument)
3024 StartArg = 1;
Douglas Gregord2baafd2008-10-21 16:13:35 +00003025
3026 // (C++ 13.3.3p1): a viable function F1 is defined to be a better
3027 // function than another viable function F2 if for all arguments i,
3028 // ICSi(F1) is not a worse conversion sequence than ICSi(F2), and
3029 // then...
3030 unsigned NumArgs = Cand1.Conversions.size();
3031 assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch");
3032 bool HasBetterConversion = false;
Douglas Gregor3257fb52008-12-22 05:46:06 +00003033 for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) {
Douglas Gregord2baafd2008-10-21 16:13:35 +00003034 switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx],
3035 Cand2.Conversions[ArgIdx])) {
3036 case ImplicitConversionSequence::Better:
3037 // Cand1 has a better conversion sequence.
3038 HasBetterConversion = true;
3039 break;
3040
3041 case ImplicitConversionSequence::Worse:
3042 // Cand1 can't be better than Cand2.
3043 return false;
3044
3045 case ImplicitConversionSequence::Indistinguishable:
3046 // Do nothing.
3047 break;
3048 }
3049 }
3050
3051 if (HasBetterConversion)
3052 return true;
3053
Douglas Gregor70d26122008-11-12 17:17:38 +00003054 // FIXME: Several other bullets in (C++ 13.3.3p1) need to be
3055 // implemented, but they require template support.
Douglas Gregord2baafd2008-10-21 16:13:35 +00003056
Douglas Gregor60714f92008-11-07 22:36:19 +00003057 // C++ [over.match.best]p1b4:
3058 //
3059 // -- the context is an initialization by user-defined conversion
3060 // (see 8.5, 13.3.1.5) and the standard conversion sequence
3061 // from the return type of F1 to the destination type (i.e.,
3062 // the type of the entity being initialized) is a better
3063 // conversion sequence than the standard conversion sequence
3064 // from the return type of F2 to the destination type.
Douglas Gregor849ea9c2008-11-19 03:25:36 +00003065 if (Cand1.Function && Cand2.Function &&
3066 isa<CXXConversionDecl>(Cand1.Function) &&
Douglas Gregor60714f92008-11-07 22:36:19 +00003067 isa<CXXConversionDecl>(Cand2.Function)) {
3068 switch (CompareStandardConversionSequences(Cand1.FinalConversion,
3069 Cand2.FinalConversion)) {
3070 case ImplicitConversionSequence::Better:
3071 // Cand1 has a better conversion sequence.
3072 return true;
3073
3074 case ImplicitConversionSequence::Worse:
3075 // Cand1 can't be better than Cand2.
3076 return false;
3077
3078 case ImplicitConversionSequence::Indistinguishable:
3079 // Do nothing
3080 break;
3081 }
3082 }
3083
Douglas Gregord2baafd2008-10-21 16:13:35 +00003084 return false;
3085}
3086
3087/// BestViableFunction - Computes the best viable function (C++ 13.3.3)
3088/// within an overload candidate set. If overloading is successful,
3089/// the result will be OR_Success and Best will be set to point to the
3090/// best viable function within the candidate set. Otherwise, one of
3091/// several kinds of errors will be returned; see
3092/// Sema::OverloadingResult.
3093Sema::OverloadingResult
3094Sema::BestViableFunction(OverloadCandidateSet& CandidateSet,
3095 OverloadCandidateSet::iterator& Best)
3096{
3097 // Find the best viable function.
3098 Best = CandidateSet.end();
3099 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin();
3100 Cand != CandidateSet.end(); ++Cand) {
3101 if (Cand->Viable) {
3102 if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best))
3103 Best = Cand;
3104 }
3105 }
3106
3107 // If we didn't find any viable functions, abort.
3108 if (Best == CandidateSet.end())
3109 return OR_No_Viable_Function;
3110
3111 // Make sure that this function is better than every other viable
3112 // function. If not, we have an ambiguity.
3113 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin();
3114 Cand != CandidateSet.end(); ++Cand) {
3115 if (Cand->Viable &&
3116 Cand != Best &&
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003117 !isBetterOverloadCandidate(*Best, *Cand)) {
3118 Best = CandidateSet.end();
Douglas Gregord2baafd2008-10-21 16:13:35 +00003119 return OR_Ambiguous;
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003120 }
Douglas Gregord2baafd2008-10-21 16:13:35 +00003121 }
3122
3123 // Best is the best viable function.
3124 return OR_Success;
3125}
3126
3127/// PrintOverloadCandidates - When overload resolution fails, prints
3128/// diagnostic messages containing the candidates in the candidate
3129/// set. If OnlyViable is true, only viable candidates will be printed.
3130void
3131Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet,
3132 bool OnlyViable)
3133{
3134 OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3135 LastCand = CandidateSet.end();
3136 for (; Cand != LastCand; ++Cand) {
Douglas Gregor70d26122008-11-12 17:17:38 +00003137 if (Cand->Viable || !OnlyViable) {
3138 if (Cand->Function) {
3139 // Normal function
3140 Diag(Cand->Function->getLocation(), diag::err_ovl_candidate);
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003141 } else if (Cand->IsSurrogate) {
Douglas Gregor30c8ddf2008-11-21 02:54:28 +00003142 // Desugar the type of the surrogate down to a function type,
3143 // retaining as many typedefs as possible while still showing
3144 // the function type (and, therefore, its parameter types).
3145 QualType FnType = Cand->Surrogate->getConversionType();
3146 bool isReference = false;
3147 bool isPointer = false;
3148 if (const ReferenceType *FnTypeRef = FnType->getAsReferenceType()) {
3149 FnType = FnTypeRef->getPointeeType();
3150 isReference = true;
3151 }
3152 if (const PointerType *FnTypePtr = FnType->getAsPointerType()) {
3153 FnType = FnTypePtr->getPointeeType();
3154 isPointer = true;
3155 }
3156 // Desugar down to a function type.
3157 FnType = QualType(FnType->getAsFunctionType(), 0);
3158 // Reconstruct the pointer/reference as appropriate.
3159 if (isPointer) FnType = Context.getPointerType(FnType);
3160 if (isReference) FnType = Context.getReferenceType(FnType);
3161
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003162 Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand)
Chris Lattner4bfd2232008-11-24 06:25:27 +00003163 << FnType;
Douglas Gregor70d26122008-11-12 17:17:38 +00003164 } else {
3165 // FIXME: We need to get the identifier in here
3166 // FIXME: Do we want the error message to point at the
3167 // operator? (built-ins won't have a location)
3168 QualType FnType
3169 = Context.getFunctionType(Cand->BuiltinTypes.ResultTy,
3170 Cand->BuiltinTypes.ParamTypes,
3171 Cand->Conversions.size(),
3172 false, 0);
3173
Chris Lattner4bfd2232008-11-24 06:25:27 +00003174 Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType;
Douglas Gregor70d26122008-11-12 17:17:38 +00003175 }
3176 }
Douglas Gregord2baafd2008-10-21 16:13:35 +00003177 }
3178}
3179
Douglas Gregor45014fd2008-11-10 20:40:00 +00003180/// ResolveAddressOfOverloadedFunction - Try to resolve the address of
3181/// an overloaded function (C++ [over.over]), where @p From is an
3182/// expression with overloaded function type and @p ToType is the type
3183/// we're trying to resolve to. For example:
3184///
3185/// @code
3186/// int f(double);
3187/// int f(int);
3188///
3189/// int (*pfd)(double) = f; // selects f(double)
3190/// @endcode
3191///
3192/// This routine returns the resulting FunctionDecl if it could be
3193/// resolved, and NULL otherwise. When @p Complain is true, this
3194/// routine will emit diagnostics if there is an error.
3195FunctionDecl *
3196Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType,
3197 bool Complain) {
3198 QualType FunctionType = ToType;
3199 if (const PointerLikeType *ToTypePtr = ToType->getAsPointerLikeType())
3200 FunctionType = ToTypePtr->getPointeeType();
3201
3202 // We only look at pointers or references to functions.
3203 if (!FunctionType->isFunctionType())
3204 return 0;
3205
3206 // Find the actual overloaded function declaration.
3207 OverloadedFunctionDecl *Ovl = 0;
3208
3209 // C++ [over.over]p1:
3210 // [...] [Note: any redundant set of parentheses surrounding the
3211 // overloaded function name is ignored (5.1). ]
3212 Expr *OvlExpr = From->IgnoreParens();
3213
3214 // C++ [over.over]p1:
3215 // [...] The overloaded function name can be preceded by the &
3216 // operator.
3217 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) {
3218 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
3219 OvlExpr = UnOp->getSubExpr()->IgnoreParens();
3220 }
3221
3222 // Try to dig out the overloaded function.
3223 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr))
3224 Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl());
3225
3226 // If there's no overloaded function declaration, we're done.
3227 if (!Ovl)
3228 return 0;
3229
3230 // Look through all of the overloaded functions, searching for one
3231 // whose type matches exactly.
3232 // FIXME: When templates or using declarations come along, we'll actually
3233 // have to deal with duplicates, partial ordering, etc. For now, we
3234 // can just do a simple search.
3235 FunctionType = Context.getCanonicalType(FunctionType.getUnqualifiedType());
3236 for (OverloadedFunctionDecl::function_iterator Fun = Ovl->function_begin();
3237 Fun != Ovl->function_end(); ++Fun) {
3238 // C++ [over.over]p3:
3239 // Non-member functions and static member functions match
3240 // targets of type “pointer-to-function”or
3241 // “reference-to-function.”
3242 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun))
3243 if (!Method->isStatic())
3244 continue;
3245
3246 if (FunctionType == Context.getCanonicalType((*Fun)->getType()))
3247 return *Fun;
3248 }
3249
3250 return 0;
3251}
3252
Douglas Gregor3ed006b2008-11-26 05:54:23 +00003253/// ResolveOverloadedCallFn - Given the call expression that calls Fn
3254/// (which eventually refers to the set of overloaded functions in
3255/// Ovl) and the call arguments Args/NumArgs, attempt to resolve the
3256/// function call down to a specific function. If overload resolution
Douglas Gregorbf4f0582008-11-26 06:01:48 +00003257/// succeeds, returns the function declaration produced by overload
3258/// resolution. Otherwise, emits diagnostics, deletes all of the
Douglas Gregor3ed006b2008-11-26 05:54:23 +00003259/// arguments and Fn, and returns NULL.
Douglas Gregorbf4f0582008-11-26 06:01:48 +00003260FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, OverloadedFunctionDecl *Ovl,
3261 SourceLocation LParenLoc,
3262 Expr **Args, unsigned NumArgs,
3263 SourceLocation *CommaLocs,
3264 SourceLocation RParenLoc) {
Douglas Gregor3ed006b2008-11-26 05:54:23 +00003265 OverloadCandidateSet CandidateSet;
3266 AddOverloadCandidates(Ovl, Args, NumArgs, CandidateSet);
3267 OverloadCandidateSet::iterator Best;
3268 switch (BestViableFunction(CandidateSet, Best)) {
Douglas Gregorbf4f0582008-11-26 06:01:48 +00003269 case OR_Success:
3270 return Best->Function;
Douglas Gregor3ed006b2008-11-26 05:54:23 +00003271
3272 case OR_No_Viable_Function:
3273 Diag(Fn->getSourceRange().getBegin(),
3274 diag::err_ovl_no_viable_function_in_call)
3275 << Ovl->getDeclName() << (unsigned)CandidateSet.size()
3276 << Fn->getSourceRange();
3277 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
3278 break;
3279
3280 case OR_Ambiguous:
3281 Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call)
3282 << Ovl->getDeclName() << Fn->getSourceRange();
3283 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
3284 break;
3285 }
3286
3287 // Overload resolution failed. Destroy all of the subexpressions and
3288 // return NULL.
3289 Fn->Destroy(Context);
3290 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
3291 Args[Arg]->Destroy(Context);
3292 return 0;
3293}
3294
Douglas Gregor3257fb52008-12-22 05:46:06 +00003295/// BuildCallToMemberFunction - Build a call to a member
3296/// function. MemExpr is the expression that refers to the member
3297/// function (and includes the object parameter), Args/NumArgs are the
3298/// arguments to the function call (not including the object
3299/// parameter). The caller needs to validate that the member
3300/// expression refers to a member function or an overloaded member
3301/// function.
3302Sema::ExprResult
3303Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE,
3304 SourceLocation LParenLoc, Expr **Args,
3305 unsigned NumArgs, SourceLocation *CommaLocs,
3306 SourceLocation RParenLoc) {
3307 // Dig out the member expression. This holds both the object
3308 // argument and the member function we're referring to.
3309 MemberExpr *MemExpr = 0;
3310 if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE))
3311 MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr());
3312 else
3313 MemExpr = dyn_cast<MemberExpr>(MemExprE);
3314 assert(MemExpr && "Building member call without member expression");
3315
3316 // Extract the object argument.
3317 Expr *ObjectArg = MemExpr->getBase();
3318 if (MemExpr->isArrow())
3319 ObjectArg = new UnaryOperator(ObjectArg, UnaryOperator::Deref,
3320 ObjectArg->getType()->getAsPointerType()->getPointeeType(),
3321 SourceLocation());
3322 CXXMethodDecl *Method = 0;
3323 if (OverloadedFunctionDecl *Ovl
3324 = dyn_cast<OverloadedFunctionDecl>(MemExpr->getMemberDecl())) {
3325 // Add overload candidates
3326 OverloadCandidateSet CandidateSet;
3327 for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(),
3328 FuncEnd = Ovl->function_end();
3329 Func != FuncEnd; ++Func) {
3330 assert(isa<CXXMethodDecl>(*Func) && "Function is not a method");
3331 Method = cast<CXXMethodDecl>(*Func);
3332 AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet,
3333 /*SuppressUserConversions=*/false);
3334 }
3335
3336 OverloadCandidateSet::iterator Best;
3337 switch (BestViableFunction(CandidateSet, Best)) {
3338 case OR_Success:
3339 Method = cast<CXXMethodDecl>(Best->Function);
3340 break;
3341
3342 case OR_No_Viable_Function:
3343 Diag(MemExpr->getSourceRange().getBegin(),
3344 diag::err_ovl_no_viable_member_function_in_call)
3345 << Ovl->getDeclName() << (unsigned)CandidateSet.size()
3346 << MemExprE->getSourceRange();
3347 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
3348 // FIXME: Leaking incoming expressions!
3349 return true;
3350
3351 case OR_Ambiguous:
3352 Diag(MemExpr->getSourceRange().getBegin(),
3353 diag::err_ovl_ambiguous_member_call)
3354 << Ovl->getDeclName() << MemExprE->getSourceRange();
3355 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
3356 // FIXME: Leaking incoming expressions!
3357 return true;
3358 }
3359
3360 FixOverloadedFunctionReference(MemExpr, Method);
3361 } else {
3362 Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl());
3363 }
3364
3365 assert(Method && "Member call to something that isn't a method?");
3366 llvm::OwningPtr<CXXMemberCallExpr>
3367 TheCall(new CXXMemberCallExpr(MemExpr, Args, NumArgs,
3368 Method->getResultType().getNonReferenceType(),
3369 RParenLoc));
3370
3371 // Convert the object argument (for a non-static member function call).
3372 if (!Method->isStatic() &&
3373 PerformObjectArgumentInitialization(ObjectArg, Method))
3374 return true;
3375 MemExpr->setBase(ObjectArg);
3376
3377 // Convert the rest of the arguments
3378 const FunctionTypeProto *Proto = cast<FunctionTypeProto>(Method->getType());
3379 if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs,
3380 RParenLoc))
3381 return true;
3382
3383 return CheckFunctionCall(Method, TheCall.take());
3384}
3385
Douglas Gregor10f3c502008-11-19 21:05:33 +00003386/// BuildCallToObjectOfClassType - Build a call to an object of class
3387/// type (C++ [over.call.object]), which can end up invoking an
3388/// overloaded function call operator (@c operator()) or performing a
3389/// user-defined conversion on the object argument.
Douglas Gregor3257fb52008-12-22 05:46:06 +00003390Sema::ExprResult
Douglas Gregora133e262008-12-06 00:22:45 +00003391Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object,
3392 SourceLocation LParenLoc,
Douglas Gregor10f3c502008-11-19 21:05:33 +00003393 Expr **Args, unsigned NumArgs,
3394 SourceLocation *CommaLocs,
3395 SourceLocation RParenLoc) {
3396 assert(Object->getType()->isRecordType() && "Requires object type argument");
3397 const RecordType *Record = Object->getType()->getAsRecordType();
3398
3399 // C++ [over.call.object]p1:
3400 // If the primary-expression E in the function call syntax
3401 // evaluates to a class object of type “cv T”, then the set of
3402 // candidate functions includes at least the function call
3403 // operators of T. The function call operators of T are obtained by
3404 // ordinary lookup of the name operator() in the context of
3405 // (E).operator().
3406 OverloadCandidateSet CandidateSet;
Douglas Gregor8acb7272008-12-11 16:49:14 +00003407 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call);
3408 DeclContext::lookup_const_result Lookup
Douglas Gregor39677622008-12-11 20:41:00 +00003409 = Record->getDecl()->lookup(Context, OpName);
Douglas Gregor8acb7272008-12-11 16:49:14 +00003410 NamedDecl *MemberOps = (Lookup.first == Lookup.second)? 0 : *Lookup.first;
Douglas Gregor10f3c502008-11-19 21:05:33 +00003411 if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(MemberOps))
3412 AddMethodCandidate(Method, Object, Args, NumArgs, CandidateSet,
3413 /*SuppressUserConversions=*/false);
3414 else if (OverloadedFunctionDecl *Ovl
3415 = dyn_cast_or_null<OverloadedFunctionDecl>(MemberOps)) {
3416 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
3417 FEnd = Ovl->function_end();
3418 F != FEnd; ++F) {
3419 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*F))
3420 AddMethodCandidate(Method, Object, Args, NumArgs, CandidateSet,
3421 /*SuppressUserConversions=*/false);
3422 }
3423 }
3424
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003425 // C++ [over.call.object]p2:
3426 // In addition, for each conversion function declared in T of the
3427 // form
3428 //
3429 // operator conversion-type-id () cv-qualifier;
3430 //
3431 // where cv-qualifier is the same cv-qualification as, or a
3432 // greater cv-qualification than, cv, and where conversion-type-id
Douglas Gregor261afa72008-11-20 13:33:37 +00003433 // denotes the type "pointer to function of (P1,...,Pn) returning
3434 // R", or the type "reference to pointer to function of
3435 // (P1,...,Pn) returning R", or the type "reference to function
3436 // of (P1,...,Pn) returning R", a surrogate call function [...]
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003437 // is also considered as a candidate function. Similarly,
3438 // surrogate call functions are added to the set of candidate
3439 // functions for each conversion function declared in an
3440 // accessible base class provided the function is not hidden
3441 // within T by another intervening declaration.
3442 //
3443 // FIXME: Look in base classes for more conversion operators!
3444 OverloadedFunctionDecl *Conversions
3445 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Douglas Gregor30c8ddf2008-11-21 02:54:28 +00003446 for (OverloadedFunctionDecl::function_iterator
3447 Func = Conversions->function_begin(),
3448 FuncEnd = Conversions->function_end();
3449 Func != FuncEnd; ++Func) {
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003450 CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
3451
3452 // Strip the reference type (if any) and then the pointer type (if
3453 // any) to get down to what might be a function type.
3454 QualType ConvType = Conv->getConversionType().getNonReferenceType();
3455 if (const PointerType *ConvPtrType = ConvType->getAsPointerType())
3456 ConvType = ConvPtrType->getPointeeType();
3457
3458 if (const FunctionTypeProto *Proto = ConvType->getAsFunctionTypeProto())
3459 AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet);
3460 }
Douglas Gregor10f3c502008-11-19 21:05:33 +00003461
3462 // Perform overload resolution.
3463 OverloadCandidateSet::iterator Best;
3464 switch (BestViableFunction(CandidateSet, Best)) {
3465 case OR_Success:
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003466 // Overload resolution succeeded; we'll build the appropriate call
3467 // below.
Douglas Gregor10f3c502008-11-19 21:05:33 +00003468 break;
3469
3470 case OR_No_Viable_Function:
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +00003471 Diag(Object->getSourceRange().getBegin(),
3472 diag::err_ovl_no_viable_object_call)
Chris Lattner4bfd2232008-11-24 06:25:27 +00003473 << Object->getType() << (unsigned)CandidateSet.size()
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +00003474 << Object->getSourceRange();
3475 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
Douglas Gregor10f3c502008-11-19 21:05:33 +00003476 break;
3477
3478 case OR_Ambiguous:
3479 Diag(Object->getSourceRange().getBegin(),
3480 diag::err_ovl_ambiguous_object_call)
Chris Lattner4bfd2232008-11-24 06:25:27 +00003481 << Object->getType() << Object->getSourceRange();
Douglas Gregor10f3c502008-11-19 21:05:33 +00003482 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
3483 break;
3484 }
3485
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003486 if (Best == CandidateSet.end()) {
Douglas Gregor10f3c502008-11-19 21:05:33 +00003487 // We had an error; delete all of the subexpressions and return
3488 // the error.
3489 delete Object;
3490 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
3491 delete Args[ArgIdx];
3492 return true;
3493 }
3494
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003495 if (Best->Function == 0) {
3496 // Since there is no function declaration, this is one of the
3497 // surrogate candidates. Dig out the conversion function.
3498 CXXConversionDecl *Conv
3499 = cast<CXXConversionDecl>(
3500 Best->Conversions[0].UserDefined.ConversionFunction);
3501
3502 // We selected one of the surrogate functions that converts the
3503 // object parameter to a function pointer. Perform the conversion
3504 // on the object argument, then let ActOnCallExpr finish the job.
3505 // FIXME: Represent the user-defined conversion in the AST!
3506 ImpCastExprToType(Object,
3507 Conv->getConversionType().getNonReferenceType(),
3508 Conv->getConversionType()->isReferenceType());
Douglas Gregora133e262008-12-06 00:22:45 +00003509 return ActOnCallExpr(S, (ExprTy*)Object, LParenLoc, (ExprTy**)Args, NumArgs,
Douglas Gregor67fdb5b2008-11-19 22:57:39 +00003510 CommaLocs, RParenLoc);
3511 }
3512
3513 // We found an overloaded operator(). Build a CXXOperatorCallExpr
3514 // that calls this method, using Object for the implicit object
3515 // parameter and passing along the remaining arguments.
3516 CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function);
Douglas Gregor10f3c502008-11-19 21:05:33 +00003517 const FunctionTypeProto *Proto = Method->getType()->getAsFunctionTypeProto();
3518
3519 unsigned NumArgsInProto = Proto->getNumArgs();
3520 unsigned NumArgsToCheck = NumArgs;
3521
3522 // Build the full argument list for the method call (the
3523 // implicit object parameter is placed at the beginning of the
3524 // list).
3525 Expr **MethodArgs;
3526 if (NumArgs < NumArgsInProto) {
3527 NumArgsToCheck = NumArgsInProto;
3528 MethodArgs = new Expr*[NumArgsInProto + 1];
3529 } else {
3530 MethodArgs = new Expr*[NumArgs + 1];
3531 }
3532 MethodArgs[0] = Object;
3533 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx)
3534 MethodArgs[ArgIdx + 1] = Args[ArgIdx];
3535
3536 Expr *NewFn = new DeclRefExpr(Method, Method->getType(),
3537 SourceLocation());
3538 UsualUnaryConversions(NewFn);
3539
3540 // Once we've built TheCall, all of the expressions are properly
3541 // owned.
3542 QualType ResultTy = Method->getResultType().getNonReferenceType();
3543 llvm::OwningPtr<CXXOperatorCallExpr>
3544 TheCall(new CXXOperatorCallExpr(NewFn, MethodArgs, NumArgs + 1,
3545 ResultTy, RParenLoc));
3546 delete [] MethodArgs;
3547
3548 // Initialize the implicit object parameter.
3549 if (!PerformObjectArgumentInitialization(Object, Method))
3550 return true;
3551 TheCall->setArg(0, Object);
3552
3553 // Check the argument types.
3554 for (unsigned i = 0; i != NumArgsToCheck; i++) {
3555 QualType ProtoArgType = Proto->getArgType(i);
3556
3557 Expr *Arg;
3558 if (i < NumArgs)
3559 Arg = Args[i];
3560 else
3561 Arg = new CXXDefaultArgExpr(Method->getParamDecl(i));
3562 QualType ArgType = Arg->getType();
3563
3564 // Pass the argument.
3565 if (PerformCopyInitialization(Arg, ProtoArgType, "passing"))
3566 return true;
3567
3568 TheCall->setArg(i + 1, Arg);
3569 }
3570
3571 // If this is a variadic call, handle args passed through "...".
3572 if (Proto->isVariadic()) {
3573 // Promote the arguments (C99 6.5.2.2p7).
3574 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
3575 Expr *Arg = Args[i];
3576 DefaultArgumentPromotion(Arg);
3577 TheCall->setArg(i + 1, Arg);
3578 }
3579 }
3580
3581 return CheckFunctionCall(Method, TheCall.take());
3582}
3583
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003584/// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator->
3585/// (if one exists), where @c Base is an expression of class type and
3586/// @c Member is the name of the member we're trying to find.
3587Action::ExprResult
3588Sema::BuildOverloadedArrowExpr(Expr *Base, SourceLocation OpLoc,
3589 SourceLocation MemberLoc,
3590 IdentifierInfo &Member) {
3591 assert(Base->getType()->isRecordType() && "left-hand side must have class type");
3592
3593 // C++ [over.ref]p1:
3594 //
3595 // [...] An expression x->m is interpreted as (x.operator->())->m
3596 // for a class object x of type T if T::operator->() exists and if
3597 // the operator is selected as the best match function by the
3598 // overload resolution mechanism (13.3).
3599 // FIXME: look in base classes.
3600 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow);
3601 OverloadCandidateSet CandidateSet;
3602 const RecordType *BaseRecord = Base->getType()->getAsRecordType();
Douglas Gregor8acb7272008-12-11 16:49:14 +00003603 DeclContext::lookup_const_result Lookup
Douglas Gregor39677622008-12-11 20:41:00 +00003604 = BaseRecord->getDecl()->lookup(Context, OpName);
Douglas Gregor8acb7272008-12-11 16:49:14 +00003605 NamedDecl *MemberOps = (Lookup.first == Lookup.second)? 0 : *Lookup.first;
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003606 if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(MemberOps))
3607 AddMethodCandidate(Method, Base, 0, 0, CandidateSet,
3608 /*SuppressUserConversions=*/false);
3609 else if (OverloadedFunctionDecl *Ovl
3610 = dyn_cast_or_null<OverloadedFunctionDecl>(MemberOps)) {
3611 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
3612 FEnd = Ovl->function_end();
3613 F != FEnd; ++F) {
3614 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*F))
3615 AddMethodCandidate(Method, Base, 0, 0, CandidateSet,
3616 /*SuppressUserConversions=*/false);
3617 }
3618 }
3619
Douglas Gregor9c690e92008-11-21 03:04:22 +00003620 llvm::OwningPtr<Expr> BasePtr(Base);
3621
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003622 // Perform overload resolution.
3623 OverloadCandidateSet::iterator Best;
3624 switch (BestViableFunction(CandidateSet, Best)) {
3625 case OR_Success:
3626 // Overload resolution succeeded; we'll build the call below.
3627 break;
3628
3629 case OR_No_Viable_Function:
3630 if (CandidateSet.empty())
3631 Diag(OpLoc, diag::err_typecheck_member_reference_arrow)
Chris Lattner4bfd2232008-11-24 06:25:27 +00003632 << BasePtr->getType() << BasePtr->getSourceRange();
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003633 else
3634 Diag(OpLoc, diag::err_ovl_no_viable_oper)
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +00003635 << "operator->" << (unsigned)CandidateSet.size()
3636 << BasePtr->getSourceRange();
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003637 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003638 return true;
3639
3640 case OR_Ambiguous:
3641 Diag(OpLoc, diag::err_ovl_ambiguous_oper)
Chris Lattner4bfd2232008-11-24 06:25:27 +00003642 << "operator->" << BasePtr->getSourceRange();
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003643 PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true);
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003644 return true;
3645 }
3646
3647 // Convert the object parameter.
3648 CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function);
Douglas Gregor9c690e92008-11-21 03:04:22 +00003649 if (PerformObjectArgumentInitialization(Base, Method))
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003650 return true;
Douglas Gregor9c690e92008-11-21 03:04:22 +00003651
3652 // No concerns about early exits now.
3653 BasePtr.take();
Douglas Gregor7f3fec52008-11-20 16:27:02 +00003654
3655 // Build the operator call.
3656 Expr *FnExpr = new DeclRefExpr(Method, Method->getType(), SourceLocation());
3657 UsualUnaryConversions(FnExpr);
3658 Base = new CXXOperatorCallExpr(FnExpr, &Base, 1,
3659 Method->getResultType().getNonReferenceType(),
3660 OpLoc);
3661 return ActOnMemberReferenceExpr(Base, OpLoc, tok::arrow, MemberLoc, Member);
3662}
3663
Douglas Gregor45014fd2008-11-10 20:40:00 +00003664/// FixOverloadedFunctionReference - E is an expression that refers to
3665/// a C++ overloaded function (possibly with some parentheses and
3666/// perhaps a '&' around it). We have resolved the overloaded function
3667/// to the function declaration Fn, so patch up the expression E to
3668/// refer (possibly indirectly) to Fn.
3669void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) {
3670 if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
3671 FixOverloadedFunctionReference(PE->getSubExpr(), Fn);
3672 E->setType(PE->getSubExpr()->getType());
3673 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) {
3674 assert(UnOp->getOpcode() == UnaryOperator::AddrOf &&
3675 "Can only take the address of an overloaded function");
3676 FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn);
3677 E->setType(Context.getPointerType(E->getType()));
3678 } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) {
3679 assert(isa<OverloadedFunctionDecl>(DR->getDecl()) &&
3680 "Expected overloaded function");
3681 DR->setDecl(Fn);
3682 E->setType(Fn->getType());
Douglas Gregor3257fb52008-12-22 05:46:06 +00003683 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) {
3684 MemExpr->setMemberDecl(Fn);
3685 E->setType(Fn->getType());
Douglas Gregor45014fd2008-11-10 20:40:00 +00003686 } else {
3687 assert(false && "Invalid reference to overloaded function");
3688 }
3689}
3690
Douglas Gregord2baafd2008-10-21 16:13:35 +00003691} // end namespace clang