clang  10.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  /// Returns the location at which template argument is
71  /// occurring.
73  return Loc;
74  }
75 
76  /// The depth of template parameters for which deduction is being
77  /// performed.
78  unsigned getDeducedDepth() const {
79  return DeducedDepth;
80  }
81 
82  /// Get the number of explicitly-specified arguments.
83  unsigned getNumExplicitArgs() const {
84  return ExplicitArgs;
85  }
86 
87  /// Take ownership of the deduced template argument list.
89  TemplateArgumentList *Result = Deduced;
90  Deduced = nullptr;
91  return Result;
92  }
93 
94  /// Take ownership of the SFINAE diagnostic.
96  assert(HasSFINAEDiagnostic);
97  PD.first = SuppressedDiagnostics.front().first;
98  PD.second.swap(SuppressedDiagnostics.front().second);
100  }
101 
102  /// Discard any SFINAE diagnostics.
104  SuppressedDiagnostics.clear();
105  HasSFINAEDiagnostic = false;
106  }
107 
108  /// Peek at the SFINAE diagnostic.
110  assert(HasSFINAEDiagnostic);
111  return SuppressedDiagnostics.front();
112  }
113 
114  /// Provide an initial template argument list that contains the
115  /// explicitly-specified arguments.
117  Deduced = NewDeduced;
118  ExplicitArgs = Deduced->size();
119  }
120 
121  /// Provide a new template argument list that contains the
122  /// results of template argument deduction.
123  void reset(TemplateArgumentList *NewDeduced) {
124  Deduced = NewDeduced;
125  }
126 
127  /// Is a SFINAE diagnostic available?
128  bool hasSFINAEDiagnostic() const {
129  return HasSFINAEDiagnostic;
130  }
131 
132  /// Set the diagnostic which caused the SFINAE failure.
134  // Only collect the first diagnostic.
135  if (HasSFINAEDiagnostic)
136  return;
137  SuppressedDiagnostics.clear();
138  SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
139  HasSFINAEDiagnostic = true;
140  }
141 
142  /// Add a new diagnostic to the set of diagnostics
144  PartialDiagnostic PD) {
145  if (HasSFINAEDiagnostic)
146  return;
147  SuppressedDiagnostics.emplace_back(Loc, std::move(PD));
148  }
149 
150  /// Iterator over the set of suppressed diagnostics.
152 
153  /// Returns an iterator at the beginning of the sequence of suppressed
154  /// diagnostics.
155  diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
156 
157  /// Returns an iterator at the end of the sequence of suppressed
158  /// diagnostics.
159  diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
160 
161  /// The template parameter to which a template argument
162  /// deduction failure refers.
163  ///
164  /// Depending on the result of template argument deduction, this
165  /// template parameter may have different meanings:
166  ///
167  /// TDK_Incomplete: this is the first template parameter whose
168  /// corresponding template argument was not deduced.
169  ///
170  /// TDK_IncompletePack: this is the expanded parameter pack for
171  /// which we deduced too few arguments.
172  ///
173  /// TDK_Inconsistent: this is the template parameter for which
174  /// two different template argument values were deduced.
176 
177  /// The first template argument to which the template
178  /// argument deduction failure refers.
179  ///
180  /// Depending on the result of the template argument deduction,
181  /// this template argument may have different meanings:
182  ///
183  /// TDK_IncompletePack: this is the number of arguments we deduced
184  /// for the pack.
185  ///
186  /// TDK_Inconsistent: this argument is the first value deduced
187  /// for the corresponding template parameter.
188  ///
189  /// TDK_SubstitutionFailure: this argument is the template
190  /// argument we were instantiating when we encountered an error.
191  ///
192  /// TDK_DeducedMismatch: this is the parameter type, after substituting
193  /// deduced arguments.
194  ///
195  /// TDK_NonDeducedMismatch: this is the component of the 'parameter'
196  /// of the deduction, directly provided in the source code.
198 
199  /// The second template argument to which the template
200  /// argument deduction failure refers.
201  ///
202  /// TDK_Inconsistent: this argument is the second value deduced
203  /// for the corresponding template parameter.
204  ///
205  /// TDK_DeducedMismatch: this is the (adjusted) call argument type.
206  ///
207  /// TDK_NonDeducedMismatch: this is the mismatching component of the
208  /// 'argument' of the deduction, from which we are deducing arguments.
209  ///
210  /// FIXME: Finish documenting this.
212 
213  /// The index of the function argument that caused a deduction
214  /// failure.
215  ///
216  /// TDK_DeducedMismatch: this is the index of the argument that had a
217  /// different argument type from its substituted parameter type.
218  unsigned CallArgIndex = 0;
219 
220  /// Information on packs that we're currently expanding.
221  ///
222  /// FIXME: This should be kept internal to SemaTemplateDeduction.
224 
225  /// \brief The constraint satisfaction details resulting from the associated
226  /// constraints satisfaction tests.
228 };
229 
230 } // namespace sema
231 
232 /// A structure used to record information about a failed
233 /// template argument deduction, for diagnosis.
235  /// A Sema::TemplateDeductionResult.
236  unsigned Result : 8;
237 
238  /// Indicates whether a diagnostic is stored in Diagnostic.
239  unsigned HasDiagnostic : 1;
240 
241  /// Opaque pointer containing additional data about
242  /// this deduction failure.
243  void *Data;
244 
245  /// A diagnostic indicating why deduction failed.
247 
248  /// Retrieve the diagnostic which caused this deduction failure,
249  /// if any.
250  PartialDiagnosticAt *getSFINAEDiagnostic();
251 
252  /// Retrieve the template parameter this deduction failure
253  /// refers to, if any.
254  TemplateParameter getTemplateParameter();
255 
256  /// Retrieve the template argument list associated with this
257  /// deduction failure, if any.
258  TemplateArgumentList *getTemplateArgumentList();
259 
260  /// Return the first template argument this deduction failure
261  /// refers to, if any.
262  const TemplateArgument *getFirstArg();
263 
264  /// Return the second template argument this deduction failure
265  /// refers to, if any.
266  const TemplateArgument *getSecondArg();
267 
268  /// Return the index of the call argument that this deduction
269  /// failure refers to, if any.
270  llvm::Optional<unsigned> getCallArgIndex();
271 
272  /// Free any memory associated with this deduction failure.
273  void Destroy();
274 };
275 
276 /// TemplateSpecCandidate - This is a generalization of OverloadCandidate
277 /// which keeps track of template argument deduction failure info, when
278 /// handling explicit specializations (and instantiations) of templates
279 /// beyond function overloading.
280 /// For now, assume that the candidates are non-matching specializations.
281 /// TODO: In the future, we may need to unify/generalize this with
282 /// OverloadCandidate.
284  /// The declaration that was looked up, together with its access.
285  /// Might be a UsingShadowDecl, but usually a FunctionTemplateDecl.
287 
288  /// Specialization - The actual specialization that this candidate
289  /// represents. When NULL, this may be a built-in candidate.
291 
292  /// Template argument deduction info
294 
295  void set(DeclAccessPair Found, Decl *Spec, DeductionFailureInfo Info) {
296  FoundDecl = Found;
297  Specialization = Spec;
298  DeductionFailure = Info;
299  }
300 
301  /// Diagnose a template argument deduction failure.
302  void NoteDeductionFailure(Sema &S, bool ForTakingAddress);
303 };
304 
305 /// TemplateSpecCandidateSet - A set of generalized overload candidates,
306 /// used in template specializations.
307 /// TODO: In the future, we may need to unify/generalize this with
308 /// OverloadCandidateSet.
311  SourceLocation Loc;
312 
313  // Stores whether we're taking the address of these candidates. This helps us
314  // produce better error messages when dealing with the pass_object_size
315  // attribute on parameters.
316  bool ForTakingAddress;
317 
318  void destroyCandidates();
319 
320 public:
321  TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress = false)
322  : Loc(Loc), ForTakingAddress(ForTakingAddress) {}
325  operator=(const TemplateSpecCandidateSet &) = delete;
326  ~TemplateSpecCandidateSet() { destroyCandidates(); }
327 
328  SourceLocation getLocation() const { return Loc; }
329 
330  /// Clear out all of the candidates.
331  /// TODO: This may be unnecessary.
332  void clear();
333 
335 
336  iterator begin() { return Candidates.begin(); }
337  iterator end() { return Candidates.end(); }
338 
339  size_t size() const { return Candidates.size(); }
340  bool empty() const { return Candidates.empty(); }
341 
342  /// Add a new candidate with NumConversions conversion sequence slots
343  /// to the overload set.
345  Candidates.emplace_back();
346  return Candidates.back();
347  }
348 
349  void NoteCandidates(Sema &S, SourceLocation Loc);
350 
351  void NoteCandidates(Sema &S, SourceLocation Loc) const {
352  const_cast<TemplateSpecCandidateSet *>(this)->NoteCandidates(S, Loc);
353  }
354 };
355 
356 } // namespace clang
357 
358 #endif // LLVM_CLANG_SEMA_TEMPLATEDEDUCTION_H
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...
unsigned size() const
Retrieve the number of template arguments in this template argument list.
Definition: DeclTemplate.h:299
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
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.
ConstraintSatisfaction AssociatedConstraintsSatisfaction
The constraint satisfaction details resulting from the associated constraints satisfaction tests...
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:336
void reset(TemplateArgumentList *NewDeduced)
Provide a new template argument list that contains the results of template argument deduction...
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:29
unsigned Result
A Sema::TemplateDeductionResult.
void setExplicitArgs(TemplateArgumentList *NewDeduced)
Provide an initial template argument list that contains the explicitly-specified arguments.
TemplateSpecCandidateSet(SourceLocation Loc, bool ForTakingAddress=false)
bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:791
SourceLocation getLocation() const
Returns the location at which template argument is occurring.
void takeSFINAEDiagnostic(PartialDiagnosticAt &PD)
Take ownership of the SFINAE diagnostic.
llvm::PointerUnion< TemplateTypeParmDecl *, NonTypeTemplateParmDecl *, TemplateTemplateParmDecl * > TemplateParameter
Stores a template parameter of any kind.
Definition: DeclTemplate.h:63
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:50
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:239
Defines the clang::SourceLocation class and associated facilities.
unsigned getNumExplicitArgs() const
Get the number of explicitly-specified arguments.
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:1327
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.
This file provides AST data structures related to concepts.
diag_iterator diag_begin() const
Returns an iterator at the beginning of the sequence of suppressed diagnostics.