TemplateDeduction.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- TemplateDeduction.h - C++ template argument deduction ----*- 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 provides types used with Sema's template argument deduction
  15. // routines.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
  19. #define LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
  20. #include "clang/Sema/Ownership.h"
  21. #include "clang/Sema/SemaConcept.h"
  22. #include "clang/AST/ASTConcept.h"
  23. #include "clang/AST/DeclAccessPair.h"
  24. #include "clang/AST/DeclTemplate.h"
  25. #include "clang/AST/TemplateBase.h"
  26. #include "clang/Basic/PartialDiagnostic.h"
  27. #include "clang/Basic/SourceLocation.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include <cassert>
  30. #include <cstddef>
  31. #include <optional>
  32. #include <utility>
  33. namespace clang {
  34. class Decl;
  35. struct DeducedPack;
  36. class Sema;
  37. namespace sema {
  38. /// Provides information about an attempted template argument
  39. /// deduction, whose success or failure was described by a
  40. /// TemplateDeductionResult value.
  41. class TemplateDeductionInfo {
  42. /// The deduced template argument list.
  43. TemplateArgumentList *DeducedSugared = nullptr, *DeducedCanonical = nullptr;
  44. /// The source location at which template argument
  45. /// deduction is occurring.
  46. SourceLocation Loc;
  47. /// Have we suppressed an error during deduction?
  48. bool HasSFINAEDiagnostic = false;
  49. /// The template parameter depth for which we're performing deduction.
  50. unsigned DeducedDepth;
  51. /// The number of parameters with explicitly-specified template arguments,
  52. /// up to and including the partially-specified pack (if any).
  53. unsigned ExplicitArgs = 0;
  54. /// Warnings (and follow-on notes) that were suppressed due to
  55. /// SFINAE while performing template argument deduction.
  56. SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics;
  57. public:
  58. TemplateDeductionInfo(SourceLocation Loc, unsigned DeducedDepth = 0)
  59. : Loc(Loc), DeducedDepth(DeducedDepth) {}
  60. TemplateDeductionInfo(const TemplateDeductionInfo &) = delete;
  61. TemplateDeductionInfo &operator=(const TemplateDeductionInfo &) = delete;
  62. enum ForBaseTag { ForBase };
  63. /// Create temporary template deduction info for speculatively deducing
  64. /// against a base class of an argument's type.
  65. TemplateDeductionInfo(ForBaseTag, const TemplateDeductionInfo &Info)
  66. : DeducedSugared(Info.DeducedSugared), Loc(Info.Loc),
  67. DeducedDepth(Info.DeducedDepth), ExplicitArgs(Info.ExplicitArgs) {}
  68. /// Returns the location at which template argument is
  69. /// occurring.
  70. SourceLocation getLocation() const {
  71. return Loc;
  72. }
  73. /// The depth of template parameters for which deduction is being
  74. /// performed.
  75. unsigned getDeducedDepth() const {
  76. return DeducedDepth;
  77. }
  78. /// Get the number of explicitly-specified arguments.
  79. unsigned getNumExplicitArgs() const {
  80. return ExplicitArgs;
  81. }
  82. /// Take ownership of the deduced template argument lists.
  83. TemplateArgumentList *takeSugared() {
  84. TemplateArgumentList *Result = DeducedSugared;
  85. DeducedSugared = nullptr;
  86. return Result;
  87. }
  88. TemplateArgumentList *takeCanonical() {
  89. TemplateArgumentList *Result = DeducedCanonical;
  90. DeducedCanonical = nullptr;
  91. return Result;
  92. }
  93. /// Take ownership of the SFINAE diagnostic.
  94. void takeSFINAEDiagnostic(PartialDiagnosticAt &PD) {
  95. assert(HasSFINAEDiagnostic);
  96. PD.first = SuppressedDiagnostics.front().first;
  97. PD.second.swap(SuppressedDiagnostics.front().second);
  98. clearSFINAEDiagnostic();
  99. }
  100. /// Discard any SFINAE diagnostics.
  101. void clearSFINAEDiagnostic() {
  102. SuppressedDiagnostics.clear();
  103. HasSFINAEDiagnostic = false;
  104. }
  105. /// Peek at the SFINAE diagnostic.
  106. const PartialDiagnosticAt &peekSFINAEDiagnostic() const {
  107. assert(HasSFINAEDiagnostic);
  108. return SuppressedDiagnostics.front();
  109. }
  110. /// Provide an initial template argument list that contains the
  111. /// explicitly-specified arguments.
  112. void setExplicitArgs(TemplateArgumentList *NewDeducedSugared,
  113. TemplateArgumentList *NewDeducedCanonical) {
  114. assert(NewDeducedSugared->size() == NewDeducedCanonical->size());
  115. DeducedSugared = NewDeducedSugared;
  116. DeducedCanonical = NewDeducedCanonical;
  117. ExplicitArgs = DeducedSugared->size();
  118. }
  119. /// Provide a new template argument list that contains the
  120. /// results of template argument deduction.
  121. void reset(TemplateArgumentList *NewDeducedSugared,
  122. TemplateArgumentList *NewDeducedCanonical) {
  123. DeducedSugared = NewDeducedSugared;
  124. DeducedCanonical = NewDeducedCanonical;
  125. }
  126. /// Is a SFINAE diagnostic available?
  127. bool hasSFINAEDiagnostic() const {
  128. return HasSFINAEDiagnostic;
  129. }
  130. /// Set the diagnostic which caused the SFINAE failure.
  131. void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD) {
  132. // Only collect the first diagnostic.
  133. if (HasSFINAEDiagnostic)
  134. return;
  135. SuppressedDiagnostics.clear();
  136. SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
  137. HasSFINAEDiagnostic = true;
  138. }
  139. /// Add a new diagnostic to the set of diagnostics
  140. void addSuppressedDiagnostic(SourceLocation Loc,
  141. PartialDiagnostic PD) {
  142. if (HasSFINAEDiagnostic)
  143. return;
  144. SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
  145. }
  146. /// Iterator over the set of suppressed diagnostics.
  147. using diag_iterator = SmallVectorImpl<PartialDiagnosticAt>::const_iterator;
  148. /// Returns an iterator at the beginning of the sequence of suppressed
  149. /// diagnostics.
  150. diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
  151. /// Returns an iterator at the end of the sequence of suppressed
  152. /// diagnostics.
  153. diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
  154. /// The template parameter to which a template argument
  155. /// deduction failure refers.
  156. ///
  157. /// Depending on the result of template argument deduction, this
  158. /// template parameter may have different meanings:
  159. ///
  160. /// TDK_Incomplete: this is the first template parameter whose
  161. /// corresponding template argument was not deduced.
  162. ///
  163. /// TDK_IncompletePack: this is the expanded parameter pack for
  164. /// which we deduced too few arguments.
  165. ///
  166. /// TDK_Inconsistent: this is the template parameter for which
  167. /// two different template argument values were deduced.
  168. TemplateParameter Param;
  169. /// The first template argument to which the template
  170. /// argument deduction failure refers.
  171. ///
  172. /// Depending on the result of the template argument deduction,
  173. /// this template argument may have different meanings:
  174. ///
  175. /// TDK_IncompletePack: this is the number of arguments we deduced
  176. /// for the pack.
  177. ///
  178. /// TDK_Inconsistent: this argument is the first value deduced
  179. /// for the corresponding template parameter.
  180. ///
  181. /// TDK_SubstitutionFailure: this argument is the template
  182. /// argument we were instantiating when we encountered an error.
  183. ///
  184. /// TDK_DeducedMismatch: this is the parameter type, after substituting
  185. /// deduced arguments.
  186. ///
  187. /// TDK_NonDeducedMismatch: this is the component of the 'parameter'
  188. /// of the deduction, directly provided in the source code.
  189. TemplateArgument FirstArg;
  190. /// The second template argument to which the template
  191. /// argument deduction failure refers.
  192. ///
  193. /// TDK_Inconsistent: this argument is the second value deduced
  194. /// for the corresponding template parameter.
  195. ///
  196. /// TDK_DeducedMismatch: this is the (adjusted) call argument type.
  197. ///
  198. /// TDK_NonDeducedMismatch: this is the mismatching component of the
  199. /// 'argument' of the deduction, from which we are deducing arguments.
  200. ///
  201. /// FIXME: Finish documenting this.
  202. TemplateArgument SecondArg;
  203. /// The index of the function argument that caused a deduction
  204. /// failure.
  205. ///
  206. /// TDK_DeducedMismatch: this is the index of the argument that had a
  207. /// different argument type from its substituted parameter type.
  208. unsigned CallArgIndex = 0;
  209. /// Information on packs that we're currently expanding.
  210. ///
  211. /// FIXME: This should be kept internal to SemaTemplateDeduction.
  212. SmallVector<DeducedPack *, 8> PendingDeducedPacks;
  213. /// \brief The constraint satisfaction details resulting from the associated
  214. /// constraints satisfaction tests.
  215. ConstraintSatisfaction AssociatedConstraintsSatisfaction;
  216. };
  217. } // namespace sema
  218. /// A structure used to record information about a failed
  219. /// template argument deduction, for diagnosis.
  220. struct DeductionFailureInfo {
  221. /// A Sema::TemplateDeductionResult.
  222. unsigned Result : 8;
  223. /// Indicates whether a diagnostic is stored in Diagnostic.
  224. unsigned HasDiagnostic : 1;
  225. /// Opaque pointer containing additional data about
  226. /// this deduction failure.
  227. void *Data;
  228. /// A diagnostic indicating why deduction failed.
  229. alignas(PartialDiagnosticAt) char Diagnostic[sizeof(PartialDiagnosticAt)];
  230. /// Retrieve the diagnostic which caused this deduction failure,
  231. /// if any.
  232. PartialDiagnosticAt *getSFINAEDiagnostic();
  233. /// Retrieve the template parameter this deduction failure
  234. /// refers to, if any.
  235. TemplateParameter getTemplateParameter();
  236. /// Retrieve the template argument list associated with this
  237. /// deduction failure, if any.
  238. TemplateArgumentList *getTemplateArgumentList();
  239. /// Return the first template argument this deduction failure
  240. /// refers to, if any.
  241. const TemplateArgument *getFirstArg();
  242. /// Return the second template argument this deduction failure
  243. /// refers to, if any.
  244. const TemplateArgument *getSecondArg();
  245. /// Return the index of the call argument that this deduction
  246. /// failure refers to, if any.
  247. std::optional<unsigned> getCallArgIndex();
  248. /// Free any memory associated with this deduction failure.
  249. void Destroy();
  250. };
  251. /// TemplateSpecCandidate - This is a generalization of OverloadCandidate
  252. /// which keeps track of template argument deduction failure info, when
  253. /// handling explicit specializations (and instantiations) of templates
  254. /// beyond function overloading.
  255. /// For now, assume that the candidates are non-matching specializations.
  256. /// TODO: In the future, we may need to unify/generalize this with
  257. /// OverloadCandidate.
  258. struct TemplateSpecCandidate {
  259. /// The declaration that was looked up, together with its access.
  260. /// Might be a UsingShadowDecl, but usually a FunctionTemplateDecl.
  261. DeclAccessPair FoundDecl;
  262. /// Specialization - The actual specialization that this candidate
  263. /// represents. When NULL, this may be a built-in candidate.
  264. Decl *Specialization;
  265. /// Template argument deduction info
  266. DeductionFailureInfo DeductionFailure;
  267. void set(DeclAccessPair Found, Decl *Spec, DeductionFailureInfo Info) {
  268. FoundDecl = Found;
  269. Specialization = Spec;
  270. DeductionFailure = Info;
  271. }
  272. /// Diagnose a template argument deduction failure.
  273. void NoteDeductionFailure(Sema &S, bool ForTakingAddress);
  274. };
  275. /// TemplateSpecCandidateSet - A set of generalized overload candidates,
  276. /// used in template specializations.
  277. /// TODO: In the future, we may need to unify/generalize this with
  278. /// OverloadCandidateSet.
  279. class TemplateSpecCandidateSet {
  280. SmallVector<TemplateSpecCandidate, 16> Candidates;
  281. SourceLocation Loc;
  282. // Stores whether we're taking the address of these candidates. This helps us
  283. // produce better error messages when dealing with the pass_object_size
  284. // attribute on parameters.
  285. bool ForTakingAddress;
  286. void destroyCandidates();
  287. public:
  288. TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress = false)
  289. : Loc(Loc), ForTakingAddress(ForTakingAddress) {}
  290. TemplateSpecCandidateSet(const TemplateSpecCandidateSet &) = delete;
  291. TemplateSpecCandidateSet &
  292. operator=(const TemplateSpecCandidateSet &) = delete;
  293. ~TemplateSpecCandidateSet() { destroyCandidates(); }
  294. SourceLocation getLocation() const { return Loc; }
  295. /// Clear out all of the candidates.
  296. /// TODO: This may be unnecessary.
  297. void clear();
  298. using iterator = SmallVector<TemplateSpecCandidate, 16>::iterator;
  299. iterator begin() { return Candidates.begin(); }
  300. iterator end() { return Candidates.end(); }
  301. size_t size() const { return Candidates.size(); }
  302. bool empty() const { return Candidates.empty(); }
  303. /// Add a new candidate with NumConversions conversion sequence slots
  304. /// to the overload set.
  305. TemplateSpecCandidate &addCandidate() {
  306. Candidates.emplace_back();
  307. return Candidates.back();
  308. }
  309. void NoteCandidates(Sema &S, SourceLocation Loc);
  310. void NoteCandidates(Sema &S, SourceLocation Loc) const {
  311. const_cast<TemplateSpecCandidateSet *>(this)->NoteCandidates(S, Loc);
  312. }
  313. };
  314. } // namespace clang
  315. #endif // LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
  316. #ifdef __GNUC__
  317. #pragma GCC diagnostic pop
  318. #endif