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