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 // This file provides types used with Sema's template argument deduction 10 // routines. 11 // 12 //===----------------------------------------------------------------------===/ 13 #ifndef LLVM_CLANG_SEMA_TEMPLATE_DEDUCTION_H 14 #define LLVM_CLANG_SEMA_TEMPLATE_DEDUCTION_H 15 16 #include "clang/AST/DeclTemplate.h" 17 #include "clang/Basic/PartialDiagnostic.h" 18 #include "llvm/ADT/SmallVector.h" 19 20 namespace clang { 21 22 class TemplateArgumentList; 23 24 namespace sema { 25 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. 31 /// 32 TemplateArgumentList *Deduced; 33 34 /// \brief The source location at which template argument 35 /// deduction is occurring. 36 SourceLocation Loc; 37 38 /// \brief Have we suppressed an error during deduction? 39 bool HasSFINAEDiagnostic; 40 41 /// \brief Warnings (and follow-on notes) that were suppressed due to 42 /// SFINAE while performing template argument deduction. 43 SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics; 44 45 TemplateDeductionInfo(const TemplateDeductionInfo &) LLVM_DELETED_FUNCTION; 46 void operator=(const TemplateDeductionInfo &) LLVM_DELETED_FUNCTION; 47 48 public: 49 TemplateDeductionInfo(SourceLocation Loc) 50 : Deduced(0), Loc(Loc), HasSFINAEDiagnostic(false), Expression(0) { } 51 52 /// \brief Returns the location at which template argument is 53 /// occurring. 54 SourceLocation getLocation() const { 55 return Loc; 56 } 57 58 /// \brief Take ownership of the deduced template argument list. 59 TemplateArgumentList *take() { 60 TemplateArgumentList *Result = Deduced; 61 Deduced = 0; 62 return Result; 63 } 64 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; 72 } 73 74 /// \brief Provide a new template argument list that contains the 75 /// results of template argument deduction. 76 void reset(TemplateArgumentList *NewDeduced) { 77 Deduced = NewDeduced; 78 } 79 80 /// \brief Is a SFINAE diagnostic available? 81 bool hasSFINAEDiagnostic() const { 82 return HasSFINAEDiagnostic; 83 } 84 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) 89 return; 90 SuppressedDiagnostics.clear(); 91 SuppressedDiagnostics.push_back( 92 std::make_pair(Loc, PartialDiagnostic::NullDiagnostic())); 93 SuppressedDiagnostics.back().second.swap(PD); 94 HasSFINAEDiagnostic = true; 95 } 96 97 /// \brief Add a new diagnostic to the set of diagnostics 98 void addSuppressedDiagnostic(SourceLocation Loc, 99 PartialDiagnostic PD) { 100 if (HasSFINAEDiagnostic) 101 return; 102 SuppressedDiagnostics.push_back( 103 std::make_pair(Loc, PartialDiagnostic::NullDiagnostic())); 104 SuppressedDiagnostics.back().second.swap(PD); 105 } 106 107 /// \brief Iterator over the set of suppressed diagnostics. 108 typedef SmallVectorImpl<PartialDiagnosticAt>::const_iterator 109 diag_iterator; 110 111 /// \brief Returns an iterator at the beginning of the sequence of suppressed 112 /// diagnostics. 113 diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); } 114 115 /// \brief Returns an iterator at the end of the sequence of suppressed 116 /// diagnostics. 117 diag_iterator diag_end() const { return SuppressedDiagnostics.end(); } 118 119 /// \brief The template parameter to which a template argument 120 /// deduction failure refers. 121 /// 122 /// Depending on the result of template argument deduction, this 123 /// template parameter may have different meanings: 124 /// 125 /// TDK_Incomplete: this is the first template parameter whose 126 /// corresponding template argument was not deduced. 127 /// 128 /// TDK_Inconsistent: this is the template parameter for which 129 /// two different template argument values were deduced. 130 TemplateParameter Param; 131 132 /// \brief The first template argument to which the template 133 /// argument deduction failure refers. 134 /// 135 /// Depending on the result of the template argument deduction, 136 /// this template argument may have different meanings: 137 /// 138 /// TDK_Inconsistent: this argument is the first value deduced 139 /// for the corresponding template parameter. 140 /// 141 /// TDK_SubstitutionFailure: this argument is the template 142 /// argument we were instantiating when we encountered an error. 143 /// 144 /// TDK_NonDeducedMismatch: this is the component of the 'parameter' 145 /// of the deduction, directly provided in the source code. 146 TemplateArgument FirstArg; 147 148 /// \brief The second template argument to which the template 149 /// argument deduction failure refers. 150 /// 151 /// TDK_NonDeducedMismatch: this is the mismatching component of the 152 /// 'argument' of the deduction, from which we are deducing arguments. 153 /// 154 /// FIXME: Finish documenting this. 155 TemplateArgument SecondArg; 156 157 /// \brief The expression which caused a deduction failure. 158 /// 159 /// TDK_FailedOverloadResolution: this argument is the reference to 160 // an overloaded function which could not be resolved to a specific 161 // function. 162 Expr *Expression; 163 }; 164 165 } 166 } 167 168 #endif 169