clang  14.0.0git
TemplateDeduction.h
Go to the documentation of this file.
1 //===- TemplateDeduction.h - C++ template argument deduction ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides types used with Sema's template argument deduction
10 // routines.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
15 #define LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
16 
17 #include "clang/Sema/Ownership.h"
18 #include "clang/Sema/SemaConcept.h"
19 #include "clang/AST/ASTConcept.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/TemplateBase.h"
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include <cassert>
28 #include <cstddef>
29 #include <utility>
30 
31 namespace clang {
32 
33 class Decl;
34 struct DeducedPack;
35 class Sema;
36 
37 namespace sema {
38 
39 /// Provides information about an attempted template argument
40 /// deduction, whose success or failure was described by a
41 /// TemplateDeductionResult value.
43  /// The deduced template argument list.
44  TemplateArgumentList *Deduced = nullptr;
45 
46  /// The source location at which template argument
47  /// deduction is occurring.
48  SourceLocation Loc;
49 
50  /// Have we suppressed an error during deduction?
51  bool HasSFINAEDiagnostic = false;
52 
53  /// The template parameter depth for which we're performing deduction.
54  unsigned DeducedDepth;
55 
56  /// The number of parameters with explicitly-specified template arguments,
57  /// up to and including the partially-specified pack (if any).
58  unsigned ExplicitArgs = 0;
59 
60  /// Warnings (and follow-on notes) that were suppressed due to
61  /// SFINAE while performing template argument deduction.
62  SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics;
63 
64 public:
65  TemplateDeductionInfo(SourceLocation Loc, unsigned DeducedDepth = 0)
66  : Loc(Loc), DeducedDepth(DeducedDepth) {}
69 
70  enum ForBaseTag { ForBase };
71  /// Create temporary template deduction info for speculatively deducing
72  /// against a base class of an argument's type.
74  : Deduced(Info.Deduced), Loc(Info.Loc), DeducedDepth(Info.DeducedDepth),
75  ExplicitArgs(Info.ExplicitArgs) {}
76 
77  /// Returns the location at which template argument is
78  /// occurring.
80  return Loc;
81  }
82 
83  /// The depth of template parameters for which deduction is being
84  /// performed.
85  unsigned getDeducedDepth() const {
86  return DeducedDepth;
87  }
88 
89  /// Get the number of explicitly-specified arguments.
90  unsigned getNumExplicitArgs() const {
91  return ExplicitArgs;
92  }
93 
94  /// Take ownership of the deduced template argument list.
96  TemplateArgumentList *Result = Deduced;
97  Deduced = nullptr;
98  return Result;
99  }
100 
101  /// Take ownership of the SFINAE diagnostic.
103  assert(HasSFINAEDiagnostic);
104  PD.first = SuppressedDiagnostics.front().first;
105  PD.second.swap(SuppressedDiagnostics.front().second);
107  }
108 
109  /// Discard any SFINAE diagnostics.
111  SuppressedDiagnostics.clear();
112  HasSFINAEDiagnostic = false;
113  }
114 
115  /// Peek at the SFINAE diagnostic.
117  assert(HasSFINAEDiagnostic);
118  return SuppressedDiagnostics.front();
119  }
120 
121  /// Provide an initial template argument list that contains the
122  /// explicitly-specified arguments.
124  Deduced = NewDeduced;
125  ExplicitArgs = Deduced->size();
126  }
127 
128  /// Provide a new template argument list that contains the
129  /// results of template argument deduction.
130  void reset(TemplateArgumentList *NewDeduced) {
131  Deduced = NewDeduced;
132  }
133 
134  /// Is a SFINAE diagnostic available?
135  bool hasSFINAEDiagnostic() const {
136  return HasSFINAEDiagnostic;
137  }
138 
139  /// Set the diagnostic which caused the SFINAE failure.
141  // Only collect the first diagnostic.
142  if (HasSFINAEDiagnostic)
143  return;
144  SuppressedDiagnostics.clear();
145  SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
146  HasSFINAEDiagnostic = true;
147  }
148 
149  /// Add a new diagnostic to the set of diagnostics
151  PartialDiagnostic PD) {
152  if (HasSFINAEDiagnostic)
153  return;
154  SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
155  }
156 
157  /// Iterator over the set of suppressed diagnostics.
159 
160  /// Returns an iterator at the beginning of the sequence of suppressed
161  /// diagnostics.
162  diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
163 
164  /// Returns an iterator at the end of the sequence of suppressed
165  /// diagnostics.
166  diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
167 
168  /// The template parameter to which a template argument
169  /// deduction failure refers.
170  ///
171  /// Depending on the result of template argument deduction, this
172  /// template parameter may have different meanings:
173  ///
174  /// TDK_Incomplete: this is the first template parameter whose
175  /// corresponding template argument was not deduced.
176  ///
177  /// TDK_IncompletePack: this is the expanded parameter pack for
178  /// which we deduced too few arguments.
179  ///
180  /// TDK_Inconsistent: this is the template parameter for which
181  /// two different template argument values were deduced.
183 
184  /// The first template argument to which the template
185  /// argument deduction failure refers.
186  ///
187  /// Depending on the result of the template argument deduction,
188  /// this template argument may have different meanings:
189  ///
190  /// TDK_IncompletePack: this is the number of arguments we deduced
191  /// for the pack.
192  ///
193  /// TDK_Inconsistent: this argument is the first value deduced
194  /// for the corresponding template parameter.
195  ///
196  /// TDK_SubstitutionFailure: this argument is the template
197  /// argument we were instantiating when we encountered an error.
198  ///
199  /// TDK_DeducedMismatch: this is the parameter type, after substituting
200  /// deduced arguments.
201  ///
202  /// TDK_NonDeducedMismatch: this is the component of the 'parameter'
203  /// of the deduction, directly provided in the source code.
205 
206  /// The second template argument to which the template
207  /// argument deduction failure refers.
208  ///
209  /// TDK_Inconsistent: this argument is the second value deduced
210  /// for the corresponding template parameter.
211  ///
212  /// TDK_DeducedMismatch: this is the (adjusted) call argument type.
213  ///
214  /// TDK_NonDeducedMismatch: this is the mismatching component of the
215  /// 'argument' of the deduction, from which we are deducing arguments.
216  ///
217  /// FIXME: Finish documenting this.
219 
220  /// The index of the function argument that caused a deduction
221  /// failure.
222  ///
223  /// TDK_DeducedMismatch: this is the index of the argument that had a
224  /// different argument type from its substituted parameter type.
225  unsigned CallArgIndex = 0;
226 
227  /// Information on packs that we're currently expanding.
228  ///
229  /// FIXME: This should be kept internal to SemaTemplateDeduction.
231 
232  /// \brief The constraint satisfaction details resulting from the associated
233  /// constraints satisfaction tests.
235 };
236 
237 } // namespace sema
238 
239 /// A structure used to record information about a failed
240 /// template argument deduction, for diagnosis.
242  /// A Sema::TemplateDeductionResult.
243  unsigned Result : 8;
244 
245  /// Indicates whether a diagnostic is stored in Diagnostic.
246  unsigned HasDiagnostic : 1;
247 
248  /// Opaque pointer containing additional data about
249  /// this deduction failure.
250  void *Data;
251 
252  /// A diagnostic indicating why deduction failed.
254 
255  /// Retrieve the diagnostic which caused this deduction failure,
256  /// if any.
258 
259  /// Retrieve the template parameter this deduction failure
260  /// refers to, if any.
262 
263  /// Retrieve the template argument list associated with this
264  /// deduction failure, if any.
266 
267  /// Return the first template argument this deduction failure
268  /// refers to, if any.
269  const TemplateArgument *getFirstArg();
270 
271  /// Return the second template argument this deduction failure
272  /// refers to, if any.
274 
275  /// Return the index of the call argument that this deduction
276  /// failure refers to, if any.
278 
279  /// Free any memory associated with this deduction failure.
280  void Destroy();
281 };
282 
283 /// TemplateSpecCandidate - This is a generalization of OverloadCandidate
284 /// which keeps track of template argument deduction failure info, when
285 /// handling explicit specializations (and instantiations) of templates
286 /// beyond function overloading.
287 /// For now, assume that the candidates are non-matching specializations.
288 /// TODO: In the future, we may need to unify/generalize this with
289 /// OverloadCandidate.
291  /// The declaration that was looked up, together with its access.
292  /// Might be a UsingShadowDecl, but usually a FunctionTemplateDecl.
294 
295  /// Specialization - The actual specialization that this candidate
296  /// represents. When NULL, this may be a built-in candidate.
298 
299  /// Template argument deduction info
301 
302  void set(DeclAccessPair Found, Decl *Spec, DeductionFailureInfo Info) {
303  FoundDecl = Found;
304  Specialization = Spec;
305  DeductionFailure = Info;
306  }
307 
308  /// Diagnose a template argument deduction failure.
309  void NoteDeductionFailure(Sema &S, bool ForTakingAddress);
310 };
311 
312 /// TemplateSpecCandidateSet - A set of generalized overload candidates,
313 /// used in template specializations.
314 /// TODO: In the future, we may need to unify/generalize this with
315 /// OverloadCandidateSet.
318  SourceLocation Loc;
319 
320  // Stores whether we're taking the address of these candidates. This helps us
321  // produce better error messages when dealing with the pass_object_size
322  // attribute on parameters.
323  bool ForTakingAddress;
324 
325  void destroyCandidates();
326 
327 public:
328  TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress = false)
329  : Loc(Loc), ForTakingAddress(ForTakingAddress) {}
332  operator=(const TemplateSpecCandidateSet &) = delete;
333  ~TemplateSpecCandidateSet() { destroyCandidates(); }
334 
335  SourceLocation getLocation() const { return Loc; }
336 
337  /// Clear out all of the candidates.
338  /// TODO: This may be unnecessary.
339  void clear();
340 
342 
343  iterator begin() { return Candidates.begin(); }
344  iterator end() { return Candidates.end(); }
345 
346  size_t size() const { return Candidates.size(); }
347  bool empty() const { return Candidates.empty(); }
348 
349  /// Add a new candidate with NumConversions conversion sequence slots
350  /// to the overload set.
352  Candidates.emplace_back();
353  return Candidates.back();
354  }
355 
356  void NoteCandidates(Sema &S, SourceLocation Loc);
357 
358  void NoteCandidates(Sema &S, SourceLocation Loc) const {
359  const_cast<TemplateSpecCandidateSet *>(this)->NoteCandidates(S, Loc);
360  }
361 };
362 
363 } // namespace clang
364 
365 #endif // LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
clang::sema::TemplateDeductionInfo::take
TemplateArgumentList * take()
Take ownership of the deduced template argument list.
Definition: TemplateDeduction.h:95
clang::sema::TemplateDeductionInfo::CallArgIndex
unsigned CallArgIndex
The index of the function argument that caused a deduction failure.
Definition: TemplateDeduction.h:225
clang::TemplateSpecCandidate::Specialization
Decl * Specialization
Specialization - The actual specialization that this candidate represents.
Definition: TemplateDeduction.h:297
clang::TemplateSpecCandidateSet::NoteCandidates
void NoteCandidates(Sema &S, SourceLocation Loc)
NoteCandidates - When no template specialization match is found, prints diagnostic messages containin...
Definition: SemaOverload.cpp:11851
clang::TemplateSpecCandidateSet::operator=
TemplateSpecCandidateSet & operator=(const TemplateSpecCandidateSet &)=delete
DeclAccessPair.h
clang::sema::TemplateDeductionInfo::ForBaseTag
ForBaseTag
Definition: TemplateDeduction.h:70
llvm::SmallVector< PartialDiagnosticAt, 4 >
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::TemplateSpecCandidateSet::getLocation
SourceLocation getLocation() const
Definition: TemplateDeduction.h:335
Ownership.h
clang::TemplateSpecCandidate
TemplateSpecCandidate - This is a generalization of OverloadCandidate which keeps track of template a...
Definition: TemplateDeduction.h:290
llvm::Optional< unsigned >
clang::sema::TemplateDeductionInfo::diag_begin
diag_iterator diag_begin() const
Returns an iterator at the beginning of the sequence of suppressed diagnostics.
Definition: TemplateDeduction.h:162
clang::TemplateSpecCandidateSet::NoteCandidates
void NoteCandidates(Sema &S, SourceLocation Loc) const
Definition: TemplateDeduction.h:358
clang::sema::TemplateDeductionInfo::ForBase
@ ForBase
Definition: TemplateDeduction.h:70
clang::DeductionFailureInfo::Result
unsigned Result
A Sema::TemplateDeductionResult.
Definition: TemplateDeduction.h:243
clang::sema::TemplateDeductionInfo::FirstArg
TemplateArgument FirstArg
The first template argument to which the template argument deduction failure refers.
Definition: TemplateDeduction.h:204
clang::TemplateSpecCandidateSet::size
size_t size() const
Definition: TemplateDeduction.h:346
clang::sema::TemplateDeductionInfo::TemplateDeductionInfo
TemplateDeductionInfo(ForBaseTag, const TemplateDeductionInfo &Info)
Create temporary template deduction info for speculatively deducing against a base class of an argume...
Definition: TemplateDeduction.h:73
clang::sema::TemplateDeductionInfo::peekSFINAEDiagnostic
const PartialDiagnosticAt & peekSFINAEDiagnostic() const
Peek at the SFINAE diagnostic.
Definition: TemplateDeduction.h:116
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1547
clang::CodeGen::AlignmentSource::Decl
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
TemplateBase.h
clang::DeductionFailureInfo::Destroy
void Destroy()
Free any memory associated with this deduction failure.
Definition: SemaOverload.cpp:694
DeclTemplate.h
clang::PartialDiagnosticAt
std::pair< SourceLocation, PartialDiagnostic > PartialDiagnosticAt
A partial diagnostic along with the source location where this diagnostic occurs.
Definition: PartialDiagnostic.h:208
clang::sema::TemplateDeductionInfo::addSuppressedDiagnostic
void addSuppressedDiagnostic(SourceLocation Loc, PartialDiagnostic PD)
Add a new diagnostic to the set of diagnostics.
Definition: TemplateDeduction.h:150
clang::TemplateSpecCandidateSet::TemplateSpecCandidateSet
TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress=false)
Definition: TemplateDeduction.h:328
clang::DeclAccessPair
A POD class for pairing a NamedDecl* with an access specifier.
Definition: DeclAccessPair.h:29
clang::DeductionFailureInfo
A structure used to record information about a failed template argument deduction,...
Definition: TemplateDeduction.h:241
clang::sema::TemplateDeductionInfo::Param
TemplateParameter Param
The template parameter to which a template argument deduction failure refers.
Definition: TemplateDeduction.h:182
clang::TemplateSpecCandidateSet::end
iterator end()
Definition: TemplateDeduction.h:344
clang::TemplateSpecCandidate::set
void set(DeclAccessPair Found, Decl *Spec, DeductionFailureInfo Info)
Definition: TemplateDeduction.h:302
clang::TemplateArgument
Represents a template argument.
Definition: TemplateBase.h:62
clang::TemplateSpecCandidateSet::iterator
SmallVector< TemplateSpecCandidate, 16 >::iterator iterator
Definition: TemplateDeduction.h:341
clang::TemplateSpecCandidateSet
TemplateSpecCandidateSet - A set of generalized overload candidates, used in template specializations...
Definition: TemplateDeduction.h:316
clang::sema::TemplateDeductionInfo::getDeducedDepth
unsigned getDeducedDepth() const
The depth of template parameters for which deduction is being performed.
Definition: TemplateDeduction.h:85
clang::TemplateSpecCandidateSet::empty
bool empty() const
Definition: TemplateDeduction.h:347
clang::sema::TemplateDeductionInfo::diag_end
diag_iterator diag_end() const
Returns an iterator at the end of the sequence of suppressed diagnostics.
Definition: TemplateDeduction.h:166
clang::DeductionFailureInfo::HasDiagnostic
unsigned HasDiagnostic
Indicates whether a diagnostic is stored in Diagnostic.
Definition: TemplateDeduction.h:246
ASTConcept.h
This file provides AST data structures related to concepts.
clang::DeductionFailureInfo::Data
void * Data
Opaque pointer containing additional data about this deduction failure.
Definition: TemplateDeduction.h:250
clang::TemplateSpecCandidateSet::begin
iterator begin()
Definition: TemplateDeduction.h:343
clang::DeductionFailureInfo::getFirstArg
const TemplateArgument * getFirstArg()
Return the first template argument this deduction failure refers to, if any.
Definition: SemaOverload.cpp:815
clang::sema::TemplateDeductionInfo::addSFINAEDiagnostic
void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD)
Set the diagnostic which caused the SFINAE failure.
Definition: TemplateDeduction.h:140
clang::sema::TemplateDeductionInfo::setExplicitArgs
void setExplicitArgs(TemplateArgumentList *NewDeduced)
Provide an initial template argument list that contains the explicitly-specified arguments.
Definition: TemplateDeduction.h:123
clang::TemplateSpecCandidateSet::clear
void clear()
Clear out all of the candidates.
Definition: SemaOverload.cpp:11841
clang::TemplateSpecCandidate::DeductionFailure
DeductionFailureInfo DeductionFailure
Template argument deduction info.
Definition: TemplateDeduction.h:300
clang::TemplateSpecCandidate::FoundDecl
DeclAccessPair FoundDecl
The declaration that was looked up, together with its access.
Definition: TemplateDeduction.h:293
clang::DeductionFailureInfo::getSecondArg
const TemplateArgument * getSecondArg()
Return the second template argument this deduction failure refers to, if any.
Definition: SemaOverload.cpp:846
clang::TemplateSpecCandidate::NoteDeductionFailure
void NoteDeductionFailure(Sema &S, bool ForTakingAddress)
Diagnose a template argument deduction failure.
Definition: SemaOverload.cpp:11829
SourceLocation.h
clang::sema::TemplateDeductionInfo::hasSFINAEDiagnostic
bool hasSFINAEDiagnostic() const
Is a SFINAE diagnostic available?
Definition: TemplateDeduction.h:135
clang::sema::TemplateDeductionInfo::AssociatedConstraintsSatisfaction
ConstraintSatisfaction AssociatedConstraintsSatisfaction
The constraint satisfaction details resulting from the associated constraints satisfaction tests.
Definition: TemplateDeduction.h:234
clang::sema::TemplateDeductionInfo::diag_iterator
SmallVectorImpl< PartialDiagnosticAt >::const_iterator diag_iterator
Iterator over the set of suppressed diagnostics.
Definition: TemplateDeduction.h:158
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:355
clang::DeductionFailureInfo::getCallArgIndex
llvm::Optional< unsigned > getCallArgIndex()
Return the index of the call argument that this deduction failure refers to, if any.
Definition: SemaOverload.cpp:877
clang::sema::TemplateDeductionInfo::getLocation
SourceLocation getLocation() const
Returns the location at which template argument is occurring.
Definition: TemplateDeduction.h:79
clang::sema::TemplateDeductionInfo::SecondArg
TemplateArgument SecondArg
The second template argument to which the template argument deduction failure refers.
Definition: TemplateDeduction.h:218
clang::TemplateArgumentList
A template argument list.
Definition: DeclTemplate.h:236
clang::sema::TemplateDeductionInfo
Provides information about an attempted template argument deduction, whose success or failure was des...
Definition: TemplateDeduction.h:42
clang::PartialDiagnostic
Definition: PartialDiagnostic.h:34
clang::sema::TemplateDeductionInfo::reset
void reset(TemplateArgumentList *NewDeduced)
Provide a new template argument list that contains the results of template argument deduction.
Definition: TemplateDeduction.h:130
clang::sema::TemplateDeductionInfo::PendingDeducedPacks
SmallVector< DeducedPack *, 8 > PendingDeducedPacks
Information on packs that we're currently expanding.
Definition: TemplateDeduction.h:230
PartialDiagnostic.h
clang
Definition: CalledOnceCheck.h:17
clang::sema::TemplateDeductionInfo::takeSFINAEDiagnostic
void takeSFINAEDiagnostic(PartialDiagnosticAt &PD)
Take ownership of the SFINAE diagnostic.
Definition: TemplateDeduction.h:102
clang::TemplateSpecCandidateSet::addCandidate
TemplateSpecCandidate & addCandidate()
Add a new candidate with NumConversions conversion sequence slots to the overload set.
Definition: TemplateDeduction.h:351
clang::sema::TemplateDeductionInfo::TemplateDeductionInfo
TemplateDeductionInfo(SourceLocation Loc, unsigned DeducedDepth=0)
Definition: TemplateDeduction.h:65
clang::ConstraintSatisfaction
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:29
clang::DeductionFailureInfo::getTemplateParameter
TemplateParameter getTemplateParameter()
Retrieve the template parameter this deduction failure refers to, if any.
Definition: SemaOverload.cpp:747
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::DeductionFailureInfo::getTemplateArgumentList
TemplateArgumentList * getTemplateArgumentList()
Retrieve the template argument list associated with this deduction failure, if any.
Definition: SemaOverload.cpp:780
clang::TemplateArgumentList::size
unsigned size() const
Retrieve the number of template arguments in this template argument list.
Definition: DeclTemplate.h:296
clang::TemplateParameter
llvm::PointerUnion< TemplateTypeParmDecl *, NonTypeTemplateParmDecl *, TemplateTemplateParmDecl * > TemplateParameter
Stores a template parameter of any kind.
Definition: DeclTemplate.h:63
SemaConcept.h
clang::TemplateSpecCandidateSet::~TemplateSpecCandidateSet
~TemplateSpecCandidateSet()
Definition: TemplateDeduction.h:333
clang::sema::TemplateDeductionInfo::getNumExplicitArgs
unsigned getNumExplicitArgs() const
Get the number of explicitly-specified arguments.
Definition: TemplateDeduction.h:90
clang::DeductionFailureInfo::getSFINAEDiagnostic
PartialDiagnosticAt * getSFINAEDiagnostic()
Retrieve the diagnostic which caused this deduction failure, if any.
Definition: SemaOverload.cpp:741
clang::sema::TemplateDeductionInfo::clearSFINAEDiagnostic
void clearSFINAEDiagnostic()
Discard any SFINAE diagnostics.
Definition: TemplateDeduction.h:110
clang::sema::TemplateDeductionInfo::operator=
TemplateDeductionInfo & operator=(const TemplateDeductionInfo &)=delete