/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaOverload.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaOverload.cpp - C++ Overloading -------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file provides Sema routines for C++ overloading. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/ASTContext.h" |
14 | | #include "clang/AST/CXXInheritance.h" |
15 | | #include "clang/AST/DeclObjC.h" |
16 | | #include "clang/AST/DependenceFlags.h" |
17 | | #include "clang/AST/Expr.h" |
18 | | #include "clang/AST/ExprCXX.h" |
19 | | #include "clang/AST/ExprObjC.h" |
20 | | #include "clang/AST/TypeOrdering.h" |
21 | | #include "clang/Basic/Diagnostic.h" |
22 | | #include "clang/Basic/DiagnosticOptions.h" |
23 | | #include "clang/Basic/PartialDiagnostic.h" |
24 | | #include "clang/Basic/SourceManager.h" |
25 | | #include "clang/Basic/TargetInfo.h" |
26 | | #include "clang/Sema/Initialization.h" |
27 | | #include "clang/Sema/Lookup.h" |
28 | | #include "clang/Sema/Overload.h" |
29 | | #include "clang/Sema/SemaInternal.h" |
30 | | #include "clang/Sema/Template.h" |
31 | | #include "clang/Sema/TemplateDeduction.h" |
32 | | #include "llvm/ADT/DenseSet.h" |
33 | | #include "llvm/ADT/Optional.h" |
34 | | #include "llvm/ADT/STLExtras.h" |
35 | | #include "llvm/ADT/SmallPtrSet.h" |
36 | | #include "llvm/ADT/SmallString.h" |
37 | | #include <algorithm> |
38 | | #include <cstdlib> |
39 | | |
40 | | using namespace clang; |
41 | | using namespace sema; |
42 | | |
43 | | using AllowedExplicit = Sema::AllowedExplicit; |
44 | | |
45 | 1.24M | static bool functionHasPassObjectSizeParams(const FunctionDecl *FD) { |
46 | 2.42M | return llvm::any_of(FD->parameters(), [](const ParmVarDecl *P) { |
47 | 2.42M | return P->hasAttr<PassObjectSizeAttr>(); |
48 | 2.42M | }); |
49 | 1.24M | } |
50 | | |
51 | | /// A convenience routine for creating a decayed reference to a function. |
52 | | static ExprResult |
53 | | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
54 | | const Expr *Base, bool HadMultipleCandidates, |
55 | | SourceLocation Loc = SourceLocation(), |
56 | 99.7k | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
57 | 99.7k | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
58 | 6 | return ExprError(); |
59 | | // If FoundDecl is different from Fn (such as if one is a template |
60 | | // and the other a specialization), make sure DiagnoseUseOfDecl is |
61 | | // called on both. |
62 | | // FIXME: This would be more comprehensively addressed by modifying |
63 | | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
64 | | // being used. |
65 | 99.7k | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)10.4k ) |
66 | 6 | return ExprError(); |
67 | 99.7k | DeclRefExpr *DRE = new (S.Context) |
68 | 99.7k | DeclRefExpr(S.Context, Fn, false, Fn->getType(), VK_LValue, Loc, LocInfo); |
69 | 99.7k | if (HadMultipleCandidates) |
70 | 49.9k | DRE->setHadMultipleCandidates(true); |
71 | | |
72 | 99.7k | S.MarkDeclRefReferenced(DRE, Base); |
73 | 99.7k | if (auto *FPT = DRE->getType()->getAs<FunctionProtoType>()) { |
74 | 99.7k | if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { |
75 | 5.92k | S.ResolveExceptionSpec(Loc, FPT); |
76 | 5.92k | DRE->setType(Fn->getType()); |
77 | 5.92k | } |
78 | 99.7k | } |
79 | 99.7k | return S.ImpCastExprToType(DRE, S.Context.getPointerType(DRE->getType()), |
80 | 99.7k | CK_FunctionToPointerDecay); |
81 | 99.7k | } |
82 | | |
83 | | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
84 | | bool InOverloadResolution, |
85 | | StandardConversionSequence &SCS, |
86 | | bool CStyle, |
87 | | bool AllowObjCWritebackConversion); |
88 | | |
89 | | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
90 | | QualType &ToType, |
91 | | bool InOverloadResolution, |
92 | | StandardConversionSequence &SCS, |
93 | | bool CStyle); |
94 | | static OverloadingResult |
95 | | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
96 | | UserDefinedConversionSequence& User, |
97 | | OverloadCandidateSet& Conversions, |
98 | | AllowedExplicit AllowExplicit, |
99 | | bool AllowObjCConversionOnExplicit); |
100 | | |
101 | | static ImplicitConversionSequence::CompareKind |
102 | | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
103 | | const StandardConversionSequence& SCS1, |
104 | | const StandardConversionSequence& SCS2); |
105 | | |
106 | | static ImplicitConversionSequence::CompareKind |
107 | | CompareQualificationConversions(Sema &S, |
108 | | const StandardConversionSequence& SCS1, |
109 | | const StandardConversionSequence& SCS2); |
110 | | |
111 | | static ImplicitConversionSequence::CompareKind |
112 | | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
113 | | const StandardConversionSequence& SCS1, |
114 | | const StandardConversionSequence& SCS2); |
115 | | |
116 | | /// GetConversionRank - Retrieve the implicit conversion rank |
117 | | /// corresponding to the given implicit conversion kind. |
118 | 37.3M | ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { |
119 | 37.3M | static const ImplicitConversionRank |
120 | 37.3M | Rank[(int)ICK_Num_Conversion_Kinds] = { |
121 | 37.3M | ICR_Exact_Match, |
122 | 37.3M | ICR_Exact_Match, |
123 | 37.3M | ICR_Exact_Match, |
124 | 37.3M | ICR_Exact_Match, |
125 | 37.3M | ICR_Exact_Match, |
126 | 37.3M | ICR_Exact_Match, |
127 | 37.3M | ICR_Promotion, |
128 | 37.3M | ICR_Promotion, |
129 | 37.3M | ICR_Promotion, |
130 | 37.3M | ICR_Conversion, |
131 | 37.3M | ICR_Conversion, |
132 | 37.3M | ICR_Conversion, |
133 | 37.3M | ICR_Conversion, |
134 | 37.3M | ICR_Conversion, |
135 | 37.3M | ICR_Conversion, |
136 | 37.3M | ICR_Conversion, |
137 | 37.3M | ICR_Conversion, |
138 | 37.3M | ICR_Conversion, |
139 | 37.3M | ICR_Conversion, |
140 | 37.3M | ICR_Conversion, |
141 | 37.3M | ICR_OCL_Scalar_Widening, |
142 | 37.3M | ICR_Complex_Real_Conversion, |
143 | 37.3M | ICR_Conversion, |
144 | 37.3M | ICR_Conversion, |
145 | 37.3M | ICR_Writeback_Conversion, |
146 | 37.3M | ICR_Exact_Match, // NOTE(gbiv): This may not be completely right -- |
147 | | // it was omitted by the patch that added |
148 | | // ICK_Zero_Event_Conversion |
149 | 37.3M | ICR_C_Conversion, |
150 | 37.3M | ICR_C_Conversion_Extension |
151 | 37.3M | }; |
152 | 37.3M | return Rank[(int)Kind]; |
153 | 37.3M | } |
154 | | |
155 | | /// GetImplicitConversionName - Return the name of this kind of |
156 | | /// implicit conversion. |
157 | 0 | static const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
158 | 0 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
159 | 0 | "No conversion", |
160 | 0 | "Lvalue-to-rvalue", |
161 | 0 | "Array-to-pointer", |
162 | 0 | "Function-to-pointer", |
163 | 0 | "Function pointer conversion", |
164 | 0 | "Qualification", |
165 | 0 | "Integral promotion", |
166 | 0 | "Floating point promotion", |
167 | 0 | "Complex promotion", |
168 | 0 | "Integral conversion", |
169 | 0 | "Floating conversion", |
170 | 0 | "Complex conversion", |
171 | 0 | "Floating-integral conversion", |
172 | 0 | "Pointer conversion", |
173 | 0 | "Pointer-to-member conversion", |
174 | 0 | "Boolean conversion", |
175 | 0 | "Compatible-types conversion", |
176 | 0 | "Derived-to-base conversion", |
177 | 0 | "Vector conversion", |
178 | 0 | "SVE Vector conversion", |
179 | 0 | "Vector splat", |
180 | 0 | "Complex-real conversion", |
181 | 0 | "Block Pointer conversion", |
182 | 0 | "Transparent Union Conversion", |
183 | 0 | "Writeback conversion", |
184 | 0 | "OpenCL Zero Event Conversion", |
185 | 0 | "C specific type conversion", |
186 | 0 | "Incompatible pointer conversion" |
187 | 0 | }; |
188 | 0 | return Name[Kind]; |
189 | 0 | } |
190 | | |
191 | | /// StandardConversionSequence - Set the standard conversion |
192 | | /// sequence to the identity conversion. |
193 | 78.7M | void StandardConversionSequence::setAsIdentityConversion() { |
194 | 78.7M | First = ICK_Identity; |
195 | 78.7M | Second = ICK_Identity; |
196 | 78.7M | Third = ICK_Identity; |
197 | 78.7M | DeprecatedStringLiteralToCharPtr = false; |
198 | 78.7M | QualificationIncludesObjCLifetime = false; |
199 | 78.7M | ReferenceBinding = false; |
200 | 78.7M | DirectBinding = false; |
201 | 78.7M | IsLvalueReference = true; |
202 | 78.7M | BindsToFunctionLvalue = false; |
203 | 78.7M | BindsToRvalue = false; |
204 | 78.7M | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
205 | 78.7M | ObjCLifetimeConversionBinding = false; |
206 | 78.7M | CopyConstructor = nullptr; |
207 | 78.7M | } |
208 | | |
209 | | /// getRank - Retrieve the rank of this standard conversion sequence |
210 | | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
211 | | /// implicit conversions. |
212 | 10.0M | ImplicitConversionRank StandardConversionSequence::getRank() const { |
213 | 10.0M | ImplicitConversionRank Rank = ICR_Exact_Match; |
214 | 10.0M | if (GetConversionRank(First) > Rank) |
215 | 0 | Rank = GetConversionRank(First); |
216 | 10.0M | if (GetConversionRank(Second) > Rank) |
217 | 7.30M | Rank = GetConversionRank(Second); |
218 | 10.0M | if (GetConversionRank(Third) > Rank) |
219 | 0 | Rank = GetConversionRank(Third); |
220 | 10.0M | return Rank; |
221 | 10.0M | } |
222 | | |
223 | | /// isPointerConversionToBool - Determines whether this conversion is |
224 | | /// a conversion of a pointer or pointer-to-member to bool. This is |
225 | | /// used as part of the ranking of standard conversion sequences |
226 | | /// (C++ 13.3.3.2p4). |
227 | 5.41M | bool StandardConversionSequence::isPointerConversionToBool() const { |
228 | | // Note that FromType has not necessarily been transformed by the |
229 | | // array-to-pointer or function-to-pointer implicit conversions, so |
230 | | // check for their presence as well as checking whether FromType is |
231 | | // a pointer. |
232 | 5.41M | if (getToType(1)->isBooleanType() && |
233 | 5.62k | (getFromType()->isPointerType() || |
234 | 5.59k | getFromType()->isMemberPointerType() || |
235 | 5.57k | getFromType()->isObjCObjectPointerType() || |
236 | 5.56k | getFromType()->isBlockPointerType() || |
237 | 5.56k | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer5.39k )) |
238 | 231 | return true; |
239 | | |
240 | 5.41M | return false; |
241 | 5.41M | } |
242 | | |
243 | | /// isPointerConversionToVoidPointer - Determines whether this |
244 | | /// conversion is a conversion of a pointer to a void pointer. This is |
245 | | /// used as part of the ranking of standard conversion sequences (C++ |
246 | | /// 13.3.3.2p4). |
247 | | bool |
248 | | StandardConversionSequence:: |
249 | 5.41M | isPointerConversionToVoidPointer(ASTContext& Context) const { |
250 | 5.41M | QualType FromType = getFromType(); |
251 | 5.41M | QualType ToType = getToType(1); |
252 | | |
253 | | // Note that FromType has not necessarily been transformed by the |
254 | | // array-to-pointer implicit conversion, so check for its presence |
255 | | // and redo the conversion to get a pointer. |
256 | 5.41M | if (First == ICK_Array_To_Pointer) |
257 | 5.45k | FromType = Context.getArrayDecayedType(FromType); |
258 | | |
259 | 5.41M | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()3.57k ) |
260 | 1.50k | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
261 | 1.43k | return ToPtrType->getPointeeType()->isVoidType(); |
262 | | |
263 | 5.41M | return false; |
264 | 5.41M | } |
265 | | |
266 | | /// Skip any implicit casts which could be either part of a narrowing conversion |
267 | | /// or after one in an implicit conversion. |
268 | | static const Expr *IgnoreNarrowingConversion(ASTContext &Ctx, |
269 | 201k | const Expr *Converted) { |
270 | | // We can have cleanups wrapping the converted expression; these need to be |
271 | | // preserved so that destructors run if necessary. |
272 | 201k | if (auto *EWC = dyn_cast<ExprWithCleanups>(Converted)) { |
273 | 7 | Expr *Inner = |
274 | 7 | const_cast<Expr *>(IgnoreNarrowingConversion(Ctx, EWC->getSubExpr())); |
275 | 7 | return ExprWithCleanups::Create(Ctx, Inner, EWC->cleanupsHaveSideEffects(), |
276 | 7 | EWC->getObjects()); |
277 | 7 | } |
278 | | |
279 | 403k | while (auto *201k ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
280 | 202k | switch (ICE->getCastKind()) { |
281 | 0 | case CK_NoOp: |
282 | 197k | case CK_IntegralCast: |
283 | 198k | case CK_IntegralToBoolean: |
284 | 200k | case CK_IntegralToFloating: |
285 | 200k | case CK_BooleanToSignedIntegral: |
286 | 200k | case CK_FloatingToIntegral: |
287 | 200k | case CK_FloatingToBoolean: |
288 | 201k | case CK_FloatingCast: |
289 | 201k | Converted = ICE->getSubExpr(); |
290 | 201k | continue; |
291 | | |
292 | 220 | default: |
293 | 220 | return Converted; |
294 | 202k | } |
295 | 202k | } |
296 | | |
297 | 201k | return Converted; |
298 | 201k | } |
299 | | |
300 | | /// Check if this standard conversion sequence represents a narrowing |
301 | | /// conversion, according to C++11 [dcl.init.list]p7. |
302 | | /// |
303 | | /// \param Ctx The AST context. |
304 | | /// \param Converted The result of applying this standard conversion sequence. |
305 | | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
306 | | /// value of the expression prior to the narrowing conversion. |
307 | | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
308 | | /// type of the expression prior to the narrowing conversion. |
309 | | /// \param IgnoreFloatToIntegralConversion If true type-narrowing conversions |
310 | | /// from floating point types to integral types should be ignored. |
311 | | NarrowingKind StandardConversionSequence::getNarrowingKind( |
312 | | ASTContext &Ctx, const Expr *Converted, APValue &ConstantValue, |
313 | 2.45M | QualType &ConstantType, bool IgnoreFloatToIntegralConversion) const { |
314 | 2.45M | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
315 | | |
316 | | // C++11 [dcl.init.list]p7: |
317 | | // A narrowing conversion is an implicit conversion ... |
318 | 2.45M | QualType FromType = getToType(0); |
319 | 2.45M | QualType ToType = getToType(1); |
320 | | |
321 | | // A conversion to an enumeration type is narrowing if the conversion to |
322 | | // the underlying type is narrowing. This only arises for expressions of |
323 | | // the form 'Enum{init}'. |
324 | 2.45M | if (auto *ET = ToType->getAs<EnumType>()) |
325 | 2.88k | ToType = ET->getDecl()->getIntegerType(); |
326 | | |
327 | 2.45M | switch (Second) { |
328 | | // 'bool' is an integral type; dispatch to the right place to handle it. |
329 | 192 | case ICK_Boolean_Conversion: |
330 | 192 | if (FromType->isRealFloatingType()) |
331 | 2 | goto FloatingIntegralConversion; |
332 | 190 | if (FromType->isIntegralOrUnscopedEnumerationType()) |
333 | 185 | goto IntegralConversion; |
334 | | // -- from a pointer type or pointer-to-member type to bool, or |
335 | 5 | return NK_Type_Narrowing; |
336 | | |
337 | | // -- from a floating-point type to an integer type, or |
338 | | // |
339 | | // -- from an integer type or unscoped enumeration type to a floating-point |
340 | | // type, except where the source is a constant expression and the actual |
341 | | // value after conversion will fit into the target type and will produce |
342 | | // the original value when converted back to the original type, or |
343 | 2.17k | case ICK_Floating_Integral: |
344 | 2.17k | FloatingIntegralConversion: |
345 | 2.17k | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)95 ) { |
346 | 95 | return NK_Type_Narrowing; |
347 | 2.07k | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
348 | 2.07k | ToType->isRealFloatingType()) { |
349 | 2.07k | if (IgnoreFloatToIntegralConversion) |
350 | 12 | return NK_Not_Narrowing; |
351 | 2.06k | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
352 | 2.06k | assert(Initializer && "Unknown conversion expression"); |
353 | | |
354 | | // If it's value-dependent, we can't tell whether it's narrowing. |
355 | 2.06k | if (Initializer->isValueDependent()) |
356 | 0 | return NK_Dependent_Narrowing; |
357 | | |
358 | 2.06k | if (Optional<llvm::APSInt> IntConstantValue = |
359 | 2.00k | Initializer->getIntegerConstantExpr(Ctx)) { |
360 | | // Convert the integer to the floating type. |
361 | 2.00k | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
362 | 2.00k | Result.convertFromAPInt(*IntConstantValue, IntConstantValue->isSigned(), |
363 | 2.00k | llvm::APFloat::rmNearestTiesToEven); |
364 | | // And back. |
365 | 2.00k | llvm::APSInt ConvertedValue = *IntConstantValue; |
366 | 2.00k | bool ignored; |
367 | 2.00k | Result.convertToInteger(ConvertedValue, |
368 | 2.00k | llvm::APFloat::rmTowardZero, &ignored); |
369 | | // If the resulting value is different, this was a narrowing conversion. |
370 | 2.00k | if (*IntConstantValue != ConvertedValue) { |
371 | 7 | ConstantValue = APValue(*IntConstantValue); |
372 | 7 | ConstantType = Initializer->getType(); |
373 | 7 | return NK_Constant_Narrowing; |
374 | 7 | } |
375 | 63 | } else { |
376 | | // Variables are always narrowings. |
377 | 63 | return NK_Variable_Narrowing; |
378 | 63 | } |
379 | 1.99k | } |
380 | 1.99k | return NK_Not_Narrowing; |
381 | | |
382 | | // -- from long double to double or float, or from double to float, except |
383 | | // where the source is a constant expression and the actual value after |
384 | | // conversion is within the range of values that can be represented (even |
385 | | // if it cannot be represented exactly), or |
386 | 1.70k | case ICK_Floating_Conversion: |
387 | 1.70k | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
388 | 1.70k | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
389 | | // FromType is larger than ToType. |
390 | 1.69k | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
391 | | |
392 | | // If it's value-dependent, we can't tell whether it's narrowing. |
393 | 1.69k | if (Initializer->isValueDependent()) |
394 | 0 | return NK_Dependent_Narrowing; |
395 | | |
396 | 1.69k | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
397 | | // Constant! |
398 | 1.65k | assert(ConstantValue.isFloat()); |
399 | 1.65k | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
400 | | // Convert the source value into the target type. |
401 | 1.65k | bool ignored; |
402 | 1.65k | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
403 | 1.65k | Ctx.getFloatTypeSemantics(ToType), |
404 | 1.65k | llvm::APFloat::rmNearestTiesToEven, &ignored); |
405 | | // If there was no overflow, the source value is within the range of |
406 | | // values that can be represented. |
407 | 1.65k | if (ConvertStatus & llvm::APFloat::opOverflow) { |
408 | 13 | ConstantType = Initializer->getType(); |
409 | 13 | return NK_Constant_Narrowing; |
410 | 13 | } |
411 | 32 | } else { |
412 | 32 | return NK_Variable_Narrowing; |
413 | 32 | } |
414 | 1.65k | } |
415 | 1.65k | return NK_Not_Narrowing; |
416 | | |
417 | | // -- from an integer type or unscoped enumeration type to an integer type |
418 | | // that cannot represent all the values of the original type, except where |
419 | | // the source is a constant expression and the actual value after |
420 | | // conversion will fit into the target type and will produce the original |
421 | | // value when converted back to the original type. |
422 | 248k | case ICK_Integral_Conversion: |
423 | 248k | IntegralConversion: { |
424 | 248k | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
425 | 248k | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
426 | 248k | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
427 | 248k | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
428 | 248k | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
429 | 248k | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
430 | | |
431 | 248k | if (FromWidth > ToWidth || |
432 | 219k | (FromWidth == ToWidth && FromSigned != ToSigned96.7k ) || |
433 | 198k | (138k FromSigned138k && !ToSigned136k )) { |
434 | | // Not all values of FromType can be represented in ToType. |
435 | 198k | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
436 | | |
437 | | // If it's value-dependent, we can't tell whether it's narrowing. |
438 | 198k | if (Initializer->isValueDependent()) |
439 | 295 | return NK_Dependent_Narrowing; |
440 | | |
441 | 197k | Optional<llvm::APSInt> OptInitializerValue; |
442 | 197k | if (!(OptInitializerValue = Initializer->getIntegerConstantExpr(Ctx))) { |
443 | | // Such conversions on variables are always narrowing. |
444 | 3.20k | return NK_Variable_Narrowing; |
445 | 3.20k | } |
446 | 194k | llvm::APSInt &InitializerValue = *OptInitializerValue; |
447 | 194k | bool Narrowing = false; |
448 | 194k | if (FromWidth < ToWidth) { |
449 | | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
450 | | // narrowing. |
451 | 88.2k | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
452 | 9 | Narrowing = true; |
453 | 106k | } else { |
454 | | // Add a bit to the InitializerValue so we don't have to worry about |
455 | | // signed vs. unsigned comparisons. |
456 | 106k | InitializerValue = InitializerValue.extend( |
457 | 106k | InitializerValue.getBitWidth() + 1); |
458 | | // Convert the initializer to and from the target width and signed-ness. |
459 | 106k | llvm::APSInt ConvertedValue = InitializerValue; |
460 | 106k | ConvertedValue = ConvertedValue.trunc(ToWidth); |
461 | 106k | ConvertedValue.setIsSigned(ToSigned); |
462 | 106k | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
463 | 106k | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
464 | | // If the result is different, this was a narrowing conversion. |
465 | 106k | if (ConvertedValue != InitializerValue) |
466 | 104 | Narrowing = true; |
467 | 106k | } |
468 | 194k | if (Narrowing) { |
469 | 113 | ConstantType = Initializer->getType(); |
470 | 113 | ConstantValue = APValue(InitializerValue); |
471 | 113 | return NK_Constant_Narrowing; |
472 | 113 | } |
473 | 244k | } |
474 | 244k | return NK_Not_Narrowing; |
475 | 244k | } |
476 | | |
477 | 2.20M | default: |
478 | | // Other kinds of conversions are not narrowings. |
479 | 2.20M | return NK_Not_Narrowing; |
480 | 2.45M | } |
481 | 2.45M | } |
482 | | |
483 | | /// dump - Print this standard conversion sequence to standard |
484 | | /// error. Useful for debugging overloading issues. |
485 | 0 | LLVM_DUMP_METHOD void StandardConversionSequence::dump() const { |
486 | 0 | raw_ostream &OS = llvm::errs(); |
487 | 0 | bool PrintedSomething = false; |
488 | 0 | if (First != ICK_Identity) { |
489 | 0 | OS << GetImplicitConversionName(First); |
490 | 0 | PrintedSomething = true; |
491 | 0 | } |
492 | |
|
493 | 0 | if (Second != ICK_Identity) { |
494 | 0 | if (PrintedSomething) { |
495 | 0 | OS << " -> "; |
496 | 0 | } |
497 | 0 | OS << GetImplicitConversionName(Second); |
498 | |
|
499 | 0 | if (CopyConstructor) { |
500 | 0 | OS << " (by copy constructor)"; |
501 | 0 | } else if (DirectBinding) { |
502 | 0 | OS << " (direct reference binding)"; |
503 | 0 | } else if (ReferenceBinding) { |
504 | 0 | OS << " (reference binding)"; |
505 | 0 | } |
506 | 0 | PrintedSomething = true; |
507 | 0 | } |
508 | |
|
509 | 0 | if (Third != ICK_Identity) { |
510 | 0 | if (PrintedSomething) { |
511 | 0 | OS << " -> "; |
512 | 0 | } |
513 | 0 | OS << GetImplicitConversionName(Third); |
514 | 0 | PrintedSomething = true; |
515 | 0 | } |
516 | |
|
517 | 0 | if (!PrintedSomething) { |
518 | 0 | OS << "No conversions required"; |
519 | 0 | } |
520 | 0 | } |
521 | | |
522 | | /// dump - Print this user-defined conversion sequence to standard |
523 | | /// error. Useful for debugging overloading issues. |
524 | 0 | void UserDefinedConversionSequence::dump() const { |
525 | 0 | raw_ostream &OS = llvm::errs(); |
526 | 0 | if (Before.First || Before.Second || Before.Third) { |
527 | 0 | Before.dump(); |
528 | 0 | OS << " -> "; |
529 | 0 | } |
530 | 0 | if (ConversionFunction) |
531 | 0 | OS << '\'' << *ConversionFunction << '\''; |
532 | 0 | else |
533 | 0 | OS << "aggregate initialization"; |
534 | 0 | if (After.First || After.Second || After.Third) { |
535 | 0 | OS << " -> "; |
536 | 0 | After.dump(); |
537 | 0 | } |
538 | 0 | } |
539 | | |
540 | | /// dump - Print this implicit conversion sequence to standard |
541 | | /// error. Useful for debugging overloading issues. |
542 | 0 | void ImplicitConversionSequence::dump() const { |
543 | 0 | raw_ostream &OS = llvm::errs(); |
544 | 0 | if (isStdInitializerListElement()) |
545 | 0 | OS << "Worst std::initializer_list element conversion: "; |
546 | 0 | switch (ConversionKind) { |
547 | 0 | case StandardConversion: |
548 | 0 | OS << "Standard conversion: "; |
549 | 0 | Standard.dump(); |
550 | 0 | break; |
551 | 0 | case UserDefinedConversion: |
552 | 0 | OS << "User-defined conversion: "; |
553 | 0 | UserDefined.dump(); |
554 | 0 | break; |
555 | 0 | case EllipsisConversion: |
556 | 0 | OS << "Ellipsis conversion"; |
557 | 0 | break; |
558 | 0 | case AmbiguousConversion: |
559 | 0 | OS << "Ambiguous conversion"; |
560 | 0 | break; |
561 | 0 | case BadConversion: |
562 | 0 | OS << "Bad conversion"; |
563 | 0 | break; |
564 | 0 | } |
565 | | |
566 | 0 | OS << "\n"; |
567 | 0 | } |
568 | | |
569 | 7.98k | void AmbiguousConversionSequence::construct() { |
570 | 7.98k | new (&conversions()) ConversionSet(); |
571 | 7.98k | } |
572 | | |
573 | 16.0k | void AmbiguousConversionSequence::destruct() { |
574 | 16.0k | conversions().~ConversionSet(); |
575 | 16.0k | } |
576 | | |
577 | | void |
578 | 8.01k | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
579 | 8.01k | FromTypePtr = O.FromTypePtr; |
580 | 8.01k | ToTypePtr = O.ToTypePtr; |
581 | 8.01k | new (&conversions()) ConversionSet(O.conversions()); |
582 | 8.01k | } |
583 | | |
584 | | namespace { |
585 | | // Structure used by DeductionFailureInfo to store |
586 | | // template argument information. |
587 | | struct DFIArguments { |
588 | | TemplateArgument FirstArg; |
589 | | TemplateArgument SecondArg; |
590 | | }; |
591 | | // Structure used by DeductionFailureInfo to store |
592 | | // template parameter and template argument information. |
593 | | struct DFIParamWithArguments : DFIArguments { |
594 | | TemplateParameter Param; |
595 | | }; |
596 | | // Structure used by DeductionFailureInfo to store template argument |
597 | | // information and the index of the problematic call argument. |
598 | | struct DFIDeducedMismatchArgs : DFIArguments { |
599 | | TemplateArgumentList *TemplateArgs; |
600 | | unsigned CallArgIndex; |
601 | | }; |
602 | | // Structure used by DeductionFailureInfo to store information about |
603 | | // unsatisfied constraints. |
604 | | struct CNSInfo { |
605 | | TemplateArgumentList *TemplateArgs; |
606 | | ConstraintSatisfaction Satisfaction; |
607 | | }; |
608 | | } |
609 | | |
610 | | /// Convert from Sema's representation of template deduction information |
611 | | /// to the form used in overload-candidate information. |
612 | | DeductionFailureInfo |
613 | | clang::MakeDeductionFailureInfo(ASTContext &Context, |
614 | | Sema::TemplateDeductionResult TDK, |
615 | 1.26M | TemplateDeductionInfo &Info) { |
616 | 1.26M | DeductionFailureInfo Result; |
617 | 1.26M | Result.Result = static_cast<unsigned>(TDK); |
618 | 1.26M | Result.HasDiagnostic = false; |
619 | 1.26M | switch (TDK) { |
620 | 16 | case Sema::TDK_Invalid: |
621 | 2.07k | case Sema::TDK_InstantiationDepth: |
622 | 32.6k | case Sema::TDK_TooManyArguments: |
623 | 171k | case Sema::TDK_TooFewArguments: |
624 | 182k | case Sema::TDK_MiscellaneousDeductionFailure: |
625 | 182k | case Sema::TDK_CUDATargetMismatch: |
626 | 182k | Result.Data = nullptr; |
627 | 182k | break; |
628 | | |
629 | 16.2k | case Sema::TDK_Incomplete: |
630 | 17.9k | case Sema::TDK_InvalidExplicitArguments: |
631 | 17.9k | Result.Data = Info.Param.getOpaqueValue(); |
632 | 17.9k | break; |
633 | | |
634 | 1.13k | case Sema::TDK_DeducedMismatch: |
635 | 1.13k | case Sema::TDK_DeducedMismatchNested: { |
636 | | // FIXME: Should allocate from normal heap so that we can free this later. |
637 | 1.13k | auto *Saved = new (Context) DFIDeducedMismatchArgs; |
638 | 1.13k | Saved->FirstArg = Info.FirstArg; |
639 | 1.13k | Saved->SecondArg = Info.SecondArg; |
640 | 1.13k | Saved->TemplateArgs = Info.take(); |
641 | 1.13k | Saved->CallArgIndex = Info.CallArgIndex; |
642 | 1.13k | Result.Data = Saved; |
643 | 1.13k | break; |
644 | 1.13k | } |
645 | | |
646 | 891k | case Sema::TDK_NonDeducedMismatch: { |
647 | | // FIXME: Should allocate from normal heap so that we can free this later. |
648 | 891k | DFIArguments *Saved = new (Context) DFIArguments; |
649 | 891k | Saved->FirstArg = Info.FirstArg; |
650 | 891k | Saved->SecondArg = Info.SecondArg; |
651 | 891k | Result.Data = Saved; |
652 | 891k | break; |
653 | 1.13k | } |
654 | | |
655 | 9 | case Sema::TDK_IncompletePack: |
656 | | // FIXME: It's slightly wasteful to allocate two TemplateArguments for this. |
657 | 20.4k | case Sema::TDK_Inconsistent: |
658 | 82.1k | case Sema::TDK_Underqualified: { |
659 | | // FIXME: Should allocate from normal heap so that we can free this later. |
660 | 82.1k | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
661 | 82.1k | Saved->Param = Info.Param; |
662 | 82.1k | Saved->FirstArg = Info.FirstArg; |
663 | 82.1k | Saved->SecondArg = Info.SecondArg; |
664 | 82.1k | Result.Data = Saved; |
665 | 82.1k | break; |
666 | 20.4k | } |
667 | | |
668 | 86.5k | case Sema::TDK_SubstitutionFailure: |
669 | 86.5k | Result.Data = Info.take(); |
670 | 86.5k | if (Info.hasSFINAEDiagnostic()) { |
671 | 86.5k | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
672 | 86.5k | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
673 | 86.5k | Info.takeSFINAEDiagnostic(*Diag); |
674 | 86.5k | Result.HasDiagnostic = true; |
675 | 86.5k | } |
676 | 86.5k | break; |
677 | | |
678 | 113 | case Sema::TDK_ConstraintsNotSatisfied: { |
679 | 113 | CNSInfo *Saved = new (Context) CNSInfo; |
680 | 113 | Saved->TemplateArgs = Info.take(); |
681 | 113 | Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction; |
682 | 113 | Result.Data = Saved; |
683 | 113 | break; |
684 | 20.4k | } |
685 | | |
686 | 0 | case Sema::TDK_Success: |
687 | 0 | case Sema::TDK_NonDependentConversionFailure: |
688 | 0 | llvm_unreachable("not a deduction failure"); |
689 | 1.26M | } |
690 | | |
691 | 1.26M | return Result; |
692 | 1.26M | } |
693 | | |
694 | 1.26M | void DeductionFailureInfo::Destroy() { |
695 | 1.26M | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
696 | 0 | case Sema::TDK_Success: |
697 | 16 | case Sema::TDK_Invalid: |
698 | 2.07k | case Sema::TDK_InstantiationDepth: |
699 | 18.3k | case Sema::TDK_Incomplete: |
700 | 48.9k | case Sema::TDK_TooManyArguments: |
701 | 187k | case Sema::TDK_TooFewArguments: |
702 | 188k | case Sema::TDK_InvalidExplicitArguments: |
703 | 188k | case Sema::TDK_CUDATargetMismatch: |
704 | 188k | case Sema::TDK_NonDependentConversionFailure: |
705 | 188k | break; |
706 | | |
707 | 9 | case Sema::TDK_IncompletePack: |
708 | 20.4k | case Sema::TDK_Inconsistent: |
709 | 82.1k | case Sema::TDK_Underqualified: |
710 | 83.3k | case Sema::TDK_DeducedMismatch: |
711 | 83.3k | case Sema::TDK_DeducedMismatchNested: |
712 | 975k | case Sema::TDK_NonDeducedMismatch: |
713 | | // FIXME: Destroy the data? |
714 | 975k | Data = nullptr; |
715 | 975k | break; |
716 | | |
717 | 86.5k | case Sema::TDK_SubstitutionFailure: |
718 | | // FIXME: Destroy the template argument list? |
719 | 86.5k | Data = nullptr; |
720 | 86.5k | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
721 | 86.5k | Diag->~PartialDiagnosticAt(); |
722 | 86.5k | HasDiagnostic = false; |
723 | 86.5k | } |
724 | 86.5k | break; |
725 | | |
726 | 113 | case Sema::TDK_ConstraintsNotSatisfied: |
727 | | // FIXME: Destroy the template argument list? |
728 | 113 | Data = nullptr; |
729 | 113 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
730 | 0 | Diag->~PartialDiagnosticAt(); |
731 | 0 | HasDiagnostic = false; |
732 | 0 | } |
733 | 113 | break; |
734 | | |
735 | | // Unhandled |
736 | 11.0k | case Sema::TDK_MiscellaneousDeductionFailure: |
737 | 11.0k | break; |
738 | 1.26M | } |
739 | 1.26M | } |
740 | | |
741 | 89.0k | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
742 | 89.0k | if (HasDiagnostic) |
743 | 88.8k | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
744 | 189 | return nullptr; |
745 | 189 | } |
746 | | |
747 | 3.15k | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
748 | 3.15k | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
749 | 0 | case Sema::TDK_Success: |
750 | 11 | case Sema::TDK_Invalid: |
751 | 14 | case Sema::TDK_InstantiationDepth: |
752 | 62 | case Sema::TDK_TooManyArguments: |
753 | 96 | case Sema::TDK_TooFewArguments: |
754 | 2.46k | case Sema::TDK_SubstitutionFailure: |
755 | 2.47k | case Sema::TDK_DeducedMismatch: |
756 | 2.48k | case Sema::TDK_DeducedMismatchNested: |
757 | 2.66k | case Sema::TDK_NonDeducedMismatch: |
758 | 2.67k | case Sema::TDK_CUDATargetMismatch: |
759 | 2.67k | case Sema::TDK_NonDependentConversionFailure: |
760 | 2.73k | case Sema::TDK_ConstraintsNotSatisfied: |
761 | 2.73k | return TemplateParameter(); |
762 | | |
763 | 232 | case Sema::TDK_Incomplete: |
764 | 297 | case Sema::TDK_InvalidExplicitArguments: |
765 | 297 | return TemplateParameter::getFromOpaqueValue(Data); |
766 | | |
767 | 9 | case Sema::TDK_IncompletePack: |
768 | 109 | case Sema::TDK_Inconsistent: |
769 | 116 | case Sema::TDK_Underqualified: |
770 | 116 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
771 | | |
772 | | // Unhandled |
773 | 12 | case Sema::TDK_MiscellaneousDeductionFailure: |
774 | 12 | break; |
775 | 12 | } |
776 | | |
777 | 12 | return TemplateParameter(); |
778 | 12 | } |
779 | | |
780 | 2.44k | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
781 | 2.44k | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
782 | 0 | case Sema::TDK_Success: |
783 | 0 | case Sema::TDK_Invalid: |
784 | 0 | case Sema::TDK_InstantiationDepth: |
785 | 0 | case Sema::TDK_TooManyArguments: |
786 | 0 | case Sema::TDK_TooFewArguments: |
787 | 0 | case Sema::TDK_Incomplete: |
788 | 0 | case Sema::TDK_IncompletePack: |
789 | 0 | case Sema::TDK_InvalidExplicitArguments: |
790 | 0 | case Sema::TDK_Inconsistent: |
791 | 0 | case Sema::TDK_Underqualified: |
792 | 0 | case Sema::TDK_NonDeducedMismatch: |
793 | 0 | case Sema::TDK_CUDATargetMismatch: |
794 | 0 | case Sema::TDK_NonDependentConversionFailure: |
795 | 0 | return nullptr; |
796 | |
|
797 | 10 | case Sema::TDK_DeducedMismatch: |
798 | 11 | case Sema::TDK_DeducedMismatchNested: |
799 | 11 | return static_cast<DFIDeducedMismatchArgs*>(Data)->TemplateArgs; |
800 | | |
801 | 2.37k | case Sema::TDK_SubstitutionFailure: |
802 | 2.37k | return static_cast<TemplateArgumentList*>(Data); |
803 | | |
804 | 58 | case Sema::TDK_ConstraintsNotSatisfied: |
805 | 58 | return static_cast<CNSInfo*>(Data)->TemplateArgs; |
806 | | |
807 | | // Unhandled |
808 | 0 | case Sema::TDK_MiscellaneousDeductionFailure: |
809 | 0 | break; |
810 | 0 | } |
811 | | |
812 | 0 | return nullptr; |
813 | 0 | } |
814 | | |
815 | 475 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
816 | 475 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
817 | 0 | case Sema::TDK_Success: |
818 | 0 | case Sema::TDK_Invalid: |
819 | 0 | case Sema::TDK_InstantiationDepth: |
820 | 0 | case Sema::TDK_Incomplete: |
821 | 0 | case Sema::TDK_TooManyArguments: |
822 | 0 | case Sema::TDK_TooFewArguments: |
823 | 0 | case Sema::TDK_InvalidExplicitArguments: |
824 | 0 | case Sema::TDK_SubstitutionFailure: |
825 | 0 | case Sema::TDK_CUDATargetMismatch: |
826 | 0 | case Sema::TDK_NonDependentConversionFailure: |
827 | 0 | case Sema::TDK_ConstraintsNotSatisfied: |
828 | 0 | return nullptr; |
829 | |
|
830 | 18 | case Sema::TDK_IncompletePack: |
831 | 276 | case Sema::TDK_Inconsistent: |
832 | 283 | case Sema::TDK_Underqualified: |
833 | 293 | case Sema::TDK_DeducedMismatch: |
834 | 294 | case Sema::TDK_DeducedMismatchNested: |
835 | 475 | case Sema::TDK_NonDeducedMismatch: |
836 | 475 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
837 | | |
838 | | // Unhandled |
839 | 0 | case Sema::TDK_MiscellaneousDeductionFailure: |
840 | 0 | break; |
841 | 0 | } |
842 | | |
843 | 0 | return nullptr; |
844 | 0 | } |
845 | | |
846 | 430 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
847 | 430 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
848 | 0 | case Sema::TDK_Success: |
849 | 0 | case Sema::TDK_Invalid: |
850 | 0 | case Sema::TDK_InstantiationDepth: |
851 | 0 | case Sema::TDK_Incomplete: |
852 | 0 | case Sema::TDK_IncompletePack: |
853 | 0 | case Sema::TDK_TooManyArguments: |
854 | 0 | case Sema::TDK_TooFewArguments: |
855 | 0 | case Sema::TDK_InvalidExplicitArguments: |
856 | 0 | case Sema::TDK_SubstitutionFailure: |
857 | 0 | case Sema::TDK_CUDATargetMismatch: |
858 | 0 | case Sema::TDK_NonDependentConversionFailure: |
859 | 0 | case Sema::TDK_ConstraintsNotSatisfied: |
860 | 0 | return nullptr; |
861 | |
|
862 | 231 | case Sema::TDK_Inconsistent: |
863 | 238 | case Sema::TDK_Underqualified: |
864 | 248 | case Sema::TDK_DeducedMismatch: |
865 | 249 | case Sema::TDK_DeducedMismatchNested: |
866 | 430 | case Sema::TDK_NonDeducedMismatch: |
867 | 430 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
868 | | |
869 | | // Unhandled |
870 | 0 | case Sema::TDK_MiscellaneousDeductionFailure: |
871 | 0 | break; |
872 | 0 | } |
873 | | |
874 | 0 | return nullptr; |
875 | 0 | } |
876 | | |
877 | 11 | llvm::Optional<unsigned> DeductionFailureInfo::getCallArgIndex() { |
878 | 11 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
879 | 10 | case Sema::TDK_DeducedMismatch: |
880 | 11 | case Sema::TDK_DeducedMismatchNested: |
881 | 11 | return static_cast<DFIDeducedMismatchArgs*>(Data)->CallArgIndex; |
882 | | |
883 | 0 | default: |
884 | 0 | return llvm::None; |
885 | 11 | } |
886 | 11 | } |
887 | | |
888 | | bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed( |
889 | 993k | OverloadedOperatorKind Op) { |
890 | 993k | if (!AllowRewrittenCandidates) |
891 | 988k | return false; |
892 | 5.07k | return Op == OO_EqualEqual || Op == OO_Spaceship3.91k ; |
893 | 5.07k | } |
894 | | |
895 | | bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed( |
896 | 881k | ASTContext &Ctx, const FunctionDecl *FD) { |
897 | 881k | if (!shouldAddReversed(FD->getDeclName().getCXXOverloadedOperator())) |
898 | 880k | return false; |
899 | | // Don't bother adding a reversed candidate that can never be a better |
900 | | // match than the non-reversed version. |
901 | 1.19k | return FD->getNumParams() != 2 || |
902 | 1.19k | !Ctx.hasSameUnqualifiedType(FD->getParamDecl(0)->getType(), |
903 | 1.19k | FD->getParamDecl(1)->getType()) || |
904 | 557 | FD->hasAttr<EnableIfAttr>(); |
905 | 1.19k | } |
906 | | |
907 | 17.6M | void OverloadCandidateSet::destroyCandidates() { |
908 | 31.6M | for (iterator i = begin(), e = end(); i != e; ++i13.9M ) { |
909 | 13.9M | for (auto &C : i->Conversions) |
910 | 23.4M | C.~ImplicitConversionSequence(); |
911 | 13.9M | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction6.85M ) |
912 | 803k | i->DeductionFailure.Destroy(); |
913 | 13.9M | } |
914 | 17.6M | } |
915 | | |
916 | 1.42M | void OverloadCandidateSet::clear(CandidateSetKind CSK) { |
917 | 1.42M | destroyCandidates(); |
918 | 1.42M | SlabAllocator.Reset(); |
919 | 1.42M | NumInlineBytesUsed = 0; |
920 | 1.42M | Candidates.clear(); |
921 | 1.42M | Functions.clear(); |
922 | 1.42M | Kind = CSK; |
923 | 1.42M | } |
924 | | |
925 | | namespace { |
926 | | class UnbridgedCastsSet { |
927 | | struct Entry { |
928 | | Expr **Addr; |
929 | | Expr *Saved; |
930 | | }; |
931 | | SmallVector<Entry, 2> Entries; |
932 | | |
933 | | public: |
934 | 7 | void save(Sema &S, Expr *&E) { |
935 | 7 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
936 | 7 | Entry entry = { &E, E }; |
937 | 7 | Entries.push_back(entry); |
938 | 7 | E = S.stripARCUnbridgedCast(E); |
939 | 7 | } |
940 | | |
941 | 811k | void restore() { |
942 | 811k | for (SmallVectorImpl<Entry>::iterator |
943 | 811k | i = Entries.begin(), e = Entries.end(); i != e; ++i7 ) |
944 | 7 | *i->Addr = i->Saved; |
945 | 811k | } |
946 | | }; |
947 | | } |
948 | | |
949 | | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
950 | | /// preprocessing on the given expression. |
951 | | /// |
952 | | /// \param unbridgedCasts a collection to which to add unbridged casts; |
953 | | /// without this, they will be immediately diagnosed as errors |
954 | | /// |
955 | | /// Return true on unrecoverable error. |
956 | | static bool |
957 | | checkPlaceholderForOverload(Sema &S, Expr *&E, |
958 | 8.49M | UnbridgedCastsSet *unbridgedCasts = nullptr) { |
959 | 8.49M | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
960 | | // We can't handle overloaded expressions here because overload |
961 | | // resolution might reasonably tweak them. |
962 | 801 | if (placeholder->getKind() == BuiltinType::Overload) return false792 ; |
963 | | |
964 | | // If the context potentially accepts unbridged ARC casts, strip |
965 | | // the unbridged cast and add it to the collection for later restoration. |
966 | 9 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
967 | 7 | unbridgedCasts) { |
968 | 7 | unbridgedCasts->save(S, E); |
969 | 7 | return false; |
970 | 7 | } |
971 | | |
972 | | // Go ahead and check everything else. |
973 | 2 | ExprResult result = S.CheckPlaceholderExpr(E); |
974 | 2 | if (result.isInvalid()) |
975 | 0 | return true; |
976 | | |
977 | 2 | E = result.get(); |
978 | 2 | return false; |
979 | 2 | } |
980 | | |
981 | | // Nothing to do. |
982 | 8.49M | return false; |
983 | 8.49M | } |
984 | | |
985 | | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
986 | | /// placeholders. |
987 | | static bool checkArgPlaceholdersForOverload(Sema &S, |
988 | | MultiExprArg Args, |
989 | 812k | UnbridgedCastsSet &unbridged) { |
990 | 1.71M | for (unsigned i = 0, e = Args.size(); i != e; ++i902k ) |
991 | 902k | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
992 | 0 | return true; |
993 | | |
994 | 812k | return false; |
995 | 812k | } |
996 | | |
997 | | /// Determine whether the given New declaration is an overload of the |
998 | | /// declarations in Old. This routine returns Ovl_Match or Ovl_NonFunction if |
999 | | /// New and Old cannot be overloaded, e.g., if New has the same signature as |
1000 | | /// some function in Old (C++ 1.3.10) or if the Old declarations aren't |
1001 | | /// functions (or function templates) at all. When it does return Ovl_Match or |
1002 | | /// Ovl_NonFunction, MatchedDecl will point to the decl that New cannot be |
1003 | | /// overloaded with. This decl may be a UsingShadowDecl on top of the underlying |
1004 | | /// declaration. |
1005 | | /// |
1006 | | /// Example: Given the following input: |
1007 | | /// |
1008 | | /// void f(int, float); // #1 |
1009 | | /// void f(int, int); // #2 |
1010 | | /// int f(int, int); // #3 |
1011 | | /// |
1012 | | /// When we process #1, there is no previous declaration of "f", so IsOverload |
1013 | | /// will not be used. |
1014 | | /// |
1015 | | /// When we process #2, Old contains only the FunctionDecl for #1. By comparing |
1016 | | /// the parameter types, we see that #1 and #2 are overloaded (since they have |
1017 | | /// different signatures), so this routine returns Ovl_Overload; MatchedDecl is |
1018 | | /// unchanged. |
1019 | | /// |
1020 | | /// When we process #3, Old is an overload set containing #1 and #2. We compare |
1021 | | /// the signatures of #3 to #1 (they're overloaded, so we do nothing) and then |
1022 | | /// #3 to #2. Since the signatures of #3 and #2 are identical (return types of |
1023 | | /// functions are not part of the signature), IsOverload returns Ovl_Match and |
1024 | | /// MatchedDecl will be set to point to the FunctionDecl for #2. |
1025 | | /// |
1026 | | /// 'NewIsUsingShadowDecl' indicates that 'New' is being introduced into a class |
1027 | | /// by a using declaration. The rules for whether to hide shadow declarations |
1028 | | /// ignore some properties which otherwise figure into a function template's |
1029 | | /// signature. |
1030 | | Sema::OverloadKind |
1031 | | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
1032 | 7.80M | NamedDecl *&Match, bool NewIsUsingDecl) { |
1033 | 7.80M | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
1034 | 56.8M | I != E; ++I49.0M ) { |
1035 | 49.2M | NamedDecl *OldD = *I; |
1036 | | |
1037 | 49.2M | bool OldIsUsingDecl = false; |
1038 | 49.2M | if (isa<UsingShadowDecl>(OldD)) { |
1039 | 11.3k | OldIsUsingDecl = true; |
1040 | | |
1041 | | // We can always introduce two using declarations into the same |
1042 | | // context, even if they have identical signatures. |
1043 | 11.3k | if (NewIsUsingDecl) continue379 ; |
1044 | | |
1045 | 10.9k | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
1046 | 10.9k | } |
1047 | | |
1048 | | // A using-declaration does not conflict with another declaration |
1049 | | // if one of them is hidden. |
1050 | 49.2M | if ((OldIsUsingDecl || NewIsUsingDecl49.2M ) && !isVisible(*I)11.4k ) |
1051 | 0 | continue; |
1052 | | |
1053 | | // If either declaration was introduced by a using declaration, |
1054 | | // we'll need to use slightly different rules for matching. |
1055 | | // Essentially, these rules are the normal rules, except that |
1056 | | // function templates hide function templates with different |
1057 | | // return types or template parameter lists. |
1058 | 49.2M | bool UseMemberUsingDeclRules = |
1059 | 49.2M | (OldIsUsingDecl || NewIsUsingDecl49.2M ) && CurContext->isRecord()11.4k && |
1060 | 7.32k | !New->getFriendObjectKind(); |
1061 | | |
1062 | 49.2M | if (FunctionDecl *OldF = OldD->getAsFunction()) { |
1063 | 49.2M | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
1064 | 224k | if (UseMemberUsingDeclRules && OldIsUsingDecl293 ) { |
1065 | 270 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
1066 | 270 | continue; |
1067 | 270 | } |
1068 | | |
1069 | 223k | if (!isa<FunctionTemplateDecl>(OldD) && |
1070 | 163k | !shouldLinkPossiblyHiddenDecl(*I, New)) |
1071 | 0 | continue; |
1072 | | |
1073 | 223k | Match = *I; |
1074 | 223k | return Ovl_Match; |
1075 | 223k | } |
1076 | | |
1077 | | // Builtins that have custom typechecking or have a reference should |
1078 | | // not be overloadable or redeclarable. |
1079 | 49.0M | if (!getASTContext().canBuiltinBeRedeclared(OldF)) { |
1080 | 3 | Match = *I; |
1081 | 3 | return Ovl_NonFunction; |
1082 | 3 | } |
1083 | 286 | } else if (isa<UsingDecl>(OldD) || isa<UsingPackDecl>(OldD)162 ) { |
1084 | | // We can overload with these, which can show up when doing |
1085 | | // redeclaration checks for UsingDecls. |
1086 | 124 | assert(Old.getLookupKind() == LookupUsingDeclName); |
1087 | 162 | } else if (isa<TagDecl>(OldD)) { |
1088 | | // We can always overload with tags by hiding them. |
1089 | 160 | } else if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(OldD)) { |
1090 | | // Optimistically assume that an unresolved using decl will |
1091 | | // overload; if it doesn't, we'll have to diagnose during |
1092 | | // template instantiation. |
1093 | | // |
1094 | | // Exception: if the scope is dependent and this is not a class |
1095 | | // member, the using declaration can only introduce an enumerator. |
1096 | 139 | if (UUD->getQualifier()->isDependent() && !UUD->isCXXClassMember()) { |
1097 | 5 | Match = *I; |
1098 | 5 | return Ovl_NonFunction; |
1099 | 5 | } |
1100 | 21 | } else { |
1101 | | // (C++ 13p1): |
1102 | | // Only function declarations can be overloaded; object and type |
1103 | | // declarations cannot be overloaded. |
1104 | 21 | Match = *I; |
1105 | 21 | return Ovl_NonFunction; |
1106 | 21 | } |
1107 | 49.2M | } |
1108 | | |
1109 | | // C++ [temp.friend]p1: |
1110 | | // For a friend function declaration that is not a template declaration: |
1111 | | // -- if the name of the friend is a qualified or unqualified template-id, |
1112 | | // [...], otherwise |
1113 | | // -- if the name of the friend is a qualified-id and a matching |
1114 | | // non-template function is found in the specified class or namespace, |
1115 | | // the friend declaration refers to that function, otherwise, |
1116 | | // -- if the name of the friend is a qualified-id and a matching function |
1117 | | // template is found in the specified class or namespace, the friend |
1118 | | // declaration refers to the deduced specialization of that function |
1119 | | // template, otherwise |
1120 | | // -- the name shall be an unqualified-id [...] |
1121 | | // If we get here for a qualified friend declaration, we've just reached the |
1122 | | // third bullet. If the type of the friend is dependent, skip this lookup |
1123 | | // until instantiation. |
1124 | 7.58M | if (New->getFriendObjectKind() && New->getQualifier()28.9k && |
1125 | 60 | !New->getDescribedFunctionTemplate() && |
1126 | 48 | !New->getDependentSpecializationInfo() && |
1127 | 42 | !New->getType()->isDependentType()) { |
1128 | 37 | LookupResult TemplateSpecResult(LookupResult::Temporary, Old); |
1129 | 37 | TemplateSpecResult.addAllDecls(Old); |
1130 | 37 | if (CheckFunctionTemplateSpecialization(New, nullptr, TemplateSpecResult, |
1131 | 21 | /*QualifiedFriend*/true)) { |
1132 | 21 | New->setInvalidDecl(); |
1133 | 21 | return Ovl_Overload; |
1134 | 21 | } |
1135 | | |
1136 | 16 | Match = TemplateSpecResult.getAsSingle<FunctionDecl>(); |
1137 | 16 | return Ovl_Match; |
1138 | 16 | } |
1139 | | |
1140 | 7.58M | return Ovl_Overload; |
1141 | 7.58M | } |
1142 | | |
1143 | | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
1144 | | bool UseMemberUsingDeclRules, bool ConsiderCudaAttrs, |
1145 | 49.2M | bool ConsiderRequiresClauses) { |
1146 | | // C++ [basic.start.main]p2: This function shall not be overloaded. |
1147 | 49.2M | if (New->isMain()) |
1148 | 17 | return false; |
1149 | | |
1150 | | // MSVCRT user defined entry points cannot be overloaded. |
1151 | 49.2M | if (New->isMSVCRTEntryPoint()) |
1152 | 1 | return false; |
1153 | | |
1154 | 49.2M | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
1155 | 49.2M | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
1156 | | |
1157 | | // C++ [temp.fct]p2: |
1158 | | // A function template can be overloaded with other function templates |
1159 | | // and with normal (non-template) functions. |
1160 | 49.2M | if ((OldTemplate == nullptr) != (NewTemplate == nullptr)) |
1161 | 1.30M | return true; |
1162 | | |
1163 | | // Is the function New an overload of the function Old? |
1164 | 47.9M | QualType OldQType = Context.getCanonicalType(Old->getType()); |
1165 | 47.9M | QualType NewQType = Context.getCanonicalType(New->getType()); |
1166 | | |
1167 | | // Compare the signatures (C++ 1.3.10) of the two functions to |
1168 | | // determine whether they are overloads. If we find any mismatch |
1169 | | // in the signature, they are overloads. |
1170 | | |
1171 | | // If either of these functions is a K&R-style function (no |
1172 | | // prototype), then we consider them to have matching signatures. |
1173 | 47.9M | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
1174 | 47.9M | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
1175 | 0 | return false; |
1176 | | |
1177 | 47.9M | const FunctionProtoType *OldType = cast<FunctionProtoType>(OldQType); |
1178 | 47.9M | const FunctionProtoType *NewType = cast<FunctionProtoType>(NewQType); |
1179 | | |
1180 | | // The signature of a function includes the types of its |
1181 | | // parameters (C++ 1.3.10), which includes the presence or absence |
1182 | | // of the ellipsis; see C++ DR 357). |
1183 | 47.9M | if (OldQType != NewQType && |
1184 | 47.6M | (OldType->getNumParams() != NewType->getNumParams() || |
1185 | 46.4M | OldType->isVariadic() != NewType->isVariadic() || |
1186 | 46.4M | !FunctionParamTypesAreEqual(OldType, NewType))) |
1187 | 47.5M | return true; |
1188 | | |
1189 | | // C++ [temp.over.link]p4: |
1190 | | // The signature of a function template consists of its function |
1191 | | // signature, its return type and its template parameter list. The names |
1192 | | // of the template parameters are significant only for establishing the |
1193 | | // relationship between the template parameters and the rest of the |
1194 | | // signature. |
1195 | | // |
1196 | | // We check the return type and template parameter lists for function |
1197 | | // templates first; the remaining checks follow. |
1198 | | // |
1199 | | // However, we don't consider either of these when deciding whether |
1200 | | // a member introduced by a shadow declaration is hidden. |
1201 | 390k | if (!UseMemberUsingDeclRules && NewTemplate390k && |
1202 | 106k | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
1203 | 106k | OldTemplate->getTemplateParameters(), |
1204 | 106k | false, TPL_TemplateMatch) || |
1205 | 78.6k | !Context.hasSameType(Old->getDeclaredReturnType(), |
1206 | 78.6k | New->getDeclaredReturnType()))) |
1207 | 46.7k | return true; |
1208 | | |
1209 | | // If the function is a class member, its signature includes the |
1210 | | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
1211 | | // |
1212 | | // As part of this, also check whether one of the member functions |
1213 | | // is static, in which case they are not overloads (C++ |
1214 | | // 13.1p2). While not part of the definition of the signature, |
1215 | | // this check is important to determine whether these functions |
1216 | | // can be overloaded. |
1217 | 343k | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
1218 | 343k | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
1219 | 343k | if (OldMethod && NewMethod272k && |
1220 | 272k | !OldMethod->isStatic() && !NewMethod->isStatic()262k ) { |
1221 | 262k | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
1222 | 298 | if (!UseMemberUsingDeclRules && |
1223 | 298 | (OldMethod->getRefQualifier() == RQ_None || |
1224 | 297 | NewMethod->getRefQualifier() == RQ_None)) { |
1225 | | // C++0x [over.load]p2: |
1226 | | // - Member function declarations with the same name and the same |
1227 | | // parameter-type-list as well as member function template |
1228 | | // declarations with the same name, the same parameter-type-list, and |
1229 | | // the same template parameter lists cannot be overloaded if any of |
1230 | | // them, but not all, have a ref-qualifier (8.3.5). |
1231 | 11 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
1232 | 11 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
1233 | 11 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
1234 | 11 | } |
1235 | 298 | return true; |
1236 | 298 | } |
1237 | | |
1238 | | // We may not have applied the implicit const for a constexpr member |
1239 | | // function yet (because we haven't yet resolved whether this is a static |
1240 | | // or non-static member function). Add it now, on the assumption that this |
1241 | | // is a redeclaration of OldMethod. |
1242 | 262k | auto OldQuals = OldMethod->getMethodQualifiers(); |
1243 | 262k | auto NewQuals = NewMethod->getMethodQualifiers(); |
1244 | 262k | if (!getLangOpts().CPlusPlus14 && NewMethod->isConstexpr()241k && |
1245 | 2.06k | !isa<CXXConstructorDecl>(NewMethod)) |
1246 | 283 | NewQuals.addConst(); |
1247 | | // We do not allow overloading based off of '__restrict'. |
1248 | 262k | OldQuals.removeRestrict(); |
1249 | 262k | NewQuals.removeRestrict(); |
1250 | 262k | if (OldQuals != NewQuals) |
1251 | 83.0k | return true; |
1252 | 260k | } |
1253 | | |
1254 | | // Though pass_object_size is placed on parameters and takes an argument, we |
1255 | | // consider it to be a function-level modifier for the sake of function |
1256 | | // identity. Either the function has one or more parameters with |
1257 | | // pass_object_size or it doesn't. |
1258 | 260k | if (functionHasPassObjectSizeParams(New) != |
1259 | 260k | functionHasPassObjectSizeParams(Old)) |
1260 | 16 | return true; |
1261 | | |
1262 | | // enable_if attributes are an order-sensitive part of the signature. |
1263 | 260k | for (specific_attr_iterator<EnableIfAttr> |
1264 | 260k | NewI = New->specific_attr_begin<EnableIfAttr>(), |
1265 | 260k | NewE = New->specific_attr_end<EnableIfAttr>(), |
1266 | 260k | OldI = Old->specific_attr_begin<EnableIfAttr>(), |
1267 | 260k | OldE = Old->specific_attr_end<EnableIfAttr>(); |
1268 | 260k | NewI != NewE || OldI != OldE255k ; ++NewI, ++OldI12 ) { |
1269 | 4.53k | if (NewI == NewE || OldI == OldE4.51k ) |
1270 | 4.47k | return true; |
1271 | 62 | llvm::FoldingSetNodeID NewID, OldID; |
1272 | 62 | NewI->getCond()->Profile(NewID, Context, true); |
1273 | 62 | OldI->getCond()->Profile(OldID, Context, true); |
1274 | 62 | if (NewID != OldID) |
1275 | 50 | return true; |
1276 | 62 | } |
1277 | | |
1278 | 255k | if (getLangOpts().CUDA && ConsiderCudaAttrs529 ) { |
1279 | | // Don't allow overloading of destructors. (In theory we could, but it |
1280 | | // would be a giant change to clang.) |
1281 | 469 | if (!isa<CXXDestructorDecl>(New)) { |
1282 | 455 | CUDAFunctionTarget NewTarget = IdentifyCUDATarget(New), |
1283 | 455 | OldTarget = IdentifyCUDATarget(Old); |
1284 | 455 | if (NewTarget != CFT_InvalidTarget) { |
1285 | 455 | assert((OldTarget != CFT_InvalidTarget) && |
1286 | 455 | "Unexpected invalid target."); |
1287 | | |
1288 | | // Allow overloading of functions with same signature and different CUDA |
1289 | | // target attributes. |
1290 | 455 | if (NewTarget != OldTarget) |
1291 | 385 | return true; |
1292 | 255k | } |
1293 | 455 | } |
1294 | 469 | } |
1295 | | |
1296 | 255k | if (ConsiderRequiresClauses) { |
1297 | 224k | Expr *NewRC = New->getTrailingRequiresClause(), |
1298 | 224k | *OldRC = Old->getTrailingRequiresClause(); |
1299 | 224k | if ((NewRC != nullptr) != (OldRC != nullptr)) |
1300 | | // RC are most certainly different - these are overloads. |
1301 | 5 | return true; |
1302 | | |
1303 | 224k | if (NewRC) { |
1304 | 35 | llvm::FoldingSetNodeID NewID, OldID; |
1305 | 35 | NewRC->Profile(NewID, Context, /*Canonical=*/true); |
1306 | 35 | OldRC->Profile(OldID, Context, /*Canonical=*/true); |
1307 | 35 | if (NewID != OldID) |
1308 | | // RCs are not equivalent - these are overloads. |
1309 | 35 | return true; |
1310 | 255k | } |
1311 | 224k | } |
1312 | | |
1313 | | // The signatures match; this is not an overload. |
1314 | 255k | return false; |
1315 | 255k | } |
1316 | | |
1317 | | /// Tries a user-defined conversion from From to ToType. |
1318 | | /// |
1319 | | /// Produces an implicit conversion sequence for when a standard conversion |
1320 | | /// is not an option. See TryImplicitConversion for more information. |
1321 | | static ImplicitConversionSequence |
1322 | | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
1323 | | bool SuppressUserConversions, |
1324 | | AllowedExplicit AllowExplicit, |
1325 | | bool InOverloadResolution, |
1326 | | bool CStyle, |
1327 | | bool AllowObjCWritebackConversion, |
1328 | 1.25M | bool AllowObjCConversionOnExplicit) { |
1329 | 1.25M | ImplicitConversionSequence ICS; |
1330 | | |
1331 | 1.25M | if (SuppressUserConversions) { |
1332 | | // We're not in the case above, so there is no conversion that |
1333 | | // we can perform. |
1334 | 186k | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1335 | 186k | return ICS; |
1336 | 186k | } |
1337 | | |
1338 | | // Attempt user-defined conversion. |
1339 | 1.07M | OverloadCandidateSet Conversions(From->getExprLoc(), |
1340 | 1.07M | OverloadCandidateSet::CSK_Normal); |
1341 | 1.07M | switch (IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, |
1342 | 1.07M | Conversions, AllowExplicit, |
1343 | 1.07M | AllowObjCConversionOnExplicit)) { |
1344 | 406k | case OR_Success: |
1345 | 406k | case OR_Deleted: |
1346 | 406k | ICS.setUserDefined(); |
1347 | | // C++ [over.ics.user]p4: |
1348 | | // A conversion of an expression of class type to the same class |
1349 | | // type is given Exact Match rank, and a conversion of an |
1350 | | // expression of class type to a base class of that type is |
1351 | | // given Conversion rank, in spite of the fact that a copy |
1352 | | // constructor (i.e., a user-defined conversion function) is |
1353 | | // called for those cases. |
1354 | 406k | if (CXXConstructorDecl *Constructor |
1355 | 26.0k | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
1356 | 26.0k | QualType FromCanon |
1357 | 26.0k | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
1358 | 26.0k | QualType ToCanon |
1359 | 26.0k | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
1360 | 26.0k | if (Constructor->isCopyConstructor() && |
1361 | 0 | (FromCanon == ToCanon || |
1362 | 0 | S.IsDerivedFrom(From->getBeginLoc(), FromCanon, ToCanon))) { |
1363 | | // Turn this into a "standard" conversion sequence, so that it |
1364 | | // gets ranked with standard conversion sequences. |
1365 | 0 | DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction; |
1366 | 0 | ICS.setStandard(); |
1367 | 0 | ICS.Standard.setAsIdentityConversion(); |
1368 | 0 | ICS.Standard.setFromType(From->getType()); |
1369 | 0 | ICS.Standard.setAllToTypes(ToType); |
1370 | 0 | ICS.Standard.CopyConstructor = Constructor; |
1371 | 0 | ICS.Standard.FoundCopyConstructor = Found; |
1372 | 0 | if (ToCanon != FromCanon) |
1373 | 0 | ICS.Standard.Second = ICK_Derived_To_Base; |
1374 | 0 | } |
1375 | 26.0k | } |
1376 | 406k | break; |
1377 | | |
1378 | 7.98k | case OR_Ambiguous: |
1379 | 7.98k | ICS.setAmbiguous(); |
1380 | 7.98k | ICS.Ambiguous.setFromType(From->getType()); |
1381 | 7.98k | ICS.Ambiguous.setToType(ToType); |
1382 | 7.98k | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
1383 | 24.2k | Cand != Conversions.end(); ++Cand16.2k ) |
1384 | 16.2k | if (Cand->Best) |
1385 | 15.9k | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
1386 | 7.98k | break; |
1387 | | |
1388 | | // Fall through. |
1389 | 657k | case OR_No_Viable_Function: |
1390 | 657k | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1391 | 657k | break; |
1392 | 1.07M | } |
1393 | | |
1394 | 1.07M | return ICS; |
1395 | 1.07M | } |
1396 | | |
1397 | | /// TryImplicitConversion - Attempt to perform an implicit conversion |
1398 | | /// from the given expression (Expr) to the given type (ToType). This |
1399 | | /// function returns an implicit conversion sequence that can be used |
1400 | | /// to perform the initialization. Given |
1401 | | /// |
1402 | | /// void f(float f); |
1403 | | /// void g(int i) { f(i); } |
1404 | | /// |
1405 | | /// this routine would produce an implicit conversion sequence to |
1406 | | /// describe the initialization of f from i, which will be a standard |
1407 | | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
1408 | | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
1409 | | // |
1410 | | /// Note that this routine only determines how the conversion can be |
1411 | | /// performed; it does not actually perform the conversion. As such, |
1412 | | /// it will not produce any diagnostics if no conversion is available, |
1413 | | /// but will instead return an implicit conversion sequence of kind |
1414 | | /// "BadConversion". |
1415 | | /// |
1416 | | /// If @p SuppressUserConversions, then user-defined conversions are |
1417 | | /// not permitted. |
1418 | | /// If @p AllowExplicit, then explicit user-defined conversions are |
1419 | | /// permitted. |
1420 | | /// |
1421 | | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
1422 | | /// writeback conversion, which allows __autoreleasing id* parameters to |
1423 | | /// be initialized with __strong id* or __weak id* arguments. |
1424 | | static ImplicitConversionSequence |
1425 | | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
1426 | | bool SuppressUserConversions, |
1427 | | AllowedExplicit AllowExplicit, |
1428 | | bool InOverloadResolution, |
1429 | | bool CStyle, |
1430 | | bool AllowObjCWritebackConversion, |
1431 | 23.2M | bool AllowObjCConversionOnExplicit) { |
1432 | 23.2M | ImplicitConversionSequence ICS; |
1433 | 23.2M | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
1434 | 21.6M | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
1435 | 21.6M | ICS.setStandard(); |
1436 | 21.6M | return ICS; |
1437 | 21.6M | } |
1438 | | |
1439 | 1.59M | if (!S.getLangOpts().CPlusPlus) { |
1440 | 290k | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1441 | 290k | return ICS; |
1442 | 290k | } |
1443 | | |
1444 | | // C++ [over.ics.user]p4: |
1445 | | // A conversion of an expression of class type to the same class |
1446 | | // type is given Exact Match rank, and a conversion of an |
1447 | | // expression of class type to a base class of that type is |
1448 | | // given Conversion rank, in spite of the fact that a copy/move |
1449 | | // constructor (i.e., a user-defined conversion function) is |
1450 | | // called for those cases. |
1451 | 1.30M | QualType FromType = From->getType(); |
1452 | 1.30M | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>()257k && |
1453 | 153k | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
1454 | 108k | S.IsDerivedFrom(From->getBeginLoc(), FromType, ToType))) { |
1455 | 45.7k | ICS.setStandard(); |
1456 | 45.7k | ICS.Standard.setAsIdentityConversion(); |
1457 | 45.7k | ICS.Standard.setFromType(FromType); |
1458 | 45.7k | ICS.Standard.setAllToTypes(ToType); |
1459 | | |
1460 | | // We don't actually check at this point whether there is a valid |
1461 | | // copy/move constructor, since overloading just assumes that it |
1462 | | // exists. When we actually perform initialization, we'll find the |
1463 | | // appropriate constructor to copy the returned object, if needed. |
1464 | 45.7k | ICS.Standard.CopyConstructor = nullptr; |
1465 | | |
1466 | | // Determine whether this is considered a derived-to-base conversion. |
1467 | 45.7k | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
1468 | 617 | ICS.Standard.Second = ICK_Derived_To_Base; |
1469 | | |
1470 | 45.7k | return ICS; |
1471 | 45.7k | } |
1472 | | |
1473 | 1.25M | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
1474 | 1.25M | AllowExplicit, InOverloadResolution, CStyle, |
1475 | 1.25M | AllowObjCWritebackConversion, |
1476 | 1.25M | AllowObjCConversionOnExplicit); |
1477 | 1.25M | } |
1478 | | |
1479 | | ImplicitConversionSequence |
1480 | | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
1481 | | bool SuppressUserConversions, |
1482 | | AllowedExplicit AllowExplicit, |
1483 | | bool InOverloadResolution, |
1484 | | bool CStyle, |
1485 | 3.72M | bool AllowObjCWritebackConversion) { |
1486 | 3.72M | return ::TryImplicitConversion(*this, From, ToType, SuppressUserConversions, |
1487 | 3.72M | AllowExplicit, InOverloadResolution, CStyle, |
1488 | 3.72M | AllowObjCWritebackConversion, |
1489 | 3.72M | /*AllowObjCConversionOnExplicit=*/false); |
1490 | 3.72M | } |
1491 | | |
1492 | | /// PerformImplicitConversion - Perform an implicit conversion of the |
1493 | | /// expression From to the type ToType. Returns the |
1494 | | /// converted expression. Flavor is the kind of conversion we're |
1495 | | /// performing, used in the error message. If @p AllowExplicit, |
1496 | | /// explicit user-defined conversions are permitted. |
1497 | | ExprResult Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
1498 | | AssignmentAction Action, |
1499 | 2.94M | bool AllowExplicit) { |
1500 | 2.94M | if (checkPlaceholderForOverload(*this, From)) |
1501 | 0 | return ExprError(); |
1502 | | |
1503 | | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
1504 | 2.94M | bool AllowObjCWritebackConversion |
1505 | 2.94M | = getLangOpts().ObjCAutoRefCount && |
1506 | 843 | (Action == AA_Passing || Action == AA_Sending); |
1507 | 2.94M | if (getLangOpts().ObjC) |
1508 | 28.9k | CheckObjCBridgeRelatedConversions(From->getBeginLoc(), ToType, |
1509 | 28.9k | From->getType(), From); |
1510 | 2.94M | ImplicitConversionSequence ICS = ::TryImplicitConversion( |
1511 | 2.94M | *this, From, ToType, |
1512 | 2.94M | /*SuppressUserConversions=*/false, |
1513 | 1.78M | AllowExplicit ? AllowedExplicit::All : AllowedExplicit::None1.15M , |
1514 | 2.94M | /*InOverloadResolution=*/false, |
1515 | 2.94M | /*CStyle=*/false, AllowObjCWritebackConversion, |
1516 | 2.94M | /*AllowObjCConversionOnExplicit=*/false); |
1517 | 2.94M | return PerformImplicitConversion(From, ToType, ICS, Action); |
1518 | 2.94M | } |
1519 | | |
1520 | | /// Determine whether the conversion from FromType to ToType is a valid |
1521 | | /// conversion that strips "noexcept" or "noreturn" off the nested function |
1522 | | /// type. |
1523 | | bool Sema::IsFunctionConversion(QualType FromType, QualType ToType, |
1524 | 22.2M | QualType &ResultTy) { |
1525 | 22.2M | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
1526 | 21.3M | return false; |
1527 | | |
1528 | | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
1529 | | // or F(t noexcept) -> F(t) |
1530 | | // where F adds one of the following at most once: |
1531 | | // - a pointer |
1532 | | // - a member pointer |
1533 | | // - a block pointer |
1534 | | // Changes here need matching changes in FindCompositePointerType. |
1535 | 926k | CanQualType CanTo = Context.getCanonicalType(ToType); |
1536 | 926k | CanQualType CanFrom = Context.getCanonicalType(FromType); |
1537 | 926k | Type::TypeClass TyClass = CanTo->getTypeClass(); |
1538 | 926k | if (TyClass != CanFrom->getTypeClass()) return false174k ; |
1539 | 751k | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto743k ) { |
1540 | 743k | if (TyClass == Type::Pointer) { |
1541 | 444k | CanTo = CanTo.castAs<PointerType>()->getPointeeType(); |
1542 | 444k | CanFrom = CanFrom.castAs<PointerType>()->getPointeeType(); |
1543 | 299k | } else if (TyClass == Type::BlockPointer) { |
1544 | 12 | CanTo = CanTo.castAs<BlockPointerType>()->getPointeeType(); |
1545 | 12 | CanFrom = CanFrom.castAs<BlockPointerType>()->getPointeeType(); |
1546 | 298k | } else if (TyClass == Type::MemberPointer) { |
1547 | 615 | auto ToMPT = CanTo.castAs<MemberPointerType>(); |
1548 | 615 | auto FromMPT = CanFrom.castAs<MemberPointerType>(); |
1549 | | // A function pointer conversion cannot change the class of the function. |
1550 | 615 | if (ToMPT->getClass() != FromMPT->getClass()) |
1551 | 139 | return false; |
1552 | 476 | CanTo = ToMPT->getPointeeType(); |
1553 | 476 | CanFrom = FromMPT->getPointeeType(); |
1554 | 298k | } else { |
1555 | 298k | return false; |
1556 | 298k | } |
1557 | | |
1558 | 444k | TyClass = CanTo->getTypeClass(); |
1559 | 444k | if (TyClass != CanFrom->getTypeClass()) return false166k ; |
1560 | 278k | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto277k ) |
1561 | 277k | return false; |
1562 | 9.71k | } |
1563 | | |
1564 | 9.71k | const auto *FromFn = cast<FunctionType>(CanFrom); |
1565 | 9.71k | FunctionType::ExtInfo FromEInfo = FromFn->getExtInfo(); |
1566 | | |
1567 | 9.71k | const auto *ToFn = cast<FunctionType>(CanTo); |
1568 | 9.71k | FunctionType::ExtInfo ToEInfo = ToFn->getExtInfo(); |
1569 | | |
1570 | 9.71k | bool Changed = false; |
1571 | | |
1572 | | // Drop 'noreturn' if not present in target type. |
1573 | 9.71k | if (FromEInfo.getNoReturn() && !ToEInfo.getNoReturn()119 ) { |
1574 | 110 | FromFn = Context.adjustFunctionType(FromFn, FromEInfo.withNoReturn(false)); |
1575 | 110 | Changed = true; |
1576 | 110 | } |
1577 | | |
1578 | | // Drop 'noexcept' if not present in target type. |
1579 | 9.71k | if (const auto *FromFPT = dyn_cast<FunctionProtoType>(FromFn)) { |
1580 | 9.71k | const auto *ToFPT = cast<FunctionProtoType>(ToFn); |
1581 | 9.71k | if (FromFPT->isNothrow() && !ToFPT->isNothrow()848 ) { |
1582 | 846 | FromFn = cast<FunctionType>( |
1583 | 846 | Context.getFunctionTypeWithExceptionSpec(QualType(FromFPT, 0), |
1584 | 846 | EST_None) |
1585 | 846 | .getTypePtr()); |
1586 | 846 | Changed = true; |
1587 | 846 | } |
1588 | | |
1589 | | // Convert FromFPT's ExtParameterInfo if necessary. The conversion is valid |
1590 | | // only if the ExtParameterInfo lists of the two function prototypes can be |
1591 | | // merged and the merged list is identical to ToFPT's ExtParameterInfo list. |
1592 | 9.71k | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
1593 | 9.71k | bool CanUseToFPT, CanUseFromFPT; |
1594 | 9.71k | if (Context.mergeExtParameterInfo(ToFPT, FromFPT, CanUseToFPT, |
1595 | 9.71k | CanUseFromFPT, NewParamInfos) && |
1596 | 9.67k | CanUseToFPT && !CanUseFromFPT9.67k ) { |
1597 | 6 | FunctionProtoType::ExtProtoInfo ExtInfo = FromFPT->getExtProtoInfo(); |
1598 | 6 | ExtInfo.ExtParameterInfos = |
1599 | 6 | NewParamInfos.empty() ? nullptr : NewParamInfos.data()0 ; |
1600 | 6 | QualType QT = Context.getFunctionType(FromFPT->getReturnType(), |
1601 | 6 | FromFPT->getParamTypes(), ExtInfo); |
1602 | 6 | FromFn = QT->getAs<FunctionType>(); |
1603 | 6 | Changed = true; |
1604 | 6 | } |
1605 | 9.71k | } |
1606 | | |
1607 | 9.71k | if (!Changed) |
1608 | 8.75k | return false; |
1609 | | |
1610 | 962 | assert(QualType(FromFn, 0).isCanonical()); |
1611 | 962 | if (QualType(FromFn, 0) != CanTo) return false100 ; |
1612 | | |
1613 | 862 | ResultTy = ToType; |
1614 | 862 | return true; |
1615 | 862 | } |
1616 | | |
1617 | | /// Determine whether the conversion from FromType to ToType is a valid |
1618 | | /// vector conversion. |
1619 | | /// |
1620 | | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
1621 | | /// conversion. |
1622 | | static bool IsVectorConversion(Sema &S, QualType FromType, |
1623 | 1.97M | QualType ToType, ImplicitConversionKind &ICK) { |
1624 | | // We need at least one of these types to be a vector type to have a vector |
1625 | | // conversion. |
1626 | 1.97M | if (!ToType->isVectorType() && !FromType->isVectorType()661k ) |
1627 | 636k | return false; |
1628 | | |
1629 | | // Identical types require no conversions. |
1630 | 1.33M | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) |
1631 | 0 | return false; |
1632 | | |
1633 | | // There are no conversions between extended vector types, only identity. |
1634 | 1.33M | if (ToType->isExtVectorType()) { |
1635 | | // There are no conversions between extended vector types other than the |
1636 | | // identity conversion. |
1637 | 5.86k | if (FromType->isExtVectorType()) |
1638 | 4.00k | return false; |
1639 | | |
1640 | | // Vector splat from any arithmetic type to a vector. |
1641 | 1.85k | if (FromType->isArithmeticType()) { |
1642 | 1.76k | ICK = ICK_Vector_Splat; |
1643 | 1.76k | return true; |
1644 | 1.76k | } |
1645 | 1.32M | } |
1646 | | |
1647 | 1.32M | if (ToType->isSizelessBuiltinType() || FromType->isSizelessBuiltinType()1.32M ) |
1648 | 485 | if (S.Context.areCompatibleSveTypes(FromType, ToType) || |
1649 | 363 | S.Context.areLaxCompatibleSveTypes(FromType, ToType)) { |
1650 | 295 | ICK = ICK_SVE_Vector_Conversion; |
1651 | 295 | return true; |
1652 | 295 | } |
1653 | | |
1654 | | // We can perform the conversion between vector types in the following cases: |
1655 | | // 1)vector types are equivalent AltiVec and GCC vector types |
1656 | | // 2)lax vector conversions are permitted and the vector types are of the |
1657 | | // same size |
1658 | | // 3)the destination type does not have the ARM MVE strict-polymorphism |
1659 | | // attribute, which inhibits lax vector conversion for overload resolution |
1660 | | // only |
1661 | 1.32M | if (ToType->isVectorType() && FromType->isVectorType()1.30M ) { |
1662 | 1.30M | if (S.Context.areCompatibleVectorTypes(FromType, ToType) || |
1663 | 1.24M | (S.isLaxVectorConversion(FromType, ToType) && |
1664 | 1.05M | !ToType->hasAttr(attr::ArmMveStrictPolymorphism)1.00M )) { |
1665 | 1.05M | ICK = ICK_Vector_Conversion; |
1666 | 1.05M | return true; |
1667 | 1.05M | } |
1668 | 276k | } |
1669 | | |
1670 | 276k | return false; |
1671 | 276k | } |
1672 | | |
1673 | | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
1674 | | bool InOverloadResolution, |
1675 | | StandardConversionSequence &SCS, |
1676 | | bool CStyle); |
1677 | | |
1678 | | /// IsStandardConversion - Determines whether there is a standard |
1679 | | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
1680 | | /// expression From to the type ToType. Standard conversion sequences |
1681 | | /// only consider non-class types; for conversions that involve class |
1682 | | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
1683 | | /// contain the standard conversion sequence required to perform this |
1684 | | /// conversion and this routine will return true. Otherwise, this |
1685 | | /// routine will return false and the value of SCS is unspecified. |
1686 | | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
1687 | | bool InOverloadResolution, |
1688 | | StandardConversionSequence &SCS, |
1689 | | bool CStyle, |
1690 | 23.2M | bool AllowObjCWritebackConversion) { |
1691 | 23.2M | QualType FromType = From->getType(); |
1692 | | |
1693 | | // Standard conversions (C++ [conv]) |
1694 | 23.2M | SCS.setAsIdentityConversion(); |
1695 | 23.2M | SCS.IncompatibleObjC = false; |
1696 | 23.2M | SCS.setFromType(FromType); |
1697 | 23.2M | SCS.CopyConstructor = nullptr; |
1698 | | |
1699 | | // There are no standard conversions for class types in C++, so |
1700 | | // abort early. When overloading in C, however, we do permit them. |
1701 | 23.2M | if (S.getLangOpts().CPlusPlus && |
1702 | 20.9M | (FromType->isRecordType() || ToType->isRecordType()20.0M )) |
1703 | 1.02M | return false; |
1704 | | |
1705 | | // The first conversion can be an lvalue-to-rvalue conversion, |
1706 | | // array-to-pointer conversion, or function-to-pointer conversion |
1707 | | // (C++ 4p1). |
1708 | | |
1709 | 22.1M | if (FromType == S.Context.OverloadTy) { |
1710 | 3.36k | DeclAccessPair AccessPair; |
1711 | 3.36k | if (FunctionDecl *Fn |
1712 | 1.62k | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
1713 | 1.62k | AccessPair)) { |
1714 | | // We were able to resolve the address of the overloaded function, |
1715 | | // so we can convert to the type of that function. |
1716 | 1.62k | FromType = Fn->getType(); |
1717 | 1.62k | SCS.setFromType(FromType); |
1718 | | |
1719 | | // we can sometimes resolve &foo<int> regardless of ToType, so check |
1720 | | // if the type matches (identity) or we are converting to bool |
1721 | 1.62k | if (!S.Context.hasSameUnqualifiedType( |
1722 | 115 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
1723 | 115 | QualType resultTy; |
1724 | | // if the function type matches except for [[noreturn]], it's ok |
1725 | 115 | if (!S.IsFunctionConversion(FromType, |
1726 | 115 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
1727 | | // otherwise, only a boolean conversion is standard |
1728 | 42 | if (!ToType->isBooleanType()) |
1729 | 18 | return false; |
1730 | 1.60k | } |
1731 | | |
1732 | | // Check if the "from" expression is taking the address of an overloaded |
1733 | | // function and recompute the FromType accordingly. Take advantage of the |
1734 | | // fact that non-static member functions *must* have such an address-of |
1735 | | // expression. |
1736 | 1.60k | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
1737 | 1.60k | if (Method && !Method->isStatic()304 ) { |
1738 | 176 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
1739 | 176 | "Non-unary operator on non-static member address"); |
1740 | 176 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
1741 | 176 | == UO_AddrOf && |
1742 | 176 | "Non-address-of operator on non-static member address"); |
1743 | 176 | const Type *ClassType |
1744 | 176 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
1745 | 176 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
1746 | 1.43k | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
1747 | 502 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
1748 | 502 | UO_AddrOf && |
1749 | 502 | "Non-address-of operator for overloaded function expression"); |
1750 | 502 | FromType = S.Context.getPointerType(FromType); |
1751 | 502 | } |
1752 | | |
1753 | | // Check that we've computed the proper type after overload resolution. |
1754 | | // FIXME: FixOverloadedFunctionReference has side-effects; we shouldn't |
1755 | | // be calling it from within an NDEBUG block. |
1756 | 1.60k | assert(S.Context.hasSameType( |
1757 | 1.60k | FromType, |
1758 | 1.60k | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
1759 | 1.74k | } else { |
1760 | 1.74k | return false; |
1761 | 1.74k | } |
1762 | 22.1M | } |
1763 | | // Lvalue-to-rvalue conversion (C++11 4.1): |
1764 | | // A glvalue (3.10) of a non-function, non-array type T can |
1765 | | // be converted to a prvalue. |
1766 | 22.1M | bool argIsLValue = From->isGLValue(); |
1767 | 22.1M | if (argIsLValue && |
1768 | 7.00M | !FromType->isFunctionType() && !FromType->isArrayType()6.99M && |
1769 | 6.84M | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
1770 | 6.84M | SCS.First = ICK_Lvalue_To_Rvalue; |
1771 | | |
1772 | | // C11 6.3.2.1p2: |
1773 | | // ... if the lvalue has atomic type, the value has the non-atomic version |
1774 | | // of the type of the lvalue ... |
1775 | 6.84M | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
1776 | 80 | FromType = Atomic->getValueType(); |
1777 | | |
1778 | | // If T is a non-class type, the type of the rvalue is the |
1779 | | // cv-unqualified version of T. Otherwise, the type of the rvalue |
1780 | | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
1781 | | // just strip the qualifiers because they don't matter. |
1782 | 6.84M | FromType = FromType.getUnqualifiedType(); |
1783 | 15.3M | } else if (FromType->isArrayType()) { |
1784 | | // Array-to-pointer conversion (C++ 4.2) |
1785 | 151k | SCS.First = ICK_Array_To_Pointer; |
1786 | | |
1787 | | // An lvalue or rvalue of type "array of N T" or "array of unknown |
1788 | | // bound of T" can be converted to an rvalue of type "pointer to |
1789 | | // T" (C++ 4.2p1). |
1790 | 151k | FromType = S.Context.getArrayDecayedType(FromType); |
1791 | | |
1792 | 151k | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
1793 | | // This conversion is deprecated in C++03 (D.4) |
1794 | 842 | SCS.DeprecatedStringLiteralToCharPtr = true; |
1795 | | |
1796 | | // For the purpose of ranking in overload resolution |
1797 | | // (13.3.3.1.1), this conversion is considered an |
1798 | | // array-to-pointer conversion followed by a qualification |
1799 | | // conversion (4.4). (C++ 4.2p2) |
1800 | 842 | SCS.Second = ICK_Identity; |
1801 | 842 | SCS.Third = ICK_Qualification; |
1802 | 842 | SCS.QualificationIncludesObjCLifetime = false; |
1803 | 842 | SCS.setAllToTypes(FromType); |
1804 | 842 | return true; |
1805 | 842 | } |
1806 | 15.1M | } else if (FromType->isFunctionType() && argIsLValue7.38k ) { |
1807 | | // Function-to-pointer conversion (C++ 4.3). |
1808 | 7.38k | SCS.First = ICK_Function_To_Pointer; |
1809 | | |
1810 | 7.38k | if (auto *DRE = dyn_cast<DeclRefExpr>(From->IgnoreParenCasts())) |
1811 | 6.09k | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) |
1812 | 6.08k | if (!S.checkAddressOfFunctionIsAvailable(FD)) |
1813 | 10 | return false; |
1814 | | |
1815 | | // An lvalue of function type T can be converted to an rvalue of |
1816 | | // type "pointer to T." The result is a pointer to the |
1817 | | // function. (C++ 4.3p1). |
1818 | 7.37k | FromType = S.Context.getPointerType(FromType); |
1819 | 15.1M | } else { |
1820 | | // We don't require any conversions for the first step. |
1821 | 15.1M | SCS.First = ICK_Identity; |
1822 | 15.1M | } |
1823 | 22.1M | SCS.setToType(0, FromType); |
1824 | | |
1825 | | // The second conversion can be an integral promotion, floating |
1826 | | // point promotion, integral conversion, floating point conversion, |
1827 | | // floating-integral conversion, pointer conversion, |
1828 | | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
1829 | | // For overloading in C, this can also be a "compatible-type" |
1830 | | // conversion. |
1831 | 22.1M | bool IncompatibleObjC = false; |
1832 | 22.1M | ImplicitConversionKind SecondICK = ICK_Identity; |
1833 | 22.1M | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
1834 | | // The unqualified versions of the types are the same: there's no |
1835 | | // conversion to do. |
1836 | 10.3M | SCS.Second = ICK_Identity; |
1837 | 11.8M | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
1838 | | // Integral promotion (C++ 4.5). |
1839 | 707k | SCS.Second = ICK_Integral_Promotion; |
1840 | 707k | FromType = ToType.getUnqualifiedType(); |
1841 | 11.0M | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
1842 | | // Floating point promotion (C++ 4.6). |
1843 | 2.26k | SCS.Second = ICK_Floating_Promotion; |
1844 | 2.26k | FromType = ToType.getUnqualifiedType(); |
1845 | 11.0M | } else if (S.IsComplexPromotion(FromType, ToType)) { |
1846 | | // Complex promotion (Clang extension) |
1847 | 30 | SCS.Second = ICK_Complex_Promotion; |
1848 | 30 | FromType = ToType.getUnqualifiedType(); |
1849 | 11.0M | } else if (ToType->isBooleanType() && |
1850 | 102k | (FromType->isArithmeticType() || |
1851 | 18.6k | FromType->isAnyPointerType() || |
1852 | 227 | FromType->isBlockPointerType() || |
1853 | 101k | FromType->isMemberPointerType()215 )) { |
1854 | | // Boolean conversions (C++ 4.12). |
1855 | 101k | SCS.Second = ICK_Boolean_Conversion; |
1856 | 101k | FromType = S.Context.BoolTy; |
1857 | 10.9M | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
1858 | 8.86M | ToType->isIntegralType(S.Context)) { |
1859 | | // Integral conversions (C++ 4.7). |
1860 | 6.64M | SCS.Second = ICK_Integral_Conversion; |
1861 | 6.64M | FromType = ToType.getUnqualifiedType(); |
1862 | 4.35M | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()694 ) { |
1863 | | // Complex conversions (C99 6.3.1.6) |
1864 | 87 | SCS.Second = ICK_Complex_Conversion; |
1865 | 87 | FromType = ToType.getUnqualifiedType(); |
1866 | 4.35M | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()607 ) || |
1867 | 4.35M | (ToType->isAnyComplexType() && FromType->isArithmeticType()445 )) { |
1868 | | // Complex-real conversions (C99 6.3.1.7) |
1869 | 1.03k | SCS.Second = ICK_Complex_Real; |
1870 | 1.03k | FromType = ToType.getUnqualifiedType(); |
1871 | 4.34M | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()50.6k ) { |
1872 | | // FIXME: disable conversions between long double and __float128 if |
1873 | | // their representation is different until there is back end support |
1874 | | // We of course allow this conversion if long double is really double. |
1875 | | |
1876 | | // Conversions between bfloat and other floats are not permitted. |
1877 | 15.8k | if (FromType == S.Context.BFloat16Ty || ToType == S.Context.BFloat16Ty15.8k ) |
1878 | 10 | return false; |
1879 | 15.8k | if (&S.Context.getFloatTypeSemantics(FromType) != |
1880 | 15.8k | &S.Context.getFloatTypeSemantics(ToType)) { |
1881 | 15.8k | bool Float128AndLongDouble = ((FromType == S.Context.Float128Ty && |
1882 | 4 | ToType == S.Context.LongDoubleTy) || |
1883 | 15.8k | (FromType == S.Context.LongDoubleTy && |
1884 | 224 | ToType == S.Context.Float128Ty)); |
1885 | 15.8k | if (Float128AndLongDouble && |
1886 | 8 | (&S.Context.getFloatTypeSemantics(S.Context.LongDoubleTy) == |
1887 | 8 | &llvm::APFloat::PPCDoubleDouble())) |
1888 | 6 | return false; |
1889 | 15.8k | } |
1890 | | // Floating point conversions (C++ 4.8). |
1891 | 15.8k | SCS.Second = ICK_Floating_Conversion; |
1892 | 15.8k | FromType = ToType.getUnqualifiedType(); |
1893 | 4.33M | } else if ((FromType->isRealFloatingType() && |
1894 | 34.7k | ToType->isIntegralType(S.Context)) || |
1895 | 4.29M | (FromType->isIntegralOrUnscopedEnumerationType() && |
1896 | 2.22M | ToType->isRealFloatingType())) { |
1897 | | // Conversions between bfloat and int are not permitted. |
1898 | 2.21M | if (FromType->isBFloat16Type() || ToType->isBFloat16Type()2.21M ) |
1899 | 22 | return false; |
1900 | | |
1901 | | // Floating-integral conversions (C++ 4.9). |
1902 | 2.21M | SCS.Second = ICK_Floating_Integral; |
1903 | 2.21M | FromType = ToType.getUnqualifiedType(); |
1904 | 2.11M | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
1905 | 8 | SCS.Second = ICK_Block_Pointer_Conversion; |
1906 | 2.11M | } else if (AllowObjCWritebackConversion && |
1907 | 410 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
1908 | 19 | SCS.Second = ICK_Writeback_Conversion; |
1909 | 2.11M | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
1910 | 143k | FromType, IncompatibleObjC)) { |
1911 | | // Pointer conversions (C++ 4.10). |
1912 | 143k | SCS.Second = ICK_Pointer_Conversion; |
1913 | 143k | SCS.IncompatibleObjC = IncompatibleObjC; |
1914 | 143k | FromType = FromType.getUnqualifiedType(); |
1915 | 1.97M | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
1916 | 1.22k | InOverloadResolution, FromType)) { |
1917 | | // Pointer to member conversions (4.11). |
1918 | 1.22k | SCS.Second = ICK_Pointer_Member; |
1919 | 1.97M | } else if (IsVectorConversion(S, FromType, ToType, SecondICK)) { |
1920 | 1.05M | SCS.Second = SecondICK; |
1921 | 1.05M | FromType = ToType.getUnqualifiedType(); |
1922 | 916k | } else if (!S.getLangOpts().CPlusPlus && |
1923 | 594k | S.Context.typesAreCompatible(ToType, FromType)) { |
1924 | | // Compatible conversions (Clang extension for C function overloading) |
1925 | 22.8k | SCS.Second = ICK_Compatible_Conversion; |
1926 | 22.8k | FromType = ToType.getUnqualifiedType(); |
1927 | 894k | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
1928 | 894k | InOverloadResolution, |
1929 | 1 | SCS, CStyle)) { |
1930 | 1 | SCS.Second = ICK_TransparentUnionConversion; |
1931 | 1 | FromType = ToType; |
1932 | 894k | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
1933 | 562 | CStyle)) { |
1934 | | // tryAtomicConversion has updated the standard conversion sequence |
1935 | | // appropriately. |
1936 | 562 | return true; |
1937 | 893k | } else if (ToType->isEventT() && |
1938 | 2 | From->isIntegerConstantExpr(S.getASTContext()) && |
1939 | 2 | From->EvaluateKnownConstInt(S.getASTContext()) == 0) { |
1940 | 2 | SCS.Second = ICK_Zero_Event_Conversion; |
1941 | 2 | FromType = ToType; |
1942 | 893k | } else if (ToType->isQueueT() && |
1943 | 4 | From->isIntegerConstantExpr(S.getASTContext()) && |
1944 | 4 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
1945 | 2 | SCS.Second = ICK_Zero_Queue_Conversion; |
1946 | 2 | FromType = ToType; |
1947 | 893k | } else if (ToType->isSamplerT() && |
1948 | 1 | From->isIntegerConstantExpr(S.getASTContext())) { |
1949 | 1 | SCS.Second = ICK_Compatible_Conversion; |
1950 | 1 | FromType = ToType; |
1951 | 893k | } else { |
1952 | | // No second conversion required. |
1953 | 893k | SCS.Second = ICK_Identity; |
1954 | 893k | } |
1955 | 22.1M | SCS.setToType(1, FromType); |
1956 | | |
1957 | | // The third conversion can be a function pointer conversion or a |
1958 | | // qualification conversion (C++ [conv.fctptr], [conv.qual]). |
1959 | 22.1M | bool ObjCLifetimeConversion; |
1960 | 22.1M | if (S.IsFunctionConversion(FromType, ToType, FromType)) { |
1961 | | // Function pointer conversions (removing 'noexcept') including removal of |
1962 | | // 'noreturn' (Clang extension). |
1963 | 150 | SCS.Third = ICK_Function_Conversion; |
1964 | 22.1M | } else if (S.IsQualificationConversion(FromType, ToType, CStyle, |
1965 | 69.2k | ObjCLifetimeConversion)) { |
1966 | 69.2k | SCS.Third = ICK_Qualification; |
1967 | 69.2k | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
1968 | 69.2k | FromType = ToType; |
1969 | 22.1M | } else { |
1970 | | // No conversion required |
1971 | 22.1M | SCS.Third = ICK_Identity; |
1972 | 22.1M | } |
1973 | | |
1974 | | // C++ [over.best.ics]p6: |
1975 | | // [...] Any difference in top-level cv-qualification is |
1976 | | // subsumed by the initialization itself and does not constitute |
1977 | | // a conversion. [...] |
1978 | 22.1M | QualType CanonFrom = S.Context.getCanonicalType(FromType); |
1979 | 22.1M | QualType CanonTo = S.Context.getCanonicalType(ToType); |
1980 | 22.1M | if (CanonFrom.getLocalUnqualifiedType() |
1981 | 22.1M | == CanonTo.getLocalUnqualifiedType() && |
1982 | 21.3M | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
1983 | 413k | FromType = ToType; |
1984 | 413k | CanonFrom = CanonTo; |
1985 | 413k | } |
1986 | | |
1987 | 22.1M | SCS.setToType(2, FromType); |
1988 | | |
1989 | 22.1M | if (CanonFrom == CanonTo) |
1990 | 21.3M | return true; |
1991 | | |
1992 | | // If we have not converted the argument type to the parameter type, |
1993 | | // this is a bad conversion sequence, unless we're resolving an overload in C. |
1994 | 836k | if (S.getLangOpts().CPlusPlus || !InOverloadResolution561k ) |
1995 | 274k | return false; |
1996 | | |
1997 | 561k | ExprResult ER = ExprResult{From}; |
1998 | 561k | Sema::AssignConvertType Conv = |
1999 | 561k | S.CheckSingleAssignmentConstraints(ToType, ER, |
2000 | 561k | /*Diagnose=*/false, |
2001 | 561k | /*DiagnoseCFAudited=*/false, |
2002 | 561k | /*ConvertRHS=*/false); |
2003 | 561k | ImplicitConversionKind SecondConv; |
2004 | 561k | switch (Conv) { |
2005 | 2.46k | case Sema::Compatible: |
2006 | 2.46k | SecondConv = ICK_C_Only_Conversion; |
2007 | 2.46k | break; |
2008 | | // For our purposes, discarding qualifiers is just as bad as using an |
2009 | | // incompatible pointer. Note that an IncompatiblePointer conversion can drop |
2010 | | // qualifiers, as well. |
2011 | 2 | case Sema::CompatiblePointerDiscardsQualifiers: |
2012 | 250k | case Sema::IncompatiblePointer: |
2013 | 268k | case Sema::IncompatiblePointerSign: |
2014 | 268k | SecondConv = ICK_Incompatible_Pointer_Conversion; |
2015 | 268k | break; |
2016 | 290k | default: |
2017 | 290k | return false; |
2018 | 271k | } |
2019 | | |
2020 | | // First can only be an lvalue conversion, so we pretend that this was the |
2021 | | // second conversion. First should already be valid from earlier in the |
2022 | | // function. |
2023 | 271k | SCS.Second = SecondConv; |
2024 | 271k | SCS.setToType(1, ToType); |
2025 | | |
2026 | | // Third is Identity, because Second should rank us worse than any other |
2027 | | // conversion. This could also be ICK_Qualification, but it's simpler to just |
2028 | | // lump everything in with the second conversion, and we don't gain anything |
2029 | | // from making this ICK_Qualification. |
2030 | 271k | SCS.Third = ICK_Identity; |
2031 | 271k | SCS.setToType(2, ToType); |
2032 | 271k | return true; |
2033 | 271k | } |
2034 | | |
2035 | | static bool |
2036 | | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
2037 | | QualType &ToType, |
2038 | | bool InOverloadResolution, |
2039 | | StandardConversionSequence &SCS, |
2040 | 894k | bool CStyle) { |
2041 | | |
2042 | 894k | const RecordType *UT = ToType->getAsUnionType(); |
2043 | 894k | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()1 ) |
2044 | 894k | return false; |
2045 | | // The field to initialize within the transparent union. |
2046 | 1 | RecordDecl *UD = UT->getDecl(); |
2047 | | // It's compatible if the expression matches any of the fields. |
2048 | 1 | for (const auto *it : UD->fields()) { |
2049 | 1 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
2050 | 1 | CStyle, /*AllowObjCWritebackConversion=*/false)) { |
2051 | 1 | ToType = it->getType(); |
2052 | 1 | return true; |
2053 | 1 | } |
2054 | 1 | } |
2055 | 0 | return false; |
2056 | 1 | } |
2057 | | |
2058 | | /// IsIntegralPromotion - Determines whether the conversion from the |
2059 | | /// expression From (whose potentially-adjusted type is FromType) to |
2060 | | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
2061 | | /// sets PromotedType to the promoted type. |
2062 | 11.9M | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
2063 | 11.9M | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
2064 | | // All integers are built-in. |
2065 | 11.9M | if (!To) { |
2066 | 1.90M | return false; |
2067 | 1.90M | } |
2068 | | |
2069 | | // An rvalue of type char, signed char, unsigned char, short int, or |
2070 | | // unsigned short int can be converted to an rvalue of type int if |
2071 | | // int can represent all the values of the source type; otherwise, |
2072 | | // the source rvalue can be converted to an rvalue of type unsigned |
2073 | | // int (C++ 4.5p1). |
2074 | 10.0M | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()7.45M && |
2075 | 7.42M | !FromType->isEnumeralType()) { |
2076 | 117k | if (// We can promote any signed, promotable integer type to an int |
2077 | 117k | (FromType->isSignedIntegerType() || |
2078 | | // We can promote any unsigned integer type whose size is |
2079 | | // less than int to an int. |
2080 | 112k | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)67.8k )) { |
2081 | 112k | return To->getKind() == BuiltinType::Int; |
2082 | 112k | } |
2083 | | |
2084 | 4.80k | return To->getKind() == BuiltinType::UInt; |
2085 | 4.80k | } |
2086 | | |
2087 | | // C++11 [conv.prom]p3: |
2088 | | // A prvalue of an unscoped enumeration type whose underlying type is not |
2089 | | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
2090 | | // following types that can represent all the values of the enumeration |
2091 | | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
2092 | | // unsigned int, long int, unsigned long int, long long int, or unsigned |
2093 | | // long long int. If none of the types in that list can represent all the |
2094 | | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
2095 | | // type can be converted to an rvalue a prvalue of the extended integer type |
2096 | | // with lowest integer conversion rank (4.13) greater than the rank of long |
2097 | | // long in which all the values of the enumeration can be represented. If |
2098 | | // there are two such extended types, the signed one is chosen. |
2099 | | // C++11 [conv.prom]p4: |
2100 | | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
2101 | | // can be converted to a prvalue of its underlying type. Moreover, if |
2102 | | // integral promotion can be applied to its underlying type, a prvalue of an |
2103 | | // unscoped enumeration type whose underlying type is fixed can also be |
2104 | | // converted to a prvalue of the promoted underlying type. |
2105 | 9.93M | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
2106 | | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
2107 | | // provided for a scoped enumeration. |
2108 | 7.41M | if (FromEnumType->getDecl()->isScoped()) |
2109 | 103k | return false; |
2110 | | |
2111 | | // We can perform an integral promotion to the underlying type of the enum, |
2112 | | // even if that's not the promoted type. Note that the check for promoting |
2113 | | // the underlying type is based on the type alone, and does not consider |
2114 | | // the bitfield-ness of the actual source expression. |
2115 | 7.31M | if (FromEnumType->getDecl()->isFixed()) { |
2116 | 167k | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
2117 | 167k | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
2118 | 152k | IsIntegralPromotion(nullptr, Underlying, ToType); |
2119 | 167k | } |
2120 | | |
2121 | | // We have already pre-calculated the promotion type, so this is trivial. |
2122 | 7.14M | if (ToType->isIntegerType() && |
2123 | 5.32M | isCompleteType(From->getBeginLoc(), FromType)) |
2124 | 5.32M | return Context.hasSameUnqualifiedType( |
2125 | 5.32M | ToType, FromEnumType->getDecl()->getPromotionType()); |
2126 | | |
2127 | | // C++ [conv.prom]p5: |
2128 | | // If the bit-field has an enumerated type, it is treated as any other |
2129 | | // value of that type for promotion purposes. |
2130 | | // |
2131 | | // ... so do not fall through into the bit-field checks below in C++. |
2132 | 1.82M | if (getLangOpts().CPlusPlus) |
2133 | 1.82M | return false; |
2134 | 2.52M | } |
2135 | | |
2136 | | // C++0x [conv.prom]p2: |
2137 | | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
2138 | | // to an rvalue a prvalue of the first of the following types that can |
2139 | | // represent all the values of its underlying type: int, unsigned int, |
2140 | | // long int, unsigned long int, long long int, or unsigned long long int. |
2141 | | // If none of the types in that list can represent all the values of its |
2142 | | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
2143 | | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
2144 | | // type. |
2145 | 2.52M | if (FromType->isAnyCharacterType() && !FromType->isCharType()0 && |
2146 | 0 | ToType->isIntegerType()) { |
2147 | | // Determine whether the type we're converting from is signed or |
2148 | | // unsigned. |
2149 | 0 | bool FromIsSigned = FromType->isSignedIntegerType(); |
2150 | 0 | uint64_t FromSize = Context.getTypeSize(FromType); |
2151 | | |
2152 | | // The types we'll try to promote to, in the appropriate |
2153 | | // order. Try each of these types. |
2154 | 0 | QualType PromoteTypes[6] = { |
2155 | 0 | Context.IntTy, Context.UnsignedIntTy, |
2156 | 0 | Context.LongTy, Context.UnsignedLongTy , |
2157 | 0 | Context.LongLongTy, Context.UnsignedLongLongTy |
2158 | 0 | }; |
2159 | 0 | for (int Idx = 0; Idx < 6; ++Idx) { |
2160 | 0 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
2161 | 0 | if (FromSize < ToSize || |
2162 | 0 | (FromSize == ToSize && |
2163 | 0 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
2164 | | // We found the type that we can promote to. If this is the |
2165 | | // type we wanted, we have a promotion. Otherwise, no |
2166 | | // promotion. |
2167 | 0 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
2168 | 0 | } |
2169 | 0 | } |
2170 | 0 | } |
2171 | | |
2172 | | // An rvalue for an integral bit-field (9.6) can be converted to an |
2173 | | // rvalue of type int if int can represent all the values of the |
2174 | | // bit-field; otherwise, it can be converted to unsigned int if |
2175 | | // unsigned int can represent all the values of the bit-field. If |
2176 | | // the bit-field is larger yet, no integral promotion applies to |
2177 | | // it. If the bit-field has an enumerated type, it is treated as any |
2178 | | // other value of that type for promotion purposes (C++ 4.5p3). |
2179 | | // FIXME: We should delay checking of bit-fields until we actually perform the |
2180 | | // conversion. |
2181 | | // |
2182 | | // FIXME: In C, only bit-fields of types _Bool, int, or unsigned int may be |
2183 | | // promoted, per C11 6.3.1.1/2. We promote all bit-fields (including enum |
2184 | | // bit-fields and those whose underlying type is larger than int) for GCC |
2185 | | // compatibility. |
2186 | 2.52M | if (From) { |
2187 | 2.40M | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
2188 | 66 | Optional<llvm::APSInt> BitWidth; |
2189 | 66 | if (FromType->isIntegralType(Context) && |
2190 | 66 | (BitWidth = |
2191 | 66 | MemberDecl->getBitWidth()->getIntegerConstantExpr(Context))) { |
2192 | 66 | llvm::APSInt ToSize(BitWidth->getBitWidth(), BitWidth->isUnsigned()); |
2193 | 66 | ToSize = Context.getTypeSize(ToType); |
2194 | | |
2195 | | // Are we promoting to an int from a bitfield that fits in an int? |
2196 | 66 | if (*BitWidth < ToSize || |
2197 | 64 | (2 FromType->isSignedIntegerType()2 && *BitWidth <= ToSize0 )) { |
2198 | 64 | return To->getKind() == BuiltinType::Int; |
2199 | 64 | } |
2200 | | |
2201 | | // Are we promoting to an unsigned int from an unsigned bitfield |
2202 | | // that fits into an unsigned int? |
2203 | 2 | if (FromType->isUnsignedIntegerType() && *BitWidth <= ToSize) { |
2204 | 2 | return To->getKind() == BuiltinType::UInt; |
2205 | 2 | } |
2206 | | |
2207 | 0 | return false; |
2208 | 0 | } |
2209 | 66 | } |
2210 | 2.40M | } |
2211 | | |
2212 | | // An rvalue of type bool can be converted to an rvalue of type int, |
2213 | | // with false becoming zero and true becoming one (C++ 4.5p4). |
2214 | 2.52M | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int22.1k ) { |
2215 | 7.51k | return true; |
2216 | 7.51k | } |
2217 | | |
2218 | 2.51M | return false; |
2219 | 2.51M | } |
2220 | | |
2221 | | /// IsFloatingPointPromotion - Determines whether the conversion from |
2222 | | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
2223 | | /// returns true and sets PromotedType to the promoted type. |
2224 | 11.0M | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
2225 | 11.0M | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
2226 | 2.46M | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
2227 | | /// An rvalue of type float can be converted to an rvalue of type |
2228 | | /// double. (C++ 4.6p1). |
2229 | 2.39M | if (FromBuiltin->getKind() == BuiltinType::Float && |
2230 | 28.0k | ToBuiltin->getKind() == BuiltinType::Double) |
2231 | 2.17k | return true; |
2232 | | |
2233 | | // C99 6.3.1.5p1: |
2234 | | // When a float is promoted to double or long double, or a |
2235 | | // double is promoted to long double [...]. |
2236 | 2.39M | if (!getLangOpts().CPlusPlus && |
2237 | 117k | (FromBuiltin->getKind() == BuiltinType::Float || |
2238 | 116k | FromBuiltin->getKind() == BuiltinType::Double) && |
2239 | 5.37k | (ToBuiltin->getKind() == BuiltinType::LongDouble || |
2240 | 5.28k | ToBuiltin->getKind() == BuiltinType::Float128)) |
2241 | 94 | return true; |
2242 | | |
2243 | | // Half can be promoted to float. |
2244 | 2.39M | if (!getLangOpts().NativeHalfType && |
2245 | 2.39M | FromBuiltin->getKind() == BuiltinType::Half && |
2246 | 139 | ToBuiltin->getKind() == BuiltinType::Float) |
2247 | 14 | return true; |
2248 | 11.0M | } |
2249 | | |
2250 | 11.0M | return false; |
2251 | 11.0M | } |
2252 | | |
2253 | | /// Determine if a conversion is a complex promotion. |
2254 | | /// |
2255 | | /// A complex promotion is defined as a complex -> complex conversion |
2256 | | /// where the conversion between the underlying real types is a |
2257 | | /// floating-point or integral promotion. |
2258 | 11.0M | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
2259 | 11.0M | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
2260 | 11.0M | if (!FromComplex) |
2261 | 11.0M | return false; |
2262 | | |
2263 | 739 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
2264 | 739 | if (!ToComplex) |
2265 | 622 | return false; |
2266 | | |
2267 | 117 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
2268 | 117 | ToComplex->getElementType()) || |
2269 | 90 | IsIntegralPromotion(nullptr, FromComplex->getElementType(), |
2270 | 90 | ToComplex->getElementType()); |
2271 | 117 | } |
2272 | | |
2273 | | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
2274 | | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
2275 | | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
2276 | | /// if non-empty, will be a pointer to ToType that may or may not have |
2277 | | /// the right set of qualifiers on its pointee. |
2278 | | /// |
2279 | | static QualType |
2280 | | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
2281 | | QualType ToPointee, QualType ToType, |
2282 | | ASTContext &Context, |
2283 | 88.6k | bool StripObjCLifetime = false) { |
2284 | 88.6k | assert((FromPtr->getTypeClass() == Type::Pointer || |
2285 | 88.6k | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
2286 | 88.6k | "Invalid similarly-qualified pointer type"); |
2287 | | |
2288 | | /// Conversions to 'id' subsume cv-qualifier conversions. |
2289 | 88.6k | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()85.3k ) |
2290 | 4.07k | return ToType.getUnqualifiedType(); |
2291 | | |
2292 | 84.5k | QualType CanonFromPointee |
2293 | 84.5k | = Context.getCanonicalType(FromPtr->getPointeeType()); |
2294 | 84.5k | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
2295 | 84.5k | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
2296 | | |
2297 | 84.5k | if (StripObjCLifetime) |
2298 | 64.3k | Quals.removeObjCLifetime(); |
2299 | | |
2300 | | // Exact qualifier match -> return the pointer type we're converting to. |
2301 | 84.5k | if (CanonToPointee.getLocalQualifiers() == Quals) { |
2302 | | // ToType is exactly what we need. Return it. |
2303 | 72.4k | if (!ToType.isNull()) |
2304 | 72.4k | return ToType.getUnqualifiedType(); |
2305 | | |
2306 | | // Build a pointer to ToPointee. It has the right qualifiers |
2307 | | // already. |
2308 | 0 | if (isa<ObjCObjectPointerType>(ToType)) |
2309 | 0 | return Context.getObjCObjectPointerType(ToPointee); |
2310 | 0 | return Context.getPointerType(ToPointee); |
2311 | 0 | } |
2312 | | |
2313 | | // Just build a canonical type that has the right qualifiers. |
2314 | 12.0k | QualType QualifiedCanonToPointee |
2315 | 12.0k | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
2316 | | |
2317 | 12.0k | if (isa<ObjCObjectPointerType>(ToType)) |
2318 | 19 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
2319 | 12.0k | return Context.getPointerType(QualifiedCanonToPointee); |
2320 | 12.0k | } |
2321 | | |
2322 | | static bool isNullPointerConstantForConversion(Expr *Expr, |
2323 | | bool InOverloadResolution, |
2324 | 577k | ASTContext &Context) { |
2325 | | // Handle value-dependent integral null pointer constants correctly. |
2326 | | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
2327 | 577k | if (Expr->isValueDependent() && !Expr->isTypeDependent()2.68k && |
2328 | 2.68k | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()23 ) |
2329 | 23 | return !InOverloadResolution; |
2330 | | |
2331 | 577k | return Expr->isNullPointerConstant(Context, |
2332 | 397k | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
2333 | 179k | : Expr::NPC_ValueDependentIsNull); |
2334 | 577k | } |
2335 | | |
2336 | | /// IsPointerConversion - Determines whether the conversion of the |
2337 | | /// expression From, which has the (possibly adjusted) type FromType, |
2338 | | /// can be converted to the type ToType via a pointer conversion (C++ |
2339 | | /// 4.10). If so, returns true and places the converted type (that |
2340 | | /// might differ from ToType in its cv-qualifiers at some level) into |
2341 | | /// ConvertedType. |
2342 | | /// |
2343 | | /// This routine also supports conversions to and from block pointers |
2344 | | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
2345 | | /// pointers to interfaces. FIXME: Once we've determined the |
2346 | | /// appropriate overloading rules for Objective-C, we may want to |
2347 | | /// split the Objective-C checks into a different routine; however, |
2348 | | /// GCC seems to consider all of these conversions to be pointer |
2349 | | /// conversions, so for now they live here. IncompatibleObjC will be |
2350 | | /// set if the conversion is an allowed Objective-C conversion that |
2351 | | /// should result in a warning. |
2352 | | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
2353 | | bool InOverloadResolution, |
2354 | | QualType& ConvertedType, |
2355 | 2.11M | bool &IncompatibleObjC) { |
2356 | 2.11M | IncompatibleObjC = false; |
2357 | 2.11M | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
2358 | 2.11M | IncompatibleObjC)) |
2359 | 4.44k | return true; |
2360 | | |
2361 | | // Conversion from a null pointer constant to any Objective-C pointer type. |
2362 | 2.11M | if (ToType->isObjCObjectPointerType() && |
2363 | 3.85k | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2364 | 629 | ConvertedType = ToType; |
2365 | 629 | return true; |
2366 | 629 | } |
2367 | | |
2368 | | // Blocks: Block pointers can be converted to void*. |
2369 | 2.11M | if (FromType->isBlockPointerType() && ToType->isPointerType()56 && |
2370 | 43 | ToType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
2371 | 12 | ConvertedType = ToType; |
2372 | 12 | return true; |
2373 | 12 | } |
2374 | | // Blocks: A null pointer constant can be converted to a block |
2375 | | // pointer type. |
2376 | 2.11M | if (ToType->isBlockPointerType() && |
2377 | 61 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2378 | 12 | ConvertedType = ToType; |
2379 | 12 | return true; |
2380 | 12 | } |
2381 | | |
2382 | | // If the left-hand-side is nullptr_t, the right side can be a null |
2383 | | // pointer constant. |
2384 | 2.11M | if (ToType->isNullPtrType() && |
2385 | 2.52k | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2386 | 20 | ConvertedType = ToType; |
2387 | 20 | return true; |
2388 | 20 | } |
2389 | | |
2390 | 2.11M | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
2391 | 2.11M | if (!ToTypePtr) |
2392 | 1.54M | return false; |
2393 | | |
2394 | | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
2395 | 571k | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2396 | 54.3k | ConvertedType = ToType; |
2397 | 54.3k | return true; |
2398 | 54.3k | } |
2399 | | |
2400 | | // Beyond this point, both types need to be pointers |
2401 | | // , including objective-c pointers. |
2402 | 517k | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
2403 | 517k | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()2.60k && |
2404 | 2.45k | !getLangOpts().ObjCAutoRefCount) { |
2405 | 2.44k | ConvertedType = BuildSimilarlyQualifiedPointerType( |
2406 | 2.44k | FromType->getAs<ObjCObjectPointerType>(), |
2407 | 2.44k | ToPointeeType, |
2408 | 2.44k | ToType, Context); |
2409 | 2.44k | return true; |
2410 | 2.44k | } |
2411 | 514k | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
2412 | 514k | if (!FromTypePtr) |
2413 | 7.75k | return false; |
2414 | | |
2415 | 506k | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
2416 | | |
2417 | | // If the unqualified pointee types are the same, this can't be a |
2418 | | // pointer conversion, so don't do all of the work below. |
2419 | 506k | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
2420 | 58.6k | return false; |
2421 | | |
2422 | | // An rvalue of type "pointer to cv T," where T is an object type, |
2423 | | // can be converted to an rvalue of type "pointer to cv void" (C++ |
2424 | | // 4.10p2). |
2425 | 448k | if (FromPointeeType->isIncompleteOrObjectType() && |
2426 | 446k | ToPointeeType->isVoidType()) { |
2427 | 64.3k | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2428 | 64.3k | ToPointeeType, |
2429 | 64.3k | ToType, Context, |
2430 | 64.3k | /*StripObjCLifetime=*/true); |
2431 | 64.3k | return true; |
2432 | 64.3k | } |
2433 | | |
2434 | | // MSVC allows implicit function to void* type conversion. |
2435 | 383k | if (getLangOpts().MSVCCompat && FromPointeeType->isFunctionType()968 && |
2436 | 67 | ToPointeeType->isVoidType()) { |
2437 | 6 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2438 | 6 | ToPointeeType, |
2439 | 6 | ToType, Context); |
2440 | 6 | return true; |
2441 | 6 | } |
2442 | | |
2443 | | // When we're overloading in C, we allow a special kind of pointer |
2444 | | // conversion for compatible-but-not-identical pointee types. |
2445 | 383k | if (!getLangOpts().CPlusPlus && |
2446 | 276k | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
2447 | 4.60k | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2448 | 4.60k | ToPointeeType, |
2449 | 4.60k | ToType, Context); |
2450 | 4.60k | return true; |
2451 | 4.60k | } |
2452 | | |
2453 | | // C++ [conv.ptr]p3: |
2454 | | // |
2455 | | // An rvalue of type "pointer to cv D," where D is a class type, |
2456 | | // can be converted to an rvalue of type "pointer to cv B," where |
2457 | | // B is a base class (clause 10) of D. If B is an inaccessible |
2458 | | // (clause 11) or ambiguous (10.2) base class of D, a program that |
2459 | | // necessitates this conversion is ill-formed. The result of the |
2460 | | // conversion is a pointer to the base class sub-object of the |
2461 | | // derived class object. The null pointer value is converted to |
2462 | | // the null pointer value of the destination type. |
2463 | | // |
2464 | | // Note that we do not check for ambiguity or inaccessibility |
2465 | | // here. That is handled by CheckPointerConversion. |
2466 | 379k | if (getLangOpts().CPlusPlus && FromPointeeType->isRecordType()107k && |
2467 | 23.8k | ToPointeeType->isRecordType() && |
2468 | 17.8k | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
2469 | 17.8k | IsDerivedFrom(From->getBeginLoc(), FromPointeeType, ToPointeeType)) { |
2470 | 12.7k | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2471 | 12.7k | ToPointeeType, |
2472 | 12.7k | ToType, Context); |
2473 | 12.7k | return true; |
2474 | 12.7k | } |
2475 | | |
2476 | 366k | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType()163k && |
2477 | 81.0k | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
2478 | 28 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2479 | 28 | ToPointeeType, |
2480 | 28 | ToType, Context); |
2481 | 28 | return true; |
2482 | 28 | } |
2483 | | |
2484 | 366k | return false; |
2485 | 366k | } |
2486 | | |
2487 | | /// Adopt the given qualifiers for the given type. |
2488 | 4.50k | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
2489 | 4.50k | Qualifiers TQs = T.getQualifiers(); |
2490 | | |
2491 | | // Check whether qualifiers already match. |
2492 | 4.50k | if (TQs == Qs) |
2493 | 4.48k | return T; |
2494 | | |
2495 | 21 | if (Qs.compatiblyIncludes(TQs)) |
2496 | 4 | return Context.getQualifiedType(T, Qs); |
2497 | | |
2498 | 17 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
2499 | 17 | } |
2500 | | |
2501 | | /// isObjCPointerConversion - Determines whether this is an |
2502 | | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
2503 | | /// with the same arguments and return values. |
2504 | | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
2505 | | QualType& ConvertedType, |
2506 | 2.11M | bool &IncompatibleObjC) { |
2507 | 2.11M | if (!getLangOpts().ObjC) |
2508 | 2.04M | return false; |
2509 | | |
2510 | | // The set of qualifiers on the type we're converting from. |
2511 | 75.8k | Qualifiers FromQualifiers = FromType.getQualifiers(); |
2512 | | |
2513 | | // First, we handle all conversions on ObjC object pointer types. |
2514 | 75.8k | const ObjCObjectPointerType* ToObjCPtr = |
2515 | 75.8k | ToType->getAs<ObjCObjectPointerType>(); |
2516 | 75.8k | const ObjCObjectPointerType *FromObjCPtr = |
2517 | 75.8k | FromType->getAs<ObjCObjectPointerType>(); |
2518 | | |
2519 | 75.8k | if (ToObjCPtr && FromObjCPtr8.42k ) { |
2520 | | // If the pointee types are the same (ignoring qualifications), |
2521 | | // then this is not a pointer conversion. |
2522 | 4.67k | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
2523 | 4.67k | FromObjCPtr->getPointeeType())) |
2524 | 117 | return false; |
2525 | | |
2526 | | // Conversion between Objective-C pointers. |
2527 | 4.55k | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
2528 | 4.41k | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
2529 | 4.41k | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
2530 | 4.41k | if (getLangOpts().CPlusPlus && LHS1.32k && RHS334 && |
2531 | 93 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
2532 | 93 | FromObjCPtr->getPointeeType())) |
2533 | 1 | return false; |
2534 | 4.41k | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
2535 | 4.41k | ToObjCPtr->getPointeeType(), |
2536 | 4.41k | ToType, Context); |
2537 | 4.41k | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2538 | 4.41k | return true; |
2539 | 4.41k | } |
2540 | | |
2541 | 142 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
2542 | | // Okay: this is some kind of implicit downcast of Objective-C |
2543 | | // interfaces, which is permitted. However, we're going to |
2544 | | // complain about it. |
2545 | 28 | IncompatibleObjC = true; |
2546 | 28 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
2547 | 28 | ToObjCPtr->getPointeeType(), |
2548 | 28 | ToType, Context); |
2549 | 28 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2550 | 28 | return true; |
2551 | 28 | } |
2552 | 71.3k | } |
2553 | | // Beyond this point, both types need to be C pointers or block pointers. |
2554 | 71.3k | QualType ToPointeeType; |
2555 | 71.3k | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
2556 | 36.8k | ToPointeeType = ToCPtr->getPointeeType(); |
2557 | 34.5k | else if (const BlockPointerType *ToBlockPtr = |
2558 | 59 | ToType->getAs<BlockPointerType>()) { |
2559 | | // Objective C++: We're able to convert from a pointer to any object |
2560 | | // to a block pointer type. |
2561 | 59 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()21 ) { |
2562 | 14 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2563 | 14 | return true; |
2564 | 14 | } |
2565 | 45 | ToPointeeType = ToBlockPtr->getPointeeType(); |
2566 | 45 | } |
2567 | 34.4k | else if (FromType->getAs<BlockPointerType>() && |
2568 | 28 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
2569 | | // Objective C++: We're able to convert from a block pointer type to a |
2570 | | // pointer to any object. |
2571 | 27 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2572 | 27 | return true; |
2573 | 27 | } |
2574 | 34.4k | else |
2575 | 34.4k | return false; |
2576 | | |
2577 | 36.8k | QualType FromPointeeType; |
2578 | 36.8k | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
2579 | 27.6k | FromPointeeType = FromCPtr->getPointeeType(); |
2580 | 9.15k | else if (const BlockPointerType *FromBlockPtr = |
2581 | 48 | FromType->getAs<BlockPointerType>()) |
2582 | 48 | FromPointeeType = FromBlockPtr->getPointeeType(); |
2583 | 9.10k | else |
2584 | 9.10k | return false; |
2585 | | |
2586 | | // If we have pointers to pointers, recursively check whether this |
2587 | | // is an Objective-C conversion. |
2588 | 27.7k | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType()176 && |
2589 | 13 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
2590 | 0 | IncompatibleObjC)) { |
2591 | | // We always complain about this conversion. |
2592 | 0 | IncompatibleObjC = true; |
2593 | 0 | ConvertedType = Context.getPointerType(ConvertedType); |
2594 | 0 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2595 | 0 | return true; |
2596 | 0 | } |
2597 | | // Allow conversion of pointee being objective-c pointer to another one; |
2598 | | // as in I* to id. |
2599 | 27.7k | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
2600 | 194 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
2601 | 101 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
2602 | 16 | IncompatibleObjC)) { |
2603 | | |
2604 | 16 | ConvertedType = Context.getPointerType(ConvertedType); |
2605 | 16 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2606 | 16 | return true; |
2607 | 16 | } |
2608 | | |
2609 | | // If we have pointers to functions or blocks, check whether the only |
2610 | | // differences in the argument and result types are in Objective-C |
2611 | | // pointer conversions. If so, we permit the conversion (but |
2612 | | // complain about it). |
2613 | 27.7k | const FunctionProtoType *FromFunctionType |
2614 | 27.7k | = FromPointeeType->getAs<FunctionProtoType>(); |
2615 | 27.7k | const FunctionProtoType *ToFunctionType |
2616 | 27.7k | = ToPointeeType->getAs<FunctionProtoType>(); |
2617 | 27.7k | if (FromFunctionType && ToFunctionType78 ) { |
2618 | | // If the function types are exactly the same, this isn't an |
2619 | | // Objective-C pointer conversion. |
2620 | 68 | if (Context.getCanonicalType(FromPointeeType) |
2621 | 68 | == Context.getCanonicalType(ToPointeeType)) |
2622 | 45 | return false; |
2623 | | |
2624 | | // Perform the quick checks that will tell us whether these |
2625 | | // function types are obviously different. |
2626 | 23 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
2627 | 18 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
2628 | 18 | FromFunctionType->getMethodQuals() != ToFunctionType->getMethodQuals()) |
2629 | 5 | return false; |
2630 | | |
2631 | 18 | bool HasObjCConversion = false; |
2632 | 18 | if (Context.getCanonicalType(FromFunctionType->getReturnType()) == |
2633 | 15 | Context.getCanonicalType(ToFunctionType->getReturnType())) { |
2634 | | // Okay, the types match exactly. Nothing to do. |
2635 | 3 | } else if (isObjCPointerConversion(FromFunctionType->getReturnType(), |
2636 | 3 | ToFunctionType->getReturnType(), |
2637 | 2 | ConvertedType, IncompatibleObjC)) { |
2638 | | // Okay, we have an Objective-C pointer conversion. |
2639 | 2 | HasObjCConversion = true; |
2640 | 1 | } else { |
2641 | | // Function types are too different. Abort. |
2642 | 1 | return false; |
2643 | 1 | } |
2644 | | |
2645 | | // Check argument types. |
2646 | 17 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
2647 | 37 | ArgIdx != NumArgs; ++ArgIdx20 ) { |
2648 | 20 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
2649 | 20 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
2650 | 20 | if (Context.getCanonicalType(FromArgType) |
2651 | 19 | == Context.getCanonicalType(ToArgType)) { |
2652 | | // Okay, the types match exactly. Nothing to do. |
2653 | 1 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
2654 | 1 | ConvertedType, IncompatibleObjC)) { |
2655 | | // Okay, we have an Objective-C pointer conversion. |
2656 | 1 | HasObjCConversion = true; |
2657 | 0 | } else { |
2658 | | // Argument types are too different. Abort. |
2659 | 0 | return false; |
2660 | 0 | } |
2661 | 20 | } |
2662 | | |
2663 | 17 | if (HasObjCConversion) { |
2664 | | // We had an Objective-C conversion. Allow this pointer |
2665 | | // conversion, but complain about it. |
2666 | 3 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2667 | 3 | IncompatibleObjC = true; |
2668 | 3 | return true; |
2669 | 3 | } |
2670 | 27.6k | } |
2671 | | |
2672 | 27.6k | return false; |
2673 | 27.6k | } |
2674 | | |
2675 | | /// Determine whether this is an Objective-C writeback conversion, |
2676 | | /// used for parameter passing when performing automatic reference counting. |
2677 | | /// |
2678 | | /// \param FromType The type we're converting form. |
2679 | | /// |
2680 | | /// \param ToType The type we're converting to. |
2681 | | /// |
2682 | | /// \param ConvertedType The type that will be produced after applying |
2683 | | /// this conversion. |
2684 | | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
2685 | 2.18k | QualType &ConvertedType) { |
2686 | 2.18k | if (!getLangOpts().ObjCAutoRefCount || |
2687 | 2.18k | Context.hasSameUnqualifiedType(FromType, ToType)) |
2688 | 1.16k | return false; |
2689 | | |
2690 | | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
2691 | 1.01k | QualType ToPointee; |
2692 | 1.01k | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
2693 | 513 | ToPointee = ToPointer->getPointeeType(); |
2694 | 502 | else |
2695 | 502 | return false; |
2696 | | |
2697 | 513 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
2698 | 513 | if (!ToPointee->isObjCLifetimeType() || |
2699 | 135 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
2700 | 97 | !ToQuals.withoutObjCLifetime().empty()) |
2701 | 427 | return false; |
2702 | | |
2703 | | // Argument must be a pointer to __strong to __weak. |
2704 | 86 | QualType FromPointee; |
2705 | 86 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
2706 | 85 | FromPointee = FromPointer->getPointeeType(); |
2707 | 1 | else |
2708 | 1 | return false; |
2709 | | |
2710 | 85 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
2711 | 85 | if (!FromPointee->isObjCLifetimeType() || |
2712 | 84 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
2713 | 20 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
2714 | 6 | return false; |
2715 | | |
2716 | | // Make sure that we have compatible qualifiers. |
2717 | 79 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
2718 | 79 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
2719 | 0 | return false; |
2720 | | |
2721 | | // Remove qualifiers from the pointee type we're converting from; they |
2722 | | // aren't used in the compatibility check belong, and we'll be adding back |
2723 | | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
2724 | 79 | FromPointee = FromPointee.getUnqualifiedType(); |
2725 | | |
2726 | | // The unqualified form of the pointee types must be compatible. |
2727 | 79 | ToPointee = ToPointee.getUnqualifiedType(); |
2728 | 79 | bool IncompatibleObjC; |
2729 | 79 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
2730 | 76 | FromPointee = ToPointee; |
2731 | 3 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
2732 | 3 | IncompatibleObjC)) |
2733 | 0 | return false; |
2734 | | |
2735 | | /// Construct the type we're converting to, which is a pointer to |
2736 | | /// __autoreleasing pointee. |
2737 | 79 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
2738 | 79 | ConvertedType = Context.getPointerType(FromPointee); |
2739 | 79 | return true; |
2740 | 79 | } |
2741 | | |
2742 | | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
2743 | 2.11M | QualType& ConvertedType) { |
2744 | 2.11M | QualType ToPointeeType; |
2745 | 2.11M | if (const BlockPointerType *ToBlockPtr = |
2746 | 83 | ToType->getAs<BlockPointerType>()) |
2747 | 83 | ToPointeeType = ToBlockPtr->getPointeeType(); |
2748 | 2.11M | else |
2749 | 2.11M | return false; |
2750 | | |
2751 | 83 | QualType FromPointeeType; |
2752 | 83 | if (const BlockPointerType *FromBlockPtr = |
2753 | 21 | FromType->getAs<BlockPointerType>()) |
2754 | 21 | FromPointeeType = FromBlockPtr->getPointeeType(); |
2755 | 62 | else |
2756 | 62 | return false; |
2757 | | // We have pointer to blocks, check whether the only |
2758 | | // differences in the argument and result types are in Objective-C |
2759 | | // pointer conversions. If so, we permit the conversion. |
2760 | | |
2761 | 21 | const FunctionProtoType *FromFunctionType |
2762 | 21 | = FromPointeeType->getAs<FunctionProtoType>(); |
2763 | 21 | const FunctionProtoType *ToFunctionType |
2764 | 21 | = ToPointeeType->getAs<FunctionProtoType>(); |
2765 | | |
2766 | 21 | if (!FromFunctionType || !ToFunctionType) |
2767 | 0 | return false; |
2768 | | |
2769 | 21 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
2770 | 0 | return true; |
2771 | | |
2772 | | // Perform the quick checks that will tell us whether these |
2773 | | // function types are obviously different. |
2774 | 21 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
2775 | 17 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
2776 | 4 | return false; |
2777 | | |
2778 | 17 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
2779 | 17 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
2780 | 17 | if (FromEInfo != ToEInfo) |
2781 | 0 | return false; |
2782 | | |
2783 | 17 | bool IncompatibleObjC = false; |
2784 | 17 | if (Context.hasSameType(FromFunctionType->getReturnType(), |
2785 | 11 | ToFunctionType->getReturnType())) { |
2786 | | // Okay, the types match exactly. Nothing to do. |
2787 | 6 | } else { |
2788 | 6 | QualType RHS = FromFunctionType->getReturnType(); |
2789 | 6 | QualType LHS = ToFunctionType->getReturnType(); |
2790 | 6 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
2791 | 6 | !RHS.hasQualifiers() && LHS.hasQualifiers()5 ) |
2792 | 1 | LHS = LHS.getUnqualifiedType(); |
2793 | | |
2794 | 6 | if (Context.hasSameType(RHS,LHS)) { |
2795 | | // OK exact match. |
2796 | 5 | } else if (isObjCPointerConversion(RHS, LHS, |
2797 | 4 | ConvertedType, IncompatibleObjC)) { |
2798 | 4 | if (IncompatibleObjC) |
2799 | 0 | return false; |
2800 | | // Okay, we have an Objective-C pointer conversion. |
2801 | 1 | } |
2802 | 1 | else |
2803 | 1 | return false; |
2804 | 16 | } |
2805 | | |
2806 | | // Check argument types. |
2807 | 16 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
2808 | 32 | ArgIdx != NumArgs; ++ArgIdx16 ) { |
2809 | 18 | IncompatibleObjC = false; |
2810 | 18 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
2811 | 18 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
2812 | 18 | if (Context.hasSameType(FromArgType, ToArgType)) { |
2813 | | // Okay, the types match exactly. Nothing to do. |
2814 | 5 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
2815 | 4 | ConvertedType, IncompatibleObjC)) { |
2816 | 4 | if (IncompatibleObjC) |
2817 | 1 | return false; |
2818 | | // Okay, we have an Objective-C pointer conversion. |
2819 | 1 | } else |
2820 | | // Argument types are too different. Abort. |
2821 | 1 | return false; |
2822 | 18 | } |
2823 | | |
2824 | 14 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
2825 | 14 | bool CanUseToFPT, CanUseFromFPT; |
2826 | 14 | if (!Context.mergeExtParameterInfo(ToFunctionType, FromFunctionType, |
2827 | 14 | CanUseToFPT, CanUseFromFPT, |
2828 | 14 | NewParamInfos)) |
2829 | 6 | return false; |
2830 | | |
2831 | 8 | ConvertedType = ToType; |
2832 | 8 | return true; |
2833 | 8 | } |
2834 | | |
2835 | | enum { |
2836 | | ft_default, |
2837 | | ft_different_class, |
2838 | | ft_parameter_arity, |
2839 | | ft_parameter_mismatch, |
2840 | | ft_return_type, |
2841 | | ft_qualifer_mismatch, |
2842 | | ft_noexcept |
2843 | | }; |
2844 | | |
2845 | | /// Attempts to get the FunctionProtoType from a Type. Handles |
2846 | | /// MemberFunctionPointers properly. |
2847 | 12.2k | static const FunctionProtoType *tryGetFunctionProtoType(QualType FromType) { |
2848 | 12.2k | if (auto *FPT = FromType->getAs<FunctionProtoType>()) |
2849 | 404 | return FPT; |
2850 | | |
2851 | 11.8k | if (auto *MPT = FromType->getAs<MemberPointerType>()) |
2852 | 3 | return MPT->getPointeeType()->getAs<FunctionProtoType>(); |
2853 | | |
2854 | 11.8k | return nullptr; |
2855 | 11.8k | } |
2856 | | |
2857 | | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
2858 | | /// function types. Catches different number of parameter, mismatch in |
2859 | | /// parameter types, and different return types. |
2860 | | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
2861 | 10.1k | QualType FromType, QualType ToType) { |
2862 | | // If either type is not valid, include no extra info. |
2863 | 10.1k | if (FromType.isNull() || ToType.isNull()) { |
2864 | 3.87k | PDiag << ft_default; |
2865 | 3.87k | return; |
2866 | 3.87k | } |
2867 | | |
2868 | | // Get the function type from the pointers. |
2869 | 6.26k | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()43 ) { |
2870 | 43 | const auto *FromMember = FromType->castAs<MemberPointerType>(), |
2871 | 43 | *ToMember = ToType->castAs<MemberPointerType>(); |
2872 | 43 | if (!Context.hasSameType(FromMember->getClass(), ToMember->getClass())) { |
2873 | 6 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
2874 | 6 | << QualType(FromMember->getClass(), 0); |
2875 | 6 | return; |
2876 | 6 | } |
2877 | 37 | FromType = FromMember->getPointeeType(); |
2878 | 37 | ToType = ToMember->getPointeeType(); |
2879 | 37 | } |
2880 | | |
2881 | 6.25k | if (FromType->isPointerType()) |
2882 | 316 | FromType = FromType->getPointeeType(); |
2883 | 6.25k | if (ToType->isPointerType()) |
2884 | 499 | ToType = ToType->getPointeeType(); |
2885 | | |
2886 | | // Remove references. |
2887 | 6.25k | FromType = FromType.getNonReferenceType(); |
2888 | 6.25k | ToType = ToType.getNonReferenceType(); |
2889 | | |
2890 | | // Don't print extra info for non-specialized template functions. |
2891 | 6.25k | if (FromType->isInstantiationDependentType() && |
2892 | 10 | !FromType->getAs<TemplateSpecializationType>()) { |
2893 | 10 | PDiag << ft_default; |
2894 | 10 | return; |
2895 | 10 | } |
2896 | | |
2897 | | // No extra info for same types. |
2898 | 6.24k | if (Context.hasSameType(FromType, ToType)) { |
2899 | 130 | PDiag << ft_default; |
2900 | 130 | return; |
2901 | 130 | } |
2902 | | |
2903 | 6.11k | const FunctionProtoType *FromFunction = tryGetFunctionProtoType(FromType), |
2904 | 6.11k | *ToFunction = tryGetFunctionProtoType(ToType); |
2905 | | |
2906 | | // Both types need to be function types. |
2907 | 6.11k | if (!FromFunction || !ToFunction201 ) { |
2908 | 5.94k | PDiag << ft_default; |
2909 | 5.94k | return; |
2910 | 5.94k | } |
2911 | | |
2912 | 176 | if (FromFunction->getNumParams() != ToFunction->getNumParams()) { |
2913 | 54 | PDiag << ft_parameter_arity << ToFunction->getNumParams() |
2914 | 54 | << FromFunction->getNumParams(); |
2915 | 54 | return; |
2916 | 54 | } |
2917 | | |
2918 | | // Handle different parameter types. |
2919 | 122 | unsigned ArgPos; |
2920 | 122 | if (!FunctionParamTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
2921 | 48 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
2922 | 48 | << ToFunction->getParamType(ArgPos) |
2923 | 48 | << FromFunction->getParamType(ArgPos); |
2924 | 48 | return; |
2925 | 48 | } |
2926 | | |
2927 | | // Handle different return type. |
2928 | 74 | if (!Context.hasSameType(FromFunction->getReturnType(), |
2929 | 12 | ToFunction->getReturnType())) { |
2930 | 12 | PDiag << ft_return_type << ToFunction->getReturnType() |
2931 | 12 | << FromFunction->getReturnType(); |
2932 | 12 | return; |
2933 | 12 | } |
2934 | | |
2935 | 62 | if (FromFunction->getMethodQuals() != ToFunction->getMethodQuals()) { |
2936 | 30 | PDiag << ft_qualifer_mismatch << ToFunction->getMethodQuals() |
2937 | 30 | << FromFunction->getMethodQuals(); |
2938 | 30 | return; |
2939 | 30 | } |
2940 | | |
2941 | | // Handle exception specification differences on canonical type (in C++17 |
2942 | | // onwards). |
2943 | 32 | if (cast<FunctionProtoType>(FromFunction->getCanonicalTypeUnqualified()) |
2944 | 32 | ->isNothrow() != |
2945 | 32 | cast<FunctionProtoType>(ToFunction->getCanonicalTypeUnqualified()) |
2946 | 17 | ->isNothrow()) { |
2947 | 17 | PDiag << ft_noexcept; |
2948 | 17 | return; |
2949 | 17 | } |
2950 | | |
2951 | | // Unable to find a difference, so add no extra info. |
2952 | 15 | PDiag << ft_default; |
2953 | 15 | } |
2954 | | |
2955 | | /// FunctionParamTypesAreEqual - This routine checks two function proto types |
2956 | | /// for equality of their argument types. Caller has already checked that |
2957 | | /// they have same number of arguments. If the parameters are different, |
2958 | | /// ArgPos will have the parameter index of the first different parameter. |
2959 | | bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, |
2960 | | const FunctionProtoType *NewType, |
2961 | 46.8M | unsigned *ArgPos) { |
2962 | 46.8M | for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), |
2963 | 46.8M | N = NewType->param_type_begin(), |
2964 | 46.8M | E = OldType->param_type_end(); |
2965 | 79.3M | O && (O != E); ++O, ++N32.5M ) { |
2966 | | // Ignore address spaces in pointee type. This is to disallow overloading |
2967 | | // on __ptr32/__ptr64 address spaces. |
2968 | 79.2M | QualType Old = Context.removePtrSizeAddrSpace(O->getUnqualifiedType()); |
2969 | 79.2M | QualType New = Context.removePtrSizeAddrSpace(N->getUnqualifiedType()); |
2970 | | |
2971 | 79.2M | if (!Context.hasSameType(Old, New)) { |
2972 | 46.6M | if (ArgPos) |
2973 | 48 | *ArgPos = O - OldType->param_type_begin(); |
2974 | 46.6M | return false; |
2975 | 46.6M | } |
2976 | 79.2M | } |
2977 | 127k | return true; |
2978 | 46.8M | } |
2979 | | |
2980 | | /// CheckPointerConversion - Check the pointer conversion from the |
2981 | | /// expression From to the type ToType. This routine checks for |
2982 | | /// ambiguous or inaccessible derived-to-base pointer |
2983 | | /// conversions for which IsPointerConversion has already returned |
2984 | | /// true. It returns true and produces a diagnostic if there was an |
2985 | | /// error, or returns false otherwise. |
2986 | | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
2987 | | CastKind &Kind, |
2988 | | CXXCastPath& BasePath, |
2989 | | bool IgnoreBaseAccess, |
2990 | 102k | bool Diagnose) { |
2991 | 102k | QualType FromType = From->getType(); |
2992 | 102k | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
2993 | | |
2994 | 102k | Kind = CK_BitCast; |
2995 | | |
2996 | 102k | if (Diagnose && !IsCStyleOrFunctionalCast && !FromType->isAnyPointerType()94.5k && |
2997 | 43.8k | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
2998 | 24 | Expr::NPCK_ZeroExpression) { |
2999 | 24 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
3000 | 9 | DiagRuntimeBehavior(From->getExprLoc(), From, |
3001 | 9 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
3002 | 9 | << ToType << From->getSourceRange()); |
3003 | 15 | else if (!isUnevaluatedContext()) |
3004 | 15 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
3005 | 15 | << ToType << From->getSourceRange(); |
3006 | 24 | } |
3007 | 102k | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
3008 | 98.5k | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
3009 | 50.2k | QualType FromPointeeType = FromPtrType->getPointeeType(), |
3010 | 50.2k | ToPointeeType = ToPtrType->getPointeeType(); |
3011 | | |
3012 | 50.2k | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType()18.8k && |
3013 | 5.25k | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
3014 | | // We must have a derived-to-base conversion. Check an |
3015 | | // ambiguous or inaccessible conversion. |
3016 | 5.25k | unsigned InaccessibleID = 0; |
3017 | 5.25k | unsigned AmbiguousID = 0; |
3018 | 5.25k | if (Diagnose) { |
3019 | 5.25k | InaccessibleID = diag::err_upcast_to_inaccessible_base; |
3020 | 5.25k | AmbiguousID = diag::err_ambiguous_derived_to_base_conv; |
3021 | 5.25k | } |
3022 | 5.25k | if (CheckDerivedToBaseConversion( |
3023 | 5.25k | FromPointeeType, ToPointeeType, InaccessibleID, AmbiguousID, |
3024 | 5.25k | From->getExprLoc(), From->getSourceRange(), DeclarationName(), |
3025 | 5.25k | &BasePath, IgnoreBaseAccess)) |
3026 | 13 | return true; |
3027 | | |
3028 | | // The conversion was successful. |
3029 | 5.24k | Kind = CK_DerivedToBase; |
3030 | 5.24k | } |
3031 | | |
3032 | 50.2k | if (Diagnose && !IsCStyleOrFunctionalCast && |
3033 | 46.7k | FromPointeeType->isFunctionType() && ToPointeeType->isVoidType()8 ) { |
3034 | 6 | assert(getLangOpts().MSVCCompat && |
3035 | 6 | "this should only be possible with MSVCCompat!"); |
3036 | 6 | Diag(From->getExprLoc(), diag::ext_ms_impcast_fn_obj) |
3037 | 6 | << From->getSourceRange(); |
3038 | 6 | } |
3039 | 50.2k | } |
3040 | 3.68k | } else if (const ObjCObjectPointerType *ToPtrType = |
3041 | 3.53k | ToType->getAs<ObjCObjectPointerType>()) { |
3042 | 3.53k | if (const ObjCObjectPointerType *FromPtrType = |
3043 | 1.27k | FromType->getAs<ObjCObjectPointerType>()) { |
3044 | | // Objective-C++ conversions are always okay. |
3045 | | // FIXME: We should have a different class of conversions for the |
3046 | | // Objective-C++ implicit conversions. |
3047 | 1.27k | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()997 ) |
3048 | 1.09k | return false; |
3049 | 2.26k | } else if (FromType->isBlockPointerType()) { |
3050 | 24 | Kind = CK_BlockPointerToObjCPointerCast; |
3051 | 2.23k | } else { |
3052 | 2.23k | Kind = CK_CPointerToObjCPointerCast; |
3053 | 2.23k | } |
3054 | 143 | } else if (ToType->isBlockPointerType()) { |
3055 | 125 | if (!FromType->isBlockPointerType()) |
3056 | 125 | Kind = CK_AnyPointerToBlockPointerCast; |
3057 | 125 | } |
3058 | | |
3059 | | // We shouldn't fall into this case unless it's valid for other |
3060 | | // reasons. |
3061 | 101k | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
3062 | 49.6k | Kind = CK_NullToPointer; |
3063 | | |
3064 | 101k | return false; |
3065 | 102k | } |
3066 | | |
3067 | | /// IsMemberPointerConversion - Determines whether the conversion of the |
3068 | | /// expression From, which has the (possibly adjusted) type FromType, can be |
3069 | | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
3070 | | /// If so, returns true and places the converted type (that might differ from |
3071 | | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
3072 | | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
3073 | | QualType ToType, |
3074 | | bool InOverloadResolution, |
3075 | 1.97M | QualType &ConvertedType) { |
3076 | 1.97M | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
3077 | 1.97M | if (!ToTypePtr) |
3078 | 1.97M | return false; |
3079 | | |
3080 | | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
3081 | 1.79k | if (From->isNullPointerConstant(Context, |
3082 | 461 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
3083 | 1.32k | : Expr::NPC_ValueDependentIsNull)) { |
3084 | 734 | ConvertedType = ToType; |
3085 | 734 | return true; |
3086 | 734 | } |
3087 | | |
3088 | | // Otherwise, both types have to be member pointers. |
3089 | 1.05k | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
3090 | 1.05k | if (!FromTypePtr) |
3091 | 7 | return false; |
3092 | | |
3093 | | // A pointer to member of B can be converted to a pointer to member of D, |
3094 | | // where D is derived from B (C++ 4.11p2). |
3095 | 1.04k | QualType FromClass(FromTypePtr->getClass(), 0); |
3096 | 1.04k | QualType ToClass(ToTypePtr->getClass(), 0); |
3097 | | |
3098 | 1.04k | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
3099 | 630 | IsDerivedFrom(From->getBeginLoc(), ToClass, FromClass)) { |
3100 | 491 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
3101 | 491 | ToClass.getTypePtr()); |
3102 | 491 | return true; |
3103 | 491 | } |
3104 | | |
3105 | 558 | return false; |
3106 | 558 | } |
3107 | | |
3108 | | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
3109 | | /// expression From to the type ToType. This routine checks for ambiguous or |
3110 | | /// virtual or inaccessible base-to-derived member pointer conversions |
3111 | | /// for which IsMemberPointerConversion has already returned true. It returns |
3112 | | /// true and produces a diagnostic if there was an error, or returns false |
3113 | | /// otherwise. |
3114 | | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
3115 | | CastKind &Kind, |
3116 | | CXXCastPath &BasePath, |
3117 | 652 | bool IgnoreBaseAccess) { |
3118 | 652 | QualType FromType = From->getType(); |
3119 | 652 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
3120 | 652 | if (!FromPtrType) { |
3121 | | // This must be a null pointer to member pointer conversion |
3122 | 246 | assert(From->isNullPointerConstant(Context, |
3123 | 246 | Expr::NPC_ValueDependentIsNull) && |
3124 | 246 | "Expr must be null pointer constant!"); |
3125 | 246 | Kind = CK_NullToMemberPointer; |
3126 | 246 | return false; |
3127 | 246 | } |
3128 | | |
3129 | 406 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
3130 | 406 | assert(ToPtrType && "No member pointer cast has a target type " |
3131 | 406 | "that is not a member pointer."); |
3132 | | |
3133 | 406 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
3134 | 406 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
3135 | | |
3136 | | // FIXME: What about dependent types? |
3137 | 406 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
3138 | 406 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
3139 | | |
3140 | 406 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
3141 | 406 | /*DetectVirtual=*/true); |
3142 | 406 | bool DerivationOkay = |
3143 | 406 | IsDerivedFrom(From->getBeginLoc(), ToClass, FromClass, Paths); |
3144 | 406 | assert(DerivationOkay && |
3145 | 406 | "Should not have been called if derivation isn't OK."); |
3146 | 406 | (void)DerivationOkay; |
3147 | | |
3148 | 406 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
3149 | 7 | getUnqualifiedType())) { |
3150 | 7 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
3151 | 7 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
3152 | 7 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
3153 | 7 | return true; |
3154 | 7 | } |
3155 | | |
3156 | 399 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
3157 | 17 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
3158 | 17 | << FromClass << ToClass << QualType(VBase, 0) |
3159 | 17 | << From->getSourceRange(); |
3160 | 17 | return true; |
3161 | 17 | } |
3162 | | |
3163 | 382 | if (!IgnoreBaseAccess) |
3164 | 328 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
3165 | 328 | Paths.front(), |
3166 | 328 | diag::err_downcast_from_inaccessible_base); |
3167 | | |
3168 | | // Must be a base to derived member conversion. |
3169 | 382 | BuildBasePathArray(Paths, BasePath); |
3170 | 382 | Kind = CK_BaseToDerivedMemberPointer; |
3171 | 382 | return false; |
3172 | 382 | } |
3173 | | |
3174 | | /// Determine whether the lifetime conversion between the two given |
3175 | | /// qualifiers sets is nontrivial. |
3176 | | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
3177 | 77 | Qualifiers ToQuals) { |
3178 | | // Converting anything to const __unsafe_unretained is trivial. |
3179 | 77 | if (ToQuals.hasConst() && |
3180 | 56 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
3181 | 13 | return false; |
3182 | | |
3183 | 64 | return true; |
3184 | 64 | } |
3185 | | |
3186 | | /// Perform a single iteration of the loop for checking if a qualification |
3187 | | /// conversion is valid. |
3188 | | /// |
3189 | | /// Specifically, check whether any change between the qualifiers of \p |
3190 | | /// FromType and \p ToType is permissible, given knowledge about whether every |
3191 | | /// outer layer is const-qualified. |
3192 | | static bool isQualificationConversionStep(QualType FromType, QualType ToType, |
3193 | | bool CStyle, bool IsTopLevel, |
3194 | | bool &PreviousToQualsIncludeConst, |
3195 | 5.23M | bool &ObjCLifetimeConversion) { |
3196 | 5.23M | Qualifiers FromQuals = FromType.getQualifiers(); |
3197 | 5.23M | Qualifiers ToQuals = ToType.getQualifiers(); |
3198 | | |
3199 | | // Ignore __unaligned qualifier if this type is void. |
3200 | 5.23M | if (ToType.getUnqualifiedType()->isVoidType()) |
3201 | 12.7k | FromQuals.removeUnaligned(); |
3202 | | |
3203 | | // Objective-C ARC: |
3204 | | // Check Objective-C lifetime conversions. |
3205 | 5.23M | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime()) { |
3206 | 170 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
3207 | 77 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
3208 | 64 | ObjCLifetimeConversion = true; |
3209 | 77 | FromQuals.removeObjCLifetime(); |
3210 | 77 | ToQuals.removeObjCLifetime(); |
3211 | 93 | } else { |
3212 | | // Qualification conversions cannot cast between different |
3213 | | // Objective-C lifetime qualifiers. |
3214 | 93 | return false; |
3215 | 93 | } |
3216 | 5.23M | } |
3217 | | |
3218 | | // Allow addition/removal of GC attributes but not changing GC attributes. |
3219 | 5.23M | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
3220 | 8 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr()2 )) { |
3221 | 6 | FromQuals.removeObjCGCAttr(); |
3222 | 6 | ToQuals.removeObjCGCAttr(); |
3223 | 6 | } |
3224 | | |
3225 | | // -- for every j > 0, if const is in cv 1,j then const is in cv |
3226 | | // 2,j, and similarly for volatile. |
3227 | 5.23M | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)5.19M ) |
3228 | 113k | return false; |
3229 | | |
3230 | | // If address spaces mismatch: |
3231 | | // - in top level it is only valid to convert to addr space that is a |
3232 | | // superset in all cases apart from C-style casts where we allow |
3233 | | // conversions between overlapping address spaces. |
3234 | | // - in non-top levels it is not a valid conversion. |
3235 | 5.11M | if (ToQuals.getAddressSpace() != FromQuals.getAddressSpace() && |
3236 | 408 | (!IsTopLevel || |
3237 | 386 | !(ToQuals.isAddressSpaceSupersetOf(FromQuals) || |
3238 | 29 | (CStyle && FromQuals.isAddressSpaceSupersetOf(ToQuals))))) |
3239 | 50 | return false; |
3240 | | |
3241 | | // -- if the cv 1,j and cv 2,j are different, then const is in |
3242 | | // every cv for 0 < k < j. |
3243 | 5.11M | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers()5.08M && |
3244 | 2.47M | !PreviousToQualsIncludeConst) |
3245 | 715 | return false; |
3246 | | |
3247 | | // Keep track of whether all prior cv-qualifiers in the "to" type |
3248 | | // include const. |
3249 | 5.11M | PreviousToQualsIncludeConst = |
3250 | 5.11M | PreviousToQualsIncludeConst && ToQuals.hasConst()5.11M ; |
3251 | 5.11M | return true; |
3252 | 5.11M | } |
3253 | | |
3254 | | /// IsQualificationConversion - Determines whether the conversion from |
3255 | | /// an rvalue of type FromType to ToType is a qualification conversion |
3256 | | /// (C++ 4.4). |
3257 | | /// |
3258 | | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
3259 | | /// when the qualification conversion involves a change in the Objective-C |
3260 | | /// object lifetime. |
3261 | | bool |
3262 | | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
3263 | 22.2M | bool CStyle, bool &ObjCLifetimeConversion) { |
3264 | 22.2M | FromType = Context.getCanonicalType(FromType); |
3265 | 22.2M | ToType = Context.getCanonicalType(ToType); |
3266 | 22.2M | ObjCLifetimeConversion = false; |
3267 | | |
3268 | | // If FromType and ToType are the same type, this is not a |
3269 | | // qualification conversion. |
3270 | 22.2M | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
3271 | 21.2M | return false; |
3272 | | |
3273 | | // (C++ 4.4p4): |
3274 | | // A conversion can add cv-qualifiers at levels other than the first |
3275 | | // in multi-level pointers, subject to the following rules: [...] |
3276 | 923k | bool PreviousToQualsIncludeConst = true; |
3277 | 923k | bool UnwrappedAnyPointer = false; |
3278 | 1.36M | while (Context.UnwrapSimilarTypes(FromType, ToType)) { |
3279 | 446k | if (!isQualificationConversionStep( |
3280 | 446k | FromType, ToType, CStyle, !UnwrappedAnyPointer, |
3281 | 446k | PreviousToQualsIncludeConst, ObjCLifetimeConversion)) |
3282 | 5.33k | return false; |
3283 | 440k | UnwrappedAnyPointer = true; |
3284 | 440k | } |
3285 | | |
3286 | | // We are left with FromType and ToType being the pointee types |
3287 | | // after unwrapping the original FromType and ToType the same number |
3288 | | // of times. If we unwrapped any pointers, and if FromType and |
3289 | | // ToType have the same unqualified type (since we checked |
3290 | | // qualifiers above), then this is a qualification conversion. |
3291 | 918k | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType)439k ; |
3292 | 923k | } |
3293 | | |
3294 | | /// - Determine whether this is a conversion from a scalar type to an |
3295 | | /// atomic type. |
3296 | | /// |
3297 | | /// If successful, updates \c SCS's second and third steps in the conversion |
3298 | | /// sequence to finish the conversion. |
3299 | | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
3300 | | bool InOverloadResolution, |
3301 | | StandardConversionSequence &SCS, |
3302 | 894k | bool CStyle) { |
3303 | 894k | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
3304 | 894k | if (!ToAtomic) |
3305 | 893k | return false; |
3306 | | |
3307 | 616 | StandardConversionSequence InnerSCS; |
3308 | 616 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
3309 | 616 | InOverloadResolution, InnerSCS, |
3310 | 616 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
3311 | 54 | return false; |
3312 | | |
3313 | 562 | SCS.Second = InnerSCS.Second; |
3314 | 562 | SCS.setToType(1, InnerSCS.getToType(1)); |
3315 | 562 | SCS.Third = InnerSCS.Third; |
3316 | 562 | SCS.QualificationIncludesObjCLifetime |
3317 | 562 | = InnerSCS.QualificationIncludesObjCLifetime; |
3318 | 562 | SCS.setToType(2, InnerSCS.getToType(2)); |
3319 | 562 | return true; |
3320 | 562 | } |
3321 | | |
3322 | | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
3323 | | CXXConstructorDecl *Constructor, |
3324 | 1.04k | QualType Type) { |
3325 | 1.04k | const auto *CtorType = Constructor->getType()->castAs<FunctionProtoType>(); |
3326 | 1.04k | if (CtorType->getNumParams() > 0) { |
3327 | 1.02k | QualType FirstArg = CtorType->getParamType(0); |
3328 | 1.02k | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
3329 | 582 | return true; |
3330 | 463 | } |
3331 | 463 | return false; |
3332 | 463 | } |
3333 | | |
3334 | | static OverloadingResult |
3335 | | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
3336 | | CXXRecordDecl *To, |
3337 | | UserDefinedConversionSequence &User, |
3338 | | OverloadCandidateSet &CandidateSet, |
3339 | 679 | bool AllowExplicit) { |
3340 | 679 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3341 | 2.93k | for (auto *D : S.LookupConstructors(To)) { |
3342 | 2.93k | auto Info = getConstructorInfo(D); |
3343 | 2.93k | if (!Info) |
3344 | 7 | continue; |
3345 | | |
3346 | 2.92k | bool Usable = !Info.Constructor->isInvalidDecl() && |
3347 | 2.92k | S.isInitListConstructor(Info.Constructor); |
3348 | 2.92k | if (Usable) { |
3349 | | // If the first argument is (a reference to) the target type, |
3350 | | // suppress conversions. |
3351 | 109 | bool SuppressUserConversions = isFirstArgumentCompatibleWithType( |
3352 | 109 | S.Context, Info.Constructor, ToType); |
3353 | 109 | if (Info.ConstructorTmpl) |
3354 | 0 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
3355 | 0 | /*ExplicitArgs*/ nullptr, From, |
3356 | 0 | CandidateSet, SuppressUserConversions, |
3357 | 0 | /*PartialOverloading*/ false, |
3358 | 0 | AllowExplicit); |
3359 | 109 | else |
3360 | 109 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, From, |
3361 | 109 | CandidateSet, SuppressUserConversions, |
3362 | 109 | /*PartialOverloading*/ false, AllowExplicit); |
3363 | 109 | } |
3364 | 2.92k | } |
3365 | | |
3366 | 679 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
3367 | | |
3368 | 679 | OverloadCandidateSet::iterator Best; |
3369 | 679 | switch (auto Result = |
3370 | 679 | CandidateSet.BestViableFunction(S, From->getBeginLoc(), Best)) { |
3371 | 2 | case OR_Deleted: |
3372 | 106 | case OR_Success: { |
3373 | | // Record the standard conversion we used and the conversion function. |
3374 | 106 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
3375 | 106 | QualType ThisType = Constructor->getThisType(); |
3376 | | // Initializer lists don't have conversions as such. |
3377 | 106 | User.Before.setAsIdentityConversion(); |
3378 | 106 | User.HadMultipleCandidates = HadMultipleCandidates; |
3379 | 106 | User.ConversionFunction = Constructor; |
3380 | 106 | User.FoundConversionFunction = Best->FoundDecl; |
3381 | 106 | User.After.setAsIdentityConversion(); |
3382 | 106 | User.After.setFromType(ThisType->castAs<PointerType>()->getPointeeType()); |
3383 | 106 | User.After.setAllToTypes(ToType); |
3384 | 106 | return Result; |
3385 | 2 | } |
3386 | | |
3387 | 573 | case OR_No_Viable_Function: |
3388 | 573 | return OR_No_Viable_Function; |
3389 | 0 | case OR_Ambiguous: |
3390 | 0 | return OR_Ambiguous; |
3391 | 0 | } |
3392 | | |
3393 | 0 | llvm_unreachable("Invalid OverloadResult!"); |
3394 | 0 | } |
3395 | | |
3396 | | /// Determines whether there is a user-defined conversion sequence |
3397 | | /// (C++ [over.ics.user]) that converts expression From to the type |
3398 | | /// ToType. If such a conversion exists, User will contain the |
3399 | | /// user-defined conversion sequence that performs such a conversion |
3400 | | /// and this routine will return true. Otherwise, this routine returns |
3401 | | /// false and User is unspecified. |
3402 | | /// |
3403 | | /// \param AllowExplicit true if the conversion should consider C++0x |
3404 | | /// "explicit" conversion functions as well as non-explicit conversion |
3405 | | /// functions (C++0x [class.conv.fct]p2). |
3406 | | /// |
3407 | | /// \param AllowObjCConversionOnExplicit true if the conversion should |
3408 | | /// allow an extra Objective-C pointer conversion on uses of explicit |
3409 | | /// constructors. Requires \c AllowExplicit to also be set. |
3410 | | static OverloadingResult |
3411 | | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
3412 | | UserDefinedConversionSequence &User, |
3413 | | OverloadCandidateSet &CandidateSet, |
3414 | | AllowedExplicit AllowExplicit, |
3415 | 1.07M | bool AllowObjCConversionOnExplicit) { |
3416 | 1.07M | assert(AllowExplicit != AllowedExplicit::None || |
3417 | 1.07M | !AllowObjCConversionOnExplicit); |
3418 | 1.07M | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3419 | | |
3420 | | // Whether we will only visit constructors. |
3421 | 1.07M | bool ConstructorsOnly = false; |
3422 | | |
3423 | | // If the type we are conversion to is a class type, enumerate its |
3424 | | // constructors. |
3425 | 1.07M | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
3426 | | // C++ [over.match.ctor]p1: |
3427 | | // When objects of class type are direct-initialized (8.5), or |
3428 | | // copy-initialized from an expression of the same or a |
3429 | | // derived class type (8.5), overload resolution selects the |
3430 | | // constructor. [...] For copy-initialization, the candidate |
3431 | | // functions are all the converting constructors (12.3.1) of |
3432 | | // that class. The argument list is the expression-list within |
3433 | | // the parentheses of the initializer. |
3434 | 159k | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
3435 | 159k | (From->getType()->getAs<RecordType>() && |
3436 | 71.1k | S.IsDerivedFrom(From->getBeginLoc(), From->getType(), ToType))) |
3437 | 0 | ConstructorsOnly = true; |
3438 | | |
3439 | 159k | if (!S.isCompleteType(From->getExprLoc(), ToType)) { |
3440 | | // We're not going to find any constructors. |
3441 | 149k | } else if (CXXRecordDecl *ToRecordDecl |
3442 | 149k | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
3443 | | |
3444 | 149k | Expr **Args = &From; |
3445 | 149k | unsigned NumArgs = 1; |
3446 | 149k | bool ListInitializing = false; |
3447 | 149k | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
3448 | | // But first, see if there is an init-list-constructor that will work. |
3449 | 679 | OverloadingResult Result = IsInitializerListConstructorConversion( |
3450 | 679 | S, From, ToType, ToRecordDecl, User, CandidateSet, |
3451 | 679 | AllowExplicit == AllowedExplicit::All); |
3452 | 679 | if (Result != OR_No_Viable_Function) |
3453 | 106 | return Result; |
3454 | | // Never mind. |
3455 | 573 | CandidateSet.clear( |
3456 | 573 | OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3457 | | |
3458 | | // If we're list-initializing, we pass the individual elements as |
3459 | | // arguments, not the entire list. |
3460 | 573 | Args = InitList->getInits(); |
3461 | 573 | NumArgs = InitList->getNumInits(); |
3462 | 573 | ListInitializing = true; |
3463 | 573 | } |
3464 | | |
3465 | 674k | for (auto *D : S.LookupConstructors(ToRecordDecl))149k { |
3466 | 674k | auto Info = getConstructorInfo(D); |
3467 | 674k | if (!Info) |
3468 | 448 | continue; |
3469 | | |
3470 | 673k | bool Usable = !Info.Constructor->isInvalidDecl(); |
3471 | 673k | if (!ListInitializing) |
3472 | 671k | Usable = Usable && Info.Constructor->isConvertingConstructor( |
3473 | 671k | /*AllowExplicit*/ true); |
3474 | 673k | if (Usable) { |
3475 | 461k | bool SuppressUserConversions = !ConstructorsOnly; |
3476 | 461k | if (SuppressUserConversions && ListInitializing) { |
3477 | 1.98k | SuppressUserConversions = false; |
3478 | 1.98k | if (NumArgs == 1) { |
3479 | | // If the first argument is (a reference to) the target type, |
3480 | | // suppress conversions. |
3481 | 936 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
3482 | 936 | S.Context, Info.Constructor, ToType); |
3483 | 936 | } |
3484 | 1.98k | } |
3485 | 461k | if (Info.ConstructorTmpl) |
3486 | 95.3k | S.AddTemplateOverloadCandidate( |
3487 | 95.3k | Info.ConstructorTmpl, Info.FoundDecl, |
3488 | 95.3k | /*ExplicitArgs*/ nullptr, llvm::makeArrayRef(Args, NumArgs), |
3489 | 95.3k | CandidateSet, SuppressUserConversions, |
3490 | 95.3k | /*PartialOverloading*/ false, |
3491 | 95.3k | AllowExplicit == AllowedExplicit::All); |
3492 | 365k | else |
3493 | | // Allow one user-defined conversion when user specifies a |
3494 | | // From->ToType conversion via an static cast (c-style, etc). |
3495 | 365k | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
3496 | 365k | llvm::makeArrayRef(Args, NumArgs), |
3497 | 365k | CandidateSet, SuppressUserConversions, |
3498 | 365k | /*PartialOverloading*/ false, |
3499 | 365k | AllowExplicit == AllowedExplicit::All); |
3500 | 461k | } |
3501 | 673k | } |
3502 | 149k | } |
3503 | 159k | } |
3504 | | |
3505 | | // Enumerate conversion functions, if we're allowed to. |
3506 | 1.07M | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
3507 | 1.07M | } else if (!S.isCompleteType(From->getBeginLoc(), From->getType())) { |
3508 | | // No conversion functions from incomplete types. |
3509 | 1.06M | } else if (const RecordType *FromRecordType = |
3510 | 798k | From->getType()->getAs<RecordType>()) { |
3511 | 798k | if (CXXRecordDecl *FromRecordDecl |
3512 | 798k | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
3513 | | // Add all of the conversion functions as candidates. |
3514 | 798k | const auto &Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
3515 | 1.26M | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I469k ) { |
3516 | 469k | DeclAccessPair FoundDecl = I.getPair(); |
3517 | 469k | NamedDecl *D = FoundDecl.getDecl(); |
3518 | 469k | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
3519 | 469k | if (isa<UsingShadowDecl>(D)) |
3520 | 14 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
3521 | | |
3522 | 469k | CXXConversionDecl *Conv; |
3523 | 469k | FunctionTemplateDecl *ConvTemplate; |
3524 | 469k | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
3525 | 2.76k | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
3526 | 466k | else |
3527 | 466k | Conv = cast<CXXConversionDecl>(D); |
3528 | | |
3529 | 469k | if (ConvTemplate) |
3530 | 2.76k | S.AddTemplateConversionCandidate( |
3531 | 2.76k | ConvTemplate, FoundDecl, ActingContext, From, ToType, |
3532 | 2.76k | CandidateSet, AllowObjCConversionOnExplicit, |
3533 | 2.76k | AllowExplicit != AllowedExplicit::None); |
3534 | 466k | else |
3535 | 466k | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, ToType, |
3536 | 466k | CandidateSet, AllowObjCConversionOnExplicit, |
3537 | 466k | AllowExplicit != AllowedExplicit::None); |
3538 | 469k | } |
3539 | 798k | } |
3540 | 798k | } |
3541 | | |
3542 | 1.07M | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
3543 | | |
3544 | 1.07M | OverloadCandidateSet::iterator Best; |
3545 | 1.07M | switch (auto Result = |
3546 | 1.07M | CandidateSet.BestViableFunction(S, From->getBeginLoc(), Best)) { |
3547 | 406k | case OR_Success: |
3548 | 406k | case OR_Deleted: |
3549 | | // Record the standard conversion we used and the conversion function. |
3550 | 406k | if (CXXConstructorDecl *Constructor |
3551 | 25.9k | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
3552 | | // C++ [over.ics.user]p1: |
3553 | | // If the user-defined conversion is specified by a |
3554 | | // constructor (12.3.1), the initial standard conversion |
3555 | | // sequence converts the source type to the type required by |
3556 | | // the argument of the constructor. |
3557 | | // |
3558 | 25.9k | QualType ThisType = Constructor->getThisType(); |
3559 | 25.9k | if (isa<InitListExpr>(From)) { |
3560 | | // Initializer lists don't have conversions as such. |
3561 | 461 | User.Before.setAsIdentityConversion(); |
3562 | 25.4k | } else { |
3563 | 25.4k | if (Best->Conversions[0].isEllipsis()) |
3564 | 386 | User.EllipsisConversion = true; |
3565 | 25.0k | else { |
3566 | 25.0k | User.Before = Best->Conversions[0].Standard; |
3567 | 25.0k | User.EllipsisConversion = false; |
3568 | 25.0k | } |
3569 | 25.4k | } |
3570 | 25.9k | User.HadMultipleCandidates = HadMultipleCandidates; |
3571 | 25.9k | User.ConversionFunction = Constructor; |
3572 | 25.9k | User.FoundConversionFunction = Best->FoundDecl; |
3573 | 25.9k | User.After.setAsIdentityConversion(); |
3574 | 25.9k | User.After.setFromType(ThisType->castAs<PointerType>()->getPointeeType()); |
3575 | 25.9k | User.After.setAllToTypes(ToType); |
3576 | 25.9k | return Result; |
3577 | 25.9k | } |
3578 | 380k | if (CXXConversionDecl *Conversion |
3579 | 380k | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
3580 | | // C++ [over.ics.user]p1: |
3581 | | // |
3582 | | // [...] If the user-defined conversion is specified by a |
3583 | | // conversion function (12.3.2), the initial standard |
3584 | | // conversion sequence converts the source type to the |
3585 | | // implicit object parameter of the conversion function. |
3586 | 380k | User.Before = Best->Conversions[0].Standard; |
3587 | 380k | User.HadMultipleCandidates = HadMultipleCandidates; |
3588 | 380k | User.ConversionFunction = Conversion; |
3589 | 380k | User.FoundConversionFunction = Best->FoundDecl; |
3590 | 380k | User.EllipsisConversion = false; |
3591 | | |
3592 | | // C++ [over.ics.user]p2: |
3593 | | // The second standard conversion sequence converts the |
3594 | | // result of the user-defined conversion to the target type |
3595 | | // for the sequence. Since an implicit conversion sequence |
3596 | | // is an initialization, the special rules for |
3597 | | // initialization by user-defined conversion apply when |
3598 | | // selecting the best user-defined conversion for a |
3599 | | // user-defined conversion sequence (see 13.3.3 and |
3600 | | // 13.3.3.1). |
3601 | 380k | User.After = Best->FinalConversion; |
3602 | 380k | return Result; |
3603 | 380k | } |
3604 | 0 | llvm_unreachable("Not a constructor or conversion function?"); |
3605 | |
|
3606 | 658k | case OR_No_Viable_Function: |
3607 | 658k | return OR_No_Viable_Function; |
3608 | |
|
3609 | 7.99k | case OR_Ambiguous: |
3610 | 7.99k | return OR_Ambiguous; |
3611 | 0 | } |
3612 | | |
3613 | 0 | llvm_unreachable("Invalid OverloadResult!"); |
3614 | 0 | } |
3615 | | |
3616 | | bool |
3617 | 1.21k | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
3618 | 1.21k | ImplicitConversionSequence ICS; |
3619 | 1.21k | OverloadCandidateSet CandidateSet(From->getExprLoc(), |
3620 | 1.21k | OverloadCandidateSet::CSK_Normal); |
3621 | 1.21k | OverloadingResult OvResult = |
3622 | 1.21k | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
3623 | 1.21k | CandidateSet, AllowedExplicit::None, false); |
3624 | | |
3625 | 1.21k | if (!(OvResult == OR_Ambiguous || |
3626 | 1.21k | (OvResult == OR_No_Viable_Function && !CandidateSet.empty()))) |
3627 | 125 | return false; |
3628 | | |
3629 | 1.08k | auto Cands = CandidateSet.CompleteCandidates( |
3630 | 1.08k | *this, |
3631 | 1.08k | OvResult == OR_Ambiguous ? OCD_AmbiguousCandidates2 : OCD_AllCandidates, |
3632 | 1.08k | From); |
3633 | 1.08k | if (OvResult == OR_Ambiguous) |
3634 | 2 | Diag(From->getBeginLoc(), diag::err_typecheck_ambiguous_condition) |
3635 | 2 | << From->getType() << ToType << From->getSourceRange(); |
3636 | 1.08k | else { // OR_No_Viable_Function && !CandidateSet.empty() |
3637 | 1.08k | if (!RequireCompleteType(From->getBeginLoc(), ToType, |
3638 | 1.08k | diag::err_typecheck_nonviable_condition_incomplete, |
3639 | 1.08k | From->getType(), From->getSourceRange())) |
3640 | 1.08k | Diag(From->getBeginLoc(), diag::err_typecheck_nonviable_condition) |
3641 | 1.08k | << false << From->getType() << From->getSourceRange() << ToType; |
3642 | 1.08k | } |
3643 | | |
3644 | 1.08k | CandidateSet.NoteCandidates( |
3645 | 1.08k | *this, From, Cands); |
3646 | 1.08k | return true; |
3647 | 1.08k | } |
3648 | | |
3649 | | // Helper for compareConversionFunctions that gets the FunctionType that the |
3650 | | // conversion-operator return value 'points' to, or nullptr. |
3651 | | static const FunctionType * |
3652 | 168 | getConversionOpReturnTyAsFunction(CXXConversionDecl *Conv) { |
3653 | 168 | const FunctionType *ConvFuncTy = Conv->getType()->castAs<FunctionType>(); |
3654 | 168 | const PointerType *RetPtrTy = |
3655 | 168 | ConvFuncTy->getReturnType()->getAs<PointerType>(); |
3656 | | |
3657 | 168 | if (!RetPtrTy) |
3658 | 0 | return nullptr; |
3659 | | |
3660 | 168 | return RetPtrTy->getPointeeType()->getAs<FunctionType>(); |
3661 | 168 | } |
3662 | | |
3663 | | /// Compare the user-defined conversion functions or constructors |
3664 | | /// of two user-defined conversion sequences to determine whether any ordering |
3665 | | /// is possible. |
3666 | | static ImplicitConversionSequence::CompareKind |
3667 | | compareConversionFunctions(Sema &S, FunctionDecl *Function1, |
3668 | 32.5k | FunctionDecl *Function2) { |
3669 | 32.5k | CXXConversionDecl *Conv1 = dyn_cast_or_null<CXXConversionDecl>(Function1); |
3670 | 32.5k | CXXConversionDecl *Conv2 = dyn_cast_or_null<CXXConversionDecl>(Function2); |
3671 | 32.5k | if (!Conv1 || !Conv232.5k ) |
3672 | 30 | return ImplicitConversionSequence::Indistinguishable; |
3673 | | |
3674 | 32.5k | if (!Conv1->getParent()->isLambda() || !Conv2->getParent()->isLambda()96 ) |
3675 | 32.4k | return ImplicitConversionSequence::Indistinguishable; |
3676 | | |
3677 | | // Objective-C++: |
3678 | | // If both conversion functions are implicitly-declared conversions from |
3679 | | // a lambda closure type to a function pointer and a block pointer, |
3680 | | // respectively, always prefer the conversion to a function pointer, |
3681 | | // because the function pointer is more lightweight and is more likely |
3682 | | // to keep code working. |
3683 | 96 | if (S.getLangOpts().ObjC && S.getLangOpts().CPlusPlus1112 ) { |
3684 | 12 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
3685 | 12 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
3686 | 12 | if (Block1 != Block2) |
3687 | 12 | return Block1 ? ImplicitConversionSequence::Worse4 |
3688 | 8 | : ImplicitConversionSequence::Better; |
3689 | 84 | } |
3690 | | |
3691 | | // In order to support multiple calling conventions for the lambda conversion |
3692 | | // operator (such as when the free and member function calling convention is |
3693 | | // different), prefer the 'free' mechanism, followed by the calling-convention |
3694 | | // of operator(). The latter is in place to support the MSVC-like solution of |
3695 | | // defining ALL of the possible conversions in regards to calling-convention. |
3696 | 84 | const FunctionType *Conv1FuncRet = getConversionOpReturnTyAsFunction(Conv1); |
3697 | 84 | const FunctionType *Conv2FuncRet = getConversionOpReturnTyAsFunction(Conv2); |
3698 | | |
3699 | 84 | if (Conv1FuncRet && Conv2FuncRet && |
3700 | 84 | Conv1FuncRet->getCallConv() != Conv2FuncRet->getCallConv()) { |
3701 | 52 | CallingConv Conv1CC = Conv1FuncRet->getCallConv(); |
3702 | 52 | CallingConv Conv2CC = Conv2FuncRet->getCallConv(); |
3703 | | |
3704 | 52 | CXXMethodDecl *CallOp = Conv2->getParent()->getLambdaCallOperator(); |
3705 | 52 | const FunctionProtoType *CallOpProto = |
3706 | 52 | CallOp->getType()->getAs<FunctionProtoType>(); |
3707 | | |
3708 | 52 | CallingConv CallOpCC = |
3709 | 52 | CallOp->getType()->getAs<FunctionType>()->getCallConv(); |
3710 | 52 | CallingConv DefaultFree = S.Context.getDefaultCallingConvention( |
3711 | 52 | CallOpProto->isVariadic(), /*IsCXXMethod=*/false); |
3712 | 52 | CallingConv DefaultMember = S.Context.getDefaultCallingConvention( |
3713 | 52 | CallOpProto->isVariadic(), /*IsCXXMethod=*/true); |
3714 | | |
3715 | 52 | CallingConv PrefOrder[] = {DefaultFree, DefaultMember, CallOpCC}; |
3716 | 52 | for (CallingConv CC : PrefOrder) { |
3717 | 52 | if (Conv1CC == CC) |
3718 | 26 | return ImplicitConversionSequence::Better; |
3719 | 26 | if (Conv2CC == CC) |
3720 | 26 | return ImplicitConversionSequence::Worse; |
3721 | 26 | } |
3722 | 52 | } |
3723 | | |
3724 | 32 | return ImplicitConversionSequence::Indistinguishable; |
3725 | 84 | } |
3726 | | |
3727 | | static bool hasDeprecatedStringLiteralToCharPtrConversion( |
3728 | 65.3M | const ImplicitConversionSequence &ICS) { |
3729 | 65.3M | return (ICS.isStandard() && ICS.Standard.DeprecatedStringLiteralToCharPtr63.1M ) || |
3730 | 65.3M | (ICS.isUserDefined() && |
3731 | 2.06M | ICS.UserDefined.Before.DeprecatedStringLiteralToCharPtr); |
3732 | 65.3M | } |
3733 | | |
3734 | | /// CompareImplicitConversionSequences - Compare two implicit |
3735 | | /// conversion sequences to determine whether one is better than the |
3736 | | /// other or if they are indistinguishable (C++ 13.3.3.2). |
3737 | | static ImplicitConversionSequence::CompareKind |
3738 | | CompareImplicitConversionSequences(Sema &S, SourceLocation Loc, |
3739 | | const ImplicitConversionSequence& ICS1, |
3740 | | const ImplicitConversionSequence& ICS2) |
3741 | 16.7M | { |
3742 | | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
3743 | | // conversion sequences (as defined in 13.3.3.1) |
3744 | | // -- a standard conversion sequence (13.3.3.1.1) is a better |
3745 | | // conversion sequence than a user-defined conversion sequence or |
3746 | | // an ellipsis conversion sequence, and |
3747 | | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
3748 | | // conversion sequence than an ellipsis conversion sequence |
3749 | | // (13.3.3.1.3). |
3750 | | // |
3751 | | // C++0x [over.best.ics]p10: |
3752 | | // For the purpose of ranking implicit conversion sequences as |
3753 | | // described in 13.3.3.2, the ambiguous conversion sequence is |
3754 | | // treated as a user-defined sequence that is indistinguishable |
3755 | | // from any other user-defined conversion sequence. |
3756 | | |
3757 | | // String literal to 'char *' conversion has been deprecated in C++03. It has |
3758 | | // been removed from C++11. We still accept this conversion, if it happens at |
3759 | | // the best viable function. Otherwise, this conversion is considered worse |
3760 | | // than ellipsis conversion. Consider this as an extension; this is not in the |
3761 | | // standard. For example: |
3762 | | // |
3763 | | // int &f(...); // #1 |
3764 | | // void f(char*); // #2 |
3765 | | // void g() { int &r = f("foo"); } |
3766 | | // |
3767 | | // In C++03, we pick #2 as the best viable function. |
3768 | | // In C++11, we pick #1 as the best viable function, because ellipsis |
3769 | | // conversion is better than string-literal to char* conversion (since there |
3770 | | // is no such conversion in C++11). If there was no #1 at all or #1 couldn't |
3771 | | // convert arguments, #2 would be the best viable function in C++11. |
3772 | | // If the best viable function has this conversion, a warning will be issued |
3773 | | // in C++03, or an ExtWarn (+SFINAE failure) will be issued in C++11. |
3774 | | |
3775 | 16.7M | if (S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings14.4M && |
3776 | 14.4M | hasDeprecatedStringLiteralToCharPtrConversion(ICS1) != |
3777 | 14.4M | hasDeprecatedStringLiteralToCharPtrConversion(ICS2)) |
3778 | 0 | return hasDeprecatedStringLiteralToCharPtrConversion(ICS1) |
3779 | 0 | ? ImplicitConversionSequence::Worse |
3780 | 0 | : ImplicitConversionSequence::Better; |
3781 | | |
3782 | 16.7M | if (ICS1.getKindRank() < ICS2.getKindRank()) |
3783 | 93.6k | return ImplicitConversionSequence::Better; |
3784 | 16.6M | if (ICS2.getKindRank() < ICS1.getKindRank()) |
3785 | 66.9k | return ImplicitConversionSequence::Worse; |
3786 | | |
3787 | | // The following checks require both conversion sequences to be of |
3788 | | // the same kind. |
3789 | 16.5M | if (ICS1.getKind() != ICS2.getKind()) |
3790 | 4 | return ImplicitConversionSequence::Indistinguishable; |
3791 | | |
3792 | 16.5M | ImplicitConversionSequence::CompareKind Result = |
3793 | 16.5M | ImplicitConversionSequence::Indistinguishable; |
3794 | | |
3795 | | // Two implicit conversion sequences of the same form are |
3796 | | // indistinguishable conversion sequences unless one of the |
3797 | | // following rules apply: (C++ 13.3.3.2p3): |
3798 | | |
3799 | | // List-initialization sequence L1 is a better conversion sequence than |
3800 | | // list-initialization sequence L2 if: |
3801 | | // - L1 converts to std::initializer_list<X> for some X and L2 does not, or, |
3802 | | // if not that, |
3803 | | // - L1 converts to type "array of N1 T", L2 converts to type "array of N2 T", |
3804 | | // and N1 is smaller than N2., |
3805 | | // even if one of the other rules in this paragraph would otherwise apply. |
3806 | 16.5M | if (!ICS1.isBad()) { |
3807 | 16.5M | if (ICS1.isStdInitializerListElement() && |
3808 | 149 | !ICS2.isStdInitializerListElement()) |
3809 | 63 | return ImplicitConversionSequence::Better; |
3810 | 16.5M | if (!ICS1.isStdInitializerListElement() && |
3811 | 16.5M | ICS2.isStdInitializerListElement()) |
3812 | 42 | return ImplicitConversionSequence::Worse; |
3813 | 16.5M | } |
3814 | | |
3815 | 16.5M | if (ICS1.isStandard()) |
3816 | | // Standard conversion sequence S1 is a better conversion sequence than |
3817 | | // standard conversion sequence S2 if [...] |
3818 | 15.9M | Result = CompareStandardConversionSequences(S, Loc, |
3819 | 15.9M | ICS1.Standard, ICS2.Standard); |
3820 | 631k | else if (ICS1.isUserDefined()) { |
3821 | | // User-defined conversion sequence U1 is a better conversion |
3822 | | // sequence than another user-defined conversion sequence U2 if |
3823 | | // they contain the same user-defined conversion function or |
3824 | | // constructor and if the second standard conversion sequence of |
3825 | | // U1 is better than the second standard conversion sequence of |
3826 | | // U2 (C++ 13.3.3.2p3). |
3827 | 618k | if (ICS1.UserDefined.ConversionFunction == |
3828 | 618k | ICS2.UserDefined.ConversionFunction) |
3829 | 606k | Result = CompareStandardConversionSequences(S, Loc, |
3830 | 606k | ICS1.UserDefined.After, |
3831 | 606k | ICS2.UserDefined.After); |
3832 | 12.0k | else |
3833 | 12.0k | Result = compareConversionFunctions(S, |
3834 | 12.0k | ICS1.UserDefined.ConversionFunction, |
3835 | 12.0k | ICS2.UserDefined.ConversionFunction); |
3836 | 618k | } |
3837 | | |
3838 | 16.5M | return Result; |
3839 | 16.5M | } |
3840 | | |
3841 | | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
3842 | | // determine if one is a proper subset of the other. |
3843 | | static ImplicitConversionSequence::CompareKind |
3844 | | compareStandardConversionSubsets(ASTContext &Context, |
3845 | | const StandardConversionSequence& SCS1, |
3846 | 16.5M | const StandardConversionSequence& SCS2) { |
3847 | 16.5M | ImplicitConversionSequence::CompareKind Result |
3848 | 16.5M | = ImplicitConversionSequence::Indistinguishable; |
3849 | | |
3850 | | // the identity conversion sequence is considered to be a subsequence of |
3851 | | // any non-identity conversion sequence |
3852 | 16.5M | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()9.07M ) |
3853 | 7.83M | return ImplicitConversionSequence::Better; |
3854 | 8.71M | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()7.48M ) |
3855 | 3.69M | return ImplicitConversionSequence::Worse; |
3856 | | |
3857 | 5.01M | if (SCS1.Second != SCS2.Second) { |
3858 | 2.41M | if (SCS1.Second == ICK_Identity) |
3859 | 3.51k | Result = ImplicitConversionSequence::Better; |
3860 | 2.41M | else if (SCS2.Second == ICK_Identity) |
3861 | 1.24k | Result = ImplicitConversionSequence::Worse; |
3862 | 2.40M | else |
3863 | 2.40M | return ImplicitConversionSequence::Indistinguishable; |
3864 | 2.60M | } else if (!Context.hasSimilarType(SCS1.getToType(1), SCS2.getToType(1))) |
3865 | 949k | return ImplicitConversionSequence::Indistinguishable; |
3866 | | |
3867 | 1.65M | if (SCS1.Third == SCS2.Third) { |
3868 | 1.31M | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
3869 | 337k | : ImplicitConversionSequence::Indistinguishable; |
3870 | 1.64M | } |
3871 | | |
3872 | 8.43k | if (SCS1.Third == ICK_Identity) |
3873 | 3.11k | return Result == ImplicitConversionSequence::Worse |
3874 | 1.24k | ? ImplicitConversionSequence::Indistinguishable |
3875 | 1.87k | : ImplicitConversionSequence::Better; |
3876 | | |
3877 | 5.32k | if (SCS2.Third == ICK_Identity) |
3878 | 5.32k | return Result == ImplicitConversionSequence::Better |
3879 | 3.47k | ? ImplicitConversionSequence::Indistinguishable |
3880 | 1.84k | : ImplicitConversionSequence::Worse; |
3881 | | |
3882 | 0 | return ImplicitConversionSequence::Indistinguishable; |
3883 | 0 | } |
3884 | | |
3885 | | /// Determine whether one of the given reference bindings is better |
3886 | | /// than the other based on what kind of bindings they are. |
3887 | | static bool |
3888 | | isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
3889 | 681k | const StandardConversionSequence &SCS2) { |
3890 | | // C++0x [over.ics.rank]p3b4: |
3891 | | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
3892 | | // implicit object parameter of a non-static member function declared |
3893 | | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
3894 | | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
3895 | | // lvalue reference to a function lvalue and S2 binds an rvalue |
3896 | | // reference*. |
3897 | | // |
3898 | | // FIXME: Rvalue references. We're going rogue with the above edits, |
3899 | | // because the semantics in the current C++0x working paper (N3225 at the |
3900 | | // time of this writing) break the standard definition of std::forward |
3901 | | // and std::reference_wrapper when dealing with references to functions. |
3902 | | // Proposed wording changes submitted to CWG for consideration. |
3903 | 681k | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
3904 | 407k | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
3905 | 286k | return false; |
3906 | | |
3907 | 395k | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue266k && |
3908 | 265k | SCS2.IsLvalueReference) || |
3909 | 150k | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue128k && |
3910 | 884 | !SCS2.IsLvalueReference && SCS2.BindsToFunctionLvalue814 ); |
3911 | 395k | } |
3912 | | |
3913 | | enum class FixedEnumPromotion { |
3914 | | None, |
3915 | | ToUnderlyingType, |
3916 | | ToPromotedUnderlyingType |
3917 | | }; |
3918 | | |
3919 | | /// Returns kind of fixed enum promotion the \a SCS uses. |
3920 | | static FixedEnumPromotion |
3921 | 5.41M | getFixedEnumPromtion(Sema &S, const StandardConversionSequence &SCS) { |
3922 | | |
3923 | 5.41M | if (SCS.Second != ICK_Integral_Promotion) |
3924 | 4.95M | return FixedEnumPromotion::None; |
3925 | | |
3926 | 455k | QualType FromType = SCS.getFromType(); |
3927 | 455k | if (!FromType->isEnumeralType()) |
3928 | 15.0k | return FixedEnumPromotion::None; |
3929 | | |
3930 | 440k | EnumDecl *Enum = FromType->getAs<EnumType>()->getDecl(); |
3931 | 440k | if (!Enum->isFixed()) |
3932 | 430k | return FixedEnumPromotion::None; |
3933 | | |
3934 | 10.5k | QualType UnderlyingType = Enum->getIntegerType(); |
3935 | 10.5k | if (S.Context.hasSameType(SCS.getToType(1), UnderlyingType)) |
3936 | 10.4k | return FixedEnumPromotion::ToUnderlyingType; |
3937 | | |
3938 | 80 | return FixedEnumPromotion::ToPromotedUnderlyingType; |
3939 | 80 | } |
3940 | | |
3941 | | /// CompareStandardConversionSequences - Compare two standard |
3942 | | /// conversion sequences to determine whether one is better than the |
3943 | | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
3944 | | static ImplicitConversionSequence::CompareKind |
3945 | | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
3946 | | const StandardConversionSequence& SCS1, |
3947 | | const StandardConversionSequence& SCS2) |
3948 | 16.5M | { |
3949 | | // Standard conversion sequence S1 is a better conversion sequence |
3950 | | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
3951 | | |
3952 | | // -- S1 is a proper subsequence of S2 (comparing the conversion |
3953 | | // sequences in the canonical form defined by 13.3.3.1.1, |
3954 | | // excluding any Lvalue Transformation; the identity conversion |
3955 | | // sequence is considered to be a subsequence of any |
3956 | | // non-identity conversion sequence) or, if not that, |
3957 | 16.5M | if (ImplicitConversionSequence::CompareKind CK |
3958 | 11.5M | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
3959 | 11.5M | return CK; |
3960 | | |
3961 | | // -- the rank of S1 is better than the rank of S2 (by the rules |
3962 | | // defined below), or, if not that, |
3963 | 5.01M | ImplicitConversionRank Rank1 = SCS1.getRank(); |
3964 | 5.01M | ImplicitConversionRank Rank2 = SCS2.getRank(); |
3965 | 5.01M | if (Rank1 < Rank2) |
3966 | 1.51M | return ImplicitConversionSequence::Better; |
3967 | 3.50M | else if (Rank2 < Rank1) |
3968 | 794k | return ImplicitConversionSequence::Worse; |
3969 | | |
3970 | | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
3971 | | // are indistinguishable unless one of the following rules |
3972 | | // applies: |
3973 | | |
3974 | | // A conversion that is not a conversion of a pointer, or |
3975 | | // pointer to member, to bool is better than another conversion |
3976 | | // that is such a conversion. |
3977 | 2.70M | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
3978 | 119 | return SCS2.isPointerConversionToBool() |
3979 | 112 | ? ImplicitConversionSequence::Better |
3980 | 7 | : ImplicitConversionSequence::Worse; |
3981 | | |
3982 | | // C++14 [over.ics.rank]p4b2: |
3983 | | // This is retroactively applied to C++11 by CWG 1601. |
3984 | | // |
3985 | | // A conversion that promotes an enumeration whose underlying type is fixed |
3986 | | // to its underlying type is better than one that promotes to the promoted |
3987 | | // underlying type, if the two are different. |
3988 | 2.70M | FixedEnumPromotion FEP1 = getFixedEnumPromtion(S, SCS1); |
3989 | 2.70M | FixedEnumPromotion FEP2 = getFixedEnumPromtion(S, SCS2); |
3990 | 2.70M | if (FEP1 != FixedEnumPromotion::None && FEP2 != FixedEnumPromotion::None5.28k && |
3991 | 5.28k | FEP1 != FEP2) |
3992 | 20 | return FEP1 == FixedEnumPromotion::ToUnderlyingType |
3993 | 20 | ? ImplicitConversionSequence::Better |
3994 | 0 | : ImplicitConversionSequence::Worse; |
3995 | | |
3996 | | // C++ [over.ics.rank]p4b2: |
3997 | | // |
3998 | | // If class B is derived directly or indirectly from class A, |
3999 | | // conversion of B* to A* is better than conversion of B* to |
4000 | | // void*, and conversion of A* to void* is better than conversion |
4001 | | // of B* to void*. |
4002 | 2.70M | bool SCS1ConvertsToVoid |
4003 | 2.70M | = SCS1.isPointerConversionToVoidPointer(S.Context); |
4004 | 2.70M | bool SCS2ConvertsToVoid |
4005 | 2.70M | = SCS2.isPointerConversionToVoidPointer(S.Context); |
4006 | 2.70M | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
4007 | | // Exactly one of the conversion sequences is a conversion to |
4008 | | // a void pointer; it's the worse conversion. |
4009 | 19 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
4010 | 3 | : ImplicitConversionSequence::Worse; |
4011 | 2.70M | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid2.70M ) { |
4012 | | // Neither conversion sequence converts to a void pointer; compare |
4013 | | // their derived-to-base conversions. |
4014 | 2.70M | if (ImplicitConversionSequence::CompareKind DerivedCK |
4015 | 519 | = CompareDerivedToBaseConversions(S, Loc, SCS1, SCS2)) |
4016 | 519 | return DerivedCK; |
4017 | 52 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
4018 | 52 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
4019 | | // Both conversion sequences are conversions to void |
4020 | | // pointers. Compare the source types to determine if there's an |
4021 | | // inheritance relationship in their sources. |
4022 | 0 | QualType FromType1 = SCS1.getFromType(); |
4023 | 0 | QualType FromType2 = SCS2.getFromType(); |
4024 | | |
4025 | | // Adjust the types we're converting from via the array-to-pointer |
4026 | | // conversion, if we need to. |
4027 | 0 | if (SCS1.First == ICK_Array_To_Pointer) |
4028 | 0 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
4029 | 0 | if (SCS2.First == ICK_Array_To_Pointer) |
4030 | 0 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
4031 | |
|
4032 | 0 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
4033 | 0 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
4034 | |
|
4035 | 0 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4036 | 0 | return ImplicitConversionSequence::Better; |
4037 | 0 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4038 | 0 | return ImplicitConversionSequence::Worse; |
4039 | | |
4040 | | // Objective-C++: If one interface is more specific than the |
4041 | | // other, it is the better one. |
4042 | 0 | const ObjCObjectPointerType* FromObjCPtr1 |
4043 | 0 | = FromType1->getAs<ObjCObjectPointerType>(); |
4044 | 0 | const ObjCObjectPointerType* FromObjCPtr2 |
4045 | 0 | = FromType2->getAs<ObjCObjectPointerType>(); |
4046 | 0 | if (FromObjCPtr1 && FromObjCPtr2) { |
4047 | 0 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
4048 | 0 | FromObjCPtr2); |
4049 | 0 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
4050 | 0 | FromObjCPtr1); |
4051 | 0 | if (AssignLeft != AssignRight) { |
4052 | 0 | return AssignLeft? ImplicitConversionSequence::Better |
4053 | 0 | : ImplicitConversionSequence::Worse; |
4054 | 0 | } |
4055 | 2.70M | } |
4056 | 0 | } |
4057 | | |
4058 | 2.70M | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding464k ) { |
4059 | | // Check for a better reference binding based on the kind of bindings. |
4060 | 460k | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
4061 | 239k | return ImplicitConversionSequence::Better; |
4062 | 220k | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
4063 | 5.48k | return ImplicitConversionSequence::Worse; |
4064 | 2.46M | } |
4065 | | |
4066 | | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
4067 | | // bullet 3). |
4068 | 2.46M | if (ImplicitConversionSequence::CompareKind QualCK |
4069 | 197 | = CompareQualificationConversions(S, SCS1, SCS2)) |
4070 | 197 | return QualCK; |
4071 | | |
4072 | 2.46M | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding219k ) { |
4073 | | // C++ [over.ics.rank]p3b4: |
4074 | | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
4075 | | // which the references refer are the same type except for |
4076 | | // top-level cv-qualifiers, and the type to which the reference |
4077 | | // initialized by S2 refers is more cv-qualified than the type |
4078 | | // to which the reference initialized by S1 refers. |
4079 | 215k | QualType T1 = SCS1.getToType(2); |
4080 | 215k | QualType T2 = SCS2.getToType(2); |
4081 | 215k | T1 = S.Context.getCanonicalType(T1); |
4082 | 215k | T2 = S.Context.getCanonicalType(T2); |
4083 | 215k | Qualifiers T1Quals, T2Quals; |
4084 | 215k | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
4085 | 215k | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
4086 | 215k | if (UnqualT1 == UnqualT2) { |
4087 | | // Objective-C++ ARC: If the references refer to objects with different |
4088 | | // lifetimes, prefer bindings that don't change lifetime. |
4089 | 215k | if (SCS1.ObjCLifetimeConversionBinding != |
4090 | 2 | SCS2.ObjCLifetimeConversionBinding) { |
4091 | 2 | return SCS1.ObjCLifetimeConversionBinding |
4092 | 0 | ? ImplicitConversionSequence::Worse |
4093 | 2 | : ImplicitConversionSequence::Better; |
4094 | 2 | } |
4095 | | |
4096 | | // If the type is an array type, promote the element qualifiers to the |
4097 | | // type for comparison. |
4098 | 215k | if (isa<ArrayType>(T1) && T1Quals69 ) |
4099 | 63 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
4100 | 215k | if (isa<ArrayType>(T2) && T2Quals69 ) |
4101 | 63 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
4102 | 215k | if (T2.isMoreQualifiedThan(T1)) |
4103 | 52.8k | return ImplicitConversionSequence::Better; |
4104 | 162k | if (T1.isMoreQualifiedThan(T2)) |
4105 | 34.1k | return ImplicitConversionSequence::Worse; |
4106 | 2.37M | } |
4107 | 215k | } |
4108 | | |
4109 | | // In Microsoft mode, prefer an integral conversion to a |
4110 | | // floating-to-integral conversion if the integral conversion |
4111 | | // is between types of the same size. |
4112 | | // For example: |
4113 | | // void f(float); |
4114 | | // void f(int); |
4115 | | // int main { |
4116 | | // long a; |
4117 | | // f(a); |
4118 | | // } |
4119 | | // Here, MSVC will call f(int) instead of generating a compile error |
4120 | | // as clang will do in standard mode. |
4121 | 2.37M | if (S.getLangOpts().MSVCCompat && SCS1.Second == ICK_Integral_Conversion16.1k && |
4122 | 7.09k | SCS2.Second == ICK_Floating_Integral && |
4123 | 2.49k | S.Context.getTypeSize(SCS1.getFromType()) == |
4124 | 2.49k | S.Context.getTypeSize(SCS1.getToType(2))) |
4125 | 540 | return ImplicitConversionSequence::Better; |
4126 | | |
4127 | | // Prefer a compatible vector conversion over a lax vector conversion |
4128 | | // For example: |
4129 | | // |
4130 | | // typedef float __v4sf __attribute__((__vector_size__(16))); |
4131 | | // void f(vector float); |
4132 | | // void f(vector signed int); |
4133 | | // int main() { |
4134 | | // __v4sf a; |
4135 | | // f(a); |
4136 | | // } |
4137 | | // Here, we'd like to choose f(vector float) and not |
4138 | | // report an ambiguous call error |
4139 | 2.37M | if (SCS1.Second == ICK_Vector_Conversion && |
4140 | 529k | SCS2.Second == ICK_Vector_Conversion) { |
4141 | 529k | bool SCS1IsCompatibleVectorConversion = S.Context.areCompatibleVectorTypes( |
4142 | 529k | SCS1.getFromType(), SCS1.getToType(2)); |
4143 | 529k | bool SCS2IsCompatibleVectorConversion = S.Context.areCompatibleVectorTypes( |
4144 | 529k | SCS2.getFromType(), SCS2.getToType(2)); |
4145 | | |
4146 | 529k | if (SCS1IsCompatibleVectorConversion != SCS2IsCompatibleVectorConversion) |
4147 | 60.8k | return SCS1IsCompatibleVectorConversion |
4148 | 42.9k | ? ImplicitConversionSequence::Better |
4149 | 17.9k | : ImplicitConversionSequence::Worse; |
4150 | 2.31M | } |
4151 | | |
4152 | 2.31M | if (SCS1.Second == ICK_SVE_Vector_Conversion && |
4153 | 178 | SCS2.Second == ICK_SVE_Vector_Conversion) { |
4154 | 178 | bool SCS1IsCompatibleSVEVectorConversion = |
4155 | 178 | S.Context.areCompatibleSveTypes(SCS1.getFromType(), SCS1.getToType(2)); |
4156 | 178 | bool SCS2IsCompatibleSVEVectorConversion = |
4157 | 178 | S.Context.areCompatibleSveTypes(SCS2.getFromType(), SCS2.getToType(2)); |
4158 | | |
4159 | 178 | if (SCS1IsCompatibleSVEVectorConversion != |
4160 | 178 | SCS2IsCompatibleSVEVectorConversion) |
4161 | 148 | return SCS1IsCompatibleSVEVectorConversion |
4162 | 128 | ? ImplicitConversionSequence::Better |
4163 | 20 | : ImplicitConversionSequence::Worse; |
4164 | 2.31M | } |
4165 | | |
4166 | 2.31M | return ImplicitConversionSequence::Indistinguishable; |
4167 | 2.31M | } |
4168 | | |
4169 | | /// CompareQualificationConversions - Compares two standard conversion |
4170 | | /// sequences to determine whether they can be ranked based on their |
4171 | | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
4172 | | static ImplicitConversionSequence::CompareKind |
4173 | | CompareQualificationConversions(Sema &S, |
4174 | | const StandardConversionSequence& SCS1, |
4175 | 2.46M | const StandardConversionSequence& SCS2) { |
4176 | | // C++ 13.3.3.2p3: |
4177 | | // -- S1 and S2 differ only in their qualification conversion and |
4178 | | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
4179 | | // cv-qualification signature of type T1 is a proper subset of |
4180 | | // the cv-qualification signature of type T2, and S1 is not the |
4181 | | // deprecated string literal array-to-pointer conversion (4.2). |
4182 | 2.46M | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second2.45M || |
4183 | 2.34M | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
4184 | 2.46M | return ImplicitConversionSequence::Indistinguishable; |
4185 | | |
4186 | | // FIXME: the example in the standard doesn't use a qualification |
4187 | | // conversion (!) |
4188 | 1.54k | QualType T1 = SCS1.getToType(2); |
4189 | 1.54k | QualType T2 = SCS2.getToType(2); |
4190 | 1.54k | T1 = S.Context.getCanonicalType(T1); |
4191 | 1.54k | T2 = S.Context.getCanonicalType(T2); |
4192 | 1.54k | assert(!T1->isReferenceType() && !T2->isReferenceType()); |
4193 | 1.54k | Qualifiers T1Quals, T2Quals; |
4194 | 1.54k | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
4195 | 1.54k | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
4196 | | |
4197 | | // If the types are the same, we won't learn anything by unwrapping |
4198 | | // them. |
4199 | 1.54k | if (UnqualT1 == UnqualT2) |
4200 | 1.31k | return ImplicitConversionSequence::Indistinguishable; |
4201 | | |
4202 | 225 | ImplicitConversionSequence::CompareKind Result |
4203 | 225 | = ImplicitConversionSequence::Indistinguishable; |
4204 | | |
4205 | | // Objective-C++ ARC: |
4206 | | // Prefer qualification conversions not involving a change in lifetime |
4207 | | // to qualification conversions that do not change lifetime. |
4208 | 225 | if (SCS1.QualificationIncludesObjCLifetime != |
4209 | 4 | SCS2.QualificationIncludesObjCLifetime) { |
4210 | 4 | Result = SCS1.QualificationIncludesObjCLifetime |
4211 | 1 | ? ImplicitConversionSequence::Worse |
4212 | 3 | : ImplicitConversionSequence::Better; |
4213 | 4 | } |
4214 | | |
4215 | 257 | while (S.Context.UnwrapSimilarTypes(T1, T2)) { |
4216 | | // Within each iteration of the loop, we check the qualifiers to |
4217 | | // determine if this still looks like a qualification |
4218 | | // conversion. Then, if all is well, we unwrap one more level of |
4219 | | // pointers or pointers-to-members and do it all again |
4220 | | // until there are no more pointers or pointers-to-members left |
4221 | | // to unwrap. This essentially mimics what |
4222 | | // IsQualificationConversion does, but here we're checking for a |
4223 | | // strict subset of qualifiers. |
4224 | 257 | if (T1.getQualifiers().withoutObjCLifetime() == |
4225 | 257 | T2.getQualifiers().withoutObjCLifetime()) |
4226 | | // The qualifiers are the same, so this doesn't tell us anything |
4227 | | // about how the sequences rank. |
4228 | | // ObjC ownership quals are omitted above as they interfere with |
4229 | | // the ARC overload rule. |
4230 | 28 | ; |
4231 | 229 | else if (T2.isMoreQualifiedThan(T1)) { |
4232 | | // T1 has fewer qualifiers, so it could be the better sequence. |
4233 | 112 | if (Result == ImplicitConversionSequence::Worse) |
4234 | | // Neither has qualifiers that are a subset of the other's |
4235 | | // qualifiers. |
4236 | 6 | return ImplicitConversionSequence::Indistinguishable; |
4237 | | |
4238 | 106 | Result = ImplicitConversionSequence::Better; |
4239 | 117 | } else if (T1.isMoreQualifiedThan(T2)) { |
4240 | | // T2 has fewer qualifiers, so it could be the better sequence. |
4241 | 105 | if (Result == ImplicitConversionSequence::Better) |
4242 | | // Neither has qualifiers that are a subset of the other's |
4243 | | // qualifiers. |
4244 | 6 | return ImplicitConversionSequence::Indistinguishable; |
4245 | | |
4246 | 99 | Result = ImplicitConversionSequence::Worse; |
4247 | 12 | } else { |
4248 | | // Qualifiers are disjoint. |
4249 | 12 | return ImplicitConversionSequence::Indistinguishable; |
4250 | 12 | } |
4251 | | |
4252 | | // If the types after this point are equivalent, we're done. |
4253 | 233 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
4254 | 201 | break; |
4255 | 233 | } |
4256 | | |
4257 | | // Check that the winning standard conversion sequence isn't using |
4258 | | // the deprecated string literal array to pointer conversion. |
4259 | 201 | switch (Result) { |
4260 | 103 | case ImplicitConversionSequence::Better: |
4261 | 103 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
4262 | 0 | Result = ImplicitConversionSequence::Indistinguishable; |
4263 | 103 | break; |
4264 | | |
4265 | 4 | case ImplicitConversionSequence::Indistinguishable: |
4266 | 4 | break; |
4267 | | |
4268 | 94 | case ImplicitConversionSequence::Worse: |
4269 | 94 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
4270 | 0 | Result = ImplicitConversionSequence::Indistinguishable; |
4271 | 94 | break; |
4272 | 201 | } |
4273 | | |
4274 | 201 | return Result; |
4275 | 201 | } |
4276 | | |
4277 | | /// CompareDerivedToBaseConversions - Compares two standard conversion |
4278 | | /// sequences to determine whether they can be ranked based on their |
4279 | | /// various kinds of derived-to-base conversions (C++ |
4280 | | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
4281 | | /// conversions between Objective-C interface types. |
4282 | | static ImplicitConversionSequence::CompareKind |
4283 | | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
4284 | | const StandardConversionSequence& SCS1, |
4285 | 2.70M | const StandardConversionSequence& SCS2) { |
4286 | 2.70M | QualType FromType1 = SCS1.getFromType(); |
4287 | 2.70M | QualType ToType1 = SCS1.getToType(1); |
4288 | 2.70M | QualType FromType2 = SCS2.getFromType(); |
4289 | 2.70M | QualType ToType2 = SCS2.getToType(1); |
4290 | | |
4291 | | // Adjust the types we're converting from via the array-to-pointer |
4292 | | // conversion, if we need to. |
4293 | 2.70M | if (SCS1.First == ICK_Array_To_Pointer) |
4294 | 2.71k | FromType1 = S.Context.getArrayDecayedType(FromType1); |
4295 | 2.70M | if (SCS2.First == ICK_Array_To_Pointer) |
4296 | 2.73k | FromType2 = S.Context.getArrayDecayedType(FromType2); |
4297 | | |
4298 | | // Canonicalize all of the types. |
4299 | 2.70M | FromType1 = S.Context.getCanonicalType(FromType1); |
4300 | 2.70M | ToType1 = S.Context.getCanonicalType(ToType1); |
4301 | 2.70M | FromType2 = S.Context.getCanonicalType(FromType2); |
4302 | 2.70M | ToType2 = S.Context.getCanonicalType(ToType2); |
4303 | | |
4304 | | // C++ [over.ics.rank]p4b3: |
4305 | | // |
4306 | | // If class B is derived directly or indirectly from class A and |
4307 | | // class C is derived directly or indirectly from B, |
4308 | | // |
4309 | | // Compare based on pointer conversions. |
4310 | 2.70M | if (SCS1.Second == ICK_Pointer_Conversion && |
4311 | 1.71k | SCS2.Second == ICK_Pointer_Conversion && |
4312 | | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
4313 | 1.70k | FromType1->isPointerType() && FromType2->isPointerType()644 && |
4314 | 644 | ToType1->isPointerType() && ToType2->isPointerType()) { |
4315 | 644 | QualType FromPointee1 = |
4316 | 644 | FromType1->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4317 | 644 | QualType ToPointee1 = |
4318 | 644 | ToType1->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4319 | 644 | QualType FromPointee2 = |
4320 | 644 | FromType2->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4321 | 644 | QualType ToPointee2 = |
4322 | 644 | ToType2->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4323 | | |
4324 | | // -- conversion of C* to B* is better than conversion of C* to A*, |
4325 | 644 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
4326 | 476 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
4327 | 10 | return ImplicitConversionSequence::Better; |
4328 | 466 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
4329 | 10 | return ImplicitConversionSequence::Worse; |
4330 | 624 | } |
4331 | | |
4332 | | // -- conversion of B* to A* is better than conversion of C* to A*, |
4333 | 624 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee20 ) { |
4334 | 0 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4335 | 0 | return ImplicitConversionSequence::Better; |
4336 | 0 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4337 | 0 | return ImplicitConversionSequence::Worse; |
4338 | 2.70M | } |
4339 | 2.70M | } else if (SCS1.Second == ICK_Pointer_Conversion && |
4340 | 1.06k | SCS2.Second == ICK_Pointer_Conversion) { |
4341 | 1.06k | const ObjCObjectPointerType *FromPtr1 |
4342 | 1.06k | = FromType1->getAs<ObjCObjectPointerType>(); |
4343 | 1.06k | const ObjCObjectPointerType *FromPtr2 |
4344 | 1.06k | = FromType2->getAs<ObjCObjectPointerType>(); |
4345 | 1.06k | const ObjCObjectPointerType *ToPtr1 |
4346 | 1.06k | = ToType1->getAs<ObjCObjectPointerType>(); |
4347 | 1.06k | const ObjCObjectPointerType *ToPtr2 |
4348 | 1.06k | = ToType2->getAs<ObjCObjectPointerType>(); |
4349 | | |
4350 | 1.06k | if (FromPtr1 && FromPtr234 && ToPtr134 && ToPtr234 ) { |
4351 | | // Apply the same conversion ranking rules for Objective-C pointer types |
4352 | | // that we do for C++ pointers to class types. However, we employ the |
4353 | | // Objective-C pseudo-subtyping relationship used for assignment of |
4354 | | // Objective-C pointer types. |
4355 | 34 | bool FromAssignLeft |
4356 | 34 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
4357 | 34 | bool FromAssignRight |
4358 | 34 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
4359 | 34 | bool ToAssignLeft |
4360 | 34 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
4361 | 34 | bool ToAssignRight |
4362 | 34 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
4363 | | |
4364 | | // A conversion to an a non-id object pointer type or qualified 'id' |
4365 | | // type is better than a conversion to 'id'. |
4366 | 34 | if (ToPtr1->isObjCIdType() && |
4367 | 5 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
4368 | 1 | return ImplicitConversionSequence::Worse; |
4369 | 33 | if (ToPtr2->isObjCIdType() && |
4370 | 9 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
4371 | 5 | return ImplicitConversionSequence::Better; |
4372 | | |
4373 | | // A conversion to a non-id object pointer type is better than a |
4374 | | // conversion to a qualified 'id' type |
4375 | 28 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()4 ) |
4376 | 0 | return ImplicitConversionSequence::Worse; |
4377 | 28 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()6 ) |
4378 | 2 | return ImplicitConversionSequence::Better; |
4379 | | |
4380 | | // A conversion to an a non-Class object pointer type or qualified 'Class' |
4381 | | // type is better than a conversion to 'Class'. |
4382 | 26 | if (ToPtr1->isObjCClassType() && |
4383 | 0 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
4384 | 0 | return ImplicitConversionSequence::Worse; |
4385 | 26 | if (ToPtr2->isObjCClassType() && |
4386 | 0 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
4387 | 0 | return ImplicitConversionSequence::Better; |
4388 | | |
4389 | | // A conversion to a non-Class object pointer type is better than a |
4390 | | // conversion to a qualified 'Class' type. |
4391 | 26 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()0 ) |
4392 | 0 | return ImplicitConversionSequence::Worse; |
4393 | 26 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()0 ) |
4394 | 0 | return ImplicitConversionSequence::Better; |
4395 | | |
4396 | | // -- "conversion of C* to B* is better than conversion of C* to A*," |
4397 | 26 | if (S.Context.hasSameType(FromType1, FromType2) && |
4398 | 14 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType()10 && |
4399 | 10 | (ToAssignLeft != ToAssignRight)) { |
4400 | 6 | if (FromPtr1->isSpecialized()) { |
4401 | | // "conversion of B<A> * to B * is better than conversion of B * to |
4402 | | // C *. |
4403 | 4 | bool IsFirstSame = |
4404 | 4 | FromPtr1->getInterfaceDecl() == ToPtr1->getInterfaceDecl(); |
4405 | 4 | bool IsSecondSame = |
4406 | 4 | FromPtr1->getInterfaceDecl() == ToPtr2->getInterfaceDecl(); |
4407 | 4 | if (IsFirstSame) { |
4408 | 3 | if (!IsSecondSame) |
4409 | 3 | return ImplicitConversionSequence::Better; |
4410 | 1 | } else if (IsSecondSame) |
4411 | 1 | return ImplicitConversionSequence::Worse; |
4412 | 2 | } |
4413 | 2 | return ToAssignLeft? ImplicitConversionSequence::Worse1 |
4414 | 1 | : ImplicitConversionSequence::Better; |
4415 | 2 | } |
4416 | | |
4417 | | // -- "conversion of B* to A* is better than conversion of C* to A*," |
4418 | 20 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
4419 | 12 | (FromAssignLeft != FromAssignRight)) |
4420 | 12 | return FromAssignLeft? ImplicitConversionSequence::Better |
4421 | 0 | : ImplicitConversionSequence::Worse; |
4422 | 2.70M | } |
4423 | 1.06k | } |
4424 | | |
4425 | | // Ranking of member-pointer types. |
4426 | 2.70M | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member599 && |
4427 | 597 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType()24 && |
4428 | 24 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
4429 | 24 | const auto *FromMemPointer1 = FromType1->castAs<MemberPointerType>(); |
4430 | 24 | const auto *ToMemPointer1 = ToType1->castAs<MemberPointerType>(); |
4431 | 24 | const auto *FromMemPointer2 = FromType2->castAs<MemberPointerType>(); |
4432 | 24 | const auto *ToMemPointer2 = ToType2->castAs<MemberPointerType>(); |
4433 | 24 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
4434 | 24 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
4435 | 24 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
4436 | 24 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
4437 | 24 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
4438 | 24 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
4439 | 24 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
4440 | 24 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
4441 | | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
4442 | 24 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee28 ) { |
4443 | 8 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
4444 | 2 | return ImplicitConversionSequence::Worse; |
4445 | 6 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
4446 | 6 | return ImplicitConversionSequence::Better; |
4447 | 16 | } |
4448 | | // conversion of B::* to C::* is better than conversion of A::* to C::* |
4449 | 16 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
4450 | 16 | if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4451 | 16 | return ImplicitConversionSequence::Better; |
4452 | 0 | else if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4453 | 0 | return ImplicitConversionSequence::Worse; |
4454 | 2.70M | } |
4455 | 16 | } |
4456 | | |
4457 | 2.70M | if (SCS1.Second == ICK_Derived_To_Base) { |
4458 | | // -- conversion of C to B is better than conversion of C to A, |
4459 | | // -- binding of an expression of type C to a reference of type |
4460 | | // B& is better than binding an expression of type C to a |
4461 | | // reference of type A&, |
4462 | 7.97k | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
4463 | 7.94k | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
4464 | 533 | if (S.IsDerivedFrom(Loc, ToType1, ToType2)) |
4465 | 433 | return ImplicitConversionSequence::Better; |
4466 | 100 | else if (S.IsDerivedFrom(Loc, ToType2, ToType1)) |
4467 | 12 | return ImplicitConversionSequence::Worse; |
4468 | 7.52k | } |
4469 | | |
4470 | | // -- conversion of B to A is better than conversion of C to A. |
4471 | | // -- binding of an expression of type B to a reference of type |
4472 | | // A& is better than binding an expression of type C to a |
4473 | | // reference of type A&, |
4474 | 7.52k | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
4475 | 28 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
4476 | 28 | if (S.IsDerivedFrom(Loc, FromType2, FromType1)) |
4477 | 2 | return ImplicitConversionSequence::Better; |
4478 | 26 | else if (S.IsDerivedFrom(Loc, FromType1, FromType2)) |
4479 | 2 | return ImplicitConversionSequence::Worse; |
4480 | 2.70M | } |
4481 | 7.52k | } |
4482 | | |
4483 | 2.70M | return ImplicitConversionSequence::Indistinguishable; |
4484 | 2.70M | } |
4485 | | |
4486 | | /// Determine whether the given type is valid, e.g., it is not an invalid |
4487 | | /// C++ class. |
4488 | 8.65M | static bool isTypeValid(QualType T) { |
4489 | 8.65M | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
4490 | 3.59M | return !Record->isInvalidDecl(); |
4491 | | |
4492 | 5.05M | return true; |
4493 | 5.05M | } |
4494 | | |
4495 | 9.57M | static QualType withoutUnaligned(ASTContext &Ctx, QualType T) { |
4496 | 9.57M | if (!T.getQualifiers().hasUnaligned()) |
4497 | 9.57M | return T; |
4498 | | |
4499 | 15 | Qualifiers Q; |
4500 | 15 | T = Ctx.getUnqualifiedArrayType(T, Q); |
4501 | 15 | Q.removeUnaligned(); |
4502 | 15 | return Ctx.getQualifiedType(T, Q); |
4503 | 15 | } |
4504 | | |
4505 | | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
4506 | | /// determine whether they are reference-compatible, |
4507 | | /// reference-related, or incompatible, for use in C++ initialization by |
4508 | | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
4509 | | /// type, and the first type (T1) is the pointee type of the reference |
4510 | | /// type being initialized. |
4511 | | Sema::ReferenceCompareResult |
4512 | | Sema::CompareReferenceRelationship(SourceLocation Loc, |
4513 | | QualType OrigT1, QualType OrigT2, |
4514 | 5.47M | ReferenceConversions *ConvOut) { |
4515 | 5.47M | assert(!OrigT1->isReferenceType() && |
4516 | 5.47M | "T1 must be the pointee type of the reference type"); |
4517 | 5.47M | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
4518 | | |
4519 | 5.47M | QualType T1 = Context.getCanonicalType(OrigT1); |
4520 | 5.47M | QualType T2 = Context.getCanonicalType(OrigT2); |
4521 | 5.47M | Qualifiers T1Quals, T2Quals; |
4522 | 5.47M | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
4523 | 5.47M | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
4524 | | |
4525 | 5.47M | ReferenceConversions ConvTmp; |
4526 | 5.46M | ReferenceConversions &Conv = ConvOut ? *ConvOut : ConvTmp11.9k ; |
4527 | 5.47M | Conv = ReferenceConversions(); |
4528 | | |
4529 | | // C++2a [dcl.init.ref]p4: |
4530 | | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
4531 | | // reference-related to "cv2 T2" if T1 is similar to T2, or |
4532 | | // T1 is a base class of T2. |
4533 | | // "cv1 T1" is reference-compatible with "cv2 T2" if |
4534 | | // a prvalue of type "pointer to cv2 T2" can be converted to the type |
4535 | | // "pointer to cv1 T1" via a standard conversion sequence. |
4536 | | |
4537 | | // Check for standard conversions we can apply to pointers: derived-to-base |
4538 | | // conversions, ObjC pointer conversions, and function pointer conversions. |
4539 | | // (Qualification conversions are checked last.) |
4540 | 5.47M | QualType ConvertedT2; |
4541 | 5.47M | if (UnqualT1 == UnqualT2) { |
4542 | | // Nothing to do. |
4543 | 4.33M | } else if (isCompleteType(Loc, OrigT2) && |
4544 | 4.32M | isTypeValid(UnqualT1) && isTypeValid(UnqualT2)4.32M && |
4545 | 4.32M | IsDerivedFrom(Loc, UnqualT2, UnqualT1)) |
4546 | 14.6k | Conv |= ReferenceConversions::DerivedToBase; |
4547 | 4.32M | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
4548 | 47 | UnqualT2->isObjCObjectOrInterfaceType() && |
4549 | 32 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
4550 | 32 | Conv |= ReferenceConversions::ObjC; |
4551 | 4.32M | else if (UnqualT2->isFunctionType() && |
4552 | 2.45k | IsFunctionConversion(UnqualT2, UnqualT1, ConvertedT2)) { |
4553 | 36 | Conv |= ReferenceConversions::Function; |
4554 | | // No need to check qualifiers; function types don't have them. |
4555 | 36 | return Ref_Compatible; |
4556 | 36 | } |
4557 | 5.47M | bool ConvertedReferent = Conv != 0; |
4558 | | |
4559 | | // We can have a qualification conversion. Compute whether the types are |
4560 | | // similar at the same time. |
4561 | 5.47M | bool PreviousToQualsIncludeConst = true; |
4562 | 5.47M | bool TopLevel = true; |
4563 | 5.47M | do { |
4564 | 5.47M | if (T1 == T2) |
4565 | 692k | break; |
4566 | | |
4567 | | // We will need a qualification conversion. |
4568 | 4.78M | Conv |= ReferenceConversions::Qualification; |
4569 | | |
4570 | | // Track whether we performed a qualification conversion anywhere other |
4571 | | // than the top level. This matters for ranking reference bindings in |
4572 | | // overload resolution. |
4573 | 4.78M | if (!TopLevel) |
4574 | 949 | Conv |= ReferenceConversions::NestedQualification; |
4575 | | |
4576 | | // MS compiler ignores __unaligned qualifier for references; do the same. |
4577 | 4.78M | T1 = withoutUnaligned(Context, T1); |
4578 | 4.78M | T2 = withoutUnaligned(Context, T2); |
4579 | | |
4580 | | // If we find a qualifier mismatch, the types are not reference-compatible, |
4581 | | // but are still be reference-related if they're similar. |
4582 | 4.78M | bool ObjCLifetimeConversion = false; |
4583 | 4.78M | if (!isQualificationConversionStep(T2, T1, /*CStyle=*/false, TopLevel, |
4584 | 4.78M | PreviousToQualsIncludeConst, |
4585 | 4.78M | ObjCLifetimeConversion)) |
4586 | 108k | return (ConvertedReferent || Context.hasSimilarType(T1, T2)108k ) |
4587 | 56.1k | ? Ref_Related |
4588 | 52.6k | : Ref_Incompatible; |
4589 | | |
4590 | | // FIXME: Should we track this for any level other than the first? |
4591 | 4.67M | if (ObjCLifetimeConversion) |
4592 | 29 | Conv |= ReferenceConversions::ObjCLifetime; |
4593 | | |
4594 | 4.67M | TopLevel = false; |
4595 | 4.67M | } while (Context.UnwrapSimilarTypes(T1, T2)); |
4596 | | |
4597 | | // At this point, if the types are reference-related, we must either have the |
4598 | | // same inner type (ignoring qualifiers), or must have already worked out how |
4599 | | // to convert the referent. |
4600 | 5.36M | return (ConvertedReferent || Context.hasSameUnqualifiedType(T1, T2)5.35M ) |
4601 | 1.09M | ? Ref_Compatible |
4602 | 4.26M | : Ref_Incompatible; |
4603 | 5.47M | } |
4604 | | |
4605 | | /// Look for a user-defined conversion to a value reference-compatible |
4606 | | /// with DeclType. Return true if something definite is found. |
4607 | | static bool |
4608 | | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
4609 | | QualType DeclType, SourceLocation DeclLoc, |
4610 | | Expr *Init, QualType T2, bool AllowRvalues, |
4611 | 1.82M | bool AllowExplicit) { |
4612 | 1.82M | assert(T2->isRecordType() && "Can only find conversions of record types."); |
4613 | 1.82M | auto *T2RecordDecl = cast<CXXRecordDecl>(T2->castAs<RecordType>()->getDecl()); |
4614 | | |
4615 | 1.82M | OverloadCandidateSet CandidateSet( |
4616 | 1.82M | DeclLoc, OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
4617 | 1.82M | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
4618 | 2.02M | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I198k ) { |
4619 | 198k | NamedDecl *D = *I; |
4620 | 198k | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
4621 | 198k | if (isa<UsingShadowDecl>(D)) |
4622 | 0 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
4623 | | |
4624 | 198k | FunctionTemplateDecl *ConvTemplate |
4625 | 198k | = dyn_cast<FunctionTemplateDecl>(D); |
4626 | 198k | CXXConversionDecl *Conv; |
4627 | 198k | if (ConvTemplate) |
4628 | 50 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
4629 | 198k | else |
4630 | 198k | Conv = cast<CXXConversionDecl>(D); |
4631 | | |
4632 | 198k | if (AllowRvalues) { |
4633 | | // If we are initializing an rvalue reference, don't permit conversion |
4634 | | // functions that return lvalues. |
4635 | 8.43k | if (!ConvTemplate && DeclType->isRValueReferenceType()8.39k ) { |
4636 | 1.66k | const ReferenceType *RefType |
4637 | 1.66k | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
4638 | 1.66k | if (RefType && !RefType->getPointeeType()->isFunctionType()92 ) |
4639 | 85 | continue; |
4640 | 8.34k | } |
4641 | | |
4642 | 8.34k | if (!ConvTemplate && |
4643 | 8.31k | S.CompareReferenceRelationship( |
4644 | 8.31k | DeclLoc, |
4645 | 8.31k | Conv->getConversionType() |
4646 | 8.31k | .getNonReferenceType() |
4647 | 8.31k | .getUnqualifiedType(), |
4648 | 8.31k | DeclType.getNonReferenceType().getUnqualifiedType()) == |
4649 | 8.31k | Sema::Ref_Incompatible) |
4650 | 6.91k | continue; |
4651 | 190k | } else { |
4652 | | // If the conversion function doesn't return a reference type, |
4653 | | // it can't be considered for this conversion. An rvalue reference |
4654 | | // is only acceptable if its referencee is a function type. |
4655 | | |
4656 | 190k | const ReferenceType *RefType = |
4657 | 190k | Conv->getConversionType()->getAs<ReferenceType>(); |
4658 | 190k | if (!RefType || |
4659 | 42.7k | (!RefType->isLValueReferenceType() && |
4660 | 2 | !RefType->getPointeeType()->isFunctionType())) |
4661 | 147k | continue; |
4662 | 44.2k | } |
4663 | | |
4664 | 44.2k | if (ConvTemplate) |
4665 | 37 | S.AddTemplateConversionCandidate( |
4666 | 37 | ConvTemplate, I.getPair(), ActingDC, Init, DeclType, CandidateSet, |
4667 | 37 | /*AllowObjCConversionOnExplicit=*/false, AllowExplicit); |
4668 | 44.1k | else |
4669 | 44.1k | S.AddConversionCandidate( |
4670 | 44.1k | Conv, I.getPair(), ActingDC, Init, DeclType, CandidateSet, |
4671 | 44.1k | /*AllowObjCConversionOnExplicit=*/false, AllowExplicit); |
4672 | 44.2k | } |
4673 | | |
4674 | 1.82M | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
4675 | | |
4676 | 1.82M | OverloadCandidateSet::iterator Best; |
4677 | 1.82M | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
4678 | 2.11k | case OR_Success: |
4679 | | // C++ [over.ics.ref]p1: |
4680 | | // |
4681 | | // [...] If the parameter binds directly to the result of |
4682 | | // applying a conversion function to the argument |
4683 | | // expression, the implicit conversion sequence is a |
4684 | | // user-defined conversion sequence (13.3.3.1.2), with the |
4685 | | // second standard conversion sequence either an identity |
4686 | | // conversion or, if the conversion function returns an |
4687 | | // entity of a type that is a derived class of the parameter |
4688 | | // type, a derived-to-base Conversion. |
4689 | 2.11k | if (!Best->FinalConversion.DirectBinding) |
4690 | 43 | return false; |
4691 | | |
4692 | 2.06k | ICS.setUserDefined(); |
4693 | 2.06k | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
4694 | 2.06k | ICS.UserDefined.After = Best->FinalConversion; |
4695 | 2.06k | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
4696 | 2.06k | ICS.UserDefined.ConversionFunction = Best->Function; |
4697 | 2.06k | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
4698 | 2.06k | ICS.UserDefined.EllipsisConversion = false; |
4699 | 2.06k | assert(ICS.UserDefined.After.ReferenceBinding && |
4700 | 2.06k | ICS.UserDefined.After.DirectBinding && |
4701 | 2.06k | "Expected a direct reference binding!"); |
4702 | 2.06k | return true; |
4703 | | |
4704 | 1 | case OR_Ambiguous: |
4705 | 1 | ICS.setAmbiguous(); |
4706 | 1 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
4707 | 3 | Cand != CandidateSet.end(); ++Cand2 ) |
4708 | 2 | if (Cand->Best) |
4709 | 2 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
4710 | 1 | return true; |
4711 | | |
4712 | 1.82M | case OR_No_Viable_Function: |
4713 | 1.82M | case OR_Deleted: |
4714 | | // There was no suitable conversion, or we found a deleted |
4715 | | // conversion; continue with other checks. |
4716 | 1.82M | return false; |
4717 | 0 | } |
4718 | | |
4719 | 0 | llvm_unreachable("Invalid OverloadResult!"); |
4720 | 0 | } |
4721 | | |
4722 | | /// Compute an implicit conversion sequence for reference |
4723 | | /// initialization. |
4724 | | static ImplicitConversionSequence |
4725 | | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
4726 | | SourceLocation DeclLoc, |
4727 | | bool SuppressUserConversions, |
4728 | 4.95M | bool AllowExplicit) { |
4729 | 4.95M | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
4730 | | |
4731 | | // Most paths end in a failed conversion. |
4732 | 4.95M | ImplicitConversionSequence ICS; |
4733 | 4.95M | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4734 | | |
4735 | 4.95M | QualType T1 = DeclType->castAs<ReferenceType>()->getPointeeType(); |
4736 | 4.95M | QualType T2 = Init->getType(); |
4737 | | |
4738 | | // If the initializer is the address of an overloaded function, try |
4739 | | // to resolve the overloaded function. If all goes well, T2 is the |
4740 | | // type of the resulting function. |
4741 | 4.95M | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
4742 | 353 | DeclAccessPair Found; |
4743 | 353 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
4744 | 87 | false, Found)) |
4745 | 87 | T2 = Fn->getType(); |
4746 | 353 | } |
4747 | | |
4748 | | // Compute some basic properties of the types and the initializer. |
4749 | 4.95M | bool isRValRef = DeclType->isRValueReferenceType(); |
4750 | 4.95M | Expr::Classification InitCategory = Init->Classify(S.Context); |
4751 | | |
4752 | 4.95M | Sema::ReferenceConversions RefConv; |
4753 | 4.95M | Sema::ReferenceCompareResult RefRelationship = |
4754 | 4.95M | S.CompareReferenceRelationship(DeclLoc, T1, T2, &RefConv); |
4755 | | |
4756 | 590k | auto SetAsReferenceBinding = [&](bool BindsDirectly) { |
4757 | 590k | ICS.setStandard(); |
4758 | 590k | ICS.Standard.First = ICK_Identity; |
4759 | | // FIXME: A reference binding can be a function conversion too. We should |
4760 | | // consider that when ordering reference-to-function bindings. |
4761 | 590k | ICS.Standard.Second = (RefConv & Sema::ReferenceConversions::DerivedToBase) |
4762 | 3.39k | ? ICK_Derived_To_Base |
4763 | 586k | : (RefConv & Sema::ReferenceConversions::ObjC) |
4764 | 17 | ? ICK_Compatible_Conversion |
4765 | 586k | : ICK_Identity; |
4766 | | // FIXME: As a speculative fix to a defect introduced by CWG2352, we rank |
4767 | | // a reference binding that performs a non-top-level qualification |
4768 | | // conversion as a qualification conversion, not as an identity conversion. |
4769 | 590k | ICS.Standard.Third = (RefConv & |
4770 | 590k | Sema::ReferenceConversions::NestedQualification) |
4771 | 17 | ? ICK_Qualification |
4772 | 589k | : ICK_Identity; |
4773 | 590k | ICS.Standard.setFromType(T2); |
4774 | 590k | ICS.Standard.setToType(0, T2); |
4775 | 590k | ICS.Standard.setToType(1, T1); |
4776 | 590k | ICS.Standard.setToType(2, T1); |
4777 | 590k | ICS.Standard.ReferenceBinding = true; |
4778 | 590k | ICS.Standard.DirectBinding = BindsDirectly; |
4779 | 590k | ICS.Standard.IsLvalueReference = !isRValRef; |
4780 | 590k | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
4781 | 590k | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
4782 | 590k | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4783 | 590k | ICS.Standard.ObjCLifetimeConversionBinding = |
4784 | 590k | (RefConv & Sema::ReferenceConversions::ObjCLifetime) != 0; |
4785 | 590k | ICS.Standard.CopyConstructor = nullptr; |
4786 | 590k | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
4787 | 590k | }; |
4788 | | |
4789 | | // C++0x [dcl.init.ref]p5: |
4790 | | // A reference to type "cv1 T1" is initialized by an expression |
4791 | | // of type "cv2 T2" as follows: |
4792 | | |
4793 | | // -- If reference is an lvalue reference and the initializer expression |
4794 | 4.95M | if (!isRValRef) { |
4795 | | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
4796 | | // reference-compatible with "cv2 T2," or |
4797 | | // |
4798 | | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
4799 | 4.57M | if (InitCategory.isLValue() && RefRelationship == Sema::Ref_Compatible4.21M ) { |
4800 | | // C++ [over.ics.ref]p1: |
4801 | | // When a parameter of reference type binds directly (8.5.3) |
4802 | | // to an argument expression, the implicit conversion sequence |
4803 | | // is the identity conversion, unless the argument expression |
4804 | | // has a type that is a derived class of the parameter type, |
4805 | | // in which case the implicit conversion sequence is a |
4806 | | // derived-to-base Conversion (13.3.3.1). |
4807 | 285k | SetAsReferenceBinding(/*BindsDirectly=*/true); |
4808 | | |
4809 | | // Nothing more to do: the inaccessibility/ambiguity check for |
4810 | | // derived-to-base conversions is suppressed when we're |
4811 | | // computing the implicit conversion sequence (C++ |
4812 | | // [over.best.ics]p2). |
4813 | 285k | return ICS; |
4814 | 285k | } |
4815 | | |
4816 | | // -- has a class type (i.e., T2 is a class type), where T1 is |
4817 | | // not reference-related to T2, and can be implicitly |
4818 | | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
4819 | | // is reference-compatible with "cv3 T3" 92) (this |
4820 | | // conversion is selected by enumerating the applicable |
4821 | | // conversion functions (13.3.1.6) and choosing the best |
4822 | | // one through overload resolution (13.3)), |
4823 | 4.28M | if (!SuppressUserConversions && T2->isRecordType()2.00M && |
4824 | 1.90M | S.isCompleteType(DeclLoc, T2) && |
4825 | 1.89M | RefRelationship == Sema::Ref_Incompatible) { |
4826 | 1.77M | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
4827 | 1.77M | Init, T2, /*AllowRvalues=*/false, |
4828 | 1.77M | AllowExplicit)) |
4829 | 698 | return ICS; |
4830 | 4.66M | } |
4831 | 4.28M | } |
4832 | | |
4833 | | // -- Otherwise, the reference shall be an lvalue reference to a |
4834 | | // non-volatile const type (i.e., cv1 shall be const), or the reference |
4835 | | // shall be an rvalue reference. |
4836 | 4.66M | if (!isRValRef && (4.28M !T1.isConstQualified()4.28M || T1.isVolatileQualified()458k )) { |
4837 | 3.82M | if (InitCategory.isRValue() && RefRelationship != Sema::Ref_Incompatible25.3k ) |
4838 | 417 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, DeclType); |
4839 | 3.82M | return ICS; |
4840 | 3.82M | } |
4841 | | |
4842 | | // -- If the initializer expression |
4843 | | // |
4844 | | // -- is an xvalue, class prvalue, array prvalue or function |
4845 | | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
4846 | 836k | if (RefRelationship == Sema::Ref_Compatible && |
4847 | 362k | (InitCategory.isXValue() || |
4848 | 302k | (InitCategory.isPRValue() && |
4849 | 263k | (T2->isRecordType() || T2->isArrayType()19.0k )) || |
4850 | 304k | (58.1k InitCategory.isLValue()58.1k && T2->isFunctionType()39.0k ))) { |
4851 | | // In C++11, this is always a direct binding. In C++98/03, it's a direct |
4852 | | // binding unless we're binding to a class prvalue. |
4853 | | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
4854 | | // allow the use of rvalue references in C++98/03 for the benefit of |
4855 | | // standard library implementors; therefore, we need the xvalue check here. |
4856 | 304k | SetAsReferenceBinding(/*BindsDirectly=*/S.getLangOpts().CPlusPlus11 || |
4857 | 4.59k | !(InitCategory.isPRValue() || T2->isRecordType()350 )); |
4858 | 304k | return ICS; |
4859 | 304k | } |
4860 | | |
4861 | | // -- has a class type (i.e., T2 is a class type), where T1 is not |
4862 | | // reference-related to T2, and can be implicitly converted to |
4863 | | // an xvalue, class prvalue, or function lvalue of type |
4864 | | // "cv3 T3", where "cv1 T1" is reference-compatible with |
4865 | | // "cv3 T3", |
4866 | | // |
4867 | | // then the reference is bound to the value of the initializer |
4868 | | // expression in the first case and to the result of the conversion |
4869 | | // in the second case (or, in either case, to an appropriate base |
4870 | | // class subobject). |
4871 | 532k | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible199k && |
4872 | 130k | T2->isRecordType() && S.isCompleteType(DeclLoc, T2)51.1k && |
4873 | 48.2k | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
4874 | 48.2k | Init, T2, /*AllowRvalues=*/true, |
4875 | 1.37k | AllowExplicit)) { |
4876 | | // In the second case, if the reference is an rvalue reference |
4877 | | // and the second standard conversion sequence of the |
4878 | | // user-defined conversion sequence includes an lvalue-to-rvalue |
4879 | | // conversion, the program is ill-formed. |
4880 | 1.37k | if (ICS.isUserDefined() && isRValRef && |
4881 | 1.07k | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
4882 | 0 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4883 | | |
4884 | 1.37k | return ICS; |
4885 | 1.37k | } |
4886 | | |
4887 | | // A temporary of function type cannot be created; don't even try. |
4888 | 530k | if (T1->isFunctionType()) |
4889 | 0 | return ICS; |
4890 | | |
4891 | | // -- Otherwise, a temporary of type "cv1 T1" is created and |
4892 | | // initialized from the initializer expression using the |
4893 | | // rules for a non-reference copy initialization (8.5). The |
4894 | | // reference is then bound to the temporary. If T1 is |
4895 | | // reference-related to T2, cv1 must be the same |
4896 | | // cv-qualification as, or greater cv-qualification than, |
4897 | | // cv2; otherwise, the program is ill-formed. |
4898 | 530k | if (RefRelationship == Sema::Ref_Related) { |
4899 | | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
4900 | | // we would be reference-compatible or reference-compatible with |
4901 | | // added qualification. But that wasn't the case, so the reference |
4902 | | // initialization fails. |
4903 | | // |
4904 | | // Note that we only want to check address spaces and cvr-qualifiers here. |
4905 | | // ObjC GC, lifetime and unaligned qualifiers aren't important. |
4906 | 54.7k | Qualifiers T1Quals = T1.getQualifiers(); |
4907 | 54.7k | Qualifiers T2Quals = T2.getQualifiers(); |
4908 | 54.7k | T1Quals.removeObjCGCAttr(); |
4909 | 54.7k | T1Quals.removeObjCLifetime(); |
4910 | 54.7k | T2Quals.removeObjCGCAttr(); |
4911 | 54.7k | T2Quals.removeObjCLifetime(); |
4912 | | // MS compiler ignores __unaligned qualifier for references; do the same. |
4913 | 54.7k | T1Quals.removeUnaligned(); |
4914 | 54.7k | T2Quals.removeUnaligned(); |
4915 | 54.7k | if (!T1Quals.compatiblyIncludes(T2Quals)) |
4916 | 54.7k | return ICS; |
4917 | 476k | } |
4918 | | |
4919 | | // If at least one of the types is a class type, the types are not |
4920 | | // related, and we aren't allowed any user conversions, the |
4921 | | // reference binding fails. This case is important for breaking |
4922 | | // recursion, since TryImplicitConversion below will attempt to |
4923 | | // create a temporary through the use of a copy constructor. |
4924 | 476k | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible290k && |
4925 | 289k | (T1->isRecordType() || T2->isRecordType()189 )) |
4926 | 289k | return ICS; |
4927 | | |
4928 | | // If T1 is reference-related to T2 and the reference is an rvalue |
4929 | | // reference, the initializer expression shall not be an lvalue. |
4930 | 186k | if (RefRelationship >= Sema::Ref_Related && isRValRef57.7k && |
4931 | 42.4k | Init->Classify(S.Context).isLValue()) { |
4932 | 38.6k | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, Init, DeclType); |
4933 | 38.6k | return ICS; |
4934 | 38.6k | } |
4935 | | |
4936 | | // C++ [over.ics.ref]p2: |
4937 | | // When a parameter of reference type is not bound directly to |
4938 | | // an argument expression, the conversion sequence is the one |
4939 | | // required to convert the argument expression to the |
4940 | | // underlying type of the reference according to |
4941 | | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
4942 | | // to copy-initializing a temporary of the underlying type with |
4943 | | // the argument expression. Any difference in top-level |
4944 | | // cv-qualification is subsumed by the initialization itself |
4945 | | // and does not constitute a conversion. |
4946 | 148k | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
4947 | 148k | AllowedExplicit::None, |
4948 | 148k | /*InOverloadResolution=*/false, |
4949 | 148k | /*CStyle=*/false, |
4950 | 148k | /*AllowObjCWritebackConversion=*/false, |
4951 | 148k | /*AllowObjCConversionOnExplicit=*/false); |
4952 | | |
4953 | | // Of course, that's still a reference binding. |
4954 | 148k | if (ICS.isStandard()) { |
4955 | 21.2k | ICS.Standard.ReferenceBinding = true; |
4956 | 21.2k | ICS.Standard.IsLvalueReference = !isRValRef; |
4957 | 21.2k | ICS.Standard.BindsToFunctionLvalue = false; |
4958 | 21.2k | ICS.Standard.BindsToRvalue = true; |
4959 | 21.2k | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4960 | 21.2k | ICS.Standard.ObjCLifetimeConversionBinding = false; |
4961 | 126k | } else if (ICS.isUserDefined()) { |
4962 | 23.9k | const ReferenceType *LValRefType = |
4963 | 23.9k | ICS.UserDefined.ConversionFunction->getReturnType() |
4964 | 23.9k | ->getAs<LValueReferenceType>(); |
4965 | | |
4966 | | // C++ [over.ics.ref]p3: |
4967 | | // Except for an implicit object parameter, for which see 13.3.1, a |
4968 | | // standard conversion sequence cannot be formed if it requires [...] |
4969 | | // binding an rvalue reference to an lvalue other than a function |
4970 | | // lvalue. |
4971 | | // Note that the function case is not possible here. |
4972 | 23.9k | if (isRValRef && LValRefType8.25k ) { |
4973 | 23 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4974 | 23 | return ICS; |
4975 | 23 | } |
4976 | | |
4977 | 23.8k | ICS.UserDefined.After.ReferenceBinding = true; |
4978 | 23.8k | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
4979 | 23.8k | ICS.UserDefined.After.BindsToFunctionLvalue = false; |
4980 | 23.8k | ICS.UserDefined.After.BindsToRvalue = !LValRefType; |
4981 | 23.8k | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4982 | 23.8k | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
4983 | 23.8k | } |
4984 | | |
4985 | 148k | return ICS; |
4986 | 148k | } |
4987 | | |
4988 | | static ImplicitConversionSequence |
4989 | | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
4990 | | bool SuppressUserConversions, |
4991 | | bool InOverloadResolution, |
4992 | | bool AllowObjCWritebackConversion, |
4993 | | bool AllowExplicit = false); |
4994 | | |
4995 | | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
4996 | | /// initializer list From. |
4997 | | static ImplicitConversionSequence |
4998 | | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
4999 | | bool SuppressUserConversions, |
5000 | | bool InOverloadResolution, |
5001 | 3.31k | bool AllowObjCWritebackConversion) { |
5002 | | // C++11 [over.ics.list]p1: |
5003 | | // When an argument is an initializer list, it is not an expression and |
5004 | | // special rules apply for converting it to a parameter type. |
5005 | | |
5006 | 3.31k | ImplicitConversionSequence Result; |
5007 | 3.31k | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
5008 | | |
5009 | | // We need a complete type for what follows. Incomplete types can never be |
5010 | | // initialized from init lists. |
5011 | 3.31k | if (!S.isCompleteType(From->getBeginLoc(), ToType)) |
5012 | 0 | return Result; |
5013 | | |
5014 | | // Per DR1467: |
5015 | | // If the parameter type is a class X and the initializer list has a single |
5016 | | // element of type cv U, where U is X or a class derived from X, the |
5017 | | // implicit conversion sequence is the one required to convert the element |
5018 | | // to the parameter type. |
5019 | | // |
5020 | | // Otherwise, if the parameter type is a character array [... ] |
5021 | | // and the initializer list has a single element that is an |
5022 | | // appropriately-typed string literal (8.5.2 [dcl.init.string]), the |
5023 | | // implicit conversion sequence is the identity conversion. |
5024 | 3.31k | if (From->getNumInits() == 1) { |
5025 | 1.36k | if (ToType->isRecordType()) { |
5026 | 641 | QualType InitType = From->getInit(0)->getType(); |
5027 | 641 | if (S.Context.hasSameUnqualifiedType(InitType, ToType) || |
5028 | 633 | S.IsDerivedFrom(From->getBeginLoc(), InitType, ToType)) |
5029 | 8 | return TryCopyInitialization(S, From->getInit(0), ToType, |
5030 | 8 | SuppressUserConversions, |
5031 | 8 | InOverloadResolution, |
5032 | 8 | AllowObjCWritebackConversion); |
5033 | 1.35k | } |
5034 | | |
5035 | 1.35k | if (const auto *AT = S.Context.getAsArrayType(ToType)) { |
5036 | 207 | if (S.IsStringInit(From->getInit(0), AT)) { |
5037 | 26 | InitializedEntity Entity = |
5038 | 26 | InitializedEntity::InitializeParameter(S.Context, ToType, |
5039 | 26 | /*Consumed=*/false); |
5040 | 26 | if (S.CanPerformCopyInitialization(Entity, From)) { |
5041 | 26 | Result.setStandard(); |
5042 | 26 | Result.Standard.setAsIdentityConversion(); |
5043 | 26 | Result.Standard.setFromType(ToType); |
5044 | 26 | Result.Standard.setAllToTypes(ToType); |
5045 | 26 | return Result; |
5046 | 26 | } |
5047 | 3.27k | } |
5048 | 207 | } |
5049 | 1.35k | } |
5050 | | |
5051 | | // C++14 [over.ics.list]p2: Otherwise, if the parameter type [...] (below). |
5052 | | // C++11 [over.ics.list]p2: |
5053 | | // If the parameter type is std::initializer_list<X> or "array of X" and |
5054 | | // all the elements can be implicitly converted to X, the implicit |
5055 | | // conversion sequence is the worst conversion necessary to convert an |
5056 | | // element of the list to X. |
5057 | | // |
5058 | | // C++14 [over.ics.list]p3: |
5059 | | // Otherwise, if the parameter type is "array of N X", if the initializer |
5060 | | // list has exactly N elements or if it has fewer than N elements and X is |
5061 | | // default-constructible, and if all the elements of the initializer list |
5062 | | // can be implicitly converted to X, the implicit conversion sequence is |
5063 | | // the worst conversion necessary to convert an element of the list to X. |
5064 | | // |
5065 | | // FIXME: We're missing a lot of these checks. |
5066 | 3.27k | bool toStdInitializerList = false; |
5067 | 3.27k | QualType X; |
5068 | 3.27k | if (ToType->isArrayType()) |
5069 | 368 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
5070 | 2.91k | else |
5071 | 2.91k | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
5072 | 3.27k | if (!X.isNull()) { |
5073 | 3.00k | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i1.97k ) { |
5074 | 2.12k | Expr *Init = From->getInit(i); |
5075 | 2.12k | ImplicitConversionSequence ICS = |
5076 | 2.12k | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
5077 | 2.12k | InOverloadResolution, |
5078 | 2.12k | AllowObjCWritebackConversion); |
5079 | | // If a single element isn't convertible, fail. |
5080 | 2.12k | if (ICS.isBad()) { |
5081 | 155 | Result = ICS; |
5082 | 155 | break; |
5083 | 155 | } |
5084 | | // Otherwise, look for the worst conversion. |
5085 | 1.97k | if (Result.isBad() || CompareImplicitConversionSequences( |
5086 | 1.15k | S, From->getBeginLoc(), ICS, Result) == |
5087 | 1.15k | ImplicitConversionSequence::Worse) |
5088 | 824 | Result = ICS; |
5089 | 1.97k | } |
5090 | | |
5091 | | // For an empty list, we won't have computed any conversion sequence. |
5092 | | // Introduce the identity conversion sequence. |
5093 | 1.02k | if (From->getNumInits
|