clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaOverload.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaOverload.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
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 | static bool functionHasPassObjectSizeParams(const FunctionDecl *FD) { |
46 | return llvm::any_of(FD->parameters(), [](const ParmVarDecl *P) { |
47 | return P->hasAttr<PassObjectSizeAttr>(); |
48 | }); |
49 | } |
50 | |
51 | |
52 | static ExprResult |
53 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
54 | const Expr *Base, bool HadMultipleCandidates, |
55 | SourceLocation Loc = SourceLocation(), |
56 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
57 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
58 | return ExprError(); |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
66 | return ExprError(); |
67 | DeclRefExpr *DRE = new (S.Context) |
68 | DeclRefExpr(S.Context, Fn, false, Fn->getType(), VK_LValue, Loc, LocInfo); |
69 | if (HadMultipleCandidates) |
70 | DRE->setHadMultipleCandidates(true); |
71 | |
72 | S.MarkDeclRefReferenced(DRE, Base); |
73 | if (auto *FPT = DRE->getType()->getAs<FunctionProtoType>()) { |
74 | if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { |
75 | S.ResolveExceptionSpec(Loc, FPT); |
76 | DRE->setType(Fn->getType()); |
77 | } |
78 | } |
79 | return S.ImpCastExprToType(DRE, S.Context.getPointerType(DRE->getType()), |
80 | CK_FunctionToPointerDecay); |
81 | } |
82 | |
83 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
84 | bool InOverloadResolution, |
85 | StandardConversionSequence &SCS, |
86 | bool CStyle, |
87 | bool AllowObjCWritebackConversion); |
88 | |
89 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
90 | QualType &ToType, |
91 | bool InOverloadResolution, |
92 | StandardConversionSequence &SCS, |
93 | bool CStyle); |
94 | static OverloadingResult |
95 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
96 | UserDefinedConversionSequence& User, |
97 | OverloadCandidateSet& Conversions, |
98 | AllowedExplicit AllowExplicit, |
99 | bool AllowObjCConversionOnExplicit); |
100 | |
101 | static ImplicitConversionSequence::CompareKind |
102 | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
103 | const StandardConversionSequence& SCS1, |
104 | const StandardConversionSequence& SCS2); |
105 | |
106 | static ImplicitConversionSequence::CompareKind |
107 | CompareQualificationConversions(Sema &S, |
108 | const StandardConversionSequence& SCS1, |
109 | const StandardConversionSequence& SCS2); |
110 | |
111 | static ImplicitConversionSequence::CompareKind |
112 | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
113 | const StandardConversionSequence& SCS1, |
114 | const StandardConversionSequence& SCS2); |
115 | |
116 | |
117 | |
118 | ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { |
119 | static const ImplicitConversionRank |
120 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
121 | ICR_Exact_Match, |
122 | ICR_Exact_Match, |
123 | ICR_Exact_Match, |
124 | ICR_Exact_Match, |
125 | ICR_Exact_Match, |
126 | ICR_Exact_Match, |
127 | ICR_Promotion, |
128 | ICR_Promotion, |
129 | ICR_Promotion, |
130 | ICR_Conversion, |
131 | ICR_Conversion, |
132 | ICR_Conversion, |
133 | ICR_Conversion, |
134 | ICR_Conversion, |
135 | ICR_Conversion, |
136 | ICR_Conversion, |
137 | ICR_Conversion, |
138 | ICR_Conversion, |
139 | ICR_Conversion, |
140 | ICR_Conversion, |
141 | ICR_OCL_Scalar_Widening, |
142 | ICR_Complex_Real_Conversion, |
143 | ICR_Conversion, |
144 | ICR_Conversion, |
145 | ICR_Writeback_Conversion, |
146 | ICR_Exact_Match, |
147 | |
148 | |
149 | ICR_C_Conversion, |
150 | ICR_C_Conversion_Extension |
151 | }; |
152 | return Rank[(int)Kind]; |
153 | } |
154 | |
155 | |
156 | |
157 | static const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
158 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
159 | "No conversion", |
160 | "Lvalue-to-rvalue", |
161 | "Array-to-pointer", |
162 | "Function-to-pointer", |
163 | "Function pointer conversion", |
164 | "Qualification", |
165 | "Integral promotion", |
166 | "Floating point promotion", |
167 | "Complex promotion", |
168 | "Integral conversion", |
169 | "Floating conversion", |
170 | "Complex conversion", |
171 | "Floating-integral conversion", |
172 | "Pointer conversion", |
173 | "Pointer-to-member conversion", |
174 | "Boolean conversion", |
175 | "Compatible-types conversion", |
176 | "Derived-to-base conversion", |
177 | "Vector conversion", |
178 | "SVE Vector conversion", |
179 | "Vector splat", |
180 | "Complex-real conversion", |
181 | "Block Pointer conversion", |
182 | "Transparent Union Conversion", |
183 | "Writeback conversion", |
184 | "OpenCL Zero Event Conversion", |
185 | "C specific type conversion", |
186 | "Incompatible pointer conversion" |
187 | }; |
188 | return Name[Kind]; |
189 | } |
190 | |
191 | |
192 | |
193 | void StandardConversionSequence::setAsIdentityConversion() { |
194 | First = ICK_Identity; |
195 | Second = ICK_Identity; |
196 | Third = ICK_Identity; |
197 | DeprecatedStringLiteralToCharPtr = false; |
198 | QualificationIncludesObjCLifetime = false; |
199 | ReferenceBinding = false; |
200 | DirectBinding = false; |
201 | IsLvalueReference = true; |
202 | BindsToFunctionLvalue = false; |
203 | BindsToRvalue = false; |
204 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
205 | ObjCLifetimeConversionBinding = false; |
206 | CopyConstructor = nullptr; |
207 | } |
208 | |
209 | |
210 | |
211 | |
212 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
213 | ImplicitConversionRank Rank = ICR_Exact_Match; |
214 | if (GetConversionRank(First) > Rank) |
215 | Rank = GetConversionRank(First); |
216 | if (GetConversionRank(Second) > Rank) |
217 | Rank = GetConversionRank(Second); |
218 | if (GetConversionRank(Third) > Rank) |
219 | Rank = GetConversionRank(Third); |
220 | return Rank; |
221 | } |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | bool StandardConversionSequence::isPointerConversionToBool() const { |
228 | |
229 | |
230 | |
231 | |
232 | if (getToType(1)->isBooleanType() && |
233 | (getFromType()->isPointerType() || |
234 | getFromType()->isMemberPointerType() || |
235 | getFromType()->isObjCObjectPointerType() || |
236 | getFromType()->isBlockPointerType() || |
237 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
238 | return true; |
239 | |
240 | return false; |
241 | } |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | bool |
248 | StandardConversionSequence:: |
249 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
250 | QualType FromType = getFromType(); |
251 | QualType ToType = getToType(1); |
252 | |
253 | |
254 | |
255 | |
256 | if (First == ICK_Array_To_Pointer) |
257 | FromType = Context.getArrayDecayedType(FromType); |
258 | |
259 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
260 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
261 | return ToPtrType->getPointeeType()->isVoidType(); |
262 | |
263 | return false; |
264 | } |
265 | |
266 | |
267 | |
268 | static const Expr *IgnoreNarrowingConversion(ASTContext &Ctx, |
269 | const Expr *Converted) { |
270 | |
271 | |
272 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Converted)) { |
273 | Expr *Inner = |
274 | const_cast<Expr *>(IgnoreNarrowingConversion(Ctx, EWC->getSubExpr())); |
275 | return ExprWithCleanups::Create(Ctx, Inner, EWC->cleanupsHaveSideEffects(), |
276 | EWC->getObjects()); |
277 | } |
278 | |
279 | while (auto *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
280 | switch (ICE->getCastKind()) { |
281 | case CK_NoOp: |
282 | case CK_IntegralCast: |
283 | case CK_IntegralToBoolean: |
284 | case CK_IntegralToFloating: |
285 | case CK_BooleanToSignedIntegral: |
286 | case CK_FloatingToIntegral: |
287 | case CK_FloatingToBoolean: |
288 | case CK_FloatingCast: |
289 | Converted = ICE->getSubExpr(); |
290 | continue; |
291 | |
292 | default: |
293 | return Converted; |
294 | } |
295 | } |
296 | |
297 | return Converted; |
298 | } |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | NarrowingKind StandardConversionSequence::getNarrowingKind( |
312 | ASTContext &Ctx, const Expr *Converted, APValue &ConstantValue, |
313 | QualType &ConstantType, bool IgnoreFloatToIntegralConversion) const { |
314 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
315 | |
316 | |
317 | |
318 | QualType FromType = getToType(0); |
319 | QualType ToType = getToType(1); |
320 | |
321 | |
322 | |
323 | |
324 | if (auto *ET = ToType->getAs<EnumType>()) |
325 | ToType = ET->getDecl()->getIntegerType(); |
326 | |
327 | switch (Second) { |
328 | |
329 | case ICK_Boolean_Conversion: |
330 | if (FromType->isRealFloatingType()) |
331 | goto FloatingIntegralConversion; |
332 | if (FromType->isIntegralOrUnscopedEnumerationType()) |
333 | goto IntegralConversion; |
334 | |
335 | return NK_Type_Narrowing; |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | |
342 | |
343 | case ICK_Floating_Integral: |
344 | FloatingIntegralConversion: |
345 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
346 | return NK_Type_Narrowing; |
347 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
348 | ToType->isRealFloatingType()) { |
349 | if (IgnoreFloatToIntegralConversion) |
350 | return NK_Not_Narrowing; |
351 | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
352 | assert(Initializer && "Unknown conversion expression"); |
353 | |
354 | |
355 | if (Initializer->isValueDependent()) |
356 | return NK_Dependent_Narrowing; |
357 | |
358 | if (Optional<llvm::APSInt> IntConstantValue = |
359 | Initializer->getIntegerConstantExpr(Ctx)) { |
360 | |
361 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
362 | Result.convertFromAPInt(*IntConstantValue, IntConstantValue->isSigned(), |
363 | llvm::APFloat::rmNearestTiesToEven); |
364 | |
365 | llvm::APSInt ConvertedValue = *IntConstantValue; |
366 | bool ignored; |
367 | Result.convertToInteger(ConvertedValue, |
368 | llvm::APFloat::rmTowardZero, &ignored); |
369 | |
370 | if (*IntConstantValue != ConvertedValue) { |
371 | ConstantValue = APValue(*IntConstantValue); |
372 | ConstantType = Initializer->getType(); |
373 | return NK_Constant_Narrowing; |
374 | } |
375 | } else { |
376 | |
377 | return NK_Variable_Narrowing; |
378 | } |
379 | } |
380 | return NK_Not_Narrowing; |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | case ICK_Floating_Conversion: |
387 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
388 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
389 | |
390 | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
391 | |
392 | |
393 | if (Initializer->isValueDependent()) |
394 | return NK_Dependent_Narrowing; |
395 | |
396 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
397 | |
398 | assert(ConstantValue.isFloat()); |
399 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
400 | |
401 | bool ignored; |
402 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
403 | Ctx.getFloatTypeSemantics(ToType), |
404 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
405 | |
406 | |
407 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
408 | ConstantType = Initializer->getType(); |
409 | return NK_Constant_Narrowing; |
410 | } |
411 | } else { |
412 | return NK_Variable_Narrowing; |
413 | } |
414 | } |
415 | return NK_Not_Narrowing; |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | |
422 | case ICK_Integral_Conversion: |
423 | IntegralConversion: { |
424 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
425 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
426 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
427 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
428 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
429 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
430 | |
431 | if (FromWidth > ToWidth || |
432 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
433 | (FromSigned && !ToSigned)) { |
434 | |
435 | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
436 | |
437 | |
438 | if (Initializer->isValueDependent()) |
439 | return NK_Dependent_Narrowing; |
440 | |
441 | Optional<llvm::APSInt> OptInitializerValue; |
442 | if (!(OptInitializerValue = Initializer->getIntegerConstantExpr(Ctx))) { |
443 | |
444 | return NK_Variable_Narrowing; |
445 | } |
446 | llvm::APSInt &InitializerValue = *OptInitializerValue; |
447 | bool Narrowing = false; |
448 | if (FromWidth < ToWidth) { |
449 | |
450 | |
451 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
452 | Narrowing = true; |
453 | } else { |
454 | |
455 | |
456 | InitializerValue = InitializerValue.extend( |
457 | InitializerValue.getBitWidth() + 1); |
458 | |
459 | llvm::APSInt ConvertedValue = InitializerValue; |
460 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
461 | ConvertedValue.setIsSigned(ToSigned); |
462 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
463 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
464 | |
465 | if (ConvertedValue != InitializerValue) |
466 | Narrowing = true; |
467 | } |
468 | if (Narrowing) { |
469 | ConstantType = Initializer->getType(); |
470 | ConstantValue = APValue(InitializerValue); |
471 | return NK_Constant_Narrowing; |
472 | } |
473 | } |
474 | return NK_Not_Narrowing; |
475 | } |
476 | |
477 | default: |
478 | |
479 | return NK_Not_Narrowing; |
480 | } |
481 | } |
482 | |
483 | |
484 | |
485 | LLVM_DUMP_METHOD void StandardConversionSequence::dump() const { |
486 | raw_ostream &OS = llvm::errs(); |
487 | bool PrintedSomething = false; |
488 | if (First != ICK_Identity) { |
489 | OS << GetImplicitConversionName(First); |
490 | PrintedSomething = true; |
491 | } |
492 | |
493 | if (Second != ICK_Identity) { |
494 | if (PrintedSomething) { |
495 | OS << " -> "; |
496 | } |
497 | OS << GetImplicitConversionName(Second); |
498 | |
499 | if (CopyConstructor) { |
500 | OS << " (by copy constructor)"; |
501 | } else if (DirectBinding) { |
502 | OS << " (direct reference binding)"; |
503 | } else if (ReferenceBinding) { |
504 | OS << " (reference binding)"; |
505 | } |
506 | PrintedSomething = true; |
507 | } |
508 | |
509 | if (Third != ICK_Identity) { |
510 | if (PrintedSomething) { |
511 | OS << " -> "; |
512 | } |
513 | OS << GetImplicitConversionName(Third); |
514 | PrintedSomething = true; |
515 | } |
516 | |
517 | if (!PrintedSomething) { |
518 | OS << "No conversions required"; |
519 | } |
520 | } |
521 | |
522 | |
523 | |
524 | void UserDefinedConversionSequence::dump() const { |
525 | raw_ostream &OS = llvm::errs(); |
526 | if (Before.First || Before.Second || Before.Third) { |
527 | Before.dump(); |
528 | OS << " -> "; |
529 | } |
530 | if (ConversionFunction) |
531 | OS << '\'' << *ConversionFunction << '\''; |
532 | else |
533 | OS << "aggregate initialization"; |
534 | if (After.First || After.Second || After.Third) { |
535 | OS << " -> "; |
536 | After.dump(); |
537 | } |
538 | } |
539 | |
540 | |
541 | |
542 | void ImplicitConversionSequence::dump() const { |
543 | raw_ostream &OS = llvm::errs(); |
544 | if (isStdInitializerListElement()) |
545 | OS << "Worst std::initializer_list element conversion: "; |
546 | switch (ConversionKind) { |
547 | case StandardConversion: |
548 | OS << "Standard conversion: "; |
549 | Standard.dump(); |
550 | break; |
551 | case UserDefinedConversion: |
552 | OS << "User-defined conversion: "; |
553 | UserDefined.dump(); |
554 | break; |
555 | case EllipsisConversion: |
556 | OS << "Ellipsis conversion"; |
557 | break; |
558 | case AmbiguousConversion: |
559 | OS << "Ambiguous conversion"; |
560 | break; |
561 | case BadConversion: |
562 | OS << "Bad conversion"; |
563 | break; |
564 | } |
565 | |
566 | OS << "\n"; |
567 | } |
568 | |
569 | void AmbiguousConversionSequence::construct() { |
570 | new (&conversions()) ConversionSet(); |
571 | } |
572 | |
573 | void AmbiguousConversionSequence::destruct() { |
574 | conversions().~ConversionSet(); |
575 | } |
576 | |
577 | void |
578 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
579 | FromTypePtr = O.FromTypePtr; |
580 | ToTypePtr = O.ToTypePtr; |
581 | new (&conversions()) ConversionSet(O.conversions()); |
582 | } |
583 | |
584 | namespace { |
585 | |
586 | |
587 | struct DFIArguments { |
588 | TemplateArgument FirstArg; |
589 | TemplateArgument SecondArg; |
590 | }; |
591 | |
592 | |
593 | struct DFIParamWithArguments : DFIArguments { |
594 | TemplateParameter Param; |
595 | }; |
596 | |
597 | |
598 | struct DFIDeducedMismatchArgs : DFIArguments { |
599 | TemplateArgumentList *TemplateArgs; |
600 | unsigned CallArgIndex; |
601 | }; |
602 | |
603 | |
604 | struct CNSInfo { |
605 | TemplateArgumentList *TemplateArgs; |
606 | ConstraintSatisfaction Satisfaction; |
607 | }; |
608 | } |
609 | |
610 | |
611 | |
612 | DeductionFailureInfo |
613 | clang::MakeDeductionFailureInfo(ASTContext &Context, |
614 | Sema::TemplateDeductionResult TDK, |
615 | TemplateDeductionInfo &Info) { |
616 | DeductionFailureInfo Result; |
617 | Result.Result = static_cast<unsigned>(TDK); |
618 | Result.HasDiagnostic = false; |
619 | switch (TDK) { |
620 | case Sema::TDK_Invalid: |
621 | case Sema::TDK_InstantiationDepth: |
622 | case Sema::TDK_TooManyArguments: |
623 | case Sema::TDK_TooFewArguments: |
624 | case Sema::TDK_MiscellaneousDeductionFailure: |
625 | case Sema::TDK_CUDATargetMismatch: |
626 | Result.Data = nullptr; |
627 | break; |
628 | |
629 | case Sema::TDK_Incomplete: |
630 | case Sema::TDK_InvalidExplicitArguments: |
631 | Result.Data = Info.Param.getOpaqueValue(); |
632 | break; |
633 | |
634 | case Sema::TDK_DeducedMismatch: |
635 | case Sema::TDK_DeducedMismatchNested: { |
636 | |
637 | auto *Saved = new (Context) DFIDeducedMismatchArgs; |
638 | Saved->FirstArg = Info.FirstArg; |
639 | Saved->SecondArg = Info.SecondArg; |
640 | Saved->TemplateArgs = Info.take(); |
641 | Saved->CallArgIndex = Info.CallArgIndex; |
642 | Result.Data = Saved; |
643 | break; |
644 | } |
645 | |
646 | case Sema::TDK_NonDeducedMismatch: { |
647 | |
648 | DFIArguments *Saved = new (Context) DFIArguments; |
649 | Saved->FirstArg = Info.FirstArg; |
650 | Saved->SecondArg = Info.SecondArg; |
651 | Result.Data = Saved; |
652 | break; |
653 | } |
654 | |
655 | case Sema::TDK_IncompletePack: |
656 | |
657 | case Sema::TDK_Inconsistent: |
658 | case Sema::TDK_Underqualified: { |
659 | |
660 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
661 | Saved->Param = Info.Param; |
662 | Saved->FirstArg = Info.FirstArg; |
663 | Saved->SecondArg = Info.SecondArg; |
664 | Result.Data = Saved; |
665 | break; |
666 | } |
667 | |
668 | case Sema::TDK_SubstitutionFailure: |
669 | Result.Data = Info.take(); |
670 | if (Info.hasSFINAEDiagnostic()) { |
671 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
672 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
673 | Info.takeSFINAEDiagnostic(*Diag); |
674 | Result.HasDiagnostic = true; |
675 | } |
676 | break; |
677 | |
678 | case Sema::TDK_ConstraintsNotSatisfied: { |
679 | CNSInfo *Saved = new (Context) CNSInfo; |
680 | Saved->TemplateArgs = Info.take(); |
681 | Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction; |
682 | Result.Data = Saved; |
683 | break; |
684 | } |
685 | |
686 | case Sema::TDK_Success: |
687 | case Sema::TDK_NonDependentConversionFailure: |
688 | llvm_unreachable("not a deduction failure"); |
689 | } |
690 | |
691 | return Result; |
692 | } |
693 | |
694 | void DeductionFailureInfo::Destroy() { |
695 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
696 | case Sema::TDK_Success: |
697 | case Sema::TDK_Invalid: |
698 | case Sema::TDK_InstantiationDepth: |
699 | case Sema::TDK_Incomplete: |
700 | case Sema::TDK_TooManyArguments: |
701 | case Sema::TDK_TooFewArguments: |
702 | case Sema::TDK_InvalidExplicitArguments: |
703 | case Sema::TDK_CUDATargetMismatch: |
704 | case Sema::TDK_NonDependentConversionFailure: |
705 | break; |
706 | |
707 | case Sema::TDK_IncompletePack: |
708 | case Sema::TDK_Inconsistent: |
709 | case Sema::TDK_Underqualified: |
710 | case Sema::TDK_DeducedMismatch: |
711 | case Sema::TDK_DeducedMismatchNested: |
712 | case Sema::TDK_NonDeducedMismatch: |
713 | |
714 | Data = nullptr; |
715 | break; |
716 | |
717 | case Sema::TDK_SubstitutionFailure: |
718 | |
719 | Data = nullptr; |
720 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
721 | Diag->~PartialDiagnosticAt(); |
722 | HasDiagnostic = false; |
723 | } |
724 | break; |
725 | |
726 | case Sema::TDK_ConstraintsNotSatisfied: |
727 | |
728 | Data = nullptr; |
729 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
730 | Diag->~PartialDiagnosticAt(); |
731 | HasDiagnostic = false; |
732 | } |
733 | break; |
734 | |
735 | |
736 | case Sema::TDK_MiscellaneousDeductionFailure: |
737 | break; |
738 | } |
739 | } |
740 | |
741 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
742 | if (HasDiagnostic) |
743 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
744 | return nullptr; |
745 | } |
746 | |
747 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
748 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
749 | case Sema::TDK_Success: |
750 | case Sema::TDK_Invalid: |
751 | case Sema::TDK_InstantiationDepth: |
752 | case Sema::TDK_TooManyArguments: |
753 | case Sema::TDK_TooFewArguments: |
754 | case Sema::TDK_SubstitutionFailure: |
755 | case Sema::TDK_DeducedMismatch: |
756 | case Sema::TDK_DeducedMismatchNested: |
757 | case Sema::TDK_NonDeducedMismatch: |
758 | case Sema::TDK_CUDATargetMismatch: |
759 | case Sema::TDK_NonDependentConversionFailure: |
760 | case Sema::TDK_ConstraintsNotSatisfied: |
761 | return TemplateParameter(); |
762 | |
763 | case Sema::TDK_Incomplete: |
764 | case Sema::TDK_InvalidExplicitArguments: |
765 | return TemplateParameter::getFromOpaqueValue(Data); |
766 | |
767 | case Sema::TDK_IncompletePack: |
768 | case Sema::TDK_Inconsistent: |
769 | case Sema::TDK_Underqualified: |
770 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
771 | |
772 | |
773 | case Sema::TDK_MiscellaneousDeductionFailure: |
774 | break; |
775 | } |
776 | |
777 | return TemplateParameter(); |
778 | } |
779 | |
780 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
781 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
782 | case Sema::TDK_Success: |
783 | case Sema::TDK_Invalid: |
784 | case Sema::TDK_InstantiationDepth: |
785 | case Sema::TDK_TooManyArguments: |
786 | case Sema::TDK_TooFewArguments: |
787 | case Sema::TDK_Incomplete: |
788 | case Sema::TDK_IncompletePack: |
789 | case Sema::TDK_InvalidExplicitArguments: |
790 | case Sema::TDK_Inconsistent: |
791 | case Sema::TDK_Underqualified: |
792 | case Sema::TDK_NonDeducedMismatch: |
793 | case Sema::TDK_CUDATargetMismatch: |
794 | case Sema::TDK_NonDependentConversionFailure: |
795 | return nullptr; |
796 | |
797 | case Sema::TDK_DeducedMismatch: |
798 | case Sema::TDK_DeducedMismatchNested: |
799 | return static_cast<DFIDeducedMismatchArgs*>(Data)->TemplateArgs; |
800 | |
801 | case Sema::TDK_SubstitutionFailure: |
802 | return static_cast<TemplateArgumentList*>(Data); |
803 | |
804 | case Sema::TDK_ConstraintsNotSatisfied: |
805 | return static_cast<CNSInfo*>(Data)->TemplateArgs; |
806 | |
807 | |
808 | case Sema::TDK_MiscellaneousDeductionFailure: |
809 | break; |
810 | } |
811 | |
812 | return nullptr; |
813 | } |
814 | |
815 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
816 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
817 | case Sema::TDK_Success: |
818 | case Sema::TDK_Invalid: |
819 | case Sema::TDK_InstantiationDepth: |
820 | case Sema::TDK_Incomplete: |
821 | case Sema::TDK_TooManyArguments: |
822 | case Sema::TDK_TooFewArguments: |
823 | case Sema::TDK_InvalidExplicitArguments: |
824 | case Sema::TDK_SubstitutionFailure: |
825 | case Sema::TDK_CUDATargetMismatch: |
826 | case Sema::TDK_NonDependentConversionFailure: |
827 | case Sema::TDK_ConstraintsNotSatisfied: |
828 | return nullptr; |
829 | |
830 | case Sema::TDK_IncompletePack: |
831 | case Sema::TDK_Inconsistent: |
832 | case Sema::TDK_Underqualified: |
833 | case Sema::TDK_DeducedMismatch: |
834 | case Sema::TDK_DeducedMismatchNested: |
835 | case Sema::TDK_NonDeducedMismatch: |
836 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
837 | |
838 | |
839 | case Sema::TDK_MiscellaneousDeductionFailure: |
840 | break; |
841 | } |
842 | |
843 | return nullptr; |
844 | } |
845 | |
846 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
847 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
848 | case Sema::TDK_Success: |
849 | case Sema::TDK_Invalid: |
850 | case Sema::TDK_InstantiationDepth: |
851 | case Sema::TDK_Incomplete: |
852 | case Sema::TDK_IncompletePack: |
853 | case Sema::TDK_TooManyArguments: |
854 | case Sema::TDK_TooFewArguments: |
855 | case Sema::TDK_InvalidExplicitArguments: |
856 | case Sema::TDK_SubstitutionFailure: |
857 | case Sema::TDK_CUDATargetMismatch: |
858 | case Sema::TDK_NonDependentConversionFailure: |
859 | case Sema::TDK_ConstraintsNotSatisfied: |
860 | return nullptr; |
861 | |
862 | case Sema::TDK_Inconsistent: |
863 | case Sema::TDK_Underqualified: |
864 | case Sema::TDK_DeducedMismatch: |
865 | case Sema::TDK_DeducedMismatchNested: |
866 | case Sema::TDK_NonDeducedMismatch: |
867 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
868 | |
869 | |
870 | case Sema::TDK_MiscellaneousDeductionFailure: |
871 | break; |
872 | } |
873 | |
874 | return nullptr; |
875 | } |
876 | |
877 | llvm::Optional<unsigned> DeductionFailureInfo::getCallArgIndex() { |
878 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
879 | case Sema::TDK_DeducedMismatch: |
880 | case Sema::TDK_DeducedMismatchNested: |
881 | return static_cast<DFIDeducedMismatchArgs*>(Data)->CallArgIndex; |
882 | |
883 | default: |
884 | return llvm::None; |
885 | } |
886 | } |
887 | |
888 | bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed( |
889 | OverloadedOperatorKind Op) { |
890 | if (!AllowRewrittenCandidates) |
891 | return false; |
892 | return Op == OO_EqualEqual || Op == OO_Spaceship; |
893 | } |
894 | |
895 | bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed( |
896 | ASTContext &Ctx, const FunctionDecl *FD) { |
897 | if (!shouldAddReversed(FD->getDeclName().getCXXOverloadedOperator())) |
898 | return false; |
899 | |
900 | |
901 | return FD->getNumParams() != 2 || |
902 | !Ctx.hasSameUnqualifiedType(FD->getParamDecl(0)->getType(), |
903 | FD->getParamDecl(1)->getType()) || |
904 | FD->hasAttr<EnableIfAttr>(); |
905 | } |
906 | |
907 | void OverloadCandidateSet::destroyCandidates() { |
908 | for (iterator i = begin(), e = end(); i != e; ++i) { |
909 | for (auto &C : i->Conversions) |
910 | C.~ImplicitConversionSequence(); |
911 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
912 | i->DeductionFailure.Destroy(); |
913 | } |
914 | } |
915 | |
916 | void OverloadCandidateSet::clear(CandidateSetKind CSK) { |
917 | destroyCandidates(); |
918 | SlabAllocator.Reset(); |
919 | NumInlineBytesUsed = 0; |
920 | Candidates.clear(); |
921 | Functions.clear(); |
922 | Kind = CSK; |
923 | } |
924 | |
925 | namespace { |
926 | class UnbridgedCastsSet { |
927 | struct Entry { |
928 | Expr **Addr; |
929 | Expr *Saved; |
930 | }; |
931 | SmallVector<Entry, 2> Entries; |
932 | |
933 | public: |
934 | void save(Sema &S, Expr *&E) { |
935 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
936 | Entry entry = { &E, E }; |
937 | Entries.push_back(entry); |
938 | E = S.stripARCUnbridgedCast(E); |
939 | } |
940 | |
941 | void restore() { |
942 | for (SmallVectorImpl<Entry>::iterator |
943 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
944 | *i->Addr = i->Saved; |
945 | } |
946 | }; |
947 | } |
948 | |
949 | |
950 | |
951 | |
952 | |
953 | |
954 | |
955 | |
956 | static bool |
957 | checkPlaceholderForOverload(Sema &S, Expr *&E, |
958 | UnbridgedCastsSet *unbridgedCasts = nullptr) { |
959 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
960 | |
961 | |
962 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
963 | |
964 | |
965 | |
966 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
967 | unbridgedCasts) { |
968 | unbridgedCasts->save(S, E); |
969 | return false; |
970 | } |
971 | |
972 | |
973 | ExprResult result = S.CheckPlaceholderExpr(E); |
974 | if (result.isInvalid()) |
975 | return true; |
976 | |
977 | E = result.get(); |
978 | return false; |
979 | } |
980 | |
981 | |
982 | return false; |
983 | } |
984 | |
985 | |
986 | |
987 | static bool checkArgPlaceholdersForOverload(Sema &S, |
988 | MultiExprArg Args, |
989 | UnbridgedCastsSet &unbridged) { |
990 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
991 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
992 | return true; |
993 | |
994 | return false; |
995 | } |
996 | |
997 | |
998 | |
999 | |
1000 | |
1001 | |
1002 | |
1003 | |
1004 | |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | |
1010 | |
1011 | |
1012 | |
1013 | |
1014 | |
1015 | |
1016 | |
1017 | |
1018 | |
1019 | |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | |
1025 | |
1026 | |
1027 | |
1028 | |
1029 | |
1030 | Sema::OverloadKind |
1031 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
1032 | NamedDecl *&Match, bool NewIsUsingDecl) { |
1033 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
1034 | I != E; ++I) { |
1035 | NamedDecl *OldD = *I; |
1036 | |
1037 | bool OldIsUsingDecl = false; |
1038 | if (isa<UsingShadowDecl>(OldD)) { |
1039 | OldIsUsingDecl = true; |
1040 | |
1041 | |
1042 | |
1043 | if (NewIsUsingDecl) continue; |
1044 | |
1045 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
1046 | } |
1047 | |
1048 | |
1049 | |
1050 | if ((OldIsUsingDecl || NewIsUsingDecl) && !isVisible(*I)) |
1051 | continue; |
1052 | |
1053 | |
1054 | |
1055 | |
1056 | |
1057 | |
1058 | bool UseMemberUsingDeclRules = |
1059 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
1060 | !New->getFriendObjectKind(); |
1061 | |
1062 | if (FunctionDecl *OldF = OldD->getAsFunction()) { |
1063 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
1064 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
1065 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
1066 | continue; |
1067 | } |
1068 | |
1069 | if (!isa<FunctionTemplateDecl>(OldD) && |
1070 | !shouldLinkPossiblyHiddenDecl(*I, New)) |
1071 | continue; |
1072 | |
1073 | Match = *I; |
1074 | return Ovl_Match; |
1075 | } |
1076 | |
1077 | |
1078 | |
1079 | if (!getASTContext().canBuiltinBeRedeclared(OldF)) { |
1080 | Match = *I; |
1081 | return Ovl_NonFunction; |
1082 | } |
1083 | } else if (isa<UsingDecl>(OldD) || isa<UsingPackDecl>(OldD)) { |
1084 | |
1085 | |
1086 | assert(Old.getLookupKind() == LookupUsingDeclName); |
1087 | } else if (isa<TagDecl>(OldD)) { |
1088 | |
1089 | } else if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(OldD)) { |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | |
1096 | if (UUD->getQualifier()->isDependent() && !UUD->isCXXClassMember()) { |
1097 | Match = *I; |
1098 | return Ovl_NonFunction; |
1099 | } |
1100 | } else { |
1101 | |
1102 | |
1103 | |
1104 | Match = *I; |
1105 | return Ovl_NonFunction; |
1106 | } |
1107 | } |
1108 | |
1109 | |
1110 | |
1111 | |
1112 | |
1113 | |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | |
1119 | |
1120 | |
1121 | |
1122 | |
1123 | |
1124 | if (New->getFriendObjectKind() && New->getQualifier() && |
1125 | !New->getDescribedFunctionTemplate() && |
1126 | !New->getDependentSpecializationInfo() && |
1127 | !New->getType()->isDependentType()) { |
1128 | LookupResult TemplateSpecResult(LookupResult::Temporary, Old); |
1129 | TemplateSpecResult.addAllDecls(Old); |
1130 | if (CheckFunctionTemplateSpecialization(New, nullptr, TemplateSpecResult, |
1131 | true)) { |
1132 | New->setInvalidDecl(); |
1133 | return Ovl_Overload; |
1134 | } |
1135 | |
1136 | Match = TemplateSpecResult.getAsSingle<FunctionDecl>(); |
1137 | return Ovl_Match; |
1138 | } |
1139 | |
1140 | return Ovl_Overload; |
1141 | } |
1142 | |
1143 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
1144 | bool UseMemberUsingDeclRules, bool ConsiderCudaAttrs, |
1145 | bool ConsiderRequiresClauses) { |
1146 | |
1147 | if (New->isMain()) |
1148 | return false; |
1149 | |
1150 | |
1151 | if (New->isMSVCRTEntryPoint()) |
1152 | return false; |
1153 | |
1154 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
1155 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
1156 | |
1157 | |
1158 | |
1159 | |
1160 | if ((OldTemplate == nullptr) != (NewTemplate == nullptr)) |
1161 | return true; |
1162 | |
1163 | |
1164 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
1165 | QualType NewQType = Context.getCanonicalType(New->getType()); |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | |
1171 | |
1172 | |
1173 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
1174 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
1175 | return false; |
1176 | |
1177 | const FunctionProtoType *OldType = cast<FunctionProtoType>(OldQType); |
1178 | const FunctionProtoType *NewType = cast<FunctionProtoType>(NewQType); |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | if (OldQType != NewQType && |
1184 | (OldType->getNumParams() != NewType->getNumParams() || |
1185 | OldType->isVariadic() != NewType->isVariadic() || |
1186 | !FunctionParamTypesAreEqual(OldType, NewType))) |
1187 | return true; |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | |
1199 | |
1200 | |
1201 | if (!UseMemberUsingDeclRules && NewTemplate && |
1202 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
1203 | OldTemplate->getTemplateParameters(), |
1204 | false, TPL_TemplateMatch) || |
1205 | !Context.hasSameType(Old->getDeclaredReturnType(), |
1206 | New->getDeclaredReturnType()))) |
1207 | return true; |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | |
1214 | |
1215 | |
1216 | |
1217 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
1218 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
1219 | if (OldMethod && NewMethod && |
1220 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
1221 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
1222 | if (!UseMemberUsingDeclRules && |
1223 | (OldMethod->getRefQualifier() == RQ_None || |
1224 | NewMethod->getRefQualifier() == RQ_None)) { |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
1232 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
1233 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
1234 | } |
1235 | return true; |
1236 | } |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | auto OldQuals = OldMethod->getMethodQualifiers(); |
1243 | auto NewQuals = NewMethod->getMethodQualifiers(); |
1244 | if (!getLangOpts().CPlusPlus14 && NewMethod->isConstexpr() && |
1245 | !isa<CXXConstructorDecl>(NewMethod)) |
1246 | NewQuals.addConst(); |
1247 | |
1248 | OldQuals.removeRestrict(); |
1249 | NewQuals.removeRestrict(); |
1250 | if (OldQuals != NewQuals) |
1251 | return true; |
1252 | } |
1253 | |
1254 | |
1255 | |
1256 | |
1257 | |
1258 | if (functionHasPassObjectSizeParams(New) != |
1259 | functionHasPassObjectSizeParams(Old)) |
1260 | return true; |
1261 | |
1262 | |
1263 | for (specific_attr_iterator<EnableIfAttr> |
1264 | NewI = New->specific_attr_begin<EnableIfAttr>(), |
1265 | NewE = New->specific_attr_end<EnableIfAttr>(), |
1266 | OldI = Old->specific_attr_begin<EnableIfAttr>(), |
1267 | OldE = Old->specific_attr_end<EnableIfAttr>(); |
1268 | NewI != NewE || OldI != OldE; ++NewI, ++OldI) { |
1269 | if (NewI == NewE || OldI == OldE) |
1270 | return true; |
1271 | llvm::FoldingSetNodeID NewID, OldID; |
1272 | NewI->getCond()->Profile(NewID, Context, true); |
1273 | OldI->getCond()->Profile(OldID, Context, true); |
1274 | if (NewID != OldID) |
1275 | return true; |
1276 | } |
1277 | |
1278 | if (getLangOpts().CUDA && ConsiderCudaAttrs) { |
1279 | |
1280 | |
1281 | if (!isa<CXXDestructorDecl>(New)) { |
1282 | CUDAFunctionTarget NewTarget = IdentifyCUDATarget(New), |
1283 | OldTarget = IdentifyCUDATarget(Old); |
1284 | if (NewTarget != CFT_InvalidTarget) { |
1285 | assert((OldTarget != CFT_InvalidTarget) && |
1286 | "Unexpected invalid target."); |
1287 | |
1288 | |
1289 | |
1290 | if (NewTarget != OldTarget) |
1291 | return true; |
1292 | } |
1293 | } |
1294 | } |
1295 | |
1296 | if (ConsiderRequiresClauses) { |
1297 | Expr *NewRC = New->getTrailingRequiresClause(), |
1298 | *OldRC = Old->getTrailingRequiresClause(); |
1299 | if ((NewRC != nullptr) != (OldRC != nullptr)) |
1300 | |
1301 | return true; |
1302 | |
1303 | if (NewRC) { |
1304 | llvm::FoldingSetNodeID NewID, OldID; |
1305 | NewRC->Profile(NewID, Context, true); |
1306 | OldRC->Profile(OldID, Context, true); |
1307 | if (NewID != OldID) |
1308 | |
1309 | return true; |
1310 | } |
1311 | } |
1312 | |
1313 | |
1314 | return false; |
1315 | } |
1316 | |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | static ImplicitConversionSequence |
1322 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
1323 | bool SuppressUserConversions, |
1324 | AllowedExplicit AllowExplicit, |
1325 | bool InOverloadResolution, |
1326 | bool CStyle, |
1327 | bool AllowObjCWritebackConversion, |
1328 | bool AllowObjCConversionOnExplicit) { |
1329 | ImplicitConversionSequence ICS; |
1330 | |
1331 | if (SuppressUserConversions) { |
1332 | |
1333 | |
1334 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1335 | return ICS; |
1336 | } |
1337 | |
1338 | |
1339 | OverloadCandidateSet Conversions(From->getExprLoc(), |
1340 | OverloadCandidateSet::CSK_Normal); |
1341 | switch (IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, |
1342 | Conversions, AllowExplicit, |
1343 | AllowObjCConversionOnExplicit)) { |
1344 | case OR_Success: |
1345 | case OR_Deleted: |
1346 | ICS.setUserDefined(); |
1347 | |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | |
1353 | |
1354 | if (CXXConstructorDecl *Constructor |
1355 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
1356 | QualType FromCanon |
1357 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
1358 | QualType ToCanon |
1359 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
1360 | if (Constructor->isCopyConstructor() && |
1361 | (FromCanon == ToCanon || |
1362 | S.IsDerivedFrom(From->getBeginLoc(), FromCanon, ToCanon))) { |
1363 | |
1364 | |
1365 | DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction; |
1366 | ICS.setStandard(); |
1367 | ICS.Standard.setAsIdentityConversion(); |
1368 | ICS.Standard.setFromType(From->getType()); |
1369 | ICS.Standard.setAllToTypes(ToType); |
1370 | ICS.Standard.CopyConstructor = Constructor; |
1371 | ICS.Standard.FoundCopyConstructor = Found; |
1372 | if (ToCanon != FromCanon) |
1373 | ICS.Standard.Second = ICK_Derived_To_Base; |
1374 | } |
1375 | } |
1376 | break; |
1377 | |
1378 | case OR_Ambiguous: |
1379 | ICS.setAmbiguous(); |
1380 | ICS.Ambiguous.setFromType(From->getType()); |
1381 | ICS.Ambiguous.setToType(ToType); |
1382 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
1383 | Cand != Conversions.end(); ++Cand) |
1384 | if (Cand->Best) |
1385 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
1386 | break; |
1387 | |
1388 | |
1389 | case OR_No_Viable_Function: |
1390 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1391 | break; |
1392 | } |
1393 | |
1394 | return ICS; |
1395 | } |
1396 | |
1397 | |
1398 | |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | |
1423 | |
1424 | static ImplicitConversionSequence |
1425 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
1426 | bool SuppressUserConversions, |
1427 | AllowedExplicit AllowExplicit, |
1428 | bool InOverloadResolution, |
1429 | bool CStyle, |
1430 | bool AllowObjCWritebackConversion, |
1431 | bool AllowObjCConversionOnExplicit) { |
1432 | ImplicitConversionSequence ICS; |
1433 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
1434 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
1435 | ICS.setStandard(); |
1436 | return ICS; |
1437 | } |
1438 | |
1439 | if (!S.getLangOpts().CPlusPlus) { |
1440 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1441 | return ICS; |
1442 | } |
1443 | |
1444 | |
1445 | |
1446 | |
1447 | |
1448 | |
1449 | |
1450 | |
1451 | QualType FromType = From->getType(); |
1452 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
1453 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
1454 | S.IsDerivedFrom(From->getBeginLoc(), FromType, ToType))) { |
1455 | ICS.setStandard(); |
1456 | ICS.Standard.setAsIdentityConversion(); |
1457 | ICS.Standard.setFromType(FromType); |
1458 | ICS.Standard.setAllToTypes(ToType); |
1459 | |
1460 | |
1461 | |
1462 | |
1463 | |
1464 | ICS.Standard.CopyConstructor = nullptr; |
1465 | |
1466 | |
1467 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
1468 | ICS.Standard.Second = ICK_Derived_To_Base; |
1469 | |
1470 | return ICS; |
1471 | } |
1472 | |
1473 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
1474 | AllowExplicit, InOverloadResolution, CStyle, |
1475 | AllowObjCWritebackConversion, |
1476 | AllowObjCConversionOnExplicit); |
1477 | } |
1478 | |
1479 | ImplicitConversionSequence |
1480 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
1481 | bool SuppressUserConversions, |
1482 | AllowedExplicit AllowExplicit, |
1483 | bool InOverloadResolution, |
1484 | bool CStyle, |
1485 | bool AllowObjCWritebackConversion) { |
1486 | return ::TryImplicitConversion(*this, From, ToType, SuppressUserConversions, |
1487 | AllowExplicit, InOverloadResolution, CStyle, |
1488 | AllowObjCWritebackConversion, |
1489 | false); |
1490 | } |
1491 | |
1492 | |
1493 | |
1494 | |
1495 | |
1496 | |
1497 | ExprResult Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
1498 | AssignmentAction Action, |
1499 | bool AllowExplicit) { |
1500 | if (checkPlaceholderForOverload(*this, From)) |
1501 | return ExprError(); |
1502 | |
1503 | |
1504 | bool AllowObjCWritebackConversion |
1505 | = getLangOpts().ObjCAutoRefCount && |
1506 | (Action == AA_Passing || Action == AA_Sending); |
1507 | if (getLangOpts().ObjC) |
1508 | CheckObjCBridgeRelatedConversions(From->getBeginLoc(), ToType, |
1509 | From->getType(), From); |
1510 | ImplicitConversionSequence ICS = ::TryImplicitConversion( |
1511 | *this, From, ToType, |
1512 | false, |
1513 | AllowExplicit ? AllowedExplicit::All : AllowedExplicit::None, |
1514 | false, |
1515 | false, AllowObjCWritebackConversion, |
1516 | false); |
1517 | return PerformImplicitConversion(From, ToType, ICS, Action); |
1518 | } |
1519 | |
1520 | |
1521 | |
1522 | |
1523 | bool Sema::IsFunctionConversion(QualType FromType, QualType ToType, |
1524 | QualType &ResultTy) { |
1525 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
1526 | return false; |
1527 | |
1528 | |
1529 | |
1530 | |
1531 | |
1532 | |
1533 | |
1534 | |
1535 | CanQualType CanTo = Context.getCanonicalType(ToType); |
1536 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
1537 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
1538 | if (TyClass != CanFrom->getTypeClass()) return false; |
1539 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
1540 | if (TyClass == Type::Pointer) { |
1541 | CanTo = CanTo.castAs<PointerType>()->getPointeeType(); |
1542 | CanFrom = CanFrom.castAs<PointerType>()->getPointeeType(); |
1543 | } else if (TyClass == Type::BlockPointer) { |
1544 | CanTo = CanTo.castAs<BlockPointerType>()->getPointeeType(); |
1545 | CanFrom = CanFrom.castAs<BlockPointerType>()->getPointeeType(); |
1546 | } else if (TyClass == Type::MemberPointer) { |
1547 | auto ToMPT = CanTo.castAs<MemberPointerType>(); |
1548 | auto FromMPT = CanFrom.castAs<MemberPointerType>(); |
1549 | |
1550 | if (ToMPT->getClass() != FromMPT->getClass()) |
1551 | return false; |
1552 | CanTo = ToMPT->getPointeeType(); |
1553 | CanFrom = FromMPT->getPointeeType(); |
1554 | } else { |
1555 | return false; |
1556 | } |
1557 | |
1558 | TyClass = CanTo->getTypeClass(); |
1559 | if (TyClass != CanFrom->getTypeClass()) return false; |
1560 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
1561 | return false; |
1562 | } |
1563 | |
1564 | const auto *FromFn = cast<FunctionType>(CanFrom); |
1565 | FunctionType::ExtInfo FromEInfo = FromFn->getExtInfo(); |
1566 | |
1567 | const auto *ToFn = cast<FunctionType>(CanTo); |
1568 | FunctionType::ExtInfo ToEInfo = ToFn->getExtInfo(); |
1569 | |
1570 | bool Changed = false; |
1571 | |
1572 | |
1573 | if (FromEInfo.getNoReturn() && !ToEInfo.getNoReturn()) { |
1574 | FromFn = Context.adjustFunctionType(FromFn, FromEInfo.withNoReturn(false)); |
1575 | Changed = true; |
1576 | } |
1577 | |
1578 | |
1579 | if (const auto *FromFPT = dyn_cast<FunctionProtoType>(FromFn)) { |
1580 | const auto *ToFPT = cast<FunctionProtoType>(ToFn); |
1581 | if (FromFPT->isNothrow() && !ToFPT->isNothrow()) { |
1582 | FromFn = cast<FunctionType>( |
1583 | Context.getFunctionTypeWithExceptionSpec(QualType(FromFPT, 0), |
1584 | EST_None) |
1585 | .getTypePtr()); |
1586 | Changed = true; |
1587 | } |
1588 | |
1589 | |
1590 | |
1591 | |
1592 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
1593 | bool CanUseToFPT, CanUseFromFPT; |
1594 | if (Context.mergeExtParameterInfo(ToFPT, FromFPT, CanUseToFPT, |
1595 | CanUseFromFPT, NewParamInfos) && |
1596 | CanUseToFPT && !CanUseFromFPT) { |
1597 | FunctionProtoType::ExtProtoInfo ExtInfo = FromFPT->getExtProtoInfo(); |
1598 | ExtInfo.ExtParameterInfos = |
1599 | NewParamInfos.empty() ? nullptr : NewParamInfos.data(); |
1600 | QualType QT = Context.getFunctionType(FromFPT->getReturnType(), |
1601 | FromFPT->getParamTypes(), ExtInfo); |
1602 | FromFn = QT->getAs<FunctionType>(); |
1603 | Changed = true; |
1604 | } |
1605 | } |
1606 | |
1607 | if (!Changed) |
1608 | return false; |
1609 | |
1610 | assert(QualType(FromFn, 0).isCanonical()); |
1611 | if (QualType(FromFn, 0) != CanTo) return false; |
1612 | |
1613 | ResultTy = ToType; |
1614 | return true; |
1615 | } |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | |
1621 | |
1622 | static bool IsVectorConversion(Sema &S, QualType FromType, |
1623 | QualType ToType, ImplicitConversionKind &ICK) { |
1624 | |
1625 | |
1626 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
1627 | return false; |
1628 | |
1629 | |
1630 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) |
1631 | return false; |
1632 | |
1633 | |
1634 | if (ToType->isExtVectorType()) { |
1635 | |
1636 | |
1637 | if (FromType->isExtVectorType()) |
1638 | return false; |
1639 | |
1640 | |
1641 | if (FromType->isArithmeticType()) { |
1642 | ICK = ICK_Vector_Splat; |
1643 | return true; |
1644 | } |
1645 | } |
1646 | |
1647 | if (ToType->isSizelessBuiltinType() || FromType->isSizelessBuiltinType()) |
1648 | if (S.Context.areCompatibleSveTypes(FromType, ToType) || |
1649 | S.Context.areLaxCompatibleSveTypes(FromType, ToType)) { |
1650 | ICK = ICK_SVE_Vector_Conversion; |
1651 | return true; |
1652 | } |
1653 | |
1654 | |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | if (ToType->isVectorType() && FromType->isVectorType()) { |
1662 | if (S.Context.areCompatibleVectorTypes(FromType, ToType) || |
1663 | (S.isLaxVectorConversion(FromType, ToType) && |
1664 | !ToType->hasAttr(attr::ArmMveStrictPolymorphism))) { |
1665 | ICK = ICK_Vector_Conversion; |
1666 | return true; |
1667 | } |
1668 | } |
1669 | |
1670 | return false; |
1671 | } |
1672 | |
1673 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
1674 | bool InOverloadResolution, |
1675 | StandardConversionSequence &SCS, |
1676 | bool CStyle); |
1677 | |
1678 | |
1679 | |
1680 | |
1681 | |
1682 | |
1683 | |
1684 | |
1685 | |
1686 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
1687 | bool InOverloadResolution, |
1688 | StandardConversionSequence &SCS, |
1689 | bool CStyle, |
1690 | bool AllowObjCWritebackConversion) { |
1691 | QualType FromType = From->getType(); |
1692 | |
1693 | |
1694 | SCS.setAsIdentityConversion(); |
1695 | SCS.IncompatibleObjC = false; |
1696 | SCS.setFromType(FromType); |
1697 | SCS.CopyConstructor = nullptr; |
1698 | |
1699 | |
1700 | |
1701 | if (S.getLangOpts().CPlusPlus && |
| 1 | Assuming field 'CPlusPlus' is 0 | |
|
1702 | (FromType->isRecordType() || ToType->isRecordType())) |
1703 | return false; |
1704 | |
1705 | |
1706 | |
1707 | |
1708 | |
1709 | if (FromType == S.Context.OverloadTy) { |
| |
1710 | DeclAccessPair AccessPair; |
1711 | if (FunctionDecl *Fn |
1712 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
1713 | AccessPair)) { |
1714 | |
1715 | |
1716 | FromType = Fn->getType(); |
1717 | SCS.setFromType(FromType); |
1718 | |
1719 | |
1720 | |
1721 | if (!S.Context.hasSameUnqualifiedType( |
1722 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
1723 | QualType resultTy; |
1724 | |
1725 | if (!S.IsFunctionConversion(FromType, |
1726 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
1727 | |
1728 | if (!ToType->isBooleanType()) |
1729 | return false; |
1730 | } |
1731 | |
1732 | |
1733 | |
1734 | |
1735 | |
1736 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
1737 | if (Method && !Method->isStatic()) { |
1738 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
1739 | "Non-unary operator on non-static member address"); |
1740 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
1741 | == UO_AddrOf && |
1742 | "Non-address-of operator on non-static member address"); |
1743 | const Type *ClassType |
1744 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
1745 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
1746 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
1747 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
1748 | UO_AddrOf && |
1749 | "Non-address-of operator for overloaded function expression"); |
1750 | FromType = S.Context.getPointerType(FromType); |
1751 | } |
1752 | |
1753 | |
1754 | |
1755 | |
1756 | assert(S.Context.hasSameType( |
1757 | FromType, |
1758 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
1759 | } else { |
1760 | return false; |
1761 | } |
1762 | } |
1763 | |
1764 | |
1765 | |
1766 | bool argIsLValue = From->isGLValue(); |
1767 | if (argIsLValue && |
| |
1768 | !FromType->isFunctionType() && !FromType->isArrayType() && |
1769 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
1770 | SCS.First = ICK_Lvalue_To_Rvalue; |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
1776 | FromType = Atomic->getValueType(); |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | |
1782 | FromType = FromType.getUnqualifiedType(); |
1783 | } else if (FromType->isArrayType()) { |
| |
1784 | |
1785 | SCS.First = ICK_Array_To_Pointer; |
1786 | |
1787 | |
1788 | |
1789 | |
1790 | FromType = S.Context.getArrayDecayedType(FromType); |
1791 | |
1792 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
1793 | |
1794 | SCS.DeprecatedStringLiteralToCharPtr = true; |
1795 | |
1796 | |
1797 | |
1798 | |
1799 | |
1800 | SCS.Second = ICK_Identity; |
1801 | SCS.Third = ICK_Qualification; |
1802 | SCS.QualificationIncludesObjCLifetime = false; |
1803 | SCS.setAllToTypes(FromType); |
1804 | return true; |
1805 | } |
1806 | } else if (FromType->isFunctionType() && argIsLValue) { |
1807 | |
1808 | SCS.First = ICK_Function_To_Pointer; |
1809 | |
1810 | if (auto *DRE = dyn_cast<DeclRefExpr>(From->IgnoreParenCasts())) |
1811 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) |
1812 | if (!S.checkAddressOfFunctionIsAvailable(FD)) |
1813 | return false; |
1814 | |
1815 | |
1816 | |
1817 | |
1818 | FromType = S.Context.getPointerType(FromType); |
1819 | } else { |
1820 | |
1821 | SCS.First = ICK_Identity; |
1822 | } |
1823 | SCS.setToType(0, FromType); |
1824 | |
1825 | |
1826 | |
1827 | |
1828 | |
1829 | |
1830 | |
1831 | bool IncompatibleObjC = false; |
1832 | ImplicitConversionKind SecondICK = ICK_Identity; |
1833 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
| 5 | | Assuming the condition is false | |
|
| |
1834 | |
1835 | |
1836 | SCS.Second = ICK_Identity; |
1837 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
| |
1838 | |
1839 | SCS.Second = ICK_Integral_Promotion; |
1840 | FromType = ToType.getUnqualifiedType(); |
1841 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
| |
1842 | |
1843 | SCS.Second = ICK_Floating_Promotion; |
1844 | FromType = ToType.getUnqualifiedType(); |
1845 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
| |
1846 | |
1847 | SCS.Second = ICK_Complex_Promotion; |
1848 | FromType = ToType.getUnqualifiedType(); |
1849 | } else if (ToType->isBooleanType() && |
1850 | (FromType->isArithmeticType() || |
1851 | FromType->isAnyPointerType() || |
1852 | FromType->isBlockPointerType() || |
1853 | FromType->isMemberPointerType())) { |
1854 | |
1855 | SCS.Second = ICK_Boolean_Conversion; |
1856 | FromType = S.Context.BoolTy; |
1857 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
| 10 | | Assuming the condition is false | |
|
1858 | ToType->isIntegralType(S.Context)) { |
1859 | |
1860 | SCS.Second = ICK_Integral_Conversion; |
1861 | FromType = ToType.getUnqualifiedType(); |
1862 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
1863 | |
1864 | SCS.Second = ICK_Complex_Conversion; |
1865 | FromType = ToType.getUnqualifiedType(); |
1866 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
1867 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
1868 | |
1869 | SCS.Second = ICK_Complex_Real; |
1870 | FromType = ToType.getUnqualifiedType(); |
1871 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
| 11 | | Assuming the condition is false | |
|
1872 | |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | if (FromType == S.Context.BFloat16Ty || ToType == S.Context.BFloat16Ty) |
1878 | return false; |
1879 | if (&S.Context.getFloatTypeSemantics(FromType) != |
1880 | &S.Context.getFloatTypeSemantics(ToType)) { |
1881 | bool Float128AndLongDouble = ((FromType == S.Context.Float128Ty && |
1882 | ToType == S.Context.LongDoubleTy) || |
1883 | (FromType == S.Context.LongDoubleTy && |
1884 | ToType == S.Context.Float128Ty)); |
1885 | if (Float128AndLongDouble && |
1886 | (&S.Context.getFloatTypeSemantics(S.Context.LongDoubleTy) == |
1887 | &llvm::APFloat::PPCDoubleDouble())) |
1888 | return false; |
1889 | } |
1890 | |
1891 | SCS.Second = ICK_Floating_Conversion; |
1892 | FromType = ToType.getUnqualifiedType(); |
1893 | } else if ((FromType->isRealFloatingType() && |
| 12 | | Assuming the condition is false | |
|
1894 | ToType->isIntegralType(S.Context)) || |
1895 | (FromType->isIntegralOrUnscopedEnumerationType() && |
| 13 | | Assuming the condition is false | |
|
1896 | ToType->isRealFloatingType())) { |
1897 | |
1898 | if (FromType->isBFloat16Type() || ToType->isBFloat16Type()) |
1899 | return false; |
1900 | |
1901 | |
1902 | SCS.Second = ICK_Floating_Integral; |
1903 | FromType = ToType.getUnqualifiedType(); |
1904 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
| |
1905 | SCS.Second = ICK_Block_Pointer_Conversion; |
1906 | } else if (AllowObjCWritebackConversion && |
| 15 | | Assuming 'AllowObjCWritebackConversion' is false | |
|
1907 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
1908 | SCS.Second = ICK_Writeback_Conversion; |
1909 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 16 | | Calling 'Sema::IsPointerConversion' | |
|
1910 | FromType, IncompatibleObjC)) { |
1911 | |
1912 | SCS.Second = ICK_Pointer_Conversion; |
1913 | SCS.IncompatibleObjC = IncompatibleObjC; |
1914 | FromType = FromType.getUnqualifiedType(); |
1915 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
1916 | InOverloadResolution, FromType)) { |
1917 | |
1918 | SCS.Second = ICK_Pointer_Member; |
1919 | } else if (IsVectorConversion(S, FromType, ToType, SecondICK)) { |
1920 | SCS.Second = SecondICK; |
1921 | FromType = ToType.getUnqualifiedType(); |
1922 | } else if (!S.getLangOpts().CPlusPlus && |
1923 | S.Context.typesAreCompatible(ToType, FromType)) { |
1924 | |
1925 | SCS.Second = ICK_Compatible_Conversion; |
1926 | FromType = ToType.getUnqualifiedType(); |
1927 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
1928 | InOverloadResolution, |
1929 | SCS, CStyle)) { |
1930 | SCS.Second = ICK_TransparentUnionConversion; |
1931 | FromType = ToType; |
1932 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
1933 | CStyle)) { |
1934 | |
1935 | |
1936 | return true; |
1937 | } else if (ToType->isEventT() && |
1938 | From->isIntegerConstantExpr(S.getASTContext()) && |
1939 | From->EvaluateKnownConstInt(S.getASTContext()) == 0) { |
1940 | SCS.Second = ICK_Zero_Event_Conversion; |
1941 | FromType = ToType; |
1942 | } else if (ToType->isQueueT() && |
1943 | From->isIntegerConstantExpr(S.getASTContext()) && |
1944 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
1945 | SCS.Second = ICK_Zero_Queue_Conversion; |
1946 | FromType = ToType; |
1947 | } else if (ToType->isSamplerT() && |
1948 | From->isIntegerConstantExpr(S.getASTContext())) { |
1949 | SCS.Second = ICK_Compatible_Conversion; |
1950 | FromType = ToType; |
1951 | } else { |
1952 | |
1953 | SCS.Second = ICK_Identity; |
1954 | } |
1955 | SCS.setToType(1, FromType); |
1956 | |
1957 | |
1958 | |
1959 | bool ObjCLifetimeConversion; |
1960 | if (S.IsFunctionConversion(FromType, ToType, FromType)) { |
1961 | |
1962 | |
1963 | SCS.Third = ICK_Function_Conversion; |
1964 | } else if (S.IsQualificationConversion(FromType, ToType, CStyle, |
1965 | ObjCLifetimeConversion)) { |
1966 | SCS.Third = ICK_Qualification; |
1967 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
1968 | FromType = ToType; |
1969 | } else { |
1970 | |
1971 | SCS.Third = ICK_Identity; |
1972 | } |
1973 | |
1974 | |
1975 | |
1976 | |
1977 | |
1978 | QualType CanonFrom = S.Context.getCanonicalType(FromType); |
1979 | QualType CanonTo = S.Context.getCanonicalType(ToType); |
1980 | if (CanonFrom.getLocalUnqualifiedType() |
1981 | == CanonTo.getLocalUnqualifiedType() && |
1982 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
1983 | FromType = ToType; |
1984 | CanonFrom = CanonTo; |
1985 | } |
1986 | |
1987 | SCS.setToType(2, FromType); |
1988 | |
1989 | if (CanonFrom == CanonTo) |
1990 | return true; |
1991 | |
1992 | |
1993 | |
1994 | if (S.getLangOpts().CPlusPlus || !InOverloadResolution) |
1995 | return false; |
1996 | |
1997 | ExprResult ER = ExprResult{From}; |
1998 | Sema::AssignConvertType Conv = |
1999 | S.CheckSingleAssignmentConstraints(ToType, ER, |
2000 | false, |
2001 | false, |
2002 | false); |
2003 | ImplicitConversionKind SecondConv; |
2004 | switch (Conv) { |
2005 | case Sema::Compatible: |
2006 | SecondConv = ICK_C_Only_Conversion; |
2007 | break; |
2008 | |
2009 | |
2010 | |
2011 | case Sema::CompatiblePointerDiscardsQualifiers: |
2012 | case Sema::IncompatiblePointer: |
2013 | case Sema::IncompatiblePointerSign: |
2014 | SecondConv = ICK_Incompatible_Pointer_Conversion; |
2015 | break; |
2016 | default: |
2017 | return false; |
2018 | } |
2019 | |
2020 | |
2021 | |
2022 | |
2023 | SCS.Second = SecondConv; |
2024 | SCS.setToType(1, ToType); |
2025 | |
2026 | |
2027 | |
2028 | |
2029 | |
2030 | SCS.Third = ICK_Identity; |
2031 | SCS.setToType(2, ToType); |
2032 | return true; |
2033 | } |
2034 | |
2035 | static bool |
2036 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
2037 | QualType &ToType, |
2038 | bool InOverloadResolution, |
2039 | StandardConversionSequence &SCS, |
2040 | bool CStyle) { |
2041 | |
2042 | const RecordType *UT = ToType->getAsUnionType(); |
2043 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
2044 | return false; |
2045 | |
2046 | RecordDecl *UD = UT->getDecl(); |
2047 | |
2048 | for (const auto *it : UD->fields()) { |
2049 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
2050 | CStyle, false)) { |
2051 | ToType = it->getType(); |
2052 | return true; |
2053 | } |
2054 | } |
2055 | return false; |
2056 | } |
2057 | |
2058 | |
2059 | |
2060 | |
2061 | |
2062 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
2063 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
2064 | |
2065 | if (!To) { |
2066 | return false; |
2067 | } |
2068 | |
2069 | |
2070 | |
2071 | |
2072 | |
2073 | |
2074 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
2075 | !FromType->isEnumeralType()) { |
2076 | if ( |
2077 | (FromType->isSignedIntegerType() || |
2078 | |
2079 | |
2080 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType))) { |
2081 | return To->getKind() == BuiltinType::Int; |
2082 | } |
2083 | |
2084 | return To->getKind() == BuiltinType::UInt; |
2085 | } |
2086 | |
2087 | |
2088 | |
2089 | |
2090 | |
2091 | |
2092 | |
2093 | |
2094 | |
2095 | |
2096 | |
2097 | |
2098 | |
2099 | |
2100 | |
2101 | |
2102 | |
2103 | |
2104 | |
2105 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
2106 | |
2107 | |
2108 | if (FromEnumType->getDecl()->isScoped()) |
2109 | return false; |
2110 | |
2111 | |
2112 | |
2113 | |
2114 | |
2115 | if (FromEnumType->getDecl()->isFixed()) { |
2116 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
2117 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
2118 | IsIntegralPromotion(nullptr, Underlying, ToType); |
2119 | } |
2120 | |
2121 | |
2122 | if (ToType->isIntegerType() && |
2123 | isCompleteType(From->getBeginLoc(), FromType)) |
2124 | return Context.hasSameUnqualifiedType( |
2125 | ToType, FromEnumType->getDecl()->getPromotionType()); |
2126 | |
2127 | |
2128 | |
2129 | |
2130 | |
2131 | |
2132 | if (getLangOpts().CPlusPlus) |
2133 | return false; |
2134 | } |
2135 | |
2136 | |
2137 | |
2138 | |
2139 | |
2140 | |
2141 | |
2142 | |
2143 | |
2144 | |
2145 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
2146 | ToType->isIntegerType()) { |
2147 | |
2148 | |
2149 | bool FromIsSigned = FromType->isSignedIntegerType(); |
2150 | uint64_t FromSize = Context.getTypeSize(FromType); |
2151 | |
2152 | |
2153 | |
2154 | QualType PromoteTypes[6] = { |
2155 | Context.IntTy, Context.UnsignedIntTy, |
2156 | Context.LongTy, Context.UnsignedLongTy , |
2157 | Context.LongLongTy, Context.UnsignedLongLongTy |
2158 | }; |
2159 | for (int Idx = 0; Idx < 6; ++Idx) { |
2160 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
2161 | if (FromSize < ToSize || |
2162 | (FromSize == ToSize && |
2163 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
2164 | |
2165 | |
2166 | |
2167 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
2168 | } |
2169 | } |
2170 | } |
2171 | |
2172 | |
2173 | |
2174 | |
2175 | |
2176 | |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | |
2184 | |
2185 | |
2186 | if (From) { |
2187 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
2188 | Optional<llvm::APSInt> BitWidth; |
2189 | if (FromType->isIntegralType(Context) && |
2190 | (BitWidth = |
2191 | MemberDecl->getBitWidth()->getIntegerConstantExpr(Context))) { |
2192 | llvm::APSInt ToSize(BitWidth->getBitWidth(), BitWidth->isUnsigned()); |
2193 | ToSize = Context.getTypeSize(ToType); |
2194 | |
2195 | |
2196 | if (*BitWidth < ToSize || |
2197 | (FromType->isSignedIntegerType() && *BitWidth <= ToSize)) { |
2198 | return To->getKind() == BuiltinType::Int; |
2199 | } |
2200 | |
2201 | |
2202 | |
2203 | if (FromType->isUnsignedIntegerType() && *BitWidth <= ToSize) { |
2204 | return To->getKind() == BuiltinType::UInt; |
2205 | } |
2206 | |
2207 | return false; |
2208 | } |
2209 | } |
2210 | } |
2211 | |
2212 | |
2213 | |
2214 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
2215 | return true; |
2216 | } |
2217 | |
2218 | return false; |
2219 | } |
2220 | |
2221 | |
2222 | |
2223 | |
2224 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
2225 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
2226 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
2227 | |
2228 | |
2229 | if (FromBuiltin->getKind() == BuiltinType::Float && |
2230 | ToBuiltin->getKind() == BuiltinType::Double) |
2231 | return true; |
2232 | |
2233 | |
2234 | |
2235 | |
2236 | if (!getLangOpts().CPlusPlus && |
2237 | (FromBuiltin->getKind() == BuiltinType::Float || |
2238 | FromBuiltin->getKind() == BuiltinType::Double) && |
2239 | (ToBuiltin->getKind() == BuiltinType::LongDouble || |
2240 | ToBuiltin->getKind() == BuiltinType::Float128)) |
2241 | return true; |
2242 | |
2243 | |
2244 | if (!getLangOpts().NativeHalfType && |
2245 | FromBuiltin->getKind() == BuiltinType::Half && |
2246 | ToBuiltin->getKind() == BuiltinType::Float) |
2247 | return true; |
2248 | } |
2249 | |
2250 | return false; |
2251 | } |
2252 | |
2253 | |
2254 | |
2255 | |
2256 | |
2257 | |
2258 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
2259 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
2260 | if (!FromComplex) |
2261 | return false; |
2262 | |
2263 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
2264 | if (!ToComplex) |
2265 | return false; |
2266 | |
2267 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
2268 | ToComplex->getElementType()) || |
2269 | IsIntegralPromotion(nullptr, FromComplex->getElementType(), |
2270 | ToComplex->getElementType()); |
2271 | } |
2272 | |
2273 | |
2274 | |
2275 | |
2276 | |
2277 | |
2278 | |
2279 | static QualType |
2280 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
2281 | QualType ToPointee, QualType ToType, |
2282 | ASTContext &Context, |
2283 | bool StripObjCLifetime = false) { |
2284 | assert((FromPtr->getTypeClass() == Type::Pointer || |
2285 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
2286 | "Invalid similarly-qualified pointer type"); |
2287 | |
2288 | |
2289 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
| 77 | | Calling 'Type::isObjCIdType' | |
|
| 81 | | Returning from 'Type::isObjCIdType' | |
|
| 82 | | Calling 'Type::isObjCQualifiedIdType' | |
|
| 86 | | Returning from 'Type::isObjCQualifiedIdType' | |
|
| |
2290 | return ToType.getUnqualifiedType(); |
2291 | |
2292 | QualType CanonFromPointee |
2293 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 88 | | Called C++ object pointer is null |
|
2294 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
2295 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
2296 | |
2297 | if (StripObjCLifetime) |
2298 | Quals.removeObjCLifetime(); |
2299 | |
2300 | |
2301 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
2302 | |
2303 | if (!ToType.isNull()) |
2304 | return ToType.getUnqualifiedType(); |
2305 | |
2306 | |
2307 | |
2308 | if (isa<ObjCObjectPointerType>(ToType)) |
2309 | return Context.getObjCObjectPointerType(ToPointee); |
2310 | return Context.getPointerType(ToPointee); |
2311 | } |
2312 | |
2313 | |
2314 | QualType QualifiedCanonToPointee |
2315 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
2316 | |
2317 | if (isa<ObjCObjectPointerType>(ToType)) |
2318 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
2319 | return Context.getPointerType(QualifiedCanonToPointee); |
2320 | } |
2321 | |
2322 | static bool isNullPointerConstantForConversion(Expr *Expr, |
2323 | bool InOverloadResolution, |
2324 | ASTContext &Context) { |
2325 | |
2326 | |
2327 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 51 | | Assuming the condition is false | |
|
| |
2328 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
2329 | return !InOverloadResolution; |
2330 | |
2331 | return Expr->isNullPointerConstant(Context, |
| 55 | | Returning value, which participates in a condition later | |
|
2332 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 53 | | Assuming 'InOverloadResolution' is false | |
|
| |
2333 | : Expr::NPC_ValueDependentIsNull); |
2334 | } |
2335 | |
2336 | |
2337 | |
2338 | |
2339 | |
2340 | |
2341 | |
2342 | |
2343 | |
2344 | |
2345 | |
2346 | |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | |
2352 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
2353 | bool InOverloadResolution, |
2354 | QualType& ConvertedType, |
2355 | bool &IncompatibleObjC) { |
2356 | IncompatibleObjC = false; |
2357 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 17 | | Calling 'Sema::isObjCPointerConversion' | |
|
| 28 | | Returning from 'Sema::isObjCPointerConversion' | |
|
| |
2358 | IncompatibleObjC)) |
2359 | return true; |
2360 | |
2361 | |
2362 | if (ToType->isObjCObjectPointerType() && |
| 30 | | Calling 'Type::isObjCObjectPointerType' | |
|
| 33 | | Returning from 'Type::isObjCObjectPointerType' | |
|
2363 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2364 | ConvertedType = ToType; |
2365 | return true; |
2366 | } |
2367 | |
2368 | |
2369 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
| |
2370 | ToType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
2371 | ConvertedType = ToType; |
2372 | return true; |
2373 | } |
2374 | |
2375 | |
2376 | if (ToType->isBlockPointerType() && |
| 35 | | Calling 'Type::isBlockPointerType' | |
|
| 38 | | Returning from 'Type::isBlockPointerType' | |
|
2377 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2378 | ConvertedType = ToType; |
2379 | return true; |
2380 | } |
2381 | |
2382 | |
2383 | |
2384 | if (ToType->isNullPtrType() && |
| 39 | | Calling 'Type::isNullPtrType' | |
|
| 46 | | Returning from 'Type::isNullPtrType' | |
|
2385 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2386 | ConvertedType = ToType; |
2387 | return true; |
2388 | } |
2389 | |
2390 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
| 47 | | Assuming the object is a 'PointerType' | |
|
2391 | if (!ToTypePtr) |
| 48 | | Assuming 'ToTypePtr' is non-null | |
|
| |
2392 | return false; |
2393 | |
2394 | |
2395 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| 50 | | Calling 'isNullPointerConstantForConversion' | |
|
| 56 | | Returning from 'isNullPointerConstantForConversion' | |
|
| 57 | | Assuming the condition is false | |
|
| |
2396 | ConvertedType = ToType; |
2397 | return true; |
2398 | } |
2399 | |
2400 | |
2401 | |
2402 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
2403 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
| 59 | | Calling 'Type::isObjCObjectPointerType' | |
|
| 62 | | Returning from 'Type::isObjCObjectPointerType' | |
|
| 63 | | Calling 'Type::isVoidType' | |
|
| 71 | | Returning from 'Type::isVoidType' | |
|
| |
2404 | !getLangOpts().ObjCAutoRefCount) { |
| 72 | | Assuming field 'ObjCAutoRefCount' is 0 | |
|
2405 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 76 | | Calling 'BuildSimilarlyQualifiedPointerType' | |
|
2406 | FromType->getAs<ObjCObjectPointerType>(), |
| 74 | | Assuming the object is not a 'ObjCObjectPointerType' | |
|
| 75 | | Passing null pointer value via 1st parameter 'FromPtr' | |
|
2407 | ToPointeeType, |
2408 | ToType, Context); |
2409 | return true; |
2410 | } |
2411 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
2412 | if (!FromTypePtr) |
2413 | return false; |
2414 | |
2415 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
2416 | |
2417 | |
2418 | |
2419 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
2420 | return false; |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | if (FromPointeeType->isIncompleteOrObjectType() && |
2426 | ToPointeeType->isVoidType()) { |
2427 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2428 | ToPointeeType, |
2429 | ToType, Context, |
2430 | true); |
2431 | return true; |
2432 | } |
2433 | |
2434 | |
2435 | if (getLangOpts().MSVCCompat && FromPointeeType->isFunctionType() && |
2436 | ToPointeeType->isVoidType()) { |
2437 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2438 | ToPointeeType, |
2439 | ToType, Context); |
2440 | return true; |
2441 | } |
2442 | |
2443 | |
2444 | |
2445 | if (!getLangOpts().CPlusPlus && |
2446 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
2447 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2448 | ToPointeeType, |
2449 | ToType, Context); |
2450 | return true; |
2451 | } |
2452 | |
2453 | |
2454 | |
2455 | |
2456 | |
2457 | |
2458 | |
2459 | |
2460 | |
2461 | |
2462 | |
2463 | |
2464 | |
2465 | |
2466 | if (getLangOpts().CPlusPlus && FromPointeeType->isRecordType() && |
2467 | ToPointeeType->isRecordType() && |
2468 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
2469 | IsDerivedFrom(From->getBeginLoc(), FromPointeeType, ToPointeeType)) { |
2470 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2471 | ToPointeeType, |
2472 | ToType, Context); |
2473 | return true; |
2474 | } |
2475 | |
2476 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
2477 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
2478 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2479 | ToPointeeType, |
2480 | ToType, Context); |
2481 | return true; |
2482 | } |
2483 | |
2484 | return false; |
2485 | } |
2486 | |
2487 | |
2488 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
2489 | Qualifiers TQs = T.getQualifiers(); |
2490 | |
2491 | |
2492 | if (TQs == Qs) |
2493 | return T; |
2494 | |
2495 | if (Qs.compatiblyIncludes(TQs)) |
2496 | return Context.getQualifiedType(T, Qs); |
2497 | |
2498 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
2499 | } |
2500 | |
2501 | |
2502 | |
2503 | |
2504 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
2505 | QualType& ConvertedType, |
2506 | bool &IncompatibleObjC) { |
2507 | if (!getLangOpts().ObjC) |
| 18 | | Assuming field 'ObjC' is not equal to 0 | |
|
| |
2508 | return false; |
2509 | |
2510 | |
2511 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
2512 | |
2513 | |
2514 | const ObjCObjectPointerType* ToObjCPtr = |
2515 | ToType->getAs<ObjCObjectPointerType>(); |
| 20 | | Assuming the object is not a 'ObjCObjectPointerType' | |
|
2516 | const ObjCObjectPointerType *FromObjCPtr = |
2517 | FromType->getAs<ObjCObjectPointerType>(); |
| 21 | | Assuming the object is not a 'ObjCObjectPointerType' | |
|
2518 | |
2519 | if (ToObjCPtr && FromObjCPtr) { |
2520 | |
2521 | |
2522 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
2523 | FromObjCPtr->getPointeeType())) |
2524 | return false; |
2525 | |
2526 | |
2527 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
2528 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
2529 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
2530 | if (getLangOpts().CPlusPlus && LHS && RHS && |
2531 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
2532 | FromObjCPtr->getPointeeType())) |
2533 | return false; |
2534 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
2535 | ToObjCPtr->getPointeeType(), |
2536 | ToType, Context); |
2537 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2538 | return true; |
2539 | } |
2540 | |
2541 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
2542 | |
2543 | |
2544 | |
2545 | IncompatibleObjC = true; |
2546 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
2547 | ToObjCPtr->getPointeeType(), |
2548 | ToType, Context); |
2549 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2550 | return true; |
2551 | } |
2552 | } |
2553 | |
2554 | QualType ToPointeeType; |
2555 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
| 22 | | Assuming the object is not a 'PointerType' | |
|
| |
2556 | ToPointeeType = ToCPtr->getPointeeType(); |
2557 | else if (const BlockPointerType *ToBlockPtr = |
| |
2558 | ToType->getAs<BlockPointerType>()) { |
| 24 | | Assuming the object is not a 'BlockPointerType' | |
|
2559 | |
2560 | |
2561 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
2562 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2563 | return true; |
2564 | } |
2565 | ToPointeeType = ToBlockPtr->getPointeeType(); |
2566 | } |
2567 | else if (FromType->getAs<BlockPointerType>() && |
| 26 | | Assuming the object is not a 'BlockPointerType' | |
|
2568 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
2569 | |
2570 | |
2571 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2572 | return true; |
2573 | } |
2574 | else |
2575 | return false; |
| 27 | | Returning zero, which participates in a condition later | |
|
2576 | |
2577 | QualType FromPointeeType; |
2578 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
2579 | FromPointeeType = FromCPtr->getPointeeType(); |
2580 | else if (const BlockPointerType *FromBlockPtr = |
2581 | FromType->getAs<BlockPointerType>()) |
2582 | FromPointeeType = FromBlockPtr->getPointeeType(); |
2583 | else |
2584 | return false; |
2585 | |
2586 | |
2587 | |
2588 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
2589 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
2590 | IncompatibleObjC)) { |
2591 | |
2592 | IncompatibleObjC = true; |
2593 | ConvertedType = Context.getPointerType(ConvertedType); |
2594 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2595 | return true; |
2596 | } |
2597 | |
2598 | |
2599 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
2600 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
2601 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
2602 | IncompatibleObjC)) { |
2603 | |
2604 | ConvertedType = Context.getPointerType(ConvertedType); |
2605 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2606 | return true; |
2607 | } |
2608 | |
2609 | |
2610 | |
2611 | |
2612 | |
2613 | const FunctionProtoType *FromFunctionType |
2614 | = FromPointeeType->getAs<FunctionProtoType>(); |
2615 | const FunctionProtoType *ToFunctionType |
2616 | = ToPointeeType->getAs<FunctionProtoType>(); |
2617 | if (FromFunctionType && ToFunctionType) { |
2618 | |
2619 | |
2620 | if (Context.getCanonicalType(FromPointeeType) |
2621 | == Context.getCanonicalType(ToPointeeType)) |
2622 | return false; |
2623 | |
2624 | |
2625 | |
2626 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
2627 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
2628 | FromFunctionType->getMethodQuals() != ToFunctionType->getMethodQuals()) |
2629 | return false; |
2630 | |
2631 | bool HasObjCConversion = false; |
2632 | if (Context.getCanonicalType(FromFunctionType->getReturnType()) == |
2633 | Context.getCanonicalType(ToFunctionType->getReturnType())) { |
2634 | |
2635 | } else if (isObjCPointerConversion(FromFunctionType->getReturnType(), |
2636 | ToFunctionType->getReturnType(), |
2637 | ConvertedType, IncompatibleObjC)) { |
2638 | |
2639 | HasObjCConversion = true; |
2640 | } else { |
2641 | |
2642 | return false; |
2643 | } |
2644 | |
2645 | |
2646 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
2647 | ArgIdx != NumArgs; ++ArgIdx) { |
2648 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
2649 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
2650 | if (Context.getCanonicalType(FromArgType) |
2651 | == Context.getCanonicalType(ToArgType)) { |
2652 | |
2653 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
2654 | ConvertedType, IncompatibleObjC)) { |
2655 | |
2656 | HasObjCConversion = true; |
2657 | } else { |
2658 | |
2659 | return false; |
2660 | } |
2661 | } |
2662 | |
2663 | if (HasObjCConversion) { |
2664 | |
2665 | |
2666 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2667 | IncompatibleObjC = true; |
2668 | return true; |
2669 | } |
2670 | } |
2671 | |
2672 | return false; |
2673 | } |
2674 | |
2675 | |
2676 | |
2677 | |
2678 | |
2679 | |
2680 | |
2681 | |
2682 | |
2683 | |
2684 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
2685 | QualType &ConvertedType) { |
2686 | if (!getLangOpts().ObjCAutoRefCount || |
2687 | Context.hasSameUnqualifiedType(FromType, ToType)) |
2688 | return false; |
2689 | |
2690 | |
2691 | QualType ToPointee; |
2692 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
2693 | ToPointee = ToPointer->getPointeeType(); |
2694 | else |
2695 | return false; |
2696 | |
2697 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
2698 | if (!ToPointee->isObjCLifetimeType() || |
2699 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
2700 | !ToQuals.withoutObjCLifetime().empty()) |
2701 | return false; |
2702 | |
2703 | |
2704 | QualType FromPointee; |
2705 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
2706 | FromPointee = FromPointer->getPointeeType(); |
2707 | else |
2708 | return false; |
2709 | |
2710 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
2711 | if (!FromPointee->isObjCLifetimeType() || |
2712 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
2713 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
2714 | return false; |
2715 | |
2716 | |
2717 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
2718 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
2719 | return false; |
2720 | |
2721 | |
2722 | |
2723 | |
2724 | FromPointee = FromPointee.getUnqualifiedType(); |
2725 | |
2726 | |
2727 | ToPointee = ToPointee.getUnqualifiedType(); |
2728 | bool IncompatibleObjC; |
2729 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
2730 | FromPointee = ToPointee; |
2731 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
2732 | IncompatibleObjC)) |
2733 | return false; |
2734 | |
2735 | |
2736 | |
2737 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
2738 | ConvertedType = Context.getPointerType(FromPointee); |
2739 | return true; |
2740 | } |
2741 | |
2742 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
2743 | QualType& ConvertedType) { |
2744 | QualType ToPointeeType; |
2745 | if (const BlockPointerType *ToBlockPtr = |
2746 | ToType->getAs<BlockPointerType>()) |
2747 | ToPointeeType = ToBlockPtr->getPointeeType(); |
2748 | else |
2749 | return false; |
2750 | |
2751 | QualType FromPointeeType; |
2752 | if (const BlockPointerType *FromBlockPtr = |
2753 | FromType->getAs<BlockPointerType>()) |
2754 | FromPointeeType = FromBlockPtr->getPointeeType(); |
2755 | else |
2756 | return false; |
2757 | |
2758 | |
2759 | |
2760 | |
2761 | const FunctionProtoType *FromFunctionType |
2762 | = FromPointeeType->getAs<FunctionProtoType>(); |
2763 | const FunctionProtoType *ToFunctionType |
2764 | = ToPointeeType->getAs<FunctionProtoType>(); |
2765 | |
2766 | if (!FromFunctionType || !ToFunctionType) |
2767 | return false; |
2768 | |
2769 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
2770 | return true; |
2771 | |
2772 | |
2773 | |
2774 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
2775 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
2776 | return false; |
2777 | |
2778 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
2779 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
2780 | if (FromEInfo != ToEInfo) |
2781 | return false; |
2782 | |
2783 | bool IncompatibleObjC = false; |
2784 | if (Context.hasSameType(FromFunctionType->getReturnType(), |
2785 | ToFunctionType->getReturnType())) { |
2786 | |
2787 | } else { |
2788 | QualType RHS = FromFunctionType->getReturnType(); |
2789 | QualType LHS = ToFunctionType->getReturnType(); |
2790 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
2791 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
2792 | LHS = LHS.getUnqualifiedType(); |
2793 | |
2794 | if (Context.hasSameType(RHS,LHS)) { |
2795 | |
2796 | } else if (isObjCPointerConversion(RHS, LHS, |
2797 | ConvertedType, IncompatibleObjC)) { |
2798 | if (IncompatibleObjC) |
2799 | return false; |
2800 | |
2801 | } |
2802 | else |
2803 | return false; |
2804 | } |
2805 | |
2806 | |
2807 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
2808 | ArgIdx != NumArgs; ++ArgIdx) { |
2809 | IncompatibleObjC = false; |
2810 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
2811 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
2812 | if (Context.hasSameType(FromArgType, ToArgType)) { |
2813 | |
2814 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
2815 | ConvertedType, IncompatibleObjC)) { |
2816 | if (IncompatibleObjC) |
2817 | return false; |
2818 | |
2819 | } else |
2820 | |
2821 | return false; |
2822 | } |
2823 | |
2824 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
2825 | bool CanUseToFPT, CanUseFromFPT; |
2826 | if (!Context.mergeExtParameterInfo(ToFunctionType, FromFunctionType, |
2827 | CanUseToFPT, CanUseFromFPT, |
2828 | NewParamInfos)) |
2829 | return false; |
2830 | |
2831 | ConvertedType = ToType; |
2832 | return true; |
2833 | } |
2834 | |
2835 | enum { |
2836 | ft_default, |
2837 | ft_different_class, |
2838 | ft_parameter_arity, |
2839 | ft_parameter_mismatch, |
2840 | ft_return_type, |
2841 | ft_qualifer_mismatch, |
2842 | ft_noexcept |
2843 | }; |
2844 | |
2845 | |
2846 | |
2847 | static const FunctionProtoType *tryGetFunctionProtoType(QualType FromType) { |
2848 | if (auto *FPT = FromType->getAs<FunctionProtoType>()) |
2849 | return FPT; |
2850 | |
2851 | if (auto *MPT = FromType->getAs<MemberPointerType>()) |
2852 | return MPT->getPointeeType()->getAs<FunctionProtoType>(); |
2853 | |
2854 | return nullptr; |
2855 | } |
2856 | |
2857 | |
2858 | |
2859 | |
2860 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
2861 | QualType FromType, QualType ToType) { |
2862 | |
2863 | if (FromType.isNull() || ToType.isNull()) { |
2864 | PDiag << ft_default; |
2865 | return; |
2866 | } |
2867 | |
2868 | |
2869 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
2870 | const auto *FromMember = FromType->castAs<MemberPointerType>(), |
2871 | *ToMember = ToType->castAs<MemberPointerType>(); |
2872 | if (!Context.hasSameType(FromMember->getClass(), ToMember->getClass())) { |
2873 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
2874 | << QualType(FromMember->getClass(), 0); |
2875 | return; |
2876 | } |
2877 | FromType = FromMember->getPointeeType(); |
2878 | ToType = ToMember->getPointeeType(); |
2879 | } |
2880 | |
2881 | if (FromType->isPointerType()) |
2882 | FromType = FromType->getPointeeType(); |
2883 | if (ToType->isPointerType()) |
2884 | ToType = ToType->getPointeeType(); |
2885 | |
2886 | |
2887 | FromType = FromType.getNonReferenceType(); |
2888 | ToType = ToType.getNonReferenceType(); |
2889 | |
2890 | |
2891 | if (FromType->isInstantiationDependentType() && |
2892 | !FromType->getAs<TemplateSpecializationType>()) { |
2893 | PDiag << ft_default; |
2894 | return; |
2895 | } |
2896 | |
2897 | |
2898 | if (Context.hasSameType(FromType, ToType)) { |
2899 | PDiag << ft_default; |
2900 | return; |
2901 | } |
2902 | |
2903 | const FunctionProtoType *FromFunction = tryGetFunctionProtoType(FromType), |
2904 | *ToFunction = tryGetFunctionProtoType(ToType); |
2905 | |
2906 | |
2907 | if (!FromFunction || !ToFunction) { |
2908 | PDiag << ft_default; |
2909 | return; |
2910 | } |
2911 | |
2912 | if (FromFunction->getNumParams() != ToFunction->getNumParams()) { |
2913 | PDiag << ft_parameter_arity << ToFunction->getNumParams() |
2914 | << FromFunction->getNumParams(); |
2915 | return; |
2916 | } |
2917 | |
2918 | |
2919 | unsigned ArgPos; |
2920 | if (!FunctionParamTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
2921 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
2922 | << ToFunction->getParamType(ArgPos) |
2923 | << FromFunction->getParamType(ArgPos); |
2924 | return; |
2925 | } |
2926 | |
2927 | |
2928 | if (!Context.hasSameType(FromFunction->getReturnType(), |
2929 | ToFunction->getReturnType())) { |
2930 | PDiag << ft_return_type << ToFunction->getReturnType() |
2931 | << FromFunction->getReturnType(); |
2932 | return; |
2933 | } |
2934 | |
2935 | if (FromFunction->getMethodQuals() != ToFunction->getMethodQuals()) { |
2936 | PDiag << ft_qualifer_mismatch << ToFunction->getMethodQuals() |
2937 | << FromFunction->getMethodQuals(); |
2938 | return; |
2939 | } |
2940 | |
2941 | |
2942 | |
2943 | if (cast<FunctionProtoType>(FromFunction->getCanonicalTypeUnqualified()) |
2944 | ->isNothrow() != |
2945 | cast<FunctionProtoType>(ToFunction->getCanonicalTypeUnqualified()) |
2946 | ->isNothrow()) { |
2947 | PDiag << ft_noexcept; |
2948 | return; |
2949 | } |
2950 | |
2951 | |
2952 | PDiag << ft_default; |
2953 | } |
2954 | |
2955 | |
2956 | |
2957 | |
2958 | |
2959 | bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, |
2960 | const FunctionProtoType *NewType, |
2961 | unsigned *ArgPos) { |
2962 | for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), |
2963 | N = NewType->param_type_begin(), |
2964 | E = OldType->param_type_end(); |
2965 | O && (O != E); ++O, ++N) { |
2966 | |
2967 | |
2968 | QualType Old = Context.removePtrSizeAddrSpace(O->getUnqualifiedType()); |
2969 | QualType New = Context.removePtrSizeAddrSpace(N->getUnqualifiedType()); |
2970 | |
2971 | if (!Context.hasSameType(Old, New)) { |
2972 | if (ArgPos) |
2973 | *ArgPos = O - OldType->param_type_begin(); |
2974 | return false; |
2975 | } |
2976 | } |
2977 | return true; |
2978 | } |
2979 | |
2980 | |
2981 | |
2982 | |
2983 | |
2984 | |
2985 | |
2986 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
2987 | CastKind &Kind, |
2988 | CXXCastPath& BasePath, |
2989 | bool IgnoreBaseAccess, |
2990 | bool Diagnose) { |
2991 | QualType FromType = From->getType(); |
2992 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
2993 | |
2994 | Kind = CK_BitCast; |
2995 | |
2996 | if (Diagnose && !IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
2997 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
2998 | Expr::NPCK_ZeroExpression) { |
2999 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
3000 | DiagRuntimeBehavior(From->getExprLoc(), From, |
3001 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
3002 | << ToType << From->getSourceRange()); |
3003 | else if (!isUnevaluatedContext()) |
3004 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
3005 | << ToType << From->getSourceRange(); |
3006 | } |
3007 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
3008 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
3009 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
3010 | ToPointeeType = ToPtrType->getPointeeType(); |
3011 | |
3012 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
3013 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
3014 | |
3015 | |
3016 | unsigned InaccessibleID = 0; |
3017 | unsigned AmbiguousID = 0; |
3018 | if (Diagnose) { |
3019 | InaccessibleID = diag::err_upcast_to_inaccessible_base; |
3020 | AmbiguousID = diag::err_ambiguous_derived_to_base_conv; |
3021 | } |
3022 | if (CheckDerivedToBaseConversion( |
3023 | FromPointeeType, ToPointeeType, InaccessibleID, AmbiguousID, |
3024 | From->getExprLoc(), From->getSourceRange(), DeclarationName(), |
3025 | &BasePath, IgnoreBaseAccess)) |
3026 | return true; |
3027 | |
3028 | |
3029 | Kind = CK_DerivedToBase; |
3030 | } |
3031 | |
3032 | if (Diagnose && !IsCStyleOrFunctionalCast && |
3033 | FromPointeeType->isFunctionType() && ToPointeeType->isVoidType()) { |
3034 | assert(getLangOpts().MSVCCompat && |
3035 | "this should only be possible with MSVCCompat!"); |
3036 | Diag(From->getExprLoc(), diag::ext_ms_impcast_fn_obj) |
3037 | << From->getSourceRange(); |
3038 | } |
3039 | } |
3040 | } else if (const ObjCObjectPointerType *ToPtrType = |
3041 | ToType->getAs<ObjCObjectPointerType>()) { |
3042 | if (const ObjCObjectPointerType *FromPtrType = |
3043 | FromType->getAs<ObjCObjectPointerType>()) { |
3044 | |
3045 | |
3046 | |
3047 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
3048 | return false; |
3049 | } else if (FromType->isBlockPointerType()) { |
3050 | Kind = CK_BlockPointerToObjCPointerCast; |
3051 | } else { |
3052 | Kind = CK_CPointerToObjCPointerCast; |
3053 | } |
3054 | } else if (ToType->isBlockPointerType()) { |
3055 | if (!FromType->isBlockPointerType()) |
3056 | Kind = CK_AnyPointerToBlockPointerCast; |
3057 | } |
3058 | |
3059 | |
3060 | |
3061 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
3062 | Kind = CK_NullToPointer; |
3063 | |
3064 | return false; |
3065 | } |
3066 | |
3067 | |
3068 | |
3069 | |
3070 | |
3071 | |
3072 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
3073 | QualType ToType, |
3074 | bool InOverloadResolution, |
3075 | QualType &ConvertedType) { |
3076 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
3077 | if (!ToTypePtr) |
3078 | return false; |
3079 | |
3080 | |
3081 | if (From->isNullPointerConstant(Context, |
3082 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
3083 | : Expr::NPC_ValueDependentIsNull)) { |
3084 | ConvertedType = ToType; |
3085 | return true; |
3086 | } |
3087 | |
3088 | |
3089 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
3090 | if (!FromTypePtr) |
3091 | return false; |
3092 | |
3093 | |
3094 | |
3095 | QualType FromClass(FromTypePtr->getClass(), 0); |
3096 | QualType ToClass(ToTypePtr->getClass(), 0); |
3097 | |
3098 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
3099 | IsDerivedFrom(From->getBeginLoc(), ToClass, FromClass)) { |
3100 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
3101 | ToClass.getTypePtr()); |
3102 | return true; |
3103 | } |
3104 | |
3105 | return false; |
3106 | } |
3107 | |
3108 | |
3109 | |
3110 | |
3111 | |
3112 | |
3113 | |
3114 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
3115 | CastKind &Kind, |
3116 | CXXCastPath &BasePath, |
3117 | bool IgnoreBaseAccess) { |
3118 | QualType FromType = From->getType(); |
3119 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
3120 | if (!FromPtrType) { |
3121 | |
3122 | assert(From->isNullPointerConstant(Context, |
3123 | Expr::NPC_ValueDependentIsNull) && |
3124 | "Expr must be null pointer constant!"); |
3125 | Kind = CK_NullToMemberPointer; |
3126 | return false; |
3127 | } |
3128 | |
3129 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
3130 | assert(ToPtrType && "No member pointer cast has a target type " |
3131 | "that is not a member pointer."); |
3132 | |
3133 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
3134 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
3135 | |
3136 | |
3137 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
3138 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
3139 | |
3140 | CXXBasePaths Paths(true, true, |
3141 | true); |
3142 | bool DerivationOkay = |
3143 | IsDerivedFrom(From->getBeginLoc(), ToClass, FromClass, Paths); |
3144 | assert(DerivationOkay && |
3145 | "Should not have been called if derivation isn't OK."); |
3146 | (void)DerivationOkay; |
3147 | |
3148 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
3149 | getUnqualifiedType())) { |
3150 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
3151 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
3152 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
3153 | return true; |
3154 | } |
3155 | |
3156 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
3157 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
3158 | << FromClass << ToClass << QualType(VBase, 0) |
3159 | << From->getSourceRange(); |
3160 | return true; |
3161 | } |
3162 | |
3163 | if (!IgnoreBaseAccess) |
3164 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
3165 | Paths.front(), |
3166 | diag::err_downcast_from_inaccessible_base); |
3167 | |
3168 | |
3169 | BuildBasePathArray(Paths, BasePath); |
3170 | Kind = CK_BaseToDerivedMemberPointer; |
3171 | return false; |
3172 | } |
3173 | |
3174 | |
3175 | |
3176 | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
3177 | Qualifiers ToQuals) { |
3178 | |
3179 | if (ToQuals.hasConst() && |
3180 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
3181 | return false; |
3182 | |
3183 | return true; |
3184 | } |
3185 | |
3186 | |
3187 | |
3188 | |
3189 | |
3190 | |
3191 | |
3192 | static bool isQualificationConversionStep(QualType FromType, QualType ToType, |
3193 | bool CStyle, bool IsTopLevel, |
3194 | bool &PreviousToQualsIncludeConst, |
3195 | bool &ObjCLifetimeConversion) { |
3196 | Qualifiers FromQuals = FromType.getQualifiers(); |
3197 | Qualifiers ToQuals = ToType.getQualifiers(); |
3198 | |
3199 | |
3200 | if (ToType.getUnqualifiedType()->isVoidType()) |
3201 | FromQuals.removeUnaligned(); |
3202 | |
3203 | |
3204 | |
3205 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime()) { |
3206 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
3207 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
3208 | ObjCLifetimeConversion = true; |
3209 | FromQuals.removeObjCLifetime(); |
3210 | ToQuals.removeObjCLifetime(); |
3211 | } else { |
3212 | |
3213 | |
3214 | return false; |
3215 | } |
3216 | } |
3217 | |
3218 | |
3219 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
3220 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
3221 | FromQuals.removeObjCGCAttr(); |
3222 | ToQuals.removeObjCGCAttr(); |
3223 | } |
3224 | |
3225 | |
3226 | |
3227 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
3228 | return false; |
3229 | |
3230 | |
3231 | |
3232 | |
3233 | |
3234 | |
3235 | if (ToQuals.getAddressSpace() != FromQuals.getAddressSpace() && |
3236 | (!IsTopLevel || |
3237 | !(ToQuals.isAddressSpaceSupersetOf(FromQuals) || |
3238 | (CStyle && FromQuals.isAddressSpaceSupersetOf(ToQuals))))) |
3239 | return false; |
3240 | |
3241 | |
3242 | |
3243 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() && |
3244 | !PreviousToQualsIncludeConst) |
3245 | return false; |
3246 | |
3247 | |
3248 | |
3249 | PreviousToQualsIncludeConst = |
3250 | PreviousToQualsIncludeConst && ToQuals.hasConst(); |
3251 | return true; |
3252 | } |
3253 | |
3254 | |
3255 | |
3256 | |
3257 | |
3258 | |
3259 | |
3260 | |
3261 | bool |
3262 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
3263 | bool CStyle, bool &ObjCLifetimeConversion) { |
3264 | FromType = Context.getCanonicalType(FromType); |
3265 | ToType = Context.getCanonicalType(ToType); |
3266 | ObjCLifetimeConversion = false; |
3267 | |
3268 | |
3269 | |
3270 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
3271 | return false; |
3272 | |
3273 | |
3274 | |
3275 | |
3276 | bool PreviousToQualsIncludeConst = true; |
3277 | bool UnwrappedAnyPointer = false; |
3278 | while (Context.UnwrapSimilarTypes(FromType, ToType)) { |
3279 | if (!isQualificationConversionStep( |
3280 | FromType, ToType, CStyle, !UnwrappedAnyPointer, |
3281 | PreviousToQualsIncludeConst, ObjCLifetimeConversion)) |
3282 | return false; |
3283 | UnwrappedAnyPointer = true; |
3284 | } |
3285 | |
3286 | |
3287 | |
3288 | |
3289 | |
3290 | |
3291 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
3292 | } |
3293 | |
3294 | |
3295 | |
3296 | |
3297 | |
3298 | |
3299 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
3300 | bool InOverloadResolution, |
3301 | StandardConversionSequence &SCS, |
3302 | bool CStyle) { |
3303 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
3304 | if (!ToAtomic) |
3305 | return false; |
3306 | |
3307 | StandardConversionSequence InnerSCS; |
3308 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
3309 | InOverloadResolution, InnerSCS, |
3310 | CStyle, false)) |
3311 | return false; |
3312 | |
3313 | SCS.Second = InnerSCS.Second; |
3314 | SCS.setToType(1, InnerSCS.getToType(1)); |
3315 | SCS.Third = InnerSCS.Third; |
3316 | SCS.QualificationIncludesObjCLifetime |
3317 | = InnerSCS.QualificationIncludesObjCLifetime; |
3318 | SCS.setToType(2, InnerSCS.getToType(2)); |
3319 | return true; |
3320 | } |
3321 | |
3322 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
3323 | CXXConstructorDecl *Constructor, |
3324 | QualType Type) { |
3325 | const auto *CtorType = Constructor->getType()->castAs<FunctionProtoType>(); |
3326 | if (CtorType->getNumParams() > 0) { |
3327 | QualType FirstArg = CtorType->getParamType(0); |
3328 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
3329 | return true; |
3330 | } |
3331 | return false; |
3332 | } |
3333 | |
3334 | static OverloadingResult |
3335 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
3336 | CXXRecordDecl *To, |
3337 | UserDefinedConversionSequence &User, |
3338 | OverloadCandidateSet &CandidateSet, |
3339 | bool AllowExplicit) { |
3340 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3341 | for (auto *D : S.LookupConstructors(To)) { |
3342 | auto Info = getConstructorInfo(D); |
3343 | if (!Info) |
3344 | continue; |
3345 | |
3346 | bool Usable = !Info.Constructor->isInvalidDecl() && |
3347 | S.isInitListConstructor(Info.Constructor); |
3348 | if (Usable) { |
3349 | bool SuppressUserConversions = false; |
3350 | if (Info.ConstructorTmpl) |
3351 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
3352 | nullptr, From, |
3353 | CandidateSet, SuppressUserConversions, |
3354 | false, |
3355 | AllowExplicit); |
3356 | else |
3357 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, From, |
3358 | CandidateSet, SuppressUserConversions, |
3359 | false, AllowExplicit); |
3360 | } |
3361 | } |
3362 | |
3363 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
3364 | |
3365 | OverloadCandidateSet::iterator Best; |
3366 | switch (auto Result = |
3367 | CandidateSet.BestViableFunction(S, From->getBeginLoc(), Best)) { |
3368 | case OR_Deleted: |
3369 | case OR_Success: { |
3370 | |
3371 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
3372 | QualType ThisType = Constructor->getThisType(); |
3373 | |
3374 | User.Before.setAsIdentityConversion(); |
3375 | User.HadMultipleCandidates = HadMultipleCandidates; |
3376 | User.ConversionFunction = Constructor; |
3377 | User.FoundConversionFunction = Best->FoundDecl; |
3378 | User.After.setAsIdentityConversion(); |
3379 | User.After.setFromType(ThisType->castAs<PointerType>()->getPointeeType()); |
3380 | User.After.setAllToTypes(ToType); |
3381 | return Result; |
3382 | } |
3383 | |
3384 | case OR_No_Viable_Function: |
3385 | return OR_No_Viable_Function; |
3386 | case OR_Ambiguous: |
3387 | return OR_Ambiguous; |
3388 | } |
3389 | |
3390 | llvm_unreachable("Invalid OverloadResult!"); |
3391 | } |
3392 | |
3393 | |
3394 | |
3395 | |
3396 | |
3397 | |
3398 | |
3399 | |
3400 | |
3401 | |
3402 | |
3403 | |
3404 | |
3405 | |
3406 | |
3407 | static OverloadingResult |
3408 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
3409 | UserDefinedConversionSequence &User, |
3410 | OverloadCandidateSet &CandidateSet, |
3411 | AllowedExplicit AllowExplicit, |
3412 | bool AllowObjCConversionOnExplicit) { |
3413 | assert(AllowExplicit != AllowedExplicit::None || |
3414 | !AllowObjCConversionOnExplicit); |
3415 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3416 | |
3417 | |
3418 | bool ConstructorsOnly = false; |
3419 | |
3420 | |
3421 | |
3422 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
3423 | |
3424 | |
3425 | |
3426 | |
3427 | |
3428 | |
3429 | |
3430 | |
3431 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
3432 | (From->getType()->getAs<RecordType>() && |
3433 | S.IsDerivedFrom(From->getBeginLoc(), From->getType(), ToType))) |
3434 | ConstructorsOnly = true; |
3435 | |
3436 | if (!S.isCompleteType(From->getExprLoc(), ToType)) { |
3437 | |
3438 | } else if (CXXRecordDecl *ToRecordDecl |
3439 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
3440 | |
3441 | Expr **Args = &From; |
3442 | unsigned NumArgs = 1; |
3443 | bool ListInitializing = false; |
3444 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
3445 | |
3446 | OverloadingResult Result = IsInitializerListConstructorConversion( |
3447 | S, From, ToType, ToRecordDecl, User, CandidateSet, |
3448 | AllowExplicit == AllowedExplicit::All); |
3449 | if (Result != OR_No_Viable_Function) |
3450 | return Result; |
3451 | |
3452 | CandidateSet.clear( |
3453 | OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3454 | |
3455 | |
3456 | |
3457 | Args = InitList->getInits(); |
3458 | NumArgs = InitList->getNumInits(); |
3459 | ListInitializing = true; |
3460 | } |
3461 | |
3462 | for (auto *D : S.LookupConstructors(ToRecordDecl)) { |
3463 | auto Info = getConstructorInfo(D); |
3464 | if (!Info) |
3465 | continue; |
3466 | |
3467 | bool Usable = !Info.Constructor->isInvalidDecl(); |
3468 | if (!ListInitializing) |
3469 | Usable = Usable && Info.Constructor->isConvertingConstructor( |
3470 | true); |
3471 | if (Usable) { |
3472 | bool SuppressUserConversions = !ConstructorsOnly; |
3473 | |
3474 | |
3475 | |
3476 | |
3477 | |
3478 | |
3479 | |
3480 | if (SuppressUserConversions && ListInitializing) { |
3481 | SuppressUserConversions = |
3482 | NumArgs == 1 && isa<InitListExpr>(Args[0]) && |
3483 | isFirstArgumentCompatibleWithType(S.Context, Info.Constructor, |
3484 | ToType); |
3485 | } |
3486 | if (Info.ConstructorTmpl) |
3487 | S.AddTemplateOverloadCandidate( |
3488 | Info.ConstructorTmpl, Info.FoundDecl, |
3489 | nullptr, llvm::makeArrayRef(Args, NumArgs), |
3490 | CandidateSet, SuppressUserConversions, |
3491 | false, |
3492 | AllowExplicit == AllowedExplicit::All); |
3493 | else |
3494 | |
3495 | |
3496 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
3497 | llvm::makeArrayRef(Args, NumArgs), |
3498 | CandidateSet, SuppressUserConversions, |
3499 | false, |
3500 | AllowExplicit == AllowedExplicit::All); |
3501 | } |
3502 | } |
3503 | } |
3504 | } |
3505 | |
3506 | |
3507 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
3508 | } else if (!S.isCompleteType(From->getBeginLoc(), From->getType())) { |
3509 | |
3510 | } else if (const RecordType *FromRecordType = |
3511 | From->getType()->getAs<RecordType>()) { |
3512 | if (CXXRecordDecl *FromRecordDecl |
3513 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
3514 | |
3515 | const auto &Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
3516 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
3517 | DeclAccessPair FoundDecl = I.getPair(); |
3518 | NamedDecl *D = FoundDecl.getDecl(); |
3519 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
3520 | if (isa<UsingShadowDecl>(D)) |
3521 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
3522 | |
3523 | CXXConversionDecl *Conv; |
3524 | FunctionTemplateDecl *ConvTemplate; |
3525 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
3526 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
3527 | else |
3528 | Conv = cast<CXXConversionDecl>(D); |
3529 | |
3530 | if (ConvTemplate) |
3531 | S.AddTemplateConversionCandidate( |
3532 | ConvTemplate, FoundDecl, ActingContext, From, ToType, |
3533 | CandidateSet, AllowObjCConversionOnExplicit, |
3534 | AllowExplicit != AllowedExplicit::None); |
3535 | else |
3536 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, ToType, |
3537 | CandidateSet, AllowObjCConversionOnExplicit, |
3538 | AllowExplicit != AllowedExplicit::None); |
3539 | } |
3540 | } |
3541 | } |
3542 | |
3543 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
3544 | |
3545 | OverloadCandidateSet::iterator Best; |
3546 | switch (auto Result = |
3547 | CandidateSet.BestViableFunction(S, From->getBeginLoc(), Best)) { |
3548 | case OR_Success: |
3549 | case OR_Deleted: |
3550 | |
3551 | if (CXXConstructorDecl *Constructor |
3552 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
3553 | |
3554 | |
3555 | |
3556 | |
3557 | |
3558 | |
3559 | QualType ThisType = Constructor->getThisType(); |
3560 | if (isa<InitListExpr>(From)) { |
3561 | |
3562 | User.Before.setAsIdentityConversion(); |
3563 | } else { |
3564 | if (Best->Conversions[0].isEllipsis()) |
3565 | User.EllipsisConversion = true; |
3566 | else { |
3567 | User.Before = Best->Conversions[0].Standard; |
3568 | User.EllipsisConversion = false; |
3569 | } |
3570 | } |
3571 | User.HadMultipleCandidates = HadMultipleCandidates; |
3572 | User.ConversionFunction = Constructor; |
3573 | User.FoundConversionFunction = Best->FoundDecl; |
3574 | User.After.setAsIdentityConversion(); |
3575 | User.After.setFromType(ThisType->castAs<PointerType>()->getPointeeType()); |
3576 | User.After.setAllToTypes(ToType); |
3577 | return Result; |
3578 | } |
3579 | if (CXXConversionDecl *Conversion |
3580 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
3581 | |
3582 | |
3583 | |
3584 | |
3585 | |
3586 | |
3587 | User.Before = Best->Conversions[0].Standard; |
3588 | User.HadMultipleCandidates = HadMultipleCandidates; |
3589 | User.ConversionFunction = Conversion; |
3590 | User.FoundConversionFunction = Best->FoundDecl; |
3591 | User.EllipsisConversion = false; |
3592 | |
3593 | |
3594 | |
3595 | |
3596 | |
3597 | |
3598 | |
3599 | |
3600 | |
3601 | |
3602 | User.After = Best->FinalConversion; |
3603 | return Result; |
3604 | } |
3605 | llvm_unreachable("Not a constructor or conversion function?"); |
3606 | |
3607 | case OR_No_Viable_Function: |
3608 | return OR_No_Viable_Function; |
3609 | |
3610 | case OR_Ambiguous: |
3611 | return OR_Ambiguous; |
3612 | } |
3613 | |
3614 | llvm_unreachable("Invalid OverloadResult!"); |
3615 | } |
3616 | |
3617 | bool |
3618 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
3619 | ImplicitConversionSequence ICS; |
3620 | OverloadCandidateSet CandidateSet(From->getExprLoc(), |
3621 | OverloadCandidateSet::CSK_Normal); |
3622 | OverloadingResult OvResult = |
3623 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
3624 | CandidateSet, AllowedExplicit::None, false); |
3625 | |
3626 | if (!(OvResult == OR_Ambiguous || |
3627 | (OvResult == OR_No_Viable_Function && !CandidateSet.empty()))) |
3628 | return false; |
3629 | |
3630 | auto Cands = CandidateSet.CompleteCandidates( |
3631 | *this, |
3632 | OvResult == OR_Ambiguous ? OCD_AmbiguousCandidates : OCD_AllCandidates, |
3633 | From); |
3634 | if (OvResult == OR_Ambiguous) |
3635 | Diag(From->getBeginLoc(), diag::err_typecheck_ambiguous_condition) |
3636 | << From->getType() << ToType << From->getSourceRange(); |
3637 | else { |
3638 | if (!RequireCompleteType(From->getBeginLoc(), ToType, |
3639 | diag::err_typecheck_nonviable_condition_incomplete, |
3640 | From->getType(), From->getSourceRange())) |
3641 | Diag(From->getBeginLoc(), diag::err_typecheck_nonviable_condition) |
3642 | << false << From->getType() << From->getSourceRange() << ToType; |
3643 | } |
3644 | |
3645 | CandidateSet.NoteCandidates( |
3646 | *this, From, Cands); |
3647 | return true; |
3648 | } |
3649 | |
3650 | |
3651 | |
3652 | static const FunctionType * |
3653 | getConversionOpReturnTyAsFunction(CXXConversionDecl *Conv) { |
3654 | const FunctionType *ConvFuncTy = Conv->getType()->castAs<FunctionType>(); |
3655 | const PointerType *RetPtrTy = |
3656 | ConvFuncTy->getReturnType()->getAs<PointerType>(); |
3657 | |
3658 | if (!RetPtrTy) |
3659 | return nullptr; |
3660 | |
3661 | return RetPtrTy->getPointeeType()->getAs<FunctionType>(); |
3662 | } |
3663 | |
3664 | |
3665 | |
3666 | |
3667 | static ImplicitConversionSequence::CompareKind |
3668 | compareConversionFunctions(Sema &S, FunctionDecl *Function1, |
3669 | FunctionDecl *Function2) { |
3670 | CXXConversionDecl *Conv1 = dyn_cast_or_null<CXXConversionDecl>(Function1); |
3671 | CXXConversionDecl *Conv2 = dyn_cast_or_null<CXXConversionDecl>(Function2); |
3672 | if (!Conv1 || !Conv2) |
3673 | return ImplicitConversionSequence::Indistinguishable; |
3674 | |
3675 | if (!Conv1->getParent()->isLambda() || !Conv2->getParent()->isLambda()) |
3676 | return ImplicitConversionSequence::Indistinguishable; |
3677 | |
3678 | |
3679 | |
3680 | |
3681 | |
3682 | |
3683 | |
3684 | if (S.getLangOpts().ObjC && S.getLangOpts().CPlusPlus11) { |
3685 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
3686 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
3687 | if (Block1 != Block2) |
3688 | return Block1 ? ImplicitConversionSequence::Worse |
3689 | : ImplicitConversionSequence::Better; |
3690 | } |
3691 | |
3692 | |
3693 | |
3694 | |
3695 | |
3696 | |
3697 | const FunctionType *Conv1FuncRet = getConversionOpReturnTyAsFunction(Conv1); |
3698 | const FunctionType *Conv2FuncRet = getConversionOpReturnTyAsFunction(Conv2); |
3699 | |
3700 | if (Conv1FuncRet && Conv2FuncRet && |
3701 | Conv1FuncRet->getCallConv() != Conv2FuncRet->getCallConv()) { |
3702 | CallingConv Conv1CC = Conv1FuncRet->getCallConv(); |
3703 | CallingConv Conv2CC = Conv2FuncRet->getCallConv(); |
3704 | |
3705 | CXXMethodDecl *CallOp = Conv2->getParent()->getLambdaCallOperator(); |
3706 | const FunctionProtoType *CallOpProto = |
3707 | CallOp->getType()->getAs<FunctionProtoType>(); |
3708 | |
3709 | CallingConv CallOpCC = |
3710 | CallOp->getType()->castAs<FunctionType>()->getCallConv(); |
3711 | CallingConv DefaultFree = S.Context.getDefaultCallingConvention( |
3712 | CallOpProto->isVariadic(), false); |
3713 | CallingConv DefaultMember = S.Context.getDefaultCallingConvention( |
3714 | CallOpProto->isVariadic(), true); |
3715 | |
3716 | CallingConv PrefOrder[] = {DefaultFree, DefaultMember, CallOpCC}; |
3717 | for (CallingConv CC : PrefOrder) { |
3718 | if (Conv1CC == CC) |
3719 | return ImplicitConversionSequence::Better; |
3720 | if (Conv2CC == CC) |
3721 | return ImplicitConversionSequence::Worse; |
3722 | } |
3723 | } |
3724 | |
3725 | return ImplicitConversionSequence::Indistinguishable; |
3726 | } |
3727 | |
3728 | static bool hasDeprecatedStringLiteralToCharPtrConversion( |
3729 | const ImplicitConversionSequence &ICS) { |
3730 | return (ICS.isStandard() && ICS.Standard.DeprecatedStringLiteralToCharPtr) || |
3731 | (ICS.isUserDefined() && |
3732 | ICS.UserDefined.Before.DeprecatedStringLiteralToCharPtr); |
3733 | } |
3734 | |
3735 | |
3736 | |
3737 | |
3738 | static ImplicitConversionSequence::CompareKind |
3739 | CompareImplicitConversionSequences(Sema &S, SourceLocation Loc, |
3740 | const ImplicitConversionSequence& ICS1, |
3741 | const ImplicitConversionSequence& ICS2) |
3742 | { |
3743 | |
3744 | |
3745 | |
3746 | |
3747 | |
3748 | |
3749 | |
3750 | |
3751 | |
3752 | |
3753 | |
3754 | |
3755 | |
3756 | |
3757 | |
3758 | |
3759 | |
3760 | |
3761 | |
3762 | |
3763 | |
3764 | |
3765 | |
3766 | |
3767 | |
3768 | |
3769 | |
3770 | |
3771 | |
3772 | |
3773 | |
3774 | |
3775 | |
3776 | if (S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
3777 | hasDeprecatedStringLiteralToCharPtrConversion(ICS1) != |
3778 | hasDeprecatedStringLiteralToCharPtrConversion(ICS2)) |
3779 | return hasDeprecatedStringLiteralToCharPtrConversion(ICS1) |
3780 | ? ImplicitConversionSequence::Worse |
3781 | : ImplicitConversionSequence::Better; |
3782 | |
3783 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
3784 | return ImplicitConversionSequence::Better; |
3785 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
3786 | return ImplicitConversionSequence::Worse; |
3787 | |
3788 | |
3789 | |
3790 | if (ICS1.getKind() != ICS2.getKind()) |
3791 | return ImplicitConversionSequence::Indistinguishable; |
3792 | |
3793 | ImplicitConversionSequence::CompareKind Result = |
3794 | ImplicitConversionSequence::Indistinguishable; |
3795 | |
3796 | |
3797 | |
3798 | |
3799 | |
3800 | |
3801 | |
3802 | |
3803 | |
3804 | |
3805 | |
3806 | |
3807 | if (!ICS1.isBad()) { |
3808 | if (ICS1.isStdInitializerListElement() && |
3809 | !ICS2.isStdInitializerListElement()) |
3810 | return ImplicitConversionSequence::Better; |
3811 | if (!ICS1.isStdInitializerListElement() && |
3812 | ICS2.isStdInitializerListElement()) |
3813 | return ImplicitConversionSequence::Worse; |
3814 | } |
3815 | |
3816 | if (ICS1.isStandard()) |
3817 | |
3818 | |
3819 | Result = CompareStandardConversionSequences(S, Loc, |
3820 | ICS1.Standard, ICS2.Standard); |
3821 | else if (ICS1.isUserDefined()) { |
3822 | |
3823 | |
3824 | |
3825 | |
3826 | |
3827 | |
3828 | if (ICS1.UserDefined.ConversionFunction == |
3829 | ICS2.UserDefined.ConversionFunction) |
3830 | Result = CompareStandardConversionSequences(S, Loc, |
3831 | ICS1.UserDefined.After, |
3832 | ICS2.UserDefined.After); |
3833 | else |
3834 | Result = compareConversionFunctions(S, |
3835 | ICS1.UserDefined.ConversionFunction, |
3836 | ICS2.UserDefined.ConversionFunction); |
3837 | } |
3838 | |
3839 | return Result; |
3840 | } |
3841 | |
3842 | |
3843 | |
3844 | static ImplicitConversionSequence::CompareKind |
3845 | compareStandardConversionSubsets(ASTContext &Context, |
3846 | const StandardConversionSequence& SCS1, |
3847 | const StandardConversionSequence& SCS2) { |
3848 | ImplicitConversionSequence::CompareKind Result |
3849 | = ImplicitConversionSequence::Indistinguishable; |
3850 | |
3851 | |
3852 | |
3853 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
3854 | return ImplicitConversionSequence::Better; |
3855 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
3856 | return ImplicitConversionSequence::Worse; |
3857 | |
3858 | if (SCS1.Second != SCS2.Second) { |
3859 | if (SCS1.Second == ICK_Identity) |
3860 | Result = ImplicitConversionSequence::Better; |
3861 | else if (SCS2.Second == ICK_Identity) |
3862 | Result = ImplicitConversionSequence::Worse; |
3863 | else |
3864 | return ImplicitConversionSequence::Indistinguishable; |
3865 | } else if (!Context.hasSimilarType(SCS1.getToType(1), SCS2.getToType(1))) |
3866 | return ImplicitConversionSequence::Indistinguishable; |
3867 | |
3868 | if (SCS1.Third == SCS2.Third) { |
3869 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
3870 | : ImplicitConversionSequence::Indistinguishable; |
3871 | } |
3872 | |
3873 | if (SCS1.Third == ICK_Identity) |
3874 | return Result == ImplicitConversionSequence::Worse |
3875 | ? ImplicitConversionSequence::Indistinguishable |
3876 | : ImplicitConversionSequence::Better; |
3877 | |
3878 | if (SCS2.Third == ICK_Identity) |
3879 | return Result == ImplicitConversionSequence::Better |
3880 | ? ImplicitConversionSequence::Indistinguishable |
3881 | : ImplicitConversionSequence::Worse; |
3882 | |
3883 | return ImplicitConversionSequence::Indistinguishable; |
3884 | } |
3885 | |
3886 | |
3887 | |
3888 | static bool |
3889 | isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
3890 | const StandardConversionSequence &SCS2) { |
3891 | |
3892 | |
3893 | |
3894 | |
3895 | |
3896 | |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | |
3902 | |
3903 | |
3904 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
3905 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
3906 | return false; |
3907 | |
3908 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
3909 | SCS2.IsLvalueReference) || |
3910 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
3911 | !SCS2.IsLvalueReference && SCS2.BindsToFunctionLvalue); |
3912 | } |
3913 | |
3914 | enum class FixedEnumPromotion { |
3915 | None, |
3916 | ToUnderlyingType, |
3917 | ToPromotedUnderlyingType |
3918 | }; |
3919 | |
3920 | |
3921 | static FixedEnumPromotion |
3922 | getFixedEnumPromtion(Sema &S, const StandardConversionSequence &SCS) { |
3923 | |
3924 | if (SCS.Second != ICK_Integral_Promotion) |
3925 | return FixedEnumPromotion::None; |
3926 | |
3927 | QualType FromType = SCS.getFromType(); |
3928 | if (!FromType->isEnumeralType()) |
3929 | return FixedEnumPromotion::None; |
3930 | |
3931 | EnumDecl *Enum = FromType->castAs<EnumType>()->getDecl(); |
3932 | if (!Enum->isFixed()) |
3933 | return FixedEnumPromotion::None; |
3934 | |
3935 | QualType UnderlyingType = Enum->getIntegerType(); |
3936 | if (S.Context.hasSameType(SCS.getToType(1), UnderlyingType)) |
3937 | return FixedEnumPromotion::ToUnderlyingType; |
3938 | |
3939 | return FixedEnumPromotion::ToPromotedUnderlyingType; |
3940 | } |
3941 | |
3942 | |
3943 | |
3944 | |
3945 | static ImplicitConversionSequence::CompareKind |
3946 | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
3947 | const StandardConversionSequence& SCS1, |
3948 | const StandardConversionSequence& SCS2) |
3949 | { |
3950 | |
3951 | |
3952 | |
3953 | |
3954 | |
3955 | |
3956 | |
3957 | |
3958 | if (ImplicitConversionSequence::CompareKind CK |
3959 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
3960 | return CK; |
3961 | |
3962 | |
3963 | |
3964 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
3965 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
3966 | if (Rank1 < Rank2) |
3967 | return ImplicitConversionSequence::Better; |
3968 | else if (Rank2 < Rank1) |
3969 | return ImplicitConversionSequence::Worse; |
3970 | |
3971 | |
3972 | |
3973 | |
3974 | |
3975 | |
3976 | |
3977 | |
3978 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
3979 | return SCS2.isPointerConversionToBool() |
3980 | ? ImplicitConversionSequence::Better |
3981 | : ImplicitConversionSequence::Worse; |
3982 | |
3983 | |
3984 | |
3985 | |
3986 | |
3987 | |
3988 | |
3989 | FixedEnumPromotion FEP1 = getFixedEnumPromtion(S, SCS1); |
3990 | FixedEnumPromotion FEP2 = getFixedEnumPromtion(S, SCS2); |
3991 | if (FEP1 != FixedEnumPromotion::None && FEP2 != FixedEnumPromotion::None && |
3992 | FEP1 != FEP2) |
3993 | return FEP1 == FixedEnumPromotion::ToUnderlyingType |
3994 | ? ImplicitConversionSequence::Better |
3995 | : ImplicitConversionSequence::Worse; |
3996 | |
3997 | |
3998 | |
3999 | |
4000 | |
4001 | |
4002 | |
4003 | bool SCS1ConvertsToVoid |
4004 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
4005 | bool SCS2ConvertsToVoid |
4006 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
4007 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
4008 | |
4009 | |
4010 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
4011 | : ImplicitConversionSequence::Worse; |
4012 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
4013 | |
4014 | |
4015 | if (ImplicitConversionSequence::CompareKind DerivedCK |
4016 | = CompareDerivedToBaseConversions(S, Loc, SCS1, SCS2)) |
4017 | return DerivedCK; |
4018 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
4019 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
4020 | |
4021 | |
4022 | |
4023 | QualType FromType1 = SCS1.getFromType(); |
4024 | QualType FromType2 = SCS2.getFromType(); |
4025 | |
4026 | |
4027 | |
4028 | if (SCS1.First == ICK_Array_To_Pointer) |
4029 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
4030 | if (SCS2.First == ICK_Array_To_Pointer) |
4031 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
4032 | |
4033 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
4034 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
4035 | |
4036 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4037 | return ImplicitConversionSequence::Better; |
4038 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4039 | return ImplicitConversionSequence::Worse; |
4040 | |
4041 | |
4042 | |
4043 | const ObjCObjectPointerType* FromObjCPtr1 |
4044 | = FromType1->getAs<ObjCObjectPointerType>(); |
4045 | const ObjCObjectPointerType* FromObjCPtr2 |
4046 | = FromType2->getAs<ObjCObjectPointerType>(); |
4047 | if (FromObjCPtr1 && FromObjCPtr2) { |
4048 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
4049 | FromObjCPtr2); |
4050 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
4051 | FromObjCPtr1); |
4052 | if (AssignLeft != AssignRight) { |
4053 | return AssignLeft? ImplicitConversionSequence::Better |
4054 | : ImplicitConversionSequence::Worse; |
4055 | } |
4056 | } |
4057 | } |
4058 | |
4059 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
4060 | |
4061 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
4062 | return ImplicitConversionSequence::Better; |
4063 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
4064 | return ImplicitConversionSequence::Worse; |
4065 | } |
4066 | |
4067 | |
4068 | |
4069 | if (ImplicitConversionSequence::CompareKind QualCK |
4070 | = CompareQualificationConversions(S, SCS1, SCS2)) |
4071 | return QualCK; |
4072 | |
4073 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
4074 | |
4075 | |
4076 | |
4077 | |
4078 | |
4079 | |
4080 | QualType T1 = SCS1.getToType(2); |
4081 | QualType T2 = SCS2.getToType(2); |
4082 | T1 = S.Context.getCanonicalType(T1); |
4083 | T2 = S.Context.getCanonicalType(T2); |
4084 | Qualifiers T1Quals, T2Quals; |
4085 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
4086 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
4087 | if (UnqualT1 == UnqualT2) { |
4088 | |
4089 | |
4090 | if (SCS1.ObjCLifetimeConversionBinding != |
4091 | SCS2.ObjCLifetimeConversionBinding) { |
4092 | return SCS1.ObjCLifetimeConversionBinding |
4093 | ? ImplicitConversionSequence::Worse |
4094 | : ImplicitConversionSequence::Better; |
4095 | } |
4096 | |
4097 | |
4098 | |
4099 | if (isa<ArrayType>(T1) && T1Quals) |
4100 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
4101 | if (isa<ArrayType>(T2) && T2Quals) |
4102 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
4103 | if (T2.isMoreQualifiedThan(T1)) |
4104 | return ImplicitConversionSequence::Better; |
4105 | if (T1.isMoreQualifiedThan(T2)) |
4106 | return ImplicitConversionSequence::Worse; |
4107 | } |
4108 | } |
4109 | |
4110 | |
4111 | |
4112 | |
4113 | |
4114 | |
4115 | |
4116 | |
4117 | |
4118 | |
4119 | |
4120 | |
4121 | |
4122 | if (S.getLangOpts().MSVCCompat && |
4123 | !S.getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2019_8) && |
4124 | SCS1.Second == ICK_Integral_Conversion && |
4125 | SCS2.Second == ICK_Floating_Integral && |
4126 | S.Context.getTypeSize(SCS1.getFromType()) == |
4127 | S.Context.getTypeSize(SCS1.getToType(2))) |
4128 | return ImplicitConversionSequence::Better; |
4129 | |
4130 | |
4131 | |
4132 | |
4133 | |
4134 | |
4135 | |
4136 | |
4137 | |
4138 | |
4139 | |
4140 | |
4141 | |
4142 | if (SCS1.Second == ICK_Vector_Conversion && |
4143 | SCS2.Second == ICK_Vector_Conversion) { |
4144 | bool SCS1IsCompatibleVectorConversion = S.Context.areCompatibleVectorTypes( |
4145 | SCS1.getFromType(), SCS1.getToType(2)); |
4146 | bool SCS2IsCompatibleVectorConversion = S.Context.areCompatibleVectorTypes( |
4147 | SCS2.getFromType(), SCS2.getToType(2)); |
4148 | |
4149 | if (SCS1IsCompatibleVectorConversion != SCS2IsCompatibleVectorConversion) |
4150 | return SCS1IsCompatibleVectorConversion |
4151 | ? ImplicitConversionSequence::Better |
4152 | : ImplicitConversionSequence::Worse; |
4153 | } |
4154 | |
4155 | if (SCS1.Second == ICK_SVE_Vector_Conversion && |
4156 | SCS2.Second == ICK_SVE_Vector_Conversion) { |
4157 | bool SCS1IsCompatibleSVEVectorConversion = |
4158 | S.Context.areCompatibleSveTypes(SCS1.getFromType(), SCS1.getToType(2)); |
4159 | bool SCS2IsCompatibleSVEVectorConversion = |
4160 | S.Context.areCompatibleSveTypes(SCS2.getFromType(), SCS2.getToType(2)); |
4161 | |
4162 | if (SCS1IsCompatibleSVEVectorConversion != |
4163 | SCS2IsCompatibleSVEVectorConversion) |
4164 | return SCS1IsCompatibleSVEVectorConversion |
4165 | ? ImplicitConversionSequence::Better |
4166 | : ImplicitConversionSequence::Worse; |
4167 | } |
4168 | |
4169 | return ImplicitConversionSequence::Indistinguishable; |
4170 | } |
4171 | |
4172 | |
4173 | |
4174 | |
4175 | static ImplicitConversionSequence::CompareKind |
4176 | CompareQualificationConversions(Sema &S, |
4177 | const StandardConversionSequence& SCS1, |
4178 | const StandardConversionSequence& SCS2) { |
4179 | |
4180 | |
4181 | |
4182 | |
4183 | |
4184 | |
4185 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
4186 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
4187 | return ImplicitConversionSequence::Indistinguishable; |
4188 | |
4189 | |
4190 | |
4191 | QualType T1 = SCS1.getToType(2); |
4192 | QualType T2 = SCS2.getToType(2); |
4193 | T1 = S.Context.getCanonicalType(T1); |
4194 | T2 = S.Context.getCanonicalType(T2); |
4195 | assert(!T1->isReferenceType() && !T2->isReferenceType()); |
4196 | Qualifiers T1Quals, T2Quals; |
4197 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
4198 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
4199 | |
4200 | |
4201 | |
4202 | if (UnqualT1 == UnqualT2) |
4203 | return ImplicitConversionSequence::Indistinguishable; |
4204 | |
4205 | ImplicitConversionSequence::CompareKind Result |
4206 | = ImplicitConversionSequence::Indistinguishable; |
4207 | |
4208 | |
4209 | |
4210 | |
4211 | if (SCS1.QualificationIncludesObjCLifetime != |
4212 | SCS2.QualificationIncludesObjCLifetime) { |
4213 | Result = SCS1.QualificationIncludesObjCLifetime |
4214 | ? ImplicitConversionSequence::Worse |
4215 | : ImplicitConversionSequence::Better; |
4216 | } |
4217 | |
4218 | while (S.Context.UnwrapSimilarTypes(T1, T2)) { |
4219 | |
4220 | |
4221 | |
4222 | |
4223 | |
4224 | |
4225 | |
4226 | |
4227 | if (T1.getQualifiers().withoutObjCLifetime() == |
4228 | T2.getQualifiers().withoutObjCLifetime()) |
4229 | |
4230 | |
4231 | |
4232 | |
4233 | ; |
4234 | else if (T2.isMoreQualifiedThan(T1)) { |
4235 | |
4236 | if (Result == ImplicitConversionSequence::Worse) |
4237 | |
4238 | |
4239 | return ImplicitConversionSequence::Indistinguishable; |
4240 | |
4241 | Result = ImplicitConversionSequence::Better; |
4242 | } else if (T1.isMoreQualifiedThan(T2)) { |
4243 | |
4244 | if (Result == ImplicitConversionSequence::Better) |
4245 | |
4246 | |
4247 | return ImplicitConversionSequence::Indistinguishable; |
4248 | |
4249 | Result = ImplicitConversionSequence::Worse; |
4250 | } else { |
4251 | |
4252 | return ImplicitConversionSequence::Indistinguishable; |
4253 | } |
4254 | |
4255 | |
4256 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
4257 | break; |
4258 | } |
4259 | |
4260 | |
4261 | |
4262 | switch (Result) { |
4263 | case ImplicitConversionSequence::Better: |
4264 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
4265 | Result = ImplicitConversionSequence::Indistinguishable; |
4266 | break; |
4267 | |
4268 | case ImplicitConversionSequence::Indistinguishable: |
4269 | break; |
4270 | |
4271 | case ImplicitConversionSequence::Worse: |
4272 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
4273 | Result = ImplicitConversionSequence::Indistinguishable; |
4274 | break; |
4275 | } |
4276 | |
4277 | return Result; |
4278 | } |
4279 | |
4280 | |
4281 | |
4282 | |
4283 | |
4284 | |
4285 | static ImplicitConversionSequence::CompareKind |
4286 | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
4287 | const StandardConversionSequence& SCS1, |
4288 | const StandardConversionSequence& SCS2) { |
4289 | QualType FromType1 = SCS1.getFromType(); |
4290 | QualType ToType1 = SCS1.getToType(1); |
4291 | QualType FromType2 = SCS2.getFromType(); |
4292 | QualType ToType2 = SCS2.getToType(1); |
4293 | |
4294 | |
4295 | |
4296 | if (SCS1.First == ICK_Array_To_Pointer) |
4297 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
4298 | if (SCS2.First == ICK_Array_To_Pointer) |
4299 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
4300 | |
4301 | |
4302 | FromType1 = S.Context.getCanonicalType(FromType1); |
4303 | ToType1 = S.Context.getCanonicalType(ToType1); |
4304 | FromType2 = S.Context.getCanonicalType(FromType2); |
4305 | ToType2 = S.Context.getCanonicalType(ToType2); |
4306 | |
4307 | |
4308 | |
4309 | |
4310 | |
4311 | |
4312 | |
4313 | if (SCS1.Second == ICK_Pointer_Conversion && |
4314 | SCS2.Second == ICK_Pointer_Conversion && |
4315 | |
4316 | FromType1->isPointerType() && FromType2->isPointerType() && |
4317 | ToType1->isPointerType() && ToType2->isPointerType()) { |
4318 | QualType FromPointee1 = |
4319 | FromType1->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4320 | QualType ToPointee1 = |
4321 | ToType1->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4322 | QualType FromPointee2 = |
4323 | FromType2->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4324 | QualType ToPointee2 = |
4325 | ToType2->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4326 | |
4327 | |
4328 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
4329 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
4330 | return ImplicitConversionSequence::Better; |
4331 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
4332 | return ImplicitConversionSequence::Worse; |
4333 | } |
4334 | |
4335 | |
4336 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
4337 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4338 | return ImplicitConversionSequence::Better; |
4339 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4340 | return ImplicitConversionSequence::Worse; |
4341 | } |
4342 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
4343 | SCS2.Second == ICK_Pointer_Conversion) { |
4344 | const ObjCObjectPointerType *FromPtr1 |
4345 | = FromType1->getAs<ObjCObjectPointerType>(); |
4346 | const ObjCObjectPointerType *FromPtr2 |
4347 | = FromType2->getAs<ObjCObjectPointerType>(); |
4348 | const ObjCObjectPointerType *ToPtr1 |
4349 | = ToType1->getAs<ObjCObjectPointerType>(); |
4350 | const ObjCObjectPointerType *ToPtr2 |
4351 | = ToType2->getAs<ObjCObjectPointerType>(); |
4352 | |
4353 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
4354 | |
4355 | |
4356 | |
4357 | |
4358 | bool FromAssignLeft |
4359 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
4360 | bool FromAssignRight |
4361 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
4362 | bool ToAssignLeft |
4363 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
4364 | bool ToAssignRight |
4365 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
4366 | |
4367 | |
4368 | |
4369 | if (ToPtr1->isObjCIdType() && |
4370 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
4371 | return ImplicitConversionSequence::Worse; |
4372 | if (ToPtr2->isObjCIdType() && |
4373 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
4374 | return ImplicitConversionSequence::Better; |
4375 | |
4376 | |
4377 | |
4378 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
4379 | return ImplicitConversionSequence::Worse; |
4380 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
4381 | return ImplicitConversionSequence::Better; |
4382 | |
4383 | |
4384 | |
4385 | if (ToPtr1->isObjCClassType() && |
4386 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
4387 | return ImplicitConversionSequence::Worse; |
4388 | if (ToPtr2->isObjCClassType() && |
4389 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
4390 | return ImplicitConversionSequence::Better; |
4391 | |
4392 | |
4393 | |
4394 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
4395 | return ImplicitConversionSequence::Worse; |
4396 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
4397 | return ImplicitConversionSequence::Better; |
4398 | |
4399 | |
4400 | if (S.Context.hasSameType(FromType1, FromType2) && |
4401 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
4402 | (ToAssignLeft != ToAssignRight)) { |
4403 | if (FromPtr1->isSpecialized()) { |
4404 | |
4405 | |
4406 | bool IsFirstSame = |
4407 | FromPtr1->getInterfaceDecl() == ToPtr1->getInterfaceDecl(); |
4408 | bool IsSecondSame = |
4409 | FromPtr1->getInterfaceDecl() == ToPtr2->getInterfaceDecl(); |
4410 | if (IsFirstSame) { |
4411 | if (!IsSecondSame) |
4412 | return ImplicitConversionSequence::Better; |
4413 | } else if (IsSecondSame) |
4414 | return ImplicitConversionSequence::Worse; |
4415 | } |
4416 | return ToAssignLeft? ImplicitConversionSequence::Worse |
4417 | : ImplicitConversionSequence::Better; |
4418 | } |
4419 | |
4420 | |
4421 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
4422 | (FromAssignLeft != FromAssignRight)) |
4423 | return FromAssignLeft? ImplicitConversionSequence::Better |
4424 | : ImplicitConversionSequence::Worse; |
4425 | } |
4426 | } |
4427 | |
4428 | |
4429 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
4430 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
4431 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
4432 | const auto *FromMemPointer1 = FromType1->castAs<MemberPointerType>(); |
4433 | const auto *ToMemPointer1 = ToType1->castAs<MemberPointerType>(); |
4434 | const auto *FromMemPointer2 = FromType2->castAs<MemberPointerType>(); |
4435 | const auto *ToMemPointer2 = ToType2->castAs<MemberPointerType>(); |
4436 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
4437 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
4438 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
4439 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
4440 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
4441 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
4442 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
4443 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
4444 | |
4445 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
4446 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
4447 | return ImplicitConversionSequence::Worse; |
4448 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
4449 | return ImplicitConversionSequence::Better; |
4450 | } |
4451 | |
4452 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
4453 | if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4454 | return ImplicitConversionSequence::Better; |
4455 | else if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4456 | return ImplicitConversionSequence::Worse; |
4457 | } |
4458 | } |
4459 | |
4460 | if (SCS1.Second == ICK_Derived_To_Base) { |
4461 | |
4462 | |
4463 | |
4464 | |
4465 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
4466 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
4467 | if (S.IsDerivedFrom(Loc, ToType1, ToType2)) |
4468 | return ImplicitConversionSequence::Better; |
4469 | else if (S.IsDerivedFrom(Loc, ToType2, ToType1)) |
4470 | return ImplicitConversionSequence::Worse; |
4471 | } |
4472 | |
4473 | |
4474 | |
4475 | |
4476 | |
4477 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
4478 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
4479 | if (S.IsDerivedFrom(Loc, FromType2, FromType1)) |
4480 | return ImplicitConversionSequence::Better; |
4481 | else if (S.IsDerivedFrom(Loc, FromType1, FromType2)) |
4482 | return ImplicitConversionSequence::Worse; |
4483 | } |
4484 | } |
4485 | |
4486 | return ImplicitConversionSequence::Indistinguishable; |
4487 | } |
4488 | |
4489 | |
4490 | |
4491 | static bool isTypeValid(QualType T) { |
4492 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
4493 | return !Record->isInvalidDecl(); |
4494 | |
4495 | return true; |
4496 | } |
4497 | |
4498 | static QualType withoutUnaligned(ASTContext &Ctx, QualType T) { |
4499 | if (!T.getQualifiers().hasUnaligned()) |
4500 | return T; |
4501 | |
4502 | Qualifiers Q; |
4503 | T = Ctx.getUnqualifiedArrayType(T, Q); |
4504 | Q.removeUnaligned(); |
4505 | return Ctx.getQualifiedType(T, Q); |
4506 | } |
4507 | |
4508 | |
4509 | |
4510 | |
4511 | |
4512 | |
4513 | |
4514 | Sema::ReferenceCompareResult |
4515 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
4516 | QualType OrigT1, QualType OrigT2, |
4517 | ReferenceConversions *ConvOut) { |
4518 | assert(!OrigT1->isReferenceType() && |
4519 | "T1 must be the pointee type of the reference type"); |
4520 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
4521 | |
4522 | QualType T1 = Context.getCanonicalType(OrigT1); |
4523 | QualType T2 = Context.getCanonicalType(OrigT2); |
4524 | Qualifiers T1Quals, T2Quals; |
4525 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
4526 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
4527 | |
4528 | ReferenceConversions ConvTmp; |
4529 | ReferenceConversions &Conv = ConvOut ? *ConvOut : ConvTmp; |
4530 | Conv = ReferenceConversions(); |
4531 | |
4532 | |
4533 | |
4534 | |
4535 | |
4536 | |
4537 | |
4538 | |
4539 | |
4540 | |
4541 | |
4542 | |
4543 | QualType ConvertedT2; |
4544 | if (UnqualT1 == UnqualT2) { |
4545 | |
4546 | } else if (isCompleteType(Loc, OrigT2) && |
4547 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
4548 | IsDerivedFrom(Loc, UnqualT2, UnqualT1)) |
4549 | Conv |= ReferenceConversions::DerivedToBase; |
4550 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
4551 | UnqualT2->isObjCObjectOrInterfaceType() && |
4552 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
4553 | Conv |= ReferenceConversions::ObjC; |
4554 | else if (UnqualT2->isFunctionType() && |
4555 | IsFunctionConversion(UnqualT2, UnqualT1, ConvertedT2)) { |
4556 | Conv |= ReferenceConversions::Function; |
4557 | |
4558 | return Ref_Compatible; |
4559 | } |
4560 | bool ConvertedReferent = Conv != 0; |
4561 | |
4562 | |
4563 | |
4564 | bool PreviousToQualsIncludeConst = true; |
4565 | bool TopLevel = true; |
4566 | do { |
4567 | if (T1 == T2) |
4568 | break; |
4569 | |
4570 | |
4571 | Conv |= ReferenceConversions::Qualification; |
4572 | |
4573 | |
4574 | |
4575 | |
4576 | if (!TopLevel) |
4577 | Conv |= ReferenceConversions::NestedQualification; |
4578 | |
4579 | |
4580 | T1 = withoutUnaligned(Context, T1); |
4581 | T2 = withoutUnaligned(Context, T2); |
4582 | |
4583 | |
4584 | |
4585 | bool ObjCLifetimeConversion = false; |
4586 | if (!isQualificationConversionStep(T2, T1, false, TopLevel, |
4587 | PreviousToQualsIncludeConst, |
4588 | ObjCLifetimeConversion)) |
4589 | return (ConvertedReferent || Context.hasSimilarType(T1, T2)) |
4590 | ? Ref_Related |
4591 | : Ref_Incompatible; |
4592 | |
4593 | |
4594 | if (ObjCLifetimeConversion) |
4595 | Conv |= ReferenceConversions::ObjCLifetime; |
4596 | |
4597 | TopLevel = false; |
4598 | } while (Context.UnwrapSimilarTypes(T1, T2)); |
4599 | |
4600 | |
4601 | |
4602 | |
4603 | return (ConvertedReferent || Context.hasSameUnqualifiedType(T1, T2)) |
4604 | ? Ref_Compatible |
4605 | : Ref_Incompatible; |
4606 | } |
4607 | |
4608 | |
4609 | |
4610 | static bool |
4611 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
4612 | QualType DeclType, SourceLocation DeclLoc, |
4613 | Expr *Init, QualType T2, bool AllowRvalues, |
4614 | bool AllowExplicit) { |
4615 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
4616 | auto *T2RecordDecl = cast<CXXRecordDecl>(T2->castAs<RecordType>()->getDecl()); |
4617 | |
4618 | OverloadCandidateSet CandidateSet( |
4619 | DeclLoc, OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
4620 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
4621 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
4622 | NamedDecl *D = *I; |
4623 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
4624 | if (isa<UsingShadowDecl>(D)) |
4625 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
4626 | |
4627 | FunctionTemplateDecl *ConvTemplate |
4628 | = dyn_cast<FunctionTemplateDecl>(D); |
4629 | CXXConversionDecl *Conv; |
4630 | if (ConvTemplate) |
4631 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
4632 | else |
4633 | Conv = cast<CXXConversionDecl>(D); |
4634 | |
4635 | if (AllowRvalues) { |
4636 | |
4637 | |
4638 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
4639 | const ReferenceType *RefType |
4640 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
4641 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
4642 | continue; |
4643 | } |
4644 | |
4645 | if (!ConvTemplate && |
4646 | S.CompareReferenceRelationship( |
4647 | DeclLoc, |
4648 | Conv->getConversionType() |
4649 | .getNonReferenceType() |
4650 | .getUnqualifiedType(), |
4651 | DeclType.getNonReferenceType().getUnqualifiedType()) == |
4652 | Sema::Ref_Incompatible) |
4653 | continue; |
4654 | } else { |
4655 | |
4656 | |
4657 | |
4658 | |
4659 | const ReferenceType *RefType = |
4660 | Conv->getConversionType()->getAs<ReferenceType>(); |
4661 | if (!RefType || |
4662 | (!RefType->isLValueReferenceType() && |
4663 | !RefType->getPointeeType()->isFunctionType())) |
4664 | continue; |
4665 | } |
4666 | |
4667 | if (ConvTemplate) |
4668 | S.AddTemplateConversionCandidate( |
4669 | ConvTemplate, I.getPair(), ActingDC, Init, DeclType, CandidateSet, |
4670 | false, AllowExplicit); |
4671 | else |
4672 | S.AddConversionCandidate( |
4673 | Conv, I.getPair(), ActingDC, Init, DeclType, CandidateSet, |
4674 | false, AllowExplicit); |
4675 | } |
4676 | |
4677 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
4678 | |
4679 | OverloadCandidateSet::iterator Best; |
4680 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
4681 | case OR_Success: |
4682 | |
4683 | |
4684 | |
4685 | |
4686 | |
4687 | |
4688 | |
4689 | |
4690 | |
4691 | |
4692 | if (!Best->FinalConversion.DirectBinding) |
4693 | return false; |
4694 | |
4695 | ICS.setUserDefined(); |
4696 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
4697 | ICS.UserDefined.After = Best->FinalConversion; |
4698 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
4699 | ICS.UserDefined.ConversionFunction = Best->Function; |
4700 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
4701 | ICS.UserDefined.EllipsisConversion = false; |
4702 | assert(ICS.UserDefined.After.ReferenceBinding && |
4703 | ICS.UserDefined.After.DirectBinding && |
4704 | "Expected a direct reference binding!"); |
4705 | return true; |
4706 | |
4707 | case OR_Ambiguous: |
4708 | ICS.setAmbiguous(); |
4709 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
4710 | Cand != CandidateSet.end(); ++Cand) |
4711 | if (Cand->Best) |
4712 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
4713 | return true; |
4714 | |
4715 | case OR_No_Viable_Function: |
4716 | case OR_Deleted: |
4717 | |
4718 | |
4719 | return false; |
4720 | } |
4721 | |
4722 | llvm_unreachable("Invalid OverloadResult!"); |
4723 | } |
4724 | |
4725 | |
4726 | |
4727 | static ImplicitConversionSequence |
4728 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
4729 | SourceLocation DeclLoc, |
4730 | bool SuppressUserConversions, |
4731 | bool AllowExplicit) { |
4732 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
4733 | |
4734 | |
4735 | ImplicitConversionSequence ICS; |
4736 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4737 | |
4738 | QualType T1 = DeclType->castAs<ReferenceType>()->getPointeeType(); |
4739 | QualType T2 = Init->getType(); |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
4745 | DeclAccessPair Found; |
4746 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
4747 | false, Found)) |
4748 | T2 = Fn->getType(); |
4749 | } |
4750 | |
4751 | |
4752 | bool isRValRef = DeclType->isRValueReferenceType(); |
4753 | Expr::Classification InitCategory = Init->Classify(S.Context); |
4754 | |
4755 | Sema::ReferenceConversions RefConv; |
4756 | Sema::ReferenceCompareResult RefRelationship = |
4757 | S.CompareReferenceRelationship(DeclLoc, T1, T2, &RefConv); |
4758 | |
4759 | auto SetAsReferenceBinding = [&](bool BindsDirectly) { |
4760 | ICS.setStandard(); |
4761 | ICS.Standard.First = ICK_Identity; |
4762 | |
4763 | |
4764 | ICS.Standard.Second = (RefConv & Sema::ReferenceConversions::DerivedToBase) |
4765 | ? ICK_Derived_To_Base |
4766 | : (RefConv & Sema::ReferenceConversions::ObjC) |
4767 | ? ICK_Compatible_Conversion |
4768 | : ICK_Identity; |
4769 | |
4770 | |
4771 | |
4772 | ICS.Standard.Third = (RefConv & |
4773 | Sema::ReferenceConversions::NestedQualification) |
4774 | ? ICK_Qualification |
4775 | : ICK_Identity; |
4776 | ICS.Standard.setFromType(T2); |
4777 | ICS.Standard.setToType(0, T2); |
4778 | ICS.Standard.setToType(1, T1); |
4779 | ICS.Standard.setToType(2, T1); |
4780 | ICS.Standard.ReferenceBinding = true; |
4781 | ICS.Standard.DirectBinding = BindsDirectly; |
4782 | ICS.Standard.IsLvalueReference = !isRValRef; |
4783 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
4784 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
4785 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4786 | ICS.Standard.ObjCLifetimeConversionBinding = |
4787 | (RefConv & Sema::ReferenceConversions::ObjCLifetime) != 0; |
4788 | ICS.Standard.CopyConstructor = nullptr; |
4789 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
4790 | }; |
4791 | |
4792 | |
4793 | |
4794 | |
4795 | |
4796 | |
4797 | if (!isRValRef) { |
4798 | |
4799 | |
4800 | |
4801 | |
4802 | if (InitCategory.isLValue() && RefRelationship == Sema::Ref_Compatible) { |
4803 | |
4804 | |
4805 | |
4806 | |
4807 | |
4808 | |
4809 | |
4810 | SetAsReferenceBinding(true); |
4811 | |
4812 | |
4813 | |
4814 | |
4815 | |
4816 | return ICS; |
4817 | } |
4818 | |
4819 | |
4820 | |
4821 | |
4822 | |
4823 | |
4824 | |
4825 | |
4826 | if (!SuppressUserConversions && T2->isRecordType() && |
4827 | S.isCompleteType(DeclLoc, T2) && |
4828 | RefRelationship == Sema::Ref_Incompatible) { |
4829 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
4830 | Init, T2, false, |
4831 | AllowExplicit)) |
4832 | return ICS; |
4833 | } |
4834 | } |
4835 | |
4836 | |
4837 | |
4838 | |
4839 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) { |
4840 | if (InitCategory.isRValue() && RefRelationship != Sema::Ref_Incompatible) |
4841 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, DeclType); |
4842 | return ICS; |
4843 | } |
4844 | |
4845 | |
4846 | |
4847 | |
4848 | |
4849 | if (RefRelationship == Sema::Ref_Compatible && |
4850 | (InitCategory.isXValue() || |
4851 | (InitCategory.isPRValue() && |
4852 | (T2->isRecordType() || T2->isArrayType())) || |
4853 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
4854 | |
4855 | |
4856 | |
4857 | |
4858 | |
4859 | SetAsReferenceBinding(S.getLangOpts().CPlusPlus11 || |
4860 | !(InitCategory.isPRValue() || T2->isRecordType())); |
4861 | return ICS; |
4862 | } |
4863 | |
4864 | |
4865 | |
4866 | |
4867 | |
4868 | |
4869 | |
4870 | |
4871 | |
4872 | |
4873 | |
4874 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
4875 | T2->isRecordType() && S.isCompleteType(DeclLoc, T2) && |
4876 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
4877 | Init, T2, true, |
4878 | AllowExplicit)) { |
4879 | |
4880 | |
4881 | |
4882 | |
4883 | if (ICS.isUserDefined() && isRValRef && |
4884 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
4885 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4886 | |
4887 | return ICS; |
4888 | } |
4889 | |
4890 | |
4891 | if (T1->isFunctionType()) |
4892 | return ICS; |
4893 | |
4894 | |
4895 | |
4896 | |
4897 | |
4898 | |
4899 | |
4900 | |
4901 | if (RefRelationship == Sema::Ref_Related) { |
4902 | |
4903 | |
4904 | |
4905 | |
4906 | |
4907 | |
4908 | |
4909 | Qualifiers T1Quals = T1.getQualifiers(); |
4910 | Qualifiers T2Quals = T2.getQualifiers(); |
4911 | T1Quals.removeObjCGCAttr(); |
4912 | T1Quals.removeObjCLifetime(); |
4913 | T2Quals.removeObjCGCAttr(); |
4914 | T2Quals.removeObjCLifetime(); |
4915 | |
4916 | T1Quals.removeUnaligned(); |
4917 | T2Quals.removeUnaligned(); |
4918 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
4919 | return ICS; |
4920 | } |
4921 | |
4922 | |
4923 | |
4924 | |
4925 | |
4926 | |
4927 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
4928 | (T1->isRecordType() || T2->isRecordType())) |
4929 | return ICS; |
4930 | |
4931 | |
4932 | |
4933 | if (RefRelationship >= Sema::Ref_Related && isRValRef && |
4934 | Init->Classify(S.Context).isLValue()) { |
4935 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, Init, DeclType); |
4936 | return ICS; |
4937 | } |
4938 | |
4939 | |
4940 | |
4941 | |
4942 | |
4943 | |
4944 | |
4945 | |
4946 | |
4947 | |
4948 | |
4949 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
4950 | AllowedExplicit::None, |
4951 | false, |
4952 | false, |
4953 | false, |
4954 | false); |
4955 | |
4956 | |
4957 | if (ICS.isStandard()) { |
4958 | ICS.Standard.ReferenceBinding = true; |
4959 | ICS.Standard.IsLvalueReference = !isRValRef; |
4960 | ICS.Standard.BindsToFunctionLvalue = false; |
4961 | ICS.Standard.BindsToRvalue = true; |
4962 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4963 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
4964 | } else if (ICS.isUserDefined()) { |
4965 | const ReferenceType *LValRefType = |
4966 | ICS.UserDefined.ConversionFunction->getReturnType() |
4967 | ->getAs<LValueReferenceType>(); |
4968 | |
4969 | |
4970 | |
4971 | |
4972 | |
4973 | |
4974 | |
4975 | if (isRValRef && LValRefType) { |
4976 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4977 | return ICS; |
4978 | } |
4979 | |
4980 | ICS.UserDefined.After.ReferenceBinding = true; |
4981 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
4982 | ICS.UserDefined.After.BindsToFunctionLvalue = false; |
4983 | ICS.UserDefined.After.BindsToRvalue = !LValRefType; |
4984 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4985 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
4986 | } |
4987 | |
4988 | return ICS; |
4989 | } |
4990 | |
4991 | static ImplicitConversionSequence |
4992 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
4993 | bool SuppressUserConversions, |
4994 | bool InOverloadResolution, |
4995 | bool AllowObjCWritebackConversion, |
4996 | bool AllowExplicit = false); |
4997 | |
4998 | |
4999 | |
5000 | static ImplicitConversionSequence |
5001 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
5002 | bool SuppressUserConversions, |
5003 | bool InOverloadResolution, |
5004 | bool AllowObjCWritebackConversion) { |
5005 | |
5006 | |
5007 | |
5008 | |
5009 | ImplicitConversionSequence Result; |
5010 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
5011 | |
5012 | |
5013 | |
5014 | if (!S.isCompleteType(From->getBeginLoc(), ToType)) |
5015 | return Result; |
5016 | |
5017 | |
5018 | |
5019 | |
5020 | |
5021 | |
5022 | |
5023 | |
5024 | |
5025 | |
5026 | |
5027 | if (From->getNumInits() == 1) { |
5028 | if (ToType->isRecordType()) { |
5029 | QualType InitType = From->getInit(0)->getType(); |
5030 | if (S.Context.hasSameUnqualifiedType(InitType, ToType) || |
5031 | S.IsDerivedFrom(From->getBeginLoc(), InitType, ToType)) |
5032 | return TryCopyInitialization(S, From->getInit(0), ToType, |
5033 | SuppressUserConversions, |
5034 | InOverloadResolution, |
5035 | AllowObjCWritebackConversion); |
5036 | } |
5037 | |
5038 | if (const auto *AT = S.Context.getAsArrayType(ToType)) { |
5039 | if (S.IsStringInit(From->getInit(0), AT)) { |
5040 | InitializedEntity Entity = |
5041 | InitializedEntity::InitializeParameter(S.Context, ToType, |
5042 | false); |
5043 | if (S.CanPerformCopyInitialization(Entity, From)) { |
5044 | Result.setStandard(); |
5045 | Result.Standard.setAsIdentityConversion(); |
5046 | Result.Standard.setFromType(ToType); |
5047 | Result.Standard.setAllToTypes(ToType); |
5048 | return Result; |
5049 | } |
5050 | } |
5051 | } |
5052 | } |
5053 | |
5054 | |
5055 | |
5056 | |
5057 | |
5058 | |
5059 | |
5060 | |
5061 | |
5062 | |
5063 | |
5064 | |
5065 | |
5066 | |
5067 | |
5068 | |
5069 | bool toStdInitializerList = false; |
5070 | QualType X; |
5071 | if (ToType->isArrayType()) |
5072 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
5073 | else |
5074 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
5075 | if (!X.isNull()) { |
5076 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
5077 | Expr *Init = From->getInit(i); |
5078 | ImplicitConversionSequence ICS = |
5079 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
5080 | InOverloadResolution, |
5081 | AllowObjCWritebackConversion); |
5082 | |
5083 | if (ICS.isBad()) { |
5084 | Result = ICS; |
5085 | break; |
5086 | } |
5087 | |
5088 | if (Result.isBad() || CompareImplicitConversionSequences( |
5089 | S, From->getBeginLoc(), ICS, Result) == |
5090 | ImplicitConversionSequence::Worse) |
5091 | Result = ICS; |
5092 | } |
5093 | |
5094 | |
5095 | |
5096 | if (From->getNumInits() == 0) { |
5097 | Result.setStandard(); |
5098 | Result.Standard.setAsIdentityConversion(); |
5099 | Result.Standard.setFromType(ToType); |
5100 | Result.Standard.setAllToTypes(ToType); |
5101 | } |
5102 | |
5103 | Result.setStdInitializerListElement(toStdInitializerList); |
5104 | return Result; |
5105 | } |
5106 | |
5107 | |
5108 | |
5109 | |
5110 | |
5111 | |
5112 | |
5113 | |
5114 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
5115 | |
5116 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
5117 | AllowedExplicit::None, |
5118 | InOverloadResolution, false, |
5119 | AllowObjCWritebackConversion, |
5120 | false); |
5121 | } |
5122 | |
5123 | |
5124 | |
5125 | |
5126 | |
5127 | |
5128 | if (ToType->isAggregateType()) { |
5129 | |
5130 | |
5131 | |
5132 | InitializedEntity Entity = |
5133 | InitializedEntity::InitializeParameter(S.Context, ToType, |
5134 | false); |
5135 | if (S.CanPerformAggregateInitializationForOverloadResolution(Entity, |
5136 | From)) { |
5137 | Result.setUserDefined(); |
5138 | Result.UserDefined.Before.setAsIdentityConversion(); |
5139 | |
5140 | Result.UserDefined.Before.setFromType(QualType()); |
5141 | Result.UserDefined.Before.setAllToTypes(QualType()); |
5142 | |
5143 | Result.UserDefined.After.setAsIdentityConversion(); |
5144 | Result.UserDefined.After.setFromType(ToType); |
5145 | Result.UserDefined.After.setAllToTypes(ToType); |
5146 | Result.UserDefined.ConversionFunction = nullptr; |
5147 | } |
5148 | return Result; |
5149 | } |
5150 | |
5151 | |
5152 | |
5153 | |
5154 | if (ToType->isReferenceType()) { |
5155 | |
5156 | |
5157 | |
5158 | |
5159 | QualType T1 = ToType->castAs<ReferenceType>()->getPointeeType(); |
5160 | |
5161 | |
5162 | |
5163 | if (From->getNumInits() == 1) { |
5164 | Expr *Init = From->getInit(0); |
5165 | |
5166 | QualType T2 = Init->getType(); |
5167 | |
5168 | |
5169 | |
5170 | |
5171 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
5172 | DeclAccessPair Found; |
5173 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
5174 | Init, ToType, false, Found)) |
5175 | T2 = Fn->getType(); |
5176 | } |
5177 | |
5178 | |
5179 | Sema::ReferenceCompareResult RefRelationship = |
5180 | S.CompareReferenceRelationship(From->getBeginLoc(), T1, T2); |
5181 | |
5182 | if (RefRelationship >= Sema::Ref_Related) { |
5183 | return TryReferenceInit(S, Init, ToType, From->getBeginLoc(), |
5184 | SuppressUserConversions, |
5185 | false); |
5186 | } |
5187 | } |
5188 | |
5189 | |
5190 | |
5191 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
5192 | InOverloadResolution, |
5193 | AllowObjCWritebackConversion); |
5194 | if (Result.isFailure()) |
5195 | return Result; |
5196 | assert(!Result.isEllipsis() && |
5197 | "Sub-initialization cannot result in ellipsis conversion."); |
5198 | |
5199 | |
5200 | if (ToType->isRValueReferenceType() || |
5201 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
5202 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
5203 | Result.UserDefined.After; |
5204 | SCS.ReferenceBinding = true; |
5205 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
5206 | SCS.BindsToRvalue = true; |
5207 | SCS.BindsToFunctionLvalue = false; |
5208 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
5209 | SCS.ObjCLifetimeConversionBinding = false; |
5210 | } else |
5211 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
5212 | From, ToType); |
5213 | return Result; |
5214 | } |
5215 | |
5216 | |
5217 | |
5218 | |
5219 | if (!ToType->isRecordType()) { |
5220 | |
5221 | |
5222 | |
5223 | unsigned NumInits = From->getNumInits(); |
5224 | if (NumInits == 1 && !isa<InitListExpr>(From->getInit(0))) |
5225 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
5226 | SuppressUserConversions, |
5227 | InOverloadResolution, |
5228 | AllowObjCWritebackConversion); |
5229 | |
5230 | |
5231 | else if (NumInits == 0) { |
5232 | Result.setStandard(); |
5233 | Result.Standard.setAsIdentityConversion(); |
5234 | Result.Standard.setFromType(ToType); |
5235 | Result.Standard.setAllToTypes(ToType); |
5236 | } |
5237 | return Result; |
5238 | } |
5239 | |
5240 | |
5241 | |
5242 | |
5243 | return Result; |
5244 | } |
5245 | |
5246 | |
5247 | |
5248 | |
5249 | |
5250 | |
5251 | |
5252 | static ImplicitConversionSequence |
5253 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
5254 | bool SuppressUserConversions, |
5255 | bool InOverloadResolution, |
5256 | bool AllowObjCWritebackConversion, |
5257 | bool AllowExplicit) { |
5258 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
5259 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
5260 | InOverloadResolution,AllowObjCWritebackConversion); |
5261 | |
5262 | if (ToType->isReferenceType()) |
5263 | return TryReferenceInit(S, From, ToType, |
5264 | From->getBeginLoc(), |
5265 | SuppressUserConversions, AllowExplicit); |
5266 | |
5267 | return TryImplicitConversion(S, From, ToType, |
5268 | SuppressUserConversions, |
5269 | AllowedExplicit::None, |
5270 | InOverloadResolution, |
5271 | false, |
5272 | AllowObjCWritebackConversion, |
5273 | false); |
5274 | } |
5275 | |
5276 | static bool TryCopyInitialization(const CanQualType FromQTy, |
5277 | const CanQualType ToQTy, |
5278 | Sema &S, |
5279 | SourceLocation Loc, |
5280 | ExprValueKind FromVK) { |
5281 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
5282 | ImplicitConversionSequence ICS = |
5283 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
5284 | |
5285 | return !ICS.isBad(); |
5286 | } |
5287 | |
5288 | |
5289 | |
5290 | |
5291 | static ImplicitConversionSequence |
5292 | TryObjectArgumentInitialization(Sema &S, SourceLocation Loc, QualType FromType, |
5293 | Expr::Classification FromClassification, |
5294 | CXXMethodDecl *Method, |
5295 | CXXRecordDecl *ActingContext) { |
5296 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
5297 | |
5298 | |
5299 | Qualifiers Quals = Method->getMethodQualifiers(); |
5300 | if (isa<CXXDestructorDecl>(Method)) { |
5301 | Quals.addConst(); |
5302 | Quals.addVolatile(); |
5303 | } |
5304 | |
5305 | QualType ImplicitParamType = S.Context.getQualifiedType(ClassType, Quals); |
5306 | |
5307 | |
5308 | |
5309 | ImplicitConversionSequence ICS; |
5310 | |
5311 | |
5312 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
5313 | FromType = PT->getPointeeType(); |
5314 | |
5315 | |
5316 | |
5317 | assert(FromClassification.isLValue()); |
5318 | } |
5319 | |
5320 | assert(FromType->isRecordType()); |
5321 | |
5322 | |
5323 | |
5324 | |
5325 | |
5326 | |
5327 | |
5328 | |
5329 | |
5330 | |
5331 | |
5332 | |
5333 | |
5334 | |
5335 | |
5336 | |
5337 | |
5338 | |
5339 | |
5340 | |
5341 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
5342 | if (ImplicitParamType.getCVRQualifiers() |
5343 | != FromTypeCanon.getLocalCVRQualifiers() && |
5344 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
5345 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
5346 | FromType, ImplicitParamType); |
5347 | return ICS; |
5348 | } |
5349 | |
5350 | if (FromTypeCanon.hasAddressSpace()) { |
5351 | Qualifiers QualsImplicitParamType = ImplicitParamType.getQualifiers(); |
5352 | Qualifiers QualsFromType = FromTypeCanon.getQualifiers(); |
5353 | if (!QualsImplicitParamType.isAddressSpaceSupersetOf(QualsFromType)) { |
5354 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
5355 | FromType, ImplicitParamType); |
5356 | return ICS; |
5357 | } |
5358 | } |
5359 | |
5360 | |
5361 | |
5362 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
5363 | ImplicitConversionKind SecondKind; |
5364 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
5365 | SecondKind = ICK_Identity; |
5366 | } else if (S.IsDerivedFrom(Loc, FromType, ClassType)) |
5367 | SecondKind = ICK_Derived_To_Base; |
5368 | else { |
5369 | ICS.setBad(BadConversionSequence::unrelated_class, |
5370 | FromType, ImplicitParamType); |
5371 | return ICS; |
5372 | } |
5373 | |
5374 | |
5375 | switch (Method->getRefQualifier()) { |
5376 | case RQ_None: |
5377 | |
5378 | break; |
5379 | |
5380 | case RQ_LValue: |
5381 | if (!FromClassification.isLValue() && !Quals.hasOnlyConst()) { |
5382 | |
5383 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
5384 | ImplicitParamType); |
5385 | return ICS; |
5386 | } |
5387 | break; |
5388 | |
5389 | case RQ_RValue: |
5390 | if (!FromClassification.isRValue()) { |
5391 | |
5392 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
5393 | ImplicitParamType); |
5394 | return ICS; |
5395 | } |
5396 | break; |
5397 | } |
5398 | |
5399 | |
5400 | ICS.setStandard(); |
5401 | ICS.Standard.setAsIdentityConversion(); |
5402 | ICS.Standard.Second = SecondKind; |
5403 | ICS.Standard.setFromType(FromType); |
5404 | ICS.Standard.setAllToTypes(ImplicitParamType); |
5405 | ICS.Standard.ReferenceBinding = true; |
5406 | ICS.Standard.DirectBinding = true; |
5407 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
5408 | ICS.Standard.BindsToFunctionLvalue = false; |
5409 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
5410 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
5411 | = (Method->getRefQualifier() == RQ_None); |
5412 | return ICS; |
5413 | } |
5414 | |
5415 | |
5416 | |
5417 | |
5418 | ExprResult |
5419 | Sema::PerformObjectArgumentInitialization(Expr *From, |
5420 | NestedNameSpecifier *Qualifier, |
5421 | NamedDecl *FoundDecl, |
5422 | CXXMethodDecl *Method) { |
5423 | QualType FromRecordType, DestType; |
5424 | QualType ImplicitParamRecordType = |
5425 | Method->getThisType()->castAs<PointerType>()->getPointeeType(); |
5426 | |
5427 | Expr::Classification FromClassification; |
5428 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
5429 | FromRecordType = PT->getPointeeType(); |
5430 | DestType = Method->getThisType(); |
5431 | FromClassification = Expr::Classification::makeSimpleLValue(); |
5432 | } else { |
5433 | FromRecordType = From->getType(); |
5434 | DestType = ImplicitParamRecordType; |
5435 | FromClassification = From->Classify(Context); |
5436 | |
5437 | |
5438 | if (From->isPRValue()) { |
5439 | From = CreateMaterializeTemporaryExpr(FromRecordType, From, |
5440 | Method->getRefQualifier() != |
5441 | RefQualifierKind::RQ_RValue); |
5442 | } |
5443 | } |
5444 | |
5445 | |
5446 | |
5447 | ImplicitConversionSequence ICS = TryObjectArgumentInitialization( |
5448 | *this, From->getBeginLoc(), From->getType(), FromClassification, Method, |
5449 | Method->getParent()); |
5450 | if (ICS.isBad()) { |
5451 | switch (ICS.Bad.Kind) { |
5452 | case BadConversionSequence::bad_qualifiers: { |
5453 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
5454 | Qualifiers ToQs = DestType.getQualifiers(); |
5455 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
5456 | if (CVR) { |
5457 | Diag(From->getBeginLoc(), diag::err_member_function_call_bad_cvr) |
5458 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
5459 | << From->getSourceRange(); |
5460 | Diag(Method->getLocation(), diag::note_previous_decl) |
5461 | << Method->getDeclName(); |
5462 | return ExprError(); |
5463 | } |
5464 | break; |
5465 | } |
5466 | |
5467 | case BadConversionSequence::lvalue_ref_to_rvalue: |
5468 | case BadConversionSequence::rvalue_ref_to_lvalue: { |
5469 | bool IsRValueQualified = |
5470 | Method->getRefQualifier() == RefQualifierKind::RQ_RValue; |
5471 | Diag(From->getBeginLoc(), diag::err_member_function_call_bad_ref) |
5472 | << Method->getDeclName() << FromClassification.isRValue() |
5473 | << IsRValueQualified; |
5474 | Diag(Method->getLocation(), diag::note_previous_decl) |
5475 | << Method->getDeclName(); |
5476 | return ExprError(); |
5477 | } |
5478 | |
5479 | case BadConversionSequence::no_conversion: |
5480 | case BadConversionSequence::unrelated_class: |
5481 | break; |
5482 | } |
5483 | |
5484 | return Diag(From->getBeginLoc(), diag::err_member_function_call_bad_type) |
5485 | << ImplicitParamRecordType << FromRecordType |
5486 | << From->getSourceRange(); |
5487 | } |
5488 | |
5489 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
5490 | ExprResult FromRes = |
5491 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
5492 | if (FromRes.isInvalid()) |
5493 | return ExprError(); |
5494 | From = FromRes.get(); |
5495 | } |
5496 | |
5497 | if (!Context.hasSameType(From->getType(), DestType)) { |
5498 | CastKind CK; |
5499 | QualType PteeTy = DestType->getPointeeType(); |
5500 | LangAS DestAS = |
5501 | PteeTy.isNull() ? DestType.getAddressSpace() : PteeTy.getAddressSpace(); |
5502 | if (FromRecordType.getAddressSpace() != DestAS) |
5503 | CK = CK_AddressSpaceConversion; |
5504 | else |
5505 | CK = CK_NoOp; |
5506 | From = ImpCastExprToType(From, DestType, CK, From->getValueKind()).get(); |
5507 | } |
5508 | return From; |
5509 | } |
5510 | |
5511 | |
5512 | |
5513 | static ImplicitConversionSequence |
5514 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
5515 | |
5516 | |
5517 | |
5518 | |
5519 | if (From->getType()->isNullPtrType()) |
5520 | return ImplicitConversionSequence::getNullptrToBool(From->getType(), |
5521 | S.Context.BoolTy, |
5522 | From->isGLValue()); |
5523 | |
5524 | |
5525 | |
5526 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
5527 | false, |
5528 | AllowedExplicit::Conversions, |
5529 | false, |
5530 | false, |
5531 | false, |
5532 | false); |
5533 | } |
5534 | |
5535 | |
5536 | |
5537 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
5538 | if (checkPlaceholderForOverload(*this, From)) |
5539 | return ExprError(); |
5540 | |
5541 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
5542 | if (!ICS.isBad()) |
5543 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
5544 | |
5545 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
5546 | return Diag(From->getBeginLoc(), diag::err_typecheck_bool_condition) |
5547 | << From->getType() << From->getSourceRange(); |
5548 | return ExprError(); |
5549 | } |
5550 | |
5551 | |
5552 | |
5553 | |
5554 | static bool CheckConvertedConstantConversions(Sema &S, |
5555 | StandardConversionSequence &SCS) { |
5556 | |
5557 | |
5558 | |
5559 | switch (SCS.Second) { |
5560 | case ICK_Identity: |
5561 | case ICK_Integral_Promotion: |
5562 | case ICK_Integral_Conversion: |
5563 | case ICK_Zero_Queue_Conversion: |
5564 | return true; |
5565 | |
5566 | case ICK_Boolean_Conversion: |
5567 | |
5568 | |
5569 | |
5570 | |
5571 | |
5572 | |
5573 | |
5574 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
5575 | SCS.getToType(2)->isBooleanType(); |
5576 | |
5577 | case ICK_Pointer_Conversion: |
5578 | case ICK_Pointer_Member: |
5579 | |
5580 | |
5581 | return SCS.getFromType()->isNullPtrType(); |
5582 | |
5583 | case ICK_Floating_Promotion: |
5584 | case ICK_Complex_Promotion: |
5585 | case ICK_Floating_Conversion: |
5586 | case ICK_Complex_Conversion: |
5587 | case ICK_Floating_Integral: |
5588 | case ICK_Compatible_Conversion: |
5589 | case ICK_Derived_To_Base: |
5590 | case ICK_Vector_Conversion: |
5591 | case ICK_SVE_Vector_Conversion: |
5592 | case ICK_Vector_Splat: |
5593 | case ICK_Complex_Real: |
5594 | case ICK_Block_Pointer_Conversion: |
5595 | case ICK_TransparentUnionConversion: |
5596 | case ICK_Writeback_Conversion: |
5597 | case ICK_Zero_Event_Conversion: |
5598 | case ICK_C_Only_Conversion: |
5599 | case ICK_Incompatible_Pointer_Conversion: |
5600 | return false; |
5601 | |
5602 | case ICK_Lvalue_To_Rvalue: |
5603 | case ICK_Array_To_Pointer: |
5604 | case ICK_Function_To_Pointer: |
5605 | llvm_unreachable("found a first conversion kind in Second"); |
5606 | |
5607 | case ICK_Function_Conversion: |
5608 | case ICK_Qualification: |
5609 | llvm_unreachable("found a third conversion kind in Second"); |
5610 | |
5611 | case ICK_Num_Conversion_Kinds: |
5612 | break; |
5613 | } |
5614 | |
5615 | llvm_unreachable("unknown conversion kind"); |
5616 | } |
5617 | |
5618 | |
5619 | |
5620 | |
5621 | static ExprResult CheckConvertedConstantExpression(Sema &S, Expr *From, |
5622 | QualType T, APValue &Value, |
5623 | Sema::CCEKind CCE, |
5624 | bool RequireInt, |
5625 | NamedDecl *Dest) { |
5626 | assert(S.getLangOpts().CPlusPlus11 && |
5627 | "converted constant expression outside C++11"); |
5628 | |
5629 | if (checkPlaceholderForOverload(S, From)) |
5630 | return ExprError(); |
5631 | |
5632 | |
5633 | |
5634 | |
5635 | |
5636 | |
5637 | ImplicitConversionSequence ICS = |
5638 | CCE == Sema::CCEK_ExplicitBool |
5639 | ? TryContextuallyConvertToBool(S, From) |
5640 | : TryCopyInitialization(S, From, T, |
5641 | false, |
5642 | false, |
5643 | false, |
5644 | false); |
5645 | StandardConversionSequence *SCS = nullptr; |
5646 | switch (ICS.getKind()) { |
5647 | case ImplicitConversionSequence::StandardConversion: |
5648 | SCS = &ICS.Standard; |
5649 | break; |
5650 | case ImplicitConversionSequence::UserDefinedConversion: |
5651 | if (T->isRecordType()) |
5652 | SCS = &ICS.UserDefined.Before; |
5653 | else |
5654 | SCS = &ICS.UserDefined.After; |
5655 | break; |
5656 | case ImplicitConversionSequence::AmbiguousConversion: |
5657 | case ImplicitConversionSequence::BadConversion: |
5658 | if (!S.DiagnoseMultipleUserDefinedConversion(From, T)) |
5659 | return S.Diag(From->getBeginLoc(), |
5660 | diag::err_typecheck_converted_constant_expression) |
5661 | << From->getType() << From->getSourceRange() << T; |
5662 | return ExprError(); |
5663 | |
5664 | case ImplicitConversionSequence::EllipsisConversion: |
5665 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
5666 | } |
5667 | |
5668 | |
5669 | if (!CheckConvertedConstantConversions(S, *SCS)) { |
5670 | return S.Diag(From->getBeginLoc(), |
5671 | diag::err_typecheck_converted_constant_expression_disallowed) |
5672 | << From->getType() << From->getSourceRange() << T; |
5673 | } |
5674 | |
5675 | if (SCS->ReferenceBinding && !SCS->DirectBinding) { |
5676 | return S.Diag(From->getBeginLoc(), |
5677 | diag::err_typecheck_converted_constant_expression_indirect) |
5678 | << From->getType() << From->getSourceRange() << T; |
5679 | } |
5680 | |
5681 | |
5682 | |
5683 | |
5684 | ExprResult Result; |
5685 | if (T->isRecordType()) { |
5686 | assert(CCE == Sema::CCEK_TemplateArg && |
5687 | "unexpected class type converted constant expr"); |
5688 | Result = S.PerformCopyInitialization( |
5689 | InitializedEntity::InitializeTemplateParameter( |
5690 | T, cast<NonTypeTemplateParmDecl>(Dest)), |
5691 | SourceLocation(), From); |
5692 | } else { |
5693 | Result = S.PerformImplicitConversion(From, T, ICS, Sema::AA_Converting); |
5694 | } |
5695 | if (Result.isInvalid()) |
5696 | return Result; |
5697 | |
5698 | |
5699 | |
5700 | Result = |
5701 | S.ActOnFinishFullExpr(Result.get(), From->getExprLoc(), |
5702 | false, true); |
5703 | if (Result.isInvalid()) |
5704 | return Result; |
5705 | |
5706 | |
5707 | bool ReturnPreNarrowingValue = false; |
5708 | APValue PreNarrowingValue; |
5709 | QualType PreNarrowingType; |
5710 | switch (SCS->getNarrowingKind(S.Context, Result.get(), PreNarrowingValue, |
5711 | PreNarrowingType)) { |
5712 | case NK_Dependent_Narrowing: |
5713 | |
5714 | |
5715 | case NK_Variable_Narrowing: |
5716 | |
5717 | |
5718 | case NK_Not_Narrowing: |
5719 | break; |
5720 | |
5721 | case NK_Constant_Narrowing: |
5722 | if (CCE == Sema::CCEK_ArrayBound && |
5723 | PreNarrowingType->isIntegralOrEnumerationType() && |
5724 | PreNarrowingValue.isInt()) { |
5725 | |
5726 | |
5727 | ReturnPreNarrowingValue = true; |
5728 | break; |
5729 | } |
5730 | S.Diag(From->getBeginLoc(), diag::ext_cce_narrowing) |
5731 | << CCE << 1 |
5732 | << PreNarrowingValue.getAsString(S.Context, PreNarrowingType) << T; |
5733 | break; |
5734 | |
5735 | case NK_Type_Narrowing: |
5736 | |
5737 | |
5738 | S.Diag(From->getBeginLoc(), diag::ext_cce_narrowing) |
5739 | << CCE << 0 << From->getType() << T; |
5740 | break; |
5741 | } |
5742 | |
5743 | if (Result.get()->isValueDependent()) { |
5744 | Value = APValue(); |
5745 | return Result; |
5746 | } |
5747 | |
5748 | |
5749 | SmallVector<PartialDiagnosticAt, 8> Notes; |
5750 | Expr::EvalResult Eval; |
5751 | Eval.Diag = &Notes; |
5752 | |
5753 | ConstantExprKind Kind; |
5754 | if (CCE == Sema::CCEK_TemplateArg && T->isRecordType()) |
5755 | Kind = ConstantExprKind::ClassTemplateArgument; |
5756 | else if (CCE == Sema::CCEK_TemplateArg) |
5757 | Kind = ConstantExprKind::NonClassTemplateArgument; |
5758 | else |
5759 | Kind = ConstantExprKind::Normal; |
5760 | |
5761 | if (!Result.get()->EvaluateAsConstantExpr(Eval, S.Context, Kind) || |
5762 | (RequireInt && !Eval.Val.isInt())) { |
5763 | |
5764 | |
5765 | Result = ExprError(); |
5766 | } else { |
5767 | Value = Eval.Val; |
5768 | |
5769 | if (Notes.empty()) { |
5770 | |
5771 | Expr *E = ConstantExpr::Create(S.Context, Result.get(), Value); |
5772 | if (ReturnPreNarrowingValue) |
5773 | Value = std::move(PreNarrowingValue); |
5774 | return E; |
5775 | } |
5776 | } |
5777 | |
5778 | |
5779 | if (Notes.size() == 1 && |
5780 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) { |
5781 | S.Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
5782 | } else if (!Notes.empty() && Notes[0].second.getDiagID() == |
5783 | diag::note_constexpr_invalid_template_arg) { |
5784 | Notes[0].second.setDiagID(diag::err_constexpr_invalid_template_arg); |
5785 | for (unsigned I = 0; I < Notes.size(); ++I) |
5786 | S.Diag(Notes[I].first, Notes[I].second); |
5787 | } else { |
5788 | S.Diag(From->getBeginLoc(), diag::err_expr_not_cce) |
5789 | << CCE << From->getSourceRange(); |
5790 | for (unsigned I = 0; I < Notes.size(); ++I) |
5791 | S.Diag(Notes[I].first, Notes[I].second); |
5792 | } |
5793 | return ExprError(); |
5794 | } |
5795 | |
5796 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
5797 | APValue &Value, CCEKind CCE, |
5798 | NamedDecl *Dest) { |
5799 | return ::CheckConvertedConstantExpression(*this, From, T, Value, CCE, false, |
5800 | Dest); |
5801 | } |
5802 | |
5803 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
5804 | llvm::APSInt &Value, |
5805 | CCEKind CCE) { |
5806 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
5807 | |
5808 | APValue V; |
5809 | auto R = ::CheckConvertedConstantExpression(*this, From, T, V, CCE, true, |
5810 | nullptr); |
5811 | if (!R.isInvalid() && !R.get()->isValueDependent()) |
5812 | Value = V.getInt(); |
5813 | return R; |
5814 | } |
5815 | |
5816 | |
5817 | |
5818 | |
5819 | |
5820 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
5821 | if (SCS.Second == ICK_Pointer_Conversion) { |
5822 | SCS.Second = ICK_Identity; |
5823 | SCS.Third = ICK_Identity; |
5824 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
5825 | } |
5826 | } |
5827 | |
5828 | |
5829 | |
5830 | static ImplicitConversionSequence |
5831 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
5832 | |
5833 | QualType Ty = S.Context.getObjCIdType(); |
5834 | ImplicitConversionSequence ICS |
5835 | = TryImplicitConversion(S, From, Ty, |
5836 | |
5837 | false, |
5838 | AllowedExplicit::Conversions, |
5839 | false, |
5840 | false, |
5841 | false, |
5842 | true); |
5843 | |
5844 | |
5845 | switch (ICS.getKind()) { |
5846 | case ImplicitConversionSequence::BadConversion: |
5847 | case ImplicitConversionSequence::AmbiguousConversion: |
5848 | case ImplicitConversionSequence::EllipsisConversion: |
5849 | break; |
5850 | |
5851 | case ImplicitConversionSequence::UserDefinedConversion: |
5852 | dropPointerConversion(ICS.UserDefined.After); |
5853 | break; |
5854 | |
5855 | case ImplicitConversionSequence::StandardConversion: |
5856 | dropPointerConversion(ICS.Standard); |
5857 | break; |
5858 | } |
5859 | |
5860 | return ICS; |
5861 | } |
5862 | |
5863 | |
5864 | |
5865 | |
5866 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
5867 | if (checkPlaceholderForOverload(*this, From)) |
5868 | return ExprError(); |
5869 | |
5870 | QualType Ty = Context.getObjCIdType(); |
5871 | ImplicitConversionSequence ICS = |
5872 | TryContextuallyConvertToObjCPointer(*this, From); |
5873 | if (!ICS.isBad()) |
5874 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
5875 | return ExprResult(); |
5876 | } |
5877 | |
5878 | |
5879 | |
5880 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
5881 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
5882 | : T->isIntegralOrUnscopedEnumerationType(); |
5883 | } |
|