SemaTemplateDeduction.cpp revision 508f1c889b9833903ea394034fe0246d3a57a32d
1//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
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 implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
20#include "llvm/Support/Compiler.h"
21
22namespace clang {
23  /// \brief Various flags that control template argument deduction.
24  ///
25  /// These flags can be bitwise-OR'd together.
26  enum TemplateDeductionFlags {
27    /// \brief No template argument deduction flags, which indicates the
28    /// strictest results for template argument deduction (as used for, e.g.,
29    /// matching class template partial specializations).
30    TDF_None = 0,
31    /// \brief Within template argument deduction from a function call, we are
32    /// matching with a parameter type for which the original parameter was
33    /// a reference.
34    TDF_ParamWithReferenceType = 0x1,
35    /// \brief Within template argument deduction from a function call, we
36    /// are matching in a case where we ignore cv-qualifiers.
37    TDF_IgnoreQualifiers = 0x02,
38    /// \brief Within template argument deduction from a function call,
39    /// we are matching in a case where we can perform template argument
40    /// deduction from a derived class of the argument type.
41    /// FIXME: this is completely unsupported right now.
42    TDF_DerivedClass = 0x04
43  };
44}
45
46using namespace clang;
47
48static Sema::TemplateDeductionResult
49DeduceTemplateArguments(ASTContext &Context,
50                        TemplateParameterList *TemplateParams,
51                        const TemplateArgument &Param,
52                        const TemplateArgument &Arg,
53                        Sema::TemplateDeductionInfo &Info,
54                        llvm::SmallVectorImpl<TemplateArgument> &Deduced);
55
56/// \brief If the given expression is of a form that permits the deduction
57/// of a non-type template parameter, return the declaration of that
58/// non-type template parameter.
59static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
60  if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
61    E = IC->getSubExpr();
62
63  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
64    return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
65
66  return 0;
67}
68
69/// \brief Deduce the value of the given non-type template parameter
70/// from the given constant.
71static Sema::TemplateDeductionResult
72DeduceNonTypeTemplateArgument(ASTContext &Context,
73                              NonTypeTemplateParmDecl *NTTP,
74                              llvm::APSInt Value,
75                              Sema::TemplateDeductionInfo &Info,
76                              llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
77  assert(NTTP->getDepth() == 0 &&
78         "Cannot deduce non-type template argument with depth > 0");
79
80  if (Deduced[NTTP->getIndex()].isNull()) {
81    QualType T = NTTP->getType();
82
83    // FIXME: Make sure we didn't overflow our data type!
84    unsigned AllowedBits = Context.getTypeSize(T);
85    if (Value.getBitWidth() != AllowedBits)
86      Value.extOrTrunc(AllowedBits);
87    Value.setIsSigned(T->isSignedIntegerType());
88
89    Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
90    return Sema::TDK_Success;
91  }
92
93  assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
94
95  // If the template argument was previously deduced to a negative value,
96  // then our deduction fails.
97  const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
98  if (PrevValuePtr->isNegative()) {
99    Info.Param = NTTP;
100    Info.FirstArg = Deduced[NTTP->getIndex()];
101    Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
102    return Sema::TDK_Inconsistent;
103  }
104
105  llvm::APSInt PrevValue = *PrevValuePtr;
106  if (Value.getBitWidth() > PrevValue.getBitWidth())
107    PrevValue.zext(Value.getBitWidth());
108  else if (Value.getBitWidth() < PrevValue.getBitWidth())
109    Value.zext(PrevValue.getBitWidth());
110
111  if (Value != PrevValue) {
112    Info.Param = NTTP;
113    Info.FirstArg = Deduced[NTTP->getIndex()];
114    Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
115    return Sema::TDK_Inconsistent;
116  }
117
118  return Sema::TDK_Success;
119}
120
121/// \brief Deduce the value of the given non-type template parameter
122/// from the given type- or value-dependent expression.
123///
124/// \returns true if deduction succeeded, false otherwise.
125
126static Sema::TemplateDeductionResult
127DeduceNonTypeTemplateArgument(ASTContext &Context,
128                              NonTypeTemplateParmDecl *NTTP,
129                              Expr *Value,
130                              Sema::TemplateDeductionInfo &Info,
131                           llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
132  assert(NTTP->getDepth() == 0 &&
133         "Cannot deduce non-type template argument with depth > 0");
134  assert((Value->isTypeDependent() || Value->isValueDependent()) &&
135         "Expression template argument must be type- or value-dependent.");
136
137  if (Deduced[NTTP->getIndex()].isNull()) {
138    // FIXME: Clone the Value?
139    Deduced[NTTP->getIndex()] = TemplateArgument(Value);
140    return Sema::TDK_Success;
141  }
142
143  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
144    // Okay, we deduced a constant in one case and a dependent expression
145    // in another case. FIXME: Later, we will check that instantiating the
146    // dependent expression gives us the constant value.
147    return Sema::TDK_Success;
148  }
149
150  // FIXME: Compare the expressions for equality!
151  return Sema::TDK_Success;
152}
153
154static Sema::TemplateDeductionResult
155DeduceTemplateArguments(ASTContext &Context,
156                        TemplateName Param,
157                        TemplateName Arg,
158                        Sema::TemplateDeductionInfo &Info,
159                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
160  // FIXME: Implement template argument deduction for template
161  // template parameters.
162
163  // FIXME: this routine does not have enough information to produce
164  // good diagnostics.
165
166  TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
167  TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
168
169  if (!ParamDecl || !ArgDecl) {
170    // FIXME: fill in Info.Param/Info.FirstArg
171    return Sema::TDK_Inconsistent;
172  }
173
174  ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl));
175  ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl));
176  if (ParamDecl != ArgDecl) {
177    // FIXME: fill in Info.Param/Info.FirstArg
178    return Sema::TDK_Inconsistent;
179  }
180
181  return Sema::TDK_Success;
182}
183
184/// \brief Deduce the template arguments by comparing the parameter type and
185/// the argument type (C++ [temp.deduct.type]).
186///
187/// \param Context the AST context in which this deduction occurs.
188///
189/// \param TemplateParams the template parameters that we are deducing
190///
191/// \param ParamIn the parameter type
192///
193/// \param ArgIn the argument type
194///
195/// \param Info information about the template argument deduction itself
196///
197/// \param Deduced the deduced template arguments
198///
199/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
200/// how template argument deduction is performed.
201///
202/// \returns the result of template argument deduction so far. Note that a
203/// "success" result means that template argument deduction has not yet failed,
204/// but it may still fail, later, for other reasons.
205static Sema::TemplateDeductionResult
206DeduceTemplateArguments(ASTContext &Context,
207                        TemplateParameterList *TemplateParams,
208                        QualType ParamIn, QualType ArgIn,
209                        Sema::TemplateDeductionInfo &Info,
210                        llvm::SmallVectorImpl<TemplateArgument> &Deduced,
211                        unsigned TDF) {
212  // We only want to look at the canonical types, since typedefs and
213  // sugar are not part of template argument deduction.
214  QualType Param = Context.getCanonicalType(ParamIn);
215  QualType Arg = Context.getCanonicalType(ArgIn);
216
217  // C++0x [temp.deduct.call]p4 bullet 1:
218  //   - If the original P is a reference type, the deduced A (i.e., the type
219  //     referred to by the reference) can be more cv-qualified than the
220  //     transformed A.
221  if (TDF & TDF_ParamWithReferenceType) {
222    unsigned ExtraQualsOnParam
223      = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
224    Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
225  }
226
227  // If the parameter type is not dependent, there is nothing to deduce.
228  if (!Param->isDependentType())
229    return Sema::TDK_Success;
230
231  // C++ [temp.deduct.type]p9:
232  //   A template type argument T, a template template argument TT or a
233  //   template non-type argument i can be deduced if P and A have one of
234  //   the following forms:
235  //
236  //     T
237  //     cv-list T
238  if (const TemplateTypeParmType *TemplateTypeParm
239        = Param->getAsTemplateTypeParmType()) {
240    unsigned Index = TemplateTypeParm->getIndex();
241
242    // The argument type can not be less qualified than the parameter
243    // type.
244    if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
245      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
246      Info.FirstArg = Deduced[Index];
247      Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
248      return Sema::TDK_InconsistentQuals;
249    }
250
251    assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
252
253    unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
254    QualType DeducedType = Arg.getQualifiedType(Quals);
255
256    if (Deduced[Index].isNull())
257      Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
258    else {
259      // C++ [temp.deduct.type]p2:
260      //   [...] If type deduction cannot be done for any P/A pair, or if for
261      //   any pair the deduction leads to more than one possible set of
262      //   deduced values, or if different pairs yield different deduced
263      //   values, or if any template argument remains neither deduced nor
264      //   explicitly specified, template argument deduction fails.
265      if (Deduced[Index].getAsType() != DeducedType) {
266        Info.Param
267          = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
268        Info.FirstArg = Deduced[Index];
269        Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
270        return Sema::TDK_Inconsistent;
271      }
272    }
273    return Sema::TDK_Success;
274  }
275
276  // Set up the template argument deduction information for a failure.
277  Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
278  Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
279
280  // Check the cv-qualifiers on the parameter and argument types.
281  if (!(TDF & TDF_IgnoreQualifiers)) {
282    if (TDF & TDF_ParamWithReferenceType) {
283      if (Param.isMoreQualifiedThan(Arg))
284        return Sema::TDK_NonDeducedMismatch;
285    } else {
286      if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
287        return Sema::TDK_NonDeducedMismatch;
288    }
289  }
290
291  switch (Param->getTypeClass()) {
292    // No deduction possible for these types
293    case Type::Builtin:
294      return Sema::TDK_NonDeducedMismatch;
295
296    //     T *
297    case Type::Pointer: {
298      const PointerType *PointerArg = Arg->getAsPointerType();
299      if (!PointerArg)
300        return Sema::TDK_NonDeducedMismatch;
301
302      return DeduceTemplateArguments(Context, TemplateParams,
303                                   cast<PointerType>(Param)->getPointeeType(),
304                                     PointerArg->getPointeeType(),
305                                     Info, Deduced,
306                                     TDF & TDF_IgnoreQualifiers);
307    }
308
309    //     T &
310    case Type::LValueReference: {
311      const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
312      if (!ReferenceArg)
313        return Sema::TDK_NonDeducedMismatch;
314
315      return DeduceTemplateArguments(Context, TemplateParams,
316                           cast<LValueReferenceType>(Param)->getPointeeType(),
317                                     ReferenceArg->getPointeeType(),
318                                     Info, Deduced, 0);
319    }
320
321    //     T && [C++0x]
322    case Type::RValueReference: {
323      const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
324      if (!ReferenceArg)
325        return Sema::TDK_NonDeducedMismatch;
326
327      return DeduceTemplateArguments(Context, TemplateParams,
328                           cast<RValueReferenceType>(Param)->getPointeeType(),
329                                     ReferenceArg->getPointeeType(),
330                                     Info, Deduced, 0);
331    }
332
333    //     T [] (implied, but not stated explicitly)
334    case Type::IncompleteArray: {
335      const IncompleteArrayType *IncompleteArrayArg =
336        Context.getAsIncompleteArrayType(Arg);
337      if (!IncompleteArrayArg)
338        return Sema::TDK_NonDeducedMismatch;
339
340      return DeduceTemplateArguments(Context, TemplateParams,
341                     Context.getAsIncompleteArrayType(Param)->getElementType(),
342                                     IncompleteArrayArg->getElementType(),
343                                     Info, Deduced, 0);
344    }
345
346    //     T [integer-constant]
347    case Type::ConstantArray: {
348      const ConstantArrayType *ConstantArrayArg =
349        Context.getAsConstantArrayType(Arg);
350      if (!ConstantArrayArg)
351        return Sema::TDK_NonDeducedMismatch;
352
353      const ConstantArrayType *ConstantArrayParm =
354        Context.getAsConstantArrayType(Param);
355      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
356        return Sema::TDK_NonDeducedMismatch;
357
358      return DeduceTemplateArguments(Context, TemplateParams,
359                                     ConstantArrayParm->getElementType(),
360                                     ConstantArrayArg->getElementType(),
361                                     Info, Deduced, 0);
362    }
363
364    //     type [i]
365    case Type::DependentSizedArray: {
366      const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
367      if (!ArrayArg)
368        return Sema::TDK_NonDeducedMismatch;
369
370      // Check the element type of the arrays
371      const DependentSizedArrayType *DependentArrayParm
372        = cast<DependentSizedArrayType>(Param);
373      if (Sema::TemplateDeductionResult Result
374            = DeduceTemplateArguments(Context, TemplateParams,
375                                      DependentArrayParm->getElementType(),
376                                      ArrayArg->getElementType(),
377                                      Info, Deduced, 0))
378        return Result;
379
380      // Determine the array bound is something we can deduce.
381      NonTypeTemplateParmDecl *NTTP
382        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
383      if (!NTTP)
384        return Sema::TDK_Success;
385
386      // We can perform template argument deduction for the given non-type
387      // template parameter.
388      assert(NTTP->getDepth() == 0 &&
389             "Cannot deduce non-type template argument at depth > 0");
390      if (const ConstantArrayType *ConstantArrayArg
391            = dyn_cast<ConstantArrayType>(ArrayArg)) {
392        llvm::APSInt Size(ConstantArrayArg->getSize());
393        return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
394                                             Info, Deduced);
395      }
396      if (const DependentSizedArrayType *DependentArrayArg
397            = dyn_cast<DependentSizedArrayType>(ArrayArg))
398        return DeduceNonTypeTemplateArgument(Context, NTTP,
399                                             DependentArrayArg->getSizeExpr(),
400                                             Info, Deduced);
401
402      // Incomplete type does not match a dependently-sized array type
403      return Sema::TDK_NonDeducedMismatch;
404    }
405
406    //     type(*)(T)
407    //     T(*)()
408    //     T(*)(T)
409    case Type::FunctionProto: {
410      const FunctionProtoType *FunctionProtoArg =
411        dyn_cast<FunctionProtoType>(Arg);
412      if (!FunctionProtoArg)
413        return Sema::TDK_NonDeducedMismatch;
414
415      const FunctionProtoType *FunctionProtoParam =
416        cast<FunctionProtoType>(Param);
417
418      if (FunctionProtoParam->getTypeQuals() !=
419          FunctionProtoArg->getTypeQuals())
420        return Sema::TDK_NonDeducedMismatch;
421
422      if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
423        return Sema::TDK_NonDeducedMismatch;
424
425      if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
426        return Sema::TDK_NonDeducedMismatch;
427
428      // Check return types.
429      if (Sema::TemplateDeductionResult Result
430            = DeduceTemplateArguments(Context, TemplateParams,
431                                      FunctionProtoParam->getResultType(),
432                                      FunctionProtoArg->getResultType(),
433                                      Info, Deduced, 0))
434        return Result;
435
436      for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
437        // Check argument types.
438        if (Sema::TemplateDeductionResult Result
439              = DeduceTemplateArguments(Context, TemplateParams,
440                                        FunctionProtoParam->getArgType(I),
441                                        FunctionProtoArg->getArgType(I),
442                                        Info, Deduced, 0))
443          return Result;
444      }
445
446      return Sema::TDK_Success;
447    }
448
449    //     template-name<T> (where template-name refers to a class template)
450    //     template-name<i>
451    //     TT<T> (TODO)
452    //     TT<i> (TODO)
453    //     TT<> (TODO)
454    case Type::TemplateSpecialization: {
455      const TemplateSpecializationType *SpecParam
456        = cast<TemplateSpecializationType>(Param);
457
458      // Check whether the template argument is a dependent template-id.
459      // FIXME: This is untested code; it can be tested when we implement
460      // partial ordering of class template partial specializations.
461      if (const TemplateSpecializationType *SpecArg
462            = dyn_cast<TemplateSpecializationType>(Arg)) {
463        // Perform template argument deduction for the template name.
464        if (Sema::TemplateDeductionResult Result
465              = DeduceTemplateArguments(Context,
466                                        SpecParam->getTemplateName(),
467                                        SpecArg->getTemplateName(),
468                                        Info, Deduced))
469          return Result;
470
471        unsigned NumArgs = SpecParam->getNumArgs();
472
473        // FIXME: When one of the template-names refers to a
474        // declaration with default template arguments, do we need to
475        // fill in those default template arguments here? Most likely,
476        // the answer is "yes", but I don't see any references. This
477        // issue may be resolved elsewhere, because we may want to
478        // instantiate default template arguments when
479        if (SpecArg->getNumArgs() != NumArgs)
480          return Sema::TDK_NonDeducedMismatch;
481
482        // Perform template argument deduction on each template
483        // argument.
484        for (unsigned I = 0; I != NumArgs; ++I)
485          if (Sema::TemplateDeductionResult Result
486                = DeduceTemplateArguments(Context, TemplateParams,
487                                          SpecParam->getArg(I),
488                                          SpecArg->getArg(I),
489                                          Info, Deduced))
490            return Result;
491
492        return Sema::TDK_Success;
493      }
494
495      // If the argument type is a class template specialization, we
496      // perform template argument deduction using its template
497      // arguments.
498      const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
499      if (!RecordArg)
500        return Sema::TDK_NonDeducedMismatch;
501
502      ClassTemplateSpecializationDecl *SpecArg
503        = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
504      if (!SpecArg)
505        return Sema::TDK_NonDeducedMismatch;
506
507      // Perform template argument deduction for the template name.
508      if (Sema::TemplateDeductionResult Result
509            = DeduceTemplateArguments(Context,
510                                      SpecParam->getTemplateName(),
511                              TemplateName(SpecArg->getSpecializedTemplate()),
512                                      Info, Deduced))
513          return Result;
514
515      // FIXME: Can the # of arguments in the parameter and the argument differ?
516      unsigned NumArgs = SpecParam->getNumArgs();
517      const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
518      if (NumArgs != ArgArgs.size())
519        return Sema::TDK_NonDeducedMismatch;
520
521      for (unsigned I = 0; I != NumArgs; ++I)
522        if (Sema::TemplateDeductionResult Result
523              = DeduceTemplateArguments(Context, TemplateParams,
524                                        SpecParam->getArg(I),
525                                        ArgArgs.get(I),
526                                        Info, Deduced))
527          return Result;
528
529      return Sema::TDK_Success;
530    }
531
532    //     T type::*
533    //     T T::*
534    //     T (type::*)()
535    //     type (T::*)()
536    //     type (type::*)(T)
537    //     type (T::*)(T)
538    //     T (type::*)(T)
539    //     T (T::*)()
540    //     T (T::*)(T)
541    case Type::MemberPointer: {
542      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
543      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
544      if (!MemPtrArg)
545        return Sema::TDK_NonDeducedMismatch;
546
547      if (Sema::TemplateDeductionResult Result
548            = DeduceTemplateArguments(Context, TemplateParams,
549                                      MemPtrParam->getPointeeType(),
550                                      MemPtrArg->getPointeeType(),
551                                      Info, Deduced,
552                                      TDF & TDF_IgnoreQualifiers))
553        return Result;
554
555      return DeduceTemplateArguments(Context, TemplateParams,
556                                     QualType(MemPtrParam->getClass(), 0),
557                                     QualType(MemPtrArg->getClass(), 0),
558                                     Info, Deduced, 0);
559    }
560
561    //     (clang extension)
562    //
563    //     type(^)(T)
564    //     T(^)()
565    //     T(^)(T)
566    case Type::BlockPointer: {
567      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
568      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
569
570      if (!BlockPtrArg)
571        return Sema::TDK_NonDeducedMismatch;
572
573      return DeduceTemplateArguments(Context, TemplateParams,
574                                     BlockPtrParam->getPointeeType(),
575                                     BlockPtrArg->getPointeeType(), Info,
576                                     Deduced, 0);
577    }
578
579    case Type::TypeOfExpr:
580    case Type::TypeOf:
581    case Type::Typename:
582      // No template argument deduction for these types
583      return Sema::TDK_Success;
584
585    default:
586      break;
587  }
588
589  // FIXME: Many more cases to go (to go).
590  return Sema::TDK_Success;
591}
592
593static Sema::TemplateDeductionResult
594DeduceTemplateArguments(ASTContext &Context,
595                        TemplateParameterList *TemplateParams,
596                        const TemplateArgument &Param,
597                        const TemplateArgument &Arg,
598                        Sema::TemplateDeductionInfo &Info,
599                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
600  switch (Param.getKind()) {
601  case TemplateArgument::Null:
602    assert(false && "Null template argument in parameter list");
603    break;
604
605  case TemplateArgument::Type:
606    assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
607    return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
608                                   Arg.getAsType(), Info, Deduced, 0);
609
610  case TemplateArgument::Declaration:
611    // FIXME: Implement this check
612    assert(false && "Unimplemented template argument deduction case");
613    Info.FirstArg = Param;
614    Info.SecondArg = Arg;
615    return Sema::TDK_NonDeducedMismatch;
616
617  case TemplateArgument::Integral:
618    if (Arg.getKind() == TemplateArgument::Integral) {
619      // FIXME: Zero extension + sign checking here?
620      if (*Param.getAsIntegral() == *Arg.getAsIntegral())
621        return Sema::TDK_Success;
622
623      Info.FirstArg = Param;
624      Info.SecondArg = Arg;
625      return Sema::TDK_NonDeducedMismatch;
626    }
627
628    if (Arg.getKind() == TemplateArgument::Expression) {
629      Info.FirstArg = Param;
630      Info.SecondArg = Arg;
631      return Sema::TDK_NonDeducedMismatch;
632    }
633
634    assert(false && "Type/value mismatch");
635    Info.FirstArg = Param;
636    Info.SecondArg = Arg;
637    return Sema::TDK_NonDeducedMismatch;
638
639  case TemplateArgument::Expression: {
640    if (NonTypeTemplateParmDecl *NTTP
641          = getDeducedParameterFromExpr(Param.getAsExpr())) {
642      if (Arg.getKind() == TemplateArgument::Integral)
643        // FIXME: Sign problems here
644        return DeduceNonTypeTemplateArgument(Context, NTTP,
645                                             *Arg.getAsIntegral(),
646                                             Info, Deduced);
647      if (Arg.getKind() == TemplateArgument::Expression)
648        return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
649                                             Info, Deduced);
650
651      assert(false && "Type/value mismatch");
652      Info.FirstArg = Param;
653      Info.SecondArg = Arg;
654      return Sema::TDK_NonDeducedMismatch;
655    }
656
657    // Can't deduce anything, but that's okay.
658    return Sema::TDK_Success;
659  }
660  case TemplateArgument::Pack:
661    assert(0 && "FIXME: Implement!");
662    break;
663  }
664
665  return Sema::TDK_Success;
666}
667
668static Sema::TemplateDeductionResult
669DeduceTemplateArguments(ASTContext &Context,
670                        TemplateParameterList *TemplateParams,
671                        const TemplateArgumentList &ParamList,
672                        const TemplateArgumentList &ArgList,
673                        Sema::TemplateDeductionInfo &Info,
674                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
675  assert(ParamList.size() == ArgList.size());
676  for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
677    if (Sema::TemplateDeductionResult Result
678          = DeduceTemplateArguments(Context, TemplateParams,
679                                    ParamList[I], ArgList[I],
680                                    Info, Deduced))
681      return Result;
682  }
683  return Sema::TDK_Success;
684}
685
686/// \brief Determine whether two template arguments are the same.
687static bool isSameTemplateArg(ASTContext &Context,
688                              const TemplateArgument &X,
689                              const TemplateArgument &Y) {
690  if (X.getKind() != Y.getKind())
691    return false;
692
693  switch (X.getKind()) {
694    case TemplateArgument::Null:
695      assert(false && "Comparing NULL template argument");
696      break;
697
698    case TemplateArgument::Type:
699      return Context.getCanonicalType(X.getAsType()) ==
700             Context.getCanonicalType(Y.getAsType());
701
702    case TemplateArgument::Declaration:
703      return Context.getCanonicalDecl(X.getAsDecl()) ==
704             Context.getCanonicalDecl(Y.getAsDecl());
705
706    case TemplateArgument::Integral:
707      return *X.getAsIntegral() == *Y.getAsIntegral();
708
709    case TemplateArgument::Expression:
710      // FIXME: We assume that all expressions are distinct, but we should
711      // really check their canonical forms.
712      return false;
713
714    case TemplateArgument::Pack:
715      if (X.pack_size() != Y.pack_size())
716        return false;
717
718      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
719                                        XPEnd = X.pack_end(),
720                                           YP = Y.pack_begin();
721           XP != XPEnd; ++XP, ++YP)
722        if (!isSameTemplateArg(Context, *XP, *YP))
723          return false;
724
725      return true;
726  }
727
728  return false;
729}
730
731/// \brief Helper function to build a TemplateParameter when we don't
732/// know its type statically.
733static TemplateParameter makeTemplateParameter(Decl *D) {
734  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
735    return TemplateParameter(TTP);
736  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
737    return TemplateParameter(NTTP);
738
739  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
740}
741
742/// \brief Perform template argument deduction to determine whether
743/// the given template arguments match the given class template
744/// partial specialization per C++ [temp.class.spec.match].
745Sema::TemplateDeductionResult
746Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
747                              const TemplateArgumentList &TemplateArgs,
748                              TemplateDeductionInfo &Info) {
749  // C++ [temp.class.spec.match]p2:
750  //   A partial specialization matches a given actual template
751  //   argument list if the template arguments of the partial
752  //   specialization can be deduced from the actual template argument
753  //   list (14.8.2).
754  SFINAETrap Trap(*this);
755  llvm::SmallVector<TemplateArgument, 4> Deduced;
756  Deduced.resize(Partial->getTemplateParameters()->size());
757  if (TemplateDeductionResult Result
758        = ::DeduceTemplateArguments(Context,
759                                    Partial->getTemplateParameters(),
760                                    Partial->getTemplateArgs(),
761                                    TemplateArgs, Info, Deduced))
762    return Result;
763
764  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
765                             Deduced.data(), Deduced.size());
766  if (Inst)
767    return TDK_InstantiationDepth;
768
769  // C++ [temp.deduct.type]p2:
770  //   [...] or if any template argument remains neither deduced nor
771  //   explicitly specified, template argument deduction fails.
772  TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
773                                      Deduced.size());
774  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
775    if (Deduced[I].isNull()) {
776      Decl *Param
777        = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
778      if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
779        Info.Param = TTP;
780      else if (NonTypeTemplateParmDecl *NTTP
781                 = dyn_cast<NonTypeTemplateParmDecl>(Param))
782        Info.Param = NTTP;
783      else
784        Info.Param = cast<TemplateTemplateParmDecl>(Param);
785      return TDK_Incomplete;
786    }
787
788    Builder.Append(Deduced[I]);
789  }
790
791  // Form the template argument list from the deduced template arguments.
792  TemplateArgumentList *DeducedArgumentList
793    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
794  Info.reset(DeducedArgumentList);
795
796  // Substitute the deduced template arguments into the template
797  // arguments of the class template partial specialization, and
798  // verify that the instantiated template arguments are both valid
799  // and are equivalent to the template arguments originally provided
800  // to the class template.
801  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
802  const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
803  for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
804    Decl *Param = const_cast<Decl *>(
805                    ClassTemplate->getTemplateParameters()->getParam(I));
806    TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
807                                           *DeducedArgumentList);
808    if (InstArg.isNull()) {
809      Info.Param = makeTemplateParameter(Param);
810      Info.FirstArg = PartialTemplateArgs[I];
811      return TDK_SubstitutionFailure;
812    }
813
814    if (InstArg.getKind() == TemplateArgument::Expression) {
815      // When the argument is an expression, check the expression result
816      // against the actual template parameter to get down to the canonical
817      // template argument.
818      Expr *InstExpr = InstArg.getAsExpr();
819      if (NonTypeTemplateParmDecl *NTTP
820            = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
821        if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
822          Info.Param = makeTemplateParameter(Param);
823          Info.FirstArg = PartialTemplateArgs[I];
824          return TDK_SubstitutionFailure;
825        }
826      } else if (TemplateTemplateParmDecl *TTP
827                   = dyn_cast<TemplateTemplateParmDecl>(Param)) {
828        // FIXME: template template arguments should really resolve to decls
829        DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
830        if (!DRE || CheckTemplateArgument(TTP, DRE)) {
831          Info.Param = makeTemplateParameter(Param);
832          Info.FirstArg = PartialTemplateArgs[I];
833          return TDK_SubstitutionFailure;
834        }
835      }
836    }
837
838    if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
839      Info.Param = makeTemplateParameter(Param);
840      Info.FirstArg = TemplateArgs[I];
841      Info.SecondArg = InstArg;
842      return TDK_NonDeducedMismatch;
843    }
844  }
845
846  if (Trap.hasErrorOccurred())
847    return TDK_SubstitutionFailure;
848
849  return TDK_Success;
850}
851
852/// \brief Perform template argument deduction from a function call
853/// (C++ [temp.deduct.call]).
854///
855/// \param FunctionTemplate the function template for which we are performing
856/// template argument deduction.
857///
858/// \param Args the function call arguments
859///
860/// \param NumArgs the number of arguments in Args
861///
862/// \param Specialization if template argument deduction was successful,
863/// this will be set to the function template specialization produced by
864/// template argument deduction.
865///
866/// \param Info the argument will be updated to provide additional information
867/// about template argument deduction.
868///
869/// \returns the result of template argument deduction.
870///
871/// FIXME: We will also need to pass in any explicitly-specified template
872/// arguments.
873Sema::TemplateDeductionResult
874Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
875                              Expr **Args, unsigned NumArgs,
876                              FunctionDecl *&Specialization,
877                              TemplateDeductionInfo &Info) {
878  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
879
880  // C++ [temp.deduct.call]p1:
881  //   Template argument deduction is done by comparing each function template
882  //   parameter type (call it P) with the type of the corresponding argument
883  //   of the call (call it A) as described below.
884  unsigned CheckArgs = NumArgs;
885  if (NumArgs < Function->getNumParams())
886    return TDK_TooFewArguments;
887  else if (NumArgs > Function->getNumParams()) {
888    const FunctionProtoType *Proto
889      = Function->getType()->getAsFunctionProtoType();
890    if (!Proto->isVariadic())
891      return TDK_TooManyArguments;
892
893    CheckArgs = Function->getNumParams();
894  }
895
896  // Template argument deduction for function templates in a SFINAE context.
897  // Trap any errors that might occur.
898  SFINAETrap Trap(*this);
899
900  // Deduce template arguments from the function parameters.
901  llvm::SmallVector<TemplateArgument, 4> Deduced;
902  Deduced.resize(FunctionTemplate->getTemplateParameters()->size());
903  TemplateParameterList *TemplateParams
904    = FunctionTemplate->getTemplateParameters();
905  for (unsigned I = 0; I != CheckArgs; ++I) {
906    QualType ParamType = Function->getParamDecl(I)->getType();
907    QualType ArgType = Args[I]->getType();
908
909    // C++ [temp.deduct.call]p2:
910    //   If P is not a reference type:
911    QualType CanonParamType = Context.getCanonicalType(ParamType);
912    bool ParamWasReference = isa<ReferenceType>(CanonParamType);
913    if (!ParamWasReference) {
914      //   - If A is an array type, the pointer type produced by the
915      //     array-to-pointer standard conversion (4.2) is used in place of
916      //     A for type deduction; otherwise,
917      if (ArgType->isArrayType())
918        ArgType = Context.getArrayDecayedType(ArgType);
919      //   - If A is a function type, the pointer type produced by the
920      //     function-to-pointer standard conversion (4.3) is used in place
921      //     of A for type deduction; otherwise,
922      else if (ArgType->isFunctionType())
923        ArgType = Context.getPointerType(ArgType);
924      else {
925        // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
926        //   type are ignored for type deduction.
927        QualType CanonArgType = Context.getCanonicalType(ArgType);
928        if (CanonArgType.getCVRQualifiers())
929          ArgType = CanonArgType.getUnqualifiedType();
930      }
931    }
932
933    // C++0x [temp.deduct.call]p3:
934    //   If P is a cv-qualified type, the top level cv-qualifiers of P’s type
935    //   are ignored for type deduction.
936    if (CanonParamType.getCVRQualifiers())
937      ParamType = CanonParamType.getUnqualifiedType();
938    if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) {
939      //   [...] If P is a reference type, the type referred to by P is used
940      //   for type deduction.
941      ParamType = ParamRefType->getPointeeType();
942
943      //   [...] If P is of the form T&&, where T is a template parameter, and
944      //   the argument is an lvalue, the type A& is used in place of A for
945      //   type deduction.
946      if (isa<RValueReferenceType>(ParamRefType) &&
947          ParamRefType->getAsTemplateTypeParmType() &&
948          Args[I]->isLvalue(Context) == Expr::LV_Valid)
949        ArgType = Context.getLValueReferenceType(ArgType);
950    }
951
952    // C++0x [temp.deduct.call]p4:
953    //   In general, the deduction process attempts to find template argument
954    //   values that will make the deduced A identical to A (after the type A
955    //   is transformed as described above). [...]
956    unsigned TDF = 0;
957
958    //     - If the original P is a reference type, the deduced A (i.e., the
959    //       type referred to by the reference) can be more cv-qualified than
960    //       the transformed A.
961    if (ParamWasReference)
962      TDF |= TDF_ParamWithReferenceType;
963    //     - The transformed A can be another pointer or pointer to member
964    //       type that can be converted to the deduced A via a qualification
965    //       conversion (4.4).
966    if (ArgType->isPointerType() || ArgType->isMemberPointerType())
967      TDF |= TDF_IgnoreQualifiers;
968    // FIXME: derived -> base checks
969    if (TemplateDeductionResult Result
970        = ::DeduceTemplateArguments(Context, TemplateParams,
971                                    ParamType, ArgType, Info, Deduced,
972                                    TDF))
973      return Result;
974
975    // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function
976    // pointer parameters.
977  }
978
979  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
980                             FunctionTemplate, Deduced.data(), Deduced.size());
981  if (Inst)
982    return TDK_InstantiationDepth;
983
984  // C++ [temp.deduct.type]p2:
985  //   [...] or if any template argument remains neither deduced nor
986  //   explicitly specified, template argument deduction fails.
987  TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
988  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
989    if (Deduced[I].isNull()) {
990      Decl *Param
991      = const_cast<Decl *>(TemplateParams->getParam(I));
992      if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
993        Info.Param = TTP;
994      else if (NonTypeTemplateParmDecl *NTTP
995               = dyn_cast<NonTypeTemplateParmDecl>(Param))
996        Info.Param = NTTP;
997      else
998        Info.Param = cast<TemplateTemplateParmDecl>(Param);
999      return TDK_Incomplete;
1000    }
1001
1002    Builder.Append(Deduced[I]);
1003  }
1004
1005  // Form the template argument list from the deduced template arguments.
1006  TemplateArgumentList *DeducedArgumentList
1007    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1008  Info.reset(DeducedArgumentList);
1009
1010  // Substitute the deduced template arguments into the function template
1011  // declaration to produce the function template specialization.
1012  Specialization = cast_or_null<FunctionDecl>(
1013                         InstantiateDecl(FunctionTemplate->getTemplatedDecl(),
1014                                         FunctionTemplate->getDeclContext(),
1015                                         *DeducedArgumentList));
1016
1017  if (!Specialization || Trap.hasErrorOccurred())
1018    return TDK_SubstitutionFailure;
1019
1020  // Turn the specialization into an actual function template specialization.
1021  Specialization->setFunctionTemplateSpecialization(Context,
1022                                                    FunctionTemplate,
1023                                                    Info.take());
1024  return TDK_Success;
1025}
1026
1027static void
1028MarkDeducedTemplateParameters(Sema &SemaRef,
1029                              const TemplateArgument &TemplateArg,
1030                              llvm::SmallVectorImpl<bool> &Deduced);
1031
1032/// \brief Mark the template arguments that are deduced by the given
1033/// expression.
1034static void
1035MarkDeducedTemplateParameters(const Expr *E,
1036                              llvm::SmallVectorImpl<bool> &Deduced) {
1037  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
1038  if (!E)
1039    return;
1040
1041  const NonTypeTemplateParmDecl *NTTP
1042    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1043  if (!NTTP)
1044    return;
1045
1046  Deduced[NTTP->getIndex()] = true;
1047}
1048
1049/// \brief Mark the template parameters that are deduced by the given
1050/// type.
1051static void
1052MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1053                              llvm::SmallVectorImpl<bool> &Deduced) {
1054  // Non-dependent types have nothing deducible
1055  if (!T->isDependentType())
1056    return;
1057
1058  T = SemaRef.Context.getCanonicalType(T);
1059  switch (T->getTypeClass()) {
1060  case Type::ExtQual:
1061    MarkDeducedTemplateParameters(SemaRef,
1062                              QualType(cast<ExtQualType>(T)->getBaseType(), 0),
1063                                  Deduced);
1064    break;
1065
1066  case Type::Pointer:
1067    MarkDeducedTemplateParameters(SemaRef,
1068                                  cast<PointerType>(T)->getPointeeType(),
1069                                  Deduced);
1070    break;
1071
1072  case Type::BlockPointer:
1073    MarkDeducedTemplateParameters(SemaRef,
1074                                  cast<BlockPointerType>(T)->getPointeeType(),
1075                                  Deduced);
1076    break;
1077
1078  case Type::LValueReference:
1079  case Type::RValueReference:
1080    MarkDeducedTemplateParameters(SemaRef,
1081                                  cast<ReferenceType>(T)->getPointeeType(),
1082                                  Deduced);
1083    break;
1084
1085  case Type::MemberPointer: {
1086    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1087    MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1088    MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1089                                  Deduced);
1090    break;
1091  }
1092
1093  case Type::DependentSizedArray:
1094    MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
1095                                  Deduced);
1096    // Fall through to check the element type
1097
1098  case Type::ConstantArray:
1099  case Type::IncompleteArray:
1100    MarkDeducedTemplateParameters(SemaRef,
1101                                  cast<ArrayType>(T)->getElementType(),
1102                                  Deduced);
1103    break;
1104
1105  case Type::Vector:
1106  case Type::ExtVector:
1107    MarkDeducedTemplateParameters(SemaRef,
1108                                  cast<VectorType>(T)->getElementType(),
1109                                  Deduced);
1110    break;
1111
1112  case Type::DependentSizedExtVector: {
1113    const DependentSizedExtVectorType *VecType
1114      = cast<DependentSizedExtVectorType>(T);
1115    MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1116    MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1117    break;
1118  }
1119
1120  case Type::FunctionProto: {
1121    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1122    MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1123    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1124      MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1125    break;
1126  }
1127
1128  case Type::TemplateTypeParm:
1129    Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
1130    break;
1131
1132  case Type::TemplateSpecialization: {
1133    const TemplateSpecializationType *Spec
1134      = cast<TemplateSpecializationType>(T);
1135    if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
1136      if (TemplateTemplateParmDecl *TTP
1137            = dyn_cast<TemplateTemplateParmDecl>(Template))
1138        Deduced[TTP->getIndex()] = true;
1139
1140      for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1141        MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1142
1143    break;
1144  }
1145
1146  // None of these types have any deducible parts.
1147  case Type::Builtin:
1148  case Type::FixedWidthInt:
1149  case Type::Complex:
1150  case Type::VariableArray:
1151  case Type::FunctionNoProto:
1152  case Type::Record:
1153  case Type::Enum:
1154  case Type::Typename:
1155  case Type::ObjCInterface:
1156  case Type::ObjCQualifiedInterface:
1157  case Type::ObjCObjectPointer:
1158#define TYPE(Class, Base)
1159#define ABSTRACT_TYPE(Class, Base)
1160#define DEPENDENT_TYPE(Class, Base)
1161#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1162#include "clang/AST/TypeNodes.def"
1163    break;
1164  }
1165}
1166
1167/// \brief Mark the template parameters that are deduced by this
1168/// template argument.
1169static void
1170MarkDeducedTemplateParameters(Sema &SemaRef,
1171                              const TemplateArgument &TemplateArg,
1172                              llvm::SmallVectorImpl<bool> &Deduced) {
1173  switch (TemplateArg.getKind()) {
1174  case TemplateArgument::Null:
1175  case TemplateArgument::Integral:
1176    break;
1177
1178  case TemplateArgument::Type:
1179    MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1180    break;
1181
1182  case TemplateArgument::Declaration:
1183    if (TemplateTemplateParmDecl *TTP
1184        = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1185      Deduced[TTP->getIndex()] = true;
1186    break;
1187
1188  case TemplateArgument::Expression:
1189    MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1190    break;
1191  case TemplateArgument::Pack:
1192    assert(0 && "FIXME: Implement!");
1193    break;
1194  }
1195}
1196
1197/// \brief Mark the template parameters can be deduced by the given
1198/// template argument list.
1199///
1200/// \param TemplateArgs the template argument list from which template
1201/// parameters will be deduced.
1202///
1203/// \param Deduced a bit vector whose elements will be set to \c true
1204/// to indicate when the corresponding template parameter will be
1205/// deduced.
1206void
1207Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1208                                    llvm::SmallVectorImpl<bool> &Deduced) {
1209  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1210    ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1211}
1212