clang API Documentation

TemplateDeduction.h
Go to the documentation of this file.
00001 //===- TemplateDeduction.h - C++ template argument deduction ----*- C++ -*-===/
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //===----------------------------------------------------------------------===/
00008 //
00009 //  This file provides types used with Sema's template argument deduction
00010 // routines.
00011 //
00012 //===----------------------------------------------------------------------===/
00013 #ifndef LLVM_CLANG_SEMA_TEMPLATE_DEDUCTION_H
00014 #define LLVM_CLANG_SEMA_TEMPLATE_DEDUCTION_H
00015 
00016 #include "clang/Basic/PartialDiagnostic.h"
00017 #include "clang/AST/DeclTemplate.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 
00020 namespace clang {
00021 
00022 class ASTContext;
00023 class TemplateArgumentList;
00024 
00025 namespace sema {
00026 
00027 /// \brief Provides information about an attempted template argument
00028 /// deduction, whose success or failure was described by a
00029 /// TemplateDeductionResult value.
00030 class TemplateDeductionInfo {
00031   /// \brief The context in which the template arguments are stored.
00032   ASTContext &Context;
00033 
00034   /// \brief The deduced template argument list.
00035   ///
00036   TemplateArgumentList *Deduced;
00037 
00038   /// \brief The source location at which template argument
00039   /// deduction is occurring.
00040   SourceLocation Loc;
00041 
00042   /// \brief Have we suppressed an error during deduction?
00043   bool HasSFINAEDiagnostic;
00044 
00045   /// \brief Warnings (and follow-on notes) that were suppressed due to
00046   /// SFINAE while performing template argument deduction.
00047   SmallVector<PartialDiagnosticAt, 4> SuppressedDiagnostics;
00048 
00049   // do not implement these
00050   TemplateDeductionInfo(const TemplateDeductionInfo&);
00051   TemplateDeductionInfo &operator=(const TemplateDeductionInfo&);
00052 
00053 public:
00054   TemplateDeductionInfo(ASTContext &Context, SourceLocation Loc)
00055     : Context(Context), Deduced(0), Loc(Loc), HasSFINAEDiagnostic(false) { }
00056 
00057   ~TemplateDeductionInfo() {
00058     // FIXME: if (Deduced) Deduced->Destroy(Context);
00059   }
00060 
00061   /// \brief Returns the location at which template argument is
00062   /// occurring.
00063   SourceLocation getLocation() const {
00064     return Loc;
00065   }
00066 
00067   /// \brief Take ownership of the deduced template argument list.
00068   TemplateArgumentList *take() {
00069     TemplateArgumentList *Result = Deduced;
00070     Deduced = 0;
00071     return Result;
00072   }
00073 
00074   /// \brief Take ownership of the SFINAE diagnostic.
00075   void takeSFINAEDiagnostic(PartialDiagnosticAt &PD) {
00076     assert(HasSFINAEDiagnostic);
00077     PD.first = SuppressedDiagnostics.front().first;
00078     PD.second.swap(SuppressedDiagnostics.front().second);
00079     SuppressedDiagnostics.clear();
00080     HasSFINAEDiagnostic = false;
00081   }
00082 
00083   /// \brief Provide a new template argument list that contains the
00084   /// results of template argument deduction.
00085   void reset(TemplateArgumentList *NewDeduced) {
00086     // FIXME: if (Deduced) Deduced->Destroy(Context);
00087     Deduced = NewDeduced;
00088   }
00089 
00090   /// \brief Is a SFINAE diagnostic available?
00091   bool hasSFINAEDiagnostic() const {
00092     return HasSFINAEDiagnostic;
00093   }
00094 
00095   /// \brief Set the diagnostic which caused the SFINAE failure.
00096   void addSFINAEDiagnostic(SourceLocation Loc, PartialDiagnostic PD) {
00097     // Only collect the first diagnostic.
00098     if (HasSFINAEDiagnostic)
00099       return;
00100     SuppressedDiagnostics.clear();
00101     SuppressedDiagnostics.push_back(
00102         std::make_pair(Loc, PartialDiagnostic::NullDiagnostic()));
00103     SuppressedDiagnostics.back().second.swap(PD);
00104     HasSFINAEDiagnostic = true;
00105   }
00106 
00107   /// \brief Add a new diagnostic to the set of diagnostics
00108   void addSuppressedDiagnostic(SourceLocation Loc,
00109                                PartialDiagnostic PD) {
00110     if (HasSFINAEDiagnostic)
00111       return;
00112     SuppressedDiagnostics.push_back(
00113         std::make_pair(Loc, PartialDiagnostic::NullDiagnostic()));
00114     SuppressedDiagnostics.back().second.swap(PD);
00115   }
00116 
00117   /// \brief Iterator over the set of suppressed diagnostics.
00118   typedef SmallVectorImpl<PartialDiagnosticAt>::const_iterator
00119     diag_iterator;
00120 
00121   /// \brief Returns an iterator at the beginning of the sequence of suppressed
00122   /// diagnostics.
00123   diag_iterator diag_begin() const { return SuppressedDiagnostics.begin(); }
00124 
00125   /// \brief Returns an iterator at the end of the sequence of suppressed
00126   /// diagnostics.
00127   diag_iterator diag_end() const { return SuppressedDiagnostics.end(); }
00128 
00129   /// \brief The template parameter to which a template argument
00130   /// deduction failure refers.
00131   ///
00132   /// Depending on the result of template argument deduction, this
00133   /// template parameter may have different meanings:
00134   ///
00135   ///   TDK_Incomplete: this is the first template parameter whose
00136   ///   corresponding template argument was not deduced.
00137   ///
00138   ///   TDK_Inconsistent: this is the template parameter for which
00139   ///   two different template argument values were deduced.
00140   TemplateParameter Param;
00141 
00142   /// \brief The first template argument to which the template
00143   /// argument deduction failure refers.
00144   ///
00145   /// Depending on the result of the template argument deduction,
00146   /// this template argument may have different meanings:
00147   ///
00148   ///   TDK_Inconsistent: this argument is the first value deduced
00149   ///   for the corresponding template parameter.
00150   ///
00151   ///   TDK_SubstitutionFailure: this argument is the template
00152   ///   argument we were instantiating when we encountered an error.
00153   ///
00154   ///   TDK_NonDeducedMismatch: this is the template argument
00155   ///   provided in the source code.
00156   TemplateArgument FirstArg;
00157 
00158   /// \brief The second template argument to which the template
00159   /// argument deduction failure refers.
00160   ///
00161   /// FIXME: Finish documenting this.
00162   TemplateArgument SecondArg;
00163 };
00164 
00165 }
00166 }
00167 
00168 #endif