clang -cc1 -triple x86_64-pc-linux-gnu -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/tools/clang/lib/Sema -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/clang/lib/Sema/SemaOverload.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Sema/Overload.h" |
14 | #include "clang/AST/ASTContext.h" |
15 | #include "clang/AST/CXXInheritance.h" |
16 | #include "clang/AST/DeclObjC.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/TargetInfo.h" |
25 | #include "clang/Sema/Initialization.h" |
26 | #include "clang/Sema/Lookup.h" |
27 | #include "clang/Sema/SemaInternal.h" |
28 | #include "clang/Sema/Template.h" |
29 | #include "clang/Sema/TemplateDeduction.h" |
30 | #include "llvm/ADT/DenseSet.h" |
31 | #include "llvm/ADT/Optional.h" |
32 | #include "llvm/ADT/STLExtras.h" |
33 | #include "llvm/ADT/SmallPtrSet.h" |
34 | #include "llvm/ADT/SmallString.h" |
35 | #include <algorithm> |
36 | #include <cstdlib> |
37 | |
38 | using namespace clang; |
39 | using namespace sema; |
40 | |
41 | static bool functionHasPassObjectSizeParams(const FunctionDecl *FD) { |
42 | return llvm::any_of(FD->parameters(), [](const ParmVarDecl *P) { |
43 | return P->hasAttr<PassObjectSizeAttr>(); |
44 | }); |
45 | } |
46 | |
47 | |
48 | static ExprResult |
49 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
50 | const Expr *Base, bool HadMultipleCandidates, |
51 | SourceLocation Loc = SourceLocation(), |
52 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
53 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
54 | return ExprError(); |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
62 | return ExprError(); |
63 | DeclRefExpr *DRE = new (S.Context) |
64 | DeclRefExpr(S.Context, Fn, false, Fn->getType(), VK_LValue, Loc, LocInfo); |
65 | if (HadMultipleCandidates) |
66 | DRE->setHadMultipleCandidates(true); |
67 | |
68 | S.MarkDeclRefReferenced(DRE, Base); |
69 | if (auto *FPT = DRE->getType()->getAs<FunctionProtoType>()) { |
70 | if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { |
71 | S.ResolveExceptionSpec(Loc, FPT); |
72 | DRE->setType(Fn->getType()); |
73 | } |
74 | } |
75 | return S.ImpCastExprToType(DRE, S.Context.getPointerType(DRE->getType()), |
76 | CK_FunctionToPointerDecay); |
77 | } |
78 | |
79 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
80 | bool InOverloadResolution, |
81 | StandardConversionSequence &SCS, |
82 | bool CStyle, |
83 | bool AllowObjCWritebackConversion); |
84 | |
85 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
86 | QualType &ToType, |
87 | bool InOverloadResolution, |
88 | StandardConversionSequence &SCS, |
89 | bool CStyle); |
90 | static OverloadingResult |
91 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
92 | UserDefinedConversionSequence& User, |
93 | OverloadCandidateSet& Conversions, |
94 | bool AllowExplicit, |
95 | bool AllowObjCConversionOnExplicit); |
96 | |
97 | |
98 | static ImplicitConversionSequence::CompareKind |
99 | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
100 | const StandardConversionSequence& SCS1, |
101 | const StandardConversionSequence& SCS2); |
102 | |
103 | static ImplicitConversionSequence::CompareKind |
104 | CompareQualificationConversions(Sema &S, |
105 | const StandardConversionSequence& SCS1, |
106 | const StandardConversionSequence& SCS2); |
107 | |
108 | static ImplicitConversionSequence::CompareKind |
109 | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
110 | const StandardConversionSequence& SCS1, |
111 | const StandardConversionSequence& SCS2); |
112 | |
113 | |
114 | |
115 | ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { |
116 | static const ImplicitConversionRank |
117 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
118 | ICR_Exact_Match, |
119 | ICR_Exact_Match, |
120 | ICR_Exact_Match, |
121 | ICR_Exact_Match, |
122 | ICR_Exact_Match, |
123 | ICR_Exact_Match, |
124 | ICR_Promotion, |
125 | ICR_Promotion, |
126 | ICR_Promotion, |
127 | ICR_Conversion, |
128 | ICR_Conversion, |
129 | ICR_Conversion, |
130 | ICR_Conversion, |
131 | ICR_Conversion, |
132 | ICR_Conversion, |
133 | ICR_Conversion, |
134 | ICR_Conversion, |
135 | ICR_Conversion, |
136 | ICR_Conversion, |
137 | ICR_OCL_Scalar_Widening, |
138 | ICR_Complex_Real_Conversion, |
139 | ICR_Conversion, |
140 | ICR_Conversion, |
141 | ICR_Writeback_Conversion, |
142 | ICR_Exact_Match, |
143 | |
144 | |
145 | ICR_C_Conversion, |
146 | ICR_C_Conversion_Extension |
147 | }; |
148 | return Rank[(int)Kind]; |
149 | } |
150 | |
151 | |
152 | |
153 | static const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
154 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
155 | "No conversion", |
156 | "Lvalue-to-rvalue", |
157 | "Array-to-pointer", |
158 | "Function-to-pointer", |
159 | "Function pointer conversion", |
160 | "Qualification", |
161 | "Integral promotion", |
162 | "Floating point promotion", |
163 | "Complex promotion", |
164 | "Integral conversion", |
165 | "Floating conversion", |
166 | "Complex conversion", |
167 | "Floating-integral conversion", |
168 | "Pointer conversion", |
169 | "Pointer-to-member conversion", |
170 | "Boolean conversion", |
171 | "Compatible-types conversion", |
172 | "Derived-to-base conversion", |
173 | "Vector conversion", |
174 | "Vector splat", |
175 | "Complex-real conversion", |
176 | "Block Pointer conversion", |
177 | "Transparent Union Conversion", |
178 | "Writeback conversion", |
179 | "OpenCL Zero Event Conversion", |
180 | "C specific type conversion", |
181 | "Incompatible pointer conversion" |
182 | }; |
183 | return Name[Kind]; |
184 | } |
185 | |
186 | |
187 | |
188 | void StandardConversionSequence::setAsIdentityConversion() { |
189 | First = ICK_Identity; |
190 | Second = ICK_Identity; |
191 | Third = ICK_Identity; |
192 | DeprecatedStringLiteralToCharPtr = false; |
193 | QualificationIncludesObjCLifetime = false; |
194 | ReferenceBinding = false; |
195 | DirectBinding = false; |
196 | IsLvalueReference = true; |
197 | BindsToFunctionLvalue = false; |
198 | BindsToRvalue = false; |
199 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
200 | ObjCLifetimeConversionBinding = false; |
201 | CopyConstructor = nullptr; |
202 | } |
203 | |
204 | |
205 | |
206 | |
207 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
208 | ImplicitConversionRank Rank = ICR_Exact_Match; |
209 | if (GetConversionRank(First) > Rank) |
210 | Rank = GetConversionRank(First); |
211 | if (GetConversionRank(Second) > Rank) |
212 | Rank = GetConversionRank(Second); |
213 | if (GetConversionRank(Third) > Rank) |
214 | Rank = GetConversionRank(Third); |
215 | return Rank; |
216 | } |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | bool StandardConversionSequence::isPointerConversionToBool() const { |
223 | |
224 | |
225 | |
226 | |
227 | if (getToType(1)->isBooleanType() && |
228 | (getFromType()->isPointerType() || |
229 | getFromType()->isMemberPointerType() || |
230 | getFromType()->isObjCObjectPointerType() || |
231 | getFromType()->isBlockPointerType() || |
232 | getFromType()->isNullPtrType() || |
233 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
234 | return true; |
235 | |
236 | return false; |
237 | } |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | bool |
244 | StandardConversionSequence:: |
245 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
246 | QualType FromType = getFromType(); |
247 | QualType ToType = getToType(1); |
248 | |
249 | |
250 | |
251 | |
252 | if (First == ICK_Array_To_Pointer) |
253 | FromType = Context.getArrayDecayedType(FromType); |
254 | |
255 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
256 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
257 | return ToPtrType->getPointeeType()->isVoidType(); |
258 | |
259 | return false; |
260 | } |
261 | |
262 | |
263 | |
264 | static const Expr *IgnoreNarrowingConversion(ASTContext &Ctx, |
265 | const Expr *Converted) { |
266 | |
267 | |
268 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Converted)) { |
269 | Expr *Inner = |
270 | const_cast<Expr *>(IgnoreNarrowingConversion(Ctx, EWC->getSubExpr())); |
271 | return ExprWithCleanups::Create(Ctx, Inner, EWC->cleanupsHaveSideEffects(), |
272 | EWC->getObjects()); |
273 | } |
274 | |
275 | while (auto *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
276 | switch (ICE->getCastKind()) { |
277 | case CK_NoOp: |
278 | case CK_IntegralCast: |
279 | case CK_IntegralToBoolean: |
280 | case CK_IntegralToFloating: |
281 | case CK_BooleanToSignedIntegral: |
282 | case CK_FloatingToIntegral: |
283 | case CK_FloatingToBoolean: |
284 | case CK_FloatingCast: |
285 | Converted = ICE->getSubExpr(); |
286 | continue; |
287 | |
288 | default: |
289 | return Converted; |
290 | } |
291 | } |
292 | |
293 | return Converted; |
294 | } |
295 | |
296 | |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | NarrowingKind StandardConversionSequence::getNarrowingKind( |
308 | ASTContext &Ctx, const Expr *Converted, APValue &ConstantValue, |
309 | QualType &ConstantType, bool IgnoreFloatToIntegralConversion) const { |
310 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
311 | |
312 | |
313 | |
314 | QualType FromType = getToType(0); |
315 | QualType ToType = getToType(1); |
316 | |
317 | |
318 | |
319 | |
320 | if (auto *ET = ToType->getAs<EnumType>()) |
321 | ToType = ET->getDecl()->getIntegerType(); |
322 | |
323 | switch (Second) { |
324 | |
325 | case ICK_Boolean_Conversion: |
326 | if (FromType->isRealFloatingType()) |
327 | goto FloatingIntegralConversion; |
328 | if (FromType->isIntegralOrUnscopedEnumerationType()) |
329 | goto IntegralConversion; |
330 | |
331 | |
332 | return NK_Not_Narrowing; |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | case ICK_Floating_Integral: |
341 | FloatingIntegralConversion: |
342 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
343 | return NK_Type_Narrowing; |
344 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
345 | ToType->isRealFloatingType()) { |
346 | if (IgnoreFloatToIntegralConversion) |
347 | return NK_Not_Narrowing; |
348 | llvm::APSInt IntConstantValue; |
349 | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
350 | assert(Initializer && "Unknown conversion expression"); |
351 | |
352 | |
353 | if (Initializer->isValueDependent()) |
354 | return NK_Dependent_Narrowing; |
355 | |
356 | if (Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
357 | |
358 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
359 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
360 | llvm::APFloat::rmNearestTiesToEven); |
361 | |
362 | llvm::APSInt ConvertedValue = IntConstantValue; |
363 | bool ignored; |
364 | Result.convertToInteger(ConvertedValue, |
365 | llvm::APFloat::rmTowardZero, &ignored); |
366 | |
367 | if (IntConstantValue != ConvertedValue) { |
368 | ConstantValue = APValue(IntConstantValue); |
369 | ConstantType = Initializer->getType(); |
370 | return NK_Constant_Narrowing; |
371 | } |
372 | } else { |
373 | |
374 | return NK_Variable_Narrowing; |
375 | } |
376 | } |
377 | return NK_Not_Narrowing; |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | case ICK_Floating_Conversion: |
384 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
385 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
386 | |
387 | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
388 | |
389 | |
390 | if (Initializer->isValueDependent()) |
391 | return NK_Dependent_Narrowing; |
392 | |
393 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
394 | |
395 | assert(ConstantValue.isFloat()); |
396 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
397 | |
398 | bool ignored; |
399 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
400 | Ctx.getFloatTypeSemantics(ToType), |
401 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
402 | |
403 | |
404 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
405 | ConstantType = Initializer->getType(); |
406 | return NK_Constant_Narrowing; |
407 | } |
408 | } else { |
409 | return NK_Variable_Narrowing; |
410 | } |
411 | } |
412 | return NK_Not_Narrowing; |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | case ICK_Integral_Conversion: |
420 | IntegralConversion: { |
421 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
422 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
423 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
424 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
425 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
426 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
427 | |
428 | if (FromWidth > ToWidth || |
429 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
430 | (FromSigned && !ToSigned)) { |
431 | |
432 | llvm::APSInt InitializerValue; |
433 | const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); |
434 | |
435 | |
436 | if (Initializer->isValueDependent()) |
437 | return NK_Dependent_Narrowing; |
438 | |
439 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
440 | |
441 | return NK_Variable_Narrowing; |
442 | } |
443 | bool Narrowing = false; |
444 | if (FromWidth < ToWidth) { |
445 | |
446 | |
447 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
448 | Narrowing = true; |
449 | } else { |
450 | |
451 | |
452 | InitializerValue = InitializerValue.extend( |
453 | InitializerValue.getBitWidth() + 1); |
454 | |
455 | llvm::APSInt ConvertedValue = InitializerValue; |
456 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
457 | ConvertedValue.setIsSigned(ToSigned); |
458 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
459 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
460 | |
461 | if (ConvertedValue != InitializerValue) |
462 | Narrowing = true; |
463 | } |
464 | if (Narrowing) { |
465 | ConstantType = Initializer->getType(); |
466 | ConstantValue = APValue(InitializerValue); |
467 | return NK_Constant_Narrowing; |
468 | } |
469 | } |
470 | return NK_Not_Narrowing; |
471 | } |
472 | |
473 | default: |
474 | |
475 | return NK_Not_Narrowing; |
476 | } |
477 | } |
478 | |
479 | |
480 | |
481 | LLVM_DUMP_METHOD void StandardConversionSequence::dump() const { |
482 | raw_ostream &OS = llvm::errs(); |
483 | bool PrintedSomething = false; |
484 | if (First != ICK_Identity) { |
485 | OS << GetImplicitConversionName(First); |
486 | PrintedSomething = true; |
487 | } |
488 | |
489 | if (Second != ICK_Identity) { |
490 | if (PrintedSomething) { |
491 | OS << " -> "; |
492 | } |
493 | OS << GetImplicitConversionName(Second); |
494 | |
495 | if (CopyConstructor) { |
496 | OS << " (by copy constructor)"; |
497 | } else if (DirectBinding) { |
498 | OS << " (direct reference binding)"; |
499 | } else if (ReferenceBinding) { |
500 | OS << " (reference binding)"; |
501 | } |
502 | PrintedSomething = true; |
503 | } |
504 | |
505 | if (Third != ICK_Identity) { |
506 | if (PrintedSomething) { |
507 | OS << " -> "; |
508 | } |
509 | OS << GetImplicitConversionName(Third); |
510 | PrintedSomething = true; |
511 | } |
512 | |
513 | if (!PrintedSomething) { |
514 | OS << "No conversions required"; |
515 | } |
516 | } |
517 | |
518 | |
519 | |
520 | void UserDefinedConversionSequence::dump() const { |
521 | raw_ostream &OS = llvm::errs(); |
522 | if (Before.First || Before.Second || Before.Third) { |
523 | Before.dump(); |
524 | OS << " -> "; |
525 | } |
526 | if (ConversionFunction) |
527 | OS << '\'' << *ConversionFunction << '\''; |
528 | else |
529 | OS << "aggregate initialization"; |
530 | if (After.First || After.Second || After.Third) { |
531 | OS << " -> "; |
532 | After.dump(); |
533 | } |
534 | } |
535 | |
536 | |
537 | |
538 | void ImplicitConversionSequence::dump() const { |
539 | raw_ostream &OS = llvm::errs(); |
540 | if (isStdInitializerListElement()) |
541 | OS << "Worst std::initializer_list element conversion: "; |
542 | switch (ConversionKind) { |
543 | case StandardConversion: |
544 | OS << "Standard conversion: "; |
545 | Standard.dump(); |
546 | break; |
547 | case UserDefinedConversion: |
548 | OS << "User-defined conversion: "; |
549 | UserDefined.dump(); |
550 | break; |
551 | case EllipsisConversion: |
552 | OS << "Ellipsis conversion"; |
553 | break; |
554 | case AmbiguousConversion: |
555 | OS << "Ambiguous conversion"; |
556 | break; |
557 | case BadConversion: |
558 | OS << "Bad conversion"; |
559 | break; |
560 | } |
561 | |
562 | OS << "\n"; |
563 | } |
564 | |
565 | void AmbiguousConversionSequence::construct() { |
566 | new (&conversions()) ConversionSet(); |
567 | } |
568 | |
569 | void AmbiguousConversionSequence::destruct() { |
570 | conversions().~ConversionSet(); |
571 | } |
572 | |
573 | void |
574 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
575 | FromTypePtr = O.FromTypePtr; |
576 | ToTypePtr = O.ToTypePtr; |
577 | new (&conversions()) ConversionSet(O.conversions()); |
578 | } |
579 | |
580 | namespace { |
581 | |
582 | |
583 | struct DFIArguments { |
584 | TemplateArgument FirstArg; |
585 | TemplateArgument SecondArg; |
586 | }; |
587 | |
588 | |
589 | struct DFIParamWithArguments : DFIArguments { |
590 | TemplateParameter Param; |
591 | }; |
592 | |
593 | |
594 | struct DFIDeducedMismatchArgs : DFIArguments { |
595 | TemplateArgumentList *TemplateArgs; |
596 | unsigned CallArgIndex; |
597 | }; |
598 | |
599 | |
600 | struct CNSInfo { |
601 | TemplateArgumentList *TemplateArgs; |
602 | ConstraintSatisfaction Satisfaction; |
603 | }; |
604 | } |
605 | |
606 | |
607 | |
608 | DeductionFailureInfo |
609 | clang::MakeDeductionFailureInfo(ASTContext &Context, |
610 | Sema::TemplateDeductionResult TDK, |
611 | TemplateDeductionInfo &Info) { |
612 | DeductionFailureInfo Result; |
613 | Result.Result = static_cast<unsigned>(TDK); |
614 | Result.HasDiagnostic = false; |
615 | switch (TDK) { |
616 | case Sema::TDK_Invalid: |
617 | case Sema::TDK_InstantiationDepth: |
618 | case Sema::TDK_TooManyArguments: |
619 | case Sema::TDK_TooFewArguments: |
620 | case Sema::TDK_MiscellaneousDeductionFailure: |
621 | case Sema::TDK_CUDATargetMismatch: |
622 | Result.Data = nullptr; |
623 | break; |
624 | |
625 | case Sema::TDK_Incomplete: |
626 | case Sema::TDK_InvalidExplicitArguments: |
627 | Result.Data = Info.Param.getOpaqueValue(); |
628 | break; |
629 | |
630 | case Sema::TDK_DeducedMismatch: |
631 | case Sema::TDK_DeducedMismatchNested: { |
632 | |
633 | auto *Saved = new (Context) DFIDeducedMismatchArgs; |
634 | Saved->FirstArg = Info.FirstArg; |
635 | Saved->SecondArg = Info.SecondArg; |
636 | Saved->TemplateArgs = Info.take(); |
637 | Saved->CallArgIndex = Info.CallArgIndex; |
638 | Result.Data = Saved; |
639 | break; |
640 | } |
641 | |
642 | case Sema::TDK_NonDeducedMismatch: { |
643 | |
644 | DFIArguments *Saved = new (Context) DFIArguments; |
645 | Saved->FirstArg = Info.FirstArg; |
646 | Saved->SecondArg = Info.SecondArg; |
647 | Result.Data = Saved; |
648 | break; |
649 | } |
650 | |
651 | case Sema::TDK_IncompletePack: |
652 | |
653 | case Sema::TDK_Inconsistent: |
654 | case Sema::TDK_Underqualified: { |
655 | |
656 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
657 | Saved->Param = Info.Param; |
658 | Saved->FirstArg = Info.FirstArg; |
659 | Saved->SecondArg = Info.SecondArg; |
660 | Result.Data = Saved; |
661 | break; |
662 | } |
663 | |
664 | case Sema::TDK_SubstitutionFailure: |
665 | Result.Data = Info.take(); |
666 | if (Info.hasSFINAEDiagnostic()) { |
667 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
668 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
669 | Info.takeSFINAEDiagnostic(*Diag); |
670 | Result.HasDiagnostic = true; |
671 | } |
672 | break; |
673 | |
674 | case Sema::TDK_ConstraintsNotSatisfied: { |
675 | CNSInfo *Saved = new (Context) CNSInfo; |
676 | Saved->TemplateArgs = Info.take(); |
677 | Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction; |
678 | Result.Data = Saved; |
679 | break; |
680 | } |
681 | |
682 | case Sema::TDK_Success: |
683 | case Sema::TDK_NonDependentConversionFailure: |
684 | llvm_unreachable("not a deduction failure"); |
685 | } |
686 | |
687 | return Result; |
688 | } |
689 | |
690 | void DeductionFailureInfo::Destroy() { |
691 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
692 | case Sema::TDK_Success: |
693 | case Sema::TDK_Invalid: |
694 | case Sema::TDK_InstantiationDepth: |
695 | case Sema::TDK_Incomplete: |
696 | case Sema::TDK_TooManyArguments: |
697 | case Sema::TDK_TooFewArguments: |
698 | case Sema::TDK_InvalidExplicitArguments: |
699 | case Sema::TDK_CUDATargetMismatch: |
700 | case Sema::TDK_NonDependentConversionFailure: |
701 | break; |
702 | |
703 | case Sema::TDK_IncompletePack: |
704 | case Sema::TDK_Inconsistent: |
705 | case Sema::TDK_Underqualified: |
706 | case Sema::TDK_DeducedMismatch: |
707 | case Sema::TDK_DeducedMismatchNested: |
708 | case Sema::TDK_NonDeducedMismatch: |
709 | |
710 | Data = nullptr; |
711 | break; |
712 | |
713 | case Sema::TDK_SubstitutionFailure: |
714 | |
715 | Data = nullptr; |
716 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
717 | Diag->~PartialDiagnosticAt(); |
718 | HasDiagnostic = false; |
719 | } |
720 | break; |
721 | |
722 | case Sema::TDK_ConstraintsNotSatisfied: |
723 | |
724 | Data = nullptr; |
725 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
726 | Diag->~PartialDiagnosticAt(); |
727 | HasDiagnostic = false; |
728 | } |
729 | break; |
730 | |
731 | |
732 | case Sema::TDK_MiscellaneousDeductionFailure: |
733 | break; |
734 | } |
735 | } |
736 | |
737 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
738 | if (HasDiagnostic) |
739 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
740 | return nullptr; |
741 | } |
742 | |
743 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
744 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
745 | case Sema::TDK_Success: |
746 | case Sema::TDK_Invalid: |
747 | case Sema::TDK_InstantiationDepth: |
748 | case Sema::TDK_TooManyArguments: |
749 | case Sema::TDK_TooFewArguments: |
750 | case Sema::TDK_SubstitutionFailure: |
751 | case Sema::TDK_DeducedMismatch: |
752 | case Sema::TDK_DeducedMismatchNested: |
753 | case Sema::TDK_NonDeducedMismatch: |
754 | case Sema::TDK_CUDATargetMismatch: |
755 | case Sema::TDK_NonDependentConversionFailure: |
756 | case Sema::TDK_ConstraintsNotSatisfied: |
757 | return TemplateParameter(); |
758 | |
759 | case Sema::TDK_Incomplete: |
760 | case Sema::TDK_InvalidExplicitArguments: |
761 | return TemplateParameter::getFromOpaqueValue(Data); |
762 | |
763 | case Sema::TDK_IncompletePack: |
764 | case Sema::TDK_Inconsistent: |
765 | case Sema::TDK_Underqualified: |
766 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
767 | |
768 | |
769 | case Sema::TDK_MiscellaneousDeductionFailure: |
770 | break; |
771 | } |
772 | |
773 | return TemplateParameter(); |
774 | } |
775 | |
776 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
777 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
778 | case Sema::TDK_Success: |
779 | case Sema::TDK_Invalid: |
780 | case Sema::TDK_InstantiationDepth: |
781 | case Sema::TDK_TooManyArguments: |
782 | case Sema::TDK_TooFewArguments: |
783 | case Sema::TDK_Incomplete: |
784 | case Sema::TDK_IncompletePack: |
785 | case Sema::TDK_InvalidExplicitArguments: |
786 | case Sema::TDK_Inconsistent: |
787 | case Sema::TDK_Underqualified: |
788 | case Sema::TDK_NonDeducedMismatch: |
789 | case Sema::TDK_CUDATargetMismatch: |
790 | case Sema::TDK_NonDependentConversionFailure: |
791 | return nullptr; |
792 | |
793 | case Sema::TDK_DeducedMismatch: |
794 | case Sema::TDK_DeducedMismatchNested: |
795 | return static_cast<DFIDeducedMismatchArgs*>(Data)->TemplateArgs; |
796 | |
797 | case Sema::TDK_SubstitutionFailure: |
798 | return static_cast<TemplateArgumentList*>(Data); |
799 | |
800 | case Sema::TDK_ConstraintsNotSatisfied: |
801 | return static_cast<CNSInfo*>(Data)->TemplateArgs; |
802 | |
803 | |
804 | case Sema::TDK_MiscellaneousDeductionFailure: |
805 | break; |
806 | } |
807 | |
808 | return nullptr; |
809 | } |
810 | |
811 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
812 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
813 | case Sema::TDK_Success: |
814 | case Sema::TDK_Invalid: |
815 | case Sema::TDK_InstantiationDepth: |
816 | case Sema::TDK_Incomplete: |
817 | case Sema::TDK_TooManyArguments: |
818 | case Sema::TDK_TooFewArguments: |
819 | case Sema::TDK_InvalidExplicitArguments: |
820 | case Sema::TDK_SubstitutionFailure: |
821 | case Sema::TDK_CUDATargetMismatch: |
822 | case Sema::TDK_NonDependentConversionFailure: |
823 | case Sema::TDK_ConstraintsNotSatisfied: |
824 | return nullptr; |
825 | |
826 | case Sema::TDK_IncompletePack: |
827 | case Sema::TDK_Inconsistent: |
828 | case Sema::TDK_Underqualified: |
829 | case Sema::TDK_DeducedMismatch: |
830 | case Sema::TDK_DeducedMismatchNested: |
831 | case Sema::TDK_NonDeducedMismatch: |
832 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
833 | |
834 | |
835 | case Sema::TDK_MiscellaneousDeductionFailure: |
836 | break; |
837 | } |
838 | |
839 | return nullptr; |
840 | } |
841 | |
842 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
843 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
844 | case Sema::TDK_Success: |
845 | case Sema::TDK_Invalid: |
846 | case Sema::TDK_InstantiationDepth: |
847 | case Sema::TDK_Incomplete: |
848 | case Sema::TDK_IncompletePack: |
849 | case Sema::TDK_TooManyArguments: |
850 | case Sema::TDK_TooFewArguments: |
851 | case Sema::TDK_InvalidExplicitArguments: |
852 | case Sema::TDK_SubstitutionFailure: |
853 | case Sema::TDK_CUDATargetMismatch: |
854 | case Sema::TDK_NonDependentConversionFailure: |
855 | case Sema::TDK_ConstraintsNotSatisfied: |
856 | return nullptr; |
857 | |
858 | case Sema::TDK_Inconsistent: |
859 | case Sema::TDK_Underqualified: |
860 | case Sema::TDK_DeducedMismatch: |
861 | case Sema::TDK_DeducedMismatchNested: |
862 | case Sema::TDK_NonDeducedMismatch: |
863 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
864 | |
865 | |
866 | case Sema::TDK_MiscellaneousDeductionFailure: |
867 | break; |
868 | } |
869 | |
870 | return nullptr; |
871 | } |
872 | |
873 | llvm::Optional<unsigned> DeductionFailureInfo::getCallArgIndex() { |
874 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
875 | case Sema::TDK_DeducedMismatch: |
876 | case Sema::TDK_DeducedMismatchNested: |
877 | return static_cast<DFIDeducedMismatchArgs*>(Data)->CallArgIndex; |
878 | |
879 | default: |
880 | return llvm::None; |
881 | } |
882 | } |
883 | |
884 | bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed( |
885 | OverloadedOperatorKind Op) { |
886 | if (!AllowRewrittenCandidates) |
887 | return false; |
888 | return Op == OO_EqualEqual || Op == OO_Spaceship; |
889 | } |
890 | |
891 | bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed( |
892 | ASTContext &Ctx, const FunctionDecl *FD) { |
893 | if (!shouldAddReversed(FD->getDeclName().getCXXOverloadedOperator())) |
894 | return false; |
895 | |
896 | |
897 | return FD->getNumParams() != 2 || |
898 | !Ctx.hasSameUnqualifiedType(FD->getParamDecl(0)->getType(), |
899 | FD->getParamDecl(1)->getType()) || |
900 | FD->hasAttr<EnableIfAttr>(); |
901 | } |
902 | |
903 | void OverloadCandidateSet::destroyCandidates() { |
904 | for (iterator i = begin(), e = end(); i != e; ++i) { |
905 | for (auto &C : i->Conversions) |
906 | C.~ImplicitConversionSequence(); |
907 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
908 | i->DeductionFailure.Destroy(); |
909 | } |
910 | } |
911 | |
912 | void OverloadCandidateSet::clear(CandidateSetKind CSK) { |
913 | destroyCandidates(); |
914 | SlabAllocator.Reset(); |
915 | NumInlineBytesUsed = 0; |
916 | Candidates.clear(); |
917 | Functions.clear(); |
918 | Kind = CSK; |
919 | } |
920 | |
921 | namespace { |
922 | class UnbridgedCastsSet { |
923 | struct Entry { |
924 | Expr **Addr; |
925 | Expr *Saved; |
926 | }; |
927 | SmallVector<Entry, 2> Entries; |
928 | |
929 | public: |
930 | void save(Sema &S, Expr *&E) { |
931 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
932 | Entry entry = { &E, E }; |
933 | Entries.push_back(entry); |
934 | E = S.stripARCUnbridgedCast(E); |
935 | } |
936 | |
937 | void restore() { |
938 | for (SmallVectorImpl<Entry>::iterator |
939 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
940 | *i->Addr = i->Saved; |
941 | } |
942 | }; |
943 | } |
944 | |
945 | |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | |
952 | static bool |
953 | checkPlaceholderForOverload(Sema &S, Expr *&E, |
954 | UnbridgedCastsSet *unbridgedCasts = nullptr) { |
955 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
956 | |
957 | |
958 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
959 | |
960 | |
961 | |
962 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
963 | unbridgedCasts) { |
964 | unbridgedCasts->save(S, E); |
965 | return false; |
966 | } |
967 | |
968 | |
969 | ExprResult result = S.CheckPlaceholderExpr(E); |
970 | if (result.isInvalid()) |
971 | return true; |
972 | |
973 | E = result.get(); |
974 | return false; |
975 | } |
976 | |
977 | |
978 | return false; |
979 | } |
980 | |
981 | |
982 | |
983 | static bool checkArgPlaceholdersForOverload(Sema &S, |
984 | MultiExprArg Args, |
985 | UnbridgedCastsSet &unbridged) { |
986 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
987 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
988 | return true; |
989 | |
990 | return false; |
991 | } |
992 | |
993 | |
994 | |
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 | Sema::OverloadKind |
1027 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
1028 | NamedDecl *&Match, bool NewIsUsingDecl) { |
1029 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
1030 | I != E; ++I) { |
1031 | NamedDecl *OldD = *I; |
1032 | |
1033 | bool OldIsUsingDecl = false; |
1034 | if (isa<UsingShadowDecl>(OldD)) { |
1035 | OldIsUsingDecl = true; |
1036 | |
1037 | |
1038 | |
1039 | if (NewIsUsingDecl) continue; |
1040 | |
1041 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
1042 | } |
1043 | |
1044 | |
1045 | |
1046 | if ((OldIsUsingDecl || NewIsUsingDecl) && !isVisible(*I)) |
1047 | continue; |
1048 | |
1049 | |
1050 | |
1051 | |
1052 | |
1053 | |
1054 | bool UseMemberUsingDeclRules = |
1055 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
1056 | !New->getFriendObjectKind(); |
1057 | |
1058 | if (FunctionDecl *OldF = OldD->getAsFunction()) { |
1059 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
1060 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
1061 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
1062 | continue; |
1063 | } |
1064 | |
1065 | if (!isa<FunctionTemplateDecl>(OldD) && |
1066 | !shouldLinkPossiblyHiddenDecl(*I, New)) |
1067 | continue; |
1068 | |
1069 | Match = *I; |
1070 | return Ovl_Match; |
1071 | } |
1072 | |
1073 | |
1074 | |
1075 | if (!getASTContext().canBuiltinBeRedeclared(OldF)) { |
1076 | Match = *I; |
1077 | return Ovl_NonFunction; |
1078 | } |
1079 | } else if (isa<UsingDecl>(OldD) || isa<UsingPackDecl>(OldD)) { |
1080 | |
1081 | |
1082 | assert(Old.getLookupKind() == LookupUsingDeclName); |
1083 | } else if (isa<TagDecl>(OldD)) { |
1084 | |
1085 | } else if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(OldD)) { |
1086 | |
1087 | |
1088 | |
1089 | |
1090 | |
1091 | |
1092 | if (UUD->getQualifier()->isDependent() && !UUD->isCXXClassMember()) { |
1093 | Match = *I; |
1094 | return Ovl_NonFunction; |
1095 | } |
1096 | } else { |
1097 | |
1098 | |
1099 | |
1100 | Match = *I; |
1101 | return Ovl_NonFunction; |
1102 | } |
1103 | } |
1104 | |
1105 | |
1106 | |
1107 | |
1108 | |
1109 | |
1110 | |
1111 | |
1112 | |
1113 | |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | |
1119 | |
1120 | if (New->getFriendObjectKind() && New->getQualifier() && |
1121 | !New->getDescribedFunctionTemplate() && |
1122 | !New->getDependentSpecializationInfo() && |
1123 | !New->getType()->isDependentType()) { |
1124 | LookupResult TemplateSpecResult(LookupResult::Temporary, Old); |
1125 | TemplateSpecResult.addAllDecls(Old); |
1126 | if (CheckFunctionTemplateSpecialization(New, nullptr, TemplateSpecResult, |
1127 | true)) { |
1128 | New->setInvalidDecl(); |
1129 | return Ovl_Overload; |
1130 | } |
1131 | |
1132 | Match = TemplateSpecResult.getAsSingle<FunctionDecl>(); |
1133 | return Ovl_Match; |
1134 | } |
1135 | |
1136 | return Ovl_Overload; |
1137 | } |
1138 | |
1139 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
1140 | bool UseMemberUsingDeclRules, bool ConsiderCudaAttrs, |
1141 | bool ConsiderRequiresClauses) { |
1142 | |
1143 | if (New->isMain()) |
1144 | return false; |
1145 | |
1146 | |
1147 | if (New->isMSVCRTEntryPoint()) |
1148 | return false; |
1149 | |
1150 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
1151 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
1152 | |
1153 | |
1154 | |
1155 | |
1156 | if ((OldTemplate == nullptr) != (NewTemplate == nullptr)) |
1157 | return true; |
1158 | |
1159 | |
1160 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
1161 | QualType NewQType = Context.getCanonicalType(New->getType()); |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | |
1168 | |
1169 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
1170 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
1171 | return false; |
1172 | |
1173 | const FunctionProtoType *OldType = cast<FunctionProtoType>(OldQType); |
1174 | const FunctionProtoType *NewType = cast<FunctionProtoType>(NewQType); |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | if (OldQType != NewQType && |
1180 | (OldType->getNumParams() != NewType->getNumParams() || |
1181 | OldType->isVariadic() != NewType->isVariadic() || |
1182 | !FunctionParamTypesAreEqual(OldType, NewType))) |
1183 | return true; |
1184 | |
1185 | |
1186 | |
1187 | |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | if (!UseMemberUsingDeclRules && NewTemplate && |
1198 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
1199 | OldTemplate->getTemplateParameters(), |
1200 | false, TPL_TemplateMatch) || |
1201 | !Context.hasSameType(Old->getDeclaredReturnType(), |
1202 | New->getDeclaredReturnType()))) |
1203 | return true; |
1204 | |
1205 | |
1206 | |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
1214 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
1215 | if (OldMethod && NewMethod && |
1216 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
1217 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
1218 | if (!UseMemberUsingDeclRules && |
1219 | (OldMethod->getRefQualifier() == RQ_None || |
1220 | NewMethod->getRefQualifier() == RQ_None)) { |
1221 | |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
1228 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
1229 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
1230 | } |
1231 | return true; |
1232 | } |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | auto OldQuals = OldMethod->getMethodQualifiers(); |
1239 | auto NewQuals = NewMethod->getMethodQualifiers(); |
1240 | if (!getLangOpts().CPlusPlus14 && NewMethod->isConstexpr() && |
1241 | !isa<CXXConstructorDecl>(NewMethod)) |
1242 | NewQuals.addConst(); |
1243 | |
1244 | OldQuals.removeRestrict(); |
1245 | NewQuals.removeRestrict(); |
1246 | if (OldQuals != NewQuals) |
1247 | return true; |
1248 | } |
1249 | |
1250 | |
1251 | |
1252 | |
1253 | |
1254 | if (functionHasPassObjectSizeParams(New) != |
1255 | functionHasPassObjectSizeParams(Old)) |
1256 | return true; |
1257 | |
1258 | |
1259 | for (specific_attr_iterator<EnableIfAttr> |
1260 | NewI = New->specific_attr_begin<EnableIfAttr>(), |
1261 | NewE = New->specific_attr_end<EnableIfAttr>(), |
1262 | OldI = Old->specific_attr_begin<EnableIfAttr>(), |
1263 | OldE = Old->specific_attr_end<EnableIfAttr>(); |
1264 | NewI != NewE || OldI != OldE; ++NewI, ++OldI) { |
1265 | if (NewI == NewE || OldI == OldE) |
1266 | return true; |
1267 | llvm::FoldingSetNodeID NewID, OldID; |
1268 | NewI->getCond()->Profile(NewID, Context, true); |
1269 | OldI->getCond()->Profile(OldID, Context, true); |
1270 | if (NewID != OldID) |
1271 | return true; |
1272 | } |
1273 | |
1274 | if (getLangOpts().CUDA && ConsiderCudaAttrs) { |
1275 | |
1276 | |
1277 | if (!isa<CXXDestructorDecl>(New)) { |
1278 | CUDAFunctionTarget NewTarget = IdentifyCUDATarget(New), |
1279 | OldTarget = IdentifyCUDATarget(Old); |
1280 | if (NewTarget != CFT_InvalidTarget) { |
1281 | assert((OldTarget != CFT_InvalidTarget) && |
1282 | "Unexpected invalid target."); |
1283 | |
1284 | |
1285 | |
1286 | if (NewTarget != OldTarget) |
1287 | return true; |
1288 | } |
1289 | } |
1290 | } |
1291 | |
1292 | if (ConsiderRequiresClauses) { |
1293 | Expr *NewRC = New->getTrailingRequiresClause(), |
1294 | *OldRC = Old->getTrailingRequiresClause(); |
1295 | if ((NewRC != nullptr) != (OldRC != nullptr)) |
1296 | |
1297 | return true; |
1298 | |
1299 | if (NewRC) { |
1300 | llvm::FoldingSetNodeID NewID, OldID; |
1301 | NewRC->Profile(NewID, Context, true); |
1302 | OldRC->Profile(OldID, Context, true); |
1303 | if (NewID != OldID) |
1304 | |
1305 | return true; |
1306 | } |
1307 | } |
1308 | |
1309 | |
1310 | return false; |
1311 | } |
1312 | |
1313 | |
1314 | |
1315 | |
1316 | |
1317 | static ImplicitConversionSequence |
1318 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
1319 | bool SuppressUserConversions, |
1320 | bool AllowExplicit, |
1321 | bool InOverloadResolution, |
1322 | bool CStyle, |
1323 | bool AllowObjCWritebackConversion, |
1324 | bool AllowObjCConversionOnExplicit) { |
1325 | ImplicitConversionSequence ICS; |
1326 | |
1327 | if (SuppressUserConversions) { |
1328 | |
1329 | |
1330 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1331 | return ICS; |
1332 | } |
1333 | |
1334 | |
1335 | OverloadCandidateSet Conversions(From->getExprLoc(), |
1336 | OverloadCandidateSet::CSK_Normal); |
1337 | switch (IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, |
1338 | Conversions, AllowExplicit, |
1339 | AllowObjCConversionOnExplicit)) { |
1340 | case OR_Success: |
1341 | case OR_Deleted: |
1342 | ICS.setUserDefined(); |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | |
1348 | |
1349 | |
1350 | if (CXXConstructorDecl *Constructor |
1351 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
1352 | QualType FromCanon |
1353 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
1354 | QualType ToCanon |
1355 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
1356 | if (Constructor->isCopyConstructor() && |
1357 | (FromCanon == ToCanon || |
1358 | S.IsDerivedFrom(From->getBeginLoc(), FromCanon, ToCanon))) { |
1359 | |
1360 | |
1361 | DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction; |
1362 | ICS.setStandard(); |
1363 | ICS.Standard.setAsIdentityConversion(); |
1364 | ICS.Standard.setFromType(From->getType()); |
1365 | ICS.Standard.setAllToTypes(ToType); |
1366 | ICS.Standard.CopyConstructor = Constructor; |
1367 | ICS.Standard.FoundCopyConstructor = Found; |
1368 | if (ToCanon != FromCanon) |
1369 | ICS.Standard.Second = ICK_Derived_To_Base; |
1370 | } |
1371 | } |
1372 | break; |
1373 | |
1374 | case OR_Ambiguous: |
1375 | ICS.setAmbiguous(); |
1376 | ICS.Ambiguous.setFromType(From->getType()); |
1377 | ICS.Ambiguous.setToType(ToType); |
1378 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
1379 | Cand != Conversions.end(); ++Cand) |
1380 | if (Cand->Best) |
1381 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
1382 | break; |
1383 | |
1384 | |
1385 | case OR_No_Viable_Function: |
1386 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1387 | break; |
1388 | } |
1389 | |
1390 | return ICS; |
1391 | } |
1392 | |
1393 | |
1394 | |
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 | static ImplicitConversionSequence |
1421 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
1422 | bool SuppressUserConversions, |
1423 | bool AllowExplicit, |
1424 | bool InOverloadResolution, |
1425 | bool CStyle, |
1426 | bool AllowObjCWritebackConversion, |
1427 | bool AllowObjCConversionOnExplicit) { |
1428 | ImplicitConversionSequence ICS; |
1429 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
1430 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
1431 | ICS.setStandard(); |
1432 | return ICS; |
1433 | } |
1434 | |
1435 | if (!S.getLangOpts().CPlusPlus) { |
1436 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
1437 | return ICS; |
1438 | } |
1439 | |
1440 | |
1441 | |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | |
1447 | QualType FromType = From->getType(); |
1448 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
1449 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
1450 | S.IsDerivedFrom(From->getBeginLoc(), FromType, ToType))) { |
1451 | ICS.setStandard(); |
1452 | ICS.Standard.setAsIdentityConversion(); |
1453 | ICS.Standard.setFromType(FromType); |
1454 | ICS.Standard.setAllToTypes(ToType); |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | ICS.Standard.CopyConstructor = nullptr; |
1461 | |
1462 | |
1463 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
1464 | ICS.Standard.Second = ICK_Derived_To_Base; |
1465 | |
1466 | return ICS; |
1467 | } |
1468 | |
1469 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
1470 | AllowExplicit, InOverloadResolution, CStyle, |
1471 | AllowObjCWritebackConversion, |
1472 | AllowObjCConversionOnExplicit); |
1473 | } |
1474 | |
1475 | ImplicitConversionSequence |
1476 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
1477 | bool SuppressUserConversions, |
1478 | bool AllowExplicit, |
1479 | bool InOverloadResolution, |
1480 | bool CStyle, |
1481 | bool AllowObjCWritebackConversion) { |
1482 | return ::TryImplicitConversion(*this, From, ToType, |
1483 | SuppressUserConversions, AllowExplicit, |
1484 | InOverloadResolution, CStyle, |
1485 | AllowObjCWritebackConversion, |
1486 | false); |
1487 | } |
1488 | |
1489 | |
1490 | |
1491 | |
1492 | |
1493 | |
1494 | ExprResult |
1495 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
1496 | AssignmentAction Action, bool AllowExplicit) { |
1497 | ImplicitConversionSequence ICS; |
1498 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
1499 | } |
1500 | |
1501 | ExprResult |
1502 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
1503 | AssignmentAction Action, bool AllowExplicit, |
1504 | ImplicitConversionSequence& ICS) { |
1505 | if (checkPlaceholderForOverload(*this, From)) |
1506 | return ExprError(); |
1507 | |
1508 | |
1509 | bool AllowObjCWritebackConversion |
1510 | = getLangOpts().ObjCAutoRefCount && |
1511 | (Action == AA_Passing || Action == AA_Sending); |
1512 | if (getLangOpts().ObjC) |
1513 | CheckObjCBridgeRelatedConversions(From->getBeginLoc(), ToType, |
1514 | From->getType(), From); |
1515 | ICS = ::TryImplicitConversion(*this, From, ToType, |
1516 | false, |
1517 | AllowExplicit, |
1518 | false, |
1519 | false, |
1520 | AllowObjCWritebackConversion, |
1521 | false); |
1522 | return PerformImplicitConversion(From, ToType, ICS, Action); |
1523 | } |
1524 | |
1525 | |
1526 | |
1527 | |
1528 | bool Sema::IsFunctionConversion(QualType FromType, QualType ToType, |
1529 | QualType &ResultTy) { |
1530 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
1531 | return false; |
1532 | |
1533 | |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | |
1539 | |
1540 | CanQualType CanTo = Context.getCanonicalType(ToType); |
1541 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
1542 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
1543 | if (TyClass != CanFrom->getTypeClass()) return false; |
1544 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
1545 | if (TyClass == Type::Pointer) { |
1546 | CanTo = CanTo.castAs<PointerType>()->getPointeeType(); |
1547 | CanFrom = CanFrom.castAs<PointerType>()->getPointeeType(); |
1548 | } else if (TyClass == Type::BlockPointer) { |
1549 | CanTo = CanTo.castAs<BlockPointerType>()->getPointeeType(); |
1550 | CanFrom = CanFrom.castAs<BlockPointerType>()->getPointeeType(); |
1551 | } else if (TyClass == Type::MemberPointer) { |
1552 | auto ToMPT = CanTo.castAs<MemberPointerType>(); |
1553 | auto FromMPT = CanFrom.castAs<MemberPointerType>(); |
1554 | |
1555 | if (ToMPT->getClass() != FromMPT->getClass()) |
1556 | return false; |
1557 | CanTo = ToMPT->getPointeeType(); |
1558 | CanFrom = FromMPT->getPointeeType(); |
1559 | } else { |
1560 | return false; |
1561 | } |
1562 | |
1563 | TyClass = CanTo->getTypeClass(); |
1564 | if (TyClass != CanFrom->getTypeClass()) return false; |
1565 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
1566 | return false; |
1567 | } |
1568 | |
1569 | const auto *FromFn = cast<FunctionType>(CanFrom); |
1570 | FunctionType::ExtInfo FromEInfo = FromFn->getExtInfo(); |
1571 | |
1572 | const auto *ToFn = cast<FunctionType>(CanTo); |
1573 | FunctionType::ExtInfo ToEInfo = ToFn->getExtInfo(); |
1574 | |
1575 | bool Changed = false; |
1576 | |
1577 | |
1578 | if (FromEInfo.getNoReturn() && !ToEInfo.getNoReturn()) { |
1579 | FromFn = Context.adjustFunctionType(FromFn, FromEInfo.withNoReturn(false)); |
1580 | Changed = true; |
1581 | } |
1582 | |
1583 | |
1584 | if (const auto *FromFPT = dyn_cast<FunctionProtoType>(FromFn)) { |
1585 | const auto *ToFPT = cast<FunctionProtoType>(ToFn); |
1586 | if (FromFPT->isNothrow() && !ToFPT->isNothrow()) { |
1587 | FromFn = cast<FunctionType>( |
1588 | Context.getFunctionTypeWithExceptionSpec(QualType(FromFPT, 0), |
1589 | EST_None) |
1590 | .getTypePtr()); |
1591 | Changed = true; |
1592 | } |
1593 | |
1594 | |
1595 | |
1596 | |
1597 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
1598 | bool CanUseToFPT, CanUseFromFPT; |
1599 | if (Context.mergeExtParameterInfo(ToFPT, FromFPT, CanUseToFPT, |
1600 | CanUseFromFPT, NewParamInfos) && |
1601 | CanUseToFPT && !CanUseFromFPT) { |
1602 | FunctionProtoType::ExtProtoInfo ExtInfo = FromFPT->getExtProtoInfo(); |
1603 | ExtInfo.ExtParameterInfos = |
1604 | NewParamInfos.empty() ? nullptr : NewParamInfos.data(); |
1605 | QualType QT = Context.getFunctionType(FromFPT->getReturnType(), |
1606 | FromFPT->getParamTypes(), ExtInfo); |
1607 | FromFn = QT->getAs<FunctionType>(); |
1608 | Changed = true; |
1609 | } |
1610 | } |
1611 | |
1612 | if (!Changed) |
1613 | return false; |
1614 | |
1615 | assert(QualType(FromFn, 0).isCanonical()); |
1616 | if (QualType(FromFn, 0) != CanTo) return false; |
1617 | |
1618 | ResultTy = ToType; |
1619 | return true; |
1620 | } |
1621 | |
1622 | |
1623 | |
1624 | |
1625 | |
1626 | |
1627 | static bool IsVectorConversion(Sema &S, QualType FromType, |
1628 | QualType ToType, ImplicitConversionKind &ICK) { |
1629 | |
1630 | |
1631 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
1632 | return false; |
1633 | |
1634 | |
1635 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) |
1636 | return false; |
1637 | |
1638 | |
1639 | if (ToType->isExtVectorType()) { |
1640 | |
1641 | |
1642 | if (FromType->isExtVectorType()) |
1643 | return false; |
1644 | |
1645 | |
1646 | if (FromType->isArithmeticType()) { |
1647 | ICK = ICK_Vector_Splat; |
1648 | return true; |
1649 | } |
1650 | } |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | |
1656 | if (ToType->isVectorType() && FromType->isVectorType()) { |
1657 | if (S.Context.areCompatibleVectorTypes(FromType, ToType) || |
1658 | S.isLaxVectorConversion(FromType, ToType)) { |
1659 | ICK = ICK_Vector_Conversion; |
1660 | return true; |
1661 | } |
1662 | } |
1663 | |
1664 | return false; |
1665 | } |
1666 | |
1667 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
1668 | bool InOverloadResolution, |
1669 | StandardConversionSequence &SCS, |
1670 | bool CStyle); |
1671 | |
1672 | |
1673 | |
1674 | |
1675 | |
1676 | |
1677 | |
1678 | |
1679 | |
1680 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
1681 | bool InOverloadResolution, |
1682 | StandardConversionSequence &SCS, |
1683 | bool CStyle, |
1684 | bool AllowObjCWritebackConversion) { |
1685 | QualType FromType = From->getType(); |
1686 | |
1687 | |
1688 | SCS.setAsIdentityConversion(); |
1689 | SCS.IncompatibleObjC = false; |
1690 | SCS.setFromType(FromType); |
1691 | SCS.CopyConstructor = nullptr; |
1692 | |
1693 | |
1694 | |
1695 | if (S.getLangOpts().CPlusPlus && |
1696 | (FromType->isRecordType() || ToType->isRecordType())) |
1697 | return false; |
1698 | |
1699 | |
1700 | |
1701 | |
1702 | |
1703 | if (FromType == S.Context.OverloadTy) { |
1704 | DeclAccessPair AccessPair; |
1705 | if (FunctionDecl *Fn |
1706 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
1707 | AccessPair)) { |
1708 | |
1709 | |
1710 | FromType = Fn->getType(); |
1711 | SCS.setFromType(FromType); |
1712 | |
1713 | |
1714 | |
1715 | if (!S.Context.hasSameUnqualifiedType( |
1716 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
1717 | QualType resultTy; |
1718 | |
1719 | if (!S.IsFunctionConversion(FromType, |
1720 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
1721 | |
1722 | if (!ToType->isBooleanType()) |
1723 | return false; |
1724 | } |
1725 | |
1726 | |
1727 | |
1728 | |
1729 | |
1730 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
1731 | if (Method && !Method->isStatic()) { |
1732 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
1733 | "Non-unary operator on non-static member address"); |
1734 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
1735 | == UO_AddrOf && |
1736 | "Non-address-of operator on non-static member address"); |
1737 | const Type *ClassType |
1738 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
1739 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
1740 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
1741 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
1742 | UO_AddrOf && |
1743 | "Non-address-of operator for overloaded function expression"); |
1744 | FromType = S.Context.getPointerType(FromType); |
1745 | } |
1746 | |
1747 | |
1748 | |
1749 | |
1750 | assert(S.Context.hasSameType( |
1751 | FromType, |
1752 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
1753 | } else { |
1754 | return false; |
1755 | } |
1756 | } |
1757 | |
1758 | |
1759 | |
1760 | bool argIsLValue = From->isGLValue(); |
1761 | if (argIsLValue && |
1762 | !FromType->isFunctionType() && !FromType->isArrayType() && |
1763 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
1764 | SCS.First = ICK_Lvalue_To_Rvalue; |
1765 | |
1766 | |
1767 | |
1768 | |
1769 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
1770 | FromType = Atomic->getValueType(); |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | FromType = FromType.getUnqualifiedType(); |
1777 | } else if (FromType->isArrayType()) { |
1778 | |
1779 | SCS.First = ICK_Array_To_Pointer; |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | FromType = S.Context.getArrayDecayedType(FromType); |
1785 | |
1786 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
1787 | |
1788 | SCS.DeprecatedStringLiteralToCharPtr = true; |
1789 | |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | SCS.Second = ICK_Identity; |
1795 | SCS.Third = ICK_Qualification; |
1796 | SCS.QualificationIncludesObjCLifetime = false; |
1797 | SCS.setAllToTypes(FromType); |
1798 | return true; |
1799 | } |
1800 | } else if (FromType->isFunctionType() && argIsLValue) { |
1801 | |
1802 | SCS.First = ICK_Function_To_Pointer; |
1803 | |
1804 | if (auto *DRE = dyn_cast<DeclRefExpr>(From->IgnoreParenCasts())) |
1805 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) |
1806 | if (!S.checkAddressOfFunctionIsAvailable(FD)) |
1807 | return false; |
1808 | |
1809 | |
1810 | |
1811 | |
1812 | FromType = S.Context.getPointerType(FromType); |
1813 | } else { |
1814 | |
1815 | SCS.First = ICK_Identity; |
1816 | } |
1817 | SCS.setToType(0, FromType); |
1818 | |
1819 | |
1820 | |
1821 | |
1822 | |
1823 | |
1824 | |
1825 | bool IncompatibleObjC = false; |
1826 | ImplicitConversionKind SecondICK = ICK_Identity; |
1827 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
1828 | |
1829 | |
1830 | SCS.Second = ICK_Identity; |
1831 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
1832 | |
1833 | SCS.Second = ICK_Integral_Promotion; |
1834 | FromType = ToType.getUnqualifiedType(); |
1835 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
1836 | |
1837 | SCS.Second = ICK_Floating_Promotion; |
1838 | FromType = ToType.getUnqualifiedType(); |
1839 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
1840 | |
1841 | SCS.Second = ICK_Complex_Promotion; |
1842 | FromType = ToType.getUnqualifiedType(); |
1843 | } else if (ToType->isBooleanType() && |
1844 | (FromType->isArithmeticType() || |
1845 | FromType->isAnyPointerType() || |
1846 | FromType->isBlockPointerType() || |
1847 | FromType->isMemberPointerType() || |
1848 | FromType->isNullPtrType())) { |
1849 | |
1850 | SCS.Second = ICK_Boolean_Conversion; |
1851 | FromType = S.Context.BoolTy; |
1852 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
1853 | ToType->isIntegralType(S.Context)) { |
1854 | |
1855 | SCS.Second = ICK_Integral_Conversion; |
1856 | FromType = ToType.getUnqualifiedType(); |
1857 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
1858 | |
1859 | SCS.Second = ICK_Complex_Conversion; |
1860 | FromType = ToType.getUnqualifiedType(); |
1861 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
1862 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
1863 | |
1864 | SCS.Second = ICK_Complex_Real; |
1865 | FromType = ToType.getUnqualifiedType(); |
1866 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
1867 | |
1868 | |
1869 | |
1870 | if (&S.Context.getFloatTypeSemantics(FromType) != |
1871 | &S.Context.getFloatTypeSemantics(ToType)) { |
1872 | bool Float128AndLongDouble = ((FromType == S.Context.Float128Ty && |
1873 | ToType == S.Context.LongDoubleTy) || |
1874 | (FromType == S.Context.LongDoubleTy && |
1875 | ToType == S.Context.Float128Ty)); |
1876 | if (Float128AndLongDouble && |
1877 | (&S.Context.getFloatTypeSemantics(S.Context.LongDoubleTy) == |
1878 | &llvm::APFloat::PPCDoubleDouble())) |
1879 | return false; |
1880 | } |
1881 | |
1882 | SCS.Second = ICK_Floating_Conversion; |
1883 | FromType = ToType.getUnqualifiedType(); |
1884 | } else if ((FromType->isRealFloatingType() && |
1885 | ToType->isIntegralType(S.Context)) || |
1886 | (FromType->isIntegralOrUnscopedEnumerationType() && |
1887 | ToType->isRealFloatingType())) { |
1888 | |
1889 | SCS.Second = ICK_Floating_Integral; |
1890 | FromType = ToType.getUnqualifiedType(); |
1891 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
1892 | SCS.Second = ICK_Block_Pointer_Conversion; |
1893 | } else if (AllowObjCWritebackConversion && |
1894 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
1895 | SCS.Second = ICK_Writeback_Conversion; |
1896 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
1897 | FromType, IncompatibleObjC)) { |
1898 | |
1899 | SCS.Second = ICK_Pointer_Conversion; |
1900 | SCS.IncompatibleObjC = IncompatibleObjC; |
1901 | FromType = FromType.getUnqualifiedType(); |
1902 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
1903 | InOverloadResolution, FromType)) { |
1904 | |
1905 | SCS.Second = ICK_Pointer_Member; |
1906 | } else if (IsVectorConversion(S, FromType, ToType, SecondICK)) { |
1907 | SCS.Second = SecondICK; |
1908 | FromType = ToType.getUnqualifiedType(); |
1909 | } else if (!S.getLangOpts().CPlusPlus && |
1910 | S.Context.typesAreCompatible(ToType, FromType)) { |
1911 | |
1912 | SCS.Second = ICK_Compatible_Conversion; |
1913 | FromType = ToType.getUnqualifiedType(); |
1914 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
1915 | InOverloadResolution, |
1916 | SCS, CStyle)) { |
1917 | SCS.Second = ICK_TransparentUnionConversion; |
1918 | FromType = ToType; |
1919 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
1920 | CStyle)) { |
1921 | |
1922 | |
1923 | return true; |
1924 | } else if (ToType->isEventT() && |
1925 | From->isIntegerConstantExpr(S.getASTContext()) && |
1926 | From->EvaluateKnownConstInt(S.getASTContext()) == 0) { |
1927 | SCS.Second = ICK_Zero_Event_Conversion; |
1928 | FromType = ToType; |
1929 | } else if (ToType->isQueueT() && |
1930 | From->isIntegerConstantExpr(S.getASTContext()) && |
1931 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
1932 | SCS.Second = ICK_Zero_Queue_Conversion; |
1933 | FromType = ToType; |
1934 | } else if (ToType->isSamplerT() && |
1935 | From->isIntegerConstantExpr(S.getASTContext())) { |
1936 | SCS.Second = ICK_Compatible_Conversion; |
1937 | FromType = ToType; |
1938 | } else { |
1939 | |
1940 | SCS.Second = ICK_Identity; |
1941 | } |
1942 | SCS.setToType(1, FromType); |
1943 | |
1944 | |
1945 | |
1946 | bool ObjCLifetimeConversion; |
1947 | if (S.IsFunctionConversion(FromType, ToType, FromType)) { |
1948 | |
1949 | |
1950 | SCS.Third = ICK_Function_Conversion; |
1951 | } else if (S.IsQualificationConversion(FromType, ToType, CStyle, |
1952 | ObjCLifetimeConversion)) { |
1953 | SCS.Third = ICK_Qualification; |
1954 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
1955 | FromType = ToType; |
1956 | } else { |
1957 | |
1958 | SCS.Third = ICK_Identity; |
1959 | } |
1960 | |
1961 | |
1962 | |
1963 | |
1964 | |
1965 | QualType CanonFrom = S.Context.getCanonicalType(FromType); |
1966 | QualType CanonTo = S.Context.getCanonicalType(ToType); |
1967 | if (CanonFrom.getLocalUnqualifiedType() |
1968 | == CanonTo.getLocalUnqualifiedType() && |
1969 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
1970 | FromType = ToType; |
1971 | CanonFrom = CanonTo; |
1972 | } |
1973 | |
1974 | SCS.setToType(2, FromType); |
1975 | |
1976 | if (CanonFrom == CanonTo) |
1977 | return true; |
1978 | |
1979 | |
1980 | |
1981 | if (S.getLangOpts().CPlusPlus || !InOverloadResolution) |
1982 | return false; |
1983 | |
1984 | ExprResult ER = ExprResult{From}; |
1985 | Sema::AssignConvertType Conv = |
1986 | S.CheckSingleAssignmentConstraints(ToType, ER, |
1987 | false, |
1988 | false, |
1989 | false); |
1990 | ImplicitConversionKind SecondConv; |
1991 | switch (Conv) { |
1992 | case Sema::Compatible: |
1993 | SecondConv = ICK_C_Only_Conversion; |
1994 | break; |
1995 | |
1996 | |
1997 | |
1998 | case Sema::CompatiblePointerDiscardsQualifiers: |
1999 | case Sema::IncompatiblePointer: |
2000 | case Sema::IncompatiblePointerSign: |
2001 | SecondConv = ICK_Incompatible_Pointer_Conversion; |
2002 | break; |
2003 | default: |
2004 | return false; |
2005 | } |
2006 | |
2007 | |
2008 | |
2009 | |
2010 | SCS.Second = SecondConv; |
2011 | SCS.setToType(1, ToType); |
2012 | |
2013 | |
2014 | |
2015 | |
2016 | |
2017 | SCS.Third = ICK_Identity; |
2018 | SCS.setToType(2, ToType); |
2019 | return true; |
2020 | } |
2021 | |
2022 | static bool |
2023 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
2024 | QualType &ToType, |
2025 | bool InOverloadResolution, |
2026 | StandardConversionSequence &SCS, |
2027 | bool CStyle) { |
2028 | |
2029 | const RecordType *UT = ToType->getAsUnionType(); |
2030 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
2031 | return false; |
2032 | |
2033 | RecordDecl *UD = UT->getDecl(); |
2034 | |
2035 | for (const auto *it : UD->fields()) { |
2036 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
2037 | CStyle, false)) { |
2038 | ToType = it->getType(); |
2039 | return true; |
2040 | } |
2041 | } |
2042 | return false; |
2043 | } |
2044 | |
2045 | |
2046 | |
2047 | |
2048 | |
2049 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
2050 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
2051 | |
2052 | if (!To) { |
2053 | return false; |
2054 | } |
2055 | |
2056 | |
2057 | |
2058 | |
2059 | |
2060 | |
2061 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
2062 | !FromType->isEnumeralType()) { |
2063 | if ( |
2064 | (FromType->isSignedIntegerType() || |
2065 | |
2066 | |
2067 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType))) { |
2068 | return To->getKind() == BuiltinType::Int; |
2069 | } |
2070 | |
2071 | return To->getKind() == BuiltinType::UInt; |
2072 | } |
2073 | |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | |
2087 | |
2088 | |
2089 | |
2090 | |
2091 | |
2092 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
2093 | |
2094 | |
2095 | if (FromEnumType->getDecl()->isScoped()) |
2096 | return false; |
2097 | |
2098 | |
2099 | |
2100 | |
2101 | |
2102 | if (FromEnumType->getDecl()->isFixed()) { |
2103 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
2104 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
2105 | IsIntegralPromotion(nullptr, Underlying, ToType); |
2106 | } |
2107 | |
2108 | |
2109 | if (ToType->isIntegerType() && |
2110 | isCompleteType(From->getBeginLoc(), FromType)) |
2111 | return Context.hasSameUnqualifiedType( |
2112 | ToType, FromEnumType->getDecl()->getPromotionType()); |
2113 | |
2114 | |
2115 | |
2116 | |
2117 | |
2118 | |
2119 | if (getLangOpts().CPlusPlus) |
2120 | return false; |
2121 | } |
2122 | |
2123 | |
2124 | |
2125 | |
2126 | |
2127 | |
2128 | |
2129 | |
2130 | |
2131 | |
2132 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
2133 | ToType->isIntegerType()) { |
2134 | |
2135 | |
2136 | bool FromIsSigned = FromType->isSignedIntegerType(); |
2137 | uint64_t FromSize = Context.getTypeSize(FromType); |
2138 | |
2139 | |
2140 | |
2141 | QualType PromoteTypes[6] = { |
2142 | Context.IntTy, Context.UnsignedIntTy, |
2143 | Context.LongTy, Context.UnsignedLongTy , |
2144 | Context.LongLongTy, Context.UnsignedLongLongTy |
2145 | }; |
2146 | for (int Idx = 0; Idx < 6; ++Idx) { |
2147 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
2148 | if (FromSize < ToSize || |
2149 | (FromSize == ToSize && |
2150 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
2151 | |
2152 | |
2153 | |
2154 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
2155 | } |
2156 | } |
2157 | } |
2158 | |
2159 | |
2160 | |
2161 | |
2162 | |
2163 | |
2164 | |
2165 | |
2166 | |
2167 | |
2168 | |
2169 | |
2170 | |
2171 | |
2172 | |
2173 | if (From) { |
2174 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
2175 | llvm::APSInt BitWidth; |
2176 | if (FromType->isIntegralType(Context) && |
2177 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
2178 | llvm::APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
2179 | ToSize = Context.getTypeSize(ToType); |
2180 | |
2181 | |
2182 | if (BitWidth < ToSize || |
2183 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
2184 | return To->getKind() == BuiltinType::Int; |
2185 | } |
2186 | |
2187 | |
2188 | |
2189 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
2190 | return To->getKind() == BuiltinType::UInt; |
2191 | } |
2192 | |
2193 | return false; |
2194 | } |
2195 | } |
2196 | } |
2197 | |
2198 | |
2199 | |
2200 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
2201 | return true; |
2202 | } |
2203 | |
2204 | return false; |
2205 | } |
2206 | |
2207 | |
2208 | |
2209 | |
2210 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
2211 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
2212 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
2213 | |
2214 | |
2215 | if (FromBuiltin->getKind() == BuiltinType::Float && |
2216 | ToBuiltin->getKind() == BuiltinType::Double) |
2217 | return true; |
2218 | |
2219 | |
2220 | |
2221 | |
2222 | if (!getLangOpts().CPlusPlus && |
2223 | (FromBuiltin->getKind() == BuiltinType::Float || |
2224 | FromBuiltin->getKind() == BuiltinType::Double) && |
2225 | (ToBuiltin->getKind() == BuiltinType::LongDouble || |
2226 | ToBuiltin->getKind() == BuiltinType::Float128)) |
2227 | return true; |
2228 | |
2229 | |
2230 | if (!getLangOpts().NativeHalfType && |
2231 | FromBuiltin->getKind() == BuiltinType::Half && |
2232 | ToBuiltin->getKind() == BuiltinType::Float) |
2233 | return true; |
2234 | } |
2235 | |
2236 | return false; |
2237 | } |
2238 | |
2239 | |
2240 | |
2241 | |
2242 | |
2243 | |
2244 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
2245 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
2246 | if (!FromComplex) |
2247 | return false; |
2248 | |
2249 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
2250 | if (!ToComplex) |
2251 | return false; |
2252 | |
2253 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
2254 | ToComplex->getElementType()) || |
2255 | IsIntegralPromotion(nullptr, FromComplex->getElementType(), |
2256 | ToComplex->getElementType()); |
2257 | } |
2258 | |
2259 | |
2260 | |
2261 | |
2262 | |
2263 | |
2264 | |
2265 | static QualType |
2266 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
2267 | QualType ToPointee, QualType ToType, |
2268 | ASTContext &Context, |
2269 | bool StripObjCLifetime = false) { |
2270 | assert((FromPtr->getTypeClass() == Type::Pointer || |
2271 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
2272 | "Invalid similarly-qualified pointer type"); |
2273 | |
2274 | |
2275 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
2276 | return ToType.getUnqualifiedType(); |
2277 | |
2278 | QualType CanonFromPointee |
2279 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
2280 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
2281 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
2282 | |
2283 | if (StripObjCLifetime) |
2284 | Quals.removeObjCLifetime(); |
2285 | |
2286 | |
2287 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
2288 | |
2289 | if (!ToType.isNull()) |
2290 | return ToType.getUnqualifiedType(); |
2291 | |
2292 | |
2293 | |
2294 | if (isa<ObjCObjectPointerType>(ToType)) |
2295 | return Context.getObjCObjectPointerType(ToPointee); |
2296 | return Context.getPointerType(ToPointee); |
2297 | } |
2298 | |
2299 | |
2300 | QualType QualifiedCanonToPointee |
2301 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
2302 | |
2303 | if (isa<ObjCObjectPointerType>(ToType)) |
2304 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
2305 | return Context.getPointerType(QualifiedCanonToPointee); |
2306 | } |
2307 | |
2308 | static bool isNullPointerConstantForConversion(Expr *Expr, |
2309 | bool InOverloadResolution, |
2310 | ASTContext &Context) { |
2311 | |
2312 | |
2313 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
2314 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
2315 | return !InOverloadResolution; |
2316 | |
2317 | return Expr->isNullPointerConstant(Context, |
2318 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
2319 | : Expr::NPC_ValueDependentIsNull); |
2320 | } |
2321 | |
2322 | |
2323 | |
2324 | |
2325 | |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | |
2331 | |
2332 | |
2333 | |
2334 | |
2335 | |
2336 | |
2337 | |
2338 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
2339 | bool InOverloadResolution, |
2340 | QualType& ConvertedType, |
2341 | bool &IncompatibleObjC) { |
2342 | IncompatibleObjC = false; |
2343 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
2344 | IncompatibleObjC)) |
2345 | return true; |
2346 | |
2347 | |
2348 | if (ToType->isObjCObjectPointerType() && |
2349 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2350 | ConvertedType = ToType; |
2351 | return true; |
2352 | } |
2353 | |
2354 | |
2355 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
2356 | ToType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
2357 | ConvertedType = ToType; |
2358 | return true; |
2359 | } |
2360 | |
2361 | |
2362 | if (ToType->isBlockPointerType() && |
2363 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2364 | ConvertedType = ToType; |
2365 | return true; |
2366 | } |
2367 | |
2368 | |
2369 | |
2370 | if (ToType->isNullPtrType() && |
2371 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2372 | ConvertedType = ToType; |
2373 | return true; |
2374 | } |
2375 | |
2376 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
2377 | if (!ToTypePtr) |
2378 | return false; |
2379 | |
2380 | |
2381 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
2382 | ConvertedType = ToType; |
2383 | return true; |
2384 | } |
2385 | |
2386 | |
2387 | |
2388 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
2389 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
2390 | !getLangOpts().ObjCAutoRefCount) { |
2391 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
2392 | FromType->getAs<ObjCObjectPointerType>(), |
2393 | ToPointeeType, |
2394 | ToType, Context); |
2395 | return true; |
2396 | } |
2397 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
2398 | if (!FromTypePtr) |
2399 | return false; |
2400 | |
2401 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
2402 | |
2403 | |
2404 | |
2405 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
2406 | return false; |
2407 | |
2408 | |
2409 | |
2410 | |
2411 | if (FromPointeeType->isIncompleteOrObjectType() && |
2412 | ToPointeeType->isVoidType()) { |
2413 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2414 | ToPointeeType, |
2415 | ToType, Context, |
2416 | true); |
2417 | return true; |
2418 | } |
2419 | |
2420 | |
2421 | if (getLangOpts().MSVCCompat && FromPointeeType->isFunctionType() && |
2422 | ToPointeeType->isVoidType()) { |
2423 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2424 | ToPointeeType, |
2425 | ToType, Context); |
2426 | return true; |
2427 | } |
2428 | |
2429 | |
2430 | |
2431 | if (!getLangOpts().CPlusPlus && |
2432 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
2433 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2434 | ToPointeeType, |
2435 | ToType, Context); |
2436 | return true; |
2437 | } |
2438 | |
2439 | |
2440 | |
2441 | |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | |
2447 | |
2448 | |
2449 | |
2450 | |
2451 | |
2452 | if (getLangOpts().CPlusPlus && FromPointeeType->isRecordType() && |
2453 | ToPointeeType->isRecordType() && |
2454 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
2455 | IsDerivedFrom(From->getBeginLoc(), FromPointeeType, ToPointeeType)) { |
2456 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2457 | ToPointeeType, |
2458 | ToType, Context); |
2459 | return true; |
2460 | } |
2461 | |
2462 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
2463 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
2464 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
2465 | ToPointeeType, |
2466 | ToType, Context); |
2467 | return true; |
2468 | } |
2469 | |
2470 | return false; |
2471 | } |
2472 | |
2473 | |
2474 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
2475 | Qualifiers TQs = T.getQualifiers(); |
2476 | |
2477 | |
2478 | if (TQs == Qs) |
2479 | return T; |
2480 | |
2481 | if (Qs.compatiblyIncludes(TQs)) |
2482 | return Context.getQualifiedType(T, Qs); |
2483 | |
2484 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
2485 | } |
2486 | |
2487 | |
2488 | |
2489 | |
2490 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
2491 | QualType& ConvertedType, |
2492 | bool &IncompatibleObjC) { |
2493 | if (!getLangOpts().ObjC) |
2494 | return false; |
2495 | |
2496 | |
2497 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
2498 | |
2499 | |
2500 | const ObjCObjectPointerType* ToObjCPtr = |
2501 | ToType->getAs<ObjCObjectPointerType>(); |
2502 | const ObjCObjectPointerType *FromObjCPtr = |
2503 | FromType->getAs<ObjCObjectPointerType>(); |
2504 | |
2505 | if (ToObjCPtr && FromObjCPtr) { |
2506 | |
2507 | |
2508 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
2509 | FromObjCPtr->getPointeeType())) |
2510 | return false; |
2511 | |
2512 | |
2513 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
2514 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
2515 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
2516 | if (getLangOpts().CPlusPlus && LHS && RHS && |
2517 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
2518 | FromObjCPtr->getPointeeType())) |
2519 | return false; |
2520 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
2521 | ToObjCPtr->getPointeeType(), |
2522 | ToType, Context); |
2523 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2524 | return true; |
2525 | } |
2526 | |
2527 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
2528 | |
2529 | |
2530 | |
2531 | IncompatibleObjC = true; |
2532 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
2533 | ToObjCPtr->getPointeeType(), |
2534 | ToType, Context); |
2535 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2536 | return true; |
2537 | } |
2538 | } |
2539 | |
2540 | QualType ToPointeeType; |
2541 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
2542 | ToPointeeType = ToCPtr->getPointeeType(); |
2543 | else if (const BlockPointerType *ToBlockPtr = |
2544 | ToType->getAs<BlockPointerType>()) { |
2545 | |
2546 | |
2547 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
2548 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2549 | return true; |
2550 | } |
2551 | ToPointeeType = ToBlockPtr->getPointeeType(); |
2552 | } |
2553 | else if (FromType->getAs<BlockPointerType>() && |
2554 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
2555 | |
2556 | |
2557 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2558 | return true; |
2559 | } |
2560 | else |
2561 | return false; |
2562 | |
2563 | QualType FromPointeeType; |
2564 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
2565 | FromPointeeType = FromCPtr->getPointeeType(); |
2566 | else if (const BlockPointerType *FromBlockPtr = |
2567 | FromType->getAs<BlockPointerType>()) |
2568 | FromPointeeType = FromBlockPtr->getPointeeType(); |
2569 | else |
2570 | return false; |
2571 | |
2572 | |
2573 | |
2574 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
2575 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
2576 | IncompatibleObjC)) { |
2577 | |
2578 | IncompatibleObjC = true; |
2579 | ConvertedType = Context.getPointerType(ConvertedType); |
2580 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2581 | return true; |
2582 | } |
2583 | |
2584 | |
2585 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
2586 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
2587 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
2588 | IncompatibleObjC)) { |
2589 | |
2590 | ConvertedType = Context.getPointerType(ConvertedType); |
2591 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
2592 | return true; |
2593 | } |
2594 | |
2595 | |
2596 | |
2597 | |
2598 | |
2599 | const FunctionProtoType *FromFunctionType |
2600 | = FromPointeeType->getAs<FunctionProtoType>(); |
2601 | const FunctionProtoType *ToFunctionType |
2602 | = ToPointeeType->getAs<FunctionProtoType>(); |
2603 | if (FromFunctionType && ToFunctionType) { |
2604 | |
2605 | |
2606 | if (Context.getCanonicalType(FromPointeeType) |
2607 | == Context.getCanonicalType(ToPointeeType)) |
2608 | return false; |
2609 | |
2610 | |
2611 | |
2612 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
2613 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
2614 | FromFunctionType->getMethodQuals() != ToFunctionType->getMethodQuals()) |
2615 | return false; |
2616 | |
2617 | bool HasObjCConversion = false; |
2618 | if (Context.getCanonicalType(FromFunctionType->getReturnType()) == |
2619 | Context.getCanonicalType(ToFunctionType->getReturnType())) { |
2620 | |
2621 | } else if (isObjCPointerConversion(FromFunctionType->getReturnType(), |
2622 | ToFunctionType->getReturnType(), |
2623 | ConvertedType, IncompatibleObjC)) { |
2624 | |
2625 | HasObjCConversion = true; |
2626 | } else { |
2627 | |
2628 | return false; |
2629 | } |
2630 | |
2631 | |
2632 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
2633 | ArgIdx != NumArgs; ++ArgIdx) { |
2634 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
2635 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
2636 | if (Context.getCanonicalType(FromArgType) |
2637 | == Context.getCanonicalType(ToArgType)) { |
2638 | |
2639 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
2640 | ConvertedType, IncompatibleObjC)) { |
2641 | |
2642 | HasObjCConversion = true; |
2643 | } else { |
2644 | |
2645 | return false; |
2646 | } |
2647 | } |
2648 | |
2649 | if (HasObjCConversion) { |
2650 | |
2651 | |
2652 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
2653 | IncompatibleObjC = true; |
2654 | return true; |
2655 | } |
2656 | } |
2657 | |
2658 | return false; |
2659 | } |
2660 | |
2661 | |
2662 | |
2663 | |
2664 | |
2665 | |
2666 | |
2667 | |
2668 | |
2669 | |
2670 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
2671 | QualType &ConvertedType) { |
2672 | if (!getLangOpts().ObjCAutoRefCount || |
2673 | Context.hasSameUnqualifiedType(FromType, ToType)) |
2674 | return false; |
2675 | |
2676 | |
2677 | QualType ToPointee; |
2678 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
2679 | ToPointee = ToPointer->getPointeeType(); |
2680 | else |
2681 | return false; |
2682 | |
2683 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
2684 | if (!ToPointee->isObjCLifetimeType() || |
2685 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
2686 | !ToQuals.withoutObjCLifetime().empty()) |
2687 | return false; |
2688 | |
2689 | |
2690 | QualType FromPointee; |
2691 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
2692 | FromPointee = FromPointer->getPointeeType(); |
2693 | else |
2694 | return false; |
2695 | |
2696 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
2697 | if (!FromPointee->isObjCLifetimeType() || |
2698 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
2699 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
2700 | return false; |
2701 | |
2702 | |
2703 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
2704 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
2705 | return false; |
2706 | |
2707 | |
2708 | |
2709 | |
2710 | FromPointee = FromPointee.getUnqualifiedType(); |
2711 | |
2712 | |
2713 | ToPointee = ToPointee.getUnqualifiedType(); |
2714 | bool IncompatibleObjC; |
2715 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
2716 | FromPointee = ToPointee; |
2717 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
2718 | IncompatibleObjC)) |
2719 | return false; |
2720 | |
2721 | |
2722 | |
2723 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
2724 | ConvertedType = Context.getPointerType(FromPointee); |
2725 | return true; |
2726 | } |
2727 | |
2728 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
2729 | QualType& ConvertedType) { |
2730 | QualType ToPointeeType; |
2731 | if (const BlockPointerType *ToBlockPtr = |
2732 | ToType->getAs<BlockPointerType>()) |
2733 | ToPointeeType = ToBlockPtr->getPointeeType(); |
2734 | else |
2735 | return false; |
2736 | |
2737 | QualType FromPointeeType; |
2738 | if (const BlockPointerType *FromBlockPtr = |
2739 | FromType->getAs<BlockPointerType>()) |
2740 | FromPointeeType = FromBlockPtr->getPointeeType(); |
2741 | else |
2742 | return false; |
2743 | |
2744 | |
2745 | |
2746 | |
2747 | const FunctionProtoType *FromFunctionType |
2748 | = FromPointeeType->getAs<FunctionProtoType>(); |
2749 | const FunctionProtoType *ToFunctionType |
2750 | = ToPointeeType->getAs<FunctionProtoType>(); |
2751 | |
2752 | if (!FromFunctionType || !ToFunctionType) |
2753 | return false; |
2754 | |
2755 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
2756 | return true; |
2757 | |
2758 | |
2759 | |
2760 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
2761 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
2762 | return false; |
2763 | |
2764 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
2765 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
2766 | if (FromEInfo != ToEInfo) |
2767 | return false; |
2768 | |
2769 | bool IncompatibleObjC = false; |
2770 | if (Context.hasSameType(FromFunctionType->getReturnType(), |
2771 | ToFunctionType->getReturnType())) { |
2772 | |
2773 | } else { |
2774 | QualType RHS = FromFunctionType->getReturnType(); |
2775 | QualType LHS = ToFunctionType->getReturnType(); |
2776 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
2777 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
2778 | LHS = LHS.getUnqualifiedType(); |
2779 | |
2780 | if (Context.hasSameType(RHS,LHS)) { |
2781 | |
2782 | } else if (isObjCPointerConversion(RHS, LHS, |
2783 | ConvertedType, IncompatibleObjC)) { |
2784 | if (IncompatibleObjC) |
2785 | return false; |
2786 | |
2787 | } |
2788 | else |
2789 | return false; |
2790 | } |
2791 | |
2792 | |
2793 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
2794 | ArgIdx != NumArgs; ++ArgIdx) { |
2795 | IncompatibleObjC = false; |
2796 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
2797 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
2798 | if (Context.hasSameType(FromArgType, ToArgType)) { |
2799 | |
2800 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
2801 | ConvertedType, IncompatibleObjC)) { |
2802 | if (IncompatibleObjC) |
2803 | return false; |
2804 | |
2805 | } else |
2806 | |
2807 | return false; |
2808 | } |
2809 | |
2810 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
2811 | bool CanUseToFPT, CanUseFromFPT; |
2812 | if (!Context.mergeExtParameterInfo(ToFunctionType, FromFunctionType, |
2813 | CanUseToFPT, CanUseFromFPT, |
2814 | NewParamInfos)) |
2815 | return false; |
2816 | |
2817 | ConvertedType = ToType; |
2818 | return true; |
2819 | } |
2820 | |
2821 | enum { |
2822 | ft_default, |
2823 | ft_different_class, |
2824 | ft_parameter_arity, |
2825 | ft_parameter_mismatch, |
2826 | ft_return_type, |
2827 | ft_qualifer_mismatch, |
2828 | ft_noexcept |
2829 | }; |
2830 | |
2831 | |
2832 | |
2833 | static const FunctionProtoType *tryGetFunctionProtoType(QualType FromType) { |
2834 | if (auto *FPT = FromType->getAs<FunctionProtoType>()) |
2835 | return FPT; |
2836 | |
2837 | if (auto *MPT = FromType->getAs<MemberPointerType>()) |
2838 | return MPT->getPointeeType()->getAs<FunctionProtoType>(); |
2839 | |
2840 | return nullptr; |
2841 | } |
2842 | |
2843 | |
2844 | |
2845 | |
2846 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
2847 | QualType FromType, QualType ToType) { |
2848 | |
2849 | if (FromType.isNull() || ToType.isNull()) { |
2850 | PDiag << ft_default; |
2851 | return; |
2852 | } |
2853 | |
2854 | |
2855 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
2856 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
2857 | *ToMember = ToType->getAs<MemberPointerType>(); |
2858 | if (!Context.hasSameType(FromMember->getClass(), ToMember->getClass())) { |
2859 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
2860 | << QualType(FromMember->getClass(), 0); |
2861 | return; |
2862 | } |
2863 | FromType = FromMember->getPointeeType(); |
2864 | ToType = ToMember->getPointeeType(); |
2865 | } |
2866 | |
2867 | if (FromType->isPointerType()) |
2868 | FromType = FromType->getPointeeType(); |
2869 | if (ToType->isPointerType()) |
2870 | ToType = ToType->getPointeeType(); |
2871 | |
2872 | |
2873 | FromType = FromType.getNonReferenceType(); |
2874 | ToType = ToType.getNonReferenceType(); |
2875 | |
2876 | |
2877 | if (FromType->isInstantiationDependentType() && |
2878 | !FromType->getAs<TemplateSpecializationType>()) { |
2879 | PDiag << ft_default; |
2880 | return; |
2881 | } |
2882 | |
2883 | |
2884 | if (Context.hasSameType(FromType, ToType)) { |
2885 | PDiag << ft_default; |
2886 | return; |
2887 | } |
2888 | |
2889 | const FunctionProtoType *FromFunction = tryGetFunctionProtoType(FromType), |
2890 | *ToFunction = tryGetFunctionProtoType(ToType); |
2891 | |
2892 | |
2893 | if (!FromFunction || !ToFunction) { |
2894 | PDiag << ft_default; |
2895 | return; |
2896 | } |
2897 | |
2898 | if (FromFunction->getNumParams() != ToFunction->getNumParams()) { |
2899 | PDiag << ft_parameter_arity << ToFunction->getNumParams() |
2900 | << FromFunction->getNumParams(); |
2901 | return; |
2902 | } |
2903 | |
2904 | |
2905 | unsigned ArgPos; |
2906 | if (!FunctionParamTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
2907 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
2908 | << ToFunction->getParamType(ArgPos) |
2909 | << FromFunction->getParamType(ArgPos); |
2910 | return; |
2911 | } |
2912 | |
2913 | |
2914 | if (!Context.hasSameType(FromFunction->getReturnType(), |
2915 | ToFunction->getReturnType())) { |
2916 | PDiag << ft_return_type << ToFunction->getReturnType() |
2917 | << FromFunction->getReturnType(); |
2918 | return; |
2919 | } |
2920 | |
2921 | if (FromFunction->getMethodQuals() != ToFunction->getMethodQuals()) { |
2922 | PDiag << ft_qualifer_mismatch << ToFunction->getMethodQuals() |
2923 | << FromFunction->getMethodQuals(); |
2924 | return; |
2925 | } |
2926 | |
2927 | |
2928 | |
2929 | if (cast<FunctionProtoType>(FromFunction->getCanonicalTypeUnqualified()) |
2930 | ->isNothrow() != |
2931 | cast<FunctionProtoType>(ToFunction->getCanonicalTypeUnqualified()) |
2932 | ->isNothrow()) { |
2933 | PDiag << ft_noexcept; |
2934 | return; |
2935 | } |
2936 | |
2937 | |
2938 | PDiag << ft_default; |
2939 | } |
2940 | |
2941 | |
2942 | |
2943 | |
2944 | |
2945 | bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, |
2946 | const FunctionProtoType *NewType, |
2947 | unsigned *ArgPos) { |
2948 | for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), |
2949 | N = NewType->param_type_begin(), |
2950 | E = OldType->param_type_end(); |
2951 | O && (O != E); ++O, ++N) { |
2952 | |
2953 | |
2954 | QualType Old = Context.removePtrSizeAddrSpace(O->getUnqualifiedType()); |
2955 | QualType New = Context.removePtrSizeAddrSpace(N->getUnqualifiedType()); |
2956 | |
2957 | if (!Context.hasSameType(Old, New)) { |
2958 | if (ArgPos) |
2959 | *ArgPos = O - OldType->param_type_begin(); |
2960 | return false; |
2961 | } |
2962 | } |
2963 | return true; |
2964 | } |
2965 | |
2966 | |
2967 | |
2968 | |
2969 | |
2970 | |
2971 | |
2972 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
2973 | CastKind &Kind, |
2974 | CXXCastPath& BasePath, |
2975 | bool IgnoreBaseAccess, |
2976 | bool Diagnose) { |
2977 | QualType FromType = From->getType(); |
2978 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
2979 | |
2980 | Kind = CK_BitCast; |
2981 | |
2982 | if (Diagnose && !IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
2983 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
2984 | Expr::NPCK_ZeroExpression) { |
2985 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
2986 | DiagRuntimeBehavior(From->getExprLoc(), From, |
2987 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
2988 | << ToType << From->getSourceRange()); |
2989 | else if (!isUnevaluatedContext()) |
2990 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
2991 | << ToType << From->getSourceRange(); |
2992 | } |
2993 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
2994 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
2995 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
2996 | ToPointeeType = ToPtrType->getPointeeType(); |
2997 | |
2998 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
2999 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
3000 | |
3001 | |
3002 | unsigned InaccessibleID = 0; |
3003 | unsigned AmbigiousID = 0; |
3004 | if (Diagnose) { |
3005 | InaccessibleID = diag::err_upcast_to_inaccessible_base; |
3006 | AmbigiousID = diag::err_ambiguous_derived_to_base_conv; |
3007 | } |
3008 | if (CheckDerivedToBaseConversion( |
3009 | FromPointeeType, ToPointeeType, InaccessibleID, AmbigiousID, |
3010 | From->getExprLoc(), From->getSourceRange(), DeclarationName(), |
3011 | &BasePath, IgnoreBaseAccess)) |
3012 | return true; |
3013 | |
3014 | |
3015 | Kind = CK_DerivedToBase; |
3016 | } |
3017 | |
3018 | if (Diagnose && !IsCStyleOrFunctionalCast && |
3019 | FromPointeeType->isFunctionType() && ToPointeeType->isVoidType()) { |
3020 | assert(getLangOpts().MSVCCompat && |
3021 | "this should only be possible with MSVCCompat!"); |
3022 | Diag(From->getExprLoc(), diag::ext_ms_impcast_fn_obj) |
3023 | << From->getSourceRange(); |
3024 | } |
3025 | } |
3026 | } else if (const ObjCObjectPointerType *ToPtrType = |
3027 | ToType->getAs<ObjCObjectPointerType>()) { |
3028 | if (const ObjCObjectPointerType *FromPtrType = |
3029 | FromType->getAs<ObjCObjectPointerType>()) { |
3030 | |
3031 | |
3032 | |
3033 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
3034 | return false; |
3035 | } else if (FromType->isBlockPointerType()) { |
3036 | Kind = CK_BlockPointerToObjCPointerCast; |
3037 | } else { |
3038 | Kind = CK_CPointerToObjCPointerCast; |
3039 | } |
3040 | } else if (ToType->isBlockPointerType()) { |
3041 | if (!FromType->isBlockPointerType()) |
3042 | Kind = CK_AnyPointerToBlockPointerCast; |
3043 | } |
3044 | |
3045 | |
3046 | |
3047 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
3048 | Kind = CK_NullToPointer; |
3049 | |
3050 | return false; |
3051 | } |
3052 | |
3053 | |
3054 | |
3055 | |
3056 | |
3057 | |
3058 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
3059 | QualType ToType, |
3060 | bool InOverloadResolution, |
3061 | QualType &ConvertedType) { |
3062 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
3063 | if (!ToTypePtr) |
3064 | return false; |
3065 | |
3066 | |
3067 | if (From->isNullPointerConstant(Context, |
3068 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
3069 | : Expr::NPC_ValueDependentIsNull)) { |
3070 | ConvertedType = ToType; |
3071 | return true; |
3072 | } |
3073 | |
3074 | |
3075 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
3076 | if (!FromTypePtr) |
3077 | return false; |
3078 | |
3079 | |
3080 | |
3081 | QualType FromClass(FromTypePtr->getClass(), 0); |
3082 | QualType ToClass(ToTypePtr->getClass(), 0); |
3083 | |
3084 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
3085 | IsDerivedFrom(From->getBeginLoc(), ToClass, FromClass)) { |
3086 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
3087 | ToClass.getTypePtr()); |
3088 | return true; |
3089 | } |
3090 | |
3091 | return false; |
3092 | } |
3093 | |
3094 | |
3095 | |
3096 | |
3097 | |
3098 | |
3099 | |
3100 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
3101 | CastKind &Kind, |
3102 | CXXCastPath &BasePath, |
3103 | bool IgnoreBaseAccess) { |
3104 | QualType FromType = From->getType(); |
3105 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
3106 | if (!FromPtrType) { |
3107 | |
3108 | assert(From->isNullPointerConstant(Context, |
3109 | Expr::NPC_ValueDependentIsNull) && |
3110 | "Expr must be null pointer constant!"); |
3111 | Kind = CK_NullToMemberPointer; |
3112 | return false; |
3113 | } |
3114 | |
3115 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
3116 | assert(ToPtrType && "No member pointer cast has a target type " |
3117 | "that is not a member pointer."); |
3118 | |
3119 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
3120 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
3121 | |
3122 | |
3123 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
3124 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
3125 | |
3126 | CXXBasePaths Paths(true, true, |
3127 | true); |
3128 | bool DerivationOkay = |
3129 | IsDerivedFrom(From->getBeginLoc(), ToClass, FromClass, Paths); |
3130 | assert(DerivationOkay && |
3131 | "Should not have been called if derivation isn't OK."); |
3132 | (void)DerivationOkay; |
3133 | |
3134 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
3135 | getUnqualifiedType())) { |
3136 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
3137 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
3138 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
3139 | return true; |
3140 | } |
3141 | |
3142 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
3143 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
3144 | << FromClass << ToClass << QualType(VBase, 0) |
3145 | << From->getSourceRange(); |
3146 | return true; |
3147 | } |
3148 | |
3149 | if (!IgnoreBaseAccess) |
3150 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
3151 | Paths.front(), |
3152 | diag::err_downcast_from_inaccessible_base); |
3153 | |
3154 | |
3155 | BuildBasePathArray(Paths, BasePath); |
3156 | Kind = CK_BaseToDerivedMemberPointer; |
3157 | return false; |
3158 | } |
3159 | |
3160 | |
3161 | |
3162 | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
3163 | Qualifiers ToQuals) { |
3164 | |
3165 | if (ToQuals.hasConst() && |
3166 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
3167 | return false; |
3168 | |
3169 | return true; |
3170 | } |
3171 | |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | |
3177 | |
3178 | static bool isQualificationConversionStep(QualType FromType, QualType ToType, |
3179 | bool CStyle, |
3180 | bool &PreviousToQualsIncludeConst, |
3181 | bool &ObjCLifetimeConversion) { |
3182 | Qualifiers FromQuals = FromType.getQualifiers(); |
3183 | Qualifiers ToQuals = ToType.getQualifiers(); |
3184 | |
3185 | |
3186 | if (ToType.getUnqualifiedType()->isVoidType()) |
3187 | FromQuals.removeUnaligned(); |
3188 | |
3189 | |
3190 | |
3191 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime()) { |
3192 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
3193 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
3194 | ObjCLifetimeConversion = true; |
3195 | FromQuals.removeObjCLifetime(); |
3196 | ToQuals.removeObjCLifetime(); |
3197 | } else { |
3198 | |
3199 | |
3200 | return false; |
3201 | } |
3202 | } |
3203 | |
3204 | |
3205 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
3206 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
3207 | FromQuals.removeObjCGCAttr(); |
3208 | ToQuals.removeObjCGCAttr(); |
3209 | } |
3210 | |
3211 | |
3212 | |
3213 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
3214 | return false; |
3215 | |
3216 | |
3217 | |
3218 | |
3219 | if (CStyle && !ToQuals.isAddressSpaceSupersetOf(FromQuals) && |
3220 | !FromQuals.isAddressSpaceSupersetOf(ToQuals)) |
3221 | return false; |
3222 | |
3223 | |
3224 | |
3225 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() && |
3226 | !PreviousToQualsIncludeConst) |
3227 | return false; |
3228 | |
3229 | |
3230 | |
3231 | PreviousToQualsIncludeConst = |
3232 | PreviousToQualsIncludeConst && ToQuals.hasConst(); |
3233 | return true; |
3234 | } |
3235 | |
3236 | |
3237 | |
3238 | |
3239 | |
3240 | |
3241 | |
3242 | |
3243 | bool |
3244 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
3245 | bool CStyle, bool &ObjCLifetimeConversion) { |
3246 | FromType = Context.getCanonicalType(FromType); |
3247 | ToType = Context.getCanonicalType(ToType); |
3248 | ObjCLifetimeConversion = false; |
3249 | |
3250 | |
3251 | |
3252 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
3253 | return false; |
3254 | |
3255 | |
3256 | |
3257 | |
3258 | bool PreviousToQualsIncludeConst = true; |
3259 | bool UnwrappedAnyPointer = false; |
3260 | while (Context.UnwrapSimilarTypes(FromType, ToType)) { |
3261 | if (!isQualificationConversionStep(FromType, ToType, CStyle, |
3262 | PreviousToQualsIncludeConst, |
3263 | ObjCLifetimeConversion)) |
3264 | return false; |
3265 | UnwrappedAnyPointer = true; |
3266 | } |
3267 | |
3268 | |
3269 | |
3270 | |
3271 | |
3272 | |
3273 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
3274 | } |
3275 | |
3276 | |
3277 | |
3278 | |
3279 | |
3280 | |
3281 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
3282 | bool InOverloadResolution, |
3283 | StandardConversionSequence &SCS, |
3284 | bool CStyle) { |
3285 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
3286 | if (!ToAtomic) |
3287 | return false; |
3288 | |
3289 | StandardConversionSequence InnerSCS; |
3290 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
3291 | InOverloadResolution, InnerSCS, |
3292 | CStyle, false)) |
3293 | return false; |
3294 | |
3295 | SCS.Second = InnerSCS.Second; |
3296 | SCS.setToType(1, InnerSCS.getToType(1)); |
3297 | SCS.Third = InnerSCS.Third; |
3298 | SCS.QualificationIncludesObjCLifetime |
3299 | = InnerSCS.QualificationIncludesObjCLifetime; |
3300 | SCS.setToType(2, InnerSCS.getToType(2)); |
3301 | return true; |
3302 | } |
3303 | |
3304 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
3305 | CXXConstructorDecl *Constructor, |
3306 | QualType Type) { |
3307 | const FunctionProtoType *CtorType = |
| 24 | | 'CtorType' initialized to a null pointer value | |
|
3308 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 23 | | Assuming the object is not a 'FunctionProtoType' | |
|
3309 | if (CtorType->getNumParams() > 0) { |
| 25 | | Called C++ object pointer is null |
|
3310 | QualType FirstArg = CtorType->getParamType(0); |
3311 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
3312 | return true; |
3313 | } |
3314 | return false; |
3315 | } |
3316 | |
3317 | static OverloadingResult |
3318 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
3319 | CXXRecordDecl *To, |
3320 | UserDefinedConversionSequence &User, |
3321 | OverloadCandidateSet &CandidateSet, |
3322 | bool AllowExplicit) { |
3323 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3324 | for (auto *D : S.LookupConstructors(To)) { |
3325 | auto Info = getConstructorInfo(D); |
3326 | if (!Info) |
| |
3327 | continue; |
3328 | |
3329 | bool Usable = !Info.Constructor->isInvalidDecl() && |
| 19 | | Assuming the condition is true | |
|
3330 | S.isInitListConstructor(Info.Constructor); |
3331 | if (Usable) { |
| 20 | | Assuming 'Usable' is true | |
|
| |
3332 | |
3333 | |
3334 | bool SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 22 | | Calling 'isFirstArgumentCompatibleWithType' | |
|
3335 | S.Context, Info.Constructor, ToType); |
3336 | if (Info.ConstructorTmpl) |
3337 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
3338 | nullptr, From, |
3339 | CandidateSet, SuppressUserConversions, |
3340 | false, |
3341 | AllowExplicit); |
3342 | else |
3343 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, From, |
3344 | CandidateSet, SuppressUserConversions, |
3345 | false, AllowExplicit); |
3346 | } |
3347 | } |
3348 | |
3349 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
3350 | |
3351 | OverloadCandidateSet::iterator Best; |
3352 | switch (auto Result = |
3353 | CandidateSet.BestViableFunction(S, From->getBeginLoc(), Best)) { |
3354 | case OR_Deleted: |
3355 | case OR_Success: { |
3356 | |
3357 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
3358 | QualType ThisType = Constructor->getThisType(); |
3359 | |
3360 | User.Before.setAsIdentityConversion(); |
3361 | User.HadMultipleCandidates = HadMultipleCandidates; |
3362 | User.ConversionFunction = Constructor; |
3363 | User.FoundConversionFunction = Best->FoundDecl; |
3364 | User.After.setAsIdentityConversion(); |
3365 | User.After.setFromType(ThisType->castAs<PointerType>()->getPointeeType()); |
3366 | User.After.setAllToTypes(ToType); |
3367 | return Result; |
3368 | } |
3369 | |
3370 | case OR_No_Viable_Function: |
3371 | return OR_No_Viable_Function; |
3372 | case OR_Ambiguous: |
3373 | return OR_Ambiguous; |
3374 | } |
3375 | |
3376 | llvm_unreachable("Invalid OverloadResult!"); |
3377 | } |
3378 | |
3379 | |
3380 | |
3381 | |
3382 | |
3383 | |
3384 | |
3385 | |
3386 | |
3387 | |
3388 | |
3389 | |
3390 | |
3391 | |
3392 | |
3393 | static OverloadingResult |
3394 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
3395 | UserDefinedConversionSequence &User, |
3396 | OverloadCandidateSet &CandidateSet, |
3397 | bool AllowExplicit, |
3398 | bool AllowObjCConversionOnExplicit) { |
3399 | assert(AllowExplicit || !AllowObjCConversionOnExplicit); |
| |
3400 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3401 | |
3402 | |
3403 | bool ConstructorsOnly = false; |
3404 | |
3405 | |
3406 | |
3407 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
| 6 | | Assuming the object is a 'RecordType' | |
|
| 7 | | Assuming 'ToRecordType' is non-null | |
|
| |
3408 | |
3409 | |
3410 | |
3411 | |
3412 | |
3413 | |
3414 | |
3415 | |
3416 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 9 | | Assuming the condition is false | |
|
| |
3417 | (From->getType()->getAs<RecordType>() && |
| 10 | | Assuming the object is not a 'RecordType' | |
|
3418 | S.IsDerivedFrom(From->getBeginLoc(), From->getType(), ToType))) |
3419 | ConstructorsOnly = true; |
3420 | |
3421 | if (!S.isCompleteType(From->getExprLoc(), ToType)) { |
| |
3422 | |
3423 | } else if (CXXRecordDecl *ToRecordDecl |
| |
3424 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 13 | | Assuming the object is a 'CXXRecordDecl' | |
|
3425 | |
3426 | Expr **Args = &From; |
3427 | unsigned NumArgs = 1; |
3428 | bool ListInitializing = false; |
3429 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
| 15 | | Assuming 'From' is a 'InitListExpr' | |
|
| |
3430 | |
3431 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 17 | | Calling 'IsInitializerListConstructorConversion' | |
|
3432 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
3433 | if (Result != OR_No_Viable_Function) |
3434 | return Result; |
3435 | |
3436 | CandidateSet.clear( |
3437 | OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
3438 | |
3439 | |
3440 | |
3441 | Args = InitList->getInits(); |
3442 | NumArgs = InitList->getNumInits(); |
3443 | ListInitializing = true; |
3444 | } |
3445 | |
3446 | for (auto *D : S.LookupConstructors(ToRecordDecl)) { |
3447 | auto Info = getConstructorInfo(D); |
3448 | if (!Info) |
3449 | continue; |
3450 | |
3451 | bool Usable = !Info.Constructor->isInvalidDecl(); |
3452 | if (!ListInitializing) |
3453 | Usable = Usable && Info.Constructor->isConvertingConstructor( |
3454 | true); |
3455 | if (Usable) { |
3456 | bool SuppressUserConversions = !ConstructorsOnly; |
3457 | if (SuppressUserConversions && ListInitializing) { |
3458 | SuppressUserConversions = false; |
3459 | if (NumArgs == 1) { |
3460 | |
3461 | |
3462 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
3463 | S.Context, Info.Constructor, ToType); |
3464 | } |
3465 | } |
3466 | if (Info.ConstructorTmpl) |
3467 | S.AddTemplateOverloadCandidate( |
3468 | Info.ConstructorTmpl, Info.FoundDecl, |
3469 | nullptr, llvm::makeArrayRef(Args, NumArgs), |
3470 | CandidateSet, SuppressUserConversions, |
3471 | false, AllowExplicit); |
3472 | else |
3473 | |
3474 | |
3475 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
3476 | llvm::makeArrayRef(Args, NumArgs), |
3477 | CandidateSet, SuppressUserConversions, |
3478 | false, AllowExplicit); |
3479 | } |
3480 | } |
3481 | } |
3482 | } |
3483 | |
3484 | |
3485 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
3486 | } else if (!S.isCompleteType(From->getBeginLoc(), From->getType())) { |
3487 | |
3488 | } else if (const RecordType *FromRecordType = |
3489 | From->getType()->getAs<RecordType>()) { |
3490 | if (CXXRecordDecl *FromRecordDecl |
3491 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
3492 | |
3493 | const auto &Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
3494 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
3495 | DeclAccessPair FoundDecl = I.getPair(); |
3496 | NamedDecl *D = FoundDecl.getDecl(); |
3497 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
3498 | if (isa<UsingShadowDecl>(D)) |
3499 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
3500 | |
3501 | CXXConversionDecl *Conv; |
3502 | FunctionTemplateDecl *ConvTemplate; |
3503 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
3504 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
3505 | else |
3506 | Conv = cast<CXXConversionDecl>(D); |
3507 | |
3508 | if (ConvTemplate) |
3509 | S.AddTemplateConversionCandidate( |
3510 | ConvTemplate, FoundDecl, ActingContext, From, ToType, |
3511 | CandidateSet, AllowObjCConversionOnExplicit, AllowExplicit); |
3512 | else |
3513 | S.AddConversionCandidate( |
3514 | Conv, FoundDecl, ActingContext, From, ToType, CandidateSet, |
3515 | AllowObjCConversionOnExplicit, AllowExplicit); |
3516 | } |
3517 | } |
3518 | } |
3519 | |
3520 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
3521 | |
3522 | OverloadCandidateSet::iterator Best; |
3523 | switch (auto Result = |
3524 | CandidateSet.BestViableFunction(S, From->getBeginLoc(), Best)) { |
3525 | case OR_Success: |
3526 | case OR_Deleted: |
3527 | |
3528 | if (CXXConstructorDecl *Constructor |
3529 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
3530 | |
3531 | |
3532 | |
3533 | |
3534 | |
3535 | |
3536 | QualType ThisType = Constructor->getThisType(); |
3537 | if (isa<InitListExpr>(From)) { |
3538 | |
3539 | User.Before.setAsIdentityConversion(); |
3540 | } else { |
3541 | if (Best->Conversions[0].isEllipsis()) |
3542 | User.EllipsisConversion = true; |
3543 | else { |
3544 | User.Before = Best->Conversions[0].Standard; |
3545 | User.EllipsisConversion = false; |
3546 | } |
3547 | } |
3548 | User.HadMultipleCandidates = HadMultipleCandidates; |
3549 | User.ConversionFunction = Constructor; |
3550 | User.FoundConversionFunction = Best->FoundDecl; |
3551 | User.After.setAsIdentityConversion(); |
3552 | User.After.setFromType(ThisType->castAs<PointerType>()->getPointeeType()); |
3553 | User.After.setAllToTypes(ToType); |
3554 | return Result; |
3555 | } |
3556 | if (CXXConversionDecl *Conversion |
3557 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | User.Before = Best->Conversions[0].Standard; |
3565 | User.HadMultipleCandidates = HadMultipleCandidates; |
3566 | User.ConversionFunction = Conversion; |
3567 | User.FoundConversionFunction = Best->FoundDecl; |
3568 | User.EllipsisConversion = false; |
3569 | |
3570 | |
3571 | |
3572 | |
3573 | |
3574 | |
3575 | |
3576 | |
3577 | |
3578 | |
3579 | User.After = Best->FinalConversion; |
3580 | return Result; |
3581 | } |
3582 | llvm_unreachable("Not a constructor or conversion function?"); |
3583 | |
3584 | case OR_No_Viable_Function: |
3585 | return OR_No_Viable_Function; |
3586 | |
3587 | case OR_Ambiguous: |
3588 | return OR_Ambiguous; |
3589 | } |
3590 | |
3591 | llvm_unreachable("Invalid OverloadResult!"); |
3592 | } |
3593 | |
3594 | bool |
3595 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
3596 | ImplicitConversionSequence ICS; |
3597 | OverloadCandidateSet CandidateSet(From->getExprLoc(), |
3598 | OverloadCandidateSet::CSK_Normal); |
3599 | OverloadingResult OvResult = |
3600 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
| 4 | | Calling 'IsUserDefinedConversion' | |
|
3601 | CandidateSet, false, false); |
3602 | |
3603 | if (!(OvResult == OR_Ambiguous || |
3604 | (OvResult == OR_No_Viable_Function && !CandidateSet.empty()))) |
3605 | return false; |
3606 | |
3607 | auto Cands = CandidateSet.CompleteCandidates( |
3608 | *this, |
3609 | OvResult == OR_Ambiguous ? OCD_AmbiguousCandidates : OCD_AllCandidates, |
3610 | From); |
3611 | if (OvResult == OR_Ambiguous) |
3612 | Diag(From->getBeginLoc(), diag::err_typecheck_ambiguous_condition) |
3613 | << From->getType() << ToType << From->getSourceRange(); |
3614 | else { |
3615 | if (!RequireCompleteType(From->getBeginLoc(), ToType, |
3616 | diag::err_typecheck_nonviable_condition_incomplete, |
3617 | From->getType(), From->getSourceRange())) |
3618 | Diag(From->getBeginLoc(), diag::err_typecheck_nonviable_condition) |
3619 | << false << From->getType() << From->getSourceRange() << ToType; |
3620 | } |
3621 | |
3622 | CandidateSet.NoteCandidates( |
3623 | *this, From, Cands); |
3624 | return true; |
3625 | } |
3626 | |
3627 | |
3628 | |
3629 | |
3630 | static ImplicitConversionSequence::CompareKind |
3631 | compareConversionFunctions(Sema &S, FunctionDecl *Function1, |
3632 | FunctionDecl *Function2) { |
3633 | if (!S.getLangOpts().ObjC || !S.getLangOpts().CPlusPlus11) |
3634 | return ImplicitConversionSequence::Indistinguishable; |
3635 | |
3636 | |
3637 | |
3638 | |
3639 | |
3640 | |
3641 | |
3642 | CXXConversionDecl *Conv1 = dyn_cast_or_null<CXXConversionDecl>(Function1); |
3643 | if (!Conv1) |
3644 | return ImplicitConversionSequence::Indistinguishable; |
3645 | |
3646 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
3647 | if (!Conv2) |
3648 | return ImplicitConversionSequence::Indistinguishable; |
3649 | |
3650 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
3651 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
3652 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
3653 | if (Block1 != Block2) |
3654 | return Block1 ? ImplicitConversionSequence::Worse |
3655 | : ImplicitConversionSequence::Better; |
3656 | } |
3657 | |
3658 | return ImplicitConversionSequence::Indistinguishable; |
3659 | } |
3660 | |
3661 | static bool hasDeprecatedStringLiteralToCharPtrConversion( |
3662 | const ImplicitConversionSequence &ICS) { |
3663 | return (ICS.isStandard() && ICS.Standard.DeprecatedStringLiteralToCharPtr) || |
3664 | (ICS.isUserDefined() && |
3665 | ICS.UserDefined.Before.DeprecatedStringLiteralToCharPtr); |
3666 | } |
3667 | |
3668 | |
3669 | |
3670 | |
3671 | static ImplicitConversionSequence::CompareKind |
3672 | CompareImplicitConversionSequences(Sema &S, SourceLocation Loc, |
3673 | const ImplicitConversionSequence& ICS1, |
3674 | const ImplicitConversionSequence& ICS2) |
3675 | { |
3676 | |
3677 | |
3678 | |
3679 | |
3680 | |
3681 | |
3682 | |
3683 | |
3684 | |
3685 | |
3686 | |
3687 | |
3688 | |
3689 | |
3690 | |
3691 | |
3692 | |
3693 | |
3694 | |
3695 | |
3696 | |
3697 | |
3698 | |
3699 | |
3700 | |
3701 | |
3702 | |
3703 | |
3704 | |
3705 | |
3706 | |
3707 | |
3708 | |
3709 | if (S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
3710 | hasDeprecatedStringLiteralToCharPtrConversion(ICS1) != |
3711 | hasDeprecatedStringLiteralToCharPtrConversion(ICS2)) |
3712 | return hasDeprecatedStringLiteralToCharPtrConversion(ICS1) |
3713 | ? ImplicitConversionSequence::Worse |
3714 | : ImplicitConversionSequence::Better; |
3715 | |
3716 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
3717 | return ImplicitConversionSequence::Better; |
3718 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
3719 | return ImplicitConversionSequence::Worse; |
3720 | |
3721 | |
3722 | |
3723 | if (ICS1.getKind() != ICS2.getKind()) |
3724 | return ImplicitConversionSequence::Indistinguishable; |
3725 | |
3726 | ImplicitConversionSequence::CompareKind Result = |
3727 | ImplicitConversionSequence::Indistinguishable; |
3728 | |
3729 | |
3730 | |
3731 | |
3732 | |
3733 | |
3734 | |
3735 | |
3736 | |
3737 | |
3738 | |
3739 | |
3740 | if (!ICS1.isBad()) { |
3741 | if (ICS1.isStdInitializerListElement() && |
3742 | !ICS2.isStdInitializerListElement()) |
3743 | return ImplicitConversionSequence::Better; |
3744 | if (!ICS1.isStdInitializerListElement() && |
3745 | ICS2.isStdInitializerListElement()) |
3746 | return ImplicitConversionSequence::Worse; |
3747 | } |
3748 | |
3749 | if (ICS1.isStandard()) |
3750 | |
3751 | |
3752 | Result = CompareStandardConversionSequences(S, Loc, |
3753 | ICS1.Standard, ICS2.Standard); |
3754 | else if (ICS1.isUserDefined()) { |
3755 | |
3756 | |
3757 | |
3758 | |
3759 | |
3760 | |
3761 | if (ICS1.UserDefined.ConversionFunction == |
3762 | ICS2.UserDefined.ConversionFunction) |
3763 | Result = CompareStandardConversionSequences(S, Loc, |
3764 | ICS1.UserDefined.After, |
3765 | ICS2.UserDefined.After); |
3766 | else |
3767 | Result = compareConversionFunctions(S, |
3768 | ICS1.UserDefined.ConversionFunction, |
3769 | ICS2.UserDefined.ConversionFunction); |
3770 | } |
3771 | |
3772 | return Result; |
3773 | } |
3774 | |
3775 | |
3776 | |
3777 | static ImplicitConversionSequence::CompareKind |
3778 | compareStandardConversionSubsets(ASTContext &Context, |
3779 | const StandardConversionSequence& SCS1, |
3780 | const StandardConversionSequence& SCS2) { |
3781 | ImplicitConversionSequence::CompareKind Result |
3782 | = ImplicitConversionSequence::Indistinguishable; |
3783 | |
3784 | |
3785 | |
3786 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
3787 | return ImplicitConversionSequence::Better; |
3788 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
3789 | return ImplicitConversionSequence::Worse; |
3790 | |
3791 | if (SCS1.Second != SCS2.Second) { |
3792 | if (SCS1.Second == ICK_Identity) |
3793 | Result = ImplicitConversionSequence::Better; |
3794 | else if (SCS2.Second == ICK_Identity) |
3795 | Result = ImplicitConversionSequence::Worse; |
3796 | else |
3797 | return ImplicitConversionSequence::Indistinguishable; |
3798 | } else if (!Context.hasSimilarType(SCS1.getToType(1), SCS2.getToType(1))) |
3799 | return ImplicitConversionSequence::Indistinguishable; |
3800 | |
3801 | if (SCS1.Third == SCS2.Third) { |
3802 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
3803 | : ImplicitConversionSequence::Indistinguishable; |
3804 | } |
3805 | |
3806 | if (SCS1.Third == ICK_Identity) |
3807 | return Result == ImplicitConversionSequence::Worse |
3808 | ? ImplicitConversionSequence::Indistinguishable |
3809 | : ImplicitConversionSequence::Better; |
3810 | |
3811 | if (SCS2.Third == ICK_Identity) |
3812 | return Result == ImplicitConversionSequence::Better |
3813 | ? ImplicitConversionSequence::Indistinguishable |
3814 | : ImplicitConversionSequence::Worse; |
3815 | |
3816 | return ImplicitConversionSequence::Indistinguishable; |
3817 | } |
3818 | |
3819 | |
3820 | |
3821 | static bool |
3822 | isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
3823 | const StandardConversionSequence &SCS2) { |
3824 | |
3825 | |
3826 | |
3827 | |
3828 | |
3829 | |
3830 | |
3831 | |
3832 | |
3833 | |
3834 | |
3835 | |
3836 | |
3837 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
3838 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
3839 | return false; |
3840 | |
3841 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
3842 | SCS2.IsLvalueReference) || |
3843 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
3844 | !SCS2.IsLvalueReference && SCS2.BindsToFunctionLvalue); |
3845 | } |
3846 | |
3847 | enum class FixedEnumPromotion { |
3848 | None, |
3849 | ToUnderlyingType, |
3850 | ToPromotedUnderlyingType |
3851 | }; |
3852 | |
3853 | |
3854 | static FixedEnumPromotion |
3855 | getFixedEnumPromtion(Sema &S, const StandardConversionSequence &SCS) { |
3856 | |
3857 | if (SCS.Second != ICK_Integral_Promotion) |
3858 | return FixedEnumPromotion::None; |
3859 | |
3860 | QualType FromType = SCS.getFromType(); |
3861 | if (!FromType->isEnumeralType()) |
3862 | return FixedEnumPromotion::None; |
3863 | |
3864 | EnumDecl *Enum = FromType->getAs<EnumType>()->getDecl(); |
3865 | if (!Enum->isFixed()) |
3866 | return FixedEnumPromotion::None; |
3867 | |
3868 | QualType UnderlyingType = Enum->getIntegerType(); |
3869 | if (S.Context.hasSameType(SCS.getToType(1), UnderlyingType)) |
3870 | return FixedEnumPromotion::ToUnderlyingType; |
3871 | |
3872 | return FixedEnumPromotion::ToPromotedUnderlyingType; |
3873 | } |
3874 | |
3875 | |
3876 | |
3877 | |
3878 | static ImplicitConversionSequence::CompareKind |
3879 | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
3880 | const StandardConversionSequence& SCS1, |
3881 | const StandardConversionSequence& SCS2) |
3882 | { |
3883 | |
3884 | |
3885 | |
3886 | |
3887 | |
3888 | |
3889 | |
3890 | |
3891 | if (ImplicitConversionSequence::CompareKind CK |
3892 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
3893 | return CK; |
3894 | |
3895 | |
3896 | |
3897 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
3898 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
3899 | if (Rank1 < Rank2) |
3900 | return ImplicitConversionSequence::Better; |
3901 | else if (Rank2 < Rank1) |
3902 | return ImplicitConversionSequence::Worse; |
3903 | |
3904 | |
3905 | |
3906 | |
3907 | |
3908 | |
3909 | |
3910 | |
3911 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
3912 | return SCS2.isPointerConversionToBool() |
3913 | ? ImplicitConversionSequence::Better |
3914 | : ImplicitConversionSequence::Worse; |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | FixedEnumPromotion FEP1 = getFixedEnumPromtion(S, SCS1); |
3923 | FixedEnumPromotion FEP2 = getFixedEnumPromtion(S, SCS2); |
3924 | if (FEP1 != FixedEnumPromotion::None && FEP2 != FixedEnumPromotion::None && |
3925 | FEP1 != FEP2) |
3926 | return FEP1 == FixedEnumPromotion::ToUnderlyingType |
3927 | ? ImplicitConversionSequence::Better |
3928 | : ImplicitConversionSequence::Worse; |
3929 | |
3930 | |
3931 | |
3932 | |
3933 | |
3934 | |
3935 | |
3936 | bool SCS1ConvertsToVoid |
3937 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
3938 | bool SCS2ConvertsToVoid |
3939 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
3940 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
3941 | |
3942 | |
3943 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
3944 | : ImplicitConversionSequence::Worse; |
3945 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
3946 | |
3947 | |
3948 | if (ImplicitConversionSequence::CompareKind DerivedCK |
3949 | = CompareDerivedToBaseConversions(S, Loc, SCS1, SCS2)) |
3950 | return DerivedCK; |
3951 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
3952 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
3953 | |
3954 | |
3955 | |
3956 | QualType FromType1 = SCS1.getFromType(); |
3957 | QualType FromType2 = SCS2.getFromType(); |
3958 | |
3959 | |
3960 | |
3961 | if (SCS1.First == ICK_Array_To_Pointer) |
3962 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
3963 | if (SCS2.First == ICK_Array_To_Pointer) |
3964 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
3965 | |
3966 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
3967 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
3968 | |
3969 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
3970 | return ImplicitConversionSequence::Better; |
3971 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
3972 | return ImplicitConversionSequence::Worse; |
3973 | |
3974 | |
3975 | |
3976 | const ObjCObjectPointerType* FromObjCPtr1 |
3977 | = FromType1->getAs<ObjCObjectPointerType>(); |
3978 | const ObjCObjectPointerType* FromObjCPtr2 |
3979 | = FromType2->getAs<ObjCObjectPointerType>(); |
3980 | if (FromObjCPtr1 && FromObjCPtr2) { |
3981 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
3982 | FromObjCPtr2); |
3983 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
3984 | FromObjCPtr1); |
3985 | if (AssignLeft != AssignRight) { |
3986 | return AssignLeft? ImplicitConversionSequence::Better |
3987 | : ImplicitConversionSequence::Worse; |
3988 | } |
3989 | } |
3990 | } |
3991 | |
3992 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
3993 | |
3994 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
3995 | return ImplicitConversionSequence::Better; |
3996 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
3997 | return ImplicitConversionSequence::Worse; |
3998 | } |
3999 | |
4000 | |
4001 | |
4002 | if (ImplicitConversionSequence::CompareKind QualCK |
4003 | = CompareQualificationConversions(S, SCS1, SCS2)) |
4004 | return QualCK; |
4005 | |
4006 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
4007 | |
4008 | |
4009 | |
4010 | |
4011 | |
4012 | |
4013 | QualType T1 = SCS1.getToType(2); |
4014 | QualType T2 = SCS2.getToType(2); |
4015 | T1 = S.Context.getCanonicalType(T1); |
4016 | T2 = S.Context.getCanonicalType(T2); |
4017 | Qualifiers T1Quals, T2Quals; |
4018 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
4019 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
4020 | if (UnqualT1 == UnqualT2) { |
4021 | |
4022 | |
4023 | if (SCS1.ObjCLifetimeConversionBinding != |
4024 | SCS2.ObjCLifetimeConversionBinding) { |
4025 | return SCS1.ObjCLifetimeConversionBinding |
4026 | ? ImplicitConversionSequence::Worse |
4027 | : ImplicitConversionSequence::Better; |
4028 | } |
4029 | |
4030 | |
4031 | |
4032 | if (isa<ArrayType>(T1) && T1Quals) |
4033 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
4034 | if (isa<ArrayType>(T2) && T2Quals) |
4035 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
4036 | if (T2.isMoreQualifiedThan(T1)) |
4037 | return ImplicitConversionSequence::Better; |
4038 | if (T1.isMoreQualifiedThan(T2)) |
4039 | return ImplicitConversionSequence::Worse; |
4040 | } |
4041 | } |
4042 | |
4043 | |
4044 | |
4045 | |
4046 | |
4047 | |
4048 | |
4049 | |
4050 | |
4051 | |
4052 | |
4053 | |
4054 | |
4055 | if (S.getLangOpts().MSVCCompat && SCS1.Second == ICK_Integral_Conversion && |
4056 | SCS2.Second == ICK_Floating_Integral && |
4057 | S.Context.getTypeSize(SCS1.getFromType()) == |
4058 | S.Context.getTypeSize(SCS1.getToType(2))) |
4059 | return ImplicitConversionSequence::Better; |
4060 | |
4061 | |
4062 | |
4063 | |
4064 | |
4065 | |
4066 | |
4067 | |
4068 | |
4069 | |
4070 | |
4071 | |
4072 | |
4073 | if (SCS1.Second == ICK_Vector_Conversion && |
4074 | SCS2.Second == ICK_Vector_Conversion) { |
4075 | bool SCS1IsCompatibleVectorConversion = S.Context.areCompatibleVectorTypes( |
4076 | SCS1.getFromType(), SCS1.getToType(2)); |
4077 | bool SCS2IsCompatibleVectorConversion = S.Context.areCompatibleVectorTypes( |
4078 | SCS2.getFromType(), SCS2.getToType(2)); |
4079 | |
4080 | if (SCS1IsCompatibleVectorConversion != SCS2IsCompatibleVectorConversion) |
4081 | return SCS1IsCompatibleVectorConversion |
4082 | ? ImplicitConversionSequence::Better |
4083 | : ImplicitConversionSequence::Worse; |
4084 | } |
4085 | |
4086 | return ImplicitConversionSequence::Indistinguishable; |
4087 | } |
4088 | |
4089 | |
4090 | |
4091 | |
4092 | static ImplicitConversionSequence::CompareKind |
4093 | CompareQualificationConversions(Sema &S, |
4094 | const StandardConversionSequence& SCS1, |
4095 | const StandardConversionSequence& SCS2) { |
4096 | |
4097 | |
4098 | |
4099 | |
4100 | |
4101 | |
4102 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
4103 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
4104 | return ImplicitConversionSequence::Indistinguishable; |
4105 | |
4106 | |
4107 | |
4108 | QualType T1 = SCS1.getToType(2); |
4109 | QualType T2 = SCS2.getToType(2); |
4110 | T1 = S.Context.getCanonicalType(T1); |
4111 | T2 = S.Context.getCanonicalType(T2); |
4112 | assert(!T1->isReferenceType() && !T2->isReferenceType()); |
4113 | Qualifiers T1Quals, T2Quals; |
4114 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
4115 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
4116 | |
4117 | |
4118 | |
4119 | if (UnqualT1 == UnqualT2) |
4120 | return ImplicitConversionSequence::Indistinguishable; |
4121 | |
4122 | ImplicitConversionSequence::CompareKind Result |
4123 | = ImplicitConversionSequence::Indistinguishable; |
4124 | |
4125 | |
4126 | |
4127 | |
4128 | if (SCS1.QualificationIncludesObjCLifetime != |
4129 | SCS2.QualificationIncludesObjCLifetime) { |
4130 | Result = SCS1.QualificationIncludesObjCLifetime |
4131 | ? ImplicitConversionSequence::Worse |
4132 | : ImplicitConversionSequence::Better; |
4133 | } |
4134 | |
4135 | while (S.Context.UnwrapSimilarTypes(T1, T2)) { |
4136 | |
4137 | |
4138 | |
4139 | |
4140 | |
4141 | |
4142 | |
4143 | |
4144 | if (T1.getQualifiers().withoutObjCLifetime() == |
4145 | T2.getQualifiers().withoutObjCLifetime()) |
4146 | |
4147 | |
4148 | |
4149 | |
4150 | ; |
4151 | else if (T2.isMoreQualifiedThan(T1)) { |
4152 | |
4153 | if (Result == ImplicitConversionSequence::Worse) |
4154 | |
4155 | |
4156 | return ImplicitConversionSequence::Indistinguishable; |
4157 | |
4158 | Result = ImplicitConversionSequence::Better; |
4159 | } else if (T1.isMoreQualifiedThan(T2)) { |
4160 | |
4161 | if (Result == ImplicitConversionSequence::Better) |
4162 | |
4163 | |
4164 | return ImplicitConversionSequence::Indistinguishable; |
4165 | |
4166 | Result = ImplicitConversionSequence::Worse; |
4167 | } else { |
4168 | |
4169 | return ImplicitConversionSequence::Indistinguishable; |
4170 | } |
4171 | |
4172 | |
4173 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
4174 | break; |
4175 | } |
4176 | |
4177 | |
4178 | |
4179 | switch (Result) { |
4180 | case ImplicitConversionSequence::Better: |
4181 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
4182 | Result = ImplicitConversionSequence::Indistinguishable; |
4183 | break; |
4184 | |
4185 | case ImplicitConversionSequence::Indistinguishable: |
4186 | break; |
4187 | |
4188 | case ImplicitConversionSequence::Worse: |
4189 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
4190 | Result = ImplicitConversionSequence::Indistinguishable; |
4191 | break; |
4192 | } |
4193 | |
4194 | return Result; |
4195 | } |
4196 | |
4197 | |
4198 | |
4199 | |
4200 | |
4201 | |
4202 | static ImplicitConversionSequence::CompareKind |
4203 | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
4204 | const StandardConversionSequence& SCS1, |
4205 | const StandardConversionSequence& SCS2) { |
4206 | QualType FromType1 = SCS1.getFromType(); |
4207 | QualType ToType1 = SCS1.getToType(1); |
4208 | QualType FromType2 = SCS2.getFromType(); |
4209 | QualType ToType2 = SCS2.getToType(1); |
4210 | |
4211 | |
4212 | |
4213 | if (SCS1.First == ICK_Array_To_Pointer) |
4214 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
4215 | if (SCS2.First == ICK_Array_To_Pointer) |
4216 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
4217 | |
4218 | |
4219 | FromType1 = S.Context.getCanonicalType(FromType1); |
4220 | ToType1 = S.Context.getCanonicalType(ToType1); |
4221 | FromType2 = S.Context.getCanonicalType(FromType2); |
4222 | ToType2 = S.Context.getCanonicalType(ToType2); |
4223 | |
4224 | |
4225 | |
4226 | |
4227 | |
4228 | |
4229 | |
4230 | if (SCS1.Second == ICK_Pointer_Conversion && |
4231 | SCS2.Second == ICK_Pointer_Conversion && |
4232 | |
4233 | FromType1->isPointerType() && FromType2->isPointerType() && |
4234 | ToType1->isPointerType() && ToType2->isPointerType()) { |
4235 | QualType FromPointee1 = |
4236 | FromType1->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4237 | QualType ToPointee1 = |
4238 | ToType1->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4239 | QualType FromPointee2 = |
4240 | FromType2->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4241 | QualType ToPointee2 = |
4242 | ToType2->castAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
4243 | |
4244 | |
4245 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
4246 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
4247 | return ImplicitConversionSequence::Better; |
4248 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
4249 | return ImplicitConversionSequence::Worse; |
4250 | } |
4251 | |
4252 | |
4253 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
4254 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4255 | return ImplicitConversionSequence::Better; |
4256 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4257 | return ImplicitConversionSequence::Worse; |
4258 | } |
4259 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
4260 | SCS2.Second == ICK_Pointer_Conversion) { |
4261 | const ObjCObjectPointerType *FromPtr1 |
4262 | = FromType1->getAs<ObjCObjectPointerType>(); |
4263 | const ObjCObjectPointerType *FromPtr2 |
4264 | = FromType2->getAs<ObjCObjectPointerType>(); |
4265 | const ObjCObjectPointerType *ToPtr1 |
4266 | = ToType1->getAs<ObjCObjectPointerType>(); |
4267 | const ObjCObjectPointerType *ToPtr2 |
4268 | = ToType2->getAs<ObjCObjectPointerType>(); |
4269 | |
4270 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
4271 | |
4272 | |
4273 | |
4274 | |
4275 | bool FromAssignLeft |
4276 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
4277 | bool FromAssignRight |
4278 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
4279 | bool ToAssignLeft |
4280 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
4281 | bool ToAssignRight |
4282 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
4283 | |
4284 | |
4285 | |
4286 | if (ToPtr1->isObjCIdType() && |
4287 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
4288 | return ImplicitConversionSequence::Worse; |
4289 | if (ToPtr2->isObjCIdType() && |
4290 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
4291 | return ImplicitConversionSequence::Better; |
4292 | |
4293 | |
4294 | |
4295 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
4296 | return ImplicitConversionSequence::Worse; |
4297 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
4298 | return ImplicitConversionSequence::Better; |
4299 | |
4300 | |
4301 | |
4302 | if (ToPtr1->isObjCClassType() && |
4303 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
4304 | return ImplicitConversionSequence::Worse; |
4305 | if (ToPtr2->isObjCClassType() && |
4306 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
4307 | return ImplicitConversionSequence::Better; |
4308 | |
4309 | |
4310 | |
4311 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
4312 | return ImplicitConversionSequence::Worse; |
4313 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
4314 | return ImplicitConversionSequence::Better; |
4315 | |
4316 | |
4317 | if (S.Context.hasSameType(FromType1, FromType2) && |
4318 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
4319 | (ToAssignLeft != ToAssignRight)) { |
4320 | if (FromPtr1->isSpecialized()) { |
4321 | |
4322 | |
4323 | bool IsFirstSame = |
4324 | FromPtr1->getInterfaceDecl() == ToPtr1->getInterfaceDecl(); |
4325 | bool IsSecondSame = |
4326 | FromPtr1->getInterfaceDecl() == ToPtr2->getInterfaceDecl(); |
4327 | if (IsFirstSame) { |
4328 | if (!IsSecondSame) |
4329 | return ImplicitConversionSequence::Better; |
4330 | } else if (IsSecondSame) |
4331 | return ImplicitConversionSequence::Worse; |
4332 | } |
4333 | return ToAssignLeft? ImplicitConversionSequence::Worse |
4334 | : ImplicitConversionSequence::Better; |
4335 | } |
4336 | |
4337 | |
4338 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
4339 | (FromAssignLeft != FromAssignRight)) |
4340 | return FromAssignLeft? ImplicitConversionSequence::Better |
4341 | : ImplicitConversionSequence::Worse; |
4342 | } |
4343 | } |
4344 | |
4345 | |
4346 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
4347 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
4348 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
4349 | const MemberPointerType * FromMemPointer1 = |
4350 | FromType1->getAs<MemberPointerType>(); |
4351 | const MemberPointerType * ToMemPointer1 = |
4352 | ToType1->getAs<MemberPointerType>(); |
4353 | const MemberPointerType * FromMemPointer2 = |
4354 | FromType2->getAs<MemberPointerType>(); |
4355 | const MemberPointerType * ToMemPointer2 = |
4356 | ToType2->getAs<MemberPointerType>(); |
4357 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
4358 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
4359 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
4360 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
4361 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
4362 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
4363 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
4364 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
4365 | |
4366 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
4367 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
4368 | return ImplicitConversionSequence::Worse; |
4369 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
4370 | return ImplicitConversionSequence::Better; |
4371 | } |
4372 | |
4373 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
4374 | if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
4375 | return ImplicitConversionSequence::Better; |
4376 | else if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
4377 | return ImplicitConversionSequence::Worse; |
4378 | } |
4379 | } |
4380 | |
4381 | if (SCS1.Second == ICK_Derived_To_Base) { |
4382 | |
4383 | |
4384 | |
4385 | |
4386 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
4387 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
4388 | if (S.IsDerivedFrom(Loc, ToType1, ToType2)) |
4389 | return ImplicitConversionSequence::Better; |
4390 | else if (S.IsDerivedFrom(Loc, ToType2, ToType1)) |
4391 | return ImplicitConversionSequence::Worse; |
4392 | } |
4393 | |
4394 | |
4395 | |
4396 | |
4397 | |
4398 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
4399 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
4400 | if (S.IsDerivedFrom(Loc, FromType2, FromType1)) |
4401 | return ImplicitConversionSequence::Better; |
4402 | else if (S.IsDerivedFrom(Loc, FromType1, FromType2)) |
4403 | return ImplicitConversionSequence::Worse; |
4404 | } |
4405 | } |
4406 | |
4407 | return ImplicitConversionSequence::Indistinguishable; |
4408 | } |
4409 | |
4410 | |
4411 | |
4412 | static bool isTypeValid(QualType T) { |
4413 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
4414 | return !Record->isInvalidDecl(); |
4415 | |
4416 | return true; |
4417 | } |
4418 | |
4419 | static QualType withoutUnaligned(ASTContext &Ctx, QualType T) { |
4420 | if (!T.getQualifiers().hasUnaligned()) |
4421 | return T; |
4422 | |
4423 | Qualifiers Q; |
4424 | T = Ctx.getUnqualifiedArrayType(T, Q); |
4425 | Q.removeUnaligned(); |
4426 | return Ctx.getQualifiedType(T, Q); |
4427 | } |
4428 | |
4429 | |
4430 | |
4431 | |
4432 | |
4433 | |
4434 | |
4435 | Sema::ReferenceCompareResult |
4436 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
4437 | QualType OrigT1, QualType OrigT2, |
4438 | ReferenceConversions *ConvOut) { |
4439 | assert(!OrigT1->isReferenceType() && |
4440 | "T1 must be the pointee type of the reference type"); |
4441 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
4442 | |
4443 | QualType T1 = Context.getCanonicalType(OrigT1); |
4444 | QualType T2 = Context.getCanonicalType(OrigT2); |
4445 | Qualifiers T1Quals, T2Quals; |
4446 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
4447 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
4448 | |
4449 | ReferenceConversions ConvTmp; |
4450 | ReferenceConversions &Conv = ConvOut ? *ConvOut : ConvTmp; |
4451 | Conv = ReferenceConversions(); |
4452 | |
4453 | |
4454 | |
4455 | |
4456 | |
4457 | |
4458 | |
4459 | |
4460 | |
4461 | |
4462 | |
4463 | |
4464 | QualType ConvertedT2; |
4465 | if (UnqualT1 == UnqualT2) { |
4466 | |
4467 | } else if (isCompleteType(Loc, OrigT2) && |
4468 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
4469 | IsDerivedFrom(Loc, UnqualT2, UnqualT1)) |
4470 | Conv |= ReferenceConversions::DerivedToBase; |
4471 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
4472 | UnqualT2->isObjCObjectOrInterfaceType() && |
4473 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
4474 | Conv |= ReferenceConversions::ObjC; |
4475 | else if (UnqualT2->isFunctionType() && |
4476 | IsFunctionConversion(UnqualT2, UnqualT1, ConvertedT2)) { |
4477 | Conv |= ReferenceConversions::Function; |
4478 | |
4479 | return Ref_Compatible; |
4480 | } |
4481 | bool ConvertedReferent = Conv != 0; |
4482 | |
4483 | |
4484 | |
4485 | bool PreviousToQualsIncludeConst = true; |
4486 | bool TopLevel = true; |
4487 | do { |
4488 | if (T1 == T2) |
4489 | break; |
4490 | |
4491 | |
4492 | Conv |= ReferenceConversions::Qualification; |
4493 | |
4494 | |
4495 | |
4496 | |
4497 | if (!TopLevel) |
4498 | Conv |= ReferenceConversions::NestedQualification; |
4499 | |
4500 | |
4501 | T1 = withoutUnaligned(Context, T1); |
4502 | T2 = withoutUnaligned(Context, T2); |
4503 | |
4504 | |
4505 | |
4506 | bool ObjCLifetimeConversion = false; |
4507 | if (!isQualificationConversionStep(T2, T1, false, |
4508 | PreviousToQualsIncludeConst, |
4509 | ObjCLifetimeConversion)) |
4510 | return (ConvertedReferent || Context.hasSimilarType(T1, T2)) |
4511 | ? Ref_Related |
4512 | : Ref_Incompatible; |
4513 | |
4514 | |
4515 | if (ObjCLifetimeConversion) |
4516 | Conv |= ReferenceConversions::ObjCLifetime; |
4517 | |
4518 | TopLevel = false; |
4519 | } while (Context.UnwrapSimilarTypes(T1, T2)); |
4520 | |
4521 | |
4522 | |
4523 | |
4524 | return (ConvertedReferent || Context.hasSameUnqualifiedType(T1, T2)) |
4525 | ? Ref_Compatible |
4526 | : Ref_Incompatible; |
4527 | } |
4528 | |
4529 | |
4530 | |
4531 | static bool |
4532 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
4533 | QualType DeclType, SourceLocation DeclLoc, |
4534 | Expr *Init, QualType T2, bool AllowRvalues, |
4535 | bool AllowExplicit) { |
4536 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
4537 | CXXRecordDecl *T2RecordDecl |
4538 | = dyn_cast<CXXRecordDecl>(T2->castAs<RecordType>()->getDecl()); |
4539 | |
4540 | OverloadCandidateSet CandidateSet( |
4541 | DeclLoc, OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
4542 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
4543 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
4544 | NamedDecl *D = *I; |
4545 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
4546 | if (isa<UsingShadowDecl>(D)) |
4547 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
4548 | |
4549 | FunctionTemplateDecl *ConvTemplate |
4550 | = dyn_cast<FunctionTemplateDecl>(D); |
4551 | CXXConversionDecl *Conv; |
4552 | if (ConvTemplate) |
4553 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
4554 | else |
4555 | Conv = cast<CXXConversionDecl>(D); |
4556 | |
4557 | if (AllowRvalues) { |
4558 | |
4559 | |
4560 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
4561 | const ReferenceType *RefType |
4562 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
4563 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
4564 | continue; |
4565 | } |
4566 | |
4567 | if (!ConvTemplate && |
4568 | S.CompareReferenceRelationship( |
4569 | DeclLoc, |
4570 | Conv->getConversionType() |
4571 | .getNonReferenceType() |
4572 | .getUnqualifiedType(), |
4573 | DeclType.getNonReferenceType().getUnqualifiedType()) == |
4574 | Sema::Ref_Incompatible) |
4575 | continue; |
4576 | } else { |
4577 | |
4578 | |
4579 | |
4580 | |
4581 | const ReferenceType *RefType = |
4582 | Conv->getConversionType()->getAs<ReferenceType>(); |
4583 | if (!RefType || |
4584 | (!RefType->isLValueReferenceType() && |
4585 | !RefType->getPointeeType()->isFunctionType())) |
4586 | continue; |
4587 | } |
4588 | |
4589 | if (ConvTemplate) |
4590 | S.AddTemplateConversionCandidate( |
4591 | ConvTemplate, I.getPair(), ActingDC, Init, DeclType, CandidateSet, |
4592 | false, AllowExplicit); |
4593 | else |
4594 | S.AddConversionCandidate( |
4595 | Conv, I.getPair(), ActingDC, Init, DeclType, CandidateSet, |
4596 | false, AllowExplicit); |
4597 | } |
4598 | |
4599 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
4600 | |
4601 | OverloadCandidateSet::iterator Best; |
4602 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
4603 | case OR_Success: |
4604 | |
4605 | |
4606 | |
4607 | |
4608 | |
4609 | |
4610 | |
4611 | |
4612 | |
4613 | |
4614 | if (!Best->FinalConversion.DirectBinding) |
4615 | return false; |
4616 | |
4617 | ICS.setUserDefined(); |
4618 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
4619 | ICS.UserDefined.After = Best->FinalConversion; |
4620 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
4621 | ICS.UserDefined.ConversionFunction = Best->Function; |
4622 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
4623 | ICS.UserDefined.EllipsisConversion = false; |
4624 | assert(ICS.UserDefined.After.ReferenceBinding && |
4625 | ICS.UserDefined.After.DirectBinding && |
4626 | "Expected a direct reference binding!"); |
4627 | return true; |
4628 | |
4629 | case OR_Ambiguous: |
4630 | ICS.setAmbiguous(); |
4631 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
4632 | Cand != CandidateSet.end(); ++Cand) |
4633 | if (Cand->Best) |
4634 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
4635 | return true; |
4636 | |
4637 | case OR_No_Viable_Function: |
4638 | case OR_Deleted: |
4639 | |
4640 | |
4641 | return false; |
4642 | } |
4643 | |
4644 | llvm_unreachable("Invalid OverloadResult!"); |
4645 | } |
4646 | |
4647 | |
4648 | |
4649 | static ImplicitConversionSequence |
4650 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
4651 | SourceLocation DeclLoc, |
4652 | bool SuppressUserConversions, |
4653 | bool AllowExplicit) { |
4654 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
4655 | |
4656 | |
4657 | ImplicitConversionSequence ICS; |
4658 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4659 | |
4660 | QualType T1 = DeclType->castAs<ReferenceType>()->getPointeeType(); |
4661 | QualType T2 = Init->getType(); |
4662 | |
4663 | |
4664 | |
4665 | |
4666 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
4667 | DeclAccessPair Found; |
4668 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
4669 | false, Found)) |
4670 | T2 = Fn->getType(); |
4671 | } |
4672 | |
4673 | |
4674 | bool isRValRef = DeclType->isRValueReferenceType(); |
4675 | Expr::Classification InitCategory = Init->Classify(S.Context); |
4676 | |
4677 | Sema::ReferenceConversions RefConv; |
4678 | Sema::ReferenceCompareResult RefRelationship = |
4679 | S.CompareReferenceRelationship(DeclLoc, T1, T2, &RefConv); |
4680 | |
4681 | auto SetAsReferenceBinding = [&](bool BindsDirectly) { |
4682 | ICS.setStandard(); |
4683 | ICS.Standard.First = ICK_Identity; |
4684 | |
4685 | |
4686 | ICS.Standard.Second = (RefConv & Sema::ReferenceConversions::DerivedToBase) |
4687 | ? ICK_Derived_To_Base |
4688 | : (RefConv & Sema::ReferenceConversions::ObjC) |
4689 | ? ICK_Compatible_Conversion |
4690 | : ICK_Identity; |
4691 | |
4692 | |
4693 | |
4694 | ICS.Standard.Third = (RefConv & |
4695 | Sema::ReferenceConversions::NestedQualification) |
4696 | ? ICK_Qualification |
4697 | : ICK_Identity; |
4698 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
4699 | ICS.Standard.setToType(0, T2); |
4700 | ICS.Standard.setToType(1, T1); |
4701 | ICS.Standard.setToType(2, T1); |
4702 | ICS.Standard.ReferenceBinding = true; |
4703 | ICS.Standard.DirectBinding = BindsDirectly; |
4704 | ICS.Standard.IsLvalueReference = !isRValRef; |
4705 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
4706 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
4707 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4708 | ICS.Standard.ObjCLifetimeConversionBinding = |
4709 | (RefConv & Sema::ReferenceConversions::ObjCLifetime) != 0; |
4710 | ICS.Standard.CopyConstructor = nullptr; |
4711 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
4712 | }; |
4713 | |
4714 | |
4715 | |
4716 | |
4717 | |
4718 | |
4719 | if (!isRValRef) { |
4720 | |
4721 | |
4722 | |
4723 | |
4724 | if (InitCategory.isLValue() && RefRelationship == Sema::Ref_Compatible) { |
4725 | |
4726 | |
4727 | |
4728 | |
4729 | |
4730 | |
4731 | |
4732 | SetAsReferenceBinding(true); |
4733 | |
4734 | |
4735 | |
4736 | |
4737 | |
4738 | return ICS; |
4739 | } |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | |
4747 | |
4748 | if (!SuppressUserConversions && T2->isRecordType() && |
4749 | S.isCompleteType(DeclLoc, T2) && |
4750 | RefRelationship == Sema::Ref_Incompatible) { |
4751 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
4752 | Init, T2, false, |
4753 | AllowExplicit)) |
4754 | return ICS; |
4755 | } |
4756 | } |
4757 | |
4758 | |
4759 | |
4760 | |
4761 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
4762 | return ICS; |
4763 | |
4764 | |
4765 | |
4766 | |
4767 | |
4768 | if (RefRelationship == Sema::Ref_Compatible && |
4769 | (InitCategory.isXValue() || |
4770 | (InitCategory.isPRValue() && |
4771 | (T2->isRecordType() || T2->isArrayType())) || |
4772 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
4773 | |
4774 | |
4775 | |
4776 | |
4777 | |
4778 | SetAsReferenceBinding(S.getLangOpts().CPlusPlus11 || |
4779 | !(InitCategory.isPRValue() || T2->isRecordType())); |
4780 | return ICS; |
4781 | } |
4782 | |
4783 | |
4784 | |
4785 | |
4786 | |
4787 | |
4788 | |
4789 | |
4790 | |
4791 | |
4792 | |
4793 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
4794 | T2->isRecordType() && S.isCompleteType(DeclLoc, T2) && |
4795 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
4796 | Init, T2, true, |
4797 | AllowExplicit)) { |
4798 | |
4799 | |
4800 | |
4801 | |
4802 | if (ICS.isUserDefined() && isRValRef && |
4803 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
4804 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
4805 | |
4806 | return ICS; |
4807 | } |
4808 | |
4809 | |
4810 | if (T1->isFunctionType()) |
4811 | return ICS; |
4812 | |
4813 | |
4814 | |
4815 | |
4816 | |
4817 | |
4818 | |
4819 | |
4820 | if (RefRelationship == Sema::Ref_Related) { |
4821 | |
4822 | |
4823 | |
4824 | |
4825 | |
4826 | |
4827 | |
4828 | Qualifiers T1Quals = T1.getQualifiers(); |
4829 | Qualifiers T2Quals = T2.getQualifiers(); |
4830 | T1Quals.removeObjCGCAttr(); |
4831 | T1Quals.removeObjCLifetime(); |
4832 | T2Quals.removeObjCGCAttr(); |
4833 | T2Quals.removeObjCLifetime(); |
4834 | |
4835 | T1Quals.removeUnaligned(); |
4836 | T2Quals.removeUnaligned(); |
4837 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
4838 | return ICS; |
4839 | } |
4840 | |
4841 | |
4842 | |
4843 | |
4844 | |
4845 | |
4846 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
4847 | (T1->isRecordType() || T2->isRecordType())) |
4848 | return ICS; |
4849 | |
4850 | |
4851 | |
4852 | if (RefRelationship >= Sema::Ref_Related && |
4853 | isRValRef && Init->Classify(S.Context).isLValue()) |
4854 | return ICS; |
4855 | |
4856 | |
4857 | |
4858 | |
4859 | |
4860 | |
4861 | |
4862 | |
4863 | |
4864 | |
4865 | |
4866 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
4867 | false, |
4868 | false, |
4869 | false, |
4870 | false, |
4871 | false); |
4872 | |
4873 | |
4874 | if (ICS.isStandard()) { |
4875 | ICS.Standard.ReferenceBinding = true; |
4876 | ICS.Standard.IsLvalueReference = !isRValRef; |
4877 | ICS.Standard.BindsToFunctionLvalue = false; |
4878 | ICS.Standard.BindsToRvalue = true; |
4879 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4880 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
4881 | } else if (ICS.isUserDefined()) { |
4882 | const ReferenceType *LValRefType = |
4883 | ICS.UserDefined.ConversionFunction->getReturnType() |
4884 | ->getAs<LValueReferenceType>(); |
4885 | |
4886 | |
4887 | |
4888 | |
4889 | |
4890 | |
4891 | |
4892 | if (DeclType->isRValueReferenceType() && LValRefType) { |
4893 | |
4894 | |
4895 | |
4896 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, DeclType); |
4897 | return ICS; |
4898 | } |
4899 | |
4900 | ICS.UserDefined.After.ReferenceBinding = true; |
4901 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
4902 | ICS.UserDefined.After.BindsToFunctionLvalue = false; |
4903 | ICS.UserDefined.After.BindsToRvalue = !LValRefType; |
4904 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
4905 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
4906 | } |
4907 | |
4908 | return ICS; |
4909 | } |
4910 | |
4911 | static ImplicitConversionSequence |
4912 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
4913 | bool SuppressUserConversions, |
4914 | bool InOverloadResolution, |
4915 | bool AllowObjCWritebackConversion, |
4916 | bool AllowExplicit = false); |
4917 | |
4918 | |
4919 | |
4920 | static ImplicitConversionSequence |
4921 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
4922 | bool SuppressUserConversions, |
4923 | bool InOverloadResolution, |
4924 | bool AllowObjCWritebackConversion) { |
4925 | |
4926 | |
4927 | |
4928 | |
4929 | ImplicitConversionSequence Result; |
4930 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
4931 | |
4932 | |
4933 | |
4934 | if (!S.isCompleteType(From->getBeginLoc(), ToType)) |
4935 | return Result; |
4936 | |
4937 | |
4938 | |
4939 | |
4940 | |
4941 | |
4942 | |
4943 | |
4944 | |
4945 | |
4946 | |
4947 | if (From->getNumInits() == 1) { |
4948 | if (ToType->isRecordType()) { |
4949 | QualType InitType = From->getInit(0)->getType(); |
4950 | if (S.Context.hasSameUnqualifiedType(InitType, ToType) || |
4951 | S.IsDerivedFrom(From->getBeginLoc(), InitType, ToType)) |
4952 | return TryCopyInitialization(S, From->getInit(0), ToType, |
4953 | SuppressUserConversions, |
4954 | InOverloadResolution, |
4955 | AllowObjCWritebackConversion); |
4956 | } |
4957 | |
4958 | |
4959 | if (ToType->isArrayType()) { |
4960 | InitializedEntity Entity = |
4961 | InitializedEntity::InitializeParameter(S.Context, ToType, |
4962 | false); |
4963 | if (S.CanPerformCopyInitialization(Entity, From)) { |
4964 | Result.setStandard(); |
4965 | Result.Standard.setAsIdentityConversion(); |
4966 | Result.Standard.setFromType(ToType); |
4967 | Result.Standard.setAllToTypes(ToType); |
4968 | return Result; |
4969 | } |
4970 | } |
4971 | } |
4972 | |
4973 | |
4974 | |
4975 | |
4976 | |
4977 | |
4978 | |
4979 | |
4980 | |
4981 | |
4982 | |
4983 | |
4984 | |
4985 | |
4986 | |
4987 | |
4988 | bool toStdInitializerList = false; |
4989 | QualType X; |
4990 | if (ToType->isArrayType()) |
4991 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
4992 | else |
4993 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
4994 | if (!X.isNull()) { |
4995 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
4996 | Expr *Init = From->getInit(i); |
4997 | ImplicitConversionSequence ICS = |
4998 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
4999 | InOverloadResolution, |
5000 | AllowObjCWritebackConversion); |
5001 | |
5002 | if (ICS.isBad()) { |
5003 | Result = ICS; |
5004 | break; |
5005 | } |
5006 | |
5007 | if (Result.isBad() || CompareImplicitConversionSequences( |
5008 | S, From->getBeginLoc(), ICS, Result) == |
5009 | ImplicitConversionSequence::Worse) |
5010 | Result = ICS; |
5011 | } |
5012 | |
5013 | |
5014 | |
5015 | if (From->getNumInits() == 0) { |
5016 | Result.setStandard(); |
5017 | Result.Standard.setAsIdentityConversion(); |
5018 | Result.Standard.setFromType(ToType); |
5019 | Result.Standard.setAllToTypes(ToType); |
5020 | } |
5021 | |
5022 | Result.setStdInitializerListElement(toStdInitializerList); |
5023 | return Result; |
5024 | } |
5025 | |
5026 | |
5027 | |
5028 | |
5029 | |
5030 | |
5031 | |
5032 | |
5033 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
5034 | |
5035 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
5036 | false, |
5037 | InOverloadResolution, false, |
5038 | AllowObjCWritebackConversion, |
5039 | false); |
5040 | } |
5041 | |
5042 | |
5043 | |
5044 | |
5045 | |
5046 | |
5047 | if (ToType->isAggregateType()) { |
5048 | |
5049 | |
5050 | |
5051 | InitializedEntity Entity = |
5052 | InitializedEntity::InitializeParameter(S.Context, ToType, |
5053 | false); |
5054 | if (S.CanPerformAggregateInitializationForOverloadResolution(Entity, |
5055 | From)) { |
5056 | Result.setUserDefined(); |
5057 | Result.UserDefined.Before.setAsIdentityConversion(); |
5058 | |
5059 | Result.UserDefined.Before.setFromType(QualType()); |
5060 | Result.UserDefined.Before.setAllToTypes(QualType()); |
5061 | |
5062 | Result.UserDefined.After.setAsIdentityConversion(); |
5063 | Result.UserDefined.After.setFromType(ToType); |
5064 | Result.UserDefined.After.setAllToTypes(ToType); |
5065 | Result.UserDefined.ConversionFunction = nullptr; |
5066 | } |
5067 | return Result; |
5068 | } |
5069 | |
5070 | |
5071 | |
5072 | |
5073 | if (ToType->isReferenceType()) { |
5074 | |
5075 | |
5076 | |
5077 | |
5078 | QualType T1 = ToType->castAs<ReferenceType>()->getPointeeType(); |
5079 | |
5080 | |
5081 | |
5082 | if (From->getNumInits() == 1) { |
5083 | Expr *Init = From->getInit(0); |
5084 | |
5085 | QualType T2 = Init->getType(); |
5086 | |
5087 | |
5088 | |
5089 | |
5090 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
5091 | DeclAccessPair Found; |
5092 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
5093 | Init, ToType, false, Found)) |
5094 | T2 = Fn->getType(); |
5095 | } |
5096 | |
5097 | |
5098 | Sema::ReferenceCompareResult RefRelationship = |
5099 | S.CompareReferenceRelationship(From->getBeginLoc(), T1, T2); |
5100 | |
5101 | if (RefRelationship >= Sema::Ref_Related) { |
5102 | return TryReferenceInit(S, Init, ToType, From->getBeginLoc(), |
5103 | SuppressUserConversions, |
5104 | false); |
5105 | } |
5106 | } |
5107 | |
5108 | |
5109 | |
5110 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
5111 | InOverloadResolution, |
5112 | AllowObjCWritebackConversion); |
5113 | if (Result.isFailure()) |
5114 | return Result; |
5115 | assert(!Result.isEllipsis() && |
5116 | "Sub-initialization cannot result in ellipsis conversion."); |
5117 | |
5118 | |
5119 | if (ToType->isRValueReferenceType() || |
5120 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
5121 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
5122 | Result.UserDefined.After; |
5123 | SCS.ReferenceBinding = true; |
5124 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
5125 | SCS.BindsToRvalue = true; |
5126 | SCS.BindsToFunctionLvalue = false; |
5127 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
5128 | SCS.ObjCLifetimeConversionBinding = false; |
5129 | } else |
5130 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
5131 | From, ToType); |
5132 | return Result; |
5133 | } |
5134 | |
5135 | |
5136 | |
5137 | |
5138 | if (!ToType->isRecordType()) { |
5139 | |
5140 | |
5141 | |
5142 | unsigned NumInits = From->getNumInits(); |
5143 | if (NumInits == 1 && !isa<InitListExpr>(From->getInit(0))) |
5144 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
5145 | SuppressUserConversions, |
5146 | InOverloadResolution, |
5147 | AllowObjCWritebackConversion); |
5148 | |
5149 | |
5150 | else if (NumInits == 0) { |
5151 | Result.setStandard(); |
5152 | Result.Standard.setAsIdentityConversion(); |
5153 | Result.Standard.setFromType(ToType); |
5154 | Result.Standard.setAllToTypes(ToType); |
5155 | } |
5156 | return Result; |
5157 | } |
5158 | |
5159 | |
5160 | |
5161 | |
5162 | return Result; |
5163 | } |
5164 | |
5165 | |
5166 | |
5167 | |
5168 | |
5169 | |
5170 | |
5171 | static ImplicitConversionSequence |
5172 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
5173 | bool SuppressUserConversions, |
5174 | bool InOverloadResolution, |
5175 | bool AllowObjCWritebackConversion, |
5176 | bool AllowExplicit) { |
5177 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
5178 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
5179 | InOverloadResolution,AllowObjCWritebackConversion); |
5180 | |
5181 | if (ToType->isReferenceType()) |
5182 | return TryReferenceInit(S, From, ToType, |
5183 | From->getBeginLoc(), |
5184 | SuppressUserConversions, AllowExplicit); |
5185 | |
5186 | return TryImplicitConversion(S, From, ToType, |
5187 | SuppressUserConversions, |
5188 | false, |
5189 | InOverloadResolution, |
5190 | false, |
5191 | AllowObjCWritebackConversion, |
5192 | false); |
5193 | } |
5194 | |
5195 | static bool TryCopyInitialization(const CanQualType FromQTy, |
5196 | const CanQualType ToQTy, |
5197 | Sema &S, |
5198 | SourceLocation Loc, |
5199 | ExprValueKind FromVK) { |
5200 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
5201 | ImplicitConversionSequence ICS = |
5202 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
5203 | |
5204 | return !ICS.isBad(); |
5205 | } |
5206 | |
5207 | |
5208 | |
5209 | |
5210 | static ImplicitConversionSequence |
5211 | TryObjectArgumentInitialization(Sema &S, SourceLocation Loc, QualType FromType, |
5212 | Expr::Classification FromClassification, |
5213 | CXXMethodDecl *Method, |
5214 | CXXRecordDecl *ActingContext) { |
5215 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
5216 | |
5217 | |
5218 | Qualifiers Quals = Method->getMethodQualifiers(); |
5219 | if (isa<CXXDestructorDecl>(Method)) { |
5220 | Quals.addConst(); |
5221 | Quals.addVolatile(); |
5222 | } |
5223 | |
5224 | QualType ImplicitParamType = S.Context.getQualifiedType(ClassType, Quals); |
5225 | |
5226 | |
5227 | |
5228 | ImplicitConversionSequence ICS; |
5229 | |
5230 | |
5231 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
5232 | FromType = PT->getPointeeType(); |
5233 | |
5234 | |
5235 | |
5236 | assert(FromClassification.isLValue()); |
5237 | } |
5238 | |
5239 | assert(FromType->isRecordType()); |
5240 | |
5241 | |
5242 | |
5243 | |
5244 | |
5245 | |
5246 | |
5247 | |
5248 | |
5249 | |
5250 | |
5251 | |
5252 | |
5253 | |
5254 | |
5255 | |
5256 | |
5257 | |
5258 | |
5259 | |
5260 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
5261 | if (ImplicitParamType.getCVRQualifiers() |
5262 | != FromTypeCanon.getLocalCVRQualifiers() && |
5263 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
5264 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
5265 | FromType, ImplicitParamType); |
5266 | return ICS; |
5267 | } |
5268 | |
5269 | if (FromTypeCanon.hasAddressSpace()) { |
5270 | Qualifiers QualsImplicitParamType = ImplicitParamType.getQualifiers(); |
5271 | Qualifiers QualsFromType = FromTypeCanon.getQualifiers(); |
5272 | if (!QualsImplicitParamType.isAddressSpaceSupersetOf(QualsFromType)) { |
5273 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
5274 | FromType, ImplicitParamType); |
5275 | return ICS; |
5276 | } |
5277 | } |
5278 | |
5279 | |
5280 | |
5281 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
5282 | ImplicitConversionKind SecondKind; |
5283 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
5284 | SecondKind = ICK_Identity; |
5285 | } else if (S.IsDerivedFrom(Loc, FromType, ClassType)) |
5286 | SecondKind = ICK_Derived_To_Base; |
5287 | else { |
5288 | ICS.setBad(BadConversionSequence::unrelated_class, |
5289 | FromType, ImplicitParamType); |
5290 | return ICS; |
5291 | } |
5292 | |
5293 | |
5294 | switch (Method->getRefQualifier()) { |
5295 | case RQ_None: |
5296 | |
5297 | break; |
5298 | |
5299 | case RQ_LValue: |
5300 | if (!FromClassification.isLValue() && !Quals.hasOnlyConst()) { |
5301 | |
5302 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
5303 | ImplicitParamType); |
5304 | return ICS; |
5305 | } |
5306 | break; |
5307 | |
5308 | case RQ_RValue: |
5309 | if (!FromClassification.isRValue()) { |
5310 | |
5311 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
5312 | ImplicitParamType); |
5313 | return ICS; |
5314 | } |
5315 | break; |
5316 | } |
5317 | |
5318 | |
5319 | ICS.setStandard(); |
5320 | ICS.Standard.setAsIdentityConversion(); |
5321 | ICS.Standard.Second = SecondKind; |
5322 | ICS.Standard.setFromType(FromType); |
5323 | ICS.Standard.setAllToTypes(ImplicitParamType); |
5324 | ICS.Standard.ReferenceBinding = true; |
5325 | ICS.Standard.DirectBinding = true; |
5326 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
5327 | ICS.Standard.BindsToFunctionLvalue = false; |
5328 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
5329 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
5330 | = (Method->getRefQualifier() == RQ_None); |
5331 | return ICS; |
5332 | } |
5333 | |
5334 | |
5335 | |
5336 | |
5337 | ExprResult |
5338 | Sema::PerformObjectArgumentInitialization(Expr *From, |
5339 | NestedNameSpecifier *Qualifier, |
5340 | NamedDecl *FoundDecl, |
5341 | CXXMethodDecl *Method) { |
5342 | QualType FromRecordType, DestType; |
5343 | QualType ImplicitParamRecordType = |
5344 | Method->getThisType()->castAs<PointerType>()->getPointeeType(); |
5345 | |
5346 | Expr::Classification FromClassification; |
5347 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
5348 | FromRecordType = PT->getPointeeType(); |
5349 | DestType = Method->getThisType(); |
5350 | FromClassification = Expr::Classification::makeSimpleLValue(); |
5351 | } else { |
5352 | FromRecordType = From->getType(); |
5353 | DestType = ImplicitParamRecordType; |
5354 | FromClassification = From->Classify(Context); |
5355 | |
5356 | |
5357 | if (From->isRValue()) { |
5358 | From = CreateMaterializeTemporaryExpr(FromRecordType, From, |
5359 | Method->getRefQualifier() != |
5360 | RefQualifierKind::RQ_RValue); |
5361 | } |
5362 | } |
5363 | |
5364 | |
5365 | |
5366 | ImplicitConversionSequence ICS = TryObjectArgumentInitialization( |
5367 | *this, From->getBeginLoc(), From->getType(), FromClassification, Method, |
5368 | Method->getParent()); |
5369 | if (ICS.isBad()) { |
5370 | switch (ICS.Bad.Kind) { |
5371 | case BadConversionSequence::bad_qualifiers: { |
5372 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
5373 | Qualifiers ToQs = DestType.getQualifiers(); |
5374 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
5375 | if (CVR) { |
5376 | Diag(From->getBeginLoc(), diag::err_member_function_call_bad_cvr) |
5377 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
5378 | << From->getSourceRange(); |
5379 | Diag(Method->getLocation(), diag::note_previous_decl) |
5380 | << Method->getDeclName(); |
5381 | return ExprError(); |
5382 | } |
5383 | break; |
5384 | } |
5385 | |
5386 | case BadConversionSequence::lvalue_ref_to_rvalue: |
5387 | case BadConversionSequence::rvalue_ref_to_lvalue: { |
5388 | bool IsRValueQualified = |
5389 | Method->getRefQualifier() == RefQualifierKind::RQ_RValue; |
5390 | Diag(From->getBeginLoc(), diag::err_member_function_call_bad_ref) |
5391 | << Method->getDeclName() << FromClassification.isRValue() |
5392 | << IsRValueQualified; |
5393 | Diag(Method->getLocation(), diag::note_previous_decl) |
5394 | << Method->getDeclName(); |
5395 | return ExprError(); |
5396 | } |
5397 | |
5398 | case BadConversionSequence::no_conversion: |
5399 | case BadConversionSequence::unrelated_class: |
5400 | break; |
5401 | } |
5402 | |
5403 | return Diag(From->getBeginLoc(), diag::err_member_function_call_bad_type) |
5404 | << ImplicitParamRecordType << FromRecordType |
5405 | << From->getSourceRange(); |
5406 | } |
5407 | |
5408 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
5409 | ExprResult FromRes = |
5410 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
5411 | if (FromRes.isInvalid()) |
5412 | return ExprError(); |
5413 | From = FromRes.get(); |
5414 | } |
5415 | |
5416 | if (!Context.hasSameType(From->getType(), DestType)) { |
5417 | CastKind CK; |
5418 | QualType PteeTy = DestType->getPointeeType(); |
5419 | LangAS DestAS = |
5420 | PteeTy.isNull() ? DestType.getAddressSpace() : PteeTy.getAddressSpace(); |
5421 | if (FromRecordType.getAddressSpace() != DestAS) |
5422 | CK = CK_AddressSpaceConversion; |
5423 | else |
5424 | CK = CK_NoOp; |
5425 | From = ImpCastExprToType(From, DestType, CK, From->getValueKind()).get(); |
5426 | } |
5427 | return From; |
5428 | } |
5429 | |
5430 | |
5431 | |
5432 | static ImplicitConversionSequence |
5433 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
5434 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
5435 | false, |
5436 | true, |
5437 | false, |
5438 | false, |
5439 | false, |
5440 | false); |
5441 | } |
5442 | |
5443 | |
5444 | |
5445 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
5446 | if (checkPlaceholderForOverload(*this, From)) |
| |
5447 | return ExprError(); |
5448 | |
5449 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
5450 | if (!ICS.isBad()) |
| |
5451 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
5452 | |
5453 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
| 3 | | Calling 'Sema::DiagnoseMultipleUserDefinedConversion' | |
|
5454 | return Diag(From->getBeginLoc(), diag::err_typecheck_bool_condition) |
5455 | << From->getType() << From->getSourceRange(); |
5456 | return ExprError(); |
5457 | } |
5458 | |
5459 | |
5460 | |
5461 | |
5462 | static bool CheckConvertedConstantConversions(Sema &S, |
5463 | StandardConversionSequence &SCS) { |
5464 | |
5465 | |
5466 | |
5467 | switch (SCS.Second) { |
5468 | case ICK_Identity: |
5469 | case ICK_Function_Conversion: |
5470 | case ICK_Integral_Promotion: |
5471 | case ICK_Integral_Conversion: |
5472 | case ICK_Zero_Queue_Conversion: |
5473 | return true; |
5474 | |
5475 | case ICK_Boolean_Conversion: |
5476 | |
5477 | |
5478 | |
5479 | |
5480 | |
5481 | |
5482 | |
5483 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
5484 | SCS.getToType(2)->isBooleanType(); |
5485 | |
5486 | case ICK_Pointer_Conversion: |
5487 | case ICK_Pointer_Member: |
5488 | |
5489 | |
5490 | return SCS.getFromType()->isNullPtrType(); |
5491 | |
5492 | case ICK_Floating_Promotion: |
5493 | case ICK_Complex_Promotion: |
5494 | case ICK_Floating_Conversion: |
5495 | case ICK_Complex_Conversion: |
5496 | case ICK_Floating_Integral: |
5497 | case ICK_Compatible_Conversion: |
5498 | case ICK_Derived_To_Base: |
5499 | case ICK_Vector_Conversion: |
5500 | case ICK_Vector_Splat: |
5501 | case ICK_Complex_Real: |
5502 | case ICK_Block_Pointer_Conversion: |
5503 | case ICK_TransparentUnionConversion: |
5504 | case ICK_Writeback_Conversion: |
5505 | case ICK_Zero_Event_Conversion: |
5506 | case ICK_C_Only_Conversion: |
5507 | case ICK_Incompatible_Pointer_Conversion: |
5508 | return false; |
5509 | |
5510 | case ICK_Lvalue_To_Rvalue: |
5511 | case ICK_Array_To_Pointer: |
5512 | case ICK_Function_To_Pointer: |
5513 | llvm_unreachable("found a first conversion kind in Second"); |
5514 | |
5515 | case ICK_Qualification: |
5516 | llvm_unreachable("found a third conversion kind in Second"); |
5517 | |
5518 | case ICK_Num_Conversion_Kinds: |
5519 | break; |
5520 | } |
5521 | |
5522 | llvm_unreachable("unknown conversion kind"); |
5523 | } |
5524 | |
5525 | |
5526 | |
5527 | |
5528 | static ExprResult CheckConvertedConstantExpression(Sema &S, Expr *From, |
5529 | QualType T, APValue &Value, |
5530 | Sema::CCEKind CCE, |
5531 | bool RequireInt) { |
5532 | assert(S.getLangOpts().CPlusPlus11 && |
5533 | "converted constant expression outside C++11"); |
5534 | |
5535 | if (checkPlaceholderForOverload(S, From)) |
5536 | return ExprError(); |
5537 | |
5538 | |
5539 | |
5540 | |
5541 | |
5542 | |
5543 | |
5544 | |
5545 | |
5546 | |
5547 | ImplicitConversionSequence ICS = |
5548 | CCE == Sema::CCEK_ConstexprIf || CCE == Sema::CCEK_ExplicitBool |
5549 | ? TryContextuallyConvertToBool(S, From) |
5550 | : TryCopyInitialization(S, From, T, |
5551 | false, |
5552 | false, |
5553 | false, |
5554 | false); |
5555 | StandardConversionSequence *SCS = nullptr; |
5556 | switch (ICS.getKind()) { |
5557 | case ImplicitConversionSequence::StandardConversion: |
5558 | SCS = &ICS.Standard; |
5559 | break; |
5560 | case ImplicitConversionSequence::UserDefinedConversion: |
5561 | |
5562 | |
5563 | SCS = &ICS.UserDefined.After; |
5564 | break; |
5565 | case ImplicitConversionSequence::AmbiguousConversion: |
5566 | case ImplicitConversionSequence::BadConversion: |
5567 | if (!S.DiagnoseMultipleUserDefinedConversion(From, T)) |
5568 | return S.Diag(From->getBeginLoc(), |
5569 | diag::err_typecheck_converted_constant_expression) |
5570 | << From->getType() << From->getSourceRange() << T; |
5571 | return ExprError(); |
5572 | |
5573 | case ImplicitConversionSequence::EllipsisConversion: |
5574 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
5575 | } |
5576 | |
5577 | |
5578 | if (!CheckConvertedConstantConversions(S, *SCS)) { |
5579 | return S.Diag(From->getBeginLoc(), |
5580 | diag::err_typecheck_converted_constant_expression_disallowed) |
5581 | << From->getType() << From->getSourceRange() << T; |
5582 | } |
5583 | |
5584 | if (SCS->ReferenceBinding && !SCS->DirectBinding) { |
5585 | return S.Diag(From->getBeginLoc(), |
5586 | diag::err_typecheck_converted_constant_expression_indirect) |
5587 | << From->getType() << From->getSourceRange() << T; |
5588 | } |
5589 | |
5590 | ExprResult Result = |
5591 | S.PerformImplicitConversion(From, T, ICS, Sema::AA_Converting); |
5592 | if (Result.isInvalid()) |
5593 | return Result; |
5594 | |
5595 | |
5596 | |
5597 | Result = |
5598 | S.ActOnFinishFullExpr(Result.get(), From->getExprLoc(), |
5599 | false, true); |
5600 | if (Result.isInvalid()) |
5601 | return Result; |
5602 | |
5603 | |
5604 | APValue PreNarrowingValue; |
5605 | QualType PreNarrowingType; |
5606 | switch (SCS->getNarrowingKind(S.Context, Result.get(), PreNarrowingValue, |
5607 | PreNarrowingType)) { |
5608 | case NK_Dependent_Narrowing: |
5609 | |
5610 | |
5611 | case NK_Variable_Narrowing: |
5612 | |
5613 | |
5614 | case NK_Not_Narrowing: |
5615 | break; |
5616 | |
5617 | case NK_Constant_Narrowing: |
5618 | S.Diag(From->getBeginLoc(), diag::ext_cce_narrowing) |
5619 | << CCE << 1 |
5620 | << PreNarrowingValue.getAsString(S.Context, PreNarrowingType) << T; |
5621 | break; |
5622 | |
5623 | case NK_Type_Narrowing: |
5624 | S.Diag(From->getBeginLoc(), diag::ext_cce_narrowing) |
5625 | << CCE << 0 << From->getType() << T; |
5626 | break; |
5627 | } |
5628 | |
5629 | if (Result.get()->isValueDependent()) { |
5630 | Value = APValue(); |
5631 | return Result; |
5632 | } |
5633 | |
5634 | |
5635 | SmallVector<PartialDiagnosticAt, 8> Notes; |
5636 | Expr::EvalResult Eval; |
5637 | Eval.Diag = &Notes; |
5638 | Expr::ConstExprUsage Usage = CCE == Sema::CCEK_TemplateArg |
5639 | ? Expr::EvaluateForMangling |
5640 | : Expr::EvaluateForCodeGen; |
5641 | |
5642 | if (!Result.get()->EvaluateAsConstantExpr(Eval, Usage, S.Context) || |
5643 | (RequireInt && !Eval.Val.isInt())) { |
5644 | |
5645 | |
5646 | Result = ExprError(); |
5647 | } else { |
5648 | Value = Eval.Val; |
5649 | |
5650 | if (Notes.empty()) { |
5651 | |
5652 | return ConstantExpr::Create(S.Context, Result.get(), Value); |
5653 | } |
5654 | } |
5655 | |
5656 | |
5657 | if (Notes.size() == 1 && |
5658 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
5659 | S.Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
5660 | else { |
5661 | S.Diag(From->getBeginLoc(), diag::err_expr_not_cce) |
5662 | << CCE << From->getSourceRange(); |
5663 | for (unsigned I = 0; I < Notes.size(); ++I) |
5664 | S.Diag(Notes[I].first, Notes[I].second); |
5665 | } |
5666 | return ExprError(); |
5667 | } |
5668 | |
5669 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
5670 | APValue &Value, CCEKind CCE) { |
5671 | return ::CheckConvertedConstantExpression(*this, From, T, Value, CCE, false); |
5672 | } |
5673 | |
5674 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
5675 | llvm::APSInt &Value, |
5676 | CCEKind CCE) { |
5677 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
5678 | |
5679 | APValue V; |
5680 | auto R = ::CheckConvertedConstantExpression(*this, From, T, V, CCE, true); |
5681 | if (!R.isInvalid() && !R.get()->isValueDependent()) |
5682 | Value = V.getInt(); |
5683 | return R; |
5684 | } |
5685 | |
5686 | |
5687 | |
5688 | |
5689 | |
5690 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
5691 | if (SCS.Second == ICK_Pointer_Conversion) { |
5692 | SCS.Second = ICK_Identity; |
5693 | SCS.Third = ICK_Identity; |
5694 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
5695 | } |
5696 | } |
5697 | |
5698 | |
5699 | |
5700 | static ImplicitConversionSequence |
5701 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
5702 | |
5703 | QualType Ty = S.Context.getObjCIdType(); |
5704 | ImplicitConversionSequence ICS |
5705 | = TryImplicitConversion(S, From, Ty, |
5706 | |
5707 | false, |
5708 | true, |
5709 | false, |
5710 | false, |
5711 | false, |
5712 | true); |
5713 | |
5714 | |
5715 | switch (ICS.getKind()) { |
5716 | case ImplicitConversionSequence::BadConversion: |
5717 | case ImplicitConversionSequence::AmbiguousConversion: |
5718 | case ImplicitConversionSequence::EllipsisConversion: |
5719 | break; |
5720 | |
5721 | case ImplicitConversionSequence::UserDefinedConversion: |
5722 | dropPointerConversion(ICS.UserDefined.After); |
5723 | break; |
5724 | |
5725 | case ImplicitConversionSequence::StandardConversion: |
5726 | dropPointerConversion(ICS.Standard); |
5727 | break; |
5728 | } |
5729 | |
5730 | return ICS; |
5731 | } |
5732 | |
5733 | |
5734 | |
5735 | |
5736 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
5737 | if (checkPlaceholderForOverload(*this, From)) |
5738 | return ExprError(); |
5739 | |
5740 | QualType Ty = Context.getObjCIdType(); |
5741 | ImplicitConversionSequence ICS = |
5742 | TryContextuallyConvertToObjCPointer(*this, From); |
5743 | if (!ICS.isBad()) |
5744 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
5745 | return ExprResult(); |
5746 | } |
5747 | |
5748 | |
5749 | |
5750 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
5751 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
5752 | : T->isIntegralOrUnscopedEnumerationType(); |
5753 | } |
5754 | |
5755 | static ExprResult |
5756 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
5757 | Sema::ContextualImplicitConverter &Converter, |
5758 | QualType T, UnresolvedSetImpl &ViableConversions) { |
5759 | |
5760 | if (Converter.Suppress) |
5761 | return ExprError(); |
5762 | |
5763 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
5764 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
5765 | CXXConversionDecl *Conv = |
5766 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
5767 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
5768 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
5769 | } |
5770 | return From; |
5771 | } |
5772 | |
5773 | static bool |
5774 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
5775 | Sema::ContextualImplicitConverter &Converter, |
5776 | QualType T, bool HadMultipleCandidates, |
5777 | UnresolvedSetImpl &ExplicitConversions) { |
5778 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
5779 | DeclAccessPair Found = ExplicitConversions[0]; |
5780 | CXXConversionDecl *Conversion = |
5781 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
5782 | |
5783 | |
5784 | |
5785 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
5786 | std::string TypeStr; |
5787 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
5788 | |
5789 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
5790 | << FixItHint::CreateInsertion(From->getBeginLoc(), |
5791 | "static_cast<" + TypeStr + ">(") |
5792 | << FixItHint::CreateInsertion( |
5793 | SemaRef.getLocForEndOfToken(From->getEndLoc()), ")"); |
5794 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
5795 | |
5796 | |
5797 | |
5798 | if (SemaRef.isSFINAEContext()) |
5799 | return true; |
5800 | |
5801 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, nullptr, Found); |
5802 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
5803 | HadMultipleCandidates); |
5804 | if (Result.isInvalid()) |
5805 | return true; |
5806 | |
5807 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
5808 | CK_UserDefinedConversion, Result.get(), |
5809 | nullptr, Result.get()->getValueKind()); |
5810 | } |
5811 | return false; |
5812 | } |
5813 | |
5814 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
5815 | Sema::ContextualImplicitConverter &Converter, |
5816 | QualType T, bool HadMultipleCandidates, |
5817 | DeclAccessPair &Found) { |
5818 | CXXConversionDecl *Conversion = |
5819 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
5820 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, nullptr, Found); |
5821 | |
5822 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
5823 | if (!Converter.SuppressConversion) { |
5824 | if (SemaRef.isSFINAEContext()) |
5825 | return true; |
5826 | |
5827 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
5828 | << From->getSourceRange(); |
5829 | } |
5830 | |
5831 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
5832 | HadMultipleCandidates); |
5833 | if (Result.isInvalid()) |
5834 | return true; |
5835 | |
5836 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
5837 | CK_UserDefinedConversion, Result.get(), |
5838 | nullptr, Result.get()->getValueKind()); |
5839 | return false; |
5840 | } |
5841 | |
5842 | static ExprResult finishContextualImplicitConversion( |
5843 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
5844 | Sema::ContextualImplicitConverter &Converter) { |
5845 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
5846 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
5847 | << From->getSourceRange(); |
5848 | |
5849 | return SemaRef.DefaultLvalueConversion(From); |
5850 | } |
5851 | |
5852 | static void |
5853 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
5854 | UnresolvedSetImpl &ViableConversions, |
5855 | OverloadCandidateSet &CandidateSet) { |
5856 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
5857 | DeclAccessPair FoundDecl = ViableConversions[I]; |
5858 | NamedDecl *D = FoundDecl.getDecl(); |
5859 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
5860 | if (isa<UsingShadowDecl>(D)) |
5861 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
5862 | |
5863 | CXXConversionDecl *Conv; |
5864 | FunctionTemplateDecl *ConvTemplate; |
5865 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
5866 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
5867 | else |
5868 | Conv = cast<CXXConversionDecl>(D); |
5869 | |
5870 | if (ConvTemplate) |
5871 | SemaRef.AddTemplateConversionCandidate( |
5872 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet, |
5873 | false, true); |
5874 | else |
5875 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
5876 | ToType, CandidateSet, |
5877 | false, |
5878 | true); |
5879 | } |
5880 | } |
5881 | |
5882 | |
5883 | |
5884 | |
5885 | |
5886 | |
5887 | |
5888 | |
5889 | |
5890 | |
5891 | |
5892 | |
5893 | |
5894 | |
5895 | |
5896 | |
5897 | |
5898 | |
5899 | |
5900 | ExprResult Sema::PerformContextualImplicitConversion( |
5901 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
5902 | |
5903 | if (From->isTypeDependent()) |
5904 | return From; |
5905 | |
5906 | |
5907 | if (From->hasPlaceholderType()) { |
5908 | ExprResult result = CheckPlaceholderExpr(From); |
5909 | if (result.isInvalid()) |
5910 | return result; |
5911 | From = result.get(); |
5912 | } |
5913 | |
5914 | |
5915 | QualType T = From->getType(); |
5916 | if (Converter.match(T)) |
5917 | return DefaultLvalueConversion(From); |
5918 | |
5919 | |
5920 | |
5921 | |
5922 | |
5923 | const RecordType *RecordTy = T->getAs<RecordType>(); |
5924 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
5925 | if (!Converter.Suppress) |
5926 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
5927 | return From; |
5928 | } |
5929 | |
5930 | |
5931 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
5932 | ContextualImplicitConverter &Converter; |
5933 | Expr *From; |
5934 | |
5935 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
5936 | : Converter(Converter), From(From) {} |
5937 | |
5938 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
5939 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
5940 | } |
5941 | } IncompleteDiagnoser(Converter, From); |
5942 | |
5943 | if (Converter.Suppress ? !isCompleteType(Loc, T) |
5944 | : RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
5945 | return From; |
5946 | |
5947 | |
5948 | UnresolvedSet<4> |
5949 | ViableConversions; |
5950 | UnresolvedSet<4> ExplicitConversions; |
5951 | const auto &Conversions = |
5952 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
5953 | |
5954 | bool HadMultipleCandidates = |
5955 | (std::distance(Conversions.begin(), Conversions.end()) > 1); |
5956 | |
5957 | |
5958 | QualType ToType; |
5959 | bool HasUniqueTargetType = true; |
5960 | |
5961 | |
5962 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
5963 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
5964 | CXXConversionDecl *Conversion; |
5965 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
5966 | if (ConvTemplate) { |
5967 | if (getLangOpts().CPlusPlus14) |
5968 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
5969 | else |
5970 | continue; |
5971 | } else |
5972 | Conversion = cast<CXXConversionDecl>(D); |
5973 | |
5974 | assert((!ConvTemplate || getLangOpts().CPlusPlus14) && |
5975 | "Conversion operator templates are considered potentially " |
5976 | "viable in C++1y"); |
5977 | |
5978 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
5979 | if (Converter.match(CurToType) || ConvTemplate) { |
5980 | |
5981 | if (Conversion->isExplicit()) { |
5982 | |
5983 | |
5984 | if (!ConvTemplate) |
5985 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
5986 | } else { |
5987 | if (!ConvTemplate && getLangOpts().CPlusPlus14) { |
5988 | if (ToType.isNull()) |
5989 | ToType = CurToType.getUnqualifiedType(); |
5990 | else if (HasUniqueTargetType && |
5991 | (CurToType.getUnqualifiedType() != ToType)) |
5992 | HasUniqueTargetType = false; |
5993 | } |
5994 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
5995 | } |
5996 | } |
5997 | } |
5998 | |
5999 | if (getLangOpts().CPlusPlus14) { |
6000 | |
6001 | |
6002 | |
6003 | |
6004 | |
6005 | |
6006 | |
6007 | |
6008 | |
6009 | |
6010 | if (ToType.isNull()) { |
6011 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
6012 | HadMultipleCandidates, |
6013 | ExplicitConversions)) |
6014 | return ExprError(); |
6015 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
6016 | } |
6017 | |
6018 | |
6019 | if (!HasUniqueTargetType) |
6020 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
6021 | ViableConversions); |
6022 | |
6023 | |
6024 | |
6025 | |
6026 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
6027 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
6028 | CandidateSet); |
6029 | |
6030 | |
6031 | OverloadCandidateSet::iterator Best; |
6032 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
6033 | case OR_Success: { |
6034 | |
6035 | DeclAccessPair Found = |
6036 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
6037 | if (recordConversion(*this, Loc, From, Converter, T, |
6038 | HadMultipleCandidates, Found)) |
6039 | return ExprError(); |
6040 | break; |
6041 | } |
6042 | case OR_Ambiguous: |
6043 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
6044 | ViableConversions); |
6045 | case OR_No_Viable_Function: |
6046 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
6047 | HadMultipleCandidates, |
6048 | ExplicitConversions)) |
6049 | return ExprError(); |
6050 | LLVM_FALLTHROUGH; |
6051 | case OR_Deleted: |
6052 | |
6053 | break; |
6054 | } |
6055 | } else { |
6056 | switch (ViableConversions.size()) { |
6057 | case 0: { |
6058 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
6059 | HadMultipleCandidates, |
6060 | ExplicitConversions)) |
6061 | return ExprError(); |
6062 | |
6063 | |
6064 | break; |
6065 | } |
6066 | case 1: { |
6067 | |
6068 | DeclAccessPair Found = ViableConversions[0]; |
6069 | if (recordConversion(*this, Loc, From, Converter, T, |
6070 | HadMultipleCandidates, Found)) |
6071 | return ExprError(); |
6072 | break; |
6073 | } |
6074 | default: |
6075 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
6076 | ViableConversions); |
6077 | } |
6078 | } |
6079 | |
6080 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
6081 | } |
6082 | |
6083 | |
6084 | |
6085 | |
6086 | |
6087 | |
6088 | static bool IsAcceptableNonMemberOperatorCandidate(ASTContext &Context, |
6089 | FunctionDecl *Fn, |
6090 | ArrayRef<Expr *> Args) { |
6091 | QualType T1 = Args[0]->getType(); |
6092 | QualType T2 = Args.size() > 1 ? Args[1]->getType() : QualType(); |
6093 | |
6094 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
6095 | return true; |
6096 | |
6097 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
6098 | return true; |
6099 | |
6100 | const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); |
6101 | if (Proto->getNumParams() < 1) |
6102 | return false; |
6103 | |
6104 | if (T1->isEnumeralType()) { |
6105 | QualType ArgType = Proto->getParamType(0).getNonReferenceType(); |
6106 | if (Context.hasSameUnqualifiedType(T1, ArgType)) |
6107 | return true; |
6108 | } |
6109 | |
6110 | if (Proto->getNumParams() < 2) |
6111 | return false; |
6112 | |
6113 | if (!T2.isNull() && T2->isEnumeralType()) { |
6114 | QualType ArgType = Proto->getParamType(1).getNonReferenceType(); |
6115 | if (Context.hasSameUnqualifiedType(T2, ArgType)) |
6116 | return true; |
6117 | } |
6118 | |
6119 | return false; |
6120 | } |
6121 | |
6122 | |
6123 | |
6124 | |
6125 | |
6126 | |
6127 | |
6128 | |
6129 | |
6130 | void Sema::AddOverloadCandidate( |
6131 | FunctionDecl *Function, DeclAccessPair FoundDecl, ArrayRef<Expr *> Args, |
6132 | OverloadCandidateSet &CandidateSet, bool SuppressUserConversions, |
6133 | bool PartialOverloading, bool AllowExplicit, bool AllowExplicitConversions, |
6134 | ADLCallKind IsADLCandidate, ConversionSequenceList EarlyConversions, |
6135 | OverloadCandidateParamOrder PO) { |
6136 | const FunctionProtoType *Proto |
6137 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
6138 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
6139 | assert(!Function->getDescribedFunctionTemplate() && |
6140 | "Use AddTemplateOverloadCandidate for function templates"); |
6141 | |
6142 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
6143 | if (!isa<CXXConstructorDecl>(Method)) { |
6144 | |
6145 | |
6146 | |
6147 | |
6148 | |
6149 | |
6150 | |
6151 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), QualType(), |
6152 | Expr::Classification::makeSimpleLValue(), Args, |
6153 | CandidateSet, SuppressUserConversions, |
6154 | PartialOverloading, EarlyConversions, PO); |
6155 | return; |
6156 | } |
6157 | |
6158 | |
6159 | } |
6160 | |
6161 | if (!CandidateSet.isNewCandidate(Function, PO)) |
6162 | return; |
6163 | |
6164 | |
6165 | |
6166 | |
6167 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function); |
6168 | if (Constructor && Constructor->isDefaulted() && Constructor->isDeleted() && |
6169 | Constructor->isMoveConstructor()) |
6170 | return; |
6171 | |
6172 | |
6173 | EnterExpressionEvaluationContext Unevaluated( |
6174 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
6175 | |
6176 | |
6177 | |
6178 | |
6179 | |
6180 | |
6181 | |
6182 | |
6183 | if (CandidateSet.getKind() == OverloadCandidateSet::CSK_Operator && |
6184 | !IsAcceptableNonMemberOperatorCandidate(Context, Function, Args)) |
6185 | return; |
6186 | |
6187 | |
6188 | OverloadCandidate &Candidate = |
6189 | CandidateSet.addCandidate(Args.size(), EarlyConversions); |
6190 | Candidate.FoundDecl = FoundDecl; |
6191 | Candidate.Function = Function; |
6192 | Candidate.Viable = true; |
6193 | Candidate.RewriteKind = |
6194 | CandidateSet.getRewriteInfo().getRewriteKind(Function, PO); |
6195 | Candidate.IsSurrogate = false; |
6196 | Candidate.IsADLCandidate = IsADLCandidate; |
6197 | Candidate.IgnoreObjectArgument = false; |
6198 | Candidate.ExplicitCallArguments = Args.size(); |
6199 | |
6200 | |
6201 | |
6202 | |
6203 | if (!AllowExplicit && ExplicitSpecifier::getFromDecl(Function).isExplicit()) { |
6204 | Candidate.Viable = false; |
6205 | Candidate.FailureKind = ovl_fail_explicit; |
6206 | return; |
6207 | } |
6208 | |
6209 | if (Function->isMultiVersion() && Function->hasAttr<TargetAttr>() && |
6210 | !Function->getAttr<TargetAttr>()->isDefaultVersion()) { |
6211 | Candidate.Viable = false; |
6212 | Candidate.FailureKind = ovl_non_default_multiversion_function; |
6213 | return; |
6214 | } |
6215 | |
6216 | if (Constructor) { |
6217 | |
6218 | |
6219 | |
6220 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
6221 | if (Args.size() == 1 && Constructor->isSpecializationCopyingObject() && |
6222 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
6223 | IsDerivedFrom(Args[0]->getBeginLoc(), Args[0]->getType(), |
6224 | ClassType))) { |
6225 | Candidate.Viable = false; |
6226 | Candidate.FailureKind = ovl_fail_illegal_constructor; |
6227 | return; |
6228 | } |
6229 | |
6230 | |
6231 | |
6232 | |
6233 | |
6234 | |
6235 | |
6236 | |
6237 | auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl.getDecl()); |
6238 | if (Shadow && Args.size() == 1 && Constructor->getNumParams() >= 1 && |
6239 | Constructor->getParamDecl(0)->getType()->isReferenceType()) { |
6240 | QualType P = Constructor->getParamDecl(0)->getType()->getPointeeType(); |
6241 | QualType C = Context.getRecordType(Constructor->getParent()); |
6242 | QualType D = Context.getRecordType(Shadow->getParent()); |
6243 | SourceLocation Loc = Args.front()->getExprLoc(); |
6244 | if ((Context.hasSameUnqualifiedType(P, C) || IsDerivedFrom(Loc, P, C)) && |
6245 | (Context.hasSameUnqualifiedType(D, P) || IsDerivedFrom(Loc, D, P))) { |
6246 | Candidate.Viable = false; |
6247 | Candidate.FailureKind = ovl_fail_inhctor_slice; |
6248 | return; |
6249 | } |
6250 | } |
6251 | |
6252 | |
6253 | |
6254 | if (!Qualifiers::isAddressSpaceSupersetOf( |
6255 | Constructor->getMethodQualifiers().getAddressSpace(), |
6256 | CandidateSet.getDestAS())) { |
6257 | Candidate.Viable = false; |
6258 | Candidate.FailureKind = ovl_fail_object_addrspace_mismatch; |
6259 | } |
6260 | } |
6261 | |
6262 | unsigned NumParams = Proto->getNumParams(); |
6263 | |
6264 | |
6265 | |
6266 | |
6267 | if (TooManyArguments(NumParams, Args.size(), PartialOverloading) && |
6268 | !Proto->isVariadic()) { |
6269 | Candidate.Viable = false; |
6270 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
6271 | return; |
6272 | } |
6273 | |
6274 | |
6275 | |
6276 | |
6277 | |
6278 | |
6279 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
6280 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
6281 | |
6282 | Candidate.Viable = false; |
6283 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
6284 | return; |
6285 | } |
6286 | |
6287 | |
6288 | if (getLangOpts().CUDA) |
6289 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
6290 | |
6291 | |
6292 | |
6293 | |
6294 | if (!Caller->isImplicit() && !IsAllowedCUDACall(Caller, Function)) { |
6295 | Candidate.Viable = false; |
6296 | Candidate.FailureKind = ovl_fail_bad_target; |
6297 | return; |
6298 | } |
6299 | |
6300 | if (Expr *RequiresClause = Function->getTrailingRequiresClause()) { |
6301 | ConstraintSatisfaction Satisfaction; |
6302 | if (CheckConstraintSatisfaction(RequiresClause, Satisfaction) || |
6303 | !Satisfaction.IsSatisfied) { |
6304 | Candidate.Viable = false; |
6305 | Candidate.FailureKind = ovl_fail_constraints_not_satisfied; |
6306 | return; |
6307 | } |
6308 | } |
6309 | |
6310 | |
6311 | |
6312 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
6313 | unsigned ConvIdx = |
6314 | PO == OverloadCandidateParamOrder::Reversed ? 1 - ArgIdx : ArgIdx; |
6315 | if (Candidate.Conversions[ConvIdx].isInitialized()) { |
6316 | |
6317 | |
6318 | } else if (ArgIdx < NumParams) { |
6319 | |
6320 | |
6321 | |
6322 | |
6323 | QualType ParamType = Proto->getParamType(ArgIdx); |
6324 | Candidate.Conversions[ConvIdx] = TryCopyInitialization( |
6325 | *this, Args[ArgIdx], ParamType, SuppressUserConversions, |
6326 | true, |
6327 | |
6328 | getLangOpts().ObjCAutoRefCount, AllowExplicitConversions); |
6329 | if (Candidate.Conversions[ConvIdx].isBad()) { |
6330 | Candidate.Viable = false; |
6331 | Candidate.FailureKind = ovl_fail_bad_conversion; |
6332 | return; |
6333 | } |
6334 | } else { |
6335 | |
6336 | |
6337 | |
6338 | Candidate.Conversions[ConvIdx].setEllipsis(); |
6339 | } |
6340 | } |
6341 | |
6342 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Function, Args)) { |
6343 | Candidate.Viable = false; |
6344 | Candidate.FailureKind = ovl_fail_enable_if; |
6345 | Candidate.DeductionFailure.Data = FailedAttr; |
6346 | return; |
6347 | } |
6348 | |
6349 | if (LangOpts.OpenCL && isOpenCLDisabledDecl(Function)) { |
6350 | Candidate.Viable = false; |
6351 | Candidate.FailureKind = ovl_fail_ext_disabled; |
6352 | return; |
6353 | } |
6354 | } |
6355 | |
6356 | ObjCMethodDecl * |
6357 | Sema::SelectBestMethod(Selector Sel, MultiExprArg Args, bool IsInstance, |
6358 | SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
6359 | if (Methods.size() <= 1) |
6360 | return nullptr; |
6361 | |
6362 | for (unsigned b = 0, e = Methods.size(); b < e; b++) { |
6363 | bool Match = true; |
6364 | ObjCMethodDecl *Method = Methods[b]; |
6365 | unsigned NumNamedArgs = Sel.getNumArgs(); |
6366 | |
6367 | |
6368 | if (Method->param_size() > NumNamedArgs) |
6369 | NumNamedArgs = Method->param_size(); |
6370 | if (Args.size() < NumNamedArgs) |
6371 | continue; |
6372 | |
6373 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
6374 | |
6375 | if (Args[i]->isTypeDependent()) { |
6376 | Match = false; |
6377 | break; |
6378 | } |
6379 | |
6380 | ParmVarDecl *param = Method->parameters()[i]; |
6381 | Expr *argExpr = Args[i]; |
6382 | assert(argExpr && "SelectBestMethod(): missing expression"); |
6383 | |
6384 | |
6385 | |
6386 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
6387 | !param->hasAttr<CFConsumedAttr>()) |
6388 | argExpr = stripARCUnbridgedCast(argExpr); |
6389 | |
6390 | |
6391 | if (param->getType() == Context.UnknownAnyTy) { |
6392 | Match = false; |
6393 | break; |
6394 | } |
6395 | |
6396 | ImplicitConversionSequence ConversionState |
6397 | = TryCopyInitialization(*this, argExpr, param->getType(), |
6398 | false, |
6399 | true, |
6400 | |
6401 | getLangOpts().ObjCAutoRefCount, |
6402 | false); |
6403 | |
6404 | |
6405 | if (ConversionState.isBad() || |
6406 | (ConversionState.isStandard() && |
6407 | ConversionState.Standard.Second == |
6408 | ICK_Incompatible_Pointer_Conversion)) { |
6409 | Match = false; |
6410 | break; |
6411 | } |
6412 | } |
6413 | |
6414 | if (Match && Method->isVariadic()) { |
6415 | for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) { |
6416 | if (Args[i]->isTypeDependent()) { |
6417 | Match = false; |
6418 | break; |
6419 | } |
6420 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
6421 | nullptr); |
6422 | if (Arg.isInvalid()) { |
6423 | Match = false; |
6424 | break; |
6425 | } |
6426 | } |
6427 | } else { |
6428 | |
6429 | if (Args.size() != NumNamedArgs) |
6430 | Match = false; |
6431 | else if (Match && NumNamedArgs == 0 && Methods.size() > 1) { |
6432 | |
6433 | |
6434 | for (unsigned b = 0, e = Methods.size(); b < e; b++) { |
6435 | QualType ReturnT = Methods[b]->getReturnType(); |
6436 | if (ReturnT->isObjCIdType()) |
6437 | return Methods[b]; |
6438 | } |
6439 | } |
6440 | } |
6441 | |
6442 | if (Match) |
6443 | return Method; |
6444 | } |
6445 | return nullptr; |
6446 | } |
6447 | |
6448 | static bool |
6449 | convertArgsForAvailabilityChecks(Sema &S, FunctionDecl *Function, Expr *ThisArg, |
6450 | ArrayRef<Expr *> Args, Sema::SFINAETrap &Trap, |
6451 | bool MissingImplicitThis, Expr *&ConvertedThis, |
6452 | SmallVectorImpl<Expr *> &ConvertedArgs) { |
6453 | if (ThisArg) { |
6454 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Function); |
6455 | assert(!isa<CXXConstructorDecl>(Method) && |
6456 | "Shouldn't have `this` for ctors!"); |
6457 | assert(!Method->isStatic() && "Shouldn't have `this` for static methods!"); |
6458 | ExprResult R = S.PerformObjectArgumentInitialization( |
6459 | ThisArg, nullptr, Method, Method); |
6460 | if (R.isInvalid()) |
6461 | return false; |
6462 | ConvertedThis = R.get(); |
6463 | } else { |
6464 | if (auto *MD = dyn_cast<CXXMethodDecl>(Function)) { |
6465 | (void)MD; |
6466 | assert((MissingImplicitThis || MD->isStatic() || |
6467 | isa<CXXConstructorDecl>(MD)) && |
6468 | "Expected `this` for non-ctor instance methods"); |
6469 | } |
6470 | ConvertedThis = nullptr; |
6471 | } |
6472 | |
6473 | |
6474 | |
6475 | unsigned ArgSizeNoVarargs = std::min(Function->param_size(), Args.size()); |
6476 | |
6477 | |
6478 | for (unsigned I = 0; I != ArgSizeNoVarargs; ++I) { |
6479 | ExprResult R; |
6480 | R = S.PerformCopyInitialization(InitializedEntity::InitializeParameter( |
6481 | S.Context, Function->getParamDecl(I)), |
6482 | SourceLocation(), Args[I]); |
6483 | |
6484 | if (R.isInvalid()) |
6485 | return false; |
6486 | |
6487 | ConvertedArgs.push_back(R.get()); |
6488 | } |
6489 | |
6490 | if (Trap.hasErrorOccurred()) |
6491 | return false; |
6492 | |
6493 | |
6494 | if (!Function->isVariadic() && Args.size() < Function->getNumParams()) { |
6495 | for (unsigned i = Args.size(), e = Function->getNumParams(); i != e; ++i) { |
6496 | ParmVarDecl *P = Function->getParamDecl(i); |
6497 | Expr *DefArg = P->hasUninstantiatedDefaultArg() |
6498 | ? P->getUninstantiatedDefaultArg() |
6499 | : P->getDefaultArg(); |
6500 | |
6501 | |
6502 | if (!DefArg) |
6503 | return false; |
6504 | ExprResult R = |
6505 | S.PerformCopyInitialization(InitializedEntity::InitializeParameter( |
6506 | S.Context, Function->getParamDecl(i)), |
6507 | SourceLocation(), DefArg); |
6508 | if (R.isInvalid()) |
6509 | return false; |
6510 | ConvertedArgs.push_back(R.get()); |
6511 | } |
6512 | |
6513 | if (Trap.hasErrorOccurred()) |
6514 | return false; |
6515 | } |
6516 | return true; |
6517 | } |
6518 | |
6519 | EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, |
6520 | bool MissingImplicitThis) { |
6521 | auto EnableIfAttrs = Function->specific_attrs<EnableIfAttr>(); |
6522 | if (EnableIfAttrs.begin() == EnableIfAttrs.end()) |
6523 | return nullptr; |
6524 | |
6525 | SFINAETrap Trap(*this); |
6526 | SmallVector<Expr *, 16> ConvertedArgs; |
6527 | |
6528 | Expr *DiscardedThis; |
6529 | if (!convertArgsForAvailabilityChecks( |
6530 | *this, Function, nullptr, Args, Trap, |
6531 | true, DiscardedThis, ConvertedArgs)) |
6532 | return *EnableIfAttrs.begin(); |
6533 | |
6534 | for (auto *EIA : EnableIfAttrs) { |
6535 | APValue Result; |
6536 | |
6537 | |
6538 | if (EIA->getCond()->isValueDependent() || |
6539 | !EIA->getCond()->EvaluateWithSubstitution( |
6540 | Result, Context, Function, llvm::makeArrayRef(ConvertedArgs))) |
6541 | return EIA; |
6542 | |
6543 | if (!Result.isInt() || !Result.getInt().getBoolValue()) |
6544 | return EIA; |
6545 | } |
6546 | return nullptr; |
6547 | } |
6548 | |
6549 | template <typename CheckFn> |
6550 | static bool diagnoseDiagnoseIfAttrsWith(Sema &S, const NamedDecl *ND, |
6551 | bool ArgDependent, SourceLocation Loc, |
6552 | CheckFn &&IsSuccessful) { |
6553 | SmallVector<const DiagnoseIfAttr *, 8> Attrs; |
6554 | for (const auto *DIA : ND->specific_attrs<DiagnoseIfAttr>()) { |
6555 | if (ArgDependent == DIA->getArgDependent()) |
6556 | Attrs.push_back(DIA); |
6557 | } |
6558 | |
6559 | |
6560 | if (Attrs.empty()) |
6561 | return false; |
6562 | |
6563 | auto WarningBegin = std::stable_partition( |
6564 | Attrs.begin(), Attrs.end(), |
6565 | [](const DiagnoseIfAttr *DIA) { return DIA->isError(); }); |
6566 | |
6567 | |
6568 | |
6569 | auto ErrAttr = llvm::find_if(llvm::make_range(Attrs.begin(), WarningBegin), |
6570 | IsSuccessful); |
6571 | if (ErrAttr != WarningBegin) { |
6572 | const DiagnoseIfAttr *DIA = *ErrAttr; |
6573 | S.Diag(Loc, diag::err_diagnose_if_succeeded) << DIA->getMessage(); |
6574 | S.Diag(DIA->getLocation(), diag::note_from_diagnose_if) |
6575 | << DIA->getParent() << DIA->getCond()->getSourceRange(); |
6576 | return true; |
6577 | } |
6578 | |
6579 | for (const auto *DIA : llvm::make_range(WarningBegin, Attrs.end())) |
6580 | if (IsSuccessful(DIA)) { |
6581 | S.Diag(Loc, diag::warn_diagnose_if_succeeded) << DIA->getMessage(); |
6582 | S.Diag(DIA->getLocation(), diag::note_from_diagnose_if) |
6583 | << DIA->getParent() << DIA->getCond()->getSourceRange(); |
6584 | } |
6585 | |
6586 | return false; |
6587 | } |
6588 | |
6589 | bool Sema::diagnoseArgDependentDiagnoseIfAttrs(const FunctionDecl *Function, |
6590 | const Expr *ThisArg, |
6591 | ArrayRef<const Expr *> Args, |
6592 | SourceLocation Loc) { |
6593 | return diagnoseDiagnoseIfAttrsWith( |
6594 | *this, Function, true, Loc, |
6595 | [&](const DiagnoseIfAttr *DIA) { |
6596 | APValue Result; |
6597 | |
6598 | |
6599 | |
6600 | if (!DIA->getCond()->EvaluateWithSubstitution( |
6601 | Result, Context, cast<FunctionDecl>(DIA->getParent()), Args, ThisArg)) |
6602 | return false; |
6603 | return Result.isInt() && Result.getInt().getBoolValue(); |
6604 | }); |
6605 | } |
6606 | |
6607 | bool Sema::diagnoseArgIndependentDiagnoseIfAttrs(const NamedDecl *ND, |
6608 | SourceLocation Loc) { |
6609 | return diagnoseDiagnoseIfAttrsWith( |
6610 | *this, ND, false, Loc, |
6611 | [&](const DiagnoseIfAttr *DIA) { |
6612 | bool Result; |
6613 | return DIA->getCond()->EvaluateAsBooleanCondition(Result, Context) && |
6614 | Result; |
6615 | }); |
6616 | } |
6617 | |
6618 | |
6619 | |
6620 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
6621 | ArrayRef<Expr *> Args, |
6622 | OverloadCandidateSet &CandidateSet, |
6623 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
6624 | bool SuppressUserConversions, |
6625 | bool PartialOverloading, |
6626 | bool FirstArgumentIsBase) { |
6627 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
6628 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
6629 | ArrayRef<Expr *> FunctionArgs = Args; |
6630 | |
6631 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
6632 | FunctionDecl *FD = |
6633 | FunTmpl ? FunTmpl->getTemplatedDecl() : cast<FunctionDecl>(D); |
6634 | |
6635 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) { |
6636 | QualType ObjectType; |
6637 | Expr::Classification ObjectClassification; |
6638 | if (Args.size() > 0) { |
6639 | if (Expr *E = Args[0]) { |
6640 | |
6641 | ObjectType = E->getType(); |
6642 | |
6643 | |
6644 | if (!ObjectType.isNull() && ObjectType->isPointerType()) |
6645 | ObjectClassification = Expr::Classification::makeSimpleLValue(); |
6646 | else |
6647 | ObjectClassification = E->Classify(Context); |
6648 | } |
6649 | FunctionArgs = Args.slice(1); |
6650 | } |
6651 | if (FunTmpl) { |
6652 | AddMethodTemplateCandidate( |
6653 | FunTmpl, F.getPair(), |
6654 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
6655 | ExplicitTemplateArgs, ObjectType, ObjectClassification, |
6656 | FunctionArgs, CandidateSet, SuppressUserConversions, |
6657 | PartialOverloading); |
6658 | } else { |
6659 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
6660 | cast<CXXMethodDecl>(FD)->getParent(), ObjectType, |
6661 | ObjectClassification, FunctionArgs, CandidateSet, |
6662 | SuppressUserConversions, PartialOverloading); |
6663 | } |
6664 | } else { |
6665 | |
6666 | |
6667 | |
6668 | |
6669 | if (Args.size() > 0 && |
6670 | (!Args[0] || (FirstArgumentIsBase && isa<CXXMethodDecl>(FD) && |
6671 | !isa<CXXConstructorDecl>(FD)))) { |
6672 | assert(cast<CXXMethodDecl>(FD)->isStatic()); |
6673 | FunctionArgs = Args.slice(1); |
6674 | } |
6675 | if (FunTmpl) { |
6676 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
6677 | ExplicitTemplateArgs, FunctionArgs, |
6678 | CandidateSet, SuppressUserConversions, |
6679 | PartialOverloading); |
6680 | } else { |
6681 | AddOverloadCandidate(FD, F.getPair(), FunctionArgs, CandidateSet, |
6682 | SuppressUserConversions, PartialOverloading); |
6683 | } |
6684 | } |
6685 | } |
6686 | } |
6687 | |
6688 | |
6689 | |
6690 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, QualType ObjectType, |
6691 | Expr::Classification ObjectClassification, |
6692 | ArrayRef<Expr *> Args, |
6693 | OverloadCandidateSet &CandidateSet, |
6694 | bool SuppressUserConversions, |
6695 | OverloadCandidateParamOrder PO) { |
6696 | NamedDecl *Decl = FoundDecl.getDecl(); |
6697 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
6698 | |
6699 | if (isa<UsingShadowDecl>(Decl)) |
6700 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
6701 | |
6702 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
6703 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
6704 | "Expected a member function template"); |
6705 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
6706 | nullptr, ObjectType, |
6707 | ObjectClassification, Args, CandidateSet, |
6708 | SuppressUserConversions, false, PO); |
6709 | } else { |
6710 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
6711 | ObjectType, ObjectClassification, Args, CandidateSet, |
6712 | SuppressUserConversions, false, None, PO); |
6713 | } |
6714 | } |
6715 | |
6716 | |
6717 | |
6718 | |
6719 | |
6720 | |
6721 | |
6722 | |
6723 | void |
6724 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
6725 | CXXRecordDecl *ActingContext, QualType ObjectType, |
6726 | Expr::Classification ObjectClassification, |
6727 | ArrayRef<Expr *> Args, |
6728 | OverloadCandidateSet &CandidateSet, |
6729 | bool SuppressUserConversions, |
6730 | bool PartialOverloading, |
6731 | ConversionSequenceList EarlyConversions, |
6732 | OverloadCandidateParamOrder PO) { |
6733 | const FunctionProtoType *Proto |
6734 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
6735 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
6736 | assert(!isa<CXXConstructorDecl>(Method) && |
6737 | "Use AddOverloadCandidate for constructors"); |
6738 | |
6739 | if (!CandidateSet.isNewCandidate(Method, PO)) |
6740 | return; |
6741 | |
6742 | |
6743 | |
6744 | |
6745 | if (Method->isDefaulted() && Method->isDeleted() && |
6746 | Method->isMoveAssignmentOperator()) |
6747 | return; |
6748 | |
6749 | |
6750 | EnterExpressionEvaluationContext Unevaluated( |
6751 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
6752 | |
6753 | |
6754 | OverloadCandidate &Candidate = |
6755 | CandidateSet.addCandidate(Args.size() + 1, EarlyConversions); |
6756 | Candidate.FoundDecl = FoundDecl; |
6757 | Candidate.Function = Method; |
6758 | Candidate.RewriteKind = |
6759 | CandidateSet.getRewriteInfo().getRewriteKind(Method, PO); |
6760 | Candidate.IsSurrogate = false; |
6761 | Candidate.IgnoreObjectArgument = false; |
6762 | Candidate.ExplicitCallArguments = Args.size(); |
6763 | |
6764 | unsigned NumParams = Proto->getNumParams(); |
6765 | |
6766 | |
6767 | |
6768 | |
6769 | if (TooManyArguments(NumParams, Args.size(), PartialOverloading) && |
6770 | !Proto->isVariadic()) { |
6771 | Candidate.Viable = false; |
6772 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
6773 | return; |
6774 | } |
6775 | |
6776 | |
6777 | |
6778 | |
6779 | |
6780 | |
6781 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
6782 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
6783 | |
6784 | Candidate.Viable = false; |
6785 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
6786 | return; |
6787 | } |
6788 | |
6789 | Candidate.Viable = true; |
6790 | |
6791 | if (Method->isStatic() || ObjectType.isNull()) |
6792 | |
6793 | Candidate.IgnoreObjectArgument = true; |
6794 | else { |
6795 | unsigned ConvIdx = PO == OverloadCandidateParamOrder::Reversed ? 1 : 0; |
6796 | |
6797 | |
6798 | Candidate.Conversions[ConvIdx] = TryObjectArgumentInitialization( |
6799 | *this, CandidateSet.getLocation(), ObjectType, ObjectClassification, |
6800 | Method, ActingContext); |
6801 | if (Candidate.Conversions[ConvIdx].isBad()) { |
6802 | Candidate.Viable = false; |
6803 | Candidate.FailureKind = ovl_fail_bad_conversion; |
6804 | return; |
6805 | } |
6806 | } |
6807 | |
6808 | |
6809 | if (getLangOpts().CUDA) |
6810 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
6811 | if (!IsAllowedCUDACall(Caller, Method)) { |
6812 | Candidate.Viable = false; |
6813 | Candidate.FailureKind = ovl_fail_bad_target; |
6814 | return; |
6815 | } |
6816 | |
6817 | if (Expr *RequiresClause = Method->getTrailingRequiresClause()) { |
6818 | ConstraintSatisfaction Satisfaction; |
6819 | if (CheckConstraintSatisfaction(RequiresClause, Satisfaction) || |
6820 | !Satisfaction.IsSatisfied) { |
6821 | Candidate.Viable = false; |
6822 | Candidate.FailureKind = ovl_fail_constraints_not_satisfied; |
6823 | return; |
6824 | } |
6825 | } |
6826 | |
6827 | |
6828 | |
6829 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
6830 | unsigned ConvIdx = |
6831 | PO == OverloadCandidateParamOrder::Reversed ? 0 : (ArgIdx + 1); |
6832 | if (Candidate.Conversions[ConvIdx].isInitialized()) { |
6833 | |
6834 | |
6835 | } else if (ArgIdx < NumParams) { |
6836 | |
6837 | |
6838 | |
6839 | |
6840 | QualType ParamType = Proto->getParamType(ArgIdx); |
6841 | Candidate.Conversions[ConvIdx] |
6842 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
6843 | SuppressUserConversions, |
6844 | true, |
6845 | |
6846 | getLangOpts().ObjCAutoRefCount); |
6847 | if (Candidate.Conversions[ConvIdx].isBad()) { |
6848 | Candidate.Viable = false; |
6849 | Candidate.FailureKind = ovl_fail_bad_conversion; |
6850 | return; |
6851 | } |
6852 | } else { |
6853 | |
6854 | |
6855 | |
6856 | Candidate.Conversions[ConvIdx].setEllipsis(); |
6857 | } |
6858 | } |
6859 | |
6860 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Method, Args, true)) { |
6861 | Candidate.Viable = false; |
6862 | Candidate.FailureKind = ovl_fail_enable_if; |
6863 | Candidate.DeductionFailure.Data = FailedAttr; |
6864 | return; |
6865 | } |
6866 | |
6867 | if (Method->isMultiVersion() && Method->hasAttr<TargetAttr>() && |
6868 | !Method->getAttr<TargetAttr>()->isDefaultVersion()) { |
6869 | Candidate.Viable = false; |
6870 | Candidate.FailureKind = ovl_non_default_multiversion_function; |
6871 | } |
6872 | } |
6873 | |
6874 | |
6875 | |
6876 | |
6877 | void Sema::AddMethodTemplateCandidate( |
6878 | FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl, |
6879 | CXXRecordDecl *ActingContext, |
6880 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType, |
6881 | Expr::Classification ObjectClassification, ArrayRef<Expr *> Args, |
6882 | OverloadCandidateSet &CandidateSet, bool SuppressUserConversions, |
6883 | bool PartialOverloading, OverloadCandidateParamOrder PO) { |
6884 | if (!CandidateSet.isNewCandidate(MethodTmpl, PO)) |
6885 | return; |
6886 | |
6887 | |
6888 | |
6889 | |
6890 | |
6891 | |
6892 | |
6893 | |
6894 | |
6895 | |
6896 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
6897 | FunctionDecl *Specialization = nullptr; |
6898 | ConversionSequenceList Conversions; |
6899 | if (TemplateDeductionResult Result = DeduceTemplateArguments( |
6900 | MethodTmpl, ExplicitTemplateArgs, Args, Specialization, Info, |
6901 | PartialOverloading, [&](ArrayRef<QualType> ParamTypes) { |
6902 | return CheckNonDependentConversions( |
6903 | MethodTmpl, ParamTypes, Args, CandidateSet, Conversions, |
6904 | SuppressUserConversions, ActingContext, ObjectType, |
6905 | ObjectClassification, PO); |
6906 | })) { |
6907 | OverloadCandidate &Candidate = |
6908 | CandidateSet.addCandidate(Conversions.size(), Conversions); |
6909 | Candidate.FoundDecl = FoundDecl; |
6910 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
6911 | Candidate.Viable = false; |
6912 | Candidate.RewriteKind = |
6913 | CandidateSet.getRewriteInfo().getRewriteKind(Candidate.Function, PO); |
6914 | Candidate.IsSurrogate = false; |
6915 | Candidate.IgnoreObjectArgument = |
6916 | cast<CXXMethodDecl>(Candidate.Function)->isStatic() || |
6917 | ObjectType.isNull(); |
6918 | Candidate.ExplicitCallArguments = Args.size(); |
6919 | if (Result == TDK_NonDependentConversionFailure) |
6920 | Candidate.FailureKind = ovl_fail_bad_conversion; |
6921 | else { |
6922 | Candidate.FailureKind = ovl_fail_bad_deduction; |
6923 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
6924 | Info); |
6925 | } |
6926 | return; |
6927 | } |
6928 | |
6929 | |
6930 | |
6931 | assert(Specialization && "Missing member function template specialization?"); |
6932 | assert(isa<CXXMethodDecl>(Specialization) && |
6933 | "Specialization is not a member function?"); |
6934 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
6935 | ActingContext, ObjectType, ObjectClassification, Args, |
6936 | CandidateSet, SuppressUserConversions, PartialOverloading, |
6937 | Conversions, PO); |
6938 | } |
6939 | |
6940 | |
6941 | |
6942 | static bool isNonDependentlyExplicit(FunctionTemplateDecl *FTD) { |
6943 | return ExplicitSpecifier::getFromDecl(FTD->getTemplatedDecl()).isExplicit(); |
6944 | } |
6945 | |
6946 | |
6947 | |
6948 | |
6949 | void Sema::AddTemplateOverloadCandidate( |
6950 | FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl, |
6951 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
6952 | OverloadCandidateSet &CandidateSet, bool SuppressUserConversions, |
6953 | bool PartialOverloading, bool AllowExplicit, ADLCallKind IsADLCandidate, |
6954 | OverloadCandidateParamOrder PO) { |
6955 | if (!CandidateSet.isNewCandidate(FunctionTemplate, PO)) |
6956 | return; |
6957 | |
6958 | |
6959 | |
6960 | |
6961 | if (!AllowExplicit && isNonDependentlyExplicit(FunctionTemplate)) { |
6962 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
6963 | Candidate.FoundDecl = FoundDecl; |
6964 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
6965 | Candidate.Viable = false; |
6966 | Candidate.FailureKind = ovl_fail_explicit; |
6967 | return; |
6968 | } |
6969 | |
6970 | |
6971 | |
6972 | |
6973 | |
6974 | |
6975 | |
6976 | |
6977 | |
6978 | |
6979 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
6980 | FunctionDecl *Specialization = nullptr; |
6981 | ConversionSequenceList Conversions; |
6982 | if (TemplateDeductionResult Result = DeduceTemplateArguments( |
6983 | FunctionTemplate, ExplicitTemplateArgs, Args, Specialization, Info, |
6984 | PartialOverloading, [&](ArrayRef<QualType> ParamTypes) { |
6985 | return CheckNonDependentConversions( |
6986 | FunctionTemplate, ParamTypes, Args, CandidateSet, Conversions, |
6987 | SuppressUserConversions, nullptr, QualType(), {}, PO); |
6988 | })) { |
6989 | OverloadCandidate &Candidate = |
6990 | CandidateSet.addCandidate(Conversions.size(), Conversions); |
6991 | Candidate.FoundDecl = FoundDecl; |
6992 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
6993 | Candidate.Viable = false; |
6994 | Candidate.RewriteKind = |
6995 | CandidateSet.getRewriteInfo().getRewriteKind(Candidate.Function, PO); |
6996 | Candidate.IsSurrogate = false; |
6997 | Candidate.IsADLCandidate = IsADLCandidate; |
6998 | |
6999 | |
7000 | Candidate.IgnoreObjectArgument = |
7001 | isa<CXXMethodDecl>(Candidate.Function) && |
7002 | !isa<CXXConstructorDecl>(Candidate.Function); |
7003 | Candidate.ExplicitCallArguments = Args.size(); |
7004 | if (Result == TDK_NonDependentConversionFailure) |
7005 | Candidate.FailureKind = ovl_fail_bad_conversion; |
7006 | else { |
7007 | Candidate.FailureKind = ovl_fail_bad_deduction; |
7008 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
7009 | Info); |
7010 | } |
7011 | return; |
7012 | } |
7013 | |
7014 | |
7015 | |
7016 | assert(Specialization && "Missing function template specialization?"); |
7017 | AddOverloadCandidate( |
7018 | Specialization, FoundDecl, Args, CandidateSet, SuppressUserConversions, |
7019 | PartialOverloading, AllowExplicit, |
7020 | false, IsADLCandidate, Conversions, PO); |
7021 | } |
7022 | |
7023 | |
7024 | |
7025 | |
7026 | bool Sema::CheckNonDependentConversions( |
7027 | FunctionTemplateDecl *FunctionTemplate, ArrayRef<QualType> ParamTypes, |
7028 | ArrayRef<Expr *> Args, OverloadCandidateSet &CandidateSet, |
7029 | ConversionSequenceList &Conversions, bool SuppressUserConversions, |
7030 | CXXRecordDecl *ActingContext, QualType ObjectType, |
7031 | Expr::Classification ObjectClassification, OverloadCandidateParamOrder PO) { |
7032 | |
7033 | |
7034 | |
7035 | const bool AllowExplicit = false; |
7036 | |
7037 | auto *FD = FunctionTemplate->getTemplatedDecl(); |
7038 | auto *Method = dyn_cast<CXXMethodDecl>(FD); |
7039 | bool HasThisConversion = Method && !isa<CXXConstructorDecl>(Method); |
7040 | unsigned ThisConversions = HasThisConversion ? 1 : 0; |
7041 | |
7042 | Conversions = |
7043 | CandidateSet.allocateConversionSequences(ThisConversions + Args.size()); |
7044 | |
7045 | |
7046 | EnterExpressionEvaluationContext Unevaluated( |
7047 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
7048 | |
7049 | |
7050 | |
7051 | |
7052 | if (HasThisConversion && !cast<CXXMethodDecl>(FD)->isStatic() && |
7053 | !ObjectType.isNull()) { |
7054 | unsigned ConvIdx = PO == OverloadCandidateParamOrder::Reversed ? 1 : 0; |
7055 | Conversions[ConvIdx] = TryObjectArgumentInitialization( |
7056 | *this, CandidateSet.getLocation(), ObjectType, ObjectClassification, |
7057 | Method, ActingContext); |
7058 | if (Conversions[ConvIdx].isBad()) |
7059 | return true; |
7060 | } |
7061 | |
7062 | for (unsigned I = 0, N = std::min(ParamTypes.size(), Args.size()); I != N; |
7063 | ++I) { |
7064 | QualType ParamType = ParamTypes[I]; |
7065 | if (!ParamType->isDependentType()) { |
7066 | unsigned ConvIdx = PO == OverloadCandidateParamOrder::Reversed |
7067 | ? 0 |
7068 | : (ThisConversions + I); |
7069 | Conversions[ConvIdx] |
7070 | = TryCopyInitialization(*this, Args[I], ParamType, |
7071 | SuppressUserConversions, |
7072 | true, |
7073 | |
7074 | getLangOpts().ObjCAutoRefCount, |
7075 | AllowExplicit); |
7076 | if (Conversions[ConvIdx].isBad()) |
7077 | return true; |
7078 | } |
7079 | } |
7080 | |
7081 | return false; |
7082 | } |
7083 | |
7084 | |
7085 | |
7086 | |
7087 | |
7088 | |
7089 | |
7090 | |
7091 | |
7092 | |
7093 | |
7094 | |
7095 | |
7096 | static bool isAllowableExplicitConversion(Sema &S, |
7097 | QualType ConvType, QualType ToType, |
7098 | bool AllowObjCPointerConversion) { |
7099 | QualType ToNonRefType = ToType.getNonReferenceType(); |
7100 | |
7101 | |
7102 | if (S.Context.hasSameUnqualifiedType(ConvType, ToNonRefType)) |
7103 | return true; |
7104 | |
7105 | |
7106 | bool ObjCLifetimeConversion; |
7107 | if (S.IsQualificationConversion(ConvType, ToNonRefType, false, |
7108 | ObjCLifetimeConversion)) |
7109 | return true; |
7110 | |
7111 | |
7112 | |
7113 | if (!AllowObjCPointerConversion) |
7114 | return false; |
7115 | |
7116 | |
7117 | bool IncompatibleObjC = false; |
7118 | QualType ConvertedType; |
7119 | return S.isObjCPointerConversion(ConvType, ToNonRefType, ConvertedType, |
7120 | IncompatibleObjC); |
7121 | } |
7122 | |
7123 | |
7124 | |
7125 | |
7126 | |
7127 | |
7128 | |
7129 | void Sema::AddConversionCandidate( |
7130 | CXXConversionDecl *Conversion, DeclAccessPair FoundDecl, |
7131 | CXXRecordDecl *ActingContext, Expr *From, QualType ToType, |
7132 | OverloadCandidateSet &CandidateSet, bool AllowObjCConversionOnExplicit, |
7133 | bool AllowExplicit, bool AllowResultConversion) { |
7134 | assert(!Conversion->getDescribedFunctionTemplate() && |
7135 | "Conversion function templates use AddTemplateConversionCandidate"); |
7136 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
7137 | if (!CandidateSet.isNewCandidate(Conversion)) |
7138 | return; |
7139 | |
7140 | |
7141 | |
7142 | if (getLangOpts().CPlusPlus14 && ConvType->isUndeducedType()) { |
7143 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
7144 | return; |
7145 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
7146 | } |
7147 | |
7148 | |
7149 | |
7150 | if (!AllowResultConversion && |
7151 | !Context.hasSameUnqualifiedType(Conversion->getConversionType(), ToType)) |
7152 | return; |
7153 | |
7154 | |
7155 | |
7156 | |
7157 | |
7158 | |
7159 | |
7160 | if (Conversion->isExplicit() && |
7161 | !isAllowableExplicitConversion(*this, ConvType, ToType, |
7162 | AllowObjCConversionOnExplicit)) |
7163 | return; |
7164 | |
7165 | |
7166 | EnterExpressionEvaluationContext Unevaluated( |
7167 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
7168 | |
7169 | |
7170 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
7171 | Candidate.FoundDecl = FoundDecl; |
7172 | Candidate.Function = Conversion; |
7173 | Candidate.IsSurrogate = false; |
7174 | Candidate.IgnoreObjectArgument = false; |
7175 | Candidate.FinalConversion.setAsIdentityConversion(); |
7176 | Candidate.FinalConversion.setFromType(ConvType); |
7177 | Candidate.FinalConversion.setAllToTypes(ToType); |
7178 | Candidate.Viable = true; |
7179 | Candidate.ExplicitCallArguments = 1; |
7180 | |
7181 | |
7182 | |
7183 | |
7184 | if (!AllowExplicit && Conversion->isExplicit()) { |
7185 | Candidate.Viable = false; |
7186 | Candidate.FailureKind = ovl_fail_explicit; |
7187 | return; |
7188 | } |
7189 | |
7190 | |
7191 | |
7192 | |
7193 | |
7194 | |
7195 | |
7196 | |
7197 | QualType ImplicitParamType = From->getType(); |
7198 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
7199 | ImplicitParamType = FromPtrType->getPointeeType(); |
7200 | CXXRecordDecl *ConversionContext |
7201 | = cast<CXXRecordDecl>(ImplicitParamType->castAs<RecordType>()->getDecl()); |
7202 | |
7203 | Candidate.Conversions[0] = TryObjectArgumentInitialization( |
7204 | *this, CandidateSet.getLocation(), From->getType(), |
7205 | From->Classify(Context), Conversion, ConversionContext); |
7206 | |
7207 | if (Candidate.Conversions[0].isBad()) { |
7208 | Candidate.Viable = false; |
7209 | Candidate.FailureKind = ovl_fail_bad_conversion; |
7210 | return; |
7211 | } |
7212 | |
7213 | Expr *RequiresClause = Conversion->getTrailingRequiresClause(); |
7214 | if (RequiresClause) { |
7215 | ConstraintSatisfaction Satisfaction; |
7216 | if (CheckConstraintSatisfaction(RequiresClause, Satisfaction) || |
7217 | !Satisfaction.IsSatisfied) { |
7218 | Candidate.Viable = false; |
7219 | Candidate.FailureKind = ovl_fail_constraints_not_satisfied; |
7220 | return; |
7221 | } |
7222 | } |
7223 | |
7224 | |
7225 | |
7226 | |
7227 | QualType FromCanon |
7228 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
7229 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
7230 | if (FromCanon == ToCanon || |
7231 | IsDerivedFrom(CandidateSet.getLocation(), FromCanon, ToCanon)) { |
7232 | Candidate.Viable = false; |
7233 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
7234 | return; |
7235 | } |
7236 | |
7237 | |
7238 | |
7239 | |
7240 | |
7241 | |
7242 | |
7243 | |
7244 | |
7245 | DeclRefExpr ConversionRef(Context, Conversion, false, Conversion->getType(), |
7246 | VK_LValue, From->getBeginLoc()); |
7247 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
7248 | Context.getPointerType(Conversion->getType()), |
7249 | CK_FunctionToPointerDecay, |
7250 | &ConversionRef, VK_RValue); |
7251 | |
7252 | QualType ConversionType = Conversion->getConversionType(); |
7253 | if (!isCompleteType(From->getBeginLoc(), ConversionType)) { |
7254 | Candidate.Viable = false; |
7255 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
7256 | return; |
7257 | } |
7258 | |
7259 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
7260 | |
7261 | |
7262 | |
7263 | |
7264 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
7265 | |
7266 | alignas(CallExpr) char Buffer[sizeof(CallExpr) + sizeof(Stmt *)]; |
7267 | CallExpr *TheTemporaryCall = CallExpr::CreateTemporary( |
7268 | Buffer, &ConversionFn, CallResultType, VK, From->getBeginLoc()); |
7269 | |
7270 | ImplicitConversionSequence ICS = |
7271 | TryCopyInitialization(*this, TheTemporaryCall, ToType, |
7272 | true, |
7273 | false, |
7274 | false); |
7275 | |
7276 | switch (ICS.getKind()) { |
7277 | case ImplicitConversionSequence::StandardConversion: |
7278 | Candidate.FinalConversion = ICS.Standard; |
7279 | |
7280 | |
7281 | |
7282 | |
7283 | |
7284 | if (Conversion->getPrimaryTemplate() && |
7285 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
7286 | Candidate.Viable = false; |
7287 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
7288 | return; |
7289 | } |
7290 | |
7291 | |
7292 | |
7293 | |
7294 | |
7295 | |
7296 | if (ToType->isRValueReferenceType() && |
7297 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
7298 | Candidate.Viable = false; |
7299 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
7300 | return; |
7301 | } |
7302 | break; |
7303 | |
7304 | case ImplicitConversionSequence::BadConversion: |
7305 | Candidate.Viable = false; |
7306 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
7307 | return; |
7308 | |
7309 | default: |
7310 | llvm_unreachable( |
7311 | "Can only end up with a standard conversion sequence or failure"); |
7312 | } |
7313 | |
7314 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, None)) { |
7315 | Candidate.Viable = false; |
7316 | Candidate.FailureKind = ovl_fail_enable_if; |
7317 | Candidate.DeductionFailure.Data = FailedAttr; |
7318 | return; |
7319 | } |
7320 | |
7321 | if (Conversion->isMultiVersion() && Conversion->hasAttr<TargetAttr>() && |
7322 | !Conversion->getAttr<TargetAttr>()->isDefaultVersion()) { |
7323 | Candidate.Viable = false; |
7324 | Candidate.FailureKind = ovl_non_default_multiversion_function; |
7325 | } |
7326 | } |
7327 | |
7328 | |
7329 | |
7330 | |
7331 | |
7332 | |
7333 | void Sema::AddTemplateConversionCandidate( |
7334 | FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl, |
7335 | CXXRecordDecl *ActingDC, Expr *From, QualType ToType, |
7336 | OverloadCandidateSet &CandidateSet, bool AllowObjCConversionOnExplicit, |
7337 | bool AllowExplicit, bool AllowResultConversion) { |
7338 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
7339 | "Only conversion function templates permitted here"); |
7340 | |
7341 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
7342 | return; |
7343 | |
7344 | |
7345 | |
7346 | |
7347 | if (!AllowExplicit && isNonDependentlyExplicit(FunctionTemplate)) { |
7348 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
7349 | Candidate.FoundDecl = FoundDecl; |
7350 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
7351 | Candidate.Viable = false; |
7352 | Candidate.FailureKind = ovl_fail_explicit; |
7353 | return; |
7354 | } |
7355 | |
7356 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
7357 | CXXConversionDecl *Specialization = nullptr; |
7358 | if (TemplateDeductionResult Result |
7359 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
7360 | Specialization, Info)) { |
7361 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
7362 | Candidate.FoundDecl = FoundDecl; |
7363 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
7364 | Candidate.Viable = false; |
7365 | Candidate.FailureKind = ovl_fail_bad_deduction; |
7366 | Candidate.IsSurrogate = false; |
7367 | Candidate.IgnoreObjectArgument = false; |
7368 | Candidate.ExplicitCallArguments = 1; |
7369 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
7370 | Info); |
7371 | return; |
7372 | } |
7373 | |
7374 | |
7375 | |
7376 | assert(Specialization && "Missing function template specialization?"); |
7377 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
7378 | CandidateSet, AllowObjCConversionOnExplicit, |
7379 | AllowExplicit, AllowResultConversion); |
7380 | } |
7381 | |
7382 | |
7383 | |
7384 | |
7385 | |
7386 | |
7387 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
7388 | DeclAccessPair FoundDecl, |
7389 | CXXRecordDecl *ActingContext, |
7390 | const FunctionProtoType *Proto, |
7391 | Expr *Object, |
7392 | ArrayRef<Expr *> Args, |
7393 | OverloadCandidateSet& CandidateSet) { |
7394 | if (!CandidateSet.isNewCandidate(Conversion)) |
7395 | return; |
7396 | |
7397 | |
7398 | EnterExpressionEvaluationContext Unevaluated( |
7399 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
7400 | |
7401 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
7402 | Candidate.FoundDecl = FoundDecl; |
7403 | Candidate.Function = nullptr; |
7404 | Candidate.Surrogate = Conversion; |
7405 | Candidate.Viable = true; |
7406 | Candidate.IsSurrogate = true; |
7407 | Candidate.IgnoreObjectArgument = false; |
7408 | Candidate.ExplicitCallArguments = Args.size(); |
7409 | |
7410 | |
7411 | |
7412 | ImplicitConversionSequence ObjectInit = TryObjectArgumentInitialization( |
7413 | *this, CandidateSet.getLocation(), Object->getType(), |
7414 | Object->Classify(Context), Conversion, ActingContext); |
7415 | if (ObjectInit.isBad()) { |
7416 | Candidate.Viable = false; |
7417 | Candidate.FailureKind = ovl_fail_bad_conversion; |
7418 | Candidate.Conversions[0] = ObjectInit; |
7419 | return; |
7420 | } |
7421 | |
7422 | |
7423 | |
7424 | |
7425 | Candidate.Conversions[0].setUserDefined(); |
7426 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
7427 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
7428 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
7429 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
7430 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
7431 | Candidate.Conversions[0].UserDefined.After |
7432 | = Candidate.Conversions[0].UserDefined.Before; |
7433 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
7434 | |
7435 | |
7436 | unsigned NumParams = Proto->getNumParams(); |
7437 | |
7438 | |
7439 | |
7440 | |
7441 | if (Args.size() > NumParams && !Proto->isVariadic()) { |
7442 | Candidate.Viable = false; |
7443 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
7444 | return; |
7445 | } |
7446 | |
7447 | |
7448 | |
7449 | if (Args.size() < NumParams) { |
7450 | |
7451 | Candidate.Viable = false; |
7452 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
7453 | return; |
7454 | } |
7455 | |
7456 | |
7457 | |
7458 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
7459 | if (ArgIdx < NumParams) { |
7460 | |
7461 | |
7462 | |
7463 | |
7464 | QualType ParamType = Proto->getParamType(ArgIdx); |
7465 | Candidate.Conversions[ArgIdx + 1] |
7466 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
7467 | false, |
7468 | false, |
7469 | |
7470 | getLangOpts().ObjCAutoRefCount); |
7471 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
7472 | Candidate.Viable = false; |
7473 | Candidate.FailureKind = ovl_fail_bad_conversion; |
7474 | return; |
7475 | } |
7476 | } else { |
7477 | |
7478 | |
7479 | |
7480 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
7481 | } |
7482 | } |
7483 | |
7484 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, None)) { |
7485 | Candidate.Viable = false; |
7486 | Candidate.FailureKind = ovl_fail_enable_if; |
7487 | Candidate.DeductionFailure.Data = FailedAttr; |
7488 | return; |
7489 | } |
7490 | } |
7491 | |
7492 | |
7493 | |
7494 | void Sema::AddNonMemberOperatorCandidates( |
7495 | const UnresolvedSetImpl &Fns, ArrayRef<Expr *> Args, |
7496 | OverloadCandidateSet &CandidateSet, |
7497 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
7498 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
7499 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
7500 | ArrayRef<Expr *> FunctionArgs = Args; |
7501 | |
7502 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
7503 | FunctionDecl *FD = |
7504 | FunTmpl ? FunTmpl->getTemplatedDecl() : cast<FunctionDecl>(D); |
7505 | |
7506 | |
7507 | if (!CandidateSet.getRewriteInfo().isAcceptableCandidate(FD)) |
7508 | continue; |
7509 | |
7510 | assert(!isa<CXXMethodDecl>(FD) && |
7511 | "unqualified operator lookup found a member function"); |
7512 | |
7513 | if (FunTmpl) { |
7514 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), ExplicitTemplateArgs, |
7515 | FunctionArgs, CandidateSet); |
7516 | if (CandidateSet.getRewriteInfo().shouldAddReversed(Context, FD)) |
7517 | AddTemplateOverloadCandidate( |
7518 | FunTmpl, F.getPair(), ExplicitTemplateArgs, |
7519 | {FunctionArgs[1], FunctionArgs[0]}, CandidateSet, false, false, |
7520 | true, ADLCallKind::NotADL, OverloadCandidateParamOrder::Reversed); |
7521 | } else { |
7522 | if (ExplicitTemplateArgs) |
7523 | continue; |
7524 | AddOverloadCandidate(FD, F.getPair(), FunctionArgs, CandidateSet); |
7525 | if (CandidateSet.getRewriteInfo().shouldAddReversed(Context, FD)) |
7526 | AddOverloadCandidate(FD, F.getPair(), |
7527 | {FunctionArgs[1], FunctionArgs[0]}, CandidateSet, |
7528 | false, false, true, false, ADLCallKind::NotADL, |
7529 | None, OverloadCandidateParamOrder::Reversed); |
7530 | } |
7531 | } |
7532 | } |
7533 | |
7534 | |
7535 | |
7536 | |
7537 | |
7538 | |
7539 | |
7540 | |
7541 | |
7542 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
7543 | SourceLocation OpLoc, |
7544 | ArrayRef<Expr *> Args, |
7545 | OverloadCandidateSet &CandidateSet, |
7546 | OverloadCandidateParamOrder PO) { |
7547 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
7548 | |
7549 | |
7550 | |
7551 | |
7552 | |
7553 | |
7554 | |
7555 | |
7556 | |
7557 | QualType T1 = Args[0]->getType(); |
7558 | |
7559 | |
7560 | |
7561 | |
7562 | |
7563 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
7564 | |
7565 | if (!isCompleteType(OpLoc, T1) && !T1Rec->isBeingDefined()) |
7566 | return; |
7567 | |
7568 | if (!T1Rec->getDecl()->getDefinition()) |
7569 | return; |
7570 | |
7571 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
7572 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
7573 | Operators.suppressDiagnostics(); |
7574 | |
7575 | for (LookupResult::iterator Oper = Operators.begin(), |
7576 | OperEnd = Operators.end(); |
7577 | Oper != OperEnd; |
7578 | ++Oper) |
7579 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
7580 | Args[0]->Classify(Context), Args.slice(1), |
7581 | CandidateSet, false, PO); |
7582 | } |
7583 | } |
7584 | |
7585 | |
7586 | |
7587 | |
7588 | |
7589 | |
7590 | |
7591 | |
7592 | |
7593 | void Sema::AddBuiltinCandidate(QualType *ParamTys, ArrayRef<Expr *> Args, |
7594 | OverloadCandidateSet& CandidateSet, |
7595 | bool IsAssignmentOperator, |
7596 | unsigned NumContextualBoolArguments) { |
7597 | |
7598 | EnterExpressionEvaluationContext Unevaluated( |
7599 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
7600 | |
7601 | |
7602 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
7603 | Candidate.FoundDecl = DeclAccessPair::make(nullptr, AS_none); |
7604 | Candidate.Function = nullptr; |
7605 | Candidate.IsSurrogate = false; |
7606 | Candidate.IgnoreObjectArgument = false; |
7607 | std::copy(ParamTys, ParamTys + Args.size(), Candidate.BuiltinParamTypes); |
7608 | |
7609 | |
7610 | |
7611 | Candidate.Viable = true; |
7612 | Candidate.ExplicitCallArguments = Args.size(); |
7613 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
7614 | |
7615 | |
7616 | |
7617 | |
7618 | |
7619 | |
7620 | |
7621 | |
7622 | |
7623 | |
7624 | |
7625 | |
7626 | if (ArgIdx < NumContextualBoolArguments) { |
7627 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
7628 | "Contextual conversion to bool requires bool type"); |
7629 | Candidate.Conversions[ArgIdx] |
7630 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
7631 | } else { |
7632 | Candidate.Conversions[ArgIdx] |
7633 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
7634 | ArgIdx == 0 && IsAssignmentOperator, |
7635 | false, |
7636 | |
7637 | getLangOpts().ObjCAutoRefCount); |
7638 | } |
7639 | if (Candidate.Conversions[ArgIdx].isBad()) { |
7640 | Candidate.Viable = false; |
7641 | Candidate.FailureKind = ovl_fail_bad_conversion; |
7642 | break; |
7643 | } |
7644 | } |
7645 | } |
7646 | |
7647 | namespace { |
7648 | |
7649 | |
7650 | |
7651 | |
7652 | |
7653 | class BuiltinCandidateTypeSet { |
7654 | |
7655 | typedef llvm::SetVector<QualType, SmallVector<QualType, 8>, |
7656 | llvm::SmallPtrSet<QualType, 8>> TypeSet; |
7657 | |
7658 | |
7659 | |
7660 | TypeSet PointerTypes; |
7661 | |
7662 | |
7663 | |
7664 | TypeSet MemberPointerTypes; |
7665 | |
7666 | |
7667 | |
7668 | TypeSet EnumerationTypes; |
7669 | |
7670 | |
7671 | |
7672 | TypeSet VectorTypes; |
7673 | |
7674 | |
7675 | bool HasNonRecordTypes; |
7676 | |
7677 | |
7678 | |
7679 | bool HasArithmeticOrEnumeralTypes; |
7680 | |
7681 | |
7682 | |
7683 | bool HasNullPtrType; |
7684 | |
7685 | |
7686 | |
7687 | Sema &SemaRef; |
7688 | |
7689 | |
7690 | ASTContext &Context; |
7691 | |
7692 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
7693 | const Qualifiers &VisibleQuals); |
7694 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
7695 | |
7696 | public: |
7697 | |
7698 | typedef TypeSet::iterator iterator; |
7699 | |
7700 | BuiltinCandidateTypeSet(Sema &SemaRef) |
7701 | : HasNonRecordTypes(false), |
7702 | HasArithmeticOrEnumeralTypes(false), |
7703 | HasNullPtrType(false), |
7704 | SemaRef(SemaRef), |
7705 | Context(SemaRef.Context) { } |
7706 | |
7707 | void AddTypesConvertedFrom(QualType Ty, |
7708 | SourceLocation Loc, |
7709 | bool AllowUserConversions, |
7710 | bool AllowExplicitConversions, |
7711 | const Qualifiers &VisibleTypeConversionsQuals); |
7712 | |
7713 | |
7714 | iterator pointer_begin() { return PointerTypes.begin(); } |
7715 | |
7716 | |
7717 | iterator pointer_end() { return PointerTypes.end(); } |
7718 | |
7719 | |
7720 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
7721 | |
7722 | |
7723 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
7724 | |
7725 | |
7726 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
7727 | |
7728 | |
7729 | iterator enumeration_end() { return EnumerationTypes.end(); } |
7730 | |
7731 | iterator vector_begin() { return VectorTypes.begin(); } |
7732 | iterator vector_end() { return VectorTypes.end(); } |
7733 | |
7734 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
7735 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
7736 | bool hasNullPtrType() const { return HasNullPtrType; } |
7737 | }; |
7738 | |
7739 | } |
7740 | |
7741 | |
7742 | |
7743 | |
7744 | |
7745 | |
7746 | |
7747 | |
7748 | |
7749 | |
7750 | bool |
7751 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
7752 | const Qualifiers &VisibleQuals) { |
7753 | |
7754 | |
7755 | if (!PointerTypes.insert(Ty)) |
7756 | return false; |
7757 | |
7758 | QualType PointeeTy; |
7759 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
7760 | bool buildObjCPtr = false; |
7761 | if (!PointerTy) { |
7762 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
7763 | PointeeTy = PTy->getPointeeType(); |
7764 | buildObjCPtr = true; |
7765 | } else { |
7766 | PointeeTy = PointerTy->getPointeeType(); |
7767 | } |
7768 | |
7769 | |
7770 | |
7771 | |
7772 | |
7773 | if (PointeeTy->isArrayType()) |
7774 | return true; |
7775 | |
7776 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
7777 | bool hasVolatile = VisibleQuals.hasVolatile(); |
7778 | bool hasRestrict = VisibleQuals.hasRestrict(); |
7779 | |
7780 | |
7781 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
7782 | if ((CVR | BaseCVR) != CVR) continue; |
7783 | |
7784 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
7785 | |
7786 | |
7787 | |
7788 | if ((CVR & Qualifiers::Restrict) && |
7789 | (!hasRestrict || |
7790 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
7791 | continue; |
7792 | |
7793 | |
7794 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
7795 | |
7796 | |
7797 | QualType QPointerTy; |
7798 | if (!buildObjCPtr) |
7799 | QPointerTy = Context.getPointerType(QPointeeTy); |
7800 | else |
7801 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
7802 | |
7803 | |
7804 | PointerTypes.insert(QPointerTy); |
7805 | } |
7806 | |
7807 | return true; |
7808 | } |
7809 | |
7810 | |
7811 | |
7812 | |
7813 | |
7814 | |
7815 | |
7816 | |
7817 | |
7818 | |
7819 | bool |
7820 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
7821 | QualType Ty) { |
7822 | |
7823 | if (!MemberPointerTypes.insert(Ty)) |
7824 | return false; |
7825 | |
7826 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
7827 | assert(PointerTy && "type was not a member pointer type!"); |
7828 | |
7829 | QualType PointeeTy = PointerTy->getPointeeType(); |
7830 | |
7831 | |
7832 | |
7833 | |
7834 | if (PointeeTy->isArrayType()) |
7835 | return true; |
7836 | const Type *ClassTy = PointerTy->getClass(); |
7837 | |
7838 | |
7839 | |
7840 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
7841 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
7842 | if ((CVR | BaseCVR) != CVR) continue; |
7843 | |
7844 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
7845 | MemberPointerTypes.insert( |
7846 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
7847 | } |
7848 | |
7849 | return true; |
7850 | } |
7851 | |
7852 | |
7853 | |
7854 | |
7855 | |
7856 | |
7857 | |
7858 | |
7859 | |
7860 | void |
7861 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
7862 | SourceLocation Loc, |
7863 | bool AllowUserConversions, |
7864 | bool AllowExplicitConversions, |
7865 | const Qualifiers &VisibleQuals) { |
7866 | |
7867 | Ty = Context.getCanonicalType(Ty); |
7868 | |
7869 | |
7870 | |
7871 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
7872 | Ty = RefTy->getPointeeType(); |
7873 | |
7874 | |
7875 | if (Ty->isArrayType()) |
7876 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
7877 | |
7878 | |
7879 | Ty = Ty.getLocalUnqualifiedType(); |
7880 | |
7881 | |
7882 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
7883 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
7884 | |
7885 | |
7886 | HasArithmeticOrEnumeralTypes = |
7887 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
7888 | |
7889 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
7890 | PointerTypes.insert(Ty); |
7891 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
7892 | |
7893 | |
7894 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
7895 | return; |
7896 | } else if (Ty->isMemberPointerType()) { |
7897 | |
7898 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
7899 | return; |
7900 | } else if (Ty->isEnumeralType()) { |
7901 | HasArithmeticOrEnumeralTypes = true; |
7902 | EnumerationTypes.insert(Ty); |
7903 | } else if (Ty->isVectorType()) { |
7904 | |
7905 | |
7906 | HasArithmeticOrEnumeralTypes = true; |
7907 | VectorTypes.insert(Ty); |
7908 | } else if (Ty->isNullPtrType()) { |
7909 | HasNullPtrType = true; |
7910 | } else if (AllowUserConversions && TyRec) { |
7911 | |
7912 | if (!SemaRef.isCompleteType(Loc, Ty)) |
7913 | return; |
7914 | |
7915 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
7916 | for (NamedDecl *D : ClassDecl->getVisibleConversionFunctions()) { |
7917 | if (isa<UsingShadowDecl>(D)) |
7918 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
7919 | |
7920 | |
7921 | |
7922 | if (isa<FunctionTemplateDecl>(D)) |
7923 | continue; |
7924 | |
7925 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
7926 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
7927 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
7928 | VisibleQuals); |
7929 | } |
7930 | } |
7931 | } |
7932 | } |
7933 | |
7934 | |
7935 | static QualType AdjustAddressSpaceForBuiltinOperandType(Sema &S, QualType T, |
7936 | Expr *Arg) { |
7937 | return S.Context.getAddrSpaceQualType(T, Arg->getType().getAddressSpace()); |
7938 | } |
7939 | |
7940 | |
7941 | |
7942 | |
7943 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
7944 | QualType T, |
7945 | ArrayRef<Expr *> Args, |
7946 | OverloadCandidateSet &CandidateSet) { |
7947 | QualType ParamTypes[2]; |
7948 | |
7949 | |
7950 | ParamTypes[0] = S.Context.getLValueReferenceType( |
7951 | AdjustAddressSpaceForBuiltinOperandType(S, T, Args[0])); |
7952 | ParamTypes[1] = T; |
7953 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
7954 | true); |
7955 | |
7956 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
7957 | |
7958 | ParamTypes[0] = S.Context.getLValueReferenceType( |
7959 | AdjustAddressSpaceForBuiltinOperandType(S, S.Context.getVolatileType(T), |
7960 | Args[0])); |
7961 | ParamTypes[1] = T; |
7962 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
7963 | true); |
7964 | } |
7965 | } |
7966 | |
7967 | |
7968 | |
7969 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
7970 | Qualifiers VRQuals; |
7971 | const RecordType *TyRec; |
7972 | if (const MemberPointerType *RHSMPType = |
7973 | ArgExpr->getType()->getAs<MemberPointerType>()) |
7974 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
7975 | else |
7976 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
7977 | if (!TyRec) { |
7978 | |
7979 | VRQuals.addVolatile(); |
7980 | VRQuals.addRestrict(); |
7981 | return VRQuals; |
7982 | } |
7983 | |
7984 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
7985 | if (!ClassDecl->hasDefinition()) |
7986 | return VRQuals; |
7987 | |
7988 | for (NamedDecl *D : ClassDecl->getVisibleConversionFunctions()) { |
7989 | if (isa<UsingShadowDecl>(D)) |
7990 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
7991 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
7992 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
7993 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
7994 | CanTy = ResTypeRef->getPointeeType(); |
7995 | |
7996 | |
7997 | bool done = false; |
7998 | while (!done) { |
7999 | if (CanTy.isRestrictQualified()) |
8000 | VRQuals.addRestrict(); |
8001 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
8002 | CanTy = ResTypePtr->getPointeeType(); |
8003 | else if (const MemberPointerType *ResTypeMPtr = |
8004 | CanTy->getAs<MemberPointerType>()) |
8005 | CanTy = ResTypeMPtr->getPointeeType(); |
8006 | else |
8007 | done = true; |
8008 | if (CanTy.isVolatileQualified()) |
8009 | VRQuals.addVolatile(); |
8010 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
8011 | return VRQuals; |
8012 | } |
8013 | } |
8014 | } |
8015 | return VRQuals; |
8016 | } |
8017 | |
8018 | namespace { |
8019 | |
8020 | |
8021 | |
8022 | |
8023 | |
8024 | class BuiltinOperatorOverloadBuilder { |
8025 | |
8026 | Sema &S; |
8027 | ArrayRef<Expr *> Args; |
8028 | Qualifiers VisibleTypeConversionsQuals; |
8029 | bool HasArithmeticOrEnumeralCandidateType; |
8030 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
8031 | OverloadCandidateSet &CandidateSet; |
8032 | |
8033 | static constexpr int ArithmeticTypesCap = 24; |
8034 | SmallVector<CanQualType, ArithmeticTypesCap> ArithmeticTypes; |
8035 | |
8036 | |
8037 | |
8038 | |
8039 | unsigned FirstIntegralType, |
8040 | LastIntegralType; |
8041 | unsigned FirstPromotedIntegralType, |
8042 | LastPromotedIntegralType; |
8043 | unsigned FirstPromotedArithmeticType, |
8044 | LastPromotedArithmeticType; |
8045 | unsigned NumArithmeticTypes; |
8046 | |
8047 | void InitArithmeticTypes() { |
8048 | |
8049 | FirstPromotedArithmeticType = 0; |
8050 | ArithmeticTypes.push_back(S.Context.FloatTy); |
8051 | ArithmeticTypes.push_back(S.Context.DoubleTy); |
8052 | ArithmeticTypes.push_back(S.Context.LongDoubleTy); |
8053 | if (S.Context.getTargetInfo().hasFloat128Type()) |
8054 | ArithmeticTypes.push_back(S.Context.Float128Ty); |
8055 | |
8056 | |
8057 | FirstIntegralType = ArithmeticTypes.size(); |
8058 | FirstPromotedIntegralType = ArithmeticTypes.size(); |
8059 | ArithmeticTypes.push_back(S.Context.IntTy); |
8060 | ArithmeticTypes.push_back(S.Context.LongTy); |
8061 | ArithmeticTypes.push_back(S.Context.LongLongTy); |
8062 | if (S.Context.getTargetInfo().hasInt128Type()) |
8063 | ArithmeticTypes.push_back(S.Context.Int128Ty); |
8064 | ArithmeticTypes.push_back(S.Context.UnsignedIntTy); |
8065 | ArithmeticTypes.push_back(S.Context.UnsignedLongTy); |
8066 | ArithmeticTypes.push_back(S.Context.UnsignedLongLongTy); |
8067 | if (S.Context.getTargetInfo().hasInt128Type()) |
8068 | ArithmeticTypes.push_back(S.Context.UnsignedInt128Ty); |
8069 | LastPromotedIntegralType = ArithmeticTypes.size(); |
8070 | LastPromotedArithmeticType = ArithmeticTypes.size(); |
8071 | |
8072 | |
8073 | ArithmeticTypes.push_back(S.Context.BoolTy); |
8074 | ArithmeticTypes.push_back(S.Context.CharTy); |
8075 | ArithmeticTypes.push_back(S.Context.WCharTy); |
8076 | if (S.Context.getLangOpts().Char8) |
8077 | ArithmeticTypes.push_back(S.Context.Char8Ty); |
8078 | ArithmeticTypes.push_back(S.Context.Char16Ty); |
8079 | ArithmeticTypes.push_back(S.Context.Char32Ty); |
8080 | ArithmeticTypes.push_back(S.Context.SignedCharTy); |
8081 | ArithmeticTypes.push_back(S.Context.ShortTy); |
8082 | ArithmeticTypes.push_back(S.Context.UnsignedCharTy); |
8083 | ArithmeticTypes.push_back(S.Context.UnsignedShortTy); |
8084 | LastIntegralType = ArithmeticTypes.size(); |
8085 | NumArithmeticTypes = ArithmeticTypes.size(); |
8086 | |
8087 | |
8088 | |
8089 | assert(ArithmeticTypes.size() <= ArithmeticTypesCap && |
8090 | "Enough inline storage for all arithmetic types."); |
8091 | } |
8092 | |
8093 | |
8094 | |
8095 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
8096 | bool HasVolatile, |
8097 | bool HasRestrict) { |
8098 | QualType ParamTypes[2] = { |
8099 | S.Context.getLValueReferenceType(CandidateTy), |
8100 | S.Context.IntTy |
8101 | }; |
8102 | |
8103 | |
8104 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8105 | |
8106 | |
8107 | |
8108 | if (HasVolatile) { |
8109 | ParamTypes[0] = |
8110 | S.Context.getLValueReferenceType( |
8111 | S.Context.getVolatileType(CandidateTy)); |
8112 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8113 | } |
8114 | |
8115 | |
8116 | |
8117 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
8118 | !CandidateTy.isRestrictQualified()) { |
8119 | ParamTypes[0] |
8120 | = S.Context.getLValueReferenceType( |
8121 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
8122 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8123 | |
8124 | if (HasVolatile) { |
8125 | ParamTypes[0] |
8126 | = S.Context.getLValueReferenceType( |
8127 | S.Context.getCVRQualifiedType(CandidateTy, |
8128 | (Qualifiers::Volatile | |
8129 | Qualifiers::Restrict))); |
8130 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8131 | } |
8132 | } |
8133 | |
8134 | } |
8135 | |
8136 | public: |
8137 | BuiltinOperatorOverloadBuilder( |
8138 | Sema &S, ArrayRef<Expr *> Args, |
8139 | Qualifiers VisibleTypeConversionsQuals, |
8140 | bool HasArithmeticOrEnumeralCandidateType, |
8141 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
8142 | OverloadCandidateSet &CandidateSet) |
8143 | : S(S), Args(Args), |
8144 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
8145 | HasArithmeticOrEnumeralCandidateType( |
8146 | HasArithmeticOrEnumeralCandidateType), |
8147 | CandidateTypes(CandidateTypes), |
8148 | CandidateSet(CandidateSet) { |
8149 | |
8150 | InitArithmeticTypes(); |
8151 | } |
8152 | |
8153 | |
8154 | |
8155 | |
8156 | |
8157 | |
8158 | |
8159 | |
8160 | |
8161 | |
8162 | |
8163 | |
8164 | |
8165 | |
8166 | |
8167 | |
8168 | |
8169 | |
8170 | |
8171 | |
8172 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
8173 | if (!HasArithmeticOrEnumeralCandidateType) |
8174 | return; |
8175 | |
8176 | for (unsigned Arith = 0; Arith < NumArithmeticTypes; ++Arith) { |
8177 | const auto TypeOfT = ArithmeticTypes[Arith]; |
8178 | if (TypeOfT == S.Context.BoolTy) { |
8179 | if (Op == OO_MinusMinus) |
8180 | continue; |
8181 | if (Op == OO_PlusPlus && S.getLangOpts().CPlusPlus17) |
8182 | continue; |
8183 | } |
8184 | addPlusPlusMinusMinusStyleOverloads( |
8185 | TypeOfT, |
8186 | VisibleTypeConversionsQuals.hasVolatile(), |
8187 | VisibleTypeConversionsQuals.hasRestrict()); |
8188 | } |
8189 | } |
8190 | |
8191 | |
8192 | |
8193 | |
8194 | |
8195 | |
8196 | |
8197 | |
8198 | |
8199 | |
8200 | |
8201 | void addPlusPlusMinusMinusPointerOverloads() { |
8202 | for (BuiltinCandidateTypeSet::iterator |
8203 | Ptr = CandidateTypes[0].pointer_begin(), |
8204 | PtrEnd = CandidateTypes[0].pointer_end(); |
8205 | Ptr != PtrEnd; ++Ptr) { |
8206 | |
8207 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
8208 | continue; |
8209 | |
8210 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
8211 | (!(*Ptr).isVolatileQualified() && |
8212 | VisibleTypeConversionsQuals.hasVolatile()), |
8213 | (!(*Ptr).isRestrictQualified() && |
8214 | VisibleTypeConversionsQuals.hasRestrict())); |
8215 | } |
8216 | } |
8217 | |
8218 | |
8219 | |
8220 | |
8221 | |
8222 | |
8223 | |
8224 | |
8225 | |
8226 | |
8227 | |
8228 | void addUnaryStarPointerOverloads() { |
8229 | for (BuiltinCandidateTypeSet::iterator |
8230 | Ptr = CandidateTypes[0].pointer_begin(), |
8231 | PtrEnd = CandidateTypes[0].pointer_end(); |
8232 | Ptr != PtrEnd; ++Ptr) { |
8233 | QualType ParamTy = *Ptr; |
8234 | QualType PointeeTy = ParamTy->getPointeeType(); |
8235 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
8236 | continue; |
8237 | |
8238 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
8239 | if (Proto->getMethodQuals() || Proto->getRefQualifier()) |
8240 | continue; |
8241 | |
8242 | S.AddBuiltinCandidate(&ParamTy, Args, CandidateSet); |
8243 | } |
8244 | } |
8245 | |
8246 | |
8247 | |
8248 | |
8249 | |
8250 | |
8251 | |
8252 | void addUnaryPlusOrMinusArithmeticOverloads() { |
8253 | if (!HasArithmeticOrEnumeralCandidateType) |
8254 | return; |
8255 | |
8256 | for (unsigned Arith = FirstPromotedArithmeticType; |
8257 | Arith < LastPromotedArithmeticType; ++Arith) { |
8258 | QualType ArithTy = ArithmeticTypes[Arith]; |
8259 | S.AddBuiltinCandidate(&ArithTy, Args, CandidateSet); |
8260 | } |
8261 | |
8262 | |
8263 | for (BuiltinCandidateTypeSet::iterator |
8264 | Vec = CandidateTypes[0].vector_begin(), |
8265 | VecEnd = CandidateTypes[0].vector_end(); |
8266 | Vec != VecEnd; ++Vec) { |
8267 | QualType VecTy = *Vec; |
8268 | S.AddBuiltinCandidate(&VecTy, Args, CandidateSet); |
8269 | } |
8270 | } |
8271 | |
8272 | |
8273 | |
8274 | |
8275 | |
8276 | |
8277 | void addUnaryPlusPointerOverloads() { |
8278 | for (BuiltinCandidateTypeSet::iterator |
8279 | Ptr = CandidateTypes[0].pointer_begin(), |
8280 | PtrEnd = CandidateTypes[0].pointer_end(); |
8281 | Ptr != PtrEnd; ++Ptr) { |
8282 | QualType ParamTy = *Ptr; |
8283 | S.AddBuiltinCandidate(&ParamTy, Args, CandidateSet); |
8284 | } |
8285 | } |
8286 | |
8287 | |
8288 | |
8289 | |
8290 | |
8291 | |
8292 | void addUnaryTildePromotedIntegralOverloads() { |
8293 | if (!HasArithmeticOrEnumeralCandidateType) |
8294 | return; |
8295 | |
8296 | for (unsigned Int = FirstPromotedIntegralType; |
8297 | Int < LastPromotedIntegralType; ++Int) { |
8298 | QualType IntTy = ArithmeticTypes[Int]; |
8299 | S.AddBuiltinCandidate(&IntTy, Args, CandidateSet); |
8300 | } |
8301 | |
8302 | |
8303 | for (BuiltinCandidateTypeSet::iterator |
8304 | Vec = CandidateTypes[0].vector_begin(), |
8305 | VecEnd = CandidateTypes[0].vector_end(); |
8306 | Vec != VecEnd; ++Vec) { |
8307 | QualType VecTy = *Vec; |
8308 | S.AddBuiltinCandidate(&VecTy, Args, CandidateSet); |
8309 | } |
8310 | } |
8311 | |
8312 | |
8313 | |
8314 | |
8315 | |
8316 | |
8317 | |
8318 | void addEqualEqualOrNotEqualMemberPointerOrNullptrOverloads() { |
8319 | |
8320 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
8321 | |
8322 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
8323 | for (BuiltinCandidateTypeSet::iterator |
8324 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
8325 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
8326 | MemPtr != MemPtrEnd; |
8327 | ++MemPtr) { |
8328 | |
8329 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
8330 | continue; |
8331 | |
8332 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
8333 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8334 | } |
8335 | |
8336 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
8337 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
8338 | if (AddedTypes.insert(NullPtrTy).second) { |
8339 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
8340 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8341 | } |
8342 | } |
8343 | } |
8344 | } |
8345 | |
8346 | |
8347 | |
8348 | |
8349 | |
8350 | |
8351 | |
8352 | |
8353 | |
8354 | |
8355 | |
8356 | |
8357 | |
8358 | void addGenericBinaryPointerOrEnumeralOverloads() { |
8359 | |
8360 | |
8361 | |
8362 | |
8363 | |
8364 | |
8365 | |
8366 | |
8367 | |
8368 | |
8369 | |
8370 | |
8371 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
8372 | UserDefinedBinaryOperators; |
8373 | |
8374 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
8375 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
8376 | CandidateTypes[ArgIdx].enumeration_end()) { |
8377 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
8378 | CEnd = CandidateSet.end(); |
8379 | C != CEnd; ++C) { |
8380 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
8381 | continue; |
8382 | |
8383 | if (C->Function->isFunctionTemplateSpecialization()) |
8384 | continue; |
8385 | |
8386 | |
8387 | |
8388 | |
8389 | bool Reversed = C->RewriteKind & CRK_Reversed; |
8390 | QualType FirstParamType = C->Function->getParamDecl(Reversed ? 1 : 0) |
8391 | ->getType() |
8392 | .getUnqualifiedType(); |
8393 | QualType SecondParamType = C->Function->getParamDecl(Reversed ? 0 : 1) |
8394 | ->getType() |
8395 | .getUnqualifiedType(); |
8396 | |
8397 | |
8398 | if (!FirstParamType->isEnumeralType() || |
8399 | !SecondParamType->isEnumeralType()) |
8400 | continue; |
8401 | |
8402 | |
8403 | UserDefinedBinaryOperators.insert( |
8404 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
8405 | S.Context.getCanonicalType(SecondParamType))); |
8406 | } |
8407 | } |
8408 | } |
8409 | |
8410 | |
8411 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
8412 | |
8413 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
8414 | for (BuiltinCandidateTypeSet::iterator |
8415 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
8416 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
8417 | Ptr != PtrEnd; ++Ptr) { |
8418 | |
8419 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
8420 | continue; |
8421 | |
8422 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
8423 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8424 | } |
8425 | for (BuiltinCandidateTypeSet::iterator |
8426 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
8427 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
8428 | Enum != EnumEnd; ++Enum) { |
8429 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
8430 | |
8431 | |
8432 | |
8433 | if (!AddedTypes.insert(CanonType).second || |
8434 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
8435 | CanonType))) |
8436 | continue; |
8437 | QualType ParamTypes[2] = { *Enum, *Enum }; |
8438 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8439 | } |
8440 | } |
8441 | } |
8442 | |
8443 | |
8444 | |
8445 | |
8446 | |
8447 | |
8448 | |
8449 | |
8450 | |
8451 | |
8452 | |
8453 | |
8454 | |
8455 | |
8456 | |
8457 | |
8458 | |
8459 | |
8460 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
8461 | |
8462 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
8463 | |
8464 | for (int Arg = 0; Arg < 2; ++Arg) { |
8465 | QualType AsymmetricParamTypes[2] = { |
8466 | S.Context.getPointerDiffType(), |
8467 | S.Context.getPointerDiffType(), |
8468 | }; |
8469 | for (BuiltinCandidateTypeSet::iterator |
8470 | Ptr = CandidateTypes[Arg].pointer_begin(), |
8471 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
8472 | Ptr != PtrEnd; ++Ptr) { |
8473 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
8474 | if (!PointeeTy->isObjectType()) |
8475 | continue; |
8476 | |
8477 | AsymmetricParamTypes[Arg] = *Ptr; |
8478 | if (Arg == 0 || Op == OO_Plus) { |
8479 | |
8480 | |
8481 | S.AddBuiltinCandidate(AsymmetricParamTypes, Args, CandidateSet); |
8482 | } |
8483 | if (Op == OO_Minus) { |
8484 | |
8485 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
8486 | continue; |
8487 | |
8488 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
8489 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8490 | } |
8491 | } |
8492 | } |
8493 | } |
8494 | |
8495 | |
8496 | |
8497 | |
8498 | |
8499 | |
8500 | |
8501 | |
8502 | |
8503 | |
8504 | |
8505 | |
8506 | |
8507 | |
8508 | |
8509 | |
8510 | |
8511 | |
8512 | |
8513 | |
8514 | |
8515 | |
8516 | |
8517 | |
8518 | |
8519 | |
8520 | |
8521 | |
8522 | |
8523 | |
8524 | void addGenericBinaryArithmeticOverloads() { |
8525 | if (!HasArithmeticOrEnumeralCandidateType) |
8526 | return; |
8527 | |
8528 | for (unsigned Left = FirstPromotedArithmeticType; |
8529 | Left < LastPromotedArithmeticType; ++Left) { |
8530 | for (unsigned Right = FirstPromotedArithmeticType; |
8531 | Right < LastPromotedArithmeticType; ++Right) { |
8532 | QualType LandR[2] = { ArithmeticTypes[Left], |
8533 | ArithmeticTypes[Right] }; |
8534 | S.AddBuiltinCandidate(LandR, Args, CandidateSet); |
8535 | } |
8536 | } |
8537 | |
8538 | |
8539 | |
8540 | for (BuiltinCandidateTypeSet::iterator |
8541 | Vec1 = CandidateTypes[0].vector_begin(), |
8542 | Vec1End = CandidateTypes[0].vector_end(); |
8543 | Vec1 != Vec1End; ++Vec1) { |
8544 | for (BuiltinCandidateTypeSet::iterator |
8545 | Vec2 = CandidateTypes[1].vector_begin(), |
8546 | Vec2End = CandidateTypes[1].vector_end(); |
8547 | Vec2 != Vec2End; ++Vec2) { |
8548 | QualType LandR[2] = { *Vec1, *Vec2 }; |
8549 | S.AddBuiltinCandidate(LandR, Args, CandidateSet); |
8550 | } |
8551 | } |
8552 | } |
8553 | |
8554 | |
8555 | |
8556 | |
8557 | |
8558 | |
8559 | |
8560 | |
8561 | |
8562 | |
8563 | |
8564 | |
8565 | |
8566 | |
8567 | |
8568 | |
8569 | |
8570 | |
8571 | |
8572 | |
8573 | |
8574 | |
8575 | |
8576 | |
8577 | |
8578 | |
8579 | |
8580 | |
8581 | |
8582 | |
8583 | |
8584 | |
8585 | void addThreeWayArithmeticOverloads() { |
8586 | addGenericBinaryArithmeticOverloads(); |
8587 | } |
8588 | |
8589 | |
8590 | |
8591 | |
8592 | |
8593 | |
8594 | |
8595 | |
8596 | |
8597 | |
8598 | |
8599 | |
8600 | |
8601 | |
8602 | |
8603 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
8604 | if (!HasArithmeticOrEnumeralCandidateType) |
8605 | return; |
8606 | |
8607 | for (unsigned Left = FirstPromotedIntegralType; |
8608 | Left < LastPromotedIntegralType; ++Left) { |
8609 | for (unsigned Right = FirstPromotedIntegralType; |
8610 | Right < LastPromotedIntegralType; ++Right) { |
8611 | QualType LandR[2] = { ArithmeticTypes[Left], |
8612 | ArithmeticTypes[Right] }; |
8613 | S.AddBuiltinCandidate(LandR, Args, CandidateSet); |
8614 | } |
8615 | } |
8616 | } |
8617 | |
8618 | |
8619 | |
8620 | |
8621 | |
8622 | |
8623 | |
8624 | |
8625 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
8626 | |
8627 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
8628 | |
8629 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
8630 | for (BuiltinCandidateTypeSet::iterator |
8631 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
8632 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
8633 | Enum != EnumEnd; ++Enum) { |
8634 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum)).second) |
8635 | continue; |
8636 | |
8637 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
8638 | } |
8639 | |
8640 | for (BuiltinCandidateTypeSet::iterator |
8641 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
8642 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
8643 | MemPtr != MemPtrEnd; ++MemPtr) { |
8644 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
8645 | continue; |
8646 | |
8647 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
8648 | } |
8649 | } |
8650 | } |
8651 | |
8652 | |
8653 | |
8654 | |
8655 | |
8656 | |
8657 | |
8658 | |
8659 | |
8660 | |
8661 | |
8662 | |
8663 | |
8664 | |
8665 | |
8666 | |
8667 | |
8668 | void addAssignmentPointerOverloads(bool isEqualOp) { |
8669 | |
8670 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
8671 | |
8672 | for (BuiltinCandidateTypeSet::iterator |
8673 | Ptr = CandidateTypes[0].pointer_begin(), |
8674 | PtrEnd = CandidateTypes[0].pointer_end(); |
8675 | Ptr != PtrEnd; ++Ptr) { |
8676 | |
8677 | if (isEqualOp) |
8678 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
8679 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
8680 | continue; |
8681 | |
8682 | |
8683 | QualType ParamTypes[2] = { |
8684 | S.Context.getLValueReferenceType(*Ptr), |
8685 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
8686 | }; |
8687 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8688 | isEqualOp); |
8689 | |
8690 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
8691 | VisibleTypeConversionsQuals.hasVolatile(); |
8692 | if (NeedVolatile) { |
8693 | |
8694 | ParamTypes[0] = |
8695 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
8696 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8697 | isEqualOp); |
8698 | } |
8699 | |
8700 | if (!(*Ptr).isRestrictQualified() && |
8701 | VisibleTypeConversionsQuals.hasRestrict()) { |
8702 | |
8703 | ParamTypes[0] |
8704 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
8705 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8706 | isEqualOp); |
8707 | |
8708 | if (NeedVolatile) { |
8709 | |
8710 | ParamTypes[0] |
8711 | = S.Context.getLValueReferenceType( |
8712 | S.Context.getCVRQualifiedType(*Ptr, |
8713 | (Qualifiers::Volatile | |
8714 | Qualifiers::Restrict))); |
8715 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8716 | isEqualOp); |
8717 | } |
8718 | } |
8719 | } |
8720 | |
8721 | if (isEqualOp) { |
8722 | for (BuiltinCandidateTypeSet::iterator |
8723 | Ptr = CandidateTypes[1].pointer_begin(), |
8724 | PtrEnd = CandidateTypes[1].pointer_end(); |
8725 | Ptr != PtrEnd; ++Ptr) { |
8726 | |
8727 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
8728 | continue; |
8729 | |
8730 | QualType ParamTypes[2] = { |
8731 | S.Context.getLValueReferenceType(*Ptr), |
8732 | *Ptr, |
8733 | }; |
8734 | |
8735 | |
8736 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8737 | true); |
8738 | |
8739 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
8740 | VisibleTypeConversionsQuals.hasVolatile(); |
8741 | if (NeedVolatile) { |
8742 | |
8743 | ParamTypes[0] = |
8744 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
8745 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8746 | true); |
8747 | } |
8748 | |
8749 | if (!(*Ptr).isRestrictQualified() && |
8750 | VisibleTypeConversionsQuals.hasRestrict()) { |
8751 | |
8752 | ParamTypes[0] |
8753 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
8754 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8755 | true); |
8756 | |
8757 | if (NeedVolatile) { |
8758 | |
8759 | ParamTypes[0] |
8760 | = S.Context.getLValueReferenceType( |
8761 | S.Context.getCVRQualifiedType(*Ptr, |
8762 | (Qualifiers::Volatile | |
8763 | Qualifiers::Restrict))); |
8764 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8765 | true); |
8766 | } |
8767 | } |
8768 | } |
8769 | } |
8770 | } |
8771 | |
8772 | |
8773 | |
8774 | |
8775 | |
8776 | |
8777 | |
8778 | |
8779 | |
8780 | |
8781 | |
8782 | |
8783 | |
8784 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
8785 | if (!HasArithmeticOrEnumeralCandidateType) |
8786 | return; |
8787 | |
8788 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
8789 | for (unsigned Right = FirstPromotedArithmeticType; |
8790 | Right < LastPromotedArithmeticType; ++Right) { |
8791 | QualType ParamTypes[2]; |
8792 | ParamTypes[1] = ArithmeticTypes[Right]; |
8793 | auto LeftBaseTy = AdjustAddressSpaceForBuiltinOperandType( |
8794 | S, ArithmeticTypes[Left], Args[0]); |
8795 | |
8796 | ParamTypes[0] = S.Context.getLValueReferenceType(LeftBaseTy); |
8797 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8798 | isEqualOp); |
8799 | |
8800 | |
8801 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
8802 | ParamTypes[0] = S.Context.getVolatileType(LeftBaseTy); |
8803 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
8804 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8805 | isEqualOp); |
8806 | } |
8807 | } |
8808 | } |
8809 | |
8810 | |
8811 | for (BuiltinCandidateTypeSet::iterator |
8812 | Vec1 = CandidateTypes[0].vector_begin(), |
8813 | Vec1End = CandidateTypes[0].vector_end(); |
8814 | Vec1 != Vec1End; ++Vec1) { |
8815 | for (BuiltinCandidateTypeSet::iterator |
8816 | Vec2 = CandidateTypes[1].vector_begin(), |
8817 | Vec2End = CandidateTypes[1].vector_end(); |
8818 | Vec2 != Vec2End; ++Vec2) { |
8819 | QualType ParamTypes[2]; |
8820 | ParamTypes[1] = *Vec2; |
8821 | |
8822 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
8823 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8824 | isEqualOp); |
8825 | |
8826 | |
8827 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
8828 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
8829 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
8830 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8831 | isEqualOp); |
8832 | } |
8833 | } |
8834 | } |
8835 | } |
8836 | |
8837 | |
8838 | |
8839 | |
8840 | |
8841 | |
8842 | |
8843 | |
8844 | |
8845 | |
8846 | |
8847 | |
8848 | |
8849 | void addAssignmentIntegralOverloads() { |
8850 | if (!HasArithmeticOrEnumeralCandidateType) |
8851 | return; |
8852 | |
8853 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
8854 | for (unsigned Right = FirstPromotedIntegralType; |
8855 | Right < LastPromotedIntegralType; ++Right) { |
8856 | QualType ParamTypes[2]; |
8857 | ParamTypes[1] = ArithmeticTypes[Right]; |
8858 | auto LeftBaseTy = AdjustAddressSpaceForBuiltinOperandType( |
8859 | S, ArithmeticTypes[Left], Args[0]); |
8860 | |
8861 | ParamTypes[0] = S.Context.getLValueReferenceType(LeftBaseTy); |
8862 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8863 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
8864 | |
8865 | ParamTypes[0] = LeftBaseTy; |
8866 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
8867 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
8868 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8869 | } |
8870 | } |
8871 | } |
8872 | } |
8873 | |
8874 | |
8875 | |
8876 | |
8877 | |
8878 | |
8879 | |
8880 | |
8881 | void addExclaimOverload() { |
8882 | QualType ParamTy = S.Context.BoolTy; |
8883 | S.AddBuiltinCandidate(&ParamTy, Args, CandidateSet, |
8884 | false, |
8885 | 1); |
8886 | } |
8887 | void addAmpAmpOrPipePipeOverload() { |
8888 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
8889 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
8890 | false, |
8891 | 2); |
8892 | } |
8893 | |
8894 | |
8895 | |
8896 | |
8897 | |
8898 | |
8899 | |
8900 | |
8901 | |
8902 | |
8903 | |
8904 | void addSubscriptOverloads() { |
8905 | for (BuiltinCandidateTypeSet::iterator |
8906 | Ptr = CandidateTypes[0].pointer_begin(), |
8907 | PtrEnd = CandidateTypes[0].pointer_end(); |
8908 | Ptr != PtrEnd; ++Ptr) { |
8909 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
8910 | QualType PointeeType = (*Ptr)->getPointeeType(); |
8911 | if (!PointeeType->isObjectType()) |
8912 | continue; |
8913 | |
8914 | |
8915 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8916 | } |
8917 | |
8918 | for (BuiltinCandidateTypeSet::iterator |
8919 | Ptr = CandidateTypes[1].pointer_begin(), |
8920 | PtrEnd = CandidateTypes[1].pointer_end(); |
8921 | Ptr != PtrEnd; ++Ptr) { |
8922 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
8923 | QualType PointeeType = (*Ptr)->getPointeeType(); |
8924 | if (!PointeeType->isObjectType()) |
8925 | continue; |
8926 | |
8927 | |
8928 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8929 | } |
8930 | } |
8931 | |
8932 | |
8933 | |
8934 | |
8935 | |
8936 | |
8937 | |
8938 | |
8939 | |
8940 | |
8941 | void addArrowStarOverloads() { |
8942 | for (BuiltinCandidateTypeSet::iterator |
8943 | Ptr = CandidateTypes[0].pointer_begin(), |
8944 | PtrEnd = CandidateTypes[0].pointer_end(); |
8945 | Ptr != PtrEnd; ++Ptr) { |
8946 | QualType C1Ty = (*Ptr); |
8947 | QualType C1; |
8948 | QualifierCollector Q1; |
8949 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
8950 | if (!isa<RecordType>(C1)) |
8951 | continue; |
8952 | |
8953 | |
8954 | |
8955 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
8956 | continue; |
8957 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
8958 | continue; |
8959 | for (BuiltinCandidateTypeSet::iterator |
8960 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
8961 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
8962 | MemPtr != MemPtrEnd; ++MemPtr) { |
8963 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
8964 | QualType C2 = QualType(mptr->getClass(), 0); |
8965 | C2 = C2.getUnqualifiedType(); |
8966 | if (C1 != C2 && !S.IsDerivedFrom(CandidateSet.getLocation(), C1, C2)) |
8967 | break; |
8968 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
8969 | |
8970 | QualType T = mptr->getPointeeType(); |
8971 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
8972 | T.isVolatileQualified()) |
8973 | continue; |
8974 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
8975 | T.isRestrictQualified()) |
8976 | continue; |
8977 | T = Q1.apply(S.Context, T); |
8978 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
8979 | } |
8980 | } |
8981 | } |
8982 | |
8983 | |
8984 | |
8985 | |
8986 | |
8987 | |
8988 | |
8989 | |
8990 | |
8991 | |
8992 | |
8993 | void addConditionalOperatorOverloads() { |
8994 | |
8995 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
8996 | |
8997 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
8998 | for (BuiltinCandidateTypeSet::iterator |
8999 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
9000 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
9001 | Ptr != PtrEnd; ++Ptr) { |
9002 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
9003 | continue; |
9004 | |
9005 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
9006 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
9007 | } |
9008 | |
9009 | for (BuiltinCandidateTypeSet::iterator |
9010 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
9011 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
9012 | MemPtr != MemPtrEnd; ++MemPtr) { |
9013 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
9014 | continue; |
9015 | |
9016 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
9017 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
9018 | } |
9019 | |
9020 | if (S.getLangOpts().CPlusPlus11) { |
9021 | for (BuiltinCandidateTypeSet::iterator |
9022 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
9023 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
9024 | Enum != EnumEnd; ++Enum) { |
9025 | if (!(*Enum)->castAs<EnumType>()->getDecl()->isScoped()) |
9026 | continue; |
9027 | |
9028 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum)).second) |
9029 | continue; |
9030 | |
9031 | QualType ParamTypes[2] = { *Enum, *Enum }; |
9032 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
9033 | } |
9034 | } |
9035 | } |
9036 | } |
9037 | }; |
9038 | |
9039 | } |
9040 | |
9041 | |
9042 | |
9043 | |
9044 | |
9045 | |
9046 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
9047 | SourceLocation OpLoc, |
9048 | ArrayRef<Expr *> Args, |
9049 | OverloadCandidateSet &CandidateSet) { |
9050 | |
9051 | |
9052 | |
9053 | |
9054 | Qualifiers VisibleTypeConversionsQuals; |
9055 | VisibleTypeConversionsQuals.addConst(); |
9056 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
9057 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
9058 | |
9059 | bool HasNonRecordCandidateType = false; |
9060 | bool HasArithmeticOrEnumeralCandidateType = false; |
9061 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
9062 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
9063 | CandidateTypes.emplace_back(*this); |
9064 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
9065 | OpLoc, |
9066 | true, |
9067 | (Op == OO_Exclaim || |
9068 | Op == OO_AmpAmp || |
9069 | Op == OO_PipePipe), |
9070 | VisibleTypeConversionsQuals); |
9071 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
9072 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
9073 | HasArithmeticOrEnumeralCandidateType = |
9074 | HasArithmeticOrEnumeralCandidateType || |
9075 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
9076 | } |
9077 | |
9078 | |
9079 | |
9080 | |
9081 | |
9082 | |
9083 | if (!HasNonRecordCandidateType && |
9084 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
9085 | return; |
9086 | |
9087 | |
9088 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
9089 | VisibleTypeConversionsQuals, |
9090 | HasArithmeticOrEnumeralCandidateType, |
9091 | CandidateTypes, CandidateSet); |
9092 | |
9093 | |
9094 | switch (Op) { |
9095 | case OO_None: |
9096 | case NUM_OVERLOADED_OPERATORS: |
9097 | llvm_unreachable("Expected an overloaded operator"); |
9098 | |
9099 | case OO_New: |
9100 | case OO_Delete: |
9101 | case OO_Array_New: |
9102 | case OO_Array_Delete: |
9103 | case OO_Call: |
9104 | llvm_unreachable( |
9105 | "Special operators don't use AddBuiltinOperatorCandidates"); |
9106 | |
9107 | case OO_Comma: |
9108 | case OO_Arrow: |
9109 | case OO_Coawait: |
9110 | |
9111 | |
9112 | |
9113 | |
9114 | break; |
9115 | |
9116 | case OO_Plus: |
9117 | if (Args.size() == 1) |
9118 | OpBuilder.addUnaryPlusPointerOverloads(); |
9119 | LLVM_FALLTHROUGH; |
9120 | |
9121 | case OO_Minus: |
9122 | if (Args.size() == 1) { |
9123 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
9124 | } else { |
9125 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
9126 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
9127 | } |
9128 | break; |
9129 | |
9130 | case OO_Star: |
9131 | if (Args.size() == 1) |
9132 | OpBuilder.addUnaryStarPointerOverloads(); |
9133 | else |
9134 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
9135 | break; |
9136 | |
9137 | case OO_Slash: |
9138 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
9139 | break; |
9140 | |
9141 | case OO_PlusPlus: |
9142 | case OO_MinusMinus: |
9143 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
9144 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
9145 | break; |
9146 | |
9147 | case OO_EqualEqual: |
9148 | case OO_ExclaimEqual: |
9149 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOrNullptrOverloads(); |
9150 | LLVM_FALLTHROUGH; |
9151 | |
9152 | case OO_Less: |
9153 | case OO_Greater: |
9154 | case OO_LessEqual: |
9155 | case OO_GreaterEqual: |
9156 | OpBuilder.addGenericBinaryPointerOrEnumeralOverloads(); |
9157 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
9158 | break; |
9159 | |
9160 | case OO_Spaceship: |
9161 | OpBuilder.addGenericBinaryPointerOrEnumeralOverloads(); |
9162 | OpBuilder.addThreeWayArithmeticOverloads(); |
9163 | break; |
9164 | |
9165 | case OO_Percent: |
9166 | case OO_Caret: |
9167 | case OO_Pipe: |
9168 | case OO_LessLess: |
9169 | case OO_GreaterGreater: |
9170 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
9171 | break; |
9172 | |
9173 | case OO_Amp: |
9174 | if (Args.size() == 1) |
9175 | |
9176 | |
9177 | |
9178 | break; |
9179 | |
9180 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
9181 | break; |
9182 | |
9183 | case OO_Tilde: |
9184 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
9185 | break; |
9186 | |
9187 | case OO_Equal: |
9188 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
9189 | LLVM_FALLTHROUGH; |
9190 | |
9191 | case OO_PlusEqual: |
9192 | case OO_MinusEqual: |
9193 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
9194 | LLVM_FALLTHROUGH; |
9195 | |
9196 | case OO_StarEqual: |
9197 | case OO_SlashEqual: |
9198 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
9199 | break; |
9200 | |
9201 | case OO_PercentEqual: |
9202 | case OO_LessLessEqual: |
9203 | case OO_GreaterGreaterEqual: |
9204 | case OO_AmpEqual: |
9205 | case OO_CaretEqual: |
9206 | case OO_PipeEqual: |
9207 | OpBuilder.addAssignmentIntegralOverloads(); |
9208 | break; |
9209 | |
9210 | case OO_Exclaim: |
9211 | OpBuilder.addExclaimOverload(); |
9212 | break; |
9213 | |
9214 | case OO_AmpAmp: |
9215 | case OO_PipePipe: |
9216 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
9217 | break; |
9218 | |
9219 | case OO_Subscript: |
9220 | OpBuilder.addSubscriptOverloads(); |
9221 | break; |
9222 | |
9223 | case OO_ArrowStar: |
9224 | OpBuilder.addArrowStarOverloads(); |
9225 | break; |
9226 | |
9227 | case OO_Conditional: |
9228 | OpBuilder.addConditionalOperatorOverloads(); |
9229 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
9230 | break; |
9231 | } |
9232 | } |
9233 | |
9234 | |
9235 | |
9236 | |
9237 | |
9238 | |
9239 | |
9240 | |
9241 | void |
9242 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
9243 | SourceLocation Loc, |
9244 | ArrayRef<Expr *> Args, |
9245 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
9246 | OverloadCandidateSet& CandidateSet, |
9247 | bool PartialOverloading) { |
9248 | ADLResult Fns; |
9249 | |
9250 | |
9251 | |
9252 | |
9253 | |
9254 | |
9255 | |
9256 | |
9257 | |
9258 | ArgumentDependentLookup(Name, Loc, Args, Fns); |
9259 | |
9260 | |
9261 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
9262 | CandEnd = CandidateSet.end(); |
9263 | Cand != CandEnd; ++Cand) |
9264 | if (Cand->Function) { |
9265 | Fns.erase(Cand->Function); |
9266 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
9267 | Fns.erase(FunTmpl); |
9268 | } |
9269 | |
9270 | |
9271 | |
9272 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
9273 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
9274 | |
9275 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
9276 | if (ExplicitTemplateArgs) |
9277 | continue; |
9278 | |
9279 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, |
9280 | false, PartialOverloading, |
9281 | true, |
9282 | false, |
9283 | ADLCallKind::UsesADL); |
9284 | } else { |
9285 | AddTemplateOverloadCandidate( |
9286 | cast<FunctionTemplateDecl>(*I), FoundDecl, ExplicitTemplateArgs, Args, |
9287 | CandidateSet, |
9288 | false, PartialOverloading, |
9289 | true, ADLCallKind::UsesADL); |
9290 | } |
9291 | } |
9292 | } |
9293 | |
9294 | namespace { |
9295 | enum class Comparison { Equal, Better, Worse }; |
9296 | } |
9297 | |
9298 | |
9299 | |
9300 | |
9301 | |
9302 | |
9303 | |
9304 | |
9305 | |
9306 | |
9307 | |
9308 | |
9309 | static Comparison compareEnableIfAttrs(const Sema &S, const FunctionDecl *Cand1, |
9310 | const FunctionDecl *Cand2) { |
9311 | |
9312 | bool Cand1Attr = Cand1->hasAttr<EnableIfAttr>(); |
9313 | bool Cand2Attr = Cand2->hasAttr<EnableIfAttr>(); |
9314 | if (!Cand1Attr || !Cand2Attr) { |
9315 | if (Cand1Attr == Cand2Attr) |
9316 | return Comparison::Equal; |
9317 | return Cand1Attr ? Comparison::Better : Comparison::Worse; |
9318 | } |
9319 | |
9320 | auto Cand1Attrs = Cand1->specific_attrs<EnableIfAttr>(); |
9321 | auto Cand2Attrs = Cand2->specific_attrs<EnableIfAttr>(); |
9322 | |
9323 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; |
9324 | for (auto Pair : zip_longest(Cand1Attrs, Cand2Attrs)) { |
9325 | Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair); |
9326 | Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair); |
9327 | |
9328 | |
9329 | |
9330 | if (!Cand1A) |
9331 | return Comparison::Worse; |
9332 | if (!Cand2A) |
9333 | return Comparison::Better; |
9334 | |
9335 | Cand1ID.clear(); |
9336 | Cand2ID.clear(); |
9337 | |
9338 | (*Cand1A)->getCond()->Profile(Cand1ID, S.getASTContext(), true); |
9339 | (*Cand2A)->getCond()->Profile(Cand2ID, S.getASTContext(), true); |
9340 | if (Cand1ID != Cand2ID) |
9341 | return Comparison::Worse; |
9342 | } |
9343 | |
9344 | return Comparison::Equal; |
9345 | } |
9346 | |
9347 | static bool isBetterMultiversionCandidate(const OverloadCandidate &Cand1, |
9348 | const OverloadCandidate &Cand2) { |
9349 | if (!Cand1.Function || !Cand1.Function->isMultiVersion() || !Cand2.Function || |
9350 | !Cand2.Function->isMultiVersion()) |
9351 | return false; |
9352 | |
9353 | |
9354 | |
9355 | if (Cand1.Function->isInvalidDecl()) return false; |
9356 | if (Cand2.Function->isInvalidDecl()) return true; |
9357 | |
9358 | |
9359 | |
9360 | bool Cand1CPUDisp = Cand1.Function->hasAttr<CPUDispatchAttr>(); |
9361 | bool Cand2CPUDisp = Cand2.Function->hasAttr<CPUDispatchAttr>(); |
9362 | const auto *Cand1CPUSpec = Cand1.Function->getAttr<CPUSpecificAttr>(); |
9363 | const auto *Cand2CPUSpec = Cand2.Function->getAttr<CPUSpecificAttr>(); |
9364 | |
9365 | if (!Cand1CPUDisp && !Cand2CPUDisp && !Cand1CPUSpec && !Cand2CPUSpec) |
9366 | return false; |
9367 | |
9368 | if (Cand1CPUDisp && !Cand2CPUDisp) |
9369 | return true; |
9370 | if (Cand2CPUDisp && !Cand1CPUDisp) |
9371 | return false; |
9372 | |
9373 | if (Cand1CPUSpec && Cand2CPUSpec) { |
9374 | if (Cand1CPUSpec->cpus_size() != Cand2CPUSpec->cpus_size()) |
9375 | return Cand1CPUSpec->cpus_size() < Cand2CPUSpec->cpus_size(); |
9376 | |
9377 | std::pair<CPUSpecificAttr::cpus_iterator, CPUSpecificAttr::cpus_iterator> |
9378 | FirstDiff = std::mismatch( |
9379 | Cand1CPUSpec->cpus_begin(), Cand1CPUSpec->cpus_end(), |
9380 | Cand2CPUSpec->cpus_begin(), |
9381 | [](const IdentifierInfo *LHS, const IdentifierInfo *RHS) { |
9382 | return LHS->getName() == RHS->getName(); |
9383 | }); |
9384 | |
9385 | assert(FirstDiff.first != Cand1CPUSpec->cpus_end() && |
9386 | "Two different cpu-specific versions should not have the same " |
9387 | "identifier list, otherwise they'd be the same decl!"); |
9388 | return (*FirstDiff.first)->getName() < (*FirstDiff.second)->getName(); |
9389 | } |
9390 | llvm_unreachable("No way to get here unless both had cpu_dispatch"); |
9391 | } |
9392 | |
9393 | |
9394 | |
9395 | bool clang::isBetterOverloadCandidate( |
9396 | Sema &S, const OverloadCandidate &Cand1, const OverloadCandidate &Cand2, |
9397 | SourceLocation Loc, OverloadCandidateSet::CandidateSetKind Kind) { |
9398 | |
9399 | |
9400 | if (!Cand2.Viable) |
9401 | return Cand1.Viable; |
9402 | else if (!Cand1.Viable) |
9403 | return false; |
9404 | |
9405 | |
9406 | |
9407 | |
9408 | |
9409 | |
9410 | |
9411 | unsigned StartArg = 0; |
9412 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
9413 | StartArg = 1; |
9414 | |
9415 | auto IsIllFormedConversion = [&](const ImplicitConversionSequence &ICS) { |
9416 | |
9417 | if (!S.getLangOpts().CPlusPlus) |
9418 | return ICS.isStandard() && |
9419 | ICS.Standard.Second == ICK_Incompatible_Pointer_Conversion; |
9420 | |
9421 | |
9422 | |
9423 | return S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
9424 | hasDeprecatedStringLiteralToCharPtrConversion(ICS); |
9425 | }; |
9426 | |
9427 | |
9428 | |
9429 | unsigned NumArgs = Cand1.Conversions.size(); |
9430 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
9431 | bool HasBetterConversion = false; |
9432 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
9433 | bool Cand1Bad = IsIllFormedConversion(Cand1.Conversions[ArgIdx]); |
9434 | bool Cand2Bad = IsIllFormedConversion(Cand2.Conversions[ArgIdx]); |
9435 | if (Cand1Bad != Cand2Bad) { |
9436 | if (Cand1Bad) |
9437 | return false; |
9438 | HasBetterConversion = true; |
9439 | } |
9440 | } |
9441 | |
9442 | if (HasBetterConversion) |
9443 | return true; |
9444 | |
9445 | |
9446 | |
9447 | |
9448 | |
9449 | bool HasWorseConversion = false; |
9450 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
9451 | switch (CompareImplicitConversionSequences(S, Loc, |
9452 | Cand1.Conversions[ArgIdx], |
9453 | Cand2.Conversions[ArgIdx])) { |
9454 | case ImplicitConversionSequence::Better: |
9455 | |
9456 | HasBetterConversion = true; |
9457 | break; |
9458 | |
9459 | case ImplicitConversionSequence::Worse: |
9460 | if (Cand1.Function && Cand1.Function == Cand2.Function && |
9461 | (Cand2.RewriteKind & CRK_Reversed) != 0) { |
9462 | |
9463 | |
9464 | |
9465 | |
9466 | |
9467 | |
9468 | |
9469 | |
9470 | |
9471 | |
9472 | |
9473 | |
9474 | HasWorseConversion = true; |
9475 | break; |
9476 | } |
9477 | |
9478 | |
9479 | return false; |
9480 | |
9481 | case ImplicitConversionSequence::Indistinguishable: |
9482 | |
9483 | break; |
9484 | } |
9485 | } |
9486 | |
9487 | |
9488 | |
9489 | if (HasBetterConversion) |
9490 | return true; |
9491 | if (HasWorseConversion) |
9492 | return false; |
9493 | |
9494 | |
9495 | |
9496 | |
9497 | |
9498 | |
9499 | |
9500 | if (Kind == OverloadCandidateSet::CSK_InitByUserDefinedConversion && |
9501 | Cand1.Function && Cand2.Function && |
9502 | isa<CXXConversionDecl>(Cand1.Function) && |
9503 | isa<CXXConversionDecl>(Cand2.Function)) { |
9504 | |
9505 | |
9506 | |
9507 | |
9508 | ImplicitConversionSequence::CompareKind Result = |
9509 | compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
9510 | if (Result == ImplicitConversionSequence::Indistinguishable) |
9511 | Result = CompareStandardConversionSequences(S, Loc, |
9512 | Cand1.FinalConversion, |
9513 | Cand2.FinalConversion); |
9514 | |
9515 | if (Result != ImplicitConversionSequence::Indistinguishable) |
9516 | return Result == ImplicitConversionSequence::Better; |
9517 | |
9518 | |
9519 | |
9520 | |
9521 | } |
9522 | |
9523 | |
9524 | |
9525 | |
9526 | |
9527 | |
9528 | |
9529 | if (Kind == OverloadCandidateSet::CSK_InitByConstructor && NumArgs == 1 && |
9530 | Cand1.Function && Cand2.Function && |
9531 | isa<CXXConstructorDecl>(Cand1.Function) != |
9532 | isa<CXXConstructorDecl>(Cand2.Function)) |
9533 | return isa<CXXConstructorDecl>(Cand1.Function); |
9534 | |
9535 | |
9536 | |
9537 | bool Cand1IsSpecialization = Cand1.Function && |
9538 | Cand1.Function->getPrimaryTemplate(); |
9539 | bool Cand2IsSpecialization = Cand2.Function && |
9540 | Cand2.Function->getPrimaryTemplate(); |
9541 | if (Cand1IsSpecialization != Cand2IsSpecialization) |
9542 | return Cand2IsSpecialization; |
9543 | |
9544 | |
9545 | |
9546 | |
9547 | |
9548 | if (Cand1IsSpecialization && Cand2IsSpecialization) { |
9549 | if (FunctionTemplateDecl *BetterTemplate |
9550 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
9551 | Cand2.Function->getPrimaryTemplate(), |
9552 | Loc, |
9553 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
9554 | : TPOC_Call, |
9555 | Cand1.ExplicitCallArguments, |
9556 | Cand2.ExplicitCallArguments)) |
9557 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
9558 | } |
9559 | |
9560 | |
9561 | |
9562 | if (Cand1.Function && Cand2.Function && !Cand1IsSpecialization && |
9563 | !Cand2IsSpecialization && Cand1.Function->hasPrototype() && |
9564 | Cand2.Function->hasPrototype()) { |
9565 | auto *PT1 = cast<FunctionProtoType>(Cand1.Function->getFunctionType()); |
9566 | auto *PT2 = cast<FunctionProtoType>(Cand2.Function->getFunctionType()); |
9567 | if (PT1->getNumParams() == PT2->getNumParams() && |
9568 | PT1->isVariadic() == PT2->isVariadic() && |
9569 | S.FunctionParamTypesAreEqual(PT1, PT2)) { |
9570 | Expr *RC1 = Cand1.Function->getTrailingRequiresClause(); |
9571 | Expr *RC2 = Cand2.Function->getTrailingRequiresClause(); |
9572 | if (RC1 && RC2) { |
9573 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
9574 | if (S.IsAtLeastAsConstrained(Cand1.Function, {RC1}, Cand2.Function, |
9575 | {RC2}, AtLeastAsConstrained1)) |
9576 | return false; |
9577 | if (!AtLeastAsConstrained1) |
9578 | return false; |
9579 | if (S.IsAtLeastAsConstrained(Cand2.Function, {RC2}, Cand1.Function, |
9580 | {RC1}, AtLeastAsConstrained2)) |
9581 | return false; |
9582 | if (!AtLeastAsConstrained2) |
9583 | return true; |
9584 | } else if (RC1 || RC2) |
9585 | return RC1 != nullptr; |
9586 | } |
9587 | } |
9588 | |
9589 | |
9590 | |
9591 | |
9592 | |
9593 | bool Cand1IsInherited = |
9594 | dyn_cast_or_null<ConstructorUsingShadowDecl>(Cand1.FoundDecl.getDecl()); |
9595 | bool Cand2IsInherited = |
9596 | dyn_cast_or_null<ConstructorUsingShadowDecl>(Cand2.FoundDecl.getDecl()); |
9597 | if (Cand1IsInherited != Cand2IsInherited) |
9598 | return Cand2IsInherited; |
9599 | else if (Cand1IsInherited) { |
9600 | assert(Cand2IsInherited); |
9601 | auto *Cand1Class = cast<CXXRecordDecl>(Cand1.Function->getDeclContext()); |
9602 | auto *Cand2Class = cast<CXXRecordDecl>(Cand2.Function->getDeclContext()); |
9603 | if (Cand1Class->isDerivedFrom(Cand2Class)) |
9604 | return true; |
9605 | if (Cand2Class->isDerivedFrom(Cand1Class)) |
9606 | return false; |
9607 | |
9608 | } |
9609 | |
9610 | |
9611 | |
9612 | |
9613 | |
9614 | |
9615 | |
9616 | |
9617 | if (Cand1.RewriteKind != Cand2.RewriteKind) |
9618 | return Cand1.RewriteKind < Cand2.RewriteKind; |
9619 | |
9620 | |
9621 | { |
9622 | auto *Guide1 = dyn_cast_or_null<CXXDeductionGuideDecl>(Cand1.Function); |
9623 | auto *Guide2 = dyn_cast_or_null<CXXDeductionGuideDecl>(Cand2.Function); |
9624 | if (Guide1 && Guide2) { |
9625 | |
9626 | if (Guide1->isImplicit() != Guide2->isImplicit()) |
9627 | return Guide2->isImplicit(); |
9628 | |
9629 | |
9630 | if (Guide1->isCopyDeductionCandidate()) |
9631 | return true; |
9632 | } |
9633 | } |
9634 | |
9635 | |
9636 | if (Cand1.Function && Cand2.Function) { |
9637 | Comparison Cmp = compareEnableIfAttrs(S, Cand1.Function, Cand2.Function); |
9638 | if (Cmp != Comparison::Equal) |
9639 | return Cmp == Comparison::Better; |
9640 | } |
9641 | |
9642 | if (S.getLangOpts().CUDA && Cand1.Function && Cand2.Function) { |
9643 | FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext); |
9644 | return S.IdentifyCUDAPreference(Caller, Cand1.Function) > |
9645 | S.IdentifyCUDAPreference(Caller, Cand2.Function); |
9646 | } |
9647 | |
9648 | bool HasPS1 = Cand1.Function != nullptr && |
9649 | functionHasPassObjectSizeParams(Cand1.Function); |
9650 | bool HasPS2 = Cand2.Function != nullptr && |
9651 | functionHasPassObjectSizeParams(Cand2.Function); |
9652 | if (HasPS1 != HasPS2 && HasPS1) |
9653 | return true; |
9654 | |
9655 | return isBetterMultiversionCandidate(Cand1, Cand2); |
9656 | } |
9657 | |
9658 | |
9659 | |
9660 | |
9661 | |
9662 | |
9663 | |
9664 | |
9665 | bool Sema::isEquivalentInternalLinkageDeclaration(const NamedDecl *A, |
9666 | const NamedDecl *B) { |
9667 | auto *VA = dyn_cast_or_null<ValueDecl>(A); |
9668 | auto *VB = dyn_cast_or_null<ValueDecl>(B); |
9669 | if (!VA || !VB) |
9670 | return false; |
9671 | |
9672 | |
9673 | |
9674 | if (!VA->getDeclContext()->getRedeclContext()->Equals( |
9675 | VB->getDeclContext()->getRedeclContext()) || |
9676 | getOwningModule(const_cast<ValueDecl *>(VA)) == |
9677 | getOwningModule(const_cast<ValueDecl *>(VB)) || |
9678 | VA->isExternallyVisible() || VB->isExternallyVisible()) |
9679 | return false; |
9680 | |
9681 | |
9682 | |
9683 | |
9684 | |
9685 | |
9686 | if (Context.hasSameType(VA->getType(), VB->getType())) |
9687 | return true; |
9688 | |
9689 | |
9690 | |
9691 | if (auto *EA = dyn_cast<EnumConstantDecl>(VA)) { |
9692 | if (auto *EB = dyn_cast<EnumConstantDecl>(VB)) { |
9693 | |
9694 | |
9695 | auto *EnumA = cast<EnumDecl>(EA->getDeclContext()); |
9696 | auto *EnumB = cast<EnumDecl>(EB->getDeclContext()); |
9697 | if (EnumA->hasNameForLinkage() || EnumB->hasNameForLinkage() || |
9698 | !Context.hasSameType(EnumA->getIntegerType(), |
9699 | EnumB->getIntegerType())) |
9700 | return false; |
9701 | |
9702 | return llvm::APSInt::isSameValue(EA->getInitVal(), EB->getInitVal()); |
9703 | } |
9704 | } |
9705 | |
9706 | |
9707 | return false; |
9708 | } |
9709 | |
9710 | void Sema::diagnoseEquivalentInternalLinkageDeclarations( |
9711 | SourceLocation Loc, const NamedDecl *D, ArrayRef<const NamedDecl *> Equiv) { |
9712 | Diag(Loc, diag::ext_equivalent_internal_linkage_decl_in_modules) << D; |
9713 | |
9714 | Module *M = getOwningModule(const_cast<NamedDecl*>(D)); |
9715 | Diag(D->getLocation(), diag::note_equivalent_internal_linkage_decl) |
9716 | << !M << (M ? M->getFullModuleName() : ""); |
9717 | |
9718 | for (auto *E : Equiv) { |
9719 | Module *M = getOwningModule(const_cast<NamedDecl*>(E)); |
9720 | Diag(E->getLocation(), diag::note_equivalent_internal_linkage_decl) |
9721 | << !M << (M ? M->getFullModuleName() : ""); |
9722 | } |
9723 | } |
9724 | |
9725 | |
9726 | |
9727 | |
9728 | |
9729 | |
9730 | |
9731 | |
9732 | |
9733 | |
9734 | |
9735 | OverloadingResult |
9736 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
9737 | iterator &Best) { |
9738 | llvm::SmallVector<OverloadCandidate *, 16> Candidates; |
9739 | std::transform(begin(), end(), std::back_inserter(Candidates), |
9740 | [](OverloadCandidate &Cand) { return &Cand; }); |
9741 | |
9742 | |
9743 | |
9744 | |
9745 | |
9746 | |
9747 | |
9748 | |
9749 | if (S.getLangOpts().CUDA) { |
9750 | const FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext); |
9751 | bool ContainsSameSideCandidate = |
9752 | llvm::any_of(Candidates, [&](OverloadCandidate *Cand) { |
9753 | |
9754 | return Cand->Viable && Cand->Function && |
9755 | S.IdentifyCUDAPreference(Caller, Cand->Function) == |
9756 | Sema::CFP_SameSide; |
9757 | }); |
9758 | if (ContainsSameSideCandidate) { |
9759 | auto IsWrongSideCandidate = [&](OverloadCandidate *Cand) { |
9760 | |
9761 | return Cand->Viable && Cand->Function && |
9762 | S.IdentifyCUDAPreference(Caller, Cand->Function) == |
9763 | Sema::CFP_WrongSide; |
9764 | }; |
9765 | llvm::erase_if(Candidates, IsWrongSideCandidate); |
9766 | } |
9767 | } |
9768 | |
9769 | |
9770 | Best = end(); |
9771 | for (auto *Cand : Candidates) { |
9772 | Cand->Best = false; |
9773 | if (Cand->Viable) |
9774 | if (Best == end() || |
9775 | isBetterOverloadCandidate(S, *Cand, *Best, Loc, Kind)) |
9776 | Best = Cand; |
9777 | } |
9778 | |
9779 | |
9780 | if (Best == end()) |
9781 | return OR_No_Viable_Function; |
9782 | |
9783 | llvm::SmallVector<const NamedDecl *, 4> EquivalentCands; |
9784 | |
9785 | llvm::SmallVector<OverloadCandidate*, 4> PendingBest; |
9786 | PendingBest.push_back(&*Best); |
9787 | Best->Best = true; |
9788 | |
9789 | |
9790 | |
9791 | while (!PendingBest.empty()) { |
9792 | auto *Curr = PendingBest.pop_back_val(); |
9793 | for (auto *Cand : Candidates) { |
9794 | if (Cand->Viable && !Cand->Best && |
9795 | !isBetterOverloadCandidate(S, *Curr, *Cand, Loc, Kind)) { |
9796 | PendingBest.push_back(Cand); |
9797 | Cand->Best = true; |
9798 | |
9799 | if (S.isEquivalentInternalLinkageDeclaration(Cand->Function, |
9800 | Curr->Function)) |
9801 | EquivalentCands.push_back(Cand->Function); |
9802 | else |
9803 | Best = end(); |
9804 | } |
9805 | } |
9806 | } |
9807 | |
9808 | |
9809 | if (Best == end()) |
9810 | return OR_Ambiguous; |
9811 | |
9812 | |
9813 | if (Best->Function && Best->Function->isDeleted()) |
9814 | return OR_Deleted; |
9815 | |
9816 | if (!EquivalentCands.empty()) |
9817 | S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function, |
9818 | EquivalentCands); |
9819 | |
9820 | return OR_Success; |
9821 | } |
9822 | |
9823 | namespace { |
9824 | |
9825 | enum OverloadCandidateKind { |
9826 | oc_function, |
9827 | oc_method, |
9828 | oc_reversed_binary_operator, |
9829 | oc_constructor, |
9830 | oc_implicit_default_constructor, |
9831 | oc_implicit_copy_constructor, |
9832 | oc_implicit_move_constructor, |
9833 | oc_implicit_copy_assignment, |
9834 | oc_implicit_move_assignment, |
9835 | oc_implicit_equality_comparison, |
9836 | oc_inherited_constructor |
9837 | }; |
9838 | |
9839 | enum OverloadCandidateSelect { |
9840 | ocs_non_template, |
9841 | ocs_template, |
9842 | ocs_described_template, |
9843 | }; |
9844 | |
9845 | static std::pair<OverloadCandidateKind, OverloadCandidateSelect> |
9846 | ClassifyOverloadCandidate(Sema &S, NamedDecl *Found, FunctionDecl *Fn, |
9847 | OverloadCandidateRewriteKind CRK, |
9848 | std::string &Description) { |
9849 | |
9850 | bool isTemplate = Fn->isTemplateDecl() || Found->isTemplateDecl(); |
9851 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
9852 | isTemplate = true; |
9853 | Description = S.getTemplateArgumentBindingsText( |
9854 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
9855 | } |
9856 | |
9857 | OverloadCandidateSelect Select = [&]() { |
9858 | if (!Description.empty()) |
9859 | return ocs_described_template; |
9860 | return isTemplate ? ocs_template : ocs_non_template; |
9861 | }(); |
9862 | |
9863 | OverloadCandidateKind Kind = [&]() { |
9864 | if (Fn->isImplicit() && Fn->getOverloadedOperator() == OO_EqualEqual) |
9865 | return oc_implicit_equality_comparison; |
9866 | |
9867 | if (CRK & CRK_Reversed) |
9868 | return oc_reversed_binary_operator; |
9869 | |
9870 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
9871 | if (!Ctor->isImplicit()) { |
9872 | if (isa<ConstructorUsingShadowDecl>(Found)) |
9873 | return oc_inherited_constructor; |
9874 | else |
9875 | return oc_constructor; |
9876 | } |
9877 | |
9878 | if (Ctor->isDefaultConstructor()) |
9879 | return oc_implicit_default_constructor; |
9880 | |
9881 | if (Ctor->isMoveConstructor()) |
9882 | return oc_implicit_move_constructor; |
9883 | |
9884 | assert(Ctor->isCopyConstructor() && |
9885 | "unexpected sort of implicit constructor"); |
9886 | return oc_implicit_copy_constructor; |
9887 | } |
9888 | |
9889 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
9890 | |
9891 | |
9892 | if (!Meth->isImplicit()) |
9893 | return oc_method; |
9894 | |
9895 | if (Meth->isMoveAssignmentOperator()) |
9896 | return oc_implicit_move_assignment; |
9897 | |
9898 | if (Meth->isCopyAssignmentOperator()) |
9899 | return oc_implicit_copy_assignment; |
9900 | |
9901 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
9902 | return oc_method; |
9903 | } |
9904 | |
9905 | return oc_function; |
9906 | }(); |
9907 | |
9908 | return std::make_pair(Kind, Select); |
9909 | } |
9910 | |
9911 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *FoundDecl) { |
9912 | |
9913 | |
9914 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl)) |
9915 | S.Diag(FoundDecl->getLocation(), |
9916 | diag::note_ovl_candidate_inherited_constructor) |
9917 | << Shadow->getNominatedBaseClass(); |
9918 | } |
9919 | |
9920 | } |
9921 | |
9922 | static bool isFunctionAlwaysEnabled(const ASTContext &Ctx, |
9923 | const FunctionDecl *FD) { |
9924 | for (auto *EnableIf : FD->specific_attrs<EnableIfAttr>()) { |
9925 | bool AlwaysTrue; |
9926 | if (EnableIf->getCond()->isValueDependent() || |
9927 | !EnableIf->getCond()->EvaluateAsBooleanCondition(AlwaysTrue, Ctx)) |
9928 | return false; |
9929 | if (!AlwaysTrue) |
9930 | return false; |
9931 | } |
9932 | return true; |
9933 | } |
9934 | |
9935 | |
9936 | |
9937 | |
9938 | |
9939 | |
9940 | |
9941 | |
9942 | static bool checkAddressOfFunctionIsAvailable(Sema &S, const FunctionDecl *FD, |
9943 | bool Complain, |
9944 | bool InOverloadResolution, |
9945 | SourceLocation Loc) { |
9946 | if (!isFunctionAlwaysEnabled(S.Context, FD)) { |
9947 | if (Complain) { |
9948 | if (InOverloadResolution) |
9949 | S.Diag(FD->getBeginLoc(), |
9950 | diag::note_addrof_ovl_candidate_disabled_by_enable_if_attr); |
9951 | else |
9952 | S.Diag(Loc, diag::err_addrof_function_disabled_by_enable_if_attr) << FD; |
9953 | } |
9954 | return false; |
9955 | } |
9956 | |
9957 | if (const Expr *RC = FD->getTrailingRequiresClause()) { |
9958 | ConstraintSatisfaction Satisfaction; |
9959 | if (S.CheckConstraintSatisfaction(RC, Satisfaction)) |
9960 | return false; |
9961 | if (!Satisfaction.IsSatisfied) { |
9962 | if (Complain) { |
9963 | if (InOverloadResolution) |
9964 | S.Diag(FD->getBeginLoc(), |
9965 | diag::note_ovl_candidate_unsatisfied_constraints); |
9966 | else |
9967 | S.Diag(Loc, diag::err_addrof_function_constraints_not_satisfied) |
9968 | << FD; |
9969 | S.DiagnoseUnsatisfiedConstraint(Satisfaction); |
9970 | } |
9971 | return false; |
9972 | } |
9973 | } |
9974 | |
9975 | auto I = llvm::find_if(FD->parameters(), [](const ParmVarDecl *P) { |
9976 | return P->hasAttr<PassObjectSizeAttr>(); |
9977 | }); |
9978 | if (I == FD->param_end()) |
9979 | return true; |
9980 | |
9981 | if (Complain) { |
9982 | |
9983 | unsigned ParamNo = std::distance(FD->param_begin(), I) + 1; |
9984 | if (InOverloadResolution) |
9985 | S.Diag(FD->getLocation(), |
9986 | diag::note_ovl_candidate_has_pass_object_size_params) |
9987 | << ParamNo; |
9988 | else |
9989 | S.Diag(Loc, diag::err_address_of_function_with_pass_object_size_params) |
9990 | << FD << ParamNo; |
9991 | } |
9992 | return false; |
9993 | } |
9994 | |
9995 | static bool checkAddressOfCandidateIsAvailable(Sema &S, |
9996 | const FunctionDecl *FD) { |
9997 | return checkAddressOfFunctionIsAvailable(S, FD, true, |
9998 | true, |
9999 | SourceLocation()); |
10000 | } |
10001 | |
10002 | bool Sema::checkAddressOfFunctionIsAvailable(const FunctionDecl *Function, |
10003 | bool Complain, |
10004 | SourceLocation Loc) { |
10005 | return ::checkAddressOfFunctionIsAvailable(*this, Function, Complain, |
10006 | false, |
10007 | Loc); |
10008 | } |
10009 | |
10010 | |
10011 | void Sema::NoteOverloadCandidate(NamedDecl *Found, FunctionDecl *Fn, |
10012 | OverloadCandidateRewriteKind RewriteKind, |
10013 | QualType DestType, bool TakingAddress) { |
10014 | if (TakingAddress && !checkAddressOfCandidateIsAvailable(*this, Fn)) |
10015 | return; |
10016 | if (Fn->isMultiVersion() && Fn->hasAttr<TargetAttr>() && |
10017 | !Fn->getAttr<TargetAttr>()->isDefaultVersion()) |
10018 | return; |
10019 | |
10020 | std::string FnDesc; |
10021 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> KSPair = |
10022 | ClassifyOverloadCandidate(*this, Found, Fn, RewriteKind, FnDesc); |
10023 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
10024 | << (unsigned)KSPair.first << (unsigned)KSPair.second |
10025 | << Fn << FnDesc; |
10026 | |
10027 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
10028 | Diag(Fn->getLocation(), PD); |
10029 | MaybeEmitInheritedConstructorNote(*this, Found); |
10030 | } |
10031 | |
10032 | static void |
10033 | MaybeDiagnoseAmbiguousConstraints(Sema &S, ArrayRef<OverloadCandidate> Cands) { |
10034 | |
10035 | |
10036 | |
10037 | |
10038 | |
10039 | |
10040 | |
10041 | |
10042 | |
10043 | |
10044 | |
10045 | |
10046 | SmallVector<const Expr *, 3> FirstAC, SecondAC; |
10047 | FunctionDecl *FirstCand = nullptr, *SecondCand = nullptr; |
10048 | for (auto I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
10049 | if (!I->Function) |
10050 | continue; |
10051 | SmallVector<const Expr *, 3> AC; |
10052 | if (auto *Template = I->Function->getPrimaryTemplate()) |
10053 | Template->getAssociatedConstraints(AC); |
10054 | else |
10055 | I->Function->getAssociatedConstraints(AC); |
10056 | if (AC.empty()) |
10057 | continue; |
10058 | if (FirstCand == nullptr) { |
10059 | FirstCand = I->Function; |
10060 | FirstAC = AC; |
10061 | } else if (SecondCand == nullptr) { |
10062 | SecondCand = I->Function; |
10063 | SecondAC = AC; |
10064 | } else { |
10065 | |
10066 | |
10067 | return; |
10068 | } |
10069 | } |
10070 | if (!SecondCand) |
10071 | return; |
10072 | |
10073 | |
10074 | if (S.MaybeEmitAmbiguousAtomicConstraintsDiagnostic(FirstCand, FirstAC, |
10075 | SecondCand, SecondAC)) |
10076 | |
10077 | |
10078 | return; |
10079 | } |
10080 | |
10081 | |
10082 | |
10083 | void Sema::NoteAllOverloadCandidates(Expr *OverloadedExpr, QualType DestType, |
10084 | bool TakingAddress) { |
10085 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
10086 | |
10087 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
10088 | OverloadExpr *OvlExpr = Ovl.Expression; |
10089 | |
10090 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
10091 | IEnd = OvlExpr->decls_end(); |
10092 | I != IEnd; ++I) { |
10093 | if (FunctionTemplateDecl *FunTmpl = |
10094 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
10095 | NoteOverloadCandidate(*I, FunTmpl->getTemplatedDecl(), CRK_None, DestType, |
10096 | TakingAddress); |
10097 | } else if (FunctionDecl *Fun |
10098 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
10099 | NoteOverloadCandidate(*I, Fun, CRK_None, DestType, TakingAddress); |
10100 | } |
10101 | } |
10102 | } |
10103 | |
10104 | |
10105 | |
10106 | |
10107 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
10108 | Sema &S, |
10109 | SourceLocation CaretLoc, |
10110 | const PartialDiagnostic &PDiag) const { |
10111 | S.Diag(CaretLoc, PDiag) |
10112 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
10113 | |
10114 | |
10115 | |
10116 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
10117 | unsigned CandsShown = 0; |
10118 | AmbiguousConversionSequence::const_iterator I, E; |
10119 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
10120 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
10121 | break; |
10122 | ++CandsShown; |
10123 | S.NoteOverloadCandidate(I->first, I->second); |
10124 | } |
10125 | if (I != E) |
10126 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
10127 | } |
10128 | |
10129 | static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, |
10130 | unsigned I, bool TakingCandidateAddress) { |
10131 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
10132 | assert(Conv.isBad()); |
10133 | assert(Cand->Function && "for now, candidate must be a function"); |
10134 | FunctionDecl *Fn = Cand->Function; |
10135 | |
10136 | |
10137 | |
10138 | |
10139 | bool isObjectArgument = false; |
10140 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
10141 | if (I == 0) |
10142 | isObjectArgument = true; |
10143 | else |
10144 | I--; |
10145 | } |
10146 | |
10147 | std::string FnDesc; |
10148 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
10149 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, Cand->getRewriteKind(), |
10150 | FnDesc); |
10151 | |
10152 | Expr *FromExpr = Conv.Bad.FromExpr; |
10153 | QualType FromTy = Conv.Bad.getFromType(); |
10154 | QualType ToTy = Conv.Bad.getToType(); |
10155 | |
10156 | if (FromTy == S.Context.OverloadTy) { |
10157 | assert(FromExpr && "overload set argument came from implicit argument?"); |
10158 | Expr *E = FromExpr->IgnoreParens(); |
10159 | if (isa<UnaryOperator>(E)) |
10160 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
10161 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
10162 | |
10163 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
10164 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10165 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << ToTy |
10166 | << Name << I + 1; |
10167 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10168 | return; |
10169 | } |
10170 | |
10171 | |
10172 | |
10173 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
10174 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
10175 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
10176 | CToTy = RT->getPointeeType(); |
10177 | else { |
10178 | |
10179 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
10180 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) { |
10181 | CFromTy = FromPT->getPointeeType(); |
10182 | CToTy = ToPT->getPointeeType(); |
10183 | } |
10184 | } |
10185 | |
10186 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
10187 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
10188 | Qualifiers FromQs = CFromTy.getQualifiers(); |
10189 | Qualifiers ToQs = CToTy.getQualifiers(); |
10190 | |
10191 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
10192 | if (isObjectArgument) |
10193 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace_this) |
10194 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
10195 | << FnDesc << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
10196 | << FromQs.getAddressSpace() << ToQs.getAddressSpace(); |
10197 | else |
10198 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
10199 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
10200 | << FnDesc << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
10201 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
10202 | << ToTy->isReferenceType() << I + 1; |
10203 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10204 | return; |
10205 | } |
10206 | |
10207 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
10208 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
10209 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10210 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10211 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
10212 | << (unsigned)isObjectArgument << I + 1; |
10213 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10214 | return; |
10215 | } |
10216 | |
10217 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
10218 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
10219 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10220 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10221 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
10222 | << (unsigned)isObjectArgument << I + 1; |
10223 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10224 | return; |
10225 | } |
10226 | |
10227 | if (FromQs.hasUnaligned() != ToQs.hasUnaligned()) { |
10228 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_unaligned) |
10229 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10230 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10231 | << FromQs.hasUnaligned() << I + 1; |
10232 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10233 | return; |
10234 | } |
10235 | |
10236 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
10237 | assert(CVR && "unexpected qualifiers mismatch"); |
10238 | |
10239 | if (isObjectArgument) { |
10240 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
10241 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10242 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10243 | << (CVR - 1); |
10244 | } else { |
10245 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
10246 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10247 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10248 | << (CVR - 1) << I + 1; |
10249 | } |
10250 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10251 | return; |
10252 | } |
10253 | |
10254 | |
10255 | |
10256 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
10257 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
10258 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10259 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10260 | << ToTy << (unsigned)isObjectArgument << I + 1; |
10261 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10262 | return; |
10263 | } |
10264 | |
10265 | |
10266 | |
10267 | |
10268 | QualType TempFromTy = FromTy.getNonReferenceType(); |
10269 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
10270 | TempFromTy = PTy->getPointeeType(); |
10271 | if (TempFromTy->isIncompleteType()) { |
10272 | |
10273 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
10274 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10275 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10276 | << ToTy << (unsigned)isObjectArgument << I + 1 |
10277 | << (unsigned)(Cand->Fix.Kind); |
10278 | |
10279 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10280 | return; |
10281 | } |
10282 | |
10283 | |
10284 | unsigned BaseToDerivedConversion = 0; |
10285 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
10286 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
10287 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
10288 | FromPtrTy->getPointeeType()) && |
10289 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
10290 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
10291 | S.IsDerivedFrom(SourceLocation(), ToPtrTy->getPointeeType(), |
10292 | FromPtrTy->getPointeeType())) |
10293 | BaseToDerivedConversion = 1; |
10294 | } |
10295 | } else if (const ObjCObjectPointerType *FromPtrTy |
10296 | = FromTy->getAs<ObjCObjectPointerType>()) { |
10297 | if (const ObjCObjectPointerType *ToPtrTy |
10298 | = ToTy->getAs<ObjCObjectPointerType>()) |
10299 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
10300 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
10301 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
10302 | FromPtrTy->getPointeeType()) && |
10303 | FromIface->isSuperClassOf(ToIface)) |
10304 | BaseToDerivedConversion = 2; |
10305 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
10306 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
10307 | !FromTy->isIncompleteType() && |
10308 | !ToRefTy->getPointeeType()->isIncompleteType() && |
10309 | S.IsDerivedFrom(SourceLocation(), ToRefTy->getPointeeType(), FromTy)) { |
10310 | BaseToDerivedConversion = 3; |
10311 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
10312 | ToTy.getNonReferenceType().getCanonicalType() == |
10313 | FromTy.getNonReferenceType().getCanonicalType()) { |
10314 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
10315 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10316 | << (unsigned)isObjectArgument << I + 1 |
10317 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()); |
10318 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10319 | return; |
10320 | } |
10321 | } |
10322 | |
10323 | if (BaseToDerivedConversion) { |
10324 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_base_to_derived_conv) |
10325 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10326 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
10327 | << (BaseToDerivedConversion - 1) << FromTy << ToTy << I + 1; |
10328 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10329 | return; |
10330 | } |
10331 | |
10332 | if (isa<ObjCObjectPointerType>(CFromTy) && |
10333 | isa<PointerType>(CToTy)) { |
10334 | Qualifiers FromQs = CFromTy.getQualifiers(); |
10335 | Qualifiers ToQs = CToTy.getQualifiers(); |
10336 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
10337 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
10338 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
10339 | << FnDesc << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
10340 | << FromTy << ToTy << (unsigned)isObjectArgument << I + 1; |
10341 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10342 | return; |
10343 | } |
10344 | } |
10345 | |
10346 | if (TakingCandidateAddress && |
10347 | !checkAddressOfCandidateIsAvailable(S, Cand->Function)) |
10348 | return; |
10349 | |
10350 | |
10351 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
10352 | FDiag << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10353 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
10354 | << ToTy << (unsigned)isObjectArgument << I + 1 |
10355 | << (unsigned)(Cand->Fix.Kind); |
10356 | |
10357 | |
10358 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
10359 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
10360 | FDiag << *HI; |
10361 | S.Diag(Fn->getLocation(), FDiag); |
10362 | |
10363 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10364 | } |
10365 | |
10366 | |
10367 | |
10368 | |
10369 | static bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
10370 | unsigned NumArgs) { |
10371 | FunctionDecl *Fn = Cand->Function; |
10372 | unsigned MinParams = Fn->getMinRequiredArguments(); |
10373 | |
10374 | |
10375 | |
10376 | |
10377 | |
10378 | |
10379 | if (Fn->isInvalidDecl() && |
10380 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
10381 | return true; |
10382 | |
10383 | if (NumArgs < MinParams) { |
10384 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
10385 | (Cand->FailureKind == ovl_fail_bad_deduction && |
10386 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
10387 | } else { |
10388 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
10389 | (Cand->FailureKind == ovl_fail_bad_deduction && |
10390 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
10391 | } |
10392 | |
10393 | return false; |
10394 | } |
10395 | |
10396 | |
10397 | static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D, |
10398 | unsigned NumFormalArgs) { |
10399 | assert(isa<FunctionDecl>(D) && |
10400 | "The templated declaration should at least be a function" |
10401 | " when diagnosing bad template argument deduction due to too many" |
10402 | " or too few arguments"); |
10403 | |
10404 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
10405 | |
10406 | |
10407 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
10408 | unsigned MinParams = Fn->getMinRequiredArguments(); |
10409 | |
10410 | |
10411 | unsigned mode, modeCount; |
10412 | if (NumFormalArgs < MinParams) { |
10413 | if (MinParams != FnTy->getNumParams() || FnTy->isVariadic() || |
10414 | FnTy->isTemplateVariadic()) |
10415 | mode = 0; |
10416 | else |
10417 | mode = 2; |
10418 | modeCount = MinParams; |
10419 | } else { |
10420 | if (MinParams != FnTy->getNumParams()) |
10421 | mode = 1; |
10422 | else |
10423 | mode = 2; |
10424 | modeCount = FnTy->getNumParams(); |
10425 | } |
10426 | |
10427 | std::string Description; |
10428 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
10429 | ClassifyOverloadCandidate(S, Found, Fn, CRK_None, Description); |
10430 | |
10431 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
10432 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
10433 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
10434 | << Description << mode << Fn->getParamDecl(0) << NumFormalArgs; |
10435 | else |
10436 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
10437 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
10438 | << Description << mode << modeCount << NumFormalArgs; |
10439 | |
10440 | MaybeEmitInheritedConstructorNote(S, Found); |
10441 | } |
10442 | |
10443 | |
10444 | static void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
10445 | unsigned NumFormalArgs) { |
10446 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
10447 | DiagnoseArityMismatch(S, Cand->FoundDecl, Cand->Function, NumFormalArgs); |
10448 | } |
10449 | |
10450 | static TemplateDecl *getDescribedTemplate(Decl *Templated) { |
10451 | if (TemplateDecl *TD = Templated->getDescribedTemplate()) |
10452 | return TD; |
10453 | llvm_unreachable("Unsupported: Getting the described template declaration" |
10454 | " for bad deduction diagnosis"); |
10455 | } |
10456 | |
10457 | |
10458 | static void DiagnoseBadDeduction(Sema &S, NamedDecl *Found, Decl *Templated, |
10459 | DeductionFailureInfo &DeductionFailure, |
10460 | unsigned NumArgs, |
10461 | bool TakingCandidateAddress) { |
10462 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
10463 | NamedDecl *ParamD; |
10464 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
10465 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
10466 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
10467 | switch (DeductionFailure.Result) { |
10468 | case Sema::TDK_Success: |
10469 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
10470 | |
10471 | case Sema::TDK_Incomplete: { |
10472 | assert(ParamD && "no parameter found for incomplete deduction result"); |
10473 | S.Diag(Templated->getLocation(), |
10474 | diag::note_ovl_candidate_incomplete_deduction) |
10475 | << ParamD->getDeclName(); |
10476 | MaybeEmitInheritedConstructorNote(S, Found); |
10477 | return; |
10478 | } |
10479 | |
10480 | case Sema::TDK_IncompletePack: { |
10481 | assert(ParamD && "no parameter found for incomplete deduction result"); |
10482 | S.Diag(Templated->getLocation(), |
10483 | diag::note_ovl_candidate_incomplete_deduction_pack) |
10484 | << ParamD->getDeclName() |
10485 | << (DeductionFailure.getFirstArg()->pack_size() + 1) |
10486 | << *DeductionFailure.getFirstArg(); |
10487 | MaybeEmitInheritedConstructorNote(S, Found); |
10488 | return; |
10489 | } |
10490 | |
10491 | case Sema::TDK_Underqualified: { |
10492 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
10493 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
10494 | |
10495 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
10496 | |
10497 | |
10498 | |
10499 | QualifierCollector Qs; |
10500 | Qs.strip(Param); |
10501 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
10502 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
10503 | |
10504 | |
10505 | |
10506 | |
10507 | |
10508 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
10509 | |
10510 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
10511 | << ParamD->getDeclName() << Arg << NonCanonParam; |
10512 | MaybeEmitInheritedConstructorNote(S, Found); |
10513 | return; |
10514 | } |
10515 | |
10516 | case Sema::TDK_Inconsistent: { |
10517 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
10518 | int which = 0; |
10519 | if (isa<TemplateTypeParmDecl>(ParamD)) |
10520 | which = 0; |
10521 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) { |
10522 | |
10523 | |
10524 | |
10525 | QualType T1 = |
10526 | DeductionFailure.getFirstArg()->getNonTypeTemplateArgumentType(); |
10527 | QualType T2 = |
10528 | DeductionFailure.getSecondArg()->getNonTypeTemplateArgumentType(); |
10529 | if (!T1.isNull() && !T2.isNull() && !S.Context.hasSameType(T1, T2)) { |
10530 | S.Diag(Templated->getLocation(), |
10531 | diag::note_ovl_candidate_inconsistent_deduction_types) |
10532 | << ParamD->getDeclName() << *DeductionFailure.getFirstArg() << T1 |
10533 | << *DeductionFailure.getSecondArg() << T2; |
10534 | MaybeEmitInheritedConstructorNote(S, Found); |
10535 | return; |
10536 | } |
10537 | |
10538 | which = 1; |
10539 | } else { |
10540 | which = 2; |
10541 | } |
10542 | |
10543 | |
10544 | |
10545 | |
10546 | if (DeductionFailure.getFirstArg()->getKind() == TemplateArgument::Pack && |
10547 | DeductionFailure.getSecondArg()->getKind() == TemplateArgument::Pack && |
10548 | DeductionFailure.getFirstArg()->pack_size() != |
10549 | DeductionFailure.getSecondArg()->pack_size()) { |
10550 | which = 3; |
10551 | } |
10552 | |
10553 | S.Diag(Templated->getLocation(), |
10554 | diag::note_ovl_candidate_inconsistent_deduction) |
10555 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
10556 | << *DeductionFailure.getSecondArg(); |
10557 | MaybeEmitInheritedConstructorNote(S, Found); |
10558 | return; |
10559 | } |
10560 | |
10561 | case Sema::TDK_InvalidExplicitArguments: |
10562 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
10563 | if (ParamD->getDeclName()) |
10564 | S.Diag(Templated->getLocation(), |
10565 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
10566 | << ParamD->getDeclName(); |
10567 | else { |
10568 | int index = 0; |
10569 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
10570 | index = TTP->getIndex(); |
10571 | else if (NonTypeTemplateParmDecl *NTTP |
10572 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
10573 | index = NTTP->getIndex(); |
10574 | else |
10575 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
10576 | S.Diag(Templated->getLocation(), |
10577 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
10578 | << (index + 1); |
10579 | } |
10580 | MaybeEmitInheritedConstructorNote(S, Found); |
10581 | return; |
10582 | |
10583 | case Sema::TDK_ConstraintsNotSatisfied: { |
10584 | |
10585 | SmallString<128> TemplateArgString; |
10586 | TemplateArgumentList *Args = DeductionFailure.getTemplateArgumentList(); |
10587 | TemplateArgString = " "; |
10588 | TemplateArgString += S.getTemplateArgumentBindingsText( |
10589 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
10590 | if (TemplateArgString.size() == 1) |
10591 | TemplateArgString.clear(); |
10592 | S.Diag(Templated->getLocation(), |
10593 | diag::note_ovl_candidate_unsatisfied_constraints) |
10594 | << TemplateArgString; |
10595 | |
10596 | S.DiagnoseUnsatisfiedConstraint( |
10597 | static_cast<CNSInfo*>(DeductionFailure.Data)->Satisfaction); |
10598 | return; |
10599 | } |
10600 | case Sema::TDK_TooManyArguments: |
10601 | case Sema::TDK_TooFewArguments: |
10602 | DiagnoseArityMismatch(S, Found, Templated, NumArgs); |
10603 | return; |
10604 | |
10605 | case Sema::TDK_InstantiationDepth: |
10606 | S.Diag(Templated->getLocation(), |
10607 | diag::note_ovl_candidate_instantiation_depth); |
10608 | MaybeEmitInheritedConstructorNote(S, Found); |
10609 | return; |
10610 | |
10611 | case Sema::TDK_SubstitutionFailure: { |
10612 | |
10613 | SmallString<128> TemplateArgString; |
10614 | if (TemplateArgumentList *Args = |
10615 | DeductionFailure.getTemplateArgumentList()) { |
10616 | TemplateArgString = " "; |
10617 | TemplateArgString += S.getTemplateArgumentBindingsText( |
10618 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
10619 | if (TemplateArgString.size() == 1) |
10620 | TemplateArgString.clear(); |
10621 | } |
10622 | |
10623 | |
10624 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
10625 | if (PDiag && PDiag->second.getDiagID() == |
10626 | diag::err_typename_nested_not_found_enable_if) { |
10627 | |
10628 | |
10629 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
10630 | << "'enable_if'" << TemplateArgString; |
10631 | return; |
10632 | } |
10633 | |
10634 | |
10635 | if (PDiag && PDiag->second.getDiagID() == |
10636 | diag::err_typename_nested_not_found_requirement) { |
10637 | S.Diag(Templated->getLocation(), |
10638 | diag::note_ovl_candidate_disabled_by_requirement) |
10639 | << PDiag->second.getStringArg(0) << TemplateArgString; |
10640 | return; |
10641 | } |
10642 | |
10643 | |
10644 | |
10645 | |
10646 | SmallString<128> SFINAEArgString; |
10647 | SourceRange R; |
10648 | if (PDiag) { |
10649 | SFINAEArgString = ": "; |
10650 | R = SourceRange(PDiag->first, PDiag->first); |
10651 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
10652 | } |
10653 | |
10654 | S.Diag(Templated->getLocation(), |
10655 | diag::note_ovl_candidate_substitution_failure) |
10656 | << TemplateArgString << SFINAEArgString << R; |
10657 | MaybeEmitInheritedConstructorNote(S, Found); |
10658 | return; |
10659 | } |
10660 | |
10661 | case Sema::TDK_DeducedMismatch: |
10662 | case Sema::TDK_DeducedMismatchNested: { |
10663 | |
10664 | SmallString<128> TemplateArgString; |
10665 | if (TemplateArgumentList *Args = |
10666 | DeductionFailure.getTemplateArgumentList()) { |
10667 | TemplateArgString = " "; |
10668 | TemplateArgString += S.getTemplateArgumentBindingsText( |
10669 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
10670 | if (TemplateArgString.size() == 1) |
10671 | TemplateArgString.clear(); |
10672 | } |
10673 | |
10674 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_deduced_mismatch) |
10675 | << (*DeductionFailure.getCallArgIndex() + 1) |
10676 | << *DeductionFailure.getFirstArg() << *DeductionFailure.getSecondArg() |
10677 | << TemplateArgString |
10678 | << (DeductionFailure.Result == Sema::TDK_DeducedMismatchNested); |
10679 | break; |
10680 | } |
10681 | |
10682 | case Sema::TDK_NonDeducedMismatch: { |
10683 | |
10684 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
10685 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
10686 | if (FirstTA.getKind() == TemplateArgument::Template && |
10687 | SecondTA.getKind() == TemplateArgument::Template) { |
10688 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
10689 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
10690 | if (FirstTN.getKind() == TemplateName::Template && |
10691 | SecondTN.getKind() == TemplateName::Template) { |
10692 | if (FirstTN.getAsTemplateDecl()->getName() == |
10693 | SecondTN.getAsTemplateDecl()->getName()) { |
10694 | |
10695 | |
10696 | |
10697 | |
10698 | |
10699 | |
10700 | S.Diag(Templated->getLocation(), |
10701 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
10702 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
10703 | return; |
10704 | } |
10705 | } |
10706 | } |
10707 | |
10708 | if (TakingCandidateAddress && isa<FunctionDecl>(Templated) && |
10709 | !checkAddressOfCandidateIsAvailable(S, cast<FunctionDecl>(Templated))) |
10710 | return; |
10711 | |
10712 | |
10713 | |
10714 | |
10715 | |
10716 | S.Diag(Templated->getLocation(), |
10717 | diag::note_ovl_candidate_non_deduced_mismatch) |
10718 | << FirstTA << SecondTA; |
10719 | return; |
10720 | } |
10721 | |
10722 | |
10723 | case Sema::TDK_MiscellaneousDeductionFailure: |
10724 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
10725 | MaybeEmitInheritedConstructorNote(S, Found); |
10726 | return; |
10727 | case Sema::TDK_CUDATargetMismatch: |
10728 | S.Diag(Templated->getLocation(), |
10729 | diag::note_cuda_ovl_candidate_target_mismatch); |
10730 | return; |
10731 | } |
10732 | } |
10733 | |
10734 | |
10735 | static void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
10736 | unsigned NumArgs, |
10737 | bool TakingCandidateAddress) { |
10738 | unsigned TDK = Cand->DeductionFailure.Result; |
10739 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
10740 | if (CheckArityMismatch(S, Cand, NumArgs)) |
10741 | return; |
10742 | } |
10743 | DiagnoseBadDeduction(S, Cand->FoundDecl, Cand->Function, |
10744 | Cand->DeductionFailure, NumArgs, TakingCandidateAddress); |
10745 | } |
10746 | |
10747 | |
10748 | static void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
10749 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
10750 | FunctionDecl *Callee = Cand->Function; |
10751 | |
10752 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
10753 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
10754 | |
10755 | std::string FnDesc; |
10756 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
10757 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Callee, |
10758 | Cand->getRewriteKind(), FnDesc); |
10759 | |
10760 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
10761 | << (unsigned)FnKindPair.first << (unsigned)ocs_non_template |
10762 | << FnDesc |
10763 | << CalleeTarget << CallerTarget; |
10764 | |
10765 | |
10766 | |
10767 | CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Callee); |
10768 | if (Meth != nullptr && Meth->isImplicit()) { |
10769 | CXXRecordDecl *ParentClass = Meth->getParent(); |
10770 | Sema::CXXSpecialMember CSM; |
10771 | |
10772 | switch (FnKindPair.first) { |
10773 | default: |
10774 | return; |
10775 | case oc_implicit_default_constructor: |
10776 | CSM = Sema::CXXDefaultConstructor; |
10777 | break; |
10778 | case oc_implicit_copy_constructor: |
10779 | CSM = Sema::CXXCopyConstructor; |
10780 | break; |
10781 | case oc_implicit_move_constructor: |
10782 | CSM = Sema::CXXMoveConstructor; |
10783 | break; |
10784 | case oc_implicit_copy_assignment: |
10785 | CSM = Sema::CXXCopyAssignment; |
10786 | break; |
10787 | case oc_implicit_move_assignment: |
10788 | CSM = Sema::CXXMoveAssignment; |
10789 | break; |
10790 | }; |
10791 | |
10792 | bool ConstRHS = false; |
10793 | if (Meth->getNumParams()) { |
10794 | if (const ReferenceType *RT = |
10795 | Meth->getParamDecl(0)->getType()->getAs<ReferenceType>()) { |
10796 | ConstRHS = RT->getPointeeType().isConstQualified(); |
10797 | } |
10798 | } |
10799 | |
10800 | S.inferCUDATargetForImplicitSpecialMember(ParentClass, CSM, Meth, |
10801 | ConstRHS, |
10802 | true); |
10803 | } |
10804 | } |
10805 | |
10806 | static void DiagnoseFailedEnableIfAttr(Sema &S, OverloadCandidate *Cand) { |
10807 | FunctionDecl *Callee = Cand->Function; |
10808 | EnableIfAttr *Attr = static_cast<EnableIfAttr*>(Cand->DeductionFailure.Data); |
10809 | |
10810 | S.Diag(Callee->getLocation(), |
10811 | diag::note_ovl_candidate_disabled_by_function_cond_attr) |
10812 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); |
10813 | } |
10814 | |
10815 | static void DiagnoseFailedExplicitSpec(Sema &S, OverloadCandidate *Cand) { |
10816 | ExplicitSpecifier ES = ExplicitSpecifier::getFromDecl(Cand->Function); |
10817 | assert(ES.isExplicit() && "not an explicit candidate"); |
10818 | |
10819 | unsigned Kind; |
10820 | switch (Cand->Function->getDeclKind()) { |
10821 | case Decl::Kind::CXXConstructor: |
10822 | Kind = 0; |
10823 | break; |
10824 | case Decl::Kind::CXXConversion: |
10825 | Kind = 1; |
10826 | break; |
10827 | case Decl::Kind::CXXDeductionGuide: |
10828 | Kind = Cand->Function->isImplicit() ? 0 : 2; |
10829 | break; |
10830 | default: |
10831 | llvm_unreachable("invalid Decl"); |
10832 | } |
10833 | |
10834 | |
10835 | |
10836 | |
10837 | |
10838 | FunctionDecl *First = Cand->Function->getFirstDecl(); |
10839 | if (FunctionDecl *Pattern = First->getTemplateInstantiationPattern()) |
10840 | First = Pattern->getFirstDecl(); |
10841 | |
10842 | S.Diag(First->getLocation(), |
10843 | diag::note_ovl_candidate_explicit) |
10844 | << Kind << (ES.getExpr() ? 1 : 0) |
10845 | << (ES.getExpr() ? ES.getExpr()->getSourceRange() : SourceRange()); |
10846 | } |
10847 | |
10848 | static void DiagnoseOpenCLExtensionDisabled(Sema &S, OverloadCandidate *Cand) { |
10849 | FunctionDecl *Callee = Cand->Function; |
10850 | |
10851 | S.Diag(Callee->getLocation(), |
10852 | diag::note_ovl_candidate_disabled_by_extension) |
10853 | << S.getOpenCLExtensionsFromDeclExtMap(Callee); |
10854 | } |
10855 | |
10856 | |
10857 | |
10858 | |
10859 | |
10860 | |
10861 | |
10862 | |
10863 | |
10864 | |
10865 | |
10866 | |
10867 | |
10868 | |
10869 | |
10870 | |
10871 | static void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
10872 | unsigned NumArgs, |
10873 | bool TakingCandidateAddress, |
10874 | LangAS CtorDestAS = LangAS::Default) { |
10875 | FunctionDecl *Fn = Cand->Function; |
10876 | |
10877 | |
10878 | if (Cand->Viable) { |
10879 | if (Fn->isDeleted()) { |
10880 | std::string FnDesc; |
10881 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
10882 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, |
10883 | Cand->getRewriteKind(), FnDesc); |
10884 | |
10885 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
10886 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
10887 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
10888 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10889 | return; |
10890 | } |
10891 | |
10892 | |
10893 | S.NoteOverloadCandidate(Cand->FoundDecl, Fn, Cand->getRewriteKind()); |
10894 | return; |
10895 | } |
10896 | |
10897 | switch (Cand->FailureKind) { |
10898 | case ovl_fail_too_many_arguments: |
10899 | case ovl_fail_too_few_arguments: |
10900 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
10901 | |
10902 | case ovl_fail_bad_deduction: |
10903 | return DiagnoseBadDeduction(S, Cand, NumArgs, |
10904 | TakingCandidateAddress); |
10905 | |
10906 | case ovl_fail_illegal_constructor: { |
10907 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_illegal_constructor) |
10908 | << (Fn->getPrimaryTemplate() ? 1 : 0); |
10909 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10910 | return; |
10911 | } |
10912 | |
10913 | case ovl_fail_object_addrspace_mismatch: { |
10914 | Qualifiers QualsForPrinting; |
10915 | QualsForPrinting.setAddressSpace(CtorDestAS); |
10916 | S.Diag(Fn->getLocation(), |
10917 | diag::note_ovl_candidate_illegal_constructor_adrspace_mismatch) |
10918 | << QualsForPrinting; |
10919 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10920 | return; |
10921 | } |
10922 | |
10923 | case ovl_fail_trivial_conversion: |
10924 | case ovl_fail_bad_final_conversion: |
10925 | case ovl_fail_final_conversion_not_exact: |
10926 | return S.NoteOverloadCandidate(Cand->FoundDecl, Fn, Cand->getRewriteKind()); |
10927 | |
10928 | case ovl_fail_bad_conversion: { |
10929 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
10930 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
10931 | if (Cand->Conversions[I].isBad()) |
10932 | return DiagnoseBadConversion(S, Cand, I, TakingCandidateAddress); |
10933 | |
10934 | |
10935 | |
10936 | |
10937 | return S.NoteOverloadCandidate(Cand->FoundDecl, Fn, Cand->getRewriteKind()); |
10938 | } |
10939 | |
10940 | case ovl_fail_bad_target: |
10941 | return DiagnoseBadTarget(S, Cand); |
10942 | |
10943 | case ovl_fail_enable_if: |
10944 | return DiagnoseFailedEnableIfAttr(S, Cand); |
10945 | |
10946 | case ovl_fail_explicit: |
10947 | return DiagnoseFailedExplicitSpec(S, Cand); |
10948 | |
10949 | case ovl_fail_ext_disabled: |
10950 | return DiagnoseOpenCLExtensionDisabled(S, Cand); |
10951 | |
10952 | case ovl_fail_inhctor_slice: |
10953 | |
10954 | if (cast<CXXConstructorDecl>(Fn)->isCopyOrMoveConstructor()) |
10955 | return; |
10956 | S.Diag(Fn->getLocation(), |
10957 | diag::note_ovl_candidate_inherited_constructor_slice) |
10958 | << (Fn->getPrimaryTemplate() ? 1 : 0) |
10959 | << Fn->getParamDecl(0)->getType()->isRValueReferenceType(); |
10960 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
10961 | return; |
10962 | |
10963 | case ovl_fail_addr_not_available: { |
10964 | bool Available = checkAddressOfCandidateIsAvailable(S, Cand->Function); |
10965 | (void)Available; |
10966 | assert(!Available); |
10967 | break; |
10968 | } |
10969 | case ovl_non_default_multiversion_function: |
10970 | |
10971 | break; |
10972 | |
10973 | case ovl_fail_constraints_not_satisfied: { |
10974 | std::string FnDesc; |
10975 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
10976 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, |
10977 | Cand->getRewriteKind(), FnDesc); |
10978 | |
10979 | S.Diag(Fn->getLocation(), |
10980 | diag::note_ovl_candidate_constraints_not_satisfied) |
10981 | << (unsigned)FnKindPair.first << (unsigned)ocs_non_template |
10982 | << FnDesc ; |
10983 | ConstraintSatisfaction Satisfaction; |
10984 | if (S.CheckConstraintSatisfaction(Fn->getTrailingRequiresClause(), |
10985 | Satisfaction)) |
10986 | break; |
10987 | S.DiagnoseUnsatisfiedConstraint(Satisfaction); |
10988 | } |
10989 | } |
10990 | } |
10991 | |
10992 | static void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
10993 | |
10994 | |
10995 | |
10996 | QualType FnType = Cand->Surrogate->getConversionType(); |
10997 | bool isLValueReference = false; |
10998 | bool isRValueReference = false; |
10999 | bool isPointer = false; |
11000 | if (const LValueReferenceType *FnTypeRef = |
11001 | FnType->getAs<LValueReferenceType>()) { |
11002 | FnType = FnTypeRef->getPointeeType(); |
11003 | isLValueReference = true; |
11004 | } else if (const RValueReferenceType *FnTypeRef = |
11005 | FnType->getAs<RValueReferenceType>()) { |
11006 | FnType = FnTypeRef->getPointeeType(); |
11007 | isRValueReference = true; |
11008 | } |
11009 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
11010 | FnType = FnTypePtr->getPointeeType(); |
11011 | isPointer = true; |
11012 | } |
11013 | |
11014 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
11015 | |
11016 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
11017 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
11018 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
11019 | |
11020 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
11021 | << FnType; |
11022 | } |
11023 | |
11024 | static void NoteBuiltinOperatorCandidate(Sema &S, StringRef Opc, |
11025 | SourceLocation OpLoc, |
11026 | OverloadCandidate *Cand) { |
11027 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
11028 | std::string TypeStr("operator"); |
11029 | TypeStr += Opc; |
11030 | TypeStr += "("; |
11031 | TypeStr += Cand->BuiltinParamTypes[0].getAsString(); |
11032 | if (Cand->Conversions.size() == 1) { |
11033 | TypeStr += ")"; |
11034 | S.Diag(OpLoc, diag::note_ovl_builtin_candidate) << TypeStr; |
11035 | } else { |
11036 | TypeStr += ", "; |
11037 | TypeStr += Cand->BuiltinParamTypes[1].getAsString(); |
11038 | TypeStr += ")"; |
11039 | S.Diag(OpLoc, diag::note_ovl_builtin_candidate) << TypeStr; |
11040 | } |
11041 | } |
11042 | |
11043 | static void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
11044 | OverloadCandidate *Cand) { |
11045 | for (const ImplicitConversionSequence &ICS : Cand->Conversions) { |
11046 | if (ICS.isBad()) break; |
11047 | if (!ICS.isAmbiguous()) continue; |
11048 | |
11049 | ICS.DiagnoseAmbiguousConversion( |
11050 | S, OpLoc, S.PDiag(diag::note_ambiguous_type_conversion)); |
11051 | } |
11052 | } |
11053 | |
11054 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
11055 | if (Cand->Function) |
11056 | return Cand->Function->getLocation(); |
11057 | if (Cand->IsSurrogate) |
11058 | return Cand->Surrogate->getLocation(); |
11059 | return SourceLocation(); |
11060 | } |
11061 | |
11062 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
11063 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
11064 | case Sema::TDK_Success: |
11065 | case Sema::TDK_NonDependentConversionFailure: |
11066 | llvm_unreachable("non-deduction failure while diagnosing bad deduction"); |
11067 | |
11068 | case Sema::TDK_Invalid: |
11069 | case Sema::TDK_Incomplete: |
11070 | case Sema::TDK_IncompletePack: |
11071 | return 1; |
11072 | |
11073 | case Sema::TDK_Underqualified: |
11074 | case Sema::TDK_Inconsistent: |
11075 | return 2; |
11076 | |
11077 | case Sema::TDK_SubstitutionFailure: |
11078 | case Sema::TDK_DeducedMismatch: |
11079 | case Sema::TDK_ConstraintsNotSatisfied: |
11080 | case Sema::TDK_DeducedMismatchNested: |
11081 | case Sema::TDK_NonDeducedMismatch: |
11082 | case Sema::TDK_MiscellaneousDeductionFailure: |
11083 | case Sema::TDK_CUDATargetMismatch: |
11084 | return 3; |
11085 | |
11086 | case Sema::TDK_InstantiationDepth: |
11087 | return 4; |
11088 | |
11089 | case Sema::TDK_InvalidExplicitArguments: |
11090 | return 5; |
11091 | |
11092 | case Sema::TDK_TooManyArguments: |
11093 | case Sema::TDK_TooFewArguments: |
11094 | return 6; |
11095 | } |
11096 | llvm_unreachable("Unhandled deduction result"); |
11097 | } |
11098 | |
11099 | namespace { |
11100 | struct CompareOverloadCandidatesForDisplay { |
11101 | Sema &S; |
11102 | SourceLocation Loc; |
11103 | size_t NumArgs; |
11104 | OverloadCandidateSet::CandidateSetKind CSK; |
11105 | |
11106 | CompareOverloadCandidatesForDisplay( |
11107 | Sema &S, SourceLocation Loc, size_t NArgs, |
11108 | OverloadCandidateSet::CandidateSetKind CSK) |
11109 | : S(S), NumArgs(NArgs), CSK(CSK) {} |
11110 | |
11111 | OverloadFailureKind EffectiveFailureKind(const OverloadCandidate *C) const { |
11112 | |
11113 | |
11114 | if (C->FailureKind == ovl_fail_too_many_arguments || |
11115 | C->FailureKind == ovl_fail_too_few_arguments) |
11116 | return static_cast<OverloadFailureKind>(C->FailureKind); |
11117 | |
11118 | if (C->Function) { |
11119 | if (NumArgs > C->Function->getNumParams() && !C->Function->isVariadic()) |
11120 | return ovl_fail_too_many_arguments; |
11121 | if (NumArgs < C->Function->getMinRequiredArguments()) |
11122 | return ovl_fail_too_few_arguments; |
11123 | } |
11124 | |
11125 | return static_cast<OverloadFailureKind>(C->FailureKind); |
11126 | } |
11127 | |
11128 | bool operator()(const OverloadCandidate *L, |
11129 | const OverloadCandidate *R) { |
11130 | |
11131 | if (L == R) return false; |
11132 | |
11133 | |
11134 | if (L->Viable) { |
11135 | if (!R->Viable) return true; |
11136 | |
11137 | |
11138 | |
11139 | |
11140 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation(), CSK)) |
11141 | return true; |
11142 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation(), CSK)) |
11143 | return false; |
11144 | } else if (R->Viable) |
11145 | return false; |
11146 | |
11147 | assert(L->Viable == R->Viable); |
11148 | |
11149 | |
11150 | if (!L->Viable) { |
11151 | OverloadFailureKind LFailureKind = EffectiveFailureKind(L); |
11152 | OverloadFailureKind RFailureKind = EffectiveFailureKind(R); |
11153 | |
11154 | |
11155 | if (LFailureKind == ovl_fail_too_many_arguments || |
11156 | LFailureKind == ovl_fail_too_few_arguments) { |
11157 | if (RFailureKind == ovl_fail_too_many_arguments || |
11158 | RFailureKind == ovl_fail_too_few_arguments) { |
11159 | int LDist = std::abs((int)L->getNumParams() - (int)NumArgs); |
11160 | int RDist = std::abs((int)R->getNumParams() - (int)NumArgs); |
11161 | if (LDist == RDist) { |
11162 | if (LFailureKind == RFailureKind) |
11163 | |
11164 | return !L->IsSurrogate && R->IsSurrogate; |
11165 | |
11166 | |
11167 | |
11168 | return LFailureKind == ovl_fail_too_many_arguments; |
11169 | } |
11170 | return LDist < RDist; |
11171 | } |
11172 | return false; |
11173 | } |
11174 | if (RFailureKind == ovl_fail_too_many_arguments || |
11175 | RFailureKind == ovl_fail_too_few_arguments) |
11176 | return true; |
11177 | |
11178 | |
11179 | |
11180 | if (LFailureKind == ovl_fail_bad_conversion) { |
11181 | if (RFailureKind != ovl_fail_bad_conversion) |
11182 | return true; |
11183 | |
11184 | |
11185 | |
11186 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
11187 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
11188 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
11189 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
11190 | if (numLFixes != numRFixes) { |
11191 | return numLFixes < numRFixes; |
11192 | } |
11193 | |
11194 | |
11195 | |
11196 | assert(L->Conversions.size() == R->Conversions.size()); |
11197 | |
11198 | int leftBetter = 0; |
11199 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
11200 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
11201 | switch (CompareImplicitConversionSequences(S, Loc, |
11202 | L->Conversions[I], |
11203 | R->Conversions[I])) { |
11204 | case ImplicitConversionSequence::Better: |
11205 | leftBetter++; |
11206 | break; |
11207 | |
11208 | case ImplicitConversionSequence::Worse: |
11209 | leftBetter--; |
11210 | break; |
11211 | |
11212 | case ImplicitConversionSequence::Indistinguishable: |
11213 | break; |
11214 | } |
11215 | } |
11216 | if (leftBetter > 0) return true; |
11217 | if (leftBetter < 0) return false; |
11218 | |
11219 | } else if (RFailureKind == ovl_fail_bad_conversion) |
11220 | return false; |
11221 | |
11222 | if (LFailureKind == ovl_fail_bad_deduction) { |
11223 | if (RFailureKind != ovl_fail_bad_deduction) |
11224 | return true; |
11225 | |
11226 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
11227 | return RankDeductionFailure(L->DeductionFailure) |
11228 | < RankDeductionFailure(R->DeductionFailure); |
11229 | } else if (RFailureKind == ovl_fail_bad_deduction) |
11230 | return false; |
11231 | |
11232 | |
11233 | } |
11234 | |
11235 | |
11236 | SourceLocation LLoc = GetLocationForCandidate(L); |
11237 | SourceLocation RLoc = GetLocationForCandidate(R); |
11238 | |
11239 | |
11240 | if (LLoc.isInvalid()) return false; |
11241 | if (RLoc.isInvalid()) return true; |
11242 | |
11243 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
11244 | } |
11245 | }; |
11246 | } |
11247 | |
11248 | |
11249 | |
11250 | |
11251 | static void |
11252 | CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
11253 | ArrayRef<Expr *> Args, |
11254 | OverloadCandidateSet::CandidateSetKind CSK) { |
11255 | assert(!Cand->Viable); |
11256 | |
11257 | |
11258 | if (Cand->FailureKind != ovl_fail_bad_conversion) |
11259 | return; |
11260 | |
11261 | |
11262 | bool Unfixable = false; |
11263 | |
11264 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
11265 | |
11266 | |
11267 | unsigned ConvCount = Cand->Conversions.size(); |
11268 | for (unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); ; |
11269 | ++ConvIdx) { |
11270 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
11271 | if (Cand->Conversions[ConvIdx].isInitialized() && |
11272 | Cand->Conversions[ConvIdx].isBad()) { |
11273 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
11274 | break; |
11275 | } |
11276 | } |
11277 | |
11278 | |
11279 | |
11280 | bool SuppressUserConversions = false; |
11281 | |
11282 | unsigned ConvIdx = 0; |
11283 | unsigned ArgIdx = 0; |
11284 | ArrayRef<QualType> ParamTypes; |
11285 | bool Reversed = Cand->RewriteKind & CRK_Reversed; |
11286 | |
11287 | if (Cand->IsSurrogate) { |
11288 | QualType ConvType |
11289 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
11290 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
11291 | ConvType = ConvPtrType->getPointeeType(); |
11292 | ParamTypes = ConvType->castAs<FunctionProtoType>()->getParamTypes(); |
11293 | |
11294 | ConvIdx = 1; |
11295 | } else if (Cand->Function) { |
11296 | ParamTypes = |
11297 | Cand->Function->getType()->castAs<FunctionProtoType>()->getParamTypes(); |
11298 | if (isa<CXXMethodDecl>(Cand->Function) && |
11299 | !isa<CXXConstructorDecl>(Cand->Function) && !Reversed) { |
11300 | |
11301 | ConvIdx = 1; |
11302 | if (CSK == OverloadCandidateSet::CSK_Operator && |
11303 | Cand->Function->getDeclName().getCXXOverloadedOperator() != OO_Call) |
11304 | |
11305 | ArgIdx = 1; |
11306 | } |
11307 | } else { |
11308 | |
11309 | assert(ConvCount <= 3); |
11310 | ParamTypes = Cand->BuiltinParamTypes; |
11311 | } |
11312 | |
11313 | |
11314 | for (unsigned ParamIdx = Reversed ? ParamTypes.size() - 1 : 0; |
11315 | ConvIdx != ConvCount; |
11316 | ++ConvIdx, ++ArgIdx, ParamIdx += (Reversed ? -1 : 1)) { |
11317 | assert(ArgIdx < Args.size() && "no argument for this arg conversion"); |
11318 | if (Cand->Conversions[ConvIdx].isInitialized()) { |
11319 | |
11320 | } else if (ParamIdx < ParamTypes.size()) { |
11321 | if (ParamTypes[ParamIdx]->isDependentType()) |
11322 | Cand->Conversions[ConvIdx].setAsIdentityConversion( |
11323 | Args[ArgIdx]->getType()); |
11324 | else { |
11325 | Cand->Conversions[ConvIdx] = |
11326 | TryCopyInitialization(S, Args[ArgIdx], ParamTypes[ParamIdx], |
11327 | SuppressUserConversions, |
11328 | true, |
11329 | |
11330 | S.getLangOpts().ObjCAutoRefCount); |
11331 | |
11332 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
11333 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
11334 | } |
11335 | } else |
11336 | Cand->Conversions[ConvIdx].setEllipsis(); |
11337 | } |
11338 | } |
11339 | |
11340 | SmallVector<OverloadCandidate *, 32> OverloadCandidateSet::CompleteCandidates( |
11341 | Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args, |
11342 | SourceLocation OpLoc, |
11343 | llvm::function_ref<bool(OverloadCandidate &)> Filter) { |
11344 | |
11345 | |
11346 | SmallVector<OverloadCandidate*, 32> Cands; |
11347 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
11348 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
11349 | if (!Filter(*Cand)) |
11350 | continue; |
11351 | switch (OCD) { |
11352 | case OCD_AllCandidates: |
11353 | if (!Cand->Viable) { |
11354 | if (!Cand->Function && !Cand->IsSurrogate) { |
11355 | |
11356 | |
11357 | continue; |
11358 | } |
11359 | CompleteNonViableCandidate(S, Cand, Args, Kind); |
11360 | } |
11361 | break; |
11362 | |
11363 | case OCD_ViableCandidates: |
11364 | if (!Cand->Viable) |
11365 | continue; |
11366 | break; |
11367 | |
11368 | case OCD_AmbiguousCandidates: |
11369 | if (!Cand->Best) |
11370 | continue; |
11371 | break; |
11372 | } |
11373 | |
11374 | Cands.push_back(Cand); |
11375 | } |
11376 | |
11377 | llvm::stable_sort( |
11378 | Cands, CompareOverloadCandidatesForDisplay(S, OpLoc, Args.size(), Kind)); |
11379 | |
11380 | return Cands; |
11381 | } |
11382 | |
11383 | |
11384 | |
11385 | void OverloadCandidateSet::NoteCandidates(PartialDiagnosticAt PD, |
11386 | Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args, |
11387 | StringRef Opc, SourceLocation OpLoc, |
11388 | llvm::function_ref<bool(OverloadCandidate &)> Filter) { |
11389 | |
11390 | auto Cands = CompleteCandidates(S, OCD, Args, OpLoc, Filter); |
11391 | |
11392 | S.Diag(PD.first, PD.second); |
11393 | |
11394 | NoteCandidates(S, Args, Cands, Opc, OpLoc); |
11395 | |
11396 | if (OCD == OCD_AmbiguousCandidates) |
11397 | MaybeDiagnoseAmbiguousConstraints(S, {begin(), end()}); |
11398 | } |
11399 | |
11400 | void OverloadCandidateSet::NoteCandidates(Sema &S, ArrayRef<Expr *> Args, |
11401 | ArrayRef<OverloadCandidate *> Cands, |
11402 | StringRef Opc, SourceLocation OpLoc) { |
11403 | bool ReportedAmbiguousConversions = false; |
11404 | |
11405 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
11406 | unsigned CandsShown = 0; |
11407 | auto I = Cands.begin(), E = Cands.end(); |
11408 | for (; I != E; ++I) { |
11409 | OverloadCandidate *Cand = *I; |
11410 | |
11411 | |
11412 | |
11413 | |
11414 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
11415 | break; |
11416 | } |
11417 | ++CandsShown; |
11418 | |
11419 | if (Cand->Function) |
11420 | NoteFunctionCandidate(S, Cand, Args.size(), |
11421 | false, DestAS); |
11422 | else if (Cand->IsSurrogate) |
11423 | NoteSurrogateCandidate(S, Cand); |
11424 | else { |
11425 | assert(Cand->Viable && |
11426 | "Non-viable built-in candidates are not added to Cands."); |
11427 | |
11428 | |
11429 | |
11430 | |
11431 | |
11432 | |
11433 | if (!ReportedAmbiguousConversions) { |
11434 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
11435 | ReportedAmbiguousConversions = true; |
11436 | } |
11437 | |
11438 | |
11439 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
11440 | } |
11441 | } |
11442 | |
11443 | if (I != E) |
11444 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
11445 | } |
11446 | |
11447 | static SourceLocation |
11448 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
11449 | return Cand->Specialization ? Cand->Specialization->getLocation() |
11450 | : SourceLocation(); |
11451 | } |
11452 | |
11453 | namespace { |
11454 | struct CompareTemplateSpecCandidatesForDisplay { |
11455 | Sema &S; |
11456 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
11457 | |
11458 | bool operator()(const TemplateSpecCandidate *L, |
11459 | const TemplateSpecCandidate *R) { |
11460 | |
11461 | if (L == R) |
11462 | return false; |
11463 | |
11464 | |
11465 | |
11466 | |
11467 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
11468 | return RankDeductionFailure(L->DeductionFailure) < |
11469 | RankDeductionFailure(R->DeductionFailure); |
11470 | |
11471 | |
11472 | SourceLocation LLoc = GetLocationForCandidate(L); |
11473 | SourceLocation RLoc = GetLocationForCandidate(R); |
11474 | |
11475 | |
11476 | if (LLoc.isInvalid()) |
11477 | return false; |
11478 | if (RLoc.isInvalid()) |
11479 | return true; |
11480 | |
11481 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
11482 | } |
11483 | }; |
11484 | } |
11485 | |
11486 | |
11487 | |
11488 | |
11489 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S, |
11490 | bool ForTakingAddress) { |
11491 | DiagnoseBadDeduction(S, FoundDecl, Specialization, |
11492 | DeductionFailure, 0, ForTakingAddress); |
11493 | } |
11494 | |
11495 | void TemplateSpecCandidateSet::destroyCandidates() { |
11496 | for (iterator i = begin(), e = end(); i != e; ++i) { |
11497 | i->DeductionFailure.Destroy(); |
11498 | } |
11499 | } |
11500 | |
11501 | void TemplateSpecCandidateSet::clear() { |
11502 | destroyCandidates(); |
11503 | Candidates.clear(); |
11504 | } |
11505 | |
11506 | |
11507 | |
11508 | |
11509 | |
11510 | |
11511 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
11512 | |
11513 | |
11514 | |
11515 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
11516 | Cands.reserve(size()); |
11517 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
11518 | if (Cand->Specialization) |
11519 | Cands.push_back(Cand); |
11520 | |
11521 | |
11522 | } |
11523 | |
11524 | llvm::sort(Cands, CompareTemplateSpecCandidatesForDisplay(S)); |
11525 | |
11526 | |
11527 | |
11528 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
11529 | |
11530 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
11531 | unsigned CandsShown = 0; |
11532 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
11533 | TemplateSpecCandidate *Cand = *I; |
11534 | |
11535 | |
11536 | |
11537 | |
11538 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
11539 | break; |
11540 | ++CandsShown; |
11541 | |
11542 | assert(Cand->Specialization && |
11543 | "Non-matching built-in candidates are not added to Cands."); |
11544 | Cand->NoteDeductionFailure(S, ForTakingAddress); |
11545 | } |
11546 | |
11547 | if (I != E) |
11548 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
11549 | } |
11550 | |
11551 | |
11552 | |
11553 | |
11554 | |
11555 | |
11556 | |
11557 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
11558 | QualType Ret = PossiblyAFunctionType; |
11559 | if (const PointerType *ToTypePtr = |
11560 | PossiblyAFunctionType->getAs<PointerType>()) |
11561 | Ret = ToTypePtr->getPointeeType(); |
11562 | else if (const ReferenceType *ToTypeRef = |
11563 | PossiblyAFunctionType->getAs<ReferenceType>()) |
11564 | Ret = ToTypeRef->getPointeeType(); |
11565 | else if (const MemberPointerType *MemTypePtr = |
11566 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
11567 | Ret = MemTypePtr->getPointeeType(); |
11568 | Ret = |
11569 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
11570 | return Ret; |
11571 | } |
11572 | |
11573 | static bool completeFunctionType(Sema &S, FunctionDecl *FD, SourceLocation Loc, |
11574 | bool Complain = true) { |
11575 | if (S.getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() && |
11576 | S.DeduceReturnType(FD, Loc, Complain)) |
11577 | return true; |
11578 | |
11579 | auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
11580 | if (S.getLangOpts().CPlusPlus17 && |
11581 | isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) && |
11582 | !S.ResolveExceptionSpec(Loc, FPT)) |
11583 | return true; |
11584 | |
11585 | return false; |
11586 | } |
11587 | |
11588 | namespace { |
11589 | |
11590 | |
11591 | class AddressOfFunctionResolver { |
11592 | Sema& S; |
11593 | Expr* SourceExpr; |
11594 | const QualType& TargetType; |
11595 | QualType TargetFunctionType; |
11596 | |
11597 | bool Complain; |
11598 | |
11599 | ASTContext& Context; |
11600 | |
11601 | bool TargetTypeIsNonStaticMemberFunction; |
11602 | bool FoundNonTemplateFunction; |
11603 | bool StaticMemberFunctionFromBoundPointer; |
11604 | bool HasComplained; |
11605 | |
11606 | OverloadExpr::FindResult OvlExprInfo; |
11607 | OverloadExpr *OvlExpr; |
11608 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
11609 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
11610 | TemplateSpecCandidateSet FailedCandidates; |
11611 | |
11612 | public: |
11613 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
11614 | const QualType &TargetType, bool Complain) |
11615 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
11616 | Complain(Complain), Context(S.getASTContext()), |
11617 | TargetTypeIsNonStaticMemberFunction( |
11618 | !!TargetType->getAs<MemberPointerType>()), |
11619 | FoundNonTemplateFunction(false), |
11620 | StaticMemberFunctionFromBoundPointer(false), |
11621 | HasComplained(false), |
11622 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
11623 | OvlExpr(OvlExprInfo.Expression), |
11624 | FailedCandidates(OvlExpr->getNameLoc(), true) { |
11625 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
11626 | |
11627 | if (TargetFunctionType->isFunctionType()) { |
11628 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
11629 | if (!UME->isImplicitAccess() && |
11630 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
11631 | StaticMemberFunctionFromBoundPointer = true; |
11632 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
11633 | DeclAccessPair dap; |
11634 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
11635 | OvlExpr, false, &dap)) { |
11636 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
11637 | if (!Method->isStatic()) { |
11638 | |
11639 | |
11640 | |
11641 | TargetTypeIsNonStaticMemberFunction = true; |
11642 | |
11643 | |
11644 | |
11645 | if (!OvlExprInfo.HasFormOfMemberPointer) |
11646 | return; |
11647 | } |
11648 | |
11649 | Matches.push_back(std::make_pair(dap, Fn)); |
11650 | } |
11651 | return; |
11652 | } |
11653 | |
11654 | if (OvlExpr->hasExplicitTemplateArgs()) |
11655 | OvlExpr->copyTemplateArgumentsInto(OvlExplicitTemplateArgs); |
11656 | |
11657 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
11658 | |
11659 | |
11660 | if (Matches.size() > 1 && !eliminiateSuboptimalOverloadCandidates()) { |
11661 | if (FoundNonTemplateFunction) |
11662 | EliminateAllTemplateMatches(); |
11663 | else |
11664 | EliminateAllExceptMostSpecializedTemplate(); |
11665 | } |
11666 | } |
11667 | |
11668 | if (S.getLangOpts().CUDA && Matches.size() > 1) |
11669 | EliminateSuboptimalCudaMatches(); |
11670 | } |
11671 | |
11672 | bool hasComplained() const { return HasComplained; } |
11673 | |
11674 | private: |
11675 | bool candidateHasExactlyCorrectType(const FunctionDecl *FD) { |
11676 | QualType Discard; |
11677 | return Context.hasSameUnqualifiedType(TargetFunctionType, FD->getType()) || |
11678 | S.IsFunctionConversion(FD->getType(), TargetFunctionType, Discard); |
11679 | } |
11680 | |
11681 | |
11682 | |
11683 | bool isBetterCandidate(const FunctionDecl *A, const FunctionDecl *B) { |
11684 | |
11685 | |
11686 | |
11687 | return candidateHasExactlyCorrectType(A) && |
11688 | (!candidateHasExactlyCorrectType(B) || |
11689 | compareEnableIfAttrs(S, A, B) == Comparison::Better); |
11690 | } |
11691 | |
11692 | |
11693 | |
11694 | bool eliminiateSuboptimalOverloadCandidates() { |
11695 | |
11696 | |
11697 | auto Best = Matches.begin(); |
11698 | for (auto I = Matches.begin()+1, E = Matches.end(); I != E; ++I) |
11699 | if (isBetterCandidate(I->second, Best->second)) |
11700 | Best = I; |
11701 | |
11702 | const FunctionDecl *BestFn = Best->second; |
11703 | auto IsBestOrInferiorToBest = [this, BestFn]( |
11704 | const std::pair<DeclAccessPair, FunctionDecl *> &Pair) { |
11705 | return BestFn == Pair.second || isBetterCandidate(BestFn, Pair.second); |
11706 | }; |
11707 | |
11708 | |
11709 | |
11710 | if (!llvm::all_of(Matches, IsBestOrInferiorToBest)) |
11711 | return false; |
11712 | Matches[0] = *Best; |
11713 | Matches.resize(1); |
11714 | return true; |
11715 | } |
11716 | |
11717 | bool isTargetTypeAFunction() const { |
11718 | return TargetFunctionType->isFunctionType(); |
11719 | } |
11720 | |
11721 | |
11722 | |
11723 | |
11724 | |
11725 | |
11726 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
11727 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
11728 | } |
11729 | |
11730 | |
11731 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
11732 | const DeclAccessPair& CurAccessFunPair) { |
11733 | if (CXXMethodDecl *Method |
11734 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
11735 | |
11736 | |
11737 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
11738 | return false; |
11739 | } |
11740 | else if (TargetTypeIsNonStaticMemberFunction) |
11741 | return false; |
11742 | |
11743 | |
11744 | |
11745 | |
11746 | |
11747 | |
11748 | |
11749 | FunctionDecl *Specialization = nullptr; |
11750 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
11751 | if (Sema::TemplateDeductionResult Result |
11752 | = S.DeduceTemplateArguments(FunctionTemplate, |
11753 | &OvlExplicitTemplateArgs, |
11754 | TargetFunctionType, Specialization, |
11755 | Info, true)) { |
11756 | |
11757 | FailedCandidates.addCandidate() |
11758 | .set(CurAccessFunPair, FunctionTemplate->getTemplatedDecl(), |
11759 | MakeDeductionFailureInfo(Context, Result, Info)); |
11760 | return false; |
11761 | } |
11762 | |
11763 | |
11764 | |
11765 | |
11766 | assert(S.isSameOrCompatibleFunctionType( |
11767 | Context.getCanonicalType(Specialization->getType()), |
11768 | Context.getCanonicalType(TargetFunctionType))); |
11769 | |
11770 | if (!S.checkAddressOfFunctionIsAvailable(Specialization)) |
11771 | return false; |
11772 | |
11773 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
11774 | return true; |
11775 | } |
11776 | |
11777 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
11778 | const DeclAccessPair& CurAccessFunPair) { |
11779 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
11780 | |
11781 | |
11782 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
11783 | return false; |
11784 | } |
11785 | else if (TargetTypeIsNonStaticMemberFunction) |
11786 | return false; |
11787 | |
11788 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
11789 | if (S.getLangOpts().CUDA) |
11790 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
11791 | if (!Caller->isImplicit() && !S.IsAllowedCUDACall(Caller, FunDecl)) |
11792 | return false; |
11793 | if (FunDecl->isMultiVersion()) { |
11794 | const auto *TA = FunDecl->getAttr<TargetAttr>(); |
11795 | if (TA && !TA->isDefaultVersion()) |
11796 | return false; |
11797 | } |
11798 | |
11799 | |
11800 | |
11801 | if (completeFunctionType(S, FunDecl, SourceExpr->getBeginLoc(), |
11802 | Complain)) { |
11803 | HasComplained |= Complain; |
11804 | return false; |
11805 | } |
11806 | |
11807 | if (!S.checkAddressOfFunctionIsAvailable(FunDecl)) |
11808 | return false; |
11809 | |
11810 | |
11811 | if (!S.getLangOpts().CPlusPlus || |
11812 | candidateHasExactlyCorrectType(FunDecl)) { |
11813 | Matches.push_back(std::make_pair( |
11814 | CurAccessFunPair, cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
11815 | FoundNonTemplateFunction = true; |
11816 | return true; |
11817 | } |
11818 | } |
11819 | |
11820 | return false; |
11821 | } |
11822 | |
11823 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
11824 | bool Ret = false; |
11825 | |
11826 | |
11827 | |
11828 | if (IsInvalidFormOfPointerToMemberFunction()) |
11829 | return false; |
11830 | |
11831 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
11832 | E = OvlExpr->decls_end(); |
11833 | I != E; ++I) { |
11834 | |
11835 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
11836 | |
11837 | |
11838 | |
11839 | |
11840 | |
11841 | |
11842 | |
11843 | if (FunctionTemplateDecl *FunctionTemplate |
11844 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
11845 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
11846 | Ret = true; |
11847 | } |
11848 | |
11849 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
11850 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
11851 | Ret = true; |
11852 | } |
11853 | assert(Ret || Matches.empty()); |
11854 | return Ret; |
11855 | } |
11856 | |
11857 | void EliminateAllExceptMostSpecializedTemplate() { |
11858 | |
11859 | |
11860 | |
11861 | |
11862 | |
11863 | |
11864 | |
11865 | |
11866 | |
11867 | |
11868 | |
11869 | UnresolvedSet<4> MatchesCopy; |
11870 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
11871 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
11872 | |
11873 | |
11874 | |
11875 | UnresolvedSetIterator Result = S.getMostSpecialized( |
11876 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
11877 | SourceExpr->getBeginLoc(), S.PDiag(), |
11878 | S.PDiag(diag::err_addr_ovl_ambiguous) |
11879 | << Matches[0].second->getDeclName(), |
11880 | S.PDiag(diag::note_ovl_candidate) |
11881 | << (unsigned)oc_function << (unsigned)ocs_described_template, |
11882 | Complain, TargetFunctionType); |
11883 | |
11884 | if (Result != MatchesCopy.end()) { |
11885 | |
11886 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
11887 | Matches[0].second = cast<FunctionDecl>(*Result); |
11888 | Matches.resize(1); |
11889 | } else |
11890 | HasComplained |= Complain; |
11891 | } |
11892 | |
11893 | void EliminateAllTemplateMatches() { |
11894 | |
11895 | |
11896 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
11897 | if (Matches[I].second->getPrimaryTemplate() == nullptr) |
11898 | ++I; |
11899 | else { |
11900 | Matches[I] = Matches[--N]; |
11901 | Matches.resize(N); |
11902 | } |
11903 | } |
11904 | } |
11905 | |
11906 | void EliminateSuboptimalCudaMatches() { |
11907 | S.EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(S.CurContext), Matches); |
11908 | } |
11909 | |
11910 | public: |
11911 | void ComplainNoMatchesFound() const { |
11912 | assert(Matches.empty()); |
11913 | S.Diag(OvlExpr->getBeginLoc(), diag::err_addr_ovl_no_viable) |
11914 | << OvlExpr->getName() << TargetFunctionType |
11915 | << OvlExpr->getSourceRange(); |
11916 | if (FailedCandidates.empty()) |
11917 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType, |
11918 | true); |
11919 | else { |
11920 | |
11921 | |
11922 | |
11923 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
11924 | IEnd = OvlExpr->decls_end(); |
11925 | I != IEnd; ++I) |
11926 | if (FunctionDecl *Fun = |
11927 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
11928 | if (!functionHasPassObjectSizeParams(Fun)) |
11929 | S.NoteOverloadCandidate(*I, Fun, CRK_None, TargetFunctionType, |
11930 | true); |
11931 | FailedCandidates.NoteCandidates(S, OvlExpr->getBeginLoc()); |
11932 | } |
11933 | } |
11934 | |
11935 | bool IsInvalidFormOfPointerToMemberFunction() const { |
11936 | return TargetTypeIsNonStaticMemberFunction && |
11937 | !OvlExprInfo.HasFormOfMemberPointer; |
11938 | } |
11939 | |
11940 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
11941 | |
11942 | |
11943 | |
11944 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
11945 | << TargetType << OvlExpr->getSourceRange(); |
11946 | } |
11947 | |
11948 | bool IsStaticMemberFunctionFromBoundPointer() const { |
11949 | return StaticMemberFunctionFromBoundPointer; |
11950 | } |
11951 | |
11952 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
11953 | S.Diag(OvlExpr->getBeginLoc(), |
11954 | diag::err_invalid_form_pointer_member_function) |
11955 | << OvlExpr->getSourceRange(); |
11956 | } |
11957 | |
11958 | void ComplainOfInvalidConversion() const { |
11959 | S.Diag(OvlExpr->getBeginLoc(), diag::err_addr_ovl_not_func_ptrref) |
11960 | << OvlExpr->getName() << TargetType; |
11961 | } |
11962 | |
11963 | void ComplainMultipleMatchesFound() const { |
11964 | assert(Matches.size() > 1); |
11965 | S.Diag(OvlExpr->getBeginLoc(), diag::err_addr_ovl_ambiguous) |
11966 | << OvlExpr->getName() << OvlExpr->getSourceRange(); |
11967 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType, |
11968 | true); |
11969 | } |
11970 | |
11971 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
11972 | |
11973 | int getNumMatches() const { return Matches.size(); } |
11974 | |
11975 | FunctionDecl* getMatchingFunctionDecl() const { |
11976 | if (Matches.size() != 1) return nullptr; |
11977 | return Matches[0].second; |
11978 | } |
11979 | |
11980 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
11981 | if (Matches.size() != 1) return nullptr; |
11982 | return &Matches[0].first; |
11983 | } |
11984 | }; |
11985 | } |
11986 | |
11987 | |
11988 | |
11989 | |
11990 | |
11991 | |
11992 | |
11993 | |
11994 | |
11995 | |
11996 | |
11997 | |
11998 | |
11999 | |
12000 | |
12001 | |
12002 | FunctionDecl * |
12003 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
12004 | QualType TargetType, |
12005 | bool Complain, |
12006 | DeclAccessPair &FoundResult, |
12007 | bool *pHadMultipleCandidates) { |
12008 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
12009 | |
12010 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
12011 | Complain); |
12012 | int NumMatches = Resolver.getNumMatches(); |
12013 | FunctionDecl *Fn = nullptr; |
12014 | bool ShouldComplain = Complain && !Resolver.hasComplained(); |
12015 | if (NumMatches == 0 && ShouldComplain) { |
12016 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
12017 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
12018 | else |
12019 | Resolver.ComplainNoMatchesFound(); |
12020 | } |
12021 | else if (NumMatches > 1 && ShouldComplain) |
12022 | Resolver.ComplainMultipleMatchesFound(); |
12023 | else if (NumMatches == 1) { |
12024 | Fn = Resolver.getMatchingFunctionDecl(); |
12025 | assert(Fn); |
12026 | if (auto *FPT = Fn->getType()->getAs<FunctionProtoType>()) |
12027 | ResolveExceptionSpec(AddressOfExpr->getExprLoc(), FPT); |
12028 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
12029 | if (Complain) { |
12030 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
12031 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
12032 | else |
12033 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
12034 | } |
12035 | } |
12036 | |
12037 | if (pHadMultipleCandidates) |
12038 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
12039 | return Fn; |
12040 | } |
12041 | |
12042 | |
12043 | |
12044 | |
12045 | |
12046 | |
12047 | |
12048 | |
12049 | FunctionDecl * |
12050 | Sema::resolveAddressOfSingleOverloadCandidate(Expr *E, DeclAccessPair &Pair) { |
12051 | OverloadExpr::FindResult R = OverloadExpr::find(E); |
12052 | OverloadExpr *Ovl = R.Expression; |
12053 | bool IsResultAmbiguous = false; |
12054 | FunctionDecl *Result = nullptr; |
12055 | DeclAccessPair DAP; |
12056 | SmallVector<FunctionDecl *, 2> AmbiguousDecls; |
12057 | |
12058 | auto CheckMoreConstrained = |
12059 | [&] (FunctionDecl *FD1, FunctionDecl *FD2) -> Optional<bool> { |
12060 | SmallVector<const Expr *, 1> AC1, AC2; |
12061 | FD1->getAssociatedConstraints(AC1); |
12062 | FD2->getAssociatedConstraints(AC2); |
12063 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
12064 | if (IsAtLeastAsConstrained(FD1, AC1, FD2, AC2, AtLeastAsConstrained1)) |
12065 | return None; |
12066 | if (IsAtLeastAsConstrained(FD2, AC2, FD1, AC1, AtLeastAsConstrained2)) |
12067 | return None; |
12068 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
12069 | return None; |
12070 | return AtLeastAsConstrained1; |
12071 | }; |
12072 | |
12073 | |
12074 | |
12075 | |
12076 | for (auto I = Ovl->decls_begin(), E = Ovl->decls_end(); I != E; ++I) { |
12077 | auto *FD = dyn_cast<FunctionDecl>(I->getUnderlyingDecl()); |
12078 | if (!FD) |
12079 | return nullptr; |
12080 | |
12081 | if (!checkAddressOfFunctionIsAvailable(FD)) |
12082 | continue; |
12083 | |
12084 | |
12085 | |
12086 | if (Result) { |
12087 | Optional<bool> MoreConstrainedThanPrevious = CheckMoreConstrained(FD, |
12088 | Result); |
12089 | if (!MoreConstrainedThanPrevious) { |
12090 | IsResultAmbiguous = true; |
12091 | AmbiguousDecls.push_back(FD); |
12092 | continue; |
12093 | } |
12094 | if (!*MoreConstrainedThanPrevious) |
12095 | continue; |
12096 | |
12097 | } |
12098 | IsResultAmbiguous = false; |
12099 | DAP = I.getPair(); |
12100 | Result = FD; |
12101 | } |
12102 | |
12103 | if (IsResultAmbiguous) |
12104 | return nullptr; |
12105 | |
12106 | if (Result) { |
12107 | SmallVector<const Expr *, 1> ResultAC; |
12108 | |
12109 | |
12110 | for (FunctionDecl *Skipped : AmbiguousDecls) |
12111 | if (!CheckMoreConstrained(Skipped, Result).hasValue()) |
12112 | return nullptr; |
12113 | Pair = DAP; |
12114 | } |
12115 | return Result; |
12116 | } |
12117 | |
12118 | |
12119 | |
12120 | |
12121 | |
12122 | |
12123 | |
12124 | |
12125 | bool Sema::resolveAndFixAddressOfSingleOverloadCandidate( |
12126 | ExprResult &SrcExpr, bool DoFunctionPointerConverion) { |
12127 | Expr *E = SrcExpr.get(); |
12128 | assert(E->getType() == Context.OverloadTy && "SrcExpr must be an overload"); |
12129 | |
12130 | DeclAccessPair DAP; |
12131 | FunctionDecl *Found = resolveAddressOfSingleOverloadCandidate(E, DAP); |
12132 | if (!Found || Found->isCPUDispatchMultiVersion() || |
12133 | Found->isCPUSpecificMultiVersion()) |
12134 | return false; |
12135 | |
12136 | |
12137 | |
12138 | |
12139 | DiagnoseUseOfDecl(Found, E->getExprLoc()); |
12140 | CheckAddressOfMemberAccess(E, DAP); |
12141 | Expr *Fixed = FixOverloadedFunctionReference(E, DAP, Found); |
12142 | if (DoFunctionPointerConverion && Fixed->getType()->isFunctionType()) |
12143 | SrcExpr = DefaultFunctionArrayConversion(Fixed, false); |
12144 | else |
12145 | SrcExpr = Fixed; |
12146 | return true; |
12147 | } |
12148 | |
12149 | |
12150 | |
12151 | |
12152 | |
12153 | |
12154 | |
12155 | |
12156 | |
12157 | |
12158 | |
12159 | FunctionDecl * |
12160 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
12161 | bool Complain, |
12162 | DeclAccessPair *FoundResult) { |
12163 | |
12164 | |
12165 | |
12166 | |
12167 | |
12168 | |
12169 | |
12170 | |
12171 | if (!ovl->hasExplicitTemplateArgs()) |
12172 | return nullptr; |
12173 | |
12174 | TemplateArgumentListInfo ExplicitTemplateArgs; |
12175 | ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
12176 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
12177 | |
12178 | |
12179 | |
12180 | FunctionDecl *Matched = nullptr; |
12181 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
12182 | E = ovl->decls_end(); I != E; ++I) { |
12183 | |
12184 | |
12185 | |
12186 | |
12187 | |
12188 | |
12189 | FunctionTemplateDecl *FunctionTemplate |
12190 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
12191 | |
12192 | |
12193 | |
12194 | |
12195 | |
12196 | |
12197 | |
12198 | FunctionDecl *Specialization = nullptr; |
12199 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
12200 | if (TemplateDeductionResult Result |
12201 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
12202 | Specialization, Info, |
12203 | true)) { |
12204 | |
12205 | |
12206 | FailedCandidates.addCandidate() |
12207 | .set(I.getPair(), FunctionTemplate->getTemplatedDecl(), |
12208 | MakeDeductionFailureInfo(Context, Result, Info)); |
12209 | continue; |
12210 | } |
12211 | |
12212 | assert(Specialization && "no specialization and no error?"); |
12213 | |
12214 | |
12215 | if (Matched) { |
12216 | if (Complain) { |
12217 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
12218 | << ovl->getName(); |
12219 | NoteAllOverloadCandidates(ovl); |
12220 | } |
12221 | return nullptr; |
12222 | } |
12223 | |
12224 | Matched = Specialization; |
12225 | if (FoundResult) *FoundResult = I.getPair(); |
12226 | } |
12227 | |
12228 | if (Matched && |
12229 | completeFunctionType(*this, Matched, ovl->getExprLoc(), Complain)) |
12230 | return nullptr; |
12231 | |
12232 | return Matched; |
12233 | } |
12234 | |
12235 | |
12236 | |
12237 | |
12238 | |
12239 | |
12240 | |
12241 | |
12242 | |
12243 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
12244 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
12245 | bool complain, SourceRange OpRangeForComplaining, |
12246 | QualType DestTypeForComplaining, |
12247 | unsigned DiagIDForComplaining) { |
12248 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
12249 | |
12250 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
12251 | |
12252 | DeclAccessPair found; |
12253 | ExprResult SingleFunctionExpression; |
12254 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
12255 | ovl.Expression, false, &found)) { |
12256 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getBeginLoc())) { |
12257 | SrcExpr = ExprError(); |
12258 | return true; |
12259 | } |
12260 | |
12261 | |
12262 | |
12263 | |
12264 | |
12265 | if (!ovl.HasFormOfMemberPointer && |
12266 | isa<CXXMethodDecl>(fn) && |
12267 | cast<CXXMethodDecl>(fn)->isInstance()) { |
12268 | if (!complain) return false; |
12269 | |
12270 | Diag(ovl.Expression->getExprLoc(), |
12271 | diag::err_bound_member_function) |
12272 | << 0 << ovl.Expression->getSourceRange(); |
12273 | |
12274 | |
12275 | |
12276 | |
12277 | |
12278 | |
12279 | SrcExpr = ExprError(); |
12280 | return true; |
12281 | } |
12282 | |
12283 | |
12284 | SingleFunctionExpression = |
12285 | FixOverloadedFunctionReference(SrcExpr.get(), found, fn); |
12286 | |
12287 | |
12288 | if (doFunctionPointerConverion) { |
12289 | SingleFunctionExpression = |
12290 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.get()); |
12291 | if (SingleFunctionExpression.isInvalid()) { |
12292 | SrcExpr = ExprError(); |
12293 | return true; |
12294 | } |
12295 | } |
12296 | } |
12297 | |
12298 | if (!SingleFunctionExpression.isUsable()) { |
12299 | if (complain) { |
12300 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
12301 | << ovl.Expression->getName() |
12302 | << DestTypeForComplaining |
12303 | << OpRangeForComplaining |
12304 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
12305 | NoteAllOverloadCandidates(SrcExpr.get()); |
12306 | |
12307 | SrcExpr = ExprError(); |
12308 | return true; |
12309 | } |
12310 | |
12311 | return false; |
12312 | } |
12313 | |
12314 | SrcExpr = SingleFunctionExpression; |
12315 | return true; |
12316 | } |
12317 | |
12318 | |
12319 | static void AddOverloadedCallCandidate(Sema &S, |
12320 | DeclAccessPair FoundDecl, |
12321 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
12322 | ArrayRef<Expr *> Args, |
12323 | OverloadCandidateSet &CandidateSet, |
12324 | bool PartialOverloading, |
12325 | bool KnownValid) { |
12326 | NamedDecl *Callee = FoundDecl.getDecl(); |
12327 | if (isa<UsingShadowDecl>(Callee)) |
12328 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
12329 | |
12330 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
12331 | if (ExplicitTemplateArgs) { |
12332 | assert(!KnownValid && "Explicit template arguments?"); |
12333 | return; |
12334 | } |
12335 | |
12336 | if (!dyn_cast<FunctionProtoType>(Func->getType()->getAs<FunctionType>())) |
12337 | return; |
12338 | |
12339 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, |
12340 | false, |
12341 | PartialOverloading); |
12342 | return; |
12343 | } |
12344 | |
12345 | if (FunctionTemplateDecl *FuncTemplate |
12346 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
12347 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
12348 | ExplicitTemplateArgs, Args, CandidateSet, |
12349 | false, |
12350 | PartialOverloading); |
12351 | return; |
12352 | } |
12353 | |
12354 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
12355 | } |
12356 | |
12357 | |
12358 | |
12359 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
12360 | ArrayRef<Expr *> Args, |
12361 | OverloadCandidateSet &CandidateSet, |
12362 | bool PartialOverloading) { |
12363 | |
12364 | #ifndef NDEBUG |
12365 | |
12366 | |
12367 | |
12368 | |
12369 | |
12370 | |
12371 | |
12372 | |
12373 | |
12374 | |
12375 | |
12376 | |
12377 | |
12378 | |
12379 | |
12380 | |
12381 | |
12382 | if (ULE->requiresADL()) { |
12383 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
12384 | E = ULE->decls_end(); I != E; ++I) { |
12385 | assert(!(*I)->getDeclContext()->isRecord()); |
12386 | assert(isa<UsingShadowDecl>(*I) || |
12387 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
12388 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
12389 | } |
12390 | } |
12391 | #endif |
12392 | |
12393 | |
12394 | TemplateArgumentListInfo TABuffer; |
12395 | TemplateArgumentListInfo *ExplicitTemplateArgs = nullptr; |
12396 | if (ULE->hasExplicitTemplateArgs()) { |
12397 | ULE->copyTemplateArgumentsInto(TABuffer); |
12398 | ExplicitTemplateArgs = &TABuffer; |
12399 | } |
12400 | |
12401 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
12402 | E = ULE->decls_end(); I != E; ++I) |
12403 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
12404 | CandidateSet, PartialOverloading, |
12405 | true); |
12406 | |
12407 | if (ULE->requiresADL()) |
12408 | AddArgumentDependentLookupCandidates(ULE->getName(), ULE->getExprLoc(), |
12409 | Args, ExplicitTemplateArgs, |
12410 | CandidateSet, PartialOverloading); |
12411 | } |
12412 | |
12413 | |
12414 | |
12415 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
12416 | switch (Name.getCXXOverloadedOperator()) { |
12417 | case OO_New: case OO_Array_New: |
12418 | case OO_Delete: case OO_Array_Delete: |
12419 | return false; |
12420 | |
12421 | default: |
12422 | return true; |
12423 | } |
12424 | } |
12425 | |
12426 | |
12427 | |
12428 | |
12429 | |
12430 | |
12431 | |
12432 | static bool |
12433 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
12434 | const CXXScopeSpec &SS, LookupResult &R, |
12435 | OverloadCandidateSet::CandidateSetKind CSK, |
12436 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
12437 | ArrayRef<Expr *> Args, |
12438 | bool *DoDiagnoseEmptyLookup = nullptr) { |
12439 | if (!SemaRef.inTemplateInstantiation() || !SS.isEmpty()) |
12440 | return false; |
12441 | |
12442 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
12443 | if (DC->isTransparentContext()) |
12444 | continue; |
12445 | |
12446 | SemaRef.LookupQualifiedName(R, DC); |
12447 | |
12448 | if (!R.empty()) { |
12449 | R.suppressDiagnostics(); |
12450 | |
12451 | if (isa<CXXRecordDecl>(DC)) { |
12452 | |
12453 | |
12454 | R.clear(); |
12455 | if (DoDiagnoseEmptyLookup) |
12456 | *DoDiagnoseEmptyLookup = true; |
12457 | return false; |
12458 | } |
12459 | |
12460 | OverloadCandidateSet Candidates(FnLoc, CSK); |
12461 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
12462 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
12463 | ExplicitTemplateArgs, Args, |
12464 | Candidates, false, false); |
12465 | |
12466 | OverloadCandidateSet::iterator Best; |
12467 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
12468 | |
12469 | |
12470 | R.clear(); |
12471 | return false; |
12472 | } |
12473 | |
12474 | |
12475 | |
12476 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
12477 | Sema::AssociatedClassSet AssociatedClasses; |
12478 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
12479 | AssociatedNamespaces, |
12480 | AssociatedClasses); |
12481 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
12482 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
12483 | DeclContext *Std = SemaRef.getStdNamespace(); |
12484 | for (Sema::AssociatedNamespaceSet::iterator |
12485 | it = AssociatedNamespaces.begin(), |
12486 | end = AssociatedNamespaces.end(); it != end; ++it) { |
12487 | |
12488 | if (Std && Std->Encloses(*it)) |
12489 | continue; |
12490 | |
12491 | |
12492 | |
12493 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
12494 | if (NS && |
12495 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
12496 | continue; |
12497 | |
12498 | SuggestedNamespaces.insert(*it); |
12499 | } |
12500 | } |
12501 | |
12502 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
12503 | << R.getLookupName(); |
12504 | if (SuggestedNamespaces.empty()) { |
12505 | SemaRef.Diag(Best->Function->getLocation(), |
12506 | diag::note_not_found_by_two_phase_lookup) |
12507 | << R.getLookupName() << 0; |
12508 | } else if (SuggestedNamespaces.size() == 1) { |
12509 | SemaRef.Diag(Best->Function->getLocation(), |
12510 | diag::note_not_found_by_two_phase_lookup) |
12511 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
12512 | } else { |
12513 | |
12514 | |
12515 | |
12516 | SemaRef.Diag(Best->Function->getLocation(), |
12517 | diag::note_not_found_by_two_phase_lookup) |
12518 | << R.getLookupName() << 2; |
12519 | } |
12520 | |
12521 | |
12522 | return true; |
12523 | } |
12524 | |
12525 | R.clear(); |
12526 | } |
12527 | |
12528 | return false; |
12529 | } |
12530 | |
12531 | |
12532 | |
12533 | |
12534 | |
12535 | |
12536 | static bool |
12537 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
12538 | SourceLocation OpLoc, |
12539 | ArrayRef<Expr *> Args) { |
12540 | DeclarationName OpName = |
12541 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
12542 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
12543 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
12544 | OverloadCandidateSet::CSK_Operator, |
12545 | nullptr, Args); |
12546 | } |
12547 | |
12548 | namespace { |
12549 | class BuildRecoveryCallExprRAII { |
12550 | Sema &SemaRef; |
12551 | public: |
12552 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
12553 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
12554 | SemaRef.IsBuildingRecoveryCallExpr = true; |
12555 | } |
12556 | |
12557 | ~BuildRecoveryCallExprRAII() { |
12558 | SemaRef.IsBuildingRecoveryCallExpr = false; |
12559 | } |
12560 | }; |
12561 | |
12562 | } |
12563 | |
12564 | |
12565 | |
12566 | |
12567 | static ExprResult |
12568 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
12569 | UnresolvedLookupExpr *ULE, |
12570 | SourceLocation LParenLoc, |
12571 | MutableArrayRef<Expr *> Args, |
12572 | SourceLocation RParenLoc, |
12573 | bool EmptyLookup, bool AllowTypoCorrection) { |
12574 | |
12575 | |
12576 | |
12577 | |
12578 | |
12579 | |
12580 | if (SemaRef.IsBuildingRecoveryCallExpr) |
12581 | return ExprError(); |
12582 | BuildRecoveryCallExprRAII RCE(SemaRef); |
12583 | |
12584 | CXXScopeSpec SS; |
12585 | SS.Adopt(ULE->getQualifierLoc()); |
12586 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
12587 | |
12588 | TemplateArgumentListInfo TABuffer; |
12589 | TemplateArgumentListInfo *ExplicitTemplateArgs = nullptr; |
12590 | if (ULE->hasExplicitTemplateArgs()) { |
12591 | ULE->copyTemplateArgumentsInto(TABuffer); |
12592 | ExplicitTemplateArgs = &TABuffer; |
12593 | } |
12594 | |
12595 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
12596 | Sema::LookupOrdinaryName); |
12597 | bool DoDiagnoseEmptyLookup = EmptyLookup; |
12598 | if (!DiagnoseTwoPhaseLookup( |
12599 | SemaRef, Fn->getExprLoc(), SS, R, OverloadCandidateSet::CSK_Normal, |
12600 | ExplicitTemplateArgs, Args, &DoDiagnoseEmptyLookup)) { |
12601 | NoTypoCorrectionCCC NoTypoValidator{}; |
12602 | FunctionCallFilterCCC FunctionCallValidator(SemaRef, Args.size(), |
12603 | ExplicitTemplateArgs != nullptr, |
12604 | dyn_cast<MemberExpr>(Fn)); |
12605 | CorrectionCandidateCallback &Validator = |
12606 | AllowTypoCorrection |
12607 | ? static_cast<CorrectionCandidateCallback &>(FunctionCallValidator) |
12608 | : static_cast<CorrectionCandidateCallback &>(NoTypoValidator); |
12609 | if (!DoDiagnoseEmptyLookup || |
12610 | SemaRef.DiagnoseEmptyLookup(S, SS, R, Validator, ExplicitTemplateArgs, |
12611 | Args)) |
12612 | return ExprError(); |
12613 | } |
12614 | |
12615 | assert(!R.empty() && "lookup results empty despite recovery"); |
12616 | |
12617 | |
12618 | if (R.isAmbiguous()) { |
12619 | R.suppressDiagnostics(); |
12620 | return ExprError(); |
12621 | } |
12622 | |
12623 | |
12624 | |
12625 | ExprResult NewFn = ExprError(); |
12626 | if ((*R.begin())->isCXXClassMember()) |
12627 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, R, |
12628 | ExplicitTemplateArgs, S); |
12629 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
12630 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
12631 | ExplicitTemplateArgs); |
12632 | else |
12633 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
12634 | |
12635 | if (NewFn.isInvalid()) |
12636 | return ExprError(); |
12637 | |
12638 | |
12639 | |
12640 | |
12641 | return SemaRef.BuildCallExpr( nullptr, NewFn.get(), LParenLoc, |
12642 | MultiExprArg(Args.data(), Args.size()), |
12643 | RParenLoc); |
12644 | } |
12645 | |
12646 | |
12647 | |
12648 | |
12649 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
12650 | UnresolvedLookupExpr *ULE, |
12651 | MultiExprArg Args, |
12652 | SourceLocation RParenLoc, |
12653 | OverloadCandidateSet *CandidateSet, |
12654 | ExprResult *Result) { |
12655 | #ifndef NDEBUG |
12656 | if (ULE->requiresADL()) { |
12657 | |
12658 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
12659 | |
12660 | |
12661 | |
12662 | FunctionDecl *F; |
12663 | if (ULE->decls_begin() != ULE->decls_end() && |
12664 | ULE->decls_begin() + 1 == ULE->decls_end() && |
12665 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
12666 | F->getBuiltinID() && F->isImplicit()) |
12667 | llvm_unreachable("performing ADL for builtin"); |
12668 | |
12669 | |
12670 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
12671 | } |
12672 | #endif |
12673 | |
12674 | UnbridgedCastsSet UnbridgedCasts; |
12675 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
12676 | *Result = ExprError(); |
12677 | return true; |
12678 | } |
12679 | |
12680 | |
12681 | |
12682 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
12683 | |
12684 | if (getLangOpts().MSVCCompat && |
12685 | CurContext->isDependentContext() && !isSFINAEContext() && |
12686 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
12687 | |
12688 | OverloadCandidateSet::iterator Best; |
12689 | if (CandidateSet->empty() || |
12690 | CandidateSet->BestViableFunction(*this, Fn->getBeginLoc(), Best) == |
12691 | OR_No_Viable_Function) { |
12692 | |
12693 | |
12694 | |
12695 | |
12696 | CallExpr *CE = CallExpr::Create(Context, Fn, Args, Context.DependentTy, |
12697 | VK_RValue, RParenLoc); |
12698 | CE->setTypeDependent(true); |
12699 | CE->setValueDependent(true); |
12700 | CE->setInstantiationDependent(true); |
12701 | *Result = CE; |
12702 | return true; |
12703 | } |
12704 | } |
12705 | |
12706 | if (CandidateSet->empty()) |
12707 | return false; |
12708 | |
12709 | UnbridgedCasts.restore(); |
12710 | return false; |
12711 | } |
12712 | |
12713 | |
12714 | |
12715 | |
12716 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
12717 | UnresolvedLookupExpr *ULE, |
12718 | SourceLocation LParenLoc, |
12719 | MultiExprArg Args, |
12720 | SourceLocation RParenLoc, |
12721 | Expr *ExecConfig, |
12722 | OverloadCandidateSet *CandidateSet, |
12723 | OverloadCandidateSet::iterator *Best, |
12724 | OverloadingResult OverloadResult, |
12725 | bool AllowTypoCorrection) { |
12726 | if (CandidateSet->empty()) |
12727 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
12728 | RParenLoc, true, |
12729 | AllowTypoCorrection); |
12730 | |
12731 | switch (OverloadResult) { |
12732 | case OR_Success: { |
12733 | FunctionDecl *FDecl = (*Best)->Function; |
12734 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
12735 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
12736 | return ExprError(); |
12737 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
12738 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
12739 | ExecConfig, false, |
12740 | (*Best)->IsADLCandidate); |
12741 | } |
12742 | |
12743 | case OR_No_Viable_Function: { |
12744 | |
12745 | |
12746 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
12747 | Args, RParenLoc, |
12748 | false, |
12749 | AllowTypoCorrection); |
12750 | if (!Recovery.isInvalid()) |
12751 | return Recovery; |
12752 | |
12753 | |
12754 | |
12755 | |
12756 | for (const Expr *Arg : Args) { |
12757 | if (!Arg->getType()->isFunctionType()) |
12758 | continue; |
12759 | if (auto *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts())) { |
12760 | auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
12761 | if (FD && |
12762 | !SemaRef.checkAddressOfFunctionIsAvailable(FD, true, |
12763 | Arg->getExprLoc())) |
12764 | return ExprError(); |
12765 | } |
12766 | } |
12767 | |
12768 | CandidateSet->NoteCandidates( |
12769 | PartialDiagnosticAt( |
12770 | Fn->getBeginLoc(), |
12771 | SemaRef.PDiag(diag::err_ovl_no_viable_function_in_call) |
12772 | << ULE->getName() << Fn->getSourceRange()), |
12773 | SemaRef, OCD_AllCandidates, Args); |
12774 | break; |
12775 | } |
12776 | |
12777 | case OR_Ambiguous: |
12778 | CandidateSet->NoteCandidates( |
12779 | PartialDiagnosticAt(Fn->getBeginLoc(), |
12780 | SemaRef.PDiag(diag::err_ovl_ambiguous_call) |
12781 | << ULE->getName() << Fn->getSourceRange()), |
12782 | SemaRef, OCD_AmbiguousCandidates, Args); |
12783 | break; |
12784 | |
12785 | case OR_Deleted: { |
12786 | CandidateSet->NoteCandidates( |
12787 | PartialDiagnosticAt(Fn->getBeginLoc(), |
12788 | SemaRef.PDiag(diag::err_ovl_deleted_call) |
12789 | << ULE->getName() << Fn->getSourceRange()), |
12790 | SemaRef, OCD_AllCandidates, Args); |
12791 | |
12792 | |
12793 | |
12794 | FunctionDecl *FDecl = (*Best)->Function; |
12795 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
12796 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
12797 | ExecConfig, false, |
12798 | (*Best)->IsADLCandidate); |
12799 | } |
12800 | } |
12801 | |
12802 | |
12803 | return ExprError(); |
12804 | } |
12805 | |
12806 | static void markUnaddressableCandidatesUnviable(Sema &S, |
12807 | OverloadCandidateSet &CS) { |
12808 | for (auto I = CS.begin(), E = CS.end(); I != E; ++I) { |
12809 | if (I->Viable && |
12810 | !S.checkAddressOfFunctionIsAvailable(I->Function, false)) { |
12811 | I->Viable = false; |
12812 | I->FailureKind = ovl_fail_addr_not_available; |
12813 | } |
12814 | } |
12815 | } |
12816 | |
12817 | |
12818 | |
12819 | |
12820 | |
12821 | |
12822 | |
12823 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
12824 | UnresolvedLookupExpr *ULE, |
12825 | SourceLocation LParenLoc, |
12826 | MultiExprArg Args, |
12827 | SourceLocation RParenLoc, |
12828 | Expr *ExecConfig, |
12829 | bool AllowTypoCorrection, |
12830 | bool CalleesAddressIsTaken) { |
12831 | OverloadCandidateSet CandidateSet(Fn->getExprLoc(), |
12832 | OverloadCandidateSet::CSK_Normal); |
12833 | ExprResult result; |
12834 | |
12835 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
12836 | &result)) |
12837 | return result; |
12838 | |
12839 | |
12840 | |
12841 | if (CalleesAddressIsTaken) |
12842 | markUnaddressableCandidatesUnviable(*this, CandidateSet); |
12843 | |
12844 | OverloadCandidateSet::iterator Best; |
12845 | OverloadingResult OverloadResult = |
12846 | CandidateSet.BestViableFunction(*this, Fn->getBeginLoc(), Best); |
12847 | |
12848 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, RParenLoc, |
12849 | ExecConfig, &CandidateSet, &Best, |
12850 | OverloadResult, AllowTypoCorrection); |
12851 | } |
12852 | |
12853 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
12854 | return Functions.size() > 1 || |
12855 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
12856 | } |
12857 | |
12858 | |
12859 | |
12860 | |
12861 | |
12862 | |
12863 | |
12864 | |
12865 | |
12866 | |
12867 | |
12868 | |
12869 | |
12870 | |
12871 | |
12872 | |
12873 | ExprResult |
12874 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc, |
12875 | const UnresolvedSetImpl &Fns, |
12876 | Expr *Input, bool PerformADL) { |
12877 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
12878 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
12879 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
12880 | |
12881 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
12882 | |
12883 | if (checkPlaceholderForOverload(*this, Input)) |
12884 | return ExprError(); |
12885 | |
12886 | Expr *Args[2] = { Input, nullptr }; |
12887 | unsigned NumArgs = 1; |
12888 | |
12889 | |
12890 | |
12891 | |
12892 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
12893 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
12894 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
12895 | SourceLocation()); |
12896 | NumArgs = 2; |
12897 | } |
12898 | |
12899 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
12900 | |
12901 | if (Input->isTypeDependent()) { |
12902 | if (Fns.empty()) |
12903 | return new (Context) UnaryOperator(Input, Opc, Context.DependentTy, |
12904 | VK_RValue, OK_Ordinary, OpLoc, false); |
12905 | |
12906 | CXXRecordDecl *NamingClass = nullptr; |
12907 | UnresolvedLookupExpr *Fn = UnresolvedLookupExpr::Create( |
12908 | Context, NamingClass, NestedNameSpecifierLoc(), OpNameInfo, |
12909 | true, IsOverloaded(Fns), Fns.begin(), Fns.end()); |
12910 | return CXXOperatorCallExpr::Create(Context, Op, Fn, ArgsArray, |
12911 | Context.DependentTy, VK_RValue, OpLoc, |
12912 | FPOptions()); |
12913 | } |
12914 | |
12915 | |
12916 | OverloadCandidateSet CandidateSet(OpLoc, OverloadCandidateSet::CSK_Operator); |
12917 | |
12918 | |
12919 | AddNonMemberOperatorCandidates(Fns, ArgsArray, CandidateSet); |
12920 | |
12921 | |
12922 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
12923 | |
12924 | |
12925 | if (PerformADL) { |
12926 | AddArgumentDependentLookupCandidates(OpName, OpLoc, ArgsArray, |
12927 | nullptr, |
12928 | CandidateSet); |
12929 | } |
12930 | |
12931 | |
12932 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
12933 | |
12934 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
12935 | |
12936 | |
12937 | OverloadCandidateSet::iterator Best; |
12938 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
12939 | case OR_Success: { |
12940 | |
12941 | FunctionDecl *FnDecl = Best->Function; |
12942 | |
12943 | if (FnDecl) { |
12944 | Expr *Base = nullptr; |
12945 | |
12946 | |
12947 | |
12948 | |
12949 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
12950 | CheckMemberOperatorAccess(OpLoc, Args[0], nullptr, Best->FoundDecl); |
12951 | |
12952 | ExprResult InputRes = |
12953 | PerformObjectArgumentInitialization(Input, nullptr, |
12954 | Best->FoundDecl, Method); |
12955 | if (InputRes.isInvalid()) |
12956 | return ExprError(); |
12957 | Base = Input = InputRes.get(); |
12958 | } else { |
12959 | |
12960 | ExprResult InputInit |
12961 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
12962 | Context, |
12963 | FnDecl->getParamDecl(0)), |
12964 | SourceLocation(), |
12965 | Input); |
12966 | if (InputInit.isInvalid()) |
12967 | return ExprError(); |
12968 | Input = InputInit.get(); |
12969 | } |
12970 | |
12971 | |
12972 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
12973 | Base, HadMultipleCandidates, |
12974 | OpLoc); |
12975 | if (FnExpr.isInvalid()) |
12976 | return ExprError(); |
12977 | |
12978 | |
12979 | QualType ResultTy = FnDecl->getReturnType(); |
12980 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
12981 | ResultTy = ResultTy.getNonLValueExprType(Context); |
12982 | |
12983 | Args[0] = Input; |
12984 | CallExpr *TheCall = CXXOperatorCallExpr::Create( |
12985 | Context, Op, FnExpr.get(), ArgsArray, ResultTy, VK, OpLoc, |
12986 | FPOptions(), Best->IsADLCandidate); |
12987 | |
12988 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, FnDecl)) |
12989 | return ExprError(); |
12990 | |
12991 | if (CheckFunctionCall(FnDecl, TheCall, |
12992 | FnDecl->getType()->castAs<FunctionProtoType>())) |
12993 | return ExprError(); |
12994 | |
12995 | return MaybeBindToTemporary(TheCall); |
12996 | } else { |
12997 | |
12998 | |
12999 | |
13000 | ExprResult InputRes = PerformImplicitConversion( |
13001 | Input, Best->BuiltinParamTypes[0], Best->Conversions[0], AA_Passing, |
13002 | CCK_ForBuiltinOverloadedOp); |
13003 | if (InputRes.isInvalid()) |
13004 | return ExprError(); |
13005 | Input = InputRes.get(); |
13006 | break; |
13007 | } |
13008 | } |
13009 | |
13010 | case OR_No_Viable_Function: |
13011 | |
13012 | |
13013 | |
13014 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
13015 | |
13016 | return ExprError(); |
13017 | |
13018 | |
13019 | |
13020 | break; |
13021 | |
13022 | case OR_Ambiguous: |
13023 | CandidateSet.NoteCandidates( |
13024 | PartialDiagnosticAt(OpLoc, |
13025 | PDiag(diag::err_ovl_ambiguous_oper_unary) |
13026 | << UnaryOperator::getOpcodeStr(Opc) |
13027 | << Input->getType() << Input->getSourceRange()), |
13028 | *this, OCD_AmbiguousCandidates, ArgsArray, |
13029 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
13030 | return ExprError(); |
13031 | |
13032 | case OR_Deleted: |
13033 | CandidateSet.NoteCandidates( |
13034 | PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_deleted_oper) |
13035 | << UnaryOperator::getOpcodeStr(Opc) |
13036 | << Input->getSourceRange()), |
13037 | *this, OCD_AllCandidates, ArgsArray, UnaryOperator::getOpcodeStr(Opc), |
13038 | OpLoc); |
13039 | return ExprError(); |
13040 | } |
13041 | |
13042 | |
13043 | |
13044 | |
13045 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
13046 | } |
13047 | |
13048 | |
13049 | void Sema::LookupOverloadedBinOp(OverloadCandidateSet &CandidateSet, |
13050 | OverloadedOperatorKind Op, |
13051 | const UnresolvedSetImpl &Fns, |
13052 | ArrayRef<Expr *> Args, bool PerformADL) { |
13053 | SourceLocation OpLoc = CandidateSet.getLocation(); |
13054 | |
13055 | OverloadedOperatorKind ExtraOp = |
13056 | CandidateSet.getRewriteInfo().AllowRewrittenCandidates |
13057 | ? getRewrittenOverloadedOperator(Op) |
13058 | : OO_None; |
13059 | |
13060 | |
13061 | |
13062 | AddNonMemberOperatorCandidates(Fns, Args, CandidateSet); |
13063 | |
13064 | |
13065 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
13066 | if (CandidateSet.getRewriteInfo().shouldAddReversed(Op)) |
13067 | AddMemberOperatorCandidates(Op, OpLoc, {Args[1], Args[0]}, CandidateSet, |
13068 | OverloadCandidateParamOrder::Reversed); |
13069 | |
13070 | |
13071 | if (ExtraOp) { |
13072 | AddMemberOperatorCandidates(ExtraOp, OpLoc, Args, CandidateSet); |
13073 | if (CandidateSet.getRewriteInfo().shouldAddReversed(ExtraOp)) |
13074 | AddMemberOperatorCandidates(ExtraOp, OpLoc, {Args[1], Args[0]}, |
13075 | CandidateSet, |
13076 | OverloadCandidateParamOrder::Reversed); |
13077 | } |
13078 | |
13079 | |
13080 | |
13081 | |
13082 | if (Op != OO_Equal && PerformADL) { |
13083 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
13084 | AddArgumentDependentLookupCandidates(OpName, OpLoc, Args, |
13085 | nullptr, |
13086 | CandidateSet); |
13087 | if (ExtraOp) { |
13088 | DeclarationName ExtraOpName = |
13089 | Context.DeclarationNames.getCXXOperatorName(ExtraOp); |
13090 | AddArgumentDependentLookupCandidates(ExtraOpName, OpLoc, Args, |
13091 | nullptr, |
13092 | CandidateSet); |
13093 | } |
13094 | } |
13095 | |
13096 | |
13097 | |
13098 | |
13099 | |
13100 | |
13101 | |
13102 | |
13103 | |
13104 | |
13105 | |
13106 | |
13107 | |
13108 | |
13109 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
13110 | } |
13111 | |
13112 | |
13113 | |
13114 | |
13115 | |
13116 | |
13117 | |
13118 | |
13119 | |
13120 | |
13121 | |
13122 | |
13123 | |
13124 | |
13125 | |
13126 | |
13127 | |
13128 | |
13129 | |
13130 | |
13131 | |
13132 | |
13133 | |
13134 | |
13135 | ExprResult Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
13136 | BinaryOperatorKind Opc, |
13137 | const UnresolvedSetImpl &Fns, Expr *LHS, |
13138 | Expr *RHS, bool PerformADL, |
13139 | bool AllowRewrittenCandidates, |
13140 | FunctionDecl *DefaultedFn) { |
13141 | Expr *Args[2] = { LHS, RHS }; |
13142 | LHS=RHS=nullptr; |
13143 | |
13144 | if (!getLangOpts().CPlusPlus2a) |
13145 | AllowRewrittenCandidates = false; |
13146 | |
13147 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
13148 | |
13149 | |
13150 | |
13151 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
13152 | if (Fns.empty()) { |
13153 | |
13154 | |
13155 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
13156 | return new (Context) BinaryOperator( |
13157 | Args[0], Args[1], Opc, Context.DependentTy, VK_RValue, OK_Ordinary, |
13158 | OpLoc, FPFeatures); |
13159 | |
13160 | return new (Context) CompoundAssignOperator( |
13161 | Args[0], Args[1], Opc, Context.DependentTy, VK_LValue, OK_Ordinary, |
13162 | Context.DependentTy, Context.DependentTy, OpLoc, |
13163 | FPFeatures); |
13164 | } |
13165 | |
13166 | |
13167 | CXXRecordDecl *NamingClass = nullptr; |
13168 | |
13169 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
13170 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
13171 | UnresolvedLookupExpr *Fn = UnresolvedLookupExpr::Create( |
13172 | Context, NamingClass, NestedNameSpecifierLoc(), OpNameInfo, |
13173 | PerformADL, IsOverloaded(Fns), Fns.begin(), Fns.end()); |
13174 | return CXXOperatorCallExpr::Create(Context, Op, Fn, Args, |
13175 | Context.DependentTy, VK_RValue, OpLoc, |
13176 | FPFeatures); |
13177 | } |
13178 | |
13179 | |
13180 | if (checkPlaceholderForOverload(*this, Args[1])) |
13181 | return ExprError(); |
13182 | |
13183 | |
13184 | |
13185 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
13186 | if (checkPlaceholderForOverload(*this, Args[0])) |
13187 | return ExprError(); |
13188 | |
13189 | |
13190 | |
13191 | |
13192 | |
13193 | |
13194 | |
13195 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
13196 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
13197 | |
13198 | |
13199 | |
13200 | if (Opc == BO_PtrMemD) |
13201 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
13202 | |
13203 | |
13204 | OverloadCandidateSet CandidateSet( |
13205 | OpLoc, OverloadCandidateSet::CSK_Operator, |
13206 | OverloadCandidateSet::OperatorRewriteInfo(Op, AllowRewrittenCandidates)); |
13207 | if (DefaultedFn) |
13208 | CandidateSet.exclude(DefaultedFn); |
13209 | LookupOverloadedBinOp(CandidateSet, Op, Fns, Args, PerformADL); |
13210 | |
13211 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
13212 | |
13213 | |
13214 | OverloadCandidateSet::iterator Best; |
13215 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
13216 | case OR_Success: { |
13217 | |
13218 | FunctionDecl *FnDecl = Best->Function; |
13219 | |
13220 | bool IsReversed = (Best->RewriteKind & CRK_Reversed); |
13221 | if (IsReversed) |
13222 | std::swap(Args[0], Args[1]); |
13223 | |
13224 | if (FnDecl) { |
13225 | Expr *Base = nullptr; |
13226 | |
13227 | |
13228 | |
13229 | OverloadedOperatorKind ChosenOp = |
13230 | FnDecl->getDeclName().getCXXOverloadedOperator(); |
13231 | |
13232 | |
13233 | |
13234 | |
13235 | if (Best->RewriteKind && ChosenOp == OO_EqualEqual && |
13236 | !FnDecl->getReturnType()->isBooleanType()) { |
13237 | Diag(OpLoc, diag::err_ovl_rewrite_equalequal_not_bool) |
13238 | << FnDecl->getReturnType() << BinaryOperator::getOpcodeStr(Opc) |
13239 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
13240 | Diag(FnDecl->getLocation(), diag::note_declared_at); |
13241 | return ExprError(); |
13242 | } |
13243 | |
13244 | if (AllowRewrittenCandidates && !IsReversed && |
13245 | CandidateSet.getRewriteInfo().shouldAddReversed(ChosenOp)) { |
13246 | |
13247 | |
13248 | |
13249 | |
13250 | for (OverloadCandidate &Cand : CandidateSet) { |
13251 | if (Cand.Viable && Cand.Function == FnDecl && |
13252 | Cand.RewriteKind & CRK_Reversed) { |
13253 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
13254 | if (CompareImplicitConversionSequences( |
13255 | *this, OpLoc, Cand.Conversions[ArgIdx], |
13256 | Best->Conversions[ArgIdx]) == |
13257 | ImplicitConversionSequence::Better) { |
13258 | Diag(OpLoc, diag::ext_ovl_ambiguous_oper_binary_reversed) |
13259 | << BinaryOperator::getOpcodeStr(Opc) |
13260 | << Args[0]->getType() << Args[1]->getType() |
13261 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
13262 | Diag(FnDecl->getLocation(), |
13263 | diag::note_ovl_ambiguous_oper_binary_reversed_candidate); |
13264 | } |
13265 | } |
13266 | break; |
13267 | } |
13268 | } |
13269 | } |
13270 | |
13271 | |
13272 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
13273 | |
13274 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
13275 | |
13276 | ExprResult Arg1 = |
13277 | PerformCopyInitialization( |
13278 | InitializedEntity::InitializeParameter(Context, |
13279 | FnDecl->getParamDecl(0)), |
13280 | SourceLocation(), Args[1]); |
13281 | if (Arg1.isInvalid()) |
13282 | return ExprError(); |
13283 | |
13284 | ExprResult Arg0 = |
13285 | PerformObjectArgumentInitialization(Args[0], nullptr, |
13286 | Best->FoundDecl, Method); |
13287 | if (Arg0.isInvalid()) |
13288 | return ExprError(); |
13289 | Base = Args[0] = Arg0.getAs<Expr>(); |
13290 | Args[1] = RHS = Arg1.getAs<Expr>(); |
13291 | } else { |
13292 | |
13293 | ExprResult Arg0 = PerformCopyInitialization( |
13294 | InitializedEntity::InitializeParameter(Context, |
13295 | FnDecl->getParamDecl(0)), |
13296 | SourceLocation(), Args[0]); |
13297 | if (Arg0.isInvalid()) |
13298 | return ExprError(); |
13299 | |
13300 | ExprResult Arg1 = |
13301 | PerformCopyInitialization( |
13302 | InitializedEntity::InitializeParameter(Context, |
13303 | FnDecl->getParamDecl(1)), |
13304 | SourceLocation(), Args[1]); |
13305 | if (Arg1.isInvalid()) |
13306 | return ExprError(); |
13307 | Args[0] = LHS = Arg0.getAs<Expr>(); |
13308 | Args[1] = RHS = Arg1.getAs<Expr>(); |
13309 | } |
13310 | |
13311 | |
13312 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
13313 | Best->FoundDecl, Base, |
13314 | HadMultipleCandidates, OpLoc); |
13315 | if (FnExpr.isInvalid()) |
13316 | return ExprError(); |
13317 | |
13318 | |
13319 | QualType ResultTy = FnDecl->getReturnType(); |
13320 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
13321 | ResultTy = ResultTy.getNonLValueExprType(Context); |
13322 | |
13323 | CXXOperatorCallExpr *TheCall = CXXOperatorCallExpr::Create( |
13324 | Context, ChosenOp, FnExpr.get(), Args, ResultTy, VK, OpLoc, |
13325 | FPFeatures, Best->IsADLCandidate); |
13326 | |
13327 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, |
13328 | FnDecl)) |
13329 | return ExprError(); |
13330 | |
13331 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
13332 | const Expr *ImplicitThis = nullptr; |
13333 | |
13334 | if (isa<CXXMethodDecl>(FnDecl)) { |
13335 | ImplicitThis = ArgsArray[0]; |
13336 | ArgsArray = ArgsArray.slice(1); |
13337 | } |
13338 | |
13339 | |
13340 | if (Op == OO_Equal) |
13341 | DiagnoseSelfMove(Args[0], Args[1], OpLoc); |
13342 | |
13343 | checkCall(FnDecl, nullptr, ImplicitThis, ArgsArray, |
13344 | isa<CXXMethodDecl>(FnDecl), OpLoc, TheCall->getSourceRange(), |
13345 | VariadicDoesNotApply); |
13346 | |
13347 | ExprResult R = MaybeBindToTemporary(TheCall); |
13348 | if (R.isInvalid()) |
13349 | return ExprError(); |
13350 | |
13351 | |
13352 | |
13353 | if ((Best->RewriteKind & CRK_DifferentOperator) || |
13354 | (Op == OO_Spaceship && IsReversed)) { |
13355 | if (Op == OO_ExclaimEqual) { |
13356 | assert(ChosenOp == OO_EqualEqual && "unexpected operator name"); |
13357 | R = CreateBuiltinUnaryOp(OpLoc, UO_LNot, R.get()); |
13358 | } else { |
13359 | assert(ChosenOp == OO_Spaceship && "unexpected operator name"); |
13360 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
13361 | Expr *ZeroLiteral = |
13362 | IntegerLiteral::Create(Context, Zero, Context.IntTy, OpLoc); |
13363 | |
13364 | Sema::CodeSynthesisContext Ctx; |
13365 | Ctx.Kind = Sema::CodeSynthesisContext::RewritingOperatorAsSpaceship; |
13366 | Ctx.Entity = FnDecl; |
13367 | pushCodeSynthesisContext(Ctx); |
13368 | |
13369 | R = CreateOverloadedBinOp( |
13370 | OpLoc, Opc, Fns, IsReversed ? ZeroLiteral : R.get(), |
13371 | IsReversed ? R.get() : ZeroLiteral, PerformADL, |
13372 | false); |
13373 | |
13374 | popCodeSynthesisContext(); |
13375 | } |
13376 | if (R.isInvalid()) |
13377 | return ExprError(); |
13378 | } else { |
13379 | assert(ChosenOp == Op && "unexpected operator name"); |
13380 | } |
13381 | |
13382 | |
13383 | if (Best->RewriteKind != CRK_None) |
13384 | R = new (Context) CXXRewrittenBinaryOperator(R.get(), IsReversed); |
13385 | |
13386 | return R; |
13387 | } else { |
13388 | |
13389 | |
13390 | |
13391 | ExprResult ArgsRes0 = PerformImplicitConversion( |
13392 | Args[0], Best->BuiltinParamTypes[0], Best->Conversions[0], |
13393 | AA_Passing, CCK_ForBuiltinOverloadedOp); |
13394 | if (ArgsRes0.isInvalid()) |
13395 | return ExprError(); |
13396 | Args[0] = ArgsRes0.get(); |
13397 | |
13398 | ExprResult ArgsRes1 = PerformImplicitConversion( |
13399 | Args[1], Best->BuiltinParamTypes[1], Best->Conversions[1], |
13400 | AA_Passing, CCK_ForBuiltinOverloadedOp); |
13401 | if (ArgsRes1.isInvalid()) |
13402 | return ExprError(); |
13403 | Args[1] = ArgsRes1.get(); |
13404 | break; |
13405 | } |
13406 | } |
13407 | |
13408 | case OR_No_Viable_Function: { |
13409 | |
13410 | |
13411 | |
13412 | |
13413 | if (Opc == BO_Comma) |
13414 | break; |
13415 | |
13416 | |
13417 | |
13418 | if (DefaultedFn && Opc == BO_Cmp) { |
13419 | ExprResult E = BuildSynthesizedThreeWayComparison(OpLoc, Fns, Args[0], |
13420 | Args[1], DefaultedFn); |
13421 | if (E.isInvalid() || E.isUsable()) |
13422 | return E; |
13423 | } |
13424 | |
13425 | |
13426 | |
13427 | |
13428 | ExprResult Result = ExprError(); |
13429 | StringRef OpcStr = BinaryOperator::getOpcodeStr(Opc); |
13430 | auto Cands = CandidateSet.CompleteCandidates(*this, OCD_AllCandidates, |
13431 | Args, OpLoc); |
13432 | if (Args[0]->getType()->isRecordType() && |
13433 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
13434 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
13435 | << BinaryOperator::getOpcodeStr(Opc) |
13436 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
13437 | if (Args[0]->getType()->isIncompleteType()) { |
13438 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
13439 | << Args[0]->getType() |
13440 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
13441 | } |
13442 | } else { |
13443 | |
13444 | |
13445 | |
13446 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
13447 | |
13448 | return ExprError(); |
13449 | |
13450 | |
13451 | |
13452 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
13453 | } |
13454 | assert(Result.isInvalid() && |
13455 | "C++ binary operator overloading is missing candidates!"); |
13456 | CandidateSet.NoteCandidates(*this, Args, Cands, OpcStr, OpLoc); |
13457 | return Result; |
13458 | } |
13459 | |
13460 | case OR_Ambiguous: |
13461 | CandidateSet.NoteCandidates( |
13462 | PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_ambiguous_oper_binary) |
13463 | << BinaryOperator::getOpcodeStr(Opc) |
13464 | << Args[0]->getType() |
13465 | << Args[1]->getType() |
13466 | << Args[0]->getSourceRange() |
13467 | << Args[1]->getSourceRange()), |
13468 | *this, OCD_AmbiguousCandidates, Args, BinaryOperator::getOpcodeStr(Opc), |
13469 | OpLoc); |
13470 | return ExprError(); |
13471 | |
13472 | case OR_Deleted: |
13473 | if (isImplicitlyDeleted(Best->Function)) { |
13474 | FunctionDecl *DeletedFD = Best->Function; |
13475 | DefaultedFunctionKind DFK = getDefaultedFunctionKind(DeletedFD); |
13476 | if (DFK.isSpecialMember()) { |
13477 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
13478 | << Args[0]->getType() << DFK.asSpecialMember(); |
13479 | } else { |
13480 | assert(DFK.isComparison()); |
13481 | Diag(OpLoc, diag::err_ovl_deleted_comparison) |
13482 | << Args[0]->getType() << DeletedFD; |
13483 | } |
13484 | |
13485 | |
13486 | |
13487 | NoteDeletedFunction(DeletedFD); |
13488 | return ExprError(); |
13489 | } |
13490 | CandidateSet.NoteCandidates( |
13491 | PartialDiagnosticAt( |
13492 | OpLoc, PDiag(diag::err_ovl_deleted_oper) |
13493 | << getOperatorSpelling(Best->Function->getDeclName() |
13494 | .getCXXOverloadedOperator()) |
13495 | << Args[0]->getSourceRange() |
13496 | << Args[1]->getSourceRange()), |
13497 | *this, OCD_AllCandidates, Args, BinaryOperator::getOpcodeStr(Opc), |
13498 | OpLoc); |
13499 | return ExprError(); |
13500 | } |
13501 | |
13502 | |
13503 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
13504 | } |
13505 | |
13506 | ExprResult Sema::BuildSynthesizedThreeWayComparison( |
13507 | SourceLocation OpLoc, const UnresolvedSetImpl &Fns, Expr *LHS, Expr *RHS, |
13508 | FunctionDecl *DefaultedFn) { |
13509 | const ComparisonCategoryInfo *Info = |
13510 | Context.CompCategories.lookupInfoForType(DefaultedFn->getReturnType()); |
13511 | |
13512 | |
13513 | if (!Info) |
13514 | return ExprResult((Expr*)nullptr); |
13515 | |
13516 | |
13517 | |
13518 | assert(LHS->isGLValue() && RHS->isGLValue() && |
13519 | "cannot use prvalue expressions more than once"); |
13520 | Expr *OrigLHS = LHS; |
13521 | Expr *OrigRHS = RHS; |
13522 | |
13523 | |
13524 | |
13525 | LHS = new (Context) |
13526 | OpaqueValueExpr(LHS->getExprLoc(), LHS->getType(), LHS->getValueKind(), |
13527 | LHS->getObjectKind(), LHS); |
13528 | RHS = new (Context) |
13529 | OpaqueValueExpr(RHS->getExprLoc(), RHS->getType(), RHS->getValueKind(), |
13530 | RHS->getObjectKind(), RHS); |
13531 | |
13532 | ExprResult Eq = CreateOverloadedBinOp(OpLoc, BO_EQ, Fns, LHS, RHS, true, true, |
13533 | DefaultedFn); |
13534 | if (Eq.isInvalid()) |
13535 | return ExprError(); |
13536 | |
13537 | ExprResult Less = CreateOverloadedBinOp(OpLoc, BO_LT, Fns, LHS, RHS, true, |
13538 | true, DefaultedFn); |
13539 | if (Less.isInvalid()) |
13540 | return ExprError(); |
13541 | |
13542 | ExprResult Greater; |
13543 | if (Info->isPartial()) { |
13544 | Greater = CreateOverloadedBinOp(OpLoc, BO_LT, Fns, RHS, LHS, true, true, |
13545 | DefaultedFn); |
13546 | if (Greater.isInvalid()) |
13547 | return ExprError(); |
13548 | } |
13549 | |
13550 | |
13551 | struct Comparison { |
13552 | ExprResult Cmp; |
13553 | ComparisonCategoryResult Result; |
13554 | } Comparisons[4] = |
13555 | { {Eq, Info->isStrong() ? ComparisonCategoryResult::Equal |
13556 | : ComparisonCategoryResult::Equivalent}, |
13557 | {Less, ComparisonCategoryResult::Less}, |
13558 | {Greater, ComparisonCategoryResult::Greater}, |
13559 | {ExprResult(), ComparisonCategoryResult::Unordered}, |
13560 | }; |
13561 | |
13562 | int I = Info->isPartial() ? 3 : 2; |
13563 | |
13564 | |
13565 | ExprResult Result; |
13566 | for (; I >= 0; --I) { |
13567 | |
13568 | auto *VI = Info->lookupValueInfo(Comparisons[I].Result); |
13569 | |
13570 | if (!VI) |
13571 | return ExprResult((Expr*)nullptr); |
13572 | ExprResult ThisResult = |
13573 | BuildDeclarationNameExpr(CXXScopeSpec(), DeclarationNameInfo(), VI->VD); |
13574 | if (ThisResult.isInvalid()) |
13575 | return ExprError(); |
13576 | |
13577 | |
13578 | if (Result.get()) { |
13579 | Result = ActOnConditionalOp(OpLoc, OpLoc, Comparisons[I].Cmp.get(), |
13580 | ThisResult.get(), Result.get()); |
13581 | if (Result.isInvalid()) |
13582 | return ExprError(); |
13583 | } else { |
13584 | Result = ThisResult; |
13585 | } |
13586 | } |
13587 | |
13588 | |
13589 | |
13590 | Expr *SyntacticForm = new (Context) |
13591 | BinaryOperator(OrigLHS, OrigRHS, BO_Cmp, Result.get()->getType(), |
13592 | Result.get()->getValueKind(), |
13593 | Result.get()->getObjectKind(), OpLoc, FPFeatures); |
13594 | Expr *SemanticForm[] = {LHS, RHS, Result.get()}; |
13595 | return PseudoObjectExpr::Create(Context, SyntacticForm, SemanticForm, 2); |
13596 | } |
13597 | |
13598 | ExprResult |
13599 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
13600 | SourceLocation RLoc, |
13601 | Expr *Base, Expr *Idx) { |
13602 | Expr *Args[2] = { Base, Idx }; |
13603 | DeclarationName OpName = |
13604 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
13605 | |
13606 | |
13607 | |
13608 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
13609 | |
13610 | CXXRecordDecl *NamingClass = nullptr; |
13611 | |
13612 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
13613 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
13614 | UnresolvedLookupExpr *Fn |
13615 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
13616 | NestedNameSpecifierLoc(), OpNameInfo, |
13617 | true, false, |
13618 | UnresolvedSetIterator(), |
13619 | UnresolvedSetIterator()); |
13620 | |
13621 | |
13622 | return CXXOperatorCallExpr::Create(Context, OO_Subscript, Fn, Args, |
13623 | Context.DependentTy, VK_RValue, RLoc, |
13624 | FPOptions()); |
13625 | } |
13626 | |
13627 | |
13628 | if (checkPlaceholderForOverload(*this, Args[0])) |
13629 | return ExprError(); |
13630 | if (checkPlaceholderForOverload(*this, Args[1])) |
13631 | return ExprError(); |
13632 | |
13633 | |
13634 | OverloadCandidateSet CandidateSet(LLoc, OverloadCandidateSet::CSK_Operator); |
13635 | |
13636 | |
13637 | |
13638 | |
13639 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
13640 | |
13641 | |
13642 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
13643 | |
13644 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
13645 | |
13646 | |
13647 | OverloadCandidateSet::iterator Best; |
13648 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
13649 | case OR_Success: { |
13650 | |
13651 | FunctionDecl *FnDecl = Best->Function; |
13652 | |
13653 | if (FnDecl) { |
13654 | |
13655 | |
13656 | |
13657 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
13658 | |
13659 | |
13660 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
13661 | ExprResult Arg0 = |
13662 | PerformObjectArgumentInitialization(Args[0], nullptr, |
13663 | Best->FoundDecl, Method); |
13664 | if (Arg0.isInvalid()) |
13665 | return ExprError(); |
13666 | Args[0] = Arg0.get(); |
13667 | |
13668 | |
13669 | ExprResult InputInit |
13670 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
13671 | Context, |
13672 | FnDecl->getParamDecl(0)), |
13673 | SourceLocation(), |
13674 | Args[1]); |
13675 | if (InputInit.isInvalid()) |
13676 | return ExprError(); |
13677 | |
13678 | Args[1] = InputInit.getAs<Expr>(); |
13679 | |
13680 | |
13681 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
13682 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
13683 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
13684 | Best->FoundDecl, |
13685 | Base, |
13686 | HadMultipleCandidates, |
13687 | OpLocInfo.getLoc(), |
13688 | OpLocInfo.getInfo()); |
13689 | if (FnExpr.isInvalid()) |
13690 | return ExprError(); |
13691 | |
13692 | |
13693 | QualType ResultTy = FnDecl->getReturnType(); |
13694 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
13695 | ResultTy = ResultTy.getNonLValueExprType(Context); |
13696 | |
13697 | CXXOperatorCallExpr *TheCall = |
13698 | CXXOperatorCallExpr::Create(Context, OO_Subscript, FnExpr.get(), |
13699 | Args, ResultTy, VK, RLoc, FPOptions()); |
13700 | |
13701 | if (CheckCallReturnType(FnDecl->getReturnType(), LLoc, TheCall, FnDecl)) |
13702 | return ExprError(); |
13703 | |
13704 | if (CheckFunctionCall(Method, TheCall, |
13705 | Method->getType()->castAs<FunctionProtoType>())) |
13706 | return ExprError(); |
13707 | |
13708 | return MaybeBindToTemporary(TheCall); |
13709 | } else { |
13710 | |
13711 | |
13712 | |
13713 | ExprResult ArgsRes0 = PerformImplicitConversion( |
13714 | Args[0], Best->BuiltinParamTypes[0], Best->Conversions[0], |
13715 | AA_Passing, CCK_ForBuiltinOverloadedOp); |
13716 | if (ArgsRes0.isInvalid()) |
13717 | return ExprError(); |
13718 | Args[0] = ArgsRes0.get(); |
13719 | |
13720 | ExprResult ArgsRes1 = PerformImplicitConversion( |
13721 | Args[1], Best->BuiltinParamTypes[1], Best->Conversions[1], |
13722 | AA_Passing, CCK_ForBuiltinOverloadedOp); |
13723 | if (ArgsRes1.isInvalid()) |
13724 | return ExprError(); |
13725 | Args[1] = ArgsRes1.get(); |
13726 | |
13727 | break; |
13728 | } |
13729 | } |
13730 | |
13731 | case OR_No_Viable_Function: { |
13732 | PartialDiagnostic PD = CandidateSet.empty() |
13733 | ? (PDiag(diag::err_ovl_no_oper) |
13734 | << Args[0]->getType() << 0 |
13735 | << Args[0]->getSourceRange() << Args[1]->getSourceRange()) |
13736 | : (PDiag(diag::err_ovl_no_viable_subscript) |
13737 | << Args[0]->getType() << Args[0]->getSourceRange() |
13738 | << Args[1]->getSourceRange()); |
13739 | CandidateSet.NoteCandidates(PartialDiagnosticAt(LLoc, PD), *this, |
13740 | OCD_AllCandidates, Args, "[]", LLoc); |
13741 | return ExprError(); |
13742 | } |
13743 | |
13744 | case OR_Ambiguous: |
13745 | CandidateSet.NoteCandidates( |
13746 | PartialDiagnosticAt(LLoc, PDiag(diag::err_ovl_ambiguous_oper_binary) |
13747 | << "[]" << Args[0]->getType() |
13748 | << Args[1]->getType() |
13749 | << Args[0]->getSourceRange() |
13750 | << Args[1]->getSourceRange()), |
13751 | *this, OCD_AmbiguousCandidates, Args, "[]", LLoc); |
13752 | return ExprError(); |
13753 | |
13754 | case OR_Deleted: |
13755 | CandidateSet.NoteCandidates( |
13756 | PartialDiagnosticAt(LLoc, PDiag(diag::err_ovl_deleted_oper) |
13757 | << "[]" << Args[0]->getSourceRange() |
13758 | << Args[1]->getSourceRange()), |
13759 | *this, OCD_AllCandidates, Args, "[]", LLoc); |
13760 | return ExprError(); |
13761 | } |
13762 | |
13763 | |
13764 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
13765 | } |
13766 | |
13767 | |
13768 | |
13769 | |
13770 | |
13771 | |
13772 | |
13773 | |
13774 | ExprResult |
13775 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
13776 | SourceLocation LParenLoc, |
13777 | MultiExprArg Args, |
13778 | SourceLocation RParenLoc) { |
13779 | assert(MemExprE->getType() == Context.BoundMemberTy || |
13780 | MemExprE->getType() == Context.OverloadTy); |
13781 | |
13782 | |
13783 | |
13784 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
13785 | |
13786 | |
13787 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
13788 | assert(op->getType() == Context.BoundMemberTy); |
13789 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
13790 | |
13791 | QualType fnType = |
13792 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
13793 | |
13794 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
13795 | QualType resultType = proto->getCallResultType(Context); |
13796 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getReturnType()); |
13797 | |
13798 | |
13799 | |
13800 | Qualifiers funcQuals = proto->getMethodQuals(); |
13801 | |
13802 | QualType objectType = op->getLHS()->getType(); |
13803 | if (op->getOpcode() == BO_PtrMemI) |
13804 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
13805 | Qualifiers objectQuals = objectType.getQualifiers(); |
13806 | |
13807 | Qualifiers difference = objectQuals - funcQuals; |
13808 | difference.removeObjCGCAttr(); |
13809 | difference.removeAddressSpace(); |
13810 | if (difference) { |
13811 | std::string qualsString = difference.getAsString(); |
13812 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
13813 | << fnType.getUnqualifiedType() |
13814 | << qualsString |
13815 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
13816 | } |
13817 | |
13818 | CXXMemberCallExpr *call = |
13819 | CXXMemberCallExpr::Create(Context, MemExprE, Args, resultType, |
13820 | valueKind, RParenLoc, proto->getNumParams()); |
13821 | |
13822 | if (CheckCallReturnType(proto->getReturnType(), op->getRHS()->getBeginLoc(), |
13823 | call, nullptr)) |
13824 | return ExprError(); |
13825 | |
13826 | if (ConvertArgumentsForCall(call, op, nullptr, proto, Args, RParenLoc)) |
13827 | return ExprError(); |
13828 | |
13829 | if (CheckOtherCall(call, proto)) |
13830 | return ExprError(); |
13831 | |
13832 | return MaybeBindToTemporary(call); |
13833 | } |
13834 | |
13835 | if (isa<CXXPseudoDestructorExpr>(NakedMemExpr)) |
13836 | return CallExpr::Create(Context, MemExprE, Args, Context.VoidTy, VK_RValue, |
13837 | RParenLoc); |
13838 | |
13839 | UnbridgedCastsSet UnbridgedCasts; |
13840 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
13841 | return ExprError(); |
13842 | |
13843 | MemberExpr *MemExpr; |
13844 | CXXMethodDecl *Method = nullptr; |
13845 | DeclAccessPair FoundDecl = DeclAccessPair::make(nullptr, AS_public); |
13846 | NestedNameSpecifier *Qualifier = nullptr; |
13847 | if (isa<MemberExpr>(NakedMemExpr)) { |
13848 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
13849 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
13850 | FoundDecl = MemExpr->getFoundDecl(); |
13851 | Qualifier = MemExpr->getQualifier(); |
13852 | UnbridgedCasts.restore(); |
13853 | } else { |
13854 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
13855 | Qualifier = UnresExpr->getQualifier(); |
13856 | |
13857 | QualType ObjectType = UnresExpr->getBaseType(); |
13858 | Expr::Classification ObjectClassification |
13859 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
13860 | : UnresExpr->getBase()->Classify(Context); |
13861 | |
13862 | |
13863 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc(), |
13864 | OverloadCandidateSet::CSK_Normal); |
13865 | |
13866 | |
13867 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
13868 | if (UnresExpr->hasExplicitTemplateArgs()) { |
13869 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
13870 | TemplateArgs = &TemplateArgsBuffer; |
13871 | } |
13872 | |
13873 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
13874 | E = UnresExpr->decls_end(); I != E; ++I) { |
13875 | |
13876 | NamedDecl *Func = *I; |
13877 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
13878 | if (isa<UsingShadowDecl>(Func)) |
13879 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
13880 | |
13881 | |
13882 | |
13883 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
13884 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, |
13885 | CandidateSet, |
13886 | false); |
13887 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
13888 | |
13889 | |
13890 | if (TemplateArgs) |
13891 | continue; |
13892 | |
13893 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
13894 | ObjectClassification, Args, CandidateSet, |
13895 | false); |
13896 | } else { |
13897 | AddMethodTemplateCandidate( |
13898 | cast<FunctionTemplateDecl>(Func), I.getPair(), ActingDC, |
13899 | TemplateArgs, ObjectType, ObjectClassification, Args, CandidateSet, |
13900 | false); |
13901 | } |
13902 | } |
13903 | |
13904 | DeclarationName DeclName = UnresExpr->getMemberName(); |
13905 | |
13906 | UnbridgedCasts.restore(); |
13907 | |
13908 | OverloadCandidateSet::iterator Best; |
13909 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getBeginLoc(), |
13910 | Best)) { |
13911 | case OR_Success: |
13912 | Method = cast<CXXMethodDecl>(Best->Function); |
13913 | FoundDecl = Best->FoundDecl; |
13914 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
13915 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
13916 | return ExprError(); |
13917 | |
13918 | |
13919 | |
13920 | |
13921 | |
13922 | |
13923 | if (Method != FoundDecl.getDecl() && |
13924 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
13925 | return ExprError(); |
13926 | break; |
13927 | |
13928 | case OR_No_Viable_Function: |
13929 | CandidateSet.NoteCandidates( |
13930 | PartialDiagnosticAt( |
13931 | UnresExpr->getMemberLoc(), |
13932 | PDiag(diag::err_ovl_no_viable_member_function_in_call) |
13933 | << DeclName << MemExprE->getSourceRange()), |
13934 | *this, OCD_AllCandidates, Args); |
13935 | |
13936 | return ExprError(); |
13937 | |
13938 | case OR_Ambiguous: |
13939 | CandidateSet.NoteCandidates( |
13940 | PartialDiagnosticAt(UnresExpr->getMemberLoc(), |
13941 | PDiag(diag::err_ovl_ambiguous_member_call) |
13942 | << DeclName << MemExprE->getSourceRange()), |
13943 | *this, OCD_AmbiguousCandidates, Args); |
13944 | |
13945 | return ExprError(); |
13946 | |
13947 | case OR_Deleted: |
13948 | CandidateSet.NoteCandidates( |
13949 | PartialDiagnosticAt(UnresExpr->getMemberLoc(), |
13950 | PDiag(diag::err_ovl_deleted_member_call) |
13951 | << DeclName << MemExprE->getSourceRange()), |
13952 | *this, OCD_AllCandidates, Args); |
13953 | |
13954 | return ExprError(); |
13955 | } |
13956 | |
13957 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
13958 | |
13959 | |
13960 | |
13961 | if (Method->isStatic()) { |
13962 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
13963 | RParenLoc); |
13964 | } |
13965 | |
13966 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
13967 | } |
13968 | |
13969 | QualType ResultType = Method->getReturnType(); |
13970 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
13971 | ResultType = ResultType.getNonLValueExprType(Context); |
13972 | |
13973 | assert(Method && "Member call to something that isn't a method?"); |
13974 | const auto *Proto = Method->getType()->getAs<FunctionProtoType>(); |
13975 | CXXMemberCallExpr *TheCall = |
13976 | CXXMemberCallExpr::Create(Context, MemExprE, Args, ResultType, VK, |
13977 | RParenLoc, Proto->getNumParams()); |
13978 | |
13979 | |
13980 | if (CheckCallReturnType(Method->getReturnType(), MemExpr->getMemberLoc(), |
13981 | TheCall, Method)) |
13982 | return ExprError(); |
13983 | |
13984 | |
13985 | |
13986 | |
13987 | if (!Method->isStatic()) { |
13988 | ExprResult ObjectArg = |
13989 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
13990 | FoundDecl, Method); |
13991 | if (ObjectArg.isInvalid()) |
13992 | return ExprError(); |
13993 | MemExpr->setBase(ObjectArg.get()); |
13994 | } |
13995 | |
13996 | |
13997 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
13998 | RParenLoc)) |
13999 | return ExprError(); |
14000 | |
14001 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
14002 | |
14003 | if (CheckFunctionCall(Method, TheCall, Proto)) |
14004 | return ExprError(); |
14005 | |
14006 | |
14007 | |
14008 | |
14009 | if (auto *MemE = dyn_cast<MemberExpr>(NakedMemExpr)) { |
14010 | if (const EnableIfAttr *Attr = CheckEnableIf(Method, Args, true)) { |
14011 | Diag(MemE->getMemberLoc(), |
14012 | diag::err_ovl_no_viable_member_function_in_call) |
14013 | << Method << Method->getSourceRange(); |
14014 | Diag(Method->getLocation(), |
14015 | diag::note_ovl_candidate_disabled_by_function_cond_attr) |
14016 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); |
14017 | return ExprError(); |
14018 | } |
14019 | } |
14020 | |
14021 | if ((isa<CXXConstructorDecl>(CurContext) || |
14022 | isa<CXXDestructorDecl>(CurContext)) && |
14023 | TheCall->getMethodDecl()->isPure()) { |
14024 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
14025 | |
14026 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts()) && |
14027 | MemExpr->performsVirtualDispatch(getLangOpts())) { |
14028 | Diag(MemExpr->getBeginLoc(), |
14029 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
14030 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
14031 | << MD->getParent()->getDeclName(); |
14032 | |
14033 | Diag(MD->getBeginLoc(), diag::note_previous_decl) << MD->getDeclName(); |
14034 | if (getLangOpts().AppleKext) |
14035 | Diag(MemExpr->getBeginLoc(), diag::note_pure_qualified_call_kext) |
14036 | << MD->getParent()->getDeclName() << MD->getDeclName(); |
14037 | } |
14038 | } |
14039 | |
14040 | if (CXXDestructorDecl *DD = |
14041 | dyn_cast<CXXDestructorDecl>(TheCall->getMethodDecl())) { |
14042 | |
14043 | bool CallCanBeVirtual = !MemExpr->hasQualifier() || getLangOpts().AppleKext; |
14044 | CheckVirtualDtorCall(DD, MemExpr->getBeginLoc(), false, |
14045 | CallCanBeVirtual, true, |
14046 | MemExpr->getMemberLoc()); |
14047 | } |
14048 | |
14049 | return MaybeBindToTemporary(TheCall); |
14050 | } |
14051 | |
14052 | |
14053 | |
14054 | |
14055 | |
14056 | ExprResult |
14057 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
14058 | SourceLocation LParenLoc, |
14059 | MultiExprArg Args, |
14060 | SourceLocation RParenLoc) { |
14061 | if (checkPlaceholderForOverload(*this, Obj)) |
14062 | return ExprError(); |
14063 | ExprResult Object = Obj; |
14064 | |
14065 | UnbridgedCastsSet UnbridgedCasts; |
14066 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
14067 | return ExprError(); |
14068 | |
14069 | assert(Object.get()->getType()->isRecordType() && |
14070 | "Requires object type argument"); |
14071 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
14072 | |
14073 | |
14074 | |
14075 | |
14076 | |
14077 | |
14078 | |
14079 | |
14080 | OverloadCandidateSet CandidateSet(LParenLoc, |
14081 | OverloadCandidateSet::CSK_Operator); |
14082 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
14083 | |
14084 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
14085 | diag::err_incomplete_object_call, Object.get())) |
14086 | return true; |
14087 | |
14088 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
14089 | LookupQualifiedName(R, Record->getDecl()); |
14090 | R.suppressDiagnostics(); |
14091 | |
14092 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
14093 | Oper != OperEnd; ++Oper) { |
14094 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
14095 | Object.get()->Classify(Context), Args, CandidateSet, |
14096 | false); |
14097 | } |
14098 | |
14099 | |
14100 | |
14101 | |
14102 | |
14103 | |
14104 | |
14105 | |
14106 | |
14107 | |
14108 | |
14109 | |
14110 | |
14111 | |
14112 | |
14113 | |
14114 | |
14115 | |
14116 | const auto &Conversions = |
14117 | cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
14118 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
14119 | NamedDecl *D = *I; |
14120 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
14121 | if (isa<UsingShadowDecl>(D)) |
14122 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
14123 | |
14124 | |
14125 | |
14126 | if (isa<FunctionTemplateDecl>(D)) |
14127 | continue; |
14128 | |
14129 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
14130 | if (!Conv->isExplicit()) { |
14131 | |
14132 | |
14133 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
14134 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
14135 | ConvType = ConvPtrType->getPointeeType(); |
14136 | |
14137 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
14138 | { |
14139 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
14140 | Object.get(), Args, CandidateSet); |
14141 | } |
14142 | } |
14143 | } |
14144 | |
14145 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
14146 | |
14147 | |
14148 | OverloadCandidateSet::iterator Best; |
14149 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getBeginLoc(), |
14150 | Best)) { |
14151 | case OR_Success: |
14152 | |
14153 | |
14154 | break; |
14155 | |
14156 | case OR_No_Viable_Function: { |
14157 | PartialDiagnostic PD = |
14158 | CandidateSet.empty() |
14159 | ? (PDiag(diag::err_ovl_no_oper) |
14160 | << Object.get()->getType() << 1 |
14161 | << Object.get()->getSourceRange()) |
14162 | : (PDiag(diag::err_ovl_no_viable_object_call) |
14163 | << Object.get()->getType() << Object.get()->getSourceRange()); |
14164 | CandidateSet.NoteCandidates( |
14165 | PartialDiagnosticAt(Object.get()->getBeginLoc(), PD), *this, |
14166 | OCD_AllCandidates, Args); |
14167 | break; |
14168 | } |
14169 | case OR_Ambiguous: |
14170 | CandidateSet.NoteCandidates( |
14171 | PartialDiagnosticAt(Object.get()->getBeginLoc(), |
14172 | PDiag(diag::err_ovl_ambiguous_object_call) |
14173 | << Object.get()->getType() |
14174 | << Object.get()->getSourceRange()), |
14175 | *this, OCD_AmbiguousCandidates, Args); |
14176 | break; |
14177 | |
14178 | case OR_Deleted: |
14179 | CandidateSet.NoteCandidates( |
14180 | PartialDiagnosticAt(Object.get()->getBeginLoc(), |
14181 | PDiag(diag::err_ovl_deleted_object_call) |
14182 | << Object.get()->getType() |
14183 | << Object.get()->getSourceRange()), |
14184 | *this, OCD_AllCandidates, Args); |
14185 | break; |
14186 | } |
14187 | |
14188 | if (Best == CandidateSet.end()) |
14189 | return true; |
14190 | |
14191 | UnbridgedCasts.restore(); |
14192 | |
14193 | if (Best->Function == nullptr) { |
14194 | |
14195 | |
14196 | CXXConversionDecl *Conv |
14197 | = cast<CXXConversionDecl>( |
14198 | Best->Conversions[0].UserDefined.ConversionFunction); |
14199 | |
14200 | CheckMemberOperatorAccess(LParenLoc, Object.get(), nullptr, |
14201 | Best->FoundDecl); |
14202 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
14203 | return ExprError(); |
14204 | assert(Conv == Best->FoundDecl.getDecl() && |
14205 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
14206 | |
14207 | |
14208 | |
14209 | |
14210 | |
14211 | |
14212 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
14213 | Conv, HadMultipleCandidates); |
14214 | if (Call.isInvalid()) |
14215 | return ExprError(); |
14216 | |
14217 | Call = ImplicitCastExpr::Create(Context, Call.get()->getType(), |
14218 | CK_UserDefinedConversion, Call.get(), |
14219 | nullptr, VK_RValue); |
14220 | |
14221 | return BuildCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
14222 | } |
14223 | |
14224 | CheckMemberOperatorAccess(LParenLoc, Object.get(), nullptr, Best->FoundDecl); |
14225 | |
14226 | |
14227 | |
14228 | |
14229 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
14230 | |
14231 | |
14232 | if (Method->isInvalidDecl()) |
14233 | return ExprError(); |
14234 | |
14235 | const FunctionProtoType *Proto = |
14236 | Method->getType()->getAs<FunctionProtoType>(); |
14237 | |
14238 | unsigned NumParams = Proto->getNumParams(); |
14239 | |
14240 | DeclarationNameInfo OpLocInfo( |
14241 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
14242 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
14243 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
14244 | Obj, HadMultipleCandidates, |
14245 | OpLocInfo.getLoc(), |
14246 | OpLocInfo.getInfo()); |
14247 | if (NewFn.isInvalid()) |
14248 | return true; |
14249 | |
14250 | |
14251 | |
14252 | |
14253 | unsigned NumArgsSlots = 1 + std::max<unsigned>(Args.size(), NumParams); |
14254 | |
14255 | |
14256 | |
14257 | SmallVector<Expr *, 8> MethodArgs(NumArgsSlots); |
14258 | |
14259 | bool IsError = false; |
14260 | |
14261 | |
14262 | ExprResult ObjRes = |
14263 | PerformObjectArgumentInitialization(Object.get(), nullptr, |
14264 | Best->FoundDecl, Method); |
14265 | if (ObjRes.isInvalid()) |
14266 | IsError = true; |
14267 | else |
14268 | Object = ObjRes; |
14269 | MethodArgs[0] = Object.get(); |
14270 | |
14271 | |
14272 | for (unsigned i = 0; i != NumParams; i++) { |
14273 | Expr *Arg; |
14274 | if (i < Args.size()) { |
14275 | Arg = Args[i]; |
14276 | |
14277 | |
14278 | |
14279 | ExprResult InputInit |
14280 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
14281 | Context, |
14282 | Method->getParamDecl(i)), |
14283 | SourceLocation(), Arg); |
14284 | |
14285 | IsError |= InputInit.isInvalid(); |
14286 | Arg = InputInit.getAs<Expr>(); |
14287 | } else { |
14288 | ExprResult DefArg |
14289 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
14290 | if (DefArg.isInvalid()) { |
14291 | IsError = true; |
14292 | break; |
14293 | } |
14294 | |
14295 | Arg = DefArg.getAs<Expr>(); |
14296 | } |
14297 | |
14298 | MethodArgs[i + 1] = Arg; |
14299 | } |
14300 | |
14301 | |
14302 | if (Proto->isVariadic()) { |
14303 | |
14304 | for (unsigned i = NumParams, e = Args.size(); i < e; i++) { |
14305 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
14306 | nullptr); |
14307 | IsError |= Arg.isInvalid(); |
14308 | MethodArgs[i + 1] = Arg.get(); |
14309 | } |
14310 | } |
14311 | |
14312 | if (IsError) |
14313 | return true; |
14314 | |
14315 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
14316 | |
14317 | |
14318 | QualType ResultTy = Method->getReturnType(); |
14319 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
14320 | ResultTy = ResultTy.getNonLValueExprType(Context); |
14321 | |
14322 | CXXOperatorCallExpr *TheCall = |
14323 | CXXOperatorCallExpr::Create(Context, OO_Call, NewFn.get(), MethodArgs, |
14324 | ResultTy, VK, RParenLoc, FPOptions()); |
14325 | |
14326 | if (CheckCallReturnType(Method->getReturnType(), LParenLoc, TheCall, Method)) |
14327 | return true; |
14328 | |
14329 | if (CheckFunctionCall(Method, TheCall, Proto)) |
14330 | return true; |
14331 | |
14332 | return MaybeBindToTemporary(TheCall); |
14333 | } |
14334 | |
14335 | |
14336 | |
14337 | |
14338 | ExprResult |
14339 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
14340 | bool *NoArrowOperatorFound) { |
14341 | assert(Base->getType()->isRecordType() && |
14342 | "left-hand side must have class type"); |
14343 | |
14344 | if (checkPlaceholderForOverload(*this, Base)) |
14345 | return ExprError(); |
14346 | |
14347 | SourceLocation Loc = Base->getExprLoc(); |
14348 | |
14349 | |
14350 | |
14351 | |
14352 | |
14353 | |
14354 | |
14355 | DeclarationName OpName = |
14356 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
14357 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Operator); |
14358 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
14359 | |
14360 | if (RequireCompleteType(Loc, Base->getType(), |
14361 | diag::err_typecheck_incomplete_tag, Base)) |
14362 | return ExprError(); |
14363 | |
14364 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
14365 | LookupQualifiedName(R, BaseRecord->getDecl()); |
14366 | R.suppressDiagnostics(); |
14367 | |
14368 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
14369 | Oper != OperEnd; ++Oper) { |
14370 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
14371 | None, CandidateSet, false); |
14372 | } |
14373 | |
14374 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
14375 | |
14376 | |
14377 | OverloadCandidateSet::iterator Best; |
14378 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
14379 | case OR_Success: |
14380 | |
14381 | break; |
14382 | |
14383 | case OR_No_Viable_Function: { |
14384 | auto Cands = CandidateSet.CompleteCandidates(*this, OCD_AllCandidates, Base); |
14385 | if (CandidateSet.empty()) { |
14386 | QualType BaseType = Base->getType(); |
14387 | if (NoArrowOperatorFound) { |
14388 | |
14389 | |
14390 | *NoArrowOperatorFound = true; |
14391 | return ExprError(); |
14392 | } |
14393 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
14394 | << BaseType << Base->getSourceRange(); |
14395 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
14396 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
14397 | << FixItHint::CreateReplacement(OpLoc, "."); |
14398 | } |
14399 | } else |
14400 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
14401 | << "operator->" << Base->getSourceRange(); |
14402 | CandidateSet.NoteCandidates(*this, Base, Cands); |
14403 | return ExprError(); |
14404 | } |
14405 | case OR_Ambiguous: |
14406 | CandidateSet.NoteCandidates( |
14407 | PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_ambiguous_oper_unary) |
14408 | << "->" << Base->getType() |
14409 | << Base->getSourceRange()), |
14410 | *this, OCD_AmbiguousCandidates, Base); |
14411 | return ExprError(); |
14412 | |
14413 | case OR_Deleted: |
14414 | CandidateSet.NoteCandidates( |
14415 | PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_deleted_oper) |
14416 | << "->" << Base->getSourceRange()), |
14417 | *this, OCD_AllCandidates, Base); |
14418 | return ExprError(); |
14419 | } |
14420 | |
14421 | CheckMemberOperatorAccess(OpLoc, Base, nullptr, Best->FoundDecl); |
14422 | |
14423 | |
14424 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
14425 | ExprResult BaseResult = |
14426 | PerformObjectArgumentInitialization(Base, nullptr, |
14427 | Best->FoundDecl, Method); |
14428 | if (BaseResult.isInvalid()) |
14429 | return ExprError(); |
14430 | Base = BaseResult.get(); |
14431 | |
14432 | |
14433 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
14434 | Base, HadMultipleCandidates, OpLoc); |
14435 | if (FnExpr.isInvalid()) |
14436 | return ExprError(); |
14437 | |
14438 | QualType ResultTy = Method->getReturnType(); |
14439 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
14440 | ResultTy = ResultTy.getNonLValueExprType(Context); |
14441 | CXXOperatorCallExpr *TheCall = CXXOperatorCallExpr::Create( |
14442 | Context, OO_Arrow, FnExpr.get(), Base, ResultTy, VK, OpLoc, FPOptions()); |
14443 | |
14444 | if (CheckCallReturnType(Method->getReturnType(), OpLoc, TheCall, Method)) |
14445 | return ExprError(); |
14446 | |
14447 | if (CheckFunctionCall(Method, TheCall, |
14448 | Method->getType()->castAs<FunctionProtoType>())) |
14449 | return ExprError(); |
14450 | |
14451 | return MaybeBindToTemporary(TheCall); |
14452 | } |
14453 | |
14454 | |
14455 | |
14456 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
14457 | DeclarationNameInfo &SuffixInfo, |
14458 | ArrayRef<Expr*> Args, |
14459 | SourceLocation LitEndLoc, |
14460 | TemplateArgumentListInfo *TemplateArgs) { |
14461 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
14462 | |
14463 | OverloadCandidateSet CandidateSet(UDSuffixLoc, |
14464 | OverloadCandidateSet::CSK_Normal); |
14465 | AddNonMemberOperatorCandidates(R.asUnresolvedSet(), Args, CandidateSet, |
14466 | TemplateArgs); |
14467 | |
14468 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
14469 | |
14470 | |
14471 | |
14472 | OverloadCandidateSet::iterator Best; |
14473 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
14474 | case OR_Success: |
14475 | case OR_Deleted: |
14476 | break; |
14477 | |
14478 | case OR_No_Viable_Function: |
14479 | CandidateSet.NoteCandidates( |
14480 | PartialDiagnosticAt(UDSuffixLoc, |
14481 | PDiag(diag::err_ovl_no_viable_function_in_call) |
14482 | << R.getLookupName()), |
14483 | *this, OCD_AllCandidates, Args); |
14484 | return ExprError(); |
14485 | |
14486 | case OR_Ambiguous: |
14487 | CandidateSet.NoteCandidates( |
14488 | PartialDiagnosticAt(R.getNameLoc(), PDiag(diag::err_ovl_ambiguous_call) |
14489 | << R.getLookupName()), |
14490 | *this, OCD_AmbiguousCandidates, Args); |
14491 | return ExprError(); |
14492 | } |
14493 | |
14494 | FunctionDecl *FD = Best->Function; |
14495 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
14496 | nullptr, HadMultipleCandidates, |
14497 | SuffixInfo.getLoc(), |
14498 | SuffixInfo.getInfo()); |
14499 | if (Fn.isInvalid()) |
14500 | return true; |
14501 | |
14502 | |
14503 | |
14504 | Expr *ConvArgs[2]; |
14505 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
14506 | ExprResult InputInit = PerformCopyInitialization( |
14507 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
14508 | SourceLocation(), Args[ArgIdx]); |
14509 | if (InputInit.isInvalid()) |
14510 | return true; |
14511 | ConvArgs[ArgIdx] = InputInit.get(); |
14512 | } |
14513 | |
14514 | QualType ResultTy = FD->getReturnType(); |
14515 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
14516 | ResultTy = ResultTy.getNonLValueExprType(Context); |
14517 | |
14518 | UserDefinedLiteral *UDL = UserDefinedLiteral::Create( |
14519 | Context, Fn.get(), llvm::makeArrayRef(ConvArgs, Args.size()), ResultTy, |
14520 | VK, LitEndLoc, UDSuffixLoc); |
14521 | |
14522 | if (CheckCallReturnType(FD->getReturnType(), UDSuffixLoc, UDL, FD)) |
14523 | return ExprError(); |
14524 | |
14525 | if (CheckFunctionCall(FD, UDL, nullptr)) |
14526 | return ExprError(); |
14527 | |
14528 | return MaybeBindToTemporary(UDL); |
14529 | } |
14530 | |
14531 | |
14532 | |
14533 | |
14534 | |
14535 | |
14536 | |
14537 | |
14538 | Sema::ForRangeStatus |
14539 | Sema::BuildForRangeBeginEndCall(SourceLocation Loc, |
14540 | SourceLocation RangeLoc, |
14541 | const DeclarationNameInfo &NameInfo, |
14542 | LookupResult &MemberLookup, |
14543 | OverloadCandidateSet *CandidateSet, |
14544 | Expr *Range, ExprResult *CallExpr) { |
14545 | Scope *S = nullptr; |
14546 | |
14547 | CandidateSet->clear(OverloadCandidateSet::CSK_Normal); |
14548 | if (!MemberLookup.empty()) { |
14549 | ExprResult MemberRef = |
14550 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
14551 | false, CXXScopeSpec(), |
14552 | SourceLocation(), |
14553 | nullptr, |
14554 | MemberLookup, |
14555 | nullptr, S); |
14556 | if (MemberRef.isInvalid()) { |
14557 | *CallExpr = ExprError(); |
14558 | return FRS_DiagnosticIssued; |
14559 | } |
14560 | *CallExpr = BuildCallExpr(S, MemberRef.get(), Loc, None, Loc, nullptr); |
14561 | if (CallExpr->isInvalid()) { |
14562 | *CallExpr = ExprError(); |
14563 | return FRS_DiagnosticIssued; |
14564 | } |
14565 | } else { |
14566 | UnresolvedSet<0> FoundNames; |
14567 | UnresolvedLookupExpr *Fn = |
14568 | UnresolvedLookupExpr::Create(Context, nullptr, |
14569 | NestedNameSpecifierLoc(), NameInfo, |
14570 | true, false, |
14571 | FoundNames.begin(), FoundNames.end()); |
14572 | |
14573 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
14574 | CandidateSet, CallExpr); |
14575 | if (CandidateSet->empty() || CandidateSetError) { |
14576 | *CallExpr = ExprError(); |
14577 | return FRS_NoViableFunction; |
14578 | } |
14579 | OverloadCandidateSet::iterator Best; |
14580 | OverloadingResult OverloadResult = |
14581 | CandidateSet->BestViableFunction(*this, Fn->getBeginLoc(), Best); |
14582 | |
14583 | if (OverloadResult == OR_No_Viable_Function) { |
14584 | *CallExpr = ExprError(); |
14585 | return FRS_NoViableFunction; |
14586 | } |
14587 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
14588 | Loc, nullptr, CandidateSet, &Best, |
14589 | OverloadResult, |
14590 | false); |
14591 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
14592 | *CallExpr = ExprError(); |
14593 | return FRS_DiagnosticIssued; |
14594 | } |
14595 | } |
14596 | return FRS_Success; |
14597 | } |
14598 | |
14599 | |
14600 | |
14601 | |
14602 | |
14603 | |
14604 | |
14605 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
14606 | FunctionDecl *Fn) { |
14607 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
14608 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
14609 | Found, Fn); |
14610 | if (SubExpr == PE->getSubExpr()) |
14611 | return PE; |
14612 | |
14613 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
14614 | } |
14615 | |
14616 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
14617 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
14618 | Found, Fn); |
14619 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
14620 | SubExpr->getType()) && |
14621 | "Implicit cast type cannot be determined from overload"); |
14622 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
14623 | if (SubExpr == ICE->getSubExpr()) |
14624 | return ICE; |
14625 | |
14626 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
14627 | ICE->getCastKind(), |
14628 | SubExpr, nullptr, |
14629 | ICE->getValueKind()); |
14630 | } |
14631 | |
14632 | if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
14633 | if (!GSE->isResultDependent()) { |
14634 | Expr *SubExpr = |
14635 | FixOverloadedFunctionReference(GSE->getResultExpr(), Found, Fn); |
14636 | if (SubExpr == GSE->getResultExpr()) |
14637 | return GSE; |
14638 | |
14639 | |
14640 | |
14641 | ArrayRef<Expr *> A = GSE->getAssocExprs(); |
14642 | SmallVector<Expr *, 4> AssocExprs(A.begin(), A.end()); |
14643 | unsigned ResultIdx = GSE->getResultIndex(); |
14644 | AssocExprs[ResultIdx] = SubExpr; |
14645 | |
14646 | return GenericSelectionExpr::Create( |
14647 | Context, GSE->getGenericLoc(), GSE->getControllingExpr(), |
14648 | GSE->getAssocTypeSourceInfos(), AssocExprs, GSE->getDefaultLoc(), |
14649 | GSE->getRParenLoc(), GSE->containsUnexpandedParameterPack(), |
14650 | ResultIdx); |
14651 | } |
14652 | |
14653 | |
14654 | return GSE; |
14655 | } |
14656 | |
14657 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
14658 | assert(UnOp->getOpcode() == UO_AddrOf && |
14659 | "Can only take the address of an overloaded function"); |
14660 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
14661 | if (Method->isStatic()) { |
14662 | |
14663 | |
14664 | } else { |
14665 | |
14666 | |
14667 | |
14668 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
14669 | Found, Fn); |
14670 | if (SubExpr == UnOp->getSubExpr()) |
14671 | return UnOp; |
14672 | |
14673 | assert(isa<DeclRefExpr>(SubExpr) |
14674 | && "fixed to something other than a decl ref"); |
14675 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
14676 | && "fixed to a member ref with no nested name qualifier"); |
14677 | |
14678 | |
14679 | |
14680 | |
14681 | QualType ClassType |
14682 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
14683 | QualType MemPtrType |
14684 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
14685 | |
14686 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) |
14687 | (void)isCompleteType(UnOp->getOperatorLoc(), MemPtrType); |
14688 | |
14689 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
14690 | VK_RValue, OK_Ordinary, |
14691 | UnOp->getOperatorLoc(), false); |
14692 | } |
14693 | } |
14694 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
14695 | Found, Fn); |
14696 | if (SubExpr == UnOp->getSubExpr()) |
14697 | return UnOp; |
14698 | |
14699 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
14700 | Context.getPointerType(SubExpr->getType()), |
14701 | VK_RValue, OK_Ordinary, |
14702 | UnOp->getOperatorLoc(), false); |
14703 | } |
14704 | |
14705 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
14706 | |
14707 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
14708 | if (ULE->hasExplicitTemplateArgs()) { |
14709 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
14710 | TemplateArgs = &TemplateArgsBuffer; |
14711 | } |
14712 | |
14713 | DeclRefExpr *DRE = |
14714 | BuildDeclRefExpr(Fn, Fn->getType(), VK_LValue, ULE->getNameInfo(), |
14715 | ULE->getQualifierLoc(), Found.getDecl(), |
14716 | ULE->getTemplateKeywordLoc(), TemplateArgs); |
14717 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
14718 | return DRE; |
14719 | } |
14720 | |
14721 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
14722 | |
14723 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
14724 | if (MemExpr->hasExplicitTemplateArgs()) { |
14725 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
14726 | TemplateArgs = &TemplateArgsBuffer; |
14727 | } |
14728 | |
14729 | Expr *Base; |
14730 | |
14731 | |
14732 | |
14733 | if (MemExpr->isImplicitAccess()) { |
14734 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
14735 | DeclRefExpr *DRE = BuildDeclRefExpr( |
14736 | Fn, Fn->getType(), VK_LValue, MemExpr->getNameInfo(), |
14737 | MemExpr->getQualifierLoc(), Found.getDecl(), |
14738 | MemExpr->getTemplateKeywordLoc(), TemplateArgs); |
14739 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
14740 | return DRE; |
14741 | } else { |
14742 | SourceLocation Loc = MemExpr->getMemberLoc(); |
14743 | if (MemExpr->getQualifier()) |
14744 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
14745 | Base = |
14746 | BuildCXXThisExpr(Loc, MemExpr->getBaseType(), true); |
14747 | } |
14748 | } else |
14749 | Base = MemExpr->getBase(); |
14750 | |
14751 | ExprValueKind valueKind; |
14752 | QualType type; |
14753 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
14754 | valueKind = VK_LValue; |
14755 | type = Fn->getType(); |
14756 | } else { |
14757 | valueKind = VK_RValue; |
14758 | type = Context.BoundMemberTy; |
14759 | } |
14760 | |
14761 | return BuildMemberExpr( |
14762 | Base, MemExpr->isArrow(), MemExpr->getOperatorLoc(), |
14763 | MemExpr->getQualifierLoc(), MemExpr->getTemplateKeywordLoc(), Fn, Found, |
14764 | true, MemExpr->getMemberNameInfo(), |
14765 | type, valueKind, OK_Ordinary, TemplateArgs); |
14766 | } |
14767 | |
14768 | llvm_unreachable("Invalid reference to overloaded function"); |
14769 | } |
14770 | |
14771 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
14772 | DeclAccessPair Found, |
14773 | FunctionDecl *Fn) { |
14774 | return FixOverloadedFunctionReference(E.get(), Found, Fn); |
14775 | } |