SemaTemplateDeduction.cpp revision 1282029f3d37f482bbba3c38ea9da17a78d11d40
10b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ 20b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 30b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// The LLVM Compiler Infrastructure 40b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 50b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// This file is distributed under the University of Illinois Open Source 60b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// License. See LICENSE.TXT for details. 70b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===----------------------------------------------------------------------===/ 80b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 90b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// This file implements C++ template argument deduction. 100b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 110b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===----------------------------------------------------------------------===/ 120b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 130b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "Sema.h" 140b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ASTContext.h" 150b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/DeclTemplate.h" 160b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/StmtVisitor.h" 170b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/Expr.h" 180b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ExprCXX.h" 190b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/Parse/DeclSpec.h" 200b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "llvm/Support/Compiler.h" 218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor#include <algorithm> 22508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor 23508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregornamespace clang { 24508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Various flags that control template argument deduction. 25508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// 26508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// These flags can be bitwise-OR'd together. 27508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor enum TemplateDeductionFlags { 28508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief No template argument deduction flags, which indicates the 29508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// strictest results for template argument deduction (as used for, e.g., 30508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// matching class template partial specializations). 31508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_None = 0, 32508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, we are 33508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// matching with a parameter type for which the original parameter was 34508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// a reference. 35508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_ParamWithReferenceType = 0x1, 36508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, we 37508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// are matching in a case where we ignore cv-qualifiers. 38508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_IgnoreQualifiers = 0x02, 39508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, 40508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// we are matching in a case where we can perform template argument 414112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor /// deduction from a template-id of a derived class of the argument type. 421282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor TDF_DerivedClass = 0x04, 431282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// \brief Allow non-dependent types to differ, e.g., when performing 441282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// template argument deduction from a function call where conversions 451282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// may apply. 461282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor TDF_SkipNonDependent = 0x08 47508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor }; 48508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor} 49508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor 500b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregorusing namespace clang; 510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 52f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 531eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceTemplateArguments(ASTContext &Context, 54f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 55f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 56d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor const TemplateArgument &Arg, 57f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 58d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced); 59d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 60199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction 61199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that 62199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter. 63199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { 64199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) 65199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor E = IC->getSubExpr(); 661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 67199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 68199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 70199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return 0; 71199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 72199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter 74199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant. 75f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceNonTypeTemplateArgument(ASTContext &Context, 771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP, 78335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson llvm::APSInt Value, 79f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 80f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 82199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument with depth > 0"); 831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 84199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Deduced[NTTP->getIndex()].isNull()) { 8525af1ed6ffa46a333886264299be98a838097b34Anders Carlsson QualType T = NTTP->getType(); 861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8725af1ed6ffa46a333886264299be98a838097b34Anders Carlsson // FIXME: Make sure we didn't overflow our data type! 8825af1ed6ffa46a333886264299be98a838097b34Anders Carlsson unsigned AllowedBits = Context.getTypeSize(T); 8925af1ed6ffa46a333886264299be98a838097b34Anders Carlsson if (Value.getBitWidth() != AllowedBits) 9025af1ed6ffa46a333886264299be98a838097b34Anders Carlsson Value.extOrTrunc(AllowedBits); 9125af1ed6ffa46a333886264299be98a838097b34Anders Carlsson Value.setIsSigned(T->isSignedIntegerType()); 9225af1ed6ffa46a333886264299be98a838097b34Anders Carlsson 9325af1ed6ffa46a333886264299be98a838097b34Anders Carlsson Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T); 94f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 95199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 97f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral); 981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If the template argument was previously deduced to a negative value, 100199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // then our deduction fails. 101199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral(); 102335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson if (PrevValuePtr->isNegative()) { 103f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 104f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 105335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); 106f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 107f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 108f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 109335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson llvm::APSInt PrevValue = *PrevValuePtr; 110199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Value.getBitWidth() > PrevValue.getBitWidth()) 111199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor PrevValue.zext(Value.getBitWidth()); 112199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor else if (Value.getBitWidth() < PrevValue.getBitWidth()) 113199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor Value.zext(PrevValue.getBitWidth()); 114f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 115f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Value != PrevValue) { 116f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 117f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 118335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); 119f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 120f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 121f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 122f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 123199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 124199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter 126199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression. 127199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// 128199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise. 129199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 130f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 1311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceNonTypeTemplateArgument(ASTContext &Context, 132f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor NonTypeTemplateParmDecl *NTTP, 133f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Expr *Value, 134f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 135f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 137199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument with depth > 0"); 138199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert((Value->isTypeDependent() || Value->isValueDependent()) && 139199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Expression template argument must be type- or value-dependent."); 1401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 141199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Deduced[NTTP->getIndex()].isNull()) { 142199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Clone the Value? 143199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor Deduced[NTTP->getIndex()] = TemplateArgument(Value); 144f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 147199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) { 1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Okay, we deduced a constant in one case and a dependent expression 1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // in another case. FIXME: Later, we will check that instantiating the 150199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // dependent expression gives us the constant value. 151f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 152199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 154199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Compare the expressions for equality! 155f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 156199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 157199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 158f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 159f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorDeduceTemplateArguments(ASTContext &Context, 160f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Param, 161f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Arg, 162f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 163f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 164d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // FIXME: Implement template argument deduction for template 165d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // template parameters. 166d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 167f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // FIXME: this routine does not have enough information to produce 168f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // good diagnostics. 169f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 170d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 171d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor TemplateDecl *ArgDecl = Arg.getAsTemplateDecl(); 1721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 173f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (!ParamDecl || !ArgDecl) { 174f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // FIXME: fill in Info.Param/Info.FirstArg 175f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 176f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 177d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 17897fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl()); 17997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl()); 180f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (ParamDecl != ArgDecl) { 181f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // FIXME: fill in Info.Param/Info.FirstArg 182f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 183f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 184f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 185f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 186d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor} 187d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter 189de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type. 190de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 191de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Context the AST context in which this deduction occurs. 192de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 193de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing 194de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 195de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type 196de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 197de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type 198de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 199de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself 200de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 201de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments 202de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 203de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a 204de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed, 205de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons. 206de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult 207de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas GregorDeduceTemplateArguments(ASTContext &Context, 208de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateParameterList *TemplateParams, 209de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateSpecializationType *Param, 210de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType Arg, 211de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionInfo &Info, 212de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 213de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor assert(Arg->isCanonical() && "Argument type must be canonical"); 2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 215de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Check whether the template argument is a dependent template-id. 216de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // FIXME: This is untested code; it can be tested when we implement 217de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // partial ordering of class template partial specializations. 2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *SpecArg 219de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<TemplateSpecializationType>(Arg)) { 220de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 221de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 222de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, 223de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 224de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getTemplateName(), 225de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 226de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 228de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor unsigned NumArgs = Param->getNumArgs(); 2291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 230de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // FIXME: When one of the template-names refers to a 231de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // declaration with default template arguments, do we need to 232de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // fill in those default template arguments here? Most likely, 233de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // the answer is "yes", but I don't see any references. This 234de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // issue may be resolved elsewhere, because we may want to 235de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // instantiate default template arguments when we actually write 236de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // the template-id. 237de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (SpecArg->getNumArgs() != NumArgs) 238de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 240de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction on each template 241de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // argument. 242de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 243de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 244de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 245de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getArg(I), 246de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getArg(I), 247de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 248de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 250de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 251de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 253de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If the argument type is a class template specialization, we 254de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // perform template argument deduction using its template 255de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // arguments. 256de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *RecordArg = dyn_cast<RecordType>(Arg); 257de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!RecordArg) 258de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ClassTemplateSpecializationDecl *SpecArg 261de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); 262de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!SpecArg) 263de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = DeduceTemplateArguments(Context, 268de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 269de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateName(SpecArg->getSpecializedTemplate()), 270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 271de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // FIXME: Can the # of arguments in the parameter and the argument 274de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // differ due to default arguments? 275de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor unsigned NumArgs = Param->getNumArgs(); 276de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); 277de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NumArgs != ArgArgs.size()) 278de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 280de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Sema::TemplateDeductionResult Result 282de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 283de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getArg(I), 284de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ArgArgs.get(I), 285de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 286de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 288de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 289de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor} 290de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor 2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Returns a completely-unqualified array type, capturing the 292f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// qualifiers in CVRQuals. 293f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 294f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \param Context the AST context in which the array type was built. 295f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 296f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \param T a canonical type that may be an array type. 297f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 298f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \param CVRQuals will receive the set of const/volatile/restrict qualifiers 299f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// that were applied to the element type of the array. 300f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 301f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \returns if \p T is an array type, the completely unqualified array type 302f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// that corresponds to T. Otherwise, returns T. 303f290e0db835a619014538c41ed552696efc0e977Douglas Gregorstatic QualType getUnqualifiedArrayType(ASTContext &Context, QualType T, 304f290e0db835a619014538c41ed552696efc0e977Douglas Gregor unsigned &CVRQuals) { 305f290e0db835a619014538c41ed552696efc0e977Douglas Gregor assert(T->isCanonical() && "Only operates on canonical types"); 306f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (!isa<ArrayType>(T)) { 307f290e0db835a619014538c41ed552696efc0e977Douglas Gregor CVRQuals = T.getCVRQualifiers(); 308f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T.getUnqualifiedType(); 309f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 311f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { 312f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(), 313f290e0db835a619014538c41ed552696efc0e977Douglas Gregor CVRQuals); 314f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (Elt == CAT->getElementType()) 315f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T; 316f290e0db835a619014538c41ed552696efc0e977Douglas Gregor 3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Context.getConstantArrayType(Elt, CAT->getSize(), 318f290e0db835a619014538c41ed552696efc0e977Douglas Gregor CAT->getSizeModifier(), 0); 319f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 321f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { 322f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(), 323f290e0db835a619014538c41ed552696efc0e977Douglas Gregor CVRQuals); 324f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (Elt == IAT->getElementType()) 325f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T; 3261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 327f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0); 328f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 330f290e0db835a619014538c41ed552696efc0e977Douglas Gregor const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); 331f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(), 332f290e0db835a619014538c41ed552696efc0e977Douglas Gregor CVRQuals); 333f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (Elt == DSAT->getElementType()) 334f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T; 3351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 336d497206844a894a0557e927adf29b34fe960dffdAnders Carlsson return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(), 337f290e0db835a619014538c41ed552696efc0e977Douglas Gregor DSAT->getSizeModifier(), 0, 338f290e0db835a619014538c41ed552696efc0e977Douglas Gregor SourceRange()); 339f290e0db835a619014538c41ed552696efc0e977Douglas Gregor} 340f290e0db835a619014538c41ed552696efc0e977Douglas Gregor 341500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and 342500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]). 343500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 344500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Context the AST context in which this deduction occurs. 345500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 346500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 347500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 348500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type 349500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 350500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type 351500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 352500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself 353500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 354500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments 355500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 356508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed. 358500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 359500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 360500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 361500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons. 362f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 3631eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceTemplateArguments(ASTContext &Context, 364f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 365f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType ParamIn, QualType ArgIn, 366f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 367500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 368508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor unsigned TDF) { 3690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // We only want to look at the canonical types, since typedefs and 3700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // sugar are not part of template argument deduction. 371f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType Param = Context.getCanonicalType(ParamIn); 372f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType Arg = Context.getCanonicalType(ArgIn); 3730b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 374500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // C++0x [temp.deduct.call]p4 bullet 1: 375500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // - If the original P is a reference type, the deduced A (i.e., the type 3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // referred to by the reference) can be more cv-qualified than the 377500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // transformed A. 378508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned ExtraQualsOnParam 380500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers(); 381500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam); 382500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor } 3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 384f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // If the parameter type is not dependent, there is nothing to deduce. 3851282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!Param->isDependentType()) { 3861282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!(TDF & TDF_SkipNonDependent) && Param != Arg) { 3871282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 3881282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor return Sema::TDK_NonDeducedMismatch; 3891282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 3901282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 391f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 3921282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 3930b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 394199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // C++ [temp.deduct.type]p9: 3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // A template type argument T, a template template argument TT or a 3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // template non-type argument i can be deduced if P and A have one of 397199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // the following forms: 398199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // 399199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T 400199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // cv-list T 4011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateTypeParmType *TemplateTypeParm 4020b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor = Param->getAsTemplateTypeParmType()) { 403f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor unsigned Index = TemplateTypeParm->getIndex(); 404f290e0db835a619014538c41ed552696efc0e977Douglas Gregor bool RecanonicalizeArg = false; 4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4069e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // If the argument type is an array type, move the qualifiers up to the 4079e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // top level, so they can be matched with the qualifiers on the parameter. 4089e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // FIXME: address spaces, ObjC GC qualifiers 409f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (isa<ArrayType>(Arg)) { 410f290e0db835a619014538c41ed552696efc0e977Douglas Gregor unsigned CVRQuals = 0; 411f290e0db835a619014538c41ed552696efc0e977Douglas Gregor Arg = getUnqualifiedArrayType(Context, Arg, CVRQuals); 412f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (CVRQuals) { 413f290e0db835a619014538c41ed552696efc0e977Douglas Gregor Arg = Arg.getWithAdditionalQualifiers(CVRQuals); 414f290e0db835a619014538c41ed552696efc0e977Douglas Gregor RecanonicalizeArg = true; 415f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 416f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4180b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // The argument type can not be less qualified than the parameter 4190b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // type. 420508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { 421f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 422f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[Index]; 423f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = TemplateArgument(SourceLocation(), Arg); 424f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_InconsistentQuals; 425f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 4260b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 4270b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); 4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4290b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); 430f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType DeducedType = Arg.getQualifiedType(Quals); 431f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (RecanonicalizeArg) 432f290e0db835a619014538c41ed552696efc0e977Douglas Gregor DeducedType = Context.getCanonicalType(DeducedType); 4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4340b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor if (Deduced[Index].isNull()) 4350b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); 4360b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor else { 4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // C++ [temp.deduct.type]p2: 4380b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // [...] If type deduction cannot be done for any P/A pair, or if for 4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // any pair the deduction leads to more than one possible set of 4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // deduced values, or if different pairs yield different deduced 4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // values, or if any template argument remains neither deduced nor 4420b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // explicitly specified, template argument deduction fails. 443f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[Index].getAsType() != DeducedType) { 4441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Info.Param 445f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 446f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[Index]; 447f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = TemplateArgument(SourceLocation(), Arg); 448f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 449f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 4500b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 451f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 4520b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 4530b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 454f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // Set up the template argument deduction information for a failure. 455f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); 456f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); 457f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 458508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // Check the cv-qualifiers on the parameter and argument types. 459508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (!(TDF & TDF_IgnoreQualifiers)) { 460508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 461508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 462508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor return Sema::TDK_NonDeducedMismatch; 463508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } else { 464508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) 4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Sema::TDK_NonDeducedMismatch; 466508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 467508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 4680b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 469d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor switch (Param->getTypeClass()) { 470199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // No deduction possible for these types 471199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::Builtin: 472f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 474199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T * 475d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::Pointer: { 4766217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *PointerArg = Arg->getAs<PointerType>(); 477d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!PointerArg) 478f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4804112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); 481f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 482d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<PointerType>(Param)->getPointeeType(), 483d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor PointerArg->getPointeeType(), 4844112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor Info, Deduced, SubTDF); 485d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 487199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T & 488d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::LValueReference: { 4896217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); 490d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 491f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 493f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 494d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<LValueReferenceType>(Param)->getPointeeType(), 495d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 496508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 497d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 4980b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 499199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T && [C++0x] 500d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::RValueReference: { 5016217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); 502d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 503f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 505f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 506d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<RValueReferenceType>(Param)->getPointeeType(), 507d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 508508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 509d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 511199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [] (implied, but not stated explicitly) 5124d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::IncompleteArray: { 5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const IncompleteArrayType *IncompleteArrayArg = 5144d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsIncompleteArrayType(Arg); 5154d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!IncompleteArrayArg) 516f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 518f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 5194d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsIncompleteArrayType(Param)->getElementType(), 5204d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson IncompleteArrayArg->getElementType(), 521508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 5224d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 523199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 524199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [integer-constant] 5254d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::ConstantArray: { 5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayArg = 5274d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsConstantArrayType(Arg); 5284d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!ConstantArrayArg) 529f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayParm = 5324d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsConstantArrayType(Param); 5334d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) 534f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 536f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 5374d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayParm->getElementType(), 5384d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayArg->getElementType(), 539508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 5404d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 5414d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson 542199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // type [i] 543199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::DependentSizedArray: { 544199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); 545199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!ArrayArg) 546f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 548199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Check the element type of the arrays 549199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const DependentSizedArrayType *DependentArrayParm 550199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = cast<DependentSizedArrayType>(Param); 551f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 552f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 553f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor DependentArrayParm->getElementType(), 554f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor ArrayArg->getElementType(), 555508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 556f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 5571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 558199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Determine the array bound is something we can deduce. 5591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP 560199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); 561199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!NTTP) 562f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We can perform template argument deduction for the given non-type 565199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // template parameter. 5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 567199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument at depth > 0"); 5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const ConstantArrayType *ConstantArrayArg 569335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson = dyn_cast<ConstantArrayType>(ArrayArg)) { 570335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson llvm::APSInt Size(ConstantArrayArg->getSize()); 571335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson return DeduceNonTypeTemplateArgument(Context, NTTP, Size, 572f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 573335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson } 574199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (const DependentSizedArrayType *DependentArrayArg 575199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = dyn_cast<DependentSizedArrayType>(ArrayArg)) 576199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return DeduceNonTypeTemplateArgument(Context, NTTP, 577199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor DependentArrayArg->getSizeExpr(), 578f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 580199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Incomplete type does not match a dependently-sized array type 581f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 582199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(*)(T) 5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)() 5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)(T) 587a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson case Type::FunctionProto: { 5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoArg = 589a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson dyn_cast<FunctionProtoType>(Arg); 590a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson if (!FunctionProtoArg) 591f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoParam = 594a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson cast<FunctionProtoType>(Param); 595994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FunctionProtoParam->getTypeQuals() != 597994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson FunctionProtoArg->getTypeQuals()) 598f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 600994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) 601f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 603994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) 604f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 605994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 606a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check return types. 607f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 608f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 609f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getResultType(), 610f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getResultType(), 611508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 612f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 614a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { 615a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check argument types. 616f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 617f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 618f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getArgType(I), 619f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getArgType(I), 620508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 621f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 622a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 624f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 625a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 627f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template-name<T> (where template-name refers to a class template) 628d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // template-name<i> 629d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // TT<T> (TODO) 630d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // TT<i> (TODO) 631d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // TT<> (TODO) 632d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor case Type::TemplateSpecialization: { 633d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor const TemplateSpecializationType *SpecParam 634d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor = cast<TemplateSpecializationType>(Param); 6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 636de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Try to deduce template arguments from the template-id. 637de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult Result 6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg, 639de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced); 6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Result && (TDF & TDF_DerivedClass) && 642de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Result != Sema::TDK_Inconsistent) { 643de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // C++ [temp.deduct.call]p3b3: 644de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If P is a class, and P has the form template-id, then A can be a 645de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // derived class of the deduced A. Likewise, if P is a pointer to a 6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // class of the form template-id, A can be a pointer to a derived 647de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // class pointed to by the deduced A. 648de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // 649de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // More importantly: 6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // These alternatives are considered only if type deduction would 651de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // otherwise fail. 652de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) { 653de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Use data recursion to crawl through the list of base classes. 6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Visited contains the set of nodes we have already visited, while 655de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // ToVisit is our stack of records that we still need to visit. 656de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallPtrSet<const RecordType *, 8> Visited; 657de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallVector<const RecordType *, 8> ToVisit; 658de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.push_back(RecordT); 659de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor bool Successful = false; 660de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor while (!ToVisit.empty()) { 661de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Retrieve the next class in the inheritance hierarchy. 662de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *NextT = ToVisit.back(); 663de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.pop_back(); 6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 665de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If we have already seen this type, skip it. 666de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!Visited.insert(NextT)) 667de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor continue; 6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 669de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If this is a base class, try to perform template argument 670de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // deduction from it. 671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NextT != RecordT) { 672de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult BaseResult 673de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, TemplateParams, SpecParam, 674de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType(NextT, 0), Info, Deduced); 6751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 676de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If template argument deduction for this base was successful, 677de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // note that we had some success. 678de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (BaseResult == Sema::TDK_Success) 679de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Successful = true; 680de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If deduction against this base resulted in an inconsistent 681de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // set of deduced template arguments, template argument 682de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // deduction fails. 683de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor else if (BaseResult == Sema::TDK_Inconsistent) 684de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return BaseResult; 685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 687de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Visit base classes 688de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); 689de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), 690de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor BaseEnd = Next->bases_end(); 691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Base != BaseEnd; ++Base) { 6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(Base->getType()->isRecordType() && 693de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor "Base class that isn't a record?"); 6946217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ToVisit.push_back(Base->getType()->getAs<RecordType>()); 695de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 696de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 698de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Successful) 699de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 700de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 702de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 704de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 705d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor } 706d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 707637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T type::* 708637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T T::* 709637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)() 710637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)() 711637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (type::*)(T) 712637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)(T) 713637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)(T) 714637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)() 715637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)(T) 716637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::MemberPointer: { 717637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); 718637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); 719637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (!MemPtrArg) 720f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 721f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 722f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 723f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 724f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrParam->getPointeeType(), 725f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrArg->getPointeeType(), 726508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 727508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF & TDF_IgnoreQualifiers)) 728f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 729f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 730f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 731f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrParam->getClass(), 0), 732f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrArg->getClass(), 0), 733508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 734637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor } 735637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 7369a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // (clang extension) 7379a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // 7381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(^)(T) 7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)() 7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)(T) 741859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson case Type::BlockPointer: { 742859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); 743859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); 7441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 745859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson if (!BlockPtrArg) 746f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 748f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 749859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson BlockPtrParam->getPointeeType(), 750f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor BlockPtrArg->getPointeeType(), Info, 751508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Deduced, 0); 752859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson } 753859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson 754637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOfExpr: 755637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOf: 756637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::Typename: 757637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // No template argument deduction for these types 758f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 759637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 760d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor default: 761d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor break; 7620b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 7630b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 7640b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // FIXME: Many more cases to go (to go). 765f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 7660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 7670b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 768f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 7691eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceTemplateArguments(ASTContext &Context, 770f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 771f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 7720b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgument &Arg, 773f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 7740b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 7750b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor switch (Param.getKind()) { 776199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Null: 777199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Null template argument in parameter list"); 778199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor break; 7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case TemplateArgument::Type: 781199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); 782508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), 783508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Arg.getAsType(), Info, Deduced, 0); 7840b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 785199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Declaration: 786199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Implement this check 787199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Unimplemented template argument deduction case"); 788f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 789f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 790f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 792199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Integral: 793199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) { 794199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Zero extension + sign checking here? 795f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (*Param.getAsIntegral() == *Arg.getAsIntegral()) 796f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 797f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 798f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 799f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 800f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 801f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 802f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 803f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) { 804f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 805f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 806f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 807199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 808199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 809199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Type/value mismatch"); 810f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 811f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 812f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 814199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Expression: { 8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 816199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(Param.getAsExpr())) { 817199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) 818199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Sign problems here 8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return DeduceNonTypeTemplateArgument(Context, NTTP, 8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *Arg.getAsIntegral(), 821f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 822199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) 823199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), 824f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 826199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Type/value mismatch"); 827f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 828f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 829f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 830199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 832199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Can't deduce anything, but that's okay. 833f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 834199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 835d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 836d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson assert(0 && "FIXME: Implement!"); 837d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 8380b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 840f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 8410b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 8420b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 8431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult 8440b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorDeduceTemplateArguments(ASTContext &Context, 845f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 8460b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgumentList &ParamList, 8470b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgumentList &ArgList, 848f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 8490b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 8500b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(ParamList.size() == ArgList.size()); 8510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { 852f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 853f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ParamList[I], ArgList[I], 855f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced)) 856f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 8570b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 858f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 8590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 8600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 861f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same. 8621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context, 863f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &X, 864f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &Y) { 865f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.getKind() != Y.getKind()) 866f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 868f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor switch (X.getKind()) { 869f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Null: 870f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor assert(false && "Comparing NULL template argument"); 871f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor break; 8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 873f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Type: 874f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Context.getCanonicalType(X.getAsType()) == 875f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Context.getCanonicalType(Y.getAsType()); 8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 877f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Declaration: 87897fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis return X.getAsDecl()->getCanonicalDecl() == 87997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis Y.getAsDecl()->getCanonicalDecl(); 8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 881f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Integral: 882f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return *X.getAsIntegral() == *Y.getAsIntegral(); 8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 884f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Expression: 885f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // FIXME: We assume that all expressions are distinct, but we should 886f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // really check their canonical forms. 887f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 889f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Pack: 890f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.pack_size() != Y.pack_size()) 891f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (TemplateArgument::pack_iterator XP = X.pack_begin(), 8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XPEnd = X.pack_end(), 895f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor YP = Y.pack_begin(); 8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XP != XPEnd; ++XP, ++YP) 897f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, *XP, *YP)) 898f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 899f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 900f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return true; 901f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 902f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 903f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 904f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 905f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 906f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't 907f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically. 908f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) { 909f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 910f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(TTP); 911f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 912f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(NTTP); 9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 914f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 915f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 917c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// \brief Perform template argument deduction to determine whether 918c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// the given template arguments match the given class template 919c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// partial specialization per C++ [temp.class.spec.match]. 920f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorSema::TemplateDeductionResult 9210b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 922f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgumentList &TemplateArgs, 923f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateDeductionInfo &Info) { 924c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // C++ [temp.class.spec.match]p2: 925c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // A partial specialization matches a given actual template 926c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // argument list if the template arguments of the partial 927c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // specialization can be deduced from the actual template argument 928c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // list (14.8.2). 929bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor SFINAETrap Trap(*this); 9300b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 9310b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor Deduced.resize(Partial->getTemplateParameters()->size()); 932f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (TemplateDeductionResult Result 9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = ::DeduceTemplateArguments(Context, 934f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Partial->getTemplateParameters(), 9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Partial->getTemplateArgs(), 936f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateArgs, Info, Deduced)) 937f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 938637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 939637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, 940637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor Deduced.data(), Deduced.size()); 941637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (Inst) 942f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_InstantiationDepth; 943199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 94402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // C++ [temp.deduct.type]p2: 94502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // [...] or if any template argument remains neither deduced nor 94602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // explicitly specified, template argument deduction fails. 947fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), 948fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson Deduced.size()); 94902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 950f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[I].isNull()) { 9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Decl *Param 952f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I)); 953f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 954f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = TTP; 9551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump else if (NonTypeTemplateParmDecl *NTTP 956f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) 957f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 958f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor else 959f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = cast<TemplateTemplateParmDecl>(Param); 960f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_Incomplete; 961f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 96202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 963fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson Builder.Append(Deduced[I]); 96402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 96502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 96602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Form the template argument list from the deduced template arguments. 9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 968fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 969f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.reset(DeducedArgumentList); 97002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 97102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Substitute the deduced template arguments into the template 97202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // arguments of the class template partial specialization, and 97302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // verify that the instantiated template arguments are both valid 97402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // and are equivalent to the template arguments originally provided 9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to the class template. 97602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); 97702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); 97802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { 979c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor Decl *Param = const_cast<Decl *>( 980c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor ClassTemplate->getTemplateParameters()->getParam(I)); 9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgument InstArg 982357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor = Subst(PartialTemplateArgs[I], 983357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*DeducedArgumentList)); 984f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (InstArg.isNull()) { 985f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 986f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = PartialTemplateArgs[I]; 9871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 988f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 9891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 990f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (InstArg.getKind() == TemplateArgument::Expression) { 9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // When the argument is an expression, check the expression result 992f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // against the actual template parameter to get down to the canonical 993f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template argument. 994f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Expr *InstExpr = InstArg.getAsExpr(); 9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 996f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 997f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { 998f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 999f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = PartialTemplateArgs[I]; 10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 1001f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (TemplateTemplateParmDecl *TTP 1003f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor = dyn_cast<TemplateTemplateParmDecl>(Param)) { 1004f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // FIXME: template template arguments should really resolve to decls 1005f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); 1006f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!DRE || CheckTemplateArgument(TTP, DRE)) { 1007f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1008f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = PartialTemplateArgs[I]; 10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 1010f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 1011f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 101202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 10131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1014f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { 1015f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1016f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = TemplateArgs[I]; 1017f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.SecondArg = InstArg; 1018f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TDK_NonDeducedMismatch; 1019f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 102002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 102102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 1022bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor if (Trap.hasErrorOccurred()) 1023bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor return TDK_SubstitutionFailure; 1024bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor 1025f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_Success; 10260b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 1027031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 10284112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type. 10294112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) { 10301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *Spec 10314112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor = T->getAsTemplateSpecializationType()) 10324112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return Spec->getTemplateName().getAsTemplateDecl() != 0; 10331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10344112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return false; 10354112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor} 103683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 103783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the 103883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit]. 103983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 104083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit 104183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted. 104283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template 104483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments. 104583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param NumExplicitTemplateArguments the number of explicitly-specified 104783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments in @p ExplicitTemplateArguments. This value may be zero. 104883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated 105083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments. 105183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function 105383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters. 105483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 105583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template 105683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with 105783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type. 105883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 105983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be 106083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure. 106183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 106283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure 106383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition. 106483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 106583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments( 106683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplateDecl *FunctionTemplate, 106783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor const TemplateArgument *ExplicitTemplateArgs, 106883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor unsigned NumExplicitTemplateArgs, 106983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 107083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<QualType> &ParamTypes, 107183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType *FunctionType, 107283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 107383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 107483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 107583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 107683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 107783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (NumExplicitTemplateArgs == 0) { 107883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // No arguments to substitute; just copy over the parameter types and 107983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // fill in the function type. 108083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 108183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 108283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 108383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) 108483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back((*P)->getType()); 10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 108683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) 108783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor *FunctionType = Function->getType(); 108883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 108983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 10901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 109183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitution of the explicit template arguments into a function template 109283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor /// is a SFINAE context. Trap any errors that might occur. 10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 10941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 109583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p3: 10961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Template arguments that are present shall be specified in the 10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // declaration order of their corresponding template-parameters. The 109883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template argument list shall not specify more template-arguments than 10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // there are corresponding template-parameters. 11001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentListBuilder Builder(TemplateParams, 110183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs); 11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Enter a new template instantiation context where we check the 110483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments against this function template, 110583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // and then substitute them into the function parameter types. 11061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 110783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 110883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); 110983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Inst) 111083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InstantiationDepth; 11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 111283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (CheckTemplateArgumentList(FunctionTemplate, 111383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SourceLocation(), SourceLocation(), 111483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ExplicitTemplateArgs, 111583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs, 111683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SourceLocation(), 111783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor true, 111883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Builder) || Trap.hasErrorOccurred()) 111983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InvalidExplicitArguments; 11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the explicitly-specified 112283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments. 11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *ExplicitArgumentList 112483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 112583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(ExplicitArgumentList); 11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Instantiate the types of each of the function parameters given the 112883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments. 112983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 113183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 113283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) { 11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ParamType 11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstType((*P)->getType(), 1135357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 1136357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor (*P)->getLocation(), (*P)->getDeclName()); 113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ParamType.isNull() || Trap.hasErrorOccurred()) 113883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back(ParamType); 114183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 114283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 114383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // If the caller wants a full function type back, instantiate the return 114483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // type and form that function type. 114583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) { 114683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // FIXME: exception-specifications? 11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 114883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = Function->getType()->getAsFunctionProtoType(); 114983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor assert(Proto && "Function template does not have a prototype?"); 11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ResultType 1152357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor = SubstType(Proto->getResultType(), 1153357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 1154357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getTypeSpecStartLoc(), 1155357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getDeclName()); 115683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ResultType.isNull() || Trap.hasErrorOccurred()) 115783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *FunctionType = BuildFunctionType(ResultType, 116083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.data(), ParamTypes.size(), 116183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->isVariadic(), 116283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->getTypeQuals(), 116383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getLocation(), 116483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getDeclName()); 116583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType->isNull() || Trap.hasErrorOccurred()) 116683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 116783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 116983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p2: 11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Trailing template arguments that can be deduced (14.8.2) may be 11711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // omitted from the list of explicit template-arguments. If all of the 117283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments can be deduced, they may all be omitted; in this 117383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // case, the empty template argument list <> itself may also be omitted. 117483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // 117583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Take all of the explicitly-specified arguments and put them into the 11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // set of deduced template arguments. 117783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced.reserve(TemplateParams->size()); 117883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) 11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.push_back(ExplicitArgumentList->get(I)); 11801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 118183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 118283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 118383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template, 118583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming 118683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization. 11871eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult 118883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, 118983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 119083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 119183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 119283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 119583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.deduct.type]p2: 119683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // [...] or if any template argument remains neither deduced nor 119783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly specified, template argument deduction fails. 119883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); 119983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 120083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Deduced[I].isNull()) { 120183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.Param = makeTemplateParameter( 120283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor const_cast<Decl *>(TemplateParams->getParam(I))); 120383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Incomplete; 120483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Builder.Append(Deduced[I]); 120783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the deduced template arguments. 12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 121183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 121283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(DeducedArgumentList); 12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Template argument deduction for function templates in a SFINAE context. 121583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Trap any errors that might occur. 12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Enter a new template instantiation context while we instantiate the 121983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // actual function declaration. 12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 122183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 122283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); 122383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Inst) 12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_InstantiationDepth; 12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Substitute the deduced template arguments into the function template 122783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration to produce the function template specialization. 122883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization = cast_or_null<FunctionDecl>( 1229ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall SubstDecl(FunctionTemplate->getTemplatedDecl(), 1230ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall FunctionTemplate->getDeclContext(), 1231357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*DeducedArgumentList))); 123283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (!Specialization) 123383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If the template argument list is owned by the function template 123683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // specialization, release it. 123783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) 123883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.take(); 12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 124083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // There may have been an error that did not prevent us from constructing a 124183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration. Mark the declaration invalid and return with a substitution 124283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // failure. 124383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Trap.hasErrorOccurred()) { 124483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization->setInvalidDecl(true); 124583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 124683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_Success; 124983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 125083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 1251e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call 1252e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]). 1253e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1254e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing 1255e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 1256e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param HasExplicitTemplateArgs whether any template arguments were 12586db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// explicitly specified. 12596db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 12606db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 12616db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// the explicitly-specified template arguments. 12626db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 12636db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// the number of explicitly-specified template arguments in 12656db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// @p ExplicitTemplateArguments. This value may be zero. 12666db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 1267e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments 1268e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1269e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args 1270e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1271e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful, 12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 1273e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 1274e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1275e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information 1276e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction. 1277e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1278e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction. 1279e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult 1280e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 12816db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor bool HasExplicitTemplateArgs, 12826db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor const TemplateArgument *ExplicitTemplateArgs, 12836db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor unsigned NumExplicitTemplateArgs, 1284e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Expr **Args, unsigned NumArgs, 1285e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *&Specialization, 1286e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateDeductionInfo &Info) { 1287e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 12886db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor 1289e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p1: 1290e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Template argument deduction is done by comparing each function template 1291e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // parameter type (call it P) with the type of the corresponding argument 1292e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of the call (call it A) as described below. 1293e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor unsigned CheckArgs = NumArgs; 12946db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor if (NumArgs < Function->getMinRequiredArguments()) 1295e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooFewArguments; 1296e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (NumArgs > Function->getNumParams()) { 12971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 1298e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = Function->getType()->getAsFunctionProtoType(); 1299e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (!Proto->isVariadic()) 1300e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooManyArguments; 13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1302e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor CheckArgs = Function->getNumParams(); 1303e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13056db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // The types of the parameters from which we will perform template argument 13066db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // deduction. 1307e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateParameterList *TemplateParams 1308e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = FunctionTemplate->getTemplateParameters(); 13096db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 13106db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 13116db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor if (NumExplicitTemplateArgs) { 131283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionResult Result = 131383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SubstituteExplicitTemplateArguments(FunctionTemplate, 131483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ExplicitTemplateArgs, 131583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs, 131683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced, 131783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes, 131883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 0, 131983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info); 132083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Result) 132183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 13226db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } else { 13236db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Just fill in the parameter types from the function declaration. 13246db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor for (unsigned I = 0; I != CheckArgs; ++I) 13256db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor ParamTypes.push_back(Function->getParamDecl(I)->getType()); 13266db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } 13271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13286db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Deduce template arguments from the function parameters. 13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 1330e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor for (unsigned I = 0; I != CheckArgs; ++I) { 13316db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor QualType ParamType = ParamTypes[I]; 1332e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType ArgType = Args[I]->getType(); 13331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1334e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p2: 1335e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // If P is not a reference type: 1336e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType CanonParamType = Context.getCanonicalType(ParamType); 1337500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor bool ParamWasReference = isa<ReferenceType>(CanonParamType); 1338500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor if (!ParamWasReference) { 13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If A is an array type, the pointer type produced by the 13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place of 1341e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // A for type deduction; otherwise, 1342e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (ArgType->isArrayType()) 1343e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getArrayDecayedType(ArgType); 13441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If A is a function type, the pointer type produced by the 13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // function-to-pointer standard conversion (4.3) is used in place 1346e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of A for type deduction; otherwise, 1347e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (ArgType->isFunctionType()) 1348e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getPointerType(ArgType); 1349e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else { 1350e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // - If A is a cv-qualified type, the top level cv-qualifiers of A’s 1351e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // type are ignored for type deduction. 1352e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType CanonArgType = Context.getCanonicalType(ArgType); 1353e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (CanonArgType.getCVRQualifiers()) 1354e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = CanonArgType.getUnqualifiedType(); 1355e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 1356e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 13571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1358e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++0x [temp.deduct.call]p3: 1359e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // If P is a cv-qualified type, the top level cv-qualifiers of P’s type 13601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // are ignored for type deduction. 1361e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (CanonParamType.getCVRQualifiers()) 1362e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ParamType = CanonParamType.getUnqualifiedType(); 13636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { 13641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] If P is a reference type, the type referred to by P is used 13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // for type deduction. 1366e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ParamType = ParamRefType->getPointeeType(); 13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] If P is of the form T&&, where T is a template parameter, and 13691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // the argument is an lvalue, the type A& is used in place of A for 1370e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // type deduction. 1371e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (isa<RValueReferenceType>(ParamRefType) && 1372e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ParamRefType->getAsTemplateTypeParmType() && 1373e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Args[I]->isLvalue(Context) == Expr::LV_Valid) 1374e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getLValueReferenceType(ArgType); 1375e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1377e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++0x [temp.deduct.call]p4: 1378e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // In general, the deduction process attempts to find template argument 1379e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // values that will make the deduced A identical to A (after the type A 1380e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // is transformed as described above). [...] 13811282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor unsigned TDF = TDF_SkipNonDependent; 13821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1383508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // - If the original P is a reference type, the deduced A (i.e., the 1384508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // type referred to by the reference) can be more cv-qualified than 1385508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // the transformed A. 1386508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (ParamWasReference) 1387508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF |= TDF_ParamWithReferenceType; 13881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - The transformed A can be another pointer or pointer to member 13891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type that can be converted to the deduced A via a qualification 1390508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // conversion (4.4). 1391508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (ArgType->isPointerType() || ArgType->isMemberPointerType()) 1392508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF |= TDF_IgnoreQualifiers; 13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If P is a class and P has the form simple-template-id, then the 13944112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // transformed A can be a derived class of the deduced A. Likewise, 13954112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // if P is a pointer to a class of the form simple-template-id, the 13964112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // transformed A can be a pointer to a derived class pointed to by 13974112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // the deduced A. 13984112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor if (isSimpleTemplateIdType(ParamType) || 13991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (isa<PointerType>(ParamType) && 14004112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor isSimpleTemplateIdType( 14016217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ParamType->getAs<PointerType>()->getPointeeType()))) 14024112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor TDF |= TDF_DerivedClass; 14031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1404e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (TemplateDeductionResult Result 1405e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = ::DeduceTemplateArguments(Context, TemplateParams, 1406500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor ParamType, ArgType, Info, Deduced, 1407508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF)) 1408e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return Result; 14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14108fdc3c49e3c8178222a35b16426dc5a08a0efb6dDouglas Gregor // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function 14111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // pointer parameters. 141265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 141365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 141465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 1415e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 141665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 141883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 141983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 1420127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 142183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function 142283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template (C++ [temp.deduct.funcaddr]). 142383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 142483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing 142583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 142683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 14271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param HasExplicitTemplateArgs whether any template arguments were 142883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// explicitly specified. 142983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 143083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 143183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the explicitly-specified template arguments. 143283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 143383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// the number of explicitly-specified template arguments in 143583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// @p ExplicitTemplateArguments. This value may be zero. 143683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 143783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the 143883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the 143983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// function template's function type. 144083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 144183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful, 14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 144383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 144483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 144583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information 144683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction. 144783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 144883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction. 144983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 145083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 145183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor bool HasExplicitTemplateArgs, 145283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor const TemplateArgument *ExplicitTemplateArgs, 145383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor unsigned NumExplicitTemplateArgs, 145483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType ArgFunctionType, 145583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 145683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 145783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 145883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 145983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 146083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType FunctionType = Function->getType(); 14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 146283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitute any explicit template arguments. 146383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 146483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 146583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (HasExplicitTemplateArgs) { 14661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateDeductionResult Result 14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstituteExplicitTemplateArguments(FunctionTemplate, 14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ExplicitTemplateArgs, 146983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs, 14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced, ParamTypes, 147183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor &FunctionType, Info)) 147283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 1473127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 147483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 147583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Template argument deduction for function templates in a SFINAE context. 147683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Trap any errors that might occur. 14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 147983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Deduce template arguments from the function type. 14801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 148183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (TemplateDeductionResult Result 148283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = ::DeduceTemplateArguments(Context, TemplateParams, 14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FunctionType, ArgFunctionType, Info, 148483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced, 0)) 148583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 148883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 1489e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 1490e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 149165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion 149265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a 149365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization. 149465ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult 149565ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 149665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType ToType, 149765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor CXXConversionDecl *&Specialization, 149865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionInfo &Info) { 14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CXXConversionDecl *Conv 150065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); 150165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType FromType = Conv->getConversionType(); 150265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 150365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Canonicalize the types for deduction. 150465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType P = Context.getCanonicalType(FromType); 150565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType A = Context.getCanonicalType(ToType); 150665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 150765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 150865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If P is a reference type, the type referred to by P is used for 150965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type deduction. 151065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 151165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = PRef->getPointeeType(); 151265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 151365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 151465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a reference type, the type referred to by A is used 151565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // for type deduction. 151665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 151765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = ARef->getPointeeType(); 151865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p2: 151965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If A is not a reference type: 152165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else { 152265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor assert(!A->isReferenceType() && "Reference types were handled above"); 152365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 152465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is an array type, the pointer type produced by the 15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place 152665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // of P for type deduction; otherwise, 152765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (P->isArrayType()) 152865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getArrayDecayedType(P); 152965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a function type, the pointer type produced by the 153065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // function-to-pointer standard conversion (4.3) is used in 153165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // place of P for type deduction; otherwise, 153265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else if (P->isFunctionType()) 153365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getPointerType(P); 153465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a cv-qualified type, the top level cv-qualifiers of 153565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // P’s type are ignored for type deduction. 153665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else 153765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = P.getUnqualifiedType(); 153865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 153965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 154065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a cv-qualified type, the top level cv-qualifiers of A’s 154165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type are ignored for type deduction. 154265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = A.getUnqualifiedType(); 154365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor } 154465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 154565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction for function templates in a SFINAE context. 154665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Trap any errors that might occur. 15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 154865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 154965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p1: 155065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction is done by comparing the return 155165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type of the template conversion function (call it P) with the 155265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that is required as the result of the conversion (call it 155365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A) as described in 14.8.2.4. 155465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateParameterList *TemplateParams 155565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = FunctionTemplate->getTemplateParameters(); 155665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 15571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 155865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 155965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p4: 156065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // In general, the deduction process attempts to find template 156165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // argument values that will make the deduced A identical to 156265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A. However, there are two cases that allow a difference: 156365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor unsigned TDF = 0; 156465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If the original A is a reference type, A can be more 156565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // cv-qualified than the deduced A (i.e., the type referred to 156665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // by the reference) 156765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (ToType->isReferenceType()) 156865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_ParamWithReferenceType; 156965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - The deduced A can be another pointer or pointer to member 157065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that can be converted to A via a qualification 157165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // conversion. 157265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 157365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 157465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // both P and A are pointers or member pointers. In this case, we 157565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // just ignore cv-qualifiers completely). 157665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if ((P->isPointerType() && A->isPointerType()) || 157765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor (P->isMemberPointerType() && P->isMemberPointerType())) 157865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_IgnoreQualifiers; 157965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (TemplateDeductionResult Result 158065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = ::DeduceTemplateArguments(Context, TemplateParams, 158165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P, A, Info, Deduced, TDF)) 158265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 158365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 158465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 158565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 158765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Finish template argument deduction. 158865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionDecl *Spec = 0; 158965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionResult Result 159065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info); 159165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Specialization = cast_or_null<CXXConversionDecl>(Spec); 159265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 159365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 159465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 15958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types. 15968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison { 15978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor NeitherMoreQualified = 0, 15988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ParamMoreQualified, 15998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ArgMoreQualified 16008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}; 16018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing 16038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]). 16048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Context the AST context in which this deduction occurs. 16068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 16088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type 16108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type 16128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself 16148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments 16168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 16188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 16198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons. 16208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult 16218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas GregorDeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context, 16228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParameterList *TemplateParams, 16238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualType ParamIn, QualType ArgIn, 16248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Sema::TemplateDeductionInfo &Info, 16258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 16268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { 16278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQualType Param = Context.getCanonicalType(ParamIn); 16288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQualType Arg = Context.getCanonicalType(ArgIn); 16298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p5: 16318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Before the partial ordering is done, certain transformations are 16328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // performed on the types used for partial ordering: 16338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If P is a reference type, P is replaced by the type referred to. 16348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); 16358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (ParamRef) 16368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Param = ParamRef->getPointeeType(); 16378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If A is a reference type, A is replaced by the type referred to. 16398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); 16408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (ArgRef) 16418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Arg = ArgRef->getPointeeType(); 16428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (QualifierComparisons && ParamRef && ArgRef) { 16448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p6: 16458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If both P and A were reference types (before being replaced with the 16468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // type referred to above), determine which of the two types (if any) is 16478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // more cv-qualified than the other; otherwise the types are considered to 16488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // be equally cv-qualified for partial ordering purposes. The result of this 16498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // determination will be used below. 16508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // 16518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // We save this information for later, using it only when deduction 16528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // succeeds in both directions. 16538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor DeductionQualifierComparison QualifierResult = NeitherMoreQualified; 16548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 16558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierResult = ParamMoreQualified; 16568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Arg.isMoreQualifiedThan(Param)) 16578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierResult = ArgMoreQualified; 16588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons->push_back(QualifierResult); 16598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 16608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p7: 16628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Remove any top-level cv-qualifiers: 16638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If P is a cv-qualified type, P is replaced by the cv-unqualified 16648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // version of P. 16658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Param = Param.getUnqualifiedType(); 16668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If A is a cv-qualified type, A is replaced by the cv-unqualified 16678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // version of A. 16688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Arg = Arg.getUnqualifiedType(); 16698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p8: 16718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Using the resulting types P and A the deduction is then done as 16728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // described in 14.9.2.5. If deduction succeeds for a given type, the type 16738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // from the argument template is considered to be at least as specialized 16748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as the type from the parameter template. 16758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info, 16768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, TDF_None); 16778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 16788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void 16808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas GregorMarkDeducedTemplateParameters(Sema &SemaRef, QualType T, 16818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<bool> &Deduced); 16828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as 16848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2. 16858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S, 16868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT1, 16878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT2, 16888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC, 16898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { 16908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD1 = FT1->getTemplatedDecl(); 16918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD2 = FT2->getTemplatedDecl(); 16928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 16938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 16948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(Proto1 && Proto2 && "Function templates must have prototypes"); 16968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 16978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 16988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced.resize(TemplateParams->size()); 16998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p3: 17018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // The types used to determine the ordering depend on the context in which 17028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the partial ordering is done: 17038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Sema::TemplateDeductionInfo Info(S.Context); 17048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 17058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 17068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a function call, the function parameter types are 17078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used. 17088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); 17098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0; I != NumParams; ++I) 17108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, 17118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 17128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto2->getArgType(I), 17138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto1->getArgType(I), 17148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 17158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 17168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 17178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 17238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a call to a conversion operator, the return types 17248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // of the conversion function templates are used. 17258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, 17268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 17278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto2->getResultType(), 17288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto1->getResultType(), 17298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 17308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 17318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 17328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 17368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In other contexts (14.6.6.2) the function template’s function type 17378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // is used. 17388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, 17398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 17408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FD2->getType(), 17418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FD1->getType(), 17428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 17438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 17448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 17458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p11: 17508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // In most cases, all template parameters must have values in order for 17518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // deduction to succeed, but for partial ordering purposes a template 17528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter may remain without a value provided it is not used in the 17538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types being used for partial ordering. [ Note: a template parameter used 17548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // in a non-deduced context is considered used. -end note] 17558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned ArgIdx = 0, NumArgs = Deduced.size(); 17568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 17578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull()) 17588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (ArgIdx == NumArgs) { 17618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // All template arguments were deduced. FT1 is at least as specialized 17628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as FT2. 17638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 17648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // FIXME: MarkDeducedTemplateParameters needs to become 17678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // MarkUsedTemplateParameters with a flag that tells us whether to mark 17688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // template parameters that are used in non-deduced contexts. 17698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<bool, 4> UsedParameters; 17708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor UsedParameters.resize(TemplateParams->size()); 17718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 17728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 17738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); 17748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0; I != NumParams; ++I) 17758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ::MarkDeducedTemplateParameters(S, Proto2->getArgType(I), UsedParameters); 17768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 17808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ::MarkDeducedTemplateParameters(S, Proto2->getResultType(), UsedParameters); 17818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 17848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ::MarkDeducedTemplateParameters(S, FD2->getType(), UsedParameters); 17858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 17898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If this argument had no value deduced but was used in one of the types 17908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used for partial ordering, then deduction fails. 17918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 17928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 17958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 17968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 179865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Returns the more specialization function template according 179965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]). 180065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 180165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template 180265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 180365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template 180465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 18058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of 18068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates. 18071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 180865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \returns the more specialization function template. If neither 180965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL. 181065ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl * 181165ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, 181265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionTemplateDecl *FT2, 18138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC) { 181465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: Implement this 18158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; 18168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0); 18178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC, 18188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor &QualifierComparisons); 18198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1 != Better2) // We have a clear winner 18218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return Better1? FT1 : FT2; 18228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (!Better1 && !Better2) // Neither is better than the other 182465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return 0; 18258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p10: 18288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If for each type being considered a given template is at least as 18298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized for all types and more specialized for some set of types and 18308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the other template is not more specialized for any types or is not at 18318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // least as specialized for any types, then the given template is more 18328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other template. Otherwise, neither template is more 18338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other. 18348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = false; 18358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = false; 18368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { 18378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p9: 18388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If, for a given type, deduction succeeds in both directions (i.e., the 18398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types are identical after the transformations above) and if the type 18408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // from the argument template is more cv-qualified than the type from the 18418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter template (as described above) that type is considered to be 18428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // more specialized than the other. If neither type is more cv-qualified 18438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // than the other then neither type is more specialized than the other. 18448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (QualifierComparisons[I]) { 18458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case NeitherMoreQualified: 18468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 18478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case ParamMoreQualified: 18498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = true; 18508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better2) 18518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 18528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 18538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case ArgMoreQualified: 18558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = true; 18568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1) 18578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 18588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 18598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 18608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 18618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(!(Better1 && Better2) && "Should have broken out in the loop above"); 186365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (Better1) 186465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return FT1; 18658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Better2) 18668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return FT2; 18678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else 18688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 186965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 187083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 18711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 1872031a5880e19d06624551aed9d74594356f4f9db1Douglas GregorMarkDeducedTemplateParameters(Sema &SemaRef, 1873031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor const TemplateArgument &TemplateArg, 1874031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced); 1875031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1876031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template arguments that are deduced by the given 1877031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression. 18781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 18791eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpMarkDeducedTemplateParameters(const Expr *E, 1880f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 1881f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 1882031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!E) 1883031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 1884031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 18851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const NonTypeTemplateParmDecl *NTTP 1886031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 1887031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!NTTP) 1888031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 1889031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1890031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced[NTTP->getIndex()] = true; 1891031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 1892031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1893031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters that are deduced by the given 1894031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type. 18951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 1896031a5880e19d06624551aed9d74594356f4f9db1Douglas GregorMarkDeducedTemplateParameters(Sema &SemaRef, QualType T, 1897031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 1898031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Non-dependent types have nothing deducible 1899031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!T->isDependentType()) 1900031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 1901031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1902031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor T = SemaRef.Context.getCanonicalType(T); 1903031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (T->getTypeClass()) { 1904031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ExtQual: 19051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump MarkDeducedTemplateParameters(SemaRef, 1906f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor QualType(cast<ExtQualType>(T)->getBaseType(), 0), 1907031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1908031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1909031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1910031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Pointer: 1911031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, 1912f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor cast<PointerType>(T)->getPointeeType(), 1913031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1914031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1915031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1916031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::BlockPointer: 1917031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, 1918f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor cast<BlockPointerType>(T)->getPointeeType(), 1919031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1920031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1921031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1922031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::LValueReference: 1923031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::RValueReference: 1924031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, 1925f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor cast<ReferenceType>(T)->getPointeeType(), 1926031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1927031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1928031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1929031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::MemberPointer: { 1930031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 1931031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); 1932031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), 1933031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1934031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1935031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 1936031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1937031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::DependentSizedArray: 1938f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(), 1939031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1940031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Fall through to check the element type 1941031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1942031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ConstantArray: 1943031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::IncompleteArray: 1944031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, 1945f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor cast<ArrayType>(T)->getElementType(), 1946031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1947031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1948031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1949031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Vector: 1950031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ExtVector: 1951031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, 1952f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor cast<VectorType>(T)->getElementType(), 1953031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced); 1954031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1955031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 19569cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor case Type::DependentSizedExtVector: { 19579cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor const DependentSizedExtVectorType *VecType 1958f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<DependentSizedExtVectorType>(T); 19599cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced); 19609cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced); 19619cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor break; 19629cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor } 19639cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor 1964031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionProto: { 1965f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 1966031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); 1967031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) 1968031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); 1969031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1970031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 1971031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1972031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::TemplateTypeParm: 1973f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true; 1974031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1975031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1976031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::TemplateSpecialization: { 19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const TemplateSpecializationType *Spec 1978f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<TemplateSpecializationType>(T); 1979031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) 19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateTemplateParmDecl *TTP 1981031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(Template)) 1982031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced[TTP->getIndex()] = true; 19831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1984031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 1985031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); 1986031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1987031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 1988031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 1989031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 1990031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // None of these types have any deducible parts. 1991031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Builtin: 1992031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FixedWidthInt: 1993031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Complex: 1994031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::VariableArray: 1995031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionNoProto: 1996031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Record: 1997031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Enum: 1998031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Typename: 1999031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ObjCInterface: 2000d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff case Type::ObjCObjectPointer: 2001031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base) 2002031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 2003031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base) 2004031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2005031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def" 2006031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2007031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2008031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2009031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2010031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters that are deduced by this 2011031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument. 20121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2013031a5880e19d06624551aed9d74594356f4f9db1Douglas GregorMarkDeducedTemplateParameters(Sema &SemaRef, 2014031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor const TemplateArgument &TemplateArg, 2015031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 2016031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (TemplateArg.getKind()) { 2017031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Null: 2018031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Integral: 2019031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2021031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Type: 2022031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); 2023031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2024031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2025031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Declaration: 20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateTemplateParmDecl *TTP 2027031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) 2028031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor Deduced[TTP->getIndex()] = true; 2029031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2030031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2031031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Expression: 2032031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); 2033031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2034d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 2035d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson assert(0 && "FIXME: Implement!"); 2036d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 2037031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2038031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2039031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2040031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given 2041031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list. 2042031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 2043031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template 2044031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced. 2045031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 2046031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true 2047031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be 2048031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced. 20491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 2050031a5880e19d06624551aed9d74594356f4f9db1Douglas GregorSema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, 2051031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 2052031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 2053031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); 2054031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2055