clang  7.0.0svn
TemplateDeduction.h
Go to the documentation of this file.
1 //===- TemplateDeduction.h - C++ template argument deduction ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides types used with Sema's template argument deduction
11 // routines.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
16 #define LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
17 
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/TemplateBase.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include <cassert>
26 #include <cstddef>
27 #include <utility>
28 
29 namespace clang {
30 
31 class Decl;
32 struct DeducedPack;
33 class Sema;
34 
35 namespace sema {
36 
37 /// Provides information about an attempted template argument
38 /// deduction, whose success or failure was described by a
39 /// TemplateDeductionResult value.
41  /// The deduced template argument list.
42  TemplateArgumentList *Deduced = nullptr;
43 
44  /// The source location at which template argument
45  /// deduction is occurring.
46  SourceLocation Loc;
47 
48  /// Have we suppressed an error during deduction?
49  bool HasSFINAEDiagnostic = false;
50 
51  /// The template parameter depth for which we're performing deduction.
52  unsigned DeducedDepth;
53 
54  /// Warnings (and follow-on notes) that were suppressed due to
55  /// SFINAE while performing template argument deduction.
56  SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics;
57 
58 public:
59  TemplateDeductionInfo(SourceLocation Loc, unsigned DeducedDepth = 0)
60  : Loc(Loc), DeducedDepth(DeducedDepth) {}
63 
64  /// Returns the location at which template argument is
65  /// occurring.
67  return Loc;
68  }
69 
70  /// The depth of template parameters for which deduction is being
71  /// performed.
72  unsigned getDeducedDepth() const {
73  return DeducedDepth;
74  }
75 
76  /// Take ownership of the deduced template argument list.
78  TemplateArgumentList *Result = Deduced;
79  Deduced = nullptr;
80  return Result;
81  }
82 
83  /// Take ownership of the SFINAE diagnostic.
85  assert(HasSFINAEDiagnostic);
86  PD.first = SuppressedDiagnostics.front().first;
87  PD.second.swap(SuppressedDiagnostics.front().second);
89  }
90 
91  /// Discard any SFINAE diagnostics.
93  SuppressedDiagnostics.clear();
94  HasSFINAEDiagnostic = false;
95  }
96 
97  /// Peek at the SFINAE diagnostic.
99  assert(HasSFINAEDiagnostic);
100  return SuppressedDiagnostics.front();
101  }
102 
103  /// Provide a new template argument list that contains the
104  /// results of template argument deduction.
105  void reset(TemplateArgumentList *NewDeduced) {
106  Deduced = NewDeduced;
107  }
108 
109  /// Is a SFINAE diagnostic available?
110  bool hasSFINAEDiagnostic() const {
111  return HasSFINAEDiagnostic;
112  }
113 
114  /// Set the diagnostic which caused the SFINAE failure.
116  // Only collect the first diagnostic.
117  if (HasSFINAEDiagnostic)
118  return;
119  SuppressedDiagnostics.clear();
120  SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
121  HasSFINAEDiagnostic = true;
122  }
123 
124  /// Add a new diagnostic to the set of diagnostics
126  PartialDiagnostic PD) {
127  if (HasSFINAEDiagnostic)
128  return;
129  SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
130  }
131 
132  /// Iterator over the set of suppressed diagnostics.
134 
135  /// Returns an iterator at the beginning of the sequence of suppressed
136  /// diagnostics.
137  diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
138 
139  /// Returns an iterator at the end of the sequence of suppressed
140  /// diagnostics.
141  diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
142 
143  /// The template parameter to which a template argument
144  /// deduction failure refers.
145  ///
146  /// Depending on the result of template argument deduction, this
147  /// template parameter may have different meanings:
148  ///
149  /// TDK_Incomplete: this is the first template parameter whose
150  /// corresponding template argument was not deduced.
151  ///
152  /// TDK_Inconsistent: this is the template parameter for which
153  /// two different template argument values were deduced.
155 
156  /// The first template argument to which the template
157  /// argument deduction failure refers.
158  ///
159  /// Depending on the result of the template argument deduction,
160  /// this template argument may have different meanings:
161  ///
162  /// TDK_Inconsistent: this argument is the first value deduced
163  /// for the corresponding template parameter.
164  ///
165  /// TDK_SubstitutionFailure: this argument is the template
166  /// argument we were instantiating when we encountered an error.
167  ///
168  /// TDK_DeducedMismatch: this is the parameter type, after substituting
169  /// deduced arguments.
170  ///
171  /// TDK_NonDeducedMismatch: this is the component of the 'parameter'
172  /// of the deduction, directly provided in the source code.
174 
175  /// The second template argument to which the template
176  /// argument deduction failure refers.
177  ///
178  /// TDK_Inconsistent: this argument is the second value deduced
179  /// for the corresponding template parameter.
180  ///
181  /// TDK_DeducedMismatch: this is the (adjusted) call argument type.
182  ///
183  /// TDK_NonDeducedMismatch: this is the mismatching component of the
184  /// 'argument' of the deduction, from which we are deducing arguments.
185  ///
186  /// FIXME: Finish documenting this.
188 
189  /// The index of the function argument that caused a deduction
190  /// failure.
191  ///
192  /// TDK_DeducedMismatch: this is the index of the argument that had a
193  /// different argument type from its substituted parameter type.
194  unsigned CallArgIndex = 0;
195 
196  /// Information on packs that we're currently expanding.
197  ///
198  /// FIXME: This should be kept internal to SemaTemplateDeduction.
200 };
201 
202 } // namespace sema
203 
204 /// A structure used to record information about a failed
205 /// template argument deduction, for diagnosis.
207  /// A Sema::TemplateDeductionResult.
208  unsigned Result : 8;
209 
210  /// Indicates whether a diagnostic is stored in Diagnostic.
211  unsigned HasDiagnostic : 1;
212 
213  /// Opaque pointer containing additional data about
214  /// this deduction failure.
215  void *Data;
216 
217  /// A diagnostic indicating why deduction failed.
219 
220  /// Retrieve the diagnostic which caused this deduction failure,
221  /// if any.
222  PartialDiagnosticAt *getSFINAEDiagnostic();
223 
224  /// Retrieve the template parameter this deduction failure
225  /// refers to, if any.
226  TemplateParameter getTemplateParameter();
227 
228  /// Retrieve the template argument list associated with this
229  /// deduction failure, if any.
230  TemplateArgumentList *getTemplateArgumentList();
231 
232  /// Return the first template argument this deduction failure
233  /// refers to, if any.
234  const TemplateArgument *getFirstArg();
235 
236  /// Return the second template argument this deduction failure
237  /// refers to, if any.
238  const TemplateArgument *getSecondArg();
239 
240  /// Return the index of the call argument that this deduction
241  /// failure refers to, if any.
242  llvm::Optional<unsigned> getCallArgIndex();
243 
244  /// Free any memory associated with this deduction failure.
245  void Destroy();
246 };
247 
248 /// TemplateSpecCandidate - This is a generalization of OverloadCandidate
249 /// which keeps track of template argument deduction failure info, when
250 /// handling explicit specializations (and instantiations) of templates
251 /// beyond function overloading.
252 /// For now, assume that the candidates are non-matching specializations.
253 /// TODO: In the future, we may need to unify/generalize this with
254 /// OverloadCandidate.
256  /// The declaration that was looked up, together with its access.
257  /// Might be a UsingShadowDecl, but usually a FunctionTemplateDecl.
259 
260  /// Specialization - The actual specialization that this candidate
261  /// represents. When NULL, this may be a built-in candidate.
263 
264  /// Template argument deduction info
266 
267  void set(DeclAccessPair Found, Decl *Spec, DeductionFailureInfo Info) {
268  FoundDecl = Found;
269  Specialization = Spec;
270  DeductionFailure = Info;
271  }
272 
273  /// Diagnose a template argument deduction failure.
274  void NoteDeductionFailure(Sema &S, bool ForTakingAddress);
275 };
276 
277 /// TemplateSpecCandidateSet - A set of generalized overload candidates,
278 /// used in template specializations.
279 /// TODO: In the future, we may need to unify/generalize this with
280 /// OverloadCandidateSet.
283  SourceLocation Loc;
284 
285  // Stores whether we're taking the address of these candidates. This helps us
286  // produce better error messages when dealing with the pass_object_size
287  // attribute on parameters.
288  bool ForTakingAddress;
289 
290  void destroyCandidates();
291 
292 public:
293  TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress = false)
294  : Loc(Loc), ForTakingAddress(ForTakingAddress) {}
297  operator=(const TemplateSpecCandidateSet &) = delete;
298  ~TemplateSpecCandidateSet() { destroyCandidates(); }
299 
300  SourceLocation getLocation() const { return Loc; }
301 
302  /// Clear out all of the candidates.
303  /// TODO: This may be unnecessary.
304  void clear();
305 
307 
308  iterator begin() { return Candidates.begin(); }
309  iterator end() { return Candidates.end(); }
310 
311  size_t size() const { return Candidates.size(); }
312  bool empty() const { return Candidates.empty(); }
313 
314  /// Add a new candidate with NumConversions conversion sequence slots
315  /// to the overload set.
317  Candidates.emplace_back();
318  return Candidates.back();
319  }
320 
321  void NoteCandidates(Sema &S, SourceLocation Loc);
322 
323  void NoteCandidates(Sema &S, SourceLocation Loc) const {
324  const_cast<TemplateSpecCandidateSet *>(this)->NoteCandidates(S, Loc);
325  }
326 };
327 
328 } // namespace clang
329 
330 #endif // LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
llvm::PointerUnion3< TemplateTypeParmDecl *, NonTypeTemplateParmDecl *, TemplateTemplateParmDecl * > TemplateParameter
Stores a template parameter of any kind.
Definition: DeclTemplate.h:62
const PartialDiagnosticAt & peekSFINAEDiagnostic() const
Peek at the SFINAE diagnostic.
A structure used to record information about a failed template argument deduction, for diagnosis.
Provides information about an attempted template argument deduction, whose success or failure was des...
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
TemplateDeductionInfo & operator=(const TemplateDeductionInfo &)=delete
Defines the C++ template declaration subclasses.
virtual void clear()
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
void addSuppressedDiagnostic(SourceLocation Loc, PartialDiagnostic PD)
Add a new diagnostic to the set of diagnostics.
void clearSFINAEDiagnostic()
Discard any SFINAE diagnostics.
Decl * Specialization
Specialization - The actual specialization that this candidate represents.
TemplateParameter Param
The template parameter to which a template argument deduction failure refers.
void NoteCandidates(Sema &S, SourceLocation Loc) const
TemplateSpecCandidateSet - A set of generalized overload candidates, used in template specializations...
DeductionFailureInfo DeductionFailure
Template argument deduction info.
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:276
void reset(TemplateArgumentList *NewDeduced)
Provide a new template argument list that contains the results of template argument deduction...
unsigned Result
A Sema::TemplateDeductionResult.
TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress=false)
SourceLocation getLocation() const
Returns the location at which template argument is occurring.
void takeSFINAEDiagnostic(PartialDiagnosticAt &PD)
Take ownership of the SFINAE diagnostic.
TemplateDeductionInfo(SourceLocation Loc, unsigned DeducedDepth=0)
SmallVector< DeducedPack *, 8 > PendingDeducedPacks
Information on packs that we&#39;re currently expanding.
Encodes a location in the source.
void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD)
Set the diagnostic which caused the SFINAE failure.
std::pair< SourceLocation, PartialDiagnostic > PartialDiagnosticAt
A partial diagnostic along with the source location where this diagnostic occurs. ...
unsigned HasDiagnostic
Indicates whether a diagnostic is stored in Diagnostic.
unsigned getDeducedDepth() const
The depth of template parameters for which deduction is being performed.
diag_iterator diag_end() const
Returns an iterator at the end of the sequence of suppressed diagnostics.
DeclAccessPair FoundDecl
The declaration that was looked up, together with its access.
SourceLocation getLocation() const
A POD class for pairing a NamedDecl* with an access specifier.
Represents a template argument.
Definition: TemplateBase.h:51
Dataflow Directional Tag Classes.
TemplateSpecCandidate & addCandidate()
Add a new candidate with NumConversions conversion sequence slots to the overload set...
void * Data
Opaque pointer containing additional data about this deduction failure.
bool hasSFINAEDiagnostic() const
Is a SFINAE diagnostic available?
TemplateArgumentList * take()
Take ownership of the deduced template argument list.
TemplateSpecCandidate - This is a generalization of OverloadCandidate which keeps track of template a...
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream...
A template argument list.
Definition: DeclTemplate.h:210
Defines the clang::SourceLocation class and associated facilities.
TemplateArgument SecondArg
The second template argument to which the template argument deduction failure refers.
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1315
unsigned CallArgIndex
The index of the function argument that caused a deduction failure.
TemplateArgument FirstArg
The first template argument to which the template argument deduction failure refers.
SmallVectorImpl< PartialDiagnosticAt >::const_iterator diag_iterator
Iterator over the set of suppressed diagnostics.
SmallVector< TemplateSpecCandidate, 16 >::iterator iterator
diag_iterator diag_begin() const
Returns an iterator at the beginning of the sequence of suppressed diagnostics.