@@ -407,6 +407,34 @@ class Sema;
407
407
Third == ICK_Identity;
408
408
}
409
409
410
+ // / A conversion sequence is perfect if it is an identity conversion and
411
+ // / the type of the source is the same as the type of the target.
412
+ bool isPerfect (const ASTContext &C) const {
413
+ if (!isIdentityConversion ())
414
+ return false ;
415
+ // If we are not performing a reference binding, we can skip comparing
416
+ // the types, which has a noticeable performance impact.
417
+ if (!ReferenceBinding) {
418
+ #ifndef NDEBUG
419
+ auto Decay = [&](QualType T) {
420
+ return (T->isArrayType () || T->isFunctionType ()) ? C.getDecayedType (T)
421
+ : T;
422
+ };
423
+ // The types might differ if there is an array-to-pointer conversion
424
+ // an function-to-pointer conversion, or lvalue-to-rvalue conversion.
425
+ // In some cases, this may happen even if First is not set.
426
+ assert (C.hasSameUnqualifiedType (Decay (getFromType ()),
427
+ Decay (getToType (2 ))));
428
+ #endif
429
+ return true ;
430
+ }
431
+ if (!C.hasSameType (getFromType (), getToType (2 )))
432
+ return false ;
433
+ if (BindsToRvalue && IsLvalueReference)
434
+ return false ;
435
+ return true ;
436
+ }
437
+
410
438
ImplicitConversionRank getRank () const ;
411
439
NarrowingKind
412
440
getNarrowingKind (ASTContext &Context, const Expr *Converted,
@@ -743,6 +771,12 @@ class Sema;
743
771
Standard.setAllToTypes (T);
744
772
}
745
773
774
+ // / A conversion sequence is perfect if it is an identity conversion and
775
+ // / the type of the source is the same as the type of the target.
776
+ bool isPerfect (const ASTContext &C) const {
777
+ return isStandard () && Standard.isPerfect (C);
778
+ }
779
+
746
780
// True iff this is a conversion sequence from an initializer list to an
747
781
// array or std::initializer.
748
782
bool hasInitializerListContainerType () const {
@@ -939,6 +973,10 @@ class Sema;
939
973
LLVM_PREFERRED_TYPE (CallExpr::ADLCallKind)
940
974
unsigned IsADLCandidate : 1 ;
941
975
976
+ // / Whether FinalConversion has been set.
977
+ LLVM_PREFERRED_TYPE (bool )
978
+ unsigned HasFinalConversion : 1 ;
979
+
942
980
// / Whether this is a rewritten candidate, and if so, of what kind?
943
981
LLVM_PREFERRED_TYPE (OverloadCandidateRewriteKind)
944
982
unsigned RewriteKind : 2 ;
@@ -979,6 +1017,20 @@ class Sema;
979
1017
return false ;
980
1018
}
981
1019
1020
+ // An overload is a perfect match if the conversion
1021
+ // sequences for each argument are perfect.
1022
+ bool isPerfectMatch (const ASTContext &Ctx) const {
1023
+ if (!Viable)
1024
+ return false ;
1025
+ for (const auto &C : Conversions) {
1026
+ if (!C.isInitialized () || !C.isPerfect (Ctx))
1027
+ return false ;
1028
+ }
1029
+ if (HasFinalConversion)
1030
+ return FinalConversion.isPerfect (Ctx);
1031
+ return true ;
1032
+ }
1033
+
982
1034
bool TryToFixBadConversion (unsigned Idx, Sema &S) {
983
1035
bool CanFix = Fix.tryToFixConversion (
984
1036
Conversions[Idx].Bad .FromExpr ,
@@ -1012,8 +1064,67 @@ class Sema;
1012
1064
: IsSurrogate(false ), IgnoreObjectArgument(false ),
1013
1065
TookAddressOfOverload (false ), StrictPackMatch(false ),
1014
1066
IsADLCandidate(llvm::to_underlying(CallExpr::NotADL)),
1015
- RewriteKind(CRK_None) {}
1067
+ HasFinalConversion(false ), RewriteKind(CRK_None) {}
1068
+ };
1069
+
1070
+ struct DeferredTemplateOverloadCandidate {
1071
+
1072
+ // intrusive linked list support for allocateDeferredCandidate
1073
+ DeferredTemplateOverloadCandidate *Next = nullptr ;
1074
+
1075
+ enum Kind { Function, Method, Conversion };
1076
+
1077
+ LLVM_PREFERRED_TYPE (Kind)
1078
+ unsigned Kind : 2 ;
1079
+ LLVM_PREFERRED_TYPE (bool )
1080
+ unsigned AllowObjCConversionOnExplicit : 1 ;
1081
+ LLVM_PREFERRED_TYPE (bool )
1082
+ unsigned AllowResultConversion : 1 ;
1083
+ LLVM_PREFERRED_TYPE (bool )
1084
+ unsigned AllowExplicit : 1 ;
1085
+ LLVM_PREFERRED_TYPE (bool )
1086
+ unsigned SuppressUserConversions : 1 ;
1087
+ LLVM_PREFERRED_TYPE (bool )
1088
+ unsigned PartialOverloading : 1 ;
1089
+ LLVM_PREFERRED_TYPE (bool )
1090
+ unsigned AggregateCandidateDeduction : 1 ;
1091
+ };
1092
+
1093
+ struct DeferredFunctionTemplateOverloadCandidate
1094
+ : public DeferredTemplateOverloadCandidate {
1095
+ FunctionTemplateDecl *FunctionTemplate;
1096
+ DeclAccessPair FoundDecl;
1097
+ ArrayRef<Expr *> Args;
1098
+ CallExpr::ADLCallKind IsADLCandidate;
1099
+ OverloadCandidateParamOrder PO;
1100
+ };
1101
+ static_assert (std::is_trivially_destructible_v<
1102
+ DeferredFunctionTemplateOverloadCandidate>);
1103
+
1104
+ struct DeferredMethodTemplateOverloadCandidate
1105
+ : public DeferredTemplateOverloadCandidate {
1106
+ FunctionTemplateDecl *FunctionTemplate;
1107
+ DeclAccessPair FoundDecl;
1108
+ ArrayRef<Expr *> Args;
1109
+ CXXRecordDecl *ActingContext;
1110
+ Expr::Classification ObjectClassification;
1111
+ QualType ObjectType;
1112
+ OverloadCandidateParamOrder PO;
1016
1113
};
1114
+ static_assert (std::is_trivially_destructible_v<
1115
+ DeferredMethodTemplateOverloadCandidate>);
1116
+
1117
+ struct DeferredConversionTemplateOverloadCandidate
1118
+ : public DeferredTemplateOverloadCandidate {
1119
+ FunctionTemplateDecl *FunctionTemplate;
1120
+ DeclAccessPair FoundDecl;
1121
+ CXXRecordDecl *ActingContext;
1122
+ Expr *From;
1123
+ QualType ToType;
1124
+ };
1125
+
1126
+ static_assert (std::is_trivially_destructible_v<
1127
+ DeferredConversionTemplateOverloadCandidate>);
1017
1128
1018
1129
// / OverloadCandidateSet - A set of overload candidates, used in C++
1019
1130
// / overload resolution (C++ 13.3).
@@ -1043,6 +1154,11 @@ class Sema;
1043
1154
// / C++ [over.match.call.general]
1044
1155
// / Resolve a call through the address of an overload set.
1045
1156
CSK_AddressOfOverloadSet,
1157
+
1158
+ // / When doing overload resolution during code completion,
1159
+ // / we want to show all viable candidates, including otherwise
1160
+ // / deferred template candidates.
1161
+ CSK_CodeCompletion,
1046
1162
};
1047
1163
1048
1164
// / Information about operator rewrites to consider when adding operator
@@ -1117,16 +1233,27 @@ class Sema;
1117
1233
SmallVector<OverloadCandidate, 16 > Candidates;
1118
1234
llvm::SmallPtrSet<uintptr_t , 16 > Functions;
1119
1235
1120
- // Allocator for ConversionSequenceLists. We store the first few of these
1236
+ DeferredTemplateOverloadCandidate *FirstDeferredCandidate = nullptr ;
1237
+ unsigned DeferredCandidatesCount : 8 * sizeof (unsigned ) - 2 ;
1238
+ LLVM_PREFERRED_TYPE (bool )
1239
+ unsigned HasDeferredTemplateConstructors : 1;
1240
+ LLVM_PREFERRED_TYPE (bool )
1241
+ unsigned ResolutionByPerfectCandidateIsDisabled : 1;
1242
+
1243
+ // Allocator for ConversionSequenceLists and deferred candidate args.
1244
+ // We store the first few of these
1121
1245
// inline to avoid allocation for small sets.
1122
1246
llvm::BumpPtrAllocator SlabAllocator;
1123
1247
1124
1248
SourceLocation Loc;
1125
1249
CandidateSetKind Kind;
1126
1250
OperatorRewriteInfo RewriteInfo;
1127
1251
1252
+ // / Small storage size for ImplicitConversionSequences
1253
+ // / and the persisted arguments of deferred candidates.
1128
1254
constexpr static unsigned NumInlineBytes =
1129
- 24 * sizeof (ImplicitConversionSequence);
1255
+ 32 * sizeof (ImplicitConversionSequence);
1256
+
1130
1257
unsigned NumInlineBytesUsed = 0 ;
1131
1258
alignas (void *) char InlineSpace[NumInlineBytes];
1132
1259
@@ -1137,15 +1264,13 @@ class Sema;
1137
1264
// / from the slab allocator.
1138
1265
// / FIXME: It would probably be nice to have a SmallBumpPtrAllocator
1139
1266
// / instead.
1140
- // / FIXME: Now that this only allocates ImplicitConversionSequences, do we
1141
- // / want to un-generalize this?
1142
1267
template <typename T>
1143
1268
T *slabAllocate (unsigned N) {
1144
1269
// It's simpler if this doesn't need to consider alignment.
1145
1270
static_assert (alignof (T) == alignof (void *),
1146
1271
" Only works for pointer-aligned types." );
1147
- static_assert (std::is_trivial <T>::value ||
1148
- std::is_same <ImplicitConversionSequence, T>::value ,
1272
+ static_assert (std::is_trivially_destructible_v <T> ||
1273
+ ( std::is_same_v <ImplicitConversionSequence, T>) ,
1149
1274
" Add destruction logic to OverloadCandidateSet::clear()." );
1150
1275
1151
1276
unsigned NBytes = sizeof (T) * N;
@@ -1159,12 +1284,34 @@ class Sema;
1159
1284
return reinterpret_cast <T *>(FreeSpaceStart);
1160
1285
}
1161
1286
1287
+ // Because the size of OverloadCandidateSet has a noticeable impact on
1288
+ // performance, we store each deferred template candidate in the slab
1289
+ // allocator such that deferred candidates are ultimately a singly-linked
1290
+ // intrusive linked list. This ends up being much more efficient than a
1291
+ // SmallVector that is empty in the common case.
1292
+ template <typename T> T *allocateDeferredCandidate () {
1293
+ T *C = slabAllocate<T>(1 );
1294
+ if (!FirstDeferredCandidate)
1295
+ FirstDeferredCandidate = C;
1296
+ else {
1297
+ auto *F = FirstDeferredCandidate;
1298
+ while (F->Next )
1299
+ F = F->Next ;
1300
+ F->Next = C;
1301
+ }
1302
+ DeferredCandidatesCount++;
1303
+ return C;
1304
+ }
1305
+
1162
1306
void destroyCandidates ();
1163
1307
1164
1308
public:
1165
1309
OverloadCandidateSet (SourceLocation Loc, CandidateSetKind CSK,
1166
1310
OperatorRewriteInfo RewriteInfo = {})
1167
- : Loc(Loc), Kind(CSK), RewriteInfo(RewriteInfo) {}
1311
+ : FirstDeferredCandidate(nullptr ), DeferredCandidatesCount(0 ),
1312
+ HasDeferredTemplateConstructors(false ),
1313
+ ResolutionByPerfectCandidateIsDisabled(false ), Loc(Loc), Kind(CSK),
1314
+ RewriteInfo(RewriteInfo) {}
1168
1315
OverloadCandidateSet (const OverloadCandidateSet &) = delete;
1169
1316
OverloadCandidateSet &operator =(const OverloadCandidateSet &) = delete ;
1170
1317
~OverloadCandidateSet () { destroyCandidates (); }
@@ -1176,6 +1323,9 @@ class Sema;
1176
1323
// / Whether diagnostics should be deferred.
1177
1324
bool shouldDeferDiags (Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc);
1178
1325
1326
+ // Whether the resolution of template candidates should be deferred
1327
+ bool shouldDeferTemplateArgumentDeduction (const LangOptions &Opts) const ;
1328
+
1179
1329
// / Determine when this overload candidate will be new to the
1180
1330
// / overload set.
1181
1331
bool isNewCandidate (Decl *F, OverloadCandidateParamOrder PO =
@@ -1199,8 +1349,10 @@ class Sema;
1199
1349
iterator begin () { return Candidates.begin (); }
1200
1350
iterator end () { return Candidates.end (); }
1201
1351
1202
- size_t size () const { return Candidates.size (); }
1203
- bool empty () const { return Candidates.empty (); }
1352
+ size_t size () const { return Candidates.size () + DeferredCandidatesCount; }
1353
+ bool empty () const {
1354
+ return Candidates.empty () && DeferredCandidatesCount == 0 ;
1355
+ }
1204
1356
1205
1357
// / Allocate storage for conversion sequences for NumConversions
1206
1358
// / conversions.
@@ -1216,6 +1368,24 @@ class Sema;
1216
1368
return ConversionSequenceList (Conversions, NumConversions);
1217
1369
}
1218
1370
1371
+ // / Provide storage for any Expr* arg that must be preserved
1372
+ // / until deferred template candidates are deduced.
1373
+ // / Typically this should be used for reversed operator arguments
1374
+ // / and any time the argument array is transformed while adding
1375
+ // / a template candidate.
1376
+ llvm::MutableArrayRef<Expr *> getPersistentArgsArray (unsigned N) {
1377
+ Expr **Exprs = slabAllocate<Expr *>(N);
1378
+ return llvm::MutableArrayRef<Expr *>(Exprs, N);
1379
+ }
1380
+
1381
+ template <typename ... T>
1382
+ llvm::MutableArrayRef<Expr *> getPersistentArgsArray (T *...Exprs) {
1383
+ llvm::MutableArrayRef<Expr *> Arr =
1384
+ getPersistentArgsArray (sizeof ...(Exprs));
1385
+ llvm::copy (std::initializer_list<Expr *>{Exprs...}, Arr.data ());
1386
+ return Arr;
1387
+ }
1388
+
1219
1389
// / Add a new candidate with NumConversions conversion sequence slots
1220
1390
// / to the overload set.
1221
1391
OverloadCandidate &addCandidate (unsigned NumConversions = 0 ,
@@ -1231,6 +1401,32 @@ class Sema;
1231
1401
return C;
1232
1402
}
1233
1403
1404
+ void AddDeferredTemplateCandidate (
1405
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
1406
+ ArrayRef<Expr *> Args, bool SuppressUserConversions,
1407
+ bool PartialOverloading, bool AllowExplicit,
1408
+ CallExpr::ADLCallKind IsADLCandidate, OverloadCandidateParamOrder PO,
1409
+ bool AggregateCandidateDeduction);
1410
+
1411
+ void AddDeferredMethodTemplateCandidate (
1412
+ FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl,
1413
+ CXXRecordDecl *ActingContext, QualType ObjectType,
1414
+ Expr::Classification ObjectClassification, ArrayRef<Expr *> Args,
1415
+ bool SuppressUserConversions, bool PartialOverloading,
1416
+ OverloadCandidateParamOrder PO);
1417
+
1418
+ void AddDeferredConversionTemplateCandidate (
1419
+ FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl,
1420
+ CXXRecordDecl *ActingContext, Expr *From, QualType ToType,
1421
+ bool AllowObjCConversionOnExplicit, bool AllowExplicit,
1422
+ bool AllowResultConversion);
1423
+
1424
+ void InjectNonDeducedTemplateCandidates (Sema &S);
1425
+
1426
+ void DisableResolutionByPerfectCandidate () {
1427
+ ResolutionByPerfectCandidateIsDisabled = true ;
1428
+ }
1429
+
1234
1430
// / Find the best viable function on this overload set, if it exists.
1235
1431
OverloadingResult BestViableFunction (Sema &S, SourceLocation Loc,
1236
1432
OverloadCandidateSet::iterator& Best);
@@ -1263,6 +1459,15 @@ class Sema;
1263
1459
DestAS = AS;
1264
1460
}
1265
1461
1462
+ private:
1463
+ OverloadingResult ResultForBestCandidate (const iterator &Best);
1464
+ void CudaExcludeWrongSideCandidates (
1465
+ Sema &S, SmallVectorImpl<OverloadCandidate *> &Candidates);
1466
+ OverloadingResult
1467
+ BestViableFunctionImpl (Sema &S, SourceLocation Loc,
1468
+ OverloadCandidateSet::iterator &Best);
1469
+ void PerfectViableFunction (Sema &S, SourceLocation Loc,
1470
+ OverloadCandidateSet::iterator &Best);
1266
1471
};
1267
1472
1268
1473
bool isBetterOverloadCandidate (Sema &S, const OverloadCandidate &Cand1,
@@ -1311,6 +1516,21 @@ class Sema;
1311
1516
// parameter.
1312
1517
bool shouldEnforceArgLimit (bool PartialOverloading, FunctionDecl *Function);
1313
1518
1519
+ inline bool OverloadCandidateSet::shouldDeferTemplateArgumentDeduction (
1520
+ const LangOptions &Opts) const {
1521
+ return
1522
+ // For user defined conversion we need to check against different
1523
+ // combination of CV qualifiers and look at any explicit specifier, so
1524
+ // always deduce template candidates.
1525
+ Kind != CSK_InitByUserDefinedConversion
1526
+ // When doing code completion, we want to see all the
1527
+ // viable candidates.
1528
+ && Kind != CSK_CodeCompletion
1529
+ // CUDA may prefer template candidates even when a non-candidate
1530
+ // is a perfect match
1531
+ && !Opts.CUDA ;
1532
+ }
1533
+
1314
1534
} // namespace clang
1315
1535
1316
1536
#endif // LLVM_CLANG_SEMA_OVERLOAD_H
0 commit comments