File indexing completed on 2026-05-10 08:37:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef LLVM_CLANG_SEMA_OVERLOAD_H
0015 #define LLVM_CLANG_SEMA_OVERLOAD_H
0016
0017 #include "clang/AST/Decl.h"
0018 #include "clang/AST/DeclAccessPair.h"
0019 #include "clang/AST/DeclBase.h"
0020 #include "clang/AST/DeclCXX.h"
0021 #include "clang/AST/DeclTemplate.h"
0022 #include "clang/AST/Expr.h"
0023 #include "clang/AST/Type.h"
0024 #include "clang/Basic/LLVM.h"
0025 #include "clang/Basic/SourceLocation.h"
0026 #include "clang/Sema/SemaFixItUtils.h"
0027 #include "clang/Sema/TemplateDeduction.h"
0028 #include "llvm/ADT/ArrayRef.h"
0029 #include "llvm/ADT/STLExtras.h"
0030 #include "llvm/ADT/SmallPtrSet.h"
0031 #include "llvm/ADT/SmallVector.h"
0032 #include "llvm/ADT/StringRef.h"
0033 #include "llvm/Support/AlignOf.h"
0034 #include "llvm/Support/Allocator.h"
0035 #include "llvm/Support/Casting.h"
0036 #include "llvm/Support/ErrorHandling.h"
0037 #include <cassert>
0038 #include <cstddef>
0039 #include <cstdint>
0040 #include <utility>
0041
0042 namespace clang {
0043
0044 class APValue;
0045 class ASTContext;
0046 class Sema;
0047
0048
0049
0050 enum OverloadingResult {
0051
0052 OR_Success,
0053
0054
0055 OR_No_Viable_Function,
0056
0057
0058 OR_Ambiguous,
0059
0060
0061 OR_Deleted
0062 };
0063
0064 enum OverloadCandidateDisplayKind {
0065
0066
0067 OCD_AllCandidates,
0068
0069
0070 OCD_ViableCandidates,
0071
0072
0073 OCD_AmbiguousCandidates
0074 };
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 enum class OverloadCandidateParamOrder : char { Normal, Reversed };
0085
0086
0087
0088
0089 enum OverloadCandidateRewriteKind : unsigned {
0090
0091 CRK_None = 0x0,
0092
0093
0094 CRK_DifferentOperator = 0x1,
0095
0096
0097 CRK_Reversed = 0x2,
0098 };
0099
0100
0101
0102
0103
0104 enum ImplicitConversionKind {
0105
0106 ICK_Identity = 0,
0107
0108
0109 ICK_Lvalue_To_Rvalue,
0110
0111
0112 ICK_Array_To_Pointer,
0113
0114
0115 ICK_Function_To_Pointer,
0116
0117
0118 ICK_Function_Conversion,
0119
0120
0121 ICK_Qualification,
0122
0123
0124 ICK_Integral_Promotion,
0125
0126
0127 ICK_Floating_Promotion,
0128
0129
0130 ICK_Complex_Promotion,
0131
0132
0133 ICK_Integral_Conversion,
0134
0135
0136 ICK_Floating_Conversion,
0137
0138
0139 ICK_Complex_Conversion,
0140
0141
0142 ICK_Floating_Integral,
0143
0144
0145 ICK_Pointer_Conversion,
0146
0147
0148 ICK_Pointer_Member,
0149
0150
0151 ICK_Boolean_Conversion,
0152
0153
0154 ICK_Compatible_Conversion,
0155
0156
0157 ICK_Derived_To_Base,
0158
0159
0160 ICK_Vector_Conversion,
0161
0162
0163 ICK_SVE_Vector_Conversion,
0164
0165
0166 ICK_RVV_Vector_Conversion,
0167
0168
0169 ICK_Vector_Splat,
0170
0171
0172 ICK_Complex_Real,
0173
0174
0175 ICK_Block_Pointer_Conversion,
0176
0177
0178 ICK_TransparentUnionConversion,
0179
0180
0181 ICK_Writeback_Conversion,
0182
0183
0184 ICK_Zero_Event_Conversion,
0185
0186
0187 ICK_Zero_Queue_Conversion,
0188
0189
0190 ICK_C_Only_Conversion,
0191
0192
0193 ICK_Incompatible_Pointer_Conversion,
0194
0195
0196 ICK_Fixed_Point_Conversion,
0197
0198
0199 ICK_HLSL_Vector_Truncation,
0200
0201
0202 ICK_HLSL_Array_RValue,
0203
0204
0205 ICK_HLSL_Vector_Splat,
0206
0207
0208 ICK_Num_Conversion_Kinds,
0209 };
0210
0211
0212
0213
0214
0215 enum ImplicitConversionRank {
0216
0217 ICR_Exact_Match = 0,
0218
0219
0220 ICR_HLSL_Scalar_Widening,
0221
0222
0223 ICR_Promotion,
0224
0225
0226 ICR_HLSL_Scalar_Widening_Promotion,
0227
0228
0229 ICR_Conversion,
0230
0231
0232 ICR_OCL_Scalar_Widening,
0233
0234
0235 ICR_HLSL_Scalar_Widening_Conversion,
0236
0237
0238 ICR_Complex_Real_Conversion,
0239
0240
0241 ICR_Writeback_Conversion,
0242
0243
0244 ICR_C_Conversion,
0245
0246
0247
0248 ICR_C_Conversion_Extension,
0249
0250
0251 ICR_HLSL_Dimension_Reduction,
0252
0253
0254 ICR_HLSL_Dimension_Reduction_Promotion,
0255
0256
0257 ICR_HLSL_Dimension_Reduction_Conversion,
0258 };
0259
0260 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
0261
0262 ImplicitConversionRank
0263 GetDimensionConversionRank(ImplicitConversionRank Base,
0264 ImplicitConversionKind Dimension);
0265
0266
0267
0268 enum NarrowingKind {
0269
0270 NK_Not_Narrowing,
0271
0272
0273 NK_Type_Narrowing,
0274
0275
0276 NK_Constant_Narrowing,
0277
0278
0279
0280 NK_Variable_Narrowing,
0281
0282
0283
0284 NK_Dependent_Narrowing,
0285 };
0286
0287
0288
0289
0290
0291
0292 class StandardConversionSequence {
0293 public:
0294
0295
0296
0297 ImplicitConversionKind First : 8;
0298
0299
0300
0301
0302
0303 ImplicitConversionKind Second : 8;
0304
0305
0306
0307
0308 ImplicitConversionKind Dimension : 8;
0309
0310
0311
0312 ImplicitConversionKind Third : 8;
0313
0314
0315
0316
0317 LLVM_PREFERRED_TYPE(bool)
0318 unsigned DeprecatedStringLiteralToCharPtr : 1;
0319
0320
0321
0322 LLVM_PREFERRED_TYPE(bool)
0323 unsigned QualificationIncludesObjCLifetime : 1;
0324
0325
0326
0327 LLVM_PREFERRED_TYPE(bool)
0328 unsigned IncompatibleObjC : 1;
0329
0330
0331
0332 LLVM_PREFERRED_TYPE(bool)
0333 unsigned ReferenceBinding : 1;
0334
0335
0336
0337 LLVM_PREFERRED_TYPE(bool)
0338 unsigned DirectBinding : 1;
0339
0340
0341
0342 LLVM_PREFERRED_TYPE(bool)
0343 unsigned IsLvalueReference : 1;
0344
0345
0346 LLVM_PREFERRED_TYPE(bool)
0347 unsigned BindsToFunctionLvalue : 1;
0348
0349
0350 LLVM_PREFERRED_TYPE(bool)
0351 unsigned BindsToRvalue : 1;
0352
0353
0354
0355 LLVM_PREFERRED_TYPE(bool)
0356 unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
0357
0358
0359
0360 LLVM_PREFERRED_TYPE(bool)
0361 unsigned ObjCLifetimeConversionBinding : 1;
0362
0363
0364
0365
0366 void *FromTypePtr;
0367
0368
0369
0370
0371 void *ToTypePtrs[3];
0372
0373
0374
0375
0376
0377
0378 CXXConstructorDecl *CopyConstructor;
0379 DeclAccessPair FoundCopyConstructor;
0380
0381 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
0382
0383 void setToType(unsigned Idx, QualType T) {
0384 assert(Idx < 3 && "To type index is out of range");
0385 ToTypePtrs[Idx] = T.getAsOpaquePtr();
0386 }
0387
0388 void setAllToTypes(QualType T) {
0389 ToTypePtrs[0] = T.getAsOpaquePtr();
0390 ToTypePtrs[1] = ToTypePtrs[0];
0391 ToTypePtrs[2] = ToTypePtrs[0];
0392 }
0393
0394 QualType getFromType() const {
0395 return QualType::getFromOpaquePtr(FromTypePtr);
0396 }
0397
0398 QualType getToType(unsigned Idx) const {
0399 assert(Idx < 3 && "To type index is out of range");
0400 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
0401 }
0402
0403 void setAsIdentityConversion();
0404
0405 bool isIdentityConversion() const {
0406 return Second == ICK_Identity && Dimension == ICK_Identity &&
0407 Third == ICK_Identity;
0408 }
0409
0410 ImplicitConversionRank getRank() const;
0411 NarrowingKind
0412 getNarrowingKind(ASTContext &Context, const Expr *Converted,
0413 APValue &ConstantValue, QualType &ConstantType,
0414 bool IgnoreFloatToIntegralConversion = false) const;
0415 bool isPointerConversionToBool() const;
0416 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
0417 void dump() const;
0418 };
0419
0420
0421
0422 struct UserDefinedConversionSequence {
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434 StandardConversionSequence Before;
0435
0436
0437
0438
0439
0440
0441
0442 bool EllipsisConversion : 1;
0443
0444
0445
0446
0447 bool HadMultipleCandidates : 1;
0448
0449
0450
0451 StandardConversionSequence After;
0452
0453
0454
0455
0456 FunctionDecl* ConversionFunction;
0457
0458
0459
0460
0461 DeclAccessPair FoundConversionFunction;
0462
0463 void dump() const;
0464 };
0465
0466
0467 struct AmbiguousConversionSequence {
0468 using ConversionSet =
0469 SmallVector<std::pair<NamedDecl *, FunctionDecl *>, 4>;
0470
0471 void *FromTypePtr;
0472 void *ToTypePtr;
0473 char Buffer[sizeof(ConversionSet)];
0474
0475 QualType getFromType() const {
0476 return QualType::getFromOpaquePtr(FromTypePtr);
0477 }
0478
0479 QualType getToType() const {
0480 return QualType::getFromOpaquePtr(ToTypePtr);
0481 }
0482
0483 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
0484 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
0485
0486 ConversionSet &conversions() {
0487 return *reinterpret_cast<ConversionSet*>(Buffer);
0488 }
0489
0490 const ConversionSet &conversions() const {
0491 return *reinterpret_cast<const ConversionSet*>(Buffer);
0492 }
0493
0494 void addConversion(NamedDecl *Found, FunctionDecl *D) {
0495 conversions().push_back(std::make_pair(Found, D));
0496 }
0497
0498 using iterator = ConversionSet::iterator;
0499
0500 iterator begin() { return conversions().begin(); }
0501 iterator end() { return conversions().end(); }
0502
0503 using const_iterator = ConversionSet::const_iterator;
0504
0505 const_iterator begin() const { return conversions().begin(); }
0506 const_iterator end() const { return conversions().end(); }
0507
0508 void construct();
0509 void destruct();
0510 void copyFrom(const AmbiguousConversionSequence &);
0511 };
0512
0513
0514
0515 struct BadConversionSequence {
0516 enum FailureKind {
0517 no_conversion,
0518 unrelated_class,
0519 bad_qualifiers,
0520 lvalue_ref_to_rvalue,
0521 rvalue_ref_to_lvalue,
0522 too_few_initializers,
0523 too_many_initializers,
0524 };
0525
0526
0527 Expr *FromExpr;
0528
0529 FailureKind Kind;
0530
0531 private:
0532
0533 void *FromTy;
0534
0535
0536 void *ToTy;
0537
0538 public:
0539 void init(FailureKind K, Expr *From, QualType To) {
0540 init(K, From->getType(), To);
0541 FromExpr = From;
0542 }
0543
0544 void init(FailureKind K, QualType From, QualType To) {
0545 Kind = K;
0546 FromExpr = nullptr;
0547 setFromType(From);
0548 setToType(To);
0549 }
0550
0551 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
0552 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
0553
0554 void setFromExpr(Expr *E) {
0555 FromExpr = E;
0556 setFromType(E->getType());
0557 }
0558
0559 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
0560 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
0561 };
0562
0563
0564
0565
0566
0567 class ImplicitConversionSequence {
0568 public:
0569
0570
0571
0572
0573
0574
0575
0576 enum Kind {
0577 StandardConversion = 0,
0578 StaticObjectArgumentConversion,
0579 UserDefinedConversion,
0580 AmbiguousConversion,
0581 EllipsisConversion,
0582 BadConversion
0583 };
0584
0585 private:
0586 enum {
0587 Uninitialized = BadConversion + 1
0588 };
0589
0590
0591 LLVM_PREFERRED_TYPE(Kind)
0592 unsigned ConversionKind : 31;
0593
0594
0595 LLVM_PREFERRED_TYPE(bool)
0596 unsigned InitializerListOfIncompleteArray : 1;
0597
0598
0599
0600
0601
0602
0603 QualType InitializerListContainerType;
0604
0605 void setKind(Kind K) {
0606 destruct();
0607 ConversionKind = K;
0608 }
0609
0610 void destruct() {
0611 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
0612 }
0613
0614 public:
0615 union {
0616
0617
0618 StandardConversionSequence Standard;
0619
0620
0621
0622 UserDefinedConversionSequence UserDefined;
0623
0624
0625
0626 AmbiguousConversionSequence Ambiguous;
0627
0628
0629
0630 BadConversionSequence Bad;
0631 };
0632
0633 ImplicitConversionSequence()
0634 : ConversionKind(Uninitialized),
0635 InitializerListOfIncompleteArray(false) {
0636 Standard.setAsIdentityConversion();
0637 }
0638
0639 ImplicitConversionSequence(const ImplicitConversionSequence &Other)
0640 : ConversionKind(Other.ConversionKind),
0641 InitializerListOfIncompleteArray(
0642 Other.InitializerListOfIncompleteArray),
0643 InitializerListContainerType(Other.InitializerListContainerType) {
0644 switch (ConversionKind) {
0645 case Uninitialized: break;
0646 case StandardConversion: Standard = Other.Standard; break;
0647 case StaticObjectArgumentConversion:
0648 break;
0649 case UserDefinedConversion: UserDefined = Other.UserDefined; break;
0650 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
0651 case EllipsisConversion: break;
0652 case BadConversion: Bad = Other.Bad; break;
0653 }
0654 }
0655
0656 ImplicitConversionSequence &
0657 operator=(const ImplicitConversionSequence &Other) {
0658 destruct();
0659 new (this) ImplicitConversionSequence(Other);
0660 return *this;
0661 }
0662
0663 ~ImplicitConversionSequence() {
0664 destruct();
0665 }
0666
0667 Kind getKind() const {
0668 assert(isInitialized() && "querying uninitialized conversion");
0669 return Kind(ConversionKind);
0670 }
0671
0672
0673
0674
0675
0676
0677
0678
0679 unsigned getKindRank() const {
0680 switch (getKind()) {
0681 case StandardConversion:
0682 case StaticObjectArgumentConversion:
0683 return 0;
0684
0685 case UserDefinedConversion:
0686 case AmbiguousConversion:
0687 return 1;
0688
0689 case EllipsisConversion:
0690 return 2;
0691
0692 case BadConversion:
0693 return 3;
0694 }
0695
0696 llvm_unreachable("Invalid ImplicitConversionSequence::Kind!");
0697 }
0698
0699 bool isBad() const { return getKind() == BadConversion; }
0700 bool isStandard() const { return getKind() == StandardConversion; }
0701 bool isStaticObjectArgument() const {
0702 return getKind() == StaticObjectArgumentConversion;
0703 }
0704 bool isEllipsis() const { return getKind() == EllipsisConversion; }
0705 bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
0706 bool isUserDefined() const { return getKind() == UserDefinedConversion; }
0707 bool isFailure() const { return isBad() || isAmbiguous(); }
0708
0709
0710
0711
0712 bool isInitialized() const { return ConversionKind != Uninitialized; }
0713
0714
0715 void setBad(BadConversionSequence::FailureKind Failure,
0716 Expr *FromExpr, QualType ToType) {
0717 setKind(BadConversion);
0718 Bad.init(Failure, FromExpr, ToType);
0719 }
0720
0721
0722 void setBad(BadConversionSequence::FailureKind Failure,
0723 QualType FromType, QualType ToType) {
0724 setKind(BadConversion);
0725 Bad.init(Failure, FromType, ToType);
0726 }
0727
0728 void setStandard() { setKind(StandardConversion); }
0729 void setStaticObjectArgument() { setKind(StaticObjectArgumentConversion); }
0730 void setEllipsis() { setKind(EllipsisConversion); }
0731 void setUserDefined() { setKind(UserDefinedConversion); }
0732
0733 void setAmbiguous() {
0734 if (ConversionKind == AmbiguousConversion) return;
0735 ConversionKind = AmbiguousConversion;
0736 Ambiguous.construct();
0737 }
0738
0739 void setAsIdentityConversion(QualType T) {
0740 setStandard();
0741 Standard.setAsIdentityConversion();
0742 Standard.setFromType(T);
0743 Standard.setAllToTypes(T);
0744 }
0745
0746
0747
0748 bool hasInitializerListContainerType() const {
0749 return !InitializerListContainerType.isNull();
0750 }
0751 void setInitializerListContainerType(QualType T, bool IA) {
0752 InitializerListContainerType = T;
0753 InitializerListOfIncompleteArray = IA;
0754 }
0755 bool isInitializerListOfIncompleteArray() const {
0756 return InitializerListOfIncompleteArray;
0757 }
0758 QualType getInitializerListContainerType() const {
0759 assert(hasInitializerListContainerType() &&
0760 "not initializer list container");
0761 return InitializerListContainerType;
0762 }
0763
0764
0765
0766 static ImplicitConversionSequence getNullptrToBool(QualType SourceType,
0767 QualType DestType,
0768 bool NeedLValToRVal) {
0769 ImplicitConversionSequence ICS;
0770 ICS.setStandard();
0771 ICS.Standard.setAsIdentityConversion();
0772 ICS.Standard.setFromType(SourceType);
0773 if (NeedLValToRVal)
0774 ICS.Standard.First = ICK_Lvalue_To_Rvalue;
0775 ICS.Standard.setToType(0, SourceType);
0776 ICS.Standard.Second = ICK_Boolean_Conversion;
0777 ICS.Standard.setToType(1, DestType);
0778 ICS.Standard.setToType(2, DestType);
0779 return ICS;
0780 }
0781
0782
0783
0784
0785 enum CompareKind {
0786 Better = -1,
0787 Indistinguishable = 0,
0788 Worse = 1
0789 };
0790
0791 void DiagnoseAmbiguousConversion(Sema &S,
0792 SourceLocation CaretLoc,
0793 const PartialDiagnostic &PDiag) const;
0794
0795 void dump() const;
0796 };
0797
0798 enum OverloadFailureKind {
0799 ovl_fail_too_many_arguments,
0800 ovl_fail_too_few_arguments,
0801 ovl_fail_bad_conversion,
0802 ovl_fail_bad_deduction,
0803
0804
0805
0806
0807 ovl_fail_trivial_conversion,
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818 ovl_fail_illegal_constructor,
0819
0820
0821
0822 ovl_fail_bad_final_conversion,
0823
0824
0825
0826 ovl_fail_final_conversion_not_exact,
0827
0828
0829
0830
0831 ovl_fail_bad_target,
0832
0833
0834
0835 ovl_fail_enable_if,
0836
0837
0838
0839 ovl_fail_explicit,
0840
0841
0842 ovl_fail_addr_not_available,
0843
0844
0845
0846 ovl_fail_inhctor_slice,
0847
0848
0849
0850 ovl_non_default_multiversion_function,
0851
0852
0853
0854
0855 ovl_fail_object_addrspace_mismatch,
0856
0857
0858
0859 ovl_fail_constraints_not_satisfied,
0860
0861
0862
0863 ovl_fail_module_mismatched,
0864 };
0865
0866
0867
0868 using ConversionSequenceList =
0869 llvm::MutableArrayRef<ImplicitConversionSequence>;
0870
0871
0872 struct OverloadCandidate {
0873
0874
0875
0876
0877 FunctionDecl *Function;
0878
0879
0880
0881
0882 DeclAccessPair FoundDecl;
0883
0884
0885
0886 QualType BuiltinParamTypes[3];
0887
0888
0889
0890 CXXConversionDecl *Surrogate;
0891
0892
0893
0894
0895 ConversionSequenceList Conversions;
0896
0897
0898 ConversionFixItGenerator Fix;
0899
0900
0901 LLVM_PREFERRED_TYPE(bool)
0902 unsigned Viable : 1;
0903
0904
0905
0906
0907
0908
0909
0910
0911 LLVM_PREFERRED_TYPE(bool)
0912 unsigned Best : 1;
0913
0914
0915
0916
0917 LLVM_PREFERRED_TYPE(bool)
0918 unsigned IsSurrogate : 1;
0919
0920
0921
0922
0923
0924
0925
0926
0927 LLVM_PREFERRED_TYPE(bool)
0928 unsigned IgnoreObjectArgument : 1;
0929
0930 LLVM_PREFERRED_TYPE(bool)
0931 unsigned TookAddressOfOverload : 1;
0932
0933
0934
0935
0936 bool HasMatchedPackOnParmToNonPackOnArg : 1;
0937
0938
0939 LLVM_PREFERRED_TYPE(CallExpr::ADLCallKind)
0940 unsigned IsADLCandidate : 1;
0941
0942
0943 LLVM_PREFERRED_TYPE(OverloadCandidateRewriteKind)
0944 unsigned RewriteKind : 2;
0945
0946
0947
0948 unsigned char FailureKind;
0949
0950
0951
0952 unsigned ExplicitCallArguments;
0953
0954 union {
0955 DeductionFailureInfo DeductionFailure;
0956
0957
0958
0959
0960
0961 StandardConversionSequence FinalConversion;
0962 };
0963
0964
0965
0966 OverloadCandidateRewriteKind getRewriteKind() const {
0967 return static_cast<OverloadCandidateRewriteKind>(RewriteKind);
0968 }
0969
0970 bool isReversed() const { return getRewriteKind() & CRK_Reversed; }
0971
0972
0973
0974 bool hasAmbiguousConversion() const {
0975 for (auto &C : Conversions) {
0976 if (!C.isInitialized()) return false;
0977 if (C.isAmbiguous()) return true;
0978 }
0979 return false;
0980 }
0981
0982 bool TryToFixBadConversion(unsigned Idx, Sema &S) {
0983 bool CanFix = Fix.tryToFixConversion(
0984 Conversions[Idx].Bad.FromExpr,
0985 Conversions[Idx].Bad.getFromType(),
0986 Conversions[Idx].Bad.getToType(), S);
0987
0988
0989 if (!CanFix)
0990 Fix.clear();
0991
0992 return CanFix;
0993 }
0994
0995 unsigned getNumParams() const {
0996 if (IsSurrogate) {
0997 QualType STy = Surrogate->getConversionType();
0998 while (STy->isPointerOrReferenceType())
0999 STy = STy->getPointeeType();
1000 return STy->castAs<FunctionProtoType>()->getNumParams();
1001 }
1002 if (Function)
1003 return Function->getNumParams();
1004 return ExplicitCallArguments;
1005 }
1006
1007 bool NotValidBecauseConstraintExprHasError() const;
1008
1009 private:
1010 friend class OverloadCandidateSet;
1011 OverloadCandidate()
1012 : IsSurrogate(false), IgnoreObjectArgument(false),
1013 TookAddressOfOverload(false),
1014 HasMatchedPackOnParmToNonPackOnArg(false),
1015 IsADLCandidate(llvm::to_underlying(CallExpr::NotADL)),
1016 RewriteKind(CRK_None) {}
1017 };
1018
1019
1020
1021 class OverloadCandidateSet {
1022 public:
1023 enum CandidateSetKind {
1024
1025 CSK_Normal,
1026
1027
1028
1029
1030
1031
1032 CSK_Operator,
1033
1034
1035
1036
1037 CSK_InitByUserDefinedConversion,
1038
1039
1040
1041
1042 CSK_InitByConstructor,
1043
1044
1045
1046 CSK_AddressOfOverloadSet,
1047 };
1048
1049
1050
1051 struct OperatorRewriteInfo {
1052 OperatorRewriteInfo()
1053 : OriginalOperator(OO_None), OpLoc(), AllowRewrittenCandidates(false) {}
1054 OperatorRewriteInfo(OverloadedOperatorKind Op, SourceLocation OpLoc,
1055 bool AllowRewritten)
1056 : OriginalOperator(Op), OpLoc(OpLoc),
1057 AllowRewrittenCandidates(AllowRewritten) {}
1058
1059
1060 OverloadedOperatorKind OriginalOperator;
1061
1062 SourceLocation OpLoc;
1063
1064 bool AllowRewrittenCandidates;
1065
1066
1067
1068 bool isRewrittenOperator(const FunctionDecl *FD) {
1069 return OriginalOperator &&
1070 FD->getDeclName().getCXXOverloadedOperator() != OriginalOperator;
1071 }
1072
1073 bool isAcceptableCandidate(const FunctionDecl *FD) {
1074 if (!OriginalOperator)
1075 return true;
1076
1077
1078
1079
1080 OverloadedOperatorKind OO =
1081 FD->getDeclName().getCXXOverloadedOperator();
1082 return OO && (OO == OriginalOperator ||
1083 (AllowRewrittenCandidates &&
1084 OO == getRewrittenOverloadedOperator(OriginalOperator)));
1085 }
1086
1087
1088
1089 OverloadCandidateRewriteKind
1090 getRewriteKind(const FunctionDecl *FD, OverloadCandidateParamOrder PO) {
1091 OverloadCandidateRewriteKind CRK = CRK_None;
1092 if (isRewrittenOperator(FD))
1093 CRK = OverloadCandidateRewriteKind(CRK | CRK_DifferentOperator);
1094 if (PO == OverloadCandidateParamOrder::Reversed)
1095 CRK = OverloadCandidateRewriteKind(CRK | CRK_Reversed);
1096 return CRK;
1097 }
1098
1099
1100 bool isReversible() {
1101 return AllowRewrittenCandidates && OriginalOperator &&
1102 (getRewrittenOverloadedOperator(OriginalOperator) != OO_None ||
1103 allowsReversed(OriginalOperator));
1104 }
1105
1106
1107
1108 bool allowsReversed(OverloadedOperatorKind Op);
1109
1110
1111
1112
1113 bool shouldAddReversed(Sema &S, ArrayRef<Expr *> OriginalArgs,
1114 FunctionDecl *FD);
1115 };
1116
1117 private:
1118 SmallVector<OverloadCandidate, 16> Candidates;
1119 llvm::SmallPtrSet<uintptr_t, 16> Functions;
1120
1121
1122
1123 llvm::BumpPtrAllocator SlabAllocator;
1124
1125 SourceLocation Loc;
1126 CandidateSetKind Kind;
1127 OperatorRewriteInfo RewriteInfo;
1128
1129 constexpr static unsigned NumInlineBytes =
1130 24 * sizeof(ImplicitConversionSequence);
1131 unsigned NumInlineBytesUsed = 0;
1132 alignas(void *) char InlineSpace[NumInlineBytes];
1133
1134
1135 LangAS DestAS = LangAS::Default;
1136
1137
1138
1139
1140
1141
1142
1143 template <typename T>
1144 T *slabAllocate(unsigned N) {
1145
1146 static_assert(alignof(T) == alignof(void *),
1147 "Only works for pointer-aligned types.");
1148 static_assert(std::is_trivial<T>::value ||
1149 std::is_same<ImplicitConversionSequence, T>::value,
1150 "Add destruction logic to OverloadCandidateSet::clear().");
1151
1152 unsigned NBytes = sizeof(T) * N;
1153 if (NBytes > NumInlineBytes - NumInlineBytesUsed)
1154 return SlabAllocator.Allocate<T>(N);
1155 char *FreeSpaceStart = InlineSpace + NumInlineBytesUsed;
1156 assert(uintptr_t(FreeSpaceStart) % alignof(void *) == 0 &&
1157 "Misaligned storage!");
1158
1159 NumInlineBytesUsed += NBytes;
1160 return reinterpret_cast<T *>(FreeSpaceStart);
1161 }
1162
1163 void destroyCandidates();
1164
1165 public:
1166 OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK,
1167 OperatorRewriteInfo RewriteInfo = {})
1168 : Loc(Loc), Kind(CSK), RewriteInfo(RewriteInfo) {}
1169 OverloadCandidateSet(const OverloadCandidateSet &) = delete;
1170 OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete;
1171 ~OverloadCandidateSet() { destroyCandidates(); }
1172
1173 SourceLocation getLocation() const { return Loc; }
1174 CandidateSetKind getKind() const { return Kind; }
1175 OperatorRewriteInfo getRewriteInfo() const { return RewriteInfo; }
1176
1177
1178 bool shouldDeferDiags(Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc);
1179
1180
1181
1182 bool isNewCandidate(Decl *F, OverloadCandidateParamOrder PO =
1183 OverloadCandidateParamOrder::Normal) {
1184 uintptr_t Key = reinterpret_cast<uintptr_t>(F->getCanonicalDecl());
1185 Key |= static_cast<uintptr_t>(PO);
1186 return Functions.insert(Key).second;
1187 }
1188
1189
1190 void exclude(Decl *F) {
1191 isNewCandidate(F, OverloadCandidateParamOrder::Normal);
1192 isNewCandidate(F, OverloadCandidateParamOrder::Reversed);
1193 }
1194
1195
1196 void clear(CandidateSetKind CSK);
1197
1198 using iterator = SmallVectorImpl<OverloadCandidate>::iterator;
1199
1200 iterator begin() { return Candidates.begin(); }
1201 iterator end() { return Candidates.end(); }
1202
1203 size_t size() const { return Candidates.size(); }
1204 bool empty() const { return Candidates.empty(); }
1205
1206
1207
1208 ConversionSequenceList
1209 allocateConversionSequences(unsigned NumConversions) {
1210 ImplicitConversionSequence *Conversions =
1211 slabAllocate<ImplicitConversionSequence>(NumConversions);
1212
1213
1214 for (unsigned I = 0; I != NumConversions; ++I)
1215 new (&Conversions[I]) ImplicitConversionSequence();
1216
1217 return ConversionSequenceList(Conversions, NumConversions);
1218 }
1219
1220
1221
1222 OverloadCandidate &addCandidate(unsigned NumConversions = 0,
1223 ConversionSequenceList Conversions = {}) {
1224 assert((Conversions.empty() || Conversions.size() == NumConversions) &&
1225 "preallocated conversion sequence has wrong length");
1226
1227 Candidates.push_back(OverloadCandidate());
1228 OverloadCandidate &C = Candidates.back();
1229 C.Conversions = Conversions.empty()
1230 ? allocateConversionSequences(NumConversions)
1231 : Conversions;
1232 return C;
1233 }
1234
1235
1236 OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
1237 OverloadCandidateSet::iterator& Best);
1238
1239 SmallVector<OverloadCandidate *, 32> CompleteCandidates(
1240 Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args,
1241 SourceLocation OpLoc = SourceLocation(),
1242 llvm::function_ref<bool(OverloadCandidate &)> Filter =
1243 [](OverloadCandidate &) { return true; });
1244
1245 void NoteCandidates(
1246 PartialDiagnosticAt PA, Sema &S, OverloadCandidateDisplayKind OCD,
1247 ArrayRef<Expr *> Args, StringRef Opc = "",
1248 SourceLocation Loc = SourceLocation(),
1249 llvm::function_ref<bool(OverloadCandidate &)> Filter =
1250 [](OverloadCandidate &) { return true; });
1251
1252 void NoteCandidates(Sema &S, ArrayRef<Expr *> Args,
1253 ArrayRef<OverloadCandidate *> Cands,
1254 StringRef Opc = "",
1255 SourceLocation OpLoc = SourceLocation());
1256
1257 LangAS getDestAS() { return DestAS; }
1258
1259 void setDestAS(LangAS AS) {
1260 assert((Kind == CSK_InitByConstructor ||
1261 Kind == CSK_InitByUserDefinedConversion) &&
1262 "can't set the destination address space when not constructing an "
1263 "object");
1264 DestAS = AS;
1265 }
1266
1267 };
1268
1269 bool isBetterOverloadCandidate(Sema &S,
1270 const OverloadCandidate &Cand1,
1271 const OverloadCandidate &Cand2,
1272 SourceLocation Loc,
1273 OverloadCandidateSet::CandidateSetKind Kind);
1274
1275 struct ConstructorInfo {
1276 DeclAccessPair FoundDecl;
1277 CXXConstructorDecl *Constructor;
1278 FunctionTemplateDecl *ConstructorTmpl;
1279
1280 explicit operator bool() const { return Constructor; }
1281 };
1282
1283
1284
1285 inline ConstructorInfo getConstructorInfo(NamedDecl *ND) {
1286 if (isa<UsingDecl>(ND))
1287 return ConstructorInfo{};
1288
1289
1290
1291 auto *D = ND->getUnderlyingDecl();
1292 ConstructorInfo Info = {DeclAccessPair::make(ND, D->getAccess()), nullptr,
1293 nullptr};
1294 Info.ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
1295 if (Info.ConstructorTmpl)
1296 D = Info.ConstructorTmpl->getTemplatedDecl();
1297 Info.Constructor = dyn_cast<CXXConstructorDecl>(D);
1298 return Info;
1299 }
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313 bool shouldEnforceArgLimit(bool PartialOverloading, FunctionDecl *Function);
1314
1315 }
1316
1317 #endif