TemplateDeduction.h revision f56faa01936b9cf909623d7f06e3c2569ca4a78e
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/Basic/PartialDiagnostic.h" 17#include "clang/AST/DeclTemplate.h" 18#include "llvm/ADT/SmallVector.h" 19 20namespace clang { 21 22class ASTContext; 23class TemplateArgumentList; 24 25namespace sema { 26 27/// \brief Provides information about an attempted template argument 28/// deduction, whose success or failure was described by a 29/// TemplateDeductionResult value. 30class TemplateDeductionInfo { 31 /// \brief The context in which the template arguments are stored. 32 ASTContext &Context; 33 34 /// \brief The deduced template argument list. 35 /// 36 TemplateArgumentList *Deduced; 37 38 /// \brief The source location at which template argument 39 /// deduction is occurring. 40 SourceLocation Loc; 41 42 /// \brief Have we suppressed an error during deduction? 43 bool HasSFINAEDiagnostic; 44 45 /// \brief Warnings (and follow-on notes) that were suppressed due to 46 /// SFINAE while performing template argument deduction. 47 SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics; 48 49 TemplateDeductionInfo(const TemplateDeductionInfo &) LLVM_DELETED_FUNCTION; 50 void operator=(const TemplateDeductionInfo &) LLVM_DELETED_FUNCTION; 51 52public: 53 TemplateDeductionInfo(ASTContext &Context, SourceLocation Loc) 54 : Context(Context), Deduced(0), Loc(Loc), HasSFINAEDiagnostic(false) { } 55 56 ~TemplateDeductionInfo() { 57 // FIXME: if (Deduced) Deduced->Destroy(Context); 58 } 59 60 /// \brief Returns the location at which template argument is 61 /// occurring. 62 SourceLocation getLocation() const { 63 return Loc; 64 } 65 66 /// \brief Take ownership of the deduced template argument list. 67 TemplateArgumentList *take() { 68 TemplateArgumentList *Result = Deduced; 69 Deduced = 0; 70 return Result; 71 } 72 73 /// \brief Take ownership of the SFINAE diagnostic. 74 void takeSFINAEDiagnostic(PartialDiagnosticAt &PD) { 75 assert(HasSFINAEDiagnostic); 76 PD.first = SuppressedDiagnostics.front().first; 77 PD.second.swap(SuppressedDiagnostics.front().second); 78 SuppressedDiagnostics.clear(); 79 HasSFINAEDiagnostic = false; 80 } 81 82 /// \brief Provide a new template argument list that contains the 83 /// results of template argument deduction. 84 void reset(TemplateArgumentList *NewDeduced) { 85 // FIXME: if (Deduced) Deduced->Destroy(Context); 86 Deduced = NewDeduced; 87 } 88 89 /// \brief Is a SFINAE diagnostic available? 90 bool hasSFINAEDiagnostic() const { 91 return HasSFINAEDiagnostic; 92 } 93 94 /// \brief Set the diagnostic which caused the SFINAE failure. 95 void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD) { 96 // Only collect the first diagnostic. 97 if (HasSFINAEDiagnostic) 98 return; 99 SuppressedDiagnostics.clear(); 100 SuppressedDiagnostics.push_back( 101 std::make_pair(Loc, PartialDiagnostic::NullDiagnostic())); 102 SuppressedDiagnostics.back().second.swap(PD); 103 HasSFINAEDiagnostic = true; 104 } 105 106 /// \brief Add a new diagnostic to the set of diagnostics 107 void addSuppressedDiagnostic(SourceLocation Loc, 108 PartialDiagnostic PD) { 109 if (HasSFINAEDiagnostic) 110 return; 111 SuppressedDiagnostics.push_back( 112 std::make_pair(Loc, PartialDiagnostic::NullDiagnostic())); 113 SuppressedDiagnostics.back().second.swap(PD); 114 } 115 116 /// \brief Iterator over the set of suppressed diagnostics. 117 typedef SmallVectorImpl<PartialDiagnosticAt>::const_iterator 118 diag_iterator; 119 120 /// \brief Returns an iterator at the beginning of the sequence of suppressed 121 /// diagnostics. 122 diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); } 123 124 /// \brief Returns an iterator at the end of the sequence of suppressed 125 /// diagnostics. 126 diag_iterator diag_end() const { return SuppressedDiagnostics.end(); } 127 128 /// \brief The template parameter to which a template argument 129 /// deduction failure refers. 130 /// 131 /// Depending on the result of template argument deduction, this 132 /// template parameter may have different meanings: 133 /// 134 /// TDK_Incomplete: this is the first template parameter whose 135 /// corresponding template argument was not deduced. 136 /// 137 /// TDK_Inconsistent: this is the template parameter for which 138 /// two different template argument values were deduced. 139 TemplateParameter Param; 140 141 /// \brief The first template argument to which the template 142 /// argument deduction failure refers. 143 /// 144 /// Depending on the result of the template argument deduction, 145 /// this template argument may have different meanings: 146 /// 147 /// TDK_Inconsistent: this argument is the first value deduced 148 /// for the corresponding template parameter. 149 /// 150 /// TDK_SubstitutionFailure: this argument is the template 151 /// argument we were instantiating when we encountered an error. 152 /// 153 /// TDK_NonDeducedMismatch: this is the template argument 154 /// provided in the source code. 155 TemplateArgument FirstArg; 156 157 /// \brief The second template argument to which the template 158 /// argument deduction failure refers. 159 /// 160 /// FIXME: Finish documenting this. 161 TemplateArgument SecondArg; 162}; 163 164} 165} 166 167#endif 168