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