Overload.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Overload.h - C++ Overloading -----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the data structures and types used in C++
  15. // overload resolution.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SEMA_OVERLOAD_H
  19. #define LLVM_CLANG_SEMA_OVERLOAD_H
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/DeclAccessPair.h"
  22. #include "clang/AST/DeclBase.h"
  23. #include "clang/AST/DeclCXX.h"
  24. #include "clang/AST/DeclTemplate.h"
  25. #include "clang/AST/Expr.h"
  26. #include "clang/AST/Type.h"
  27. #include "clang/Basic/LLVM.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "clang/Sema/SemaFixItUtils.h"
  30. #include "clang/Sema/TemplateDeduction.h"
  31. #include "llvm/ADT/ArrayRef.h"
  32. #include "llvm/ADT/STLExtras.h"
  33. #include "llvm/ADT/SmallPtrSet.h"
  34. #include "llvm/ADT/SmallVector.h"
  35. #include "llvm/ADT/StringRef.h"
  36. #include "llvm/Support/AlignOf.h"
  37. #include "llvm/Support/Allocator.h"
  38. #include "llvm/Support/Casting.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include <cassert>
  41. #include <cstddef>
  42. #include <cstdint>
  43. #include <utility>
  44. namespace clang {
  45. class APValue;
  46. class ASTContext;
  47. class Sema;
  48. /// OverloadingResult - Capture the result of performing overload
  49. /// resolution.
  50. enum OverloadingResult {
  51. /// Overload resolution succeeded.
  52. OR_Success,
  53. /// No viable function found.
  54. OR_No_Viable_Function,
  55. /// Ambiguous candidates found.
  56. OR_Ambiguous,
  57. /// Succeeded, but refers to a deleted function.
  58. OR_Deleted
  59. };
  60. enum OverloadCandidateDisplayKind {
  61. /// Requests that all candidates be shown. Viable candidates will
  62. /// be printed first.
  63. OCD_AllCandidates,
  64. /// Requests that only viable candidates be shown.
  65. OCD_ViableCandidates,
  66. /// Requests that only tied-for-best candidates be shown.
  67. OCD_AmbiguousCandidates
  68. };
  69. /// The parameter ordering that will be used for the candidate. This is
  70. /// used to represent C++20 binary operator rewrites that reverse the order
  71. /// of the arguments. If the parameter ordering is Reversed, the Args list is
  72. /// reversed (but obviously the ParamDecls for the function are not).
  73. ///
  74. /// After forming an OverloadCandidate with reversed parameters, the list
  75. /// of conversions will (as always) be indexed by argument, so will be
  76. /// in reverse parameter order.
  77. enum class OverloadCandidateParamOrder : char { Normal, Reversed };
  78. /// The kinds of rewrite we perform on overload candidates. Note that the
  79. /// values here are chosen to serve as both bitflags and as a rank (lower
  80. /// values are preferred by overload resolution).
  81. enum OverloadCandidateRewriteKind : unsigned {
  82. /// Candidate is not a rewritten candidate.
  83. CRK_None = 0x0,
  84. /// Candidate is a rewritten candidate with a different operator name.
  85. CRK_DifferentOperator = 0x1,
  86. /// Candidate is a rewritten candidate with a reversed order of parameters.
  87. CRK_Reversed = 0x2,
  88. };
  89. /// ImplicitConversionKind - The kind of implicit conversion used to
  90. /// convert an argument to a parameter's type. The enumerator values
  91. /// match with the table titled 'Conversions' in [over.ics.scs] and are listed
  92. /// such that better conversion kinds have smaller values.
  93. enum ImplicitConversionKind {
  94. /// Identity conversion (no conversion)
  95. ICK_Identity = 0,
  96. /// Lvalue-to-rvalue conversion (C++ [conv.lval])
  97. ICK_Lvalue_To_Rvalue,
  98. /// Array-to-pointer conversion (C++ [conv.array])
  99. ICK_Array_To_Pointer,
  100. /// Function-to-pointer (C++ [conv.array])
  101. ICK_Function_To_Pointer,
  102. /// Function pointer conversion (C++17 [conv.fctptr])
  103. ICK_Function_Conversion,
  104. /// Qualification conversions (C++ [conv.qual])
  105. ICK_Qualification,
  106. /// Integral promotions (C++ [conv.prom])
  107. ICK_Integral_Promotion,
  108. /// Floating point promotions (C++ [conv.fpprom])
  109. ICK_Floating_Promotion,
  110. /// Complex promotions (Clang extension)
  111. ICK_Complex_Promotion,
  112. /// Integral conversions (C++ [conv.integral])
  113. ICK_Integral_Conversion,
  114. /// Floating point conversions (C++ [conv.double]
  115. ICK_Floating_Conversion,
  116. /// Complex conversions (C99 6.3.1.6)
  117. ICK_Complex_Conversion,
  118. /// Floating-integral conversions (C++ [conv.fpint])
  119. ICK_Floating_Integral,
  120. /// Pointer conversions (C++ [conv.ptr])
  121. ICK_Pointer_Conversion,
  122. /// Pointer-to-member conversions (C++ [conv.mem])
  123. ICK_Pointer_Member,
  124. /// Boolean conversions (C++ [conv.bool])
  125. ICK_Boolean_Conversion,
  126. /// Conversions between compatible types in C99
  127. ICK_Compatible_Conversion,
  128. /// Derived-to-base (C++ [over.best.ics])
  129. ICK_Derived_To_Base,
  130. /// Vector conversions
  131. ICK_Vector_Conversion,
  132. /// Arm SVE Vector conversions
  133. ICK_SVE_Vector_Conversion,
  134. /// A vector splat from an arithmetic type
  135. ICK_Vector_Splat,
  136. /// Complex-real conversions (C99 6.3.1.7)
  137. ICK_Complex_Real,
  138. /// Block Pointer conversions
  139. ICK_Block_Pointer_Conversion,
  140. /// Transparent Union Conversions
  141. ICK_TransparentUnionConversion,
  142. /// Objective-C ARC writeback conversion
  143. ICK_Writeback_Conversion,
  144. /// Zero constant to event (OpenCL1.2 6.12.10)
  145. ICK_Zero_Event_Conversion,
  146. /// Zero constant to queue
  147. ICK_Zero_Queue_Conversion,
  148. /// Conversions allowed in C, but not C++
  149. ICK_C_Only_Conversion,
  150. /// C-only conversion between pointers with incompatible types
  151. ICK_Incompatible_Pointer_Conversion,
  152. /// The number of conversion kinds
  153. ICK_Num_Conversion_Kinds,
  154. };
  155. /// ImplicitConversionRank - The rank of an implicit conversion
  156. /// kind. The enumerator values match with Table 9 of (C++
  157. /// 13.3.3.1.1) and are listed such that better conversion ranks
  158. /// have smaller values.
  159. enum ImplicitConversionRank {
  160. /// Exact Match
  161. ICR_Exact_Match = 0,
  162. /// Promotion
  163. ICR_Promotion,
  164. /// Conversion
  165. ICR_Conversion,
  166. /// OpenCL Scalar Widening
  167. ICR_OCL_Scalar_Widening,
  168. /// Complex <-> Real conversion
  169. ICR_Complex_Real_Conversion,
  170. /// ObjC ARC writeback conversion
  171. ICR_Writeback_Conversion,
  172. /// Conversion only allowed in the C standard (e.g. void* to char*).
  173. ICR_C_Conversion,
  174. /// Conversion not allowed by the C standard, but that we accept as an
  175. /// extension anyway.
  176. ICR_C_Conversion_Extension
  177. };
  178. ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
  179. /// NarrowingKind - The kind of narrowing conversion being performed by a
  180. /// standard conversion sequence according to C++11 [dcl.init.list]p7.
  181. enum NarrowingKind {
  182. /// Not a narrowing conversion.
  183. NK_Not_Narrowing,
  184. /// A narrowing conversion by virtue of the source and destination types.
  185. NK_Type_Narrowing,
  186. /// A narrowing conversion, because a constant expression got narrowed.
  187. NK_Constant_Narrowing,
  188. /// A narrowing conversion, because a non-constant-expression variable might
  189. /// have got narrowed.
  190. NK_Variable_Narrowing,
  191. /// Cannot tell whether this is a narrowing conversion because the
  192. /// expression is value-dependent.
  193. NK_Dependent_Narrowing,
  194. };
  195. /// StandardConversionSequence - represents a standard conversion
  196. /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
  197. /// contains between zero and three conversions. If a particular
  198. /// conversion is not needed, it will be set to the identity conversion
  199. /// (ICK_Identity). Note that the three conversions are
  200. /// specified as separate members (rather than in an array) so that
  201. /// we can keep the size of a standard conversion sequence to a
  202. /// single word.
  203. class StandardConversionSequence {
  204. public:
  205. /// First -- The first conversion can be an lvalue-to-rvalue
  206. /// conversion, array-to-pointer conversion, or
  207. /// function-to-pointer conversion.
  208. ImplicitConversionKind First : 8;
  209. /// Second - The second conversion can be an integral promotion,
  210. /// floating point promotion, integral conversion, floating point
  211. /// conversion, floating-integral conversion, pointer conversion,
  212. /// pointer-to-member conversion, or boolean conversion.
  213. ImplicitConversionKind Second : 8;
  214. /// Third - The third conversion can be a qualification conversion
  215. /// or a function conversion.
  216. ImplicitConversionKind Third : 8;
  217. /// Whether this is the deprecated conversion of a
  218. /// string literal to a pointer to non-const character data
  219. /// (C++ 4.2p2).
  220. unsigned DeprecatedStringLiteralToCharPtr : 1;
  221. /// Whether the qualification conversion involves a change in the
  222. /// Objective-C lifetime (for automatic reference counting).
  223. unsigned QualificationIncludesObjCLifetime : 1;
  224. /// IncompatibleObjC - Whether this is an Objective-C conversion
  225. /// that we should warn about (if we actually use it).
  226. unsigned IncompatibleObjC : 1;
  227. /// ReferenceBinding - True when this is a reference binding
  228. /// (C++ [over.ics.ref]).
  229. unsigned ReferenceBinding : 1;
  230. /// DirectBinding - True when this is a reference binding that is a
  231. /// direct binding (C++ [dcl.init.ref]).
  232. unsigned DirectBinding : 1;
  233. /// Whether this is an lvalue reference binding (otherwise, it's
  234. /// an rvalue reference binding).
  235. unsigned IsLvalueReference : 1;
  236. /// Whether we're binding to a function lvalue.
  237. unsigned BindsToFunctionLvalue : 1;
  238. /// Whether we're binding to an rvalue.
  239. unsigned BindsToRvalue : 1;
  240. /// Whether this binds an implicit object argument to a
  241. /// non-static member function without a ref-qualifier.
  242. unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
  243. /// Whether this binds a reference to an object with a different
  244. /// Objective-C lifetime qualifier.
  245. unsigned ObjCLifetimeConversionBinding : 1;
  246. /// FromType - The type that this conversion is converting
  247. /// from. This is an opaque pointer that can be translated into a
  248. /// QualType.
  249. void *FromTypePtr;
  250. /// ToType - The types that this conversion is converting to in
  251. /// each step. This is an opaque pointer that can be translated
  252. /// into a QualType.
  253. void *ToTypePtrs[3];
  254. /// CopyConstructor - The copy constructor that is used to perform
  255. /// this conversion, when the conversion is actually just the
  256. /// initialization of an object via copy constructor. Such
  257. /// conversions are either identity conversions or derived-to-base
  258. /// conversions.
  259. CXXConstructorDecl *CopyConstructor;
  260. DeclAccessPair FoundCopyConstructor;
  261. void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
  262. void setToType(unsigned Idx, QualType T) {
  263. assert(Idx < 3 && "To type index is out of range");
  264. ToTypePtrs[Idx] = T.getAsOpaquePtr();
  265. }
  266. void setAllToTypes(QualType T) {
  267. ToTypePtrs[0] = T.getAsOpaquePtr();
  268. ToTypePtrs[1] = ToTypePtrs[0];
  269. ToTypePtrs[2] = ToTypePtrs[0];
  270. }
  271. QualType getFromType() const {
  272. return QualType::getFromOpaquePtr(FromTypePtr);
  273. }
  274. QualType getToType(unsigned Idx) const {
  275. assert(Idx < 3 && "To type index is out of range");
  276. return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
  277. }
  278. void setAsIdentityConversion();
  279. bool isIdentityConversion() const {
  280. return Second == ICK_Identity && Third == ICK_Identity;
  281. }
  282. ImplicitConversionRank getRank() const;
  283. NarrowingKind
  284. getNarrowingKind(ASTContext &Context, const Expr *Converted,
  285. APValue &ConstantValue, QualType &ConstantType,
  286. bool IgnoreFloatToIntegralConversion = false) const;
  287. bool isPointerConversionToBool() const;
  288. bool isPointerConversionToVoidPointer(ASTContext& Context) const;
  289. void dump() const;
  290. };
  291. /// UserDefinedConversionSequence - Represents a user-defined
  292. /// conversion sequence (C++ 13.3.3.1.2).
  293. struct UserDefinedConversionSequence {
  294. /// Represents the standard conversion that occurs before
  295. /// the actual user-defined conversion.
  296. ///
  297. /// C++11 13.3.3.1.2p1:
  298. /// If the user-defined conversion is specified by a constructor
  299. /// (12.3.1), the initial standard conversion sequence converts
  300. /// the source type to the type required by the argument of the
  301. /// constructor. If the user-defined conversion is specified by
  302. /// a conversion function (12.3.2), the initial standard
  303. /// conversion sequence converts the source type to the implicit
  304. /// object parameter of the conversion function.
  305. StandardConversionSequence Before;
  306. /// EllipsisConversion - When this is true, it means user-defined
  307. /// conversion sequence starts with a ... (ellipsis) conversion, instead of
  308. /// a standard conversion. In this case, 'Before' field must be ignored.
  309. // FIXME. I much rather put this as the first field. But there seems to be
  310. // a gcc code gen. bug which causes a crash in a test. Putting it here seems
  311. // to work around the crash.
  312. bool EllipsisConversion : 1;
  313. /// HadMultipleCandidates - When this is true, it means that the
  314. /// conversion function was resolved from an overloaded set having
  315. /// size greater than 1.
  316. bool HadMultipleCandidates : 1;
  317. /// After - Represents the standard conversion that occurs after
  318. /// the actual user-defined conversion.
  319. StandardConversionSequence After;
  320. /// ConversionFunction - The function that will perform the
  321. /// user-defined conversion. Null if the conversion is an
  322. /// aggregate initialization from an initializer list.
  323. FunctionDecl* ConversionFunction;
  324. /// The declaration that we found via name lookup, which might be
  325. /// the same as \c ConversionFunction or it might be a using declaration
  326. /// that refers to \c ConversionFunction.
  327. DeclAccessPair FoundConversionFunction;
  328. void dump() const;
  329. };
  330. /// Represents an ambiguous user-defined conversion sequence.
  331. struct AmbiguousConversionSequence {
  332. using ConversionSet =
  333. SmallVector<std::pair<NamedDecl *, FunctionDecl *>, 4>;
  334. void *FromTypePtr;
  335. void *ToTypePtr;
  336. char Buffer[sizeof(ConversionSet)];
  337. QualType getFromType() const {
  338. return QualType::getFromOpaquePtr(FromTypePtr);
  339. }
  340. QualType getToType() const {
  341. return QualType::getFromOpaquePtr(ToTypePtr);
  342. }
  343. void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
  344. void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
  345. ConversionSet &conversions() {
  346. return *reinterpret_cast<ConversionSet*>(Buffer);
  347. }
  348. const ConversionSet &conversions() const {
  349. return *reinterpret_cast<const ConversionSet*>(Buffer);
  350. }
  351. void addConversion(NamedDecl *Found, FunctionDecl *D) {
  352. conversions().push_back(std::make_pair(Found, D));
  353. }
  354. using iterator = ConversionSet::iterator;
  355. iterator begin() { return conversions().begin(); }
  356. iterator end() { return conversions().end(); }
  357. using const_iterator = ConversionSet::const_iterator;
  358. const_iterator begin() const { return conversions().begin(); }
  359. const_iterator end() const { return conversions().end(); }
  360. void construct();
  361. void destruct();
  362. void copyFrom(const AmbiguousConversionSequence &);
  363. };
  364. /// BadConversionSequence - Records information about an invalid
  365. /// conversion sequence.
  366. struct BadConversionSequence {
  367. enum FailureKind {
  368. no_conversion,
  369. unrelated_class,
  370. bad_qualifiers,
  371. lvalue_ref_to_rvalue,
  372. rvalue_ref_to_lvalue,
  373. too_few_initializers,
  374. too_many_initializers,
  375. };
  376. // This can be null, e.g. for implicit object arguments.
  377. Expr *FromExpr;
  378. FailureKind Kind;
  379. private:
  380. // The type we're converting from (an opaque QualType).
  381. void *FromTy;
  382. // The type we're converting to (an opaque QualType).
  383. void *ToTy;
  384. public:
  385. void init(FailureKind K, Expr *From, QualType To) {
  386. init(K, From->getType(), To);
  387. FromExpr = From;
  388. }
  389. void init(FailureKind K, QualType From, QualType To) {
  390. Kind = K;
  391. FromExpr = nullptr;
  392. setFromType(From);
  393. setToType(To);
  394. }
  395. QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
  396. QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
  397. void setFromExpr(Expr *E) {
  398. FromExpr = E;
  399. setFromType(E->getType());
  400. }
  401. void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
  402. void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
  403. };
  404. /// ImplicitConversionSequence - Represents an implicit conversion
  405. /// sequence, which may be a standard conversion sequence
  406. /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
  407. /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
  408. class ImplicitConversionSequence {
  409. public:
  410. /// Kind - The kind of implicit conversion sequence. BadConversion
  411. /// specifies that there is no conversion from the source type to
  412. /// the target type. AmbiguousConversion represents the unique
  413. /// ambiguous conversion (C++0x [over.best.ics]p10).
  414. /// StaticObjectArgumentConversion represents the conversion rules for
  415. /// the synthesized first argument of calls to static member functions
  416. /// ([over.best.ics.general]p8).
  417. enum Kind {
  418. StandardConversion = 0,
  419. StaticObjectArgumentConversion,
  420. UserDefinedConversion,
  421. AmbiguousConversion,
  422. EllipsisConversion,
  423. BadConversion
  424. };
  425. private:
  426. enum {
  427. Uninitialized = BadConversion + 1
  428. };
  429. /// ConversionKind - The kind of implicit conversion sequence.
  430. unsigned ConversionKind : 31;
  431. // Whether the initializer list was of an incomplete array.
  432. unsigned InitializerListOfIncompleteArray : 1;
  433. /// When initializing an array or std::initializer_list from an
  434. /// initializer-list, this is the array or std::initializer_list type being
  435. /// initialized. The remainder of the conversion sequence, including ToType,
  436. /// describe the worst conversion of an initializer to an element of the
  437. /// array or std::initializer_list. (Note, 'worst' is not well defined.)
  438. QualType InitializerListContainerType;
  439. void setKind(Kind K) {
  440. destruct();
  441. ConversionKind = K;
  442. }
  443. void destruct() {
  444. if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
  445. }
  446. public:
  447. union {
  448. /// When ConversionKind == StandardConversion, provides the
  449. /// details of the standard conversion sequence.
  450. StandardConversionSequence Standard;
  451. /// When ConversionKind == UserDefinedConversion, provides the
  452. /// details of the user-defined conversion sequence.
  453. UserDefinedConversionSequence UserDefined;
  454. /// When ConversionKind == AmbiguousConversion, provides the
  455. /// details of the ambiguous conversion.
  456. AmbiguousConversionSequence Ambiguous;
  457. /// When ConversionKind == BadConversion, provides the details
  458. /// of the bad conversion.
  459. BadConversionSequence Bad;
  460. };
  461. ImplicitConversionSequence()
  462. : ConversionKind(Uninitialized),
  463. InitializerListOfIncompleteArray(false) {
  464. Standard.setAsIdentityConversion();
  465. }
  466. ImplicitConversionSequence(const ImplicitConversionSequence &Other)
  467. : ConversionKind(Other.ConversionKind),
  468. InitializerListOfIncompleteArray(
  469. Other.InitializerListOfIncompleteArray),
  470. InitializerListContainerType(Other.InitializerListContainerType) {
  471. switch (ConversionKind) {
  472. case Uninitialized: break;
  473. case StandardConversion: Standard = Other.Standard; break;
  474. case StaticObjectArgumentConversion:
  475. break;
  476. case UserDefinedConversion: UserDefined = Other.UserDefined; break;
  477. case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
  478. case EllipsisConversion: break;
  479. case BadConversion: Bad = Other.Bad; break;
  480. }
  481. }
  482. ImplicitConversionSequence &
  483. operator=(const ImplicitConversionSequence &Other) {
  484. destruct();
  485. new (this) ImplicitConversionSequence(Other);
  486. return *this;
  487. }
  488. ~ImplicitConversionSequence() {
  489. destruct();
  490. }
  491. Kind getKind() const {
  492. assert(isInitialized() && "querying uninitialized conversion");
  493. return Kind(ConversionKind);
  494. }
  495. /// Return a ranking of the implicit conversion sequence
  496. /// kind, where smaller ranks represent better conversion
  497. /// sequences.
  498. ///
  499. /// In particular, this routine gives user-defined conversion
  500. /// sequences and ambiguous conversion sequences the same rank,
  501. /// per C++ [over.best.ics]p10.
  502. unsigned getKindRank() const {
  503. switch (getKind()) {
  504. case StandardConversion:
  505. case StaticObjectArgumentConversion:
  506. return 0;
  507. case UserDefinedConversion:
  508. case AmbiguousConversion:
  509. return 1;
  510. case EllipsisConversion:
  511. return 2;
  512. case BadConversion:
  513. return 3;
  514. }
  515. llvm_unreachable("Invalid ImplicitConversionSequence::Kind!");
  516. }
  517. bool isBad() const { return getKind() == BadConversion; }
  518. bool isStandard() const { return getKind() == StandardConversion; }
  519. bool isStaticObjectArgument() const {
  520. return getKind() == StaticObjectArgumentConversion;
  521. }
  522. bool isEllipsis() const { return getKind() == EllipsisConversion; }
  523. bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
  524. bool isUserDefined() const { return getKind() == UserDefinedConversion; }
  525. bool isFailure() const { return isBad() || isAmbiguous(); }
  526. /// Determines whether this conversion sequence has been
  527. /// initialized. Most operations should never need to query
  528. /// uninitialized conversions and should assert as above.
  529. bool isInitialized() const { return ConversionKind != Uninitialized; }
  530. /// Sets this sequence as a bad conversion for an explicit argument.
  531. void setBad(BadConversionSequence::FailureKind Failure,
  532. Expr *FromExpr, QualType ToType) {
  533. setKind(BadConversion);
  534. Bad.init(Failure, FromExpr, ToType);
  535. }
  536. /// Sets this sequence as a bad conversion for an implicit argument.
  537. void setBad(BadConversionSequence::FailureKind Failure,
  538. QualType FromType, QualType ToType) {
  539. setKind(BadConversion);
  540. Bad.init(Failure, FromType, ToType);
  541. }
  542. void setStandard() { setKind(StandardConversion); }
  543. void setStaticObjectArgument() { setKind(StaticObjectArgumentConversion); }
  544. void setEllipsis() { setKind(EllipsisConversion); }
  545. void setUserDefined() { setKind(UserDefinedConversion); }
  546. void setAmbiguous() {
  547. if (ConversionKind == AmbiguousConversion) return;
  548. ConversionKind = AmbiguousConversion;
  549. Ambiguous.construct();
  550. }
  551. void setAsIdentityConversion(QualType T) {
  552. setStandard();
  553. Standard.setAsIdentityConversion();
  554. Standard.setFromType(T);
  555. Standard.setAllToTypes(T);
  556. }
  557. // True iff this is a conversion sequence from an initializer list to an
  558. // array or std::initializer.
  559. bool hasInitializerListContainerType() const {
  560. return !InitializerListContainerType.isNull();
  561. }
  562. void setInitializerListContainerType(QualType T, bool IA) {
  563. InitializerListContainerType = T;
  564. InitializerListOfIncompleteArray = IA;
  565. }
  566. bool isInitializerListOfIncompleteArray() const {
  567. return InitializerListOfIncompleteArray;
  568. }
  569. QualType getInitializerListContainerType() const {
  570. assert(hasInitializerListContainerType() &&
  571. "not initializer list container");
  572. return InitializerListContainerType;
  573. }
  574. /// Form an "implicit" conversion sequence from nullptr_t to bool, for a
  575. /// direct-initialization of a bool object from nullptr_t.
  576. static ImplicitConversionSequence getNullptrToBool(QualType SourceType,
  577. QualType DestType,
  578. bool NeedLValToRVal) {
  579. ImplicitConversionSequence ICS;
  580. ICS.setStandard();
  581. ICS.Standard.setAsIdentityConversion();
  582. ICS.Standard.setFromType(SourceType);
  583. if (NeedLValToRVal)
  584. ICS.Standard.First = ICK_Lvalue_To_Rvalue;
  585. ICS.Standard.setToType(0, SourceType);
  586. ICS.Standard.Second = ICK_Boolean_Conversion;
  587. ICS.Standard.setToType(1, DestType);
  588. ICS.Standard.setToType(2, DestType);
  589. return ICS;
  590. }
  591. // The result of a comparison between implicit conversion
  592. // sequences. Use Sema::CompareImplicitConversionSequences to
  593. // actually perform the comparison.
  594. enum CompareKind {
  595. Better = -1,
  596. Indistinguishable = 0,
  597. Worse = 1
  598. };
  599. void DiagnoseAmbiguousConversion(Sema &S,
  600. SourceLocation CaretLoc,
  601. const PartialDiagnostic &PDiag) const;
  602. void dump() const;
  603. };
  604. enum OverloadFailureKind {
  605. ovl_fail_too_many_arguments,
  606. ovl_fail_too_few_arguments,
  607. ovl_fail_bad_conversion,
  608. ovl_fail_bad_deduction,
  609. /// This conversion candidate was not considered because it
  610. /// duplicates the work of a trivial or derived-to-base
  611. /// conversion.
  612. ovl_fail_trivial_conversion,
  613. /// This conversion candidate was not considered because it is
  614. /// an illegal instantiation of a constructor temploid: it is
  615. /// callable with one argument, we only have one argument, and
  616. /// its first parameter type is exactly the type of the class.
  617. ///
  618. /// Defining such a constructor directly is illegal, and
  619. /// template-argument deduction is supposed to ignore such
  620. /// instantiations, but we can still get one with the right
  621. /// kind of implicit instantiation.
  622. ovl_fail_illegal_constructor,
  623. /// This conversion candidate is not viable because its result
  624. /// type is not implicitly convertible to the desired type.
  625. ovl_fail_bad_final_conversion,
  626. /// This conversion function template specialization candidate is not
  627. /// viable because the final conversion was not an exact match.
  628. ovl_fail_final_conversion_not_exact,
  629. /// (CUDA) This candidate was not viable because the callee
  630. /// was not accessible from the caller's target (i.e. host->device,
  631. /// global->host, device->host).
  632. ovl_fail_bad_target,
  633. /// This candidate function was not viable because an enable_if
  634. /// attribute disabled it.
  635. ovl_fail_enable_if,
  636. /// This candidate constructor or conversion function is explicit but
  637. /// the context doesn't permit explicit functions.
  638. ovl_fail_explicit,
  639. /// This candidate was not viable because its address could not be taken.
  640. ovl_fail_addr_not_available,
  641. /// This inherited constructor is not viable because it would slice the
  642. /// argument.
  643. ovl_fail_inhctor_slice,
  644. /// This candidate was not viable because it is a non-default multiversioned
  645. /// function.
  646. ovl_non_default_multiversion_function,
  647. /// This constructor/conversion candidate fail due to an address space
  648. /// mismatch between the object being constructed and the overload
  649. /// candidate.
  650. ovl_fail_object_addrspace_mismatch,
  651. /// This candidate was not viable because its associated constraints were
  652. /// not satisfied.
  653. ovl_fail_constraints_not_satisfied,
  654. /// This candidate was not viable because it has internal linkage and is
  655. /// from a different module unit than the use.
  656. ovl_fail_module_mismatched,
  657. };
  658. /// A list of implicit conversion sequences for the arguments of an
  659. /// OverloadCandidate.
  660. using ConversionSequenceList =
  661. llvm::MutableArrayRef<ImplicitConversionSequence>;
  662. /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
  663. struct OverloadCandidate {
  664. /// Function - The actual function that this candidate
  665. /// represents. When NULL, this is a built-in candidate
  666. /// (C++ [over.oper]) or a surrogate for a conversion to a
  667. /// function pointer or reference (C++ [over.call.object]).
  668. FunctionDecl *Function;
  669. /// FoundDecl - The original declaration that was looked up /
  670. /// invented / otherwise found, together with its access.
  671. /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
  672. DeclAccessPair FoundDecl;
  673. /// BuiltinParamTypes - Provides the parameter types of a built-in overload
  674. /// candidate. Only valid when Function is NULL.
  675. QualType BuiltinParamTypes[3];
  676. /// Surrogate - The conversion function for which this candidate
  677. /// is a surrogate, but only if IsSurrogate is true.
  678. CXXConversionDecl *Surrogate;
  679. /// The conversion sequences used to convert the function arguments
  680. /// to the function parameters. Note that these are indexed by argument,
  681. /// so may not match the parameter order of Function.
  682. ConversionSequenceList Conversions;
  683. /// The FixIt hints which can be used to fix the Bad candidate.
  684. ConversionFixItGenerator Fix;
  685. /// Viable - True to indicate that this overload candidate is viable.
  686. bool Viable : 1;
  687. /// Whether this candidate is the best viable function, or tied for being
  688. /// the best viable function.
  689. ///
  690. /// For an ambiguous overload resolution, indicates whether this candidate
  691. /// was part of the ambiguity kernel: the minimal non-empty set of viable
  692. /// candidates such that all elements of the ambiguity kernel are better
  693. /// than all viable candidates not in the ambiguity kernel.
  694. bool Best : 1;
  695. /// IsSurrogate - True to indicate that this candidate is a
  696. /// surrogate for a conversion to a function pointer or reference
  697. /// (C++ [over.call.object]).
  698. bool IsSurrogate : 1;
  699. /// IgnoreObjectArgument - True to indicate that the first
  700. /// argument's conversion, which for this function represents the
  701. /// implicit object argument, should be ignored. This will be true
  702. /// when the candidate is a static member function (where the
  703. /// implicit object argument is just a placeholder) or a
  704. /// non-static member function when the call doesn't have an
  705. /// object argument.
  706. bool IgnoreObjectArgument : 1;
  707. /// True if the candidate was found using ADL.
  708. CallExpr::ADLCallKind IsADLCandidate : 1;
  709. /// Whether this is a rewritten candidate, and if so, of what kind?
  710. unsigned RewriteKind : 2;
  711. /// FailureKind - The reason why this candidate is not viable.
  712. /// Actually an OverloadFailureKind.
  713. unsigned char FailureKind;
  714. /// The number of call arguments that were explicitly provided,
  715. /// to be used while performing partial ordering of function templates.
  716. unsigned ExplicitCallArguments;
  717. union {
  718. DeductionFailureInfo DeductionFailure;
  719. /// FinalConversion - For a conversion function (where Function is
  720. /// a CXXConversionDecl), the standard conversion that occurs
  721. /// after the call to the overload candidate to convert the result
  722. /// of calling the conversion function to the required type.
  723. StandardConversionSequence FinalConversion;
  724. };
  725. /// Get RewriteKind value in OverloadCandidateRewriteKind type (This
  726. /// function is to workaround the spurious GCC bitfield enum warning)
  727. OverloadCandidateRewriteKind getRewriteKind() const {
  728. return static_cast<OverloadCandidateRewriteKind>(RewriteKind);
  729. }
  730. bool isReversed() const { return getRewriteKind() & CRK_Reversed; }
  731. /// hasAmbiguousConversion - Returns whether this overload
  732. /// candidate requires an ambiguous conversion or not.
  733. bool hasAmbiguousConversion() const {
  734. for (auto &C : Conversions) {
  735. if (!C.isInitialized()) return false;
  736. if (C.isAmbiguous()) return true;
  737. }
  738. return false;
  739. }
  740. bool TryToFixBadConversion(unsigned Idx, Sema &S) {
  741. bool CanFix = Fix.tryToFixConversion(
  742. Conversions[Idx].Bad.FromExpr,
  743. Conversions[Idx].Bad.getFromType(),
  744. Conversions[Idx].Bad.getToType(), S);
  745. // If at least one conversion fails, the candidate cannot be fixed.
  746. if (!CanFix)
  747. Fix.clear();
  748. return CanFix;
  749. }
  750. unsigned getNumParams() const {
  751. if (IsSurrogate) {
  752. QualType STy = Surrogate->getConversionType();
  753. while (STy->isPointerType() || STy->isReferenceType())
  754. STy = STy->getPointeeType();
  755. return STy->castAs<FunctionProtoType>()->getNumParams();
  756. }
  757. if (Function)
  758. return Function->getNumParams();
  759. return ExplicitCallArguments;
  760. }
  761. bool NotValidBecauseConstraintExprHasError() const;
  762. private:
  763. friend class OverloadCandidateSet;
  764. OverloadCandidate()
  765. : IsSurrogate(false), IsADLCandidate(CallExpr::NotADL), RewriteKind(CRK_None) {}
  766. };
  767. /// OverloadCandidateSet - A set of overload candidates, used in C++
  768. /// overload resolution (C++ 13.3).
  769. class OverloadCandidateSet {
  770. public:
  771. enum CandidateSetKind {
  772. /// Normal lookup.
  773. CSK_Normal,
  774. /// C++ [over.match.oper]:
  775. /// Lookup of operator function candidates in a call using operator
  776. /// syntax. Candidates that have no parameters of class type will be
  777. /// skipped unless there is a parameter of (reference to) enum type and
  778. /// the corresponding argument is of the same enum type.
  779. CSK_Operator,
  780. /// C++ [over.match.copy]:
  781. /// Copy-initialization of an object of class type by user-defined
  782. /// conversion.
  783. CSK_InitByUserDefinedConversion,
  784. /// C++ [over.match.ctor], [over.match.list]
  785. /// Initialization of an object of class type by constructor,
  786. /// using either a parenthesized or braced list of arguments.
  787. CSK_InitByConstructor,
  788. };
  789. /// Information about operator rewrites to consider when adding operator
  790. /// functions to a candidate set.
  791. struct OperatorRewriteInfo {
  792. OperatorRewriteInfo()
  793. : OriginalOperator(OO_None), OpLoc(), AllowRewrittenCandidates(false) {}
  794. OperatorRewriteInfo(OverloadedOperatorKind Op, SourceLocation OpLoc,
  795. bool AllowRewritten)
  796. : OriginalOperator(Op), OpLoc(OpLoc),
  797. AllowRewrittenCandidates(AllowRewritten) {}
  798. /// The original operator as written in the source.
  799. OverloadedOperatorKind OriginalOperator;
  800. /// The source location of the operator.
  801. SourceLocation OpLoc;
  802. /// Whether we should include rewritten candidates in the overload set.
  803. bool AllowRewrittenCandidates;
  804. /// Would use of this function result in a rewrite using a different
  805. /// operator?
  806. bool isRewrittenOperator(const FunctionDecl *FD) {
  807. return OriginalOperator &&
  808. FD->getDeclName().getCXXOverloadedOperator() != OriginalOperator;
  809. }
  810. bool isAcceptableCandidate(const FunctionDecl *FD) {
  811. if (!OriginalOperator)
  812. return true;
  813. // For an overloaded operator, we can have candidates with a different
  814. // name in our unqualified lookup set. Make sure we only consider the
  815. // ones we're supposed to.
  816. OverloadedOperatorKind OO =
  817. FD->getDeclName().getCXXOverloadedOperator();
  818. return OO && (OO == OriginalOperator ||
  819. (AllowRewrittenCandidates &&
  820. OO == getRewrittenOverloadedOperator(OriginalOperator)));
  821. }
  822. /// Determine the kind of rewrite that should be performed for this
  823. /// candidate.
  824. OverloadCandidateRewriteKind
  825. getRewriteKind(const FunctionDecl *FD, OverloadCandidateParamOrder PO) {
  826. OverloadCandidateRewriteKind CRK = CRK_None;
  827. if (isRewrittenOperator(FD))
  828. CRK = OverloadCandidateRewriteKind(CRK | CRK_DifferentOperator);
  829. if (PO == OverloadCandidateParamOrder::Reversed)
  830. CRK = OverloadCandidateRewriteKind(CRK | CRK_Reversed);
  831. return CRK;
  832. }
  833. /// Determines whether this operator could be implemented by a function
  834. /// with reversed parameter order.
  835. bool isReversible() {
  836. return AllowRewrittenCandidates && OriginalOperator &&
  837. (getRewrittenOverloadedOperator(OriginalOperator) != OO_None ||
  838. allowsReversed(OriginalOperator));
  839. }
  840. /// Determine whether reversing parameter order is allowed for operator
  841. /// Op.
  842. bool allowsReversed(OverloadedOperatorKind Op);
  843. /// Determine whether we should add a rewritten candidate for \p FD with
  844. /// reversed parameter order.
  845. /// \param OriginalArgs are the original non reversed arguments.
  846. bool shouldAddReversed(Sema &S, ArrayRef<Expr *> OriginalArgs,
  847. FunctionDecl *FD);
  848. };
  849. private:
  850. SmallVector<OverloadCandidate, 16> Candidates;
  851. llvm::SmallPtrSet<uintptr_t, 16> Functions;
  852. // Allocator for ConversionSequenceLists. We store the first few of these
  853. // inline to avoid allocation for small sets.
  854. llvm::BumpPtrAllocator SlabAllocator;
  855. SourceLocation Loc;
  856. CandidateSetKind Kind;
  857. OperatorRewriteInfo RewriteInfo;
  858. constexpr static unsigned NumInlineBytes =
  859. 24 * sizeof(ImplicitConversionSequence);
  860. unsigned NumInlineBytesUsed = 0;
  861. alignas(void *) char InlineSpace[NumInlineBytes];
  862. // Address space of the object being constructed.
  863. LangAS DestAS = LangAS::Default;
  864. /// If we have space, allocates from inline storage. Otherwise, allocates
  865. /// from the slab allocator.
  866. /// FIXME: It would probably be nice to have a SmallBumpPtrAllocator
  867. /// instead.
  868. /// FIXME: Now that this only allocates ImplicitConversionSequences, do we
  869. /// want to un-generalize this?
  870. template <typename T>
  871. T *slabAllocate(unsigned N) {
  872. // It's simpler if this doesn't need to consider alignment.
  873. static_assert(alignof(T) == alignof(void *),
  874. "Only works for pointer-aligned types.");
  875. static_assert(std::is_trivial<T>::value ||
  876. std::is_same<ImplicitConversionSequence, T>::value,
  877. "Add destruction logic to OverloadCandidateSet::clear().");
  878. unsigned NBytes = sizeof(T) * N;
  879. if (NBytes > NumInlineBytes - NumInlineBytesUsed)
  880. return SlabAllocator.Allocate<T>(N);
  881. char *FreeSpaceStart = InlineSpace + NumInlineBytesUsed;
  882. assert(uintptr_t(FreeSpaceStart) % alignof(void *) == 0 &&
  883. "Misaligned storage!");
  884. NumInlineBytesUsed += NBytes;
  885. return reinterpret_cast<T *>(FreeSpaceStart);
  886. }
  887. void destroyCandidates();
  888. public:
  889. OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK,
  890. OperatorRewriteInfo RewriteInfo = {})
  891. : Loc(Loc), Kind(CSK), RewriteInfo(RewriteInfo) {}
  892. OverloadCandidateSet(const OverloadCandidateSet &) = delete;
  893. OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete;
  894. ~OverloadCandidateSet() { destroyCandidates(); }
  895. SourceLocation getLocation() const { return Loc; }
  896. CandidateSetKind getKind() const { return Kind; }
  897. OperatorRewriteInfo getRewriteInfo() const { return RewriteInfo; }
  898. /// Whether diagnostics should be deferred.
  899. bool shouldDeferDiags(Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc);
  900. /// Determine when this overload candidate will be new to the
  901. /// overload set.
  902. bool isNewCandidate(Decl *F, OverloadCandidateParamOrder PO =
  903. OverloadCandidateParamOrder::Normal) {
  904. uintptr_t Key = reinterpret_cast<uintptr_t>(F->getCanonicalDecl());
  905. Key |= static_cast<uintptr_t>(PO);
  906. return Functions.insert(Key).second;
  907. }
  908. /// Exclude a function from being considered by overload resolution.
  909. void exclude(Decl *F) {
  910. isNewCandidate(F, OverloadCandidateParamOrder::Normal);
  911. isNewCandidate(F, OverloadCandidateParamOrder::Reversed);
  912. }
  913. /// Clear out all of the candidates.
  914. void clear(CandidateSetKind CSK);
  915. using iterator = SmallVectorImpl<OverloadCandidate>::iterator;
  916. iterator begin() { return Candidates.begin(); }
  917. iterator end() { return Candidates.end(); }
  918. size_t size() const { return Candidates.size(); }
  919. bool empty() const { return Candidates.empty(); }
  920. /// Allocate storage for conversion sequences for NumConversions
  921. /// conversions.
  922. ConversionSequenceList
  923. allocateConversionSequences(unsigned NumConversions) {
  924. ImplicitConversionSequence *Conversions =
  925. slabAllocate<ImplicitConversionSequence>(NumConversions);
  926. // Construct the new objects.
  927. for (unsigned I = 0; I != NumConversions; ++I)
  928. new (&Conversions[I]) ImplicitConversionSequence();
  929. return ConversionSequenceList(Conversions, NumConversions);
  930. }
  931. /// Add a new candidate with NumConversions conversion sequence slots
  932. /// to the overload set.
  933. OverloadCandidate &
  934. addCandidate(unsigned NumConversions = 0,
  935. ConversionSequenceList Conversions = std::nullopt) {
  936. assert((Conversions.empty() || Conversions.size() == NumConversions) &&
  937. "preallocated conversion sequence has wrong length");
  938. Candidates.push_back(OverloadCandidate());
  939. OverloadCandidate &C = Candidates.back();
  940. C.Conversions = Conversions.empty()
  941. ? allocateConversionSequences(NumConversions)
  942. : Conversions;
  943. return C;
  944. }
  945. /// Find the best viable function on this overload set, if it exists.
  946. OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
  947. OverloadCandidateSet::iterator& Best);
  948. SmallVector<OverloadCandidate *, 32> CompleteCandidates(
  949. Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args,
  950. SourceLocation OpLoc = SourceLocation(),
  951. llvm::function_ref<bool(OverloadCandidate &)> Filter =
  952. [](OverloadCandidate &) { return true; });
  953. void NoteCandidates(
  954. PartialDiagnosticAt PA, Sema &S, OverloadCandidateDisplayKind OCD,
  955. ArrayRef<Expr *> Args, StringRef Opc = "",
  956. SourceLocation Loc = SourceLocation(),
  957. llvm::function_ref<bool(OverloadCandidate &)> Filter =
  958. [](OverloadCandidate &) { return true; });
  959. void NoteCandidates(Sema &S, ArrayRef<Expr *> Args,
  960. ArrayRef<OverloadCandidate *> Cands,
  961. StringRef Opc = "",
  962. SourceLocation OpLoc = SourceLocation());
  963. LangAS getDestAS() { return DestAS; }
  964. void setDestAS(LangAS AS) {
  965. assert((Kind == CSK_InitByConstructor ||
  966. Kind == CSK_InitByUserDefinedConversion) &&
  967. "can't set the destination address space when not constructing an "
  968. "object");
  969. DestAS = AS;
  970. }
  971. };
  972. bool isBetterOverloadCandidate(Sema &S,
  973. const OverloadCandidate &Cand1,
  974. const OverloadCandidate &Cand2,
  975. SourceLocation Loc,
  976. OverloadCandidateSet::CandidateSetKind Kind);
  977. struct ConstructorInfo {
  978. DeclAccessPair FoundDecl;
  979. CXXConstructorDecl *Constructor;
  980. FunctionTemplateDecl *ConstructorTmpl;
  981. explicit operator bool() const { return Constructor; }
  982. };
  983. // FIXME: Add an AddOverloadCandidate / AddTemplateOverloadCandidate overload
  984. // that takes one of these.
  985. inline ConstructorInfo getConstructorInfo(NamedDecl *ND) {
  986. if (isa<UsingDecl>(ND))
  987. return ConstructorInfo{};
  988. // For constructors, the access check is performed against the underlying
  989. // declaration, not the found declaration.
  990. auto *D = ND->getUnderlyingDecl();
  991. ConstructorInfo Info = {DeclAccessPair::make(ND, D->getAccess()), nullptr,
  992. nullptr};
  993. Info.ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
  994. if (Info.ConstructorTmpl)
  995. D = Info.ConstructorTmpl->getTemplatedDecl();
  996. Info.Constructor = dyn_cast<CXXConstructorDecl>(D);
  997. return Info;
  998. }
  999. // Returns false if signature help is relevant despite number of arguments
  1000. // exceeding parameters. Specifically, it returns false when
  1001. // PartialOverloading is true and one of the following:
  1002. // * Function is variadic
  1003. // * Function is template variadic
  1004. // * Function is an instantiation of template variadic function
  1005. // The last case may seem strange. The idea is that if we added one more
  1006. // argument, we'd end up with a function similar to Function. Since, in the
  1007. // context of signature help and/or code completion, we do not know what the
  1008. // type of the next argument (that the user is typing) will be, this is as
  1009. // good candidate as we can get, despite the fact that it takes one less
  1010. // parameter.
  1011. bool shouldEnforceArgLimit(bool PartialOverloading, FunctionDecl *Function);
  1012. } // namespace clang
  1013. #endif // LLVM_CLANG_SEMA_OVERLOAD_H
  1014. #ifdef __GNUC__
  1015. #pragma GCC diagnostic pop
  1016. #endif