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_TEMPLATE_DEDUCTION_H
14 #define LLVM_CLANG_SEMA_TEMPLATE_DEDUCTION_H
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/Basic/PartialDiagnostic.h"
18 #include "llvm/ADT/SmallVector.h"
22 class TemplateArgumentList;
26 /// \brief Provides information about an attempted template argument
27 /// deduction, whose success or failure was described by a
28 /// TemplateDeductionResult value.
29 class TemplateDeductionInfo {
30 /// \brief The deduced template argument list.
32 TemplateArgumentList *Deduced;
34 /// \brief The source location at which template argument
35 /// deduction is occurring.
38 /// \brief Have we suppressed an error during deduction?
39 bool HasSFINAEDiagnostic;
41 /// \brief Warnings (and follow-on notes) that were suppressed due to
42 /// SFINAE while performing template argument deduction.
43 SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics;
45 TemplateDeductionInfo(const TemplateDeductionInfo &) LLVM_DELETED_FUNCTION;
46 void operator=(const TemplateDeductionInfo &) LLVM_DELETED_FUNCTION;
49 TemplateDeductionInfo(SourceLocation Loc)
50 : Deduced(0), Loc(Loc), HasSFINAEDiagnostic(false), Expression(0) { }
52 /// \brief Returns the location at which template argument is
54 SourceLocation getLocation() const {
58 /// \brief Take ownership of the deduced template argument list.
59 TemplateArgumentList *take() {
60 TemplateArgumentList *Result = Deduced;
65 /// \brief Take ownership of the SFINAE diagnostic.
66 void takeSFINAEDiagnostic(PartialDiagnosticAt &PD) {
67 assert(HasSFINAEDiagnostic);
68 PD.first = SuppressedDiagnostics.front().first;
69 PD.second.swap(SuppressedDiagnostics.front().second);
70 SuppressedDiagnostics.clear();
71 HasSFINAEDiagnostic = false;
74 /// \brief Provide a new template argument list that contains the
75 /// results of template argument deduction.
76 void reset(TemplateArgumentList *NewDeduced) {
80 /// \brief Is a SFINAE diagnostic available?
81 bool hasSFINAEDiagnostic() const {
82 return HasSFINAEDiagnostic;
85 /// \brief Set the diagnostic which caused the SFINAE failure.
86 void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD) {
87 // Only collect the first diagnostic.
88 if (HasSFINAEDiagnostic)
90 SuppressedDiagnostics.clear();
91 SuppressedDiagnostics.push_back(
92 std::make_pair(Loc, PartialDiagnostic::NullDiagnostic()));
93 SuppressedDiagnostics.back().second.swap(PD);
94 HasSFINAEDiagnostic = true;
97 /// \brief Add a new diagnostic to the set of diagnostics
98 void addSuppressedDiagnostic(SourceLocation Loc,
99 PartialDiagnostic PD) {
100 if (HasSFINAEDiagnostic)
102 SuppressedDiagnostics.push_back(
103 std::make_pair(Loc, PartialDiagnostic::NullDiagnostic()));
104 SuppressedDiagnostics.back().second.swap(PD);
107 /// \brief Iterator over the set of suppressed diagnostics.
108 typedef SmallVectorImpl<PartialDiagnosticAt>::const_iterator
111 /// \brief Returns an iterator at the beginning of the sequence of suppressed
113 diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
115 /// \brief Returns an iterator at the end of the sequence of suppressed
117 diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
119 /// \brief The template parameter to which a template argument
120 /// deduction failure refers.
122 /// Depending on the result of template argument deduction, this
123 /// template parameter may have different meanings:
125 /// TDK_Incomplete: this is the first template parameter whose
126 /// corresponding template argument was not deduced.
128 /// TDK_Inconsistent: this is the template parameter for which
129 /// two different template argument values were deduced.
130 TemplateParameter Param;
132 /// \brief The first template argument to which the template
133 /// argument deduction failure refers.
135 /// Depending on the result of the template argument deduction,
136 /// this template argument may have different meanings:
138 /// TDK_Inconsistent: this argument is the first value deduced
139 /// for the corresponding template parameter.
141 /// TDK_SubstitutionFailure: this argument is the template
142 /// argument we were instantiating when we encountered an error.
144 /// TDK_NonDeducedMismatch: this is the component of the 'parameter'
145 /// of the deduction, directly provided in the source code.
146 TemplateArgument FirstArg;
148 /// \brief The second template argument to which the template
149 /// argument deduction failure refers.
151 /// TDK_NonDeducedMismatch: this is the mismatching component of the
152 /// 'argument' of the deduction, from which we are deducing arguments.
154 /// FIXME: Finish documenting this.
155 TemplateArgument SecondArg;
157 /// \brief The expression which caused a deduction failure.
159 /// TDK_FailedOverloadResolution: this argument is the reference to
160 /// an overloaded function which could not be resolved to a specific