1 //===- TemplateDeduction.h - C++ template argument deduction ----*- C++ -*-===/
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===/
9 // This file provides types used with Sema's template argument deduction
12 //===----------------------------------------------------------------------===/
13 #ifndef LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
14 #define LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/Basic/PartialDiagnostic.h"
18 #include "llvm/ADT/SmallVector.h"
23 class TemplateArgumentList;
28 /// \brief Provides information about an attempted template argument
29 /// deduction, whose success or failure was described by a
30 /// TemplateDeductionResult value.
31 class TemplateDeductionInfo {
32 /// \brief The deduced template argument list.
34 TemplateArgumentList *Deduced;
36 /// \brief The source location at which template argument
37 /// deduction is occurring.
40 /// \brief Have we suppressed an error during deduction?
41 bool HasSFINAEDiagnostic;
43 /// \brief Warnings (and follow-on notes) that were suppressed due to
44 /// SFINAE while performing template argument deduction.
45 SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics;
47 TemplateDeductionInfo(const TemplateDeductionInfo &) = delete;
48 void operator=(const TemplateDeductionInfo &) = delete;
51 TemplateDeductionInfo(SourceLocation Loc)
52 : Deduced(nullptr), Loc(Loc), HasSFINAEDiagnostic(false),
53 Expression(nullptr) {}
55 /// \brief Returns the location at which template argument is
57 SourceLocation getLocation() const {
61 /// \brief Take ownership of the deduced template argument list.
62 TemplateArgumentList *take() {
63 TemplateArgumentList *Result = Deduced;
68 /// \brief Take ownership of the SFINAE diagnostic.
69 void takeSFINAEDiagnostic(PartialDiagnosticAt &PD) {
70 assert(HasSFINAEDiagnostic);
71 PD.first = SuppressedDiagnostics.front().first;
72 PD.second.swap(SuppressedDiagnostics.front().second);
73 SuppressedDiagnostics.clear();
74 HasSFINAEDiagnostic = false;
77 /// \brief Provide a new template argument list that contains the
78 /// results of template argument deduction.
79 void reset(TemplateArgumentList *NewDeduced) {
83 /// \brief Is a SFINAE diagnostic available?
84 bool hasSFINAEDiagnostic() const {
85 return HasSFINAEDiagnostic;
88 /// \brief Set the diagnostic which caused the SFINAE failure.
89 void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD) {
90 // Only collect the first diagnostic.
91 if (HasSFINAEDiagnostic)
93 SuppressedDiagnostics.clear();
94 SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
95 HasSFINAEDiagnostic = true;
98 /// \brief Add a new diagnostic to the set of diagnostics
99 void addSuppressedDiagnostic(SourceLocation Loc,
100 PartialDiagnostic PD) {
101 if (HasSFINAEDiagnostic)
103 SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
106 /// \brief Iterator over the set of suppressed diagnostics.
107 typedef SmallVectorImpl<PartialDiagnosticAt>::const_iterator
110 /// \brief Returns an iterator at the beginning of the sequence of suppressed
112 diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
114 /// \brief Returns an iterator at the end of the sequence of suppressed
116 diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
118 /// \brief The template parameter to which a template argument
119 /// deduction failure refers.
121 /// Depending on the result of template argument deduction, this
122 /// template parameter may have different meanings:
124 /// TDK_Incomplete: this is the first template parameter whose
125 /// corresponding template argument was not deduced.
127 /// TDK_Inconsistent: this is the template parameter for which
128 /// two different template argument values were deduced.
129 TemplateParameter Param;
131 /// \brief The first template argument to which the template
132 /// argument deduction failure refers.
134 /// Depending on the result of the template argument deduction,
135 /// this template argument may have different meanings:
137 /// TDK_Inconsistent: this argument is the first value deduced
138 /// for the corresponding template parameter.
140 /// TDK_SubstitutionFailure: this argument is the template
141 /// argument we were instantiating when we encountered an error.
143 /// TDK_DeducedMismatch: this is the parameter type, after substituting
144 /// deduced arguments.
146 /// TDK_NonDeducedMismatch: this is the component of the 'parameter'
147 /// of the deduction, directly provided in the source code.
148 TemplateArgument FirstArg;
150 /// \brief The second template argument to which the template
151 /// argument deduction failure refers.
153 /// TDK_Inconsistent: this argument is the second value deduced
154 /// for the corresponding template parameter.
156 /// TDK_DeducedMismatch: this is the (adjusted) call argument type.
158 /// TDK_NonDeducedMismatch: this is the mismatching component of the
159 /// 'argument' of the deduction, from which we are deducing arguments.
161 /// FIXME: Finish documenting this.
162 TemplateArgument SecondArg;
165 /// \brief The expression which caused a deduction failure.
167 /// TDK_FailedOverloadResolution: this argument is the reference to
168 /// an overloaded function which could not be resolved to a specific
172 /// \brief The index of the function argument that caused a deduction
175 /// TDK_DeducedMismatch: this is the index of the argument that had a
176 /// different argument type from its substituted parameter type.
177 unsigned CallArgIndex;
180 /// \brief Information on packs that we're currently expanding.
182 /// FIXME: This should be kept internal to SemaTemplateDeduction.
183 SmallVector<DeducedPack *, 8> PendingDeducedPacks;
186 } // end namespace sema
188 /// A structure used to record information about a failed
189 /// template argument deduction, for diagnosis.
190 struct DeductionFailureInfo {
191 /// A Sema::TemplateDeductionResult.
194 /// \brief Indicates whether a diagnostic is stored in Diagnostic.
195 unsigned HasDiagnostic : 1;
197 /// \brief Opaque pointer containing additional data about
198 /// this deduction failure.
201 /// \brief A diagnostic indicating why deduction failed.
204 char Diagnostic[sizeof(PartialDiagnosticAt)];
207 /// \brief Retrieve the diagnostic which caused this deduction failure,
209 PartialDiagnosticAt *getSFINAEDiagnostic();
211 /// \brief Retrieve the template parameter this deduction failure
212 /// refers to, if any.
213 TemplateParameter getTemplateParameter();
215 /// \brief Retrieve the template argument list associated with this
216 /// deduction failure, if any.
217 TemplateArgumentList *getTemplateArgumentList();
219 /// \brief Return the first template argument this deduction failure
220 /// refers to, if any.
221 const TemplateArgument *getFirstArg();
223 /// \brief Return the second template argument this deduction failure
224 /// refers to, if any.
225 const TemplateArgument *getSecondArg();
227 /// \brief Return the expression this deduction failure refers to,
231 /// \brief Return the index of the call argument that this deduction
232 /// failure refers to, if any.
233 llvm::Optional<unsigned> getCallArgIndex();
235 /// \brief Free any memory associated with this deduction failure.
239 /// TemplateSpecCandidate - This is a generalization of OverloadCandidate
240 /// which keeps track of template argument deduction failure info, when
241 /// handling explicit specializations (and instantiations) of templates
242 /// beyond function overloading.
243 /// For now, assume that the candidates are non-matching specializations.
244 /// TODO: In the future, we may need to unify/generalize this with
245 /// OverloadCandidate.
246 struct TemplateSpecCandidate {
247 /// Specialization - The actual specialization that this candidate
248 /// represents. When NULL, this may be a built-in candidate.
249 Decl *Specialization;
251 /// Template argument deduction info
252 DeductionFailureInfo DeductionFailure;
254 void set(Decl *Spec, DeductionFailureInfo Info) {
255 Specialization = Spec;
256 DeductionFailure = Info;
259 /// Diagnose a template argument deduction failure.
260 void NoteDeductionFailure(Sema &S, bool ForTakingAddress);
263 /// TemplateSpecCandidateSet - A set of generalized overload candidates,
264 /// used in template specializations.
265 /// TODO: In the future, we may need to unify/generalize this with
266 /// OverloadCandidateSet.
267 class TemplateSpecCandidateSet {
268 SmallVector<TemplateSpecCandidate, 16> Candidates;
270 // Stores whether we're taking the address of these candidates. This helps us
271 // produce better error messages when dealing with the pass_object_size
272 // attribute on parameters.
273 bool ForTakingAddress;
275 TemplateSpecCandidateSet(
276 const TemplateSpecCandidateSet &) = delete;
277 void operator=(const TemplateSpecCandidateSet &) = delete;
279 void destroyCandidates();
282 TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress = false)
283 : Loc(Loc), ForTakingAddress(ForTakingAddress) {}
284 ~TemplateSpecCandidateSet() { destroyCandidates(); }
286 SourceLocation getLocation() const { return Loc; }
288 /// \brief Clear out all of the candidates.
289 /// TODO: This may be unnecessary.
292 typedef SmallVector<TemplateSpecCandidate, 16>::iterator iterator;
293 iterator begin() { return Candidates.begin(); }
294 iterator end() { return Candidates.end(); }
296 size_t size() const { return Candidates.size(); }
297 bool empty() const { return Candidates.empty(); }
299 /// \brief Add a new candidate with NumConversions conversion sequence slots
300 /// to the overload set.
301 TemplateSpecCandidate &addCandidate() {
302 Candidates.emplace_back();
303 return Candidates.back();
306 void NoteCandidates(Sema &S, SourceLocation Loc);
308 void NoteCandidates(Sema &S, SourceLocation Loc) const {
309 const_cast<TemplateSpecCandidateSet *>(this)->NoteCandidates(S, Loc);
313 } // end namespace clang