SemaTemplateDeduction.cpp revision 9994a34f6cf842721ba7723edc0b9036229fe387
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 1549eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) { 1559eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor // Compare the expressions for equality 1569eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor llvm::FoldingSetNodeID ID1, ID2; 1579eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true); 1589eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor Value->Profile(ID2, Context, true); 1599eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor if (ID1 == ID2) 1609eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor return Sema::TDK_Success; 1619eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor 1629eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor // FIXME: Fill in argument mismatch information 1639eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor return Sema::TDK_NonDeducedMismatch; 1649eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor } 1659eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor 166f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 167199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 168199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 169f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 170f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorDeduceTemplateArguments(ASTContext &Context, 171f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Param, 172f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Arg, 173f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 174f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 175d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // FIXME: Implement template argument deduction for template 176d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // template parameters. 177d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 178f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // FIXME: this routine does not have enough information to produce 179f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // good diagnostics. 180f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 181d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 182d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor TemplateDecl *ArgDecl = Arg.getAsTemplateDecl(); 1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 184f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (!ParamDecl || !ArgDecl) { 185f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // FIXME: fill in Info.Param/Info.FirstArg 186f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 187f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 188d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 18997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl()); 19097fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl()); 191f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (ParamDecl != ArgDecl) { 192f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // FIXME: fill in Info.Param/Info.FirstArg 193f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 194f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 195f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 196f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 197d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor} 198d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter 200de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type. 201de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 202de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Context the AST context in which this deduction occurs. 203de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 204de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing 205de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 206de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type 207de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 208de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type 209de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 210de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself 211de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 212de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments 213de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 214de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a 215de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed, 216de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons. 217de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult 218de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas GregorDeduceTemplateArguments(ASTContext &Context, 219de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateParameterList *TemplateParams, 220de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateSpecializationType *Param, 221de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType Arg, 222de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionInfo &Info, 223de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 224467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall assert(Arg.isCanonical() && "Argument type must be canonical"); 2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 226de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Check whether the template argument is a dependent template-id. 227de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // FIXME: This is untested code; it can be tested when we implement 228de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // partial ordering of class template partial specializations. 2291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *SpecArg 230de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<TemplateSpecializationType>(Arg)) { 231de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 232de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 233de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, 234de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 235de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getTemplateName(), 236de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 237de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 239de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor unsigned NumArgs = Param->getNumArgs(); 2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 241de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // FIXME: When one of the template-names refers to a 242de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // declaration with default template arguments, do we need to 243de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // fill in those default template arguments here? Most likely, 244de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // the answer is "yes", but I don't see any references. This 245de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // issue may be resolved elsewhere, because we may want to 246de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // instantiate default template arguments when we actually write 247de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // the template-id. 248de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (SpecArg->getNumArgs() != NumArgs) 249de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 251de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction on each template 252de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // argument. 253de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 254de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 255de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 256de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getArg(I), 257de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getArg(I), 258de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 259de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 261de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 262de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 264de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If the argument type is a class template specialization, we 265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // perform template argument deduction using its template 266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // arguments. 267de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *RecordArg = dyn_cast<RecordType>(Arg); 268de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!RecordArg) 269de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ClassTemplateSpecializationDecl *SpecArg 272de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); 273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!SpecArg) 274de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 276de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 277de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = DeduceTemplateArguments(Context, 279de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 280de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateName(SpecArg->getSpecializedTemplate()), 281de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 282de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 284de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // FIXME: Can the # of arguments in the parameter and the argument 285de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // differ due to default arguments? 286de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor unsigned NumArgs = Param->getNumArgs(); 287de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); 288de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NumArgs != ArgArgs.size()) 289de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 2901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 291de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Sema::TemplateDeductionResult Result 293de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 294de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getArg(I), 295de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ArgArgs.get(I), 296de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 297de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 299de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 300de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor} 301de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor 3021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Returns a completely-unqualified array type, capturing the 3030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// qualifiers in Quals. 304f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 305f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \param Context the AST context in which the array type was built. 306f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 307f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \param T a canonical type that may be an array type. 308f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 3090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// \param Quals will receive the full set of qualifiers that were 3100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// applied to the element type of the array. 311f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// 312f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// \returns if \p T is an array type, the completely unqualified array type 313f290e0db835a619014538c41ed552696efc0e977Douglas Gregor/// that corresponds to T. Otherwise, returns T. 314f290e0db835a619014538c41ed552696efc0e977Douglas Gregorstatic QualType getUnqualifiedArrayType(ASTContext &Context, QualType T, 3150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Qualifiers &Quals) { 316467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall assert(T.isCanonical() && "Only operates on canonical types"); 317f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (!isa<ArrayType>(T)) { 3180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Quals = T.getQualifiers(); 319f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T.getUnqualifiedType(); 320f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall assert(!T.hasQualifiers() && "canonical array type has qualifiers!"); 3230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall 324f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { 325f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(), 3260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Quals); 327f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (Elt == CAT->getElementType()) 328f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T; 329f290e0db835a619014538c41ed552696efc0e977Douglas Gregor 3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Context.getConstantArrayType(Elt, CAT->getSize(), 331f290e0db835a619014538c41ed552696efc0e977Douglas Gregor CAT->getSizeModifier(), 0); 332f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 334f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { 335f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(), 3360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Quals); 337f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (Elt == IAT->getElementType()) 338f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T; 3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 340f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0); 341f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 343f290e0db835a619014538c41ed552696efc0e977Douglas Gregor const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); 344f290e0db835a619014538c41ed552696efc0e977Douglas Gregor QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(), 3450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Quals); 346f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (Elt == DSAT->getElementType()) 347f290e0db835a619014538c41ed552696efc0e977Douglas Gregor return T; 3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 349d497206844a894a0557e927adf29b34fe960dffdAnders Carlsson return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(), 350f290e0db835a619014538c41ed552696efc0e977Douglas Gregor DSAT->getSizeModifier(), 0, 351f290e0db835a619014538c41ed552696efc0e977Douglas Gregor SourceRange()); 352f290e0db835a619014538c41ed552696efc0e977Douglas Gregor} 353f290e0db835a619014538c41ed552696efc0e977Douglas Gregor 354500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and 355500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]). 356500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 357500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Context the AST context in which this deduction occurs. 358500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 359500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 360500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 361500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type 362500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 363500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type 364500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 365500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself 366500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 367500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments 368500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 369508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed. 371500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 372500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 373500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 374500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons. 375f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 3761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceTemplateArguments(ASTContext &Context, 377f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 378f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType ParamIn, QualType ArgIn, 379f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 380500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 381508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor unsigned TDF) { 3820b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // We only want to look at the canonical types, since typedefs and 3830b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // sugar are not part of template argument deduction. 384f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType Param = Context.getCanonicalType(ParamIn); 385f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType Arg = Context.getCanonicalType(ArgIn); 3860b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 387500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // C++0x [temp.deduct.call]p4 bullet 1: 388500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // - If the original P is a reference type, the deduced A (i.e., the type 3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // referred to by the reference) can be more cv-qualified than the 390500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // transformed A. 391508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 3920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Qualifiers Quals = Param.getQualifiers(); 3930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Quals.setCVRQualifiers(Quals.getCVRQualifiers() & Arg.getCVRQualifiers()); 3940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Param = Context.getQualifiedType(Param.getUnqualifiedType(), Quals); 395500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor } 3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 397f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // If the parameter type is not dependent, there is nothing to deduce. 3981282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!Param->isDependentType()) { 3991282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!(TDF & TDF_SkipNonDependent) && Param != Arg) { 4001282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 4011282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor return Sema::TDK_NonDeducedMismatch; 4021282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 4031282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 404f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 4051282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 4060b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 407199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // C++ [temp.deduct.type]p9: 4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // A template type argument T, a template template argument TT or a 4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // template non-type argument i can be deduced if P and A have one of 410199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // the following forms: 411199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // 412199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T 413199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // cv-list T 4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateTypeParmType *TemplateTypeParm 415183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Param->getAs<TemplateTypeParmType>()) { 416f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor unsigned Index = TemplateTypeParm->getIndex(); 417f290e0db835a619014538c41ed552696efc0e977Douglas Gregor bool RecanonicalizeArg = false; 4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4199e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // If the argument type is an array type, move the qualifiers up to the 4209e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // top level, so they can be matched with the qualifiers on the parameter. 4219e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // FIXME: address spaces, ObjC GC qualifiers 422f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (isa<ArrayType>(Arg)) { 4230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Qualifiers Quals; 4240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Arg = getUnqualifiedArrayType(Context, Arg, Quals); 4250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (Quals) { 4260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Arg = Context.getQualifiedType(Arg, Quals); 427f290e0db835a619014538c41ed552696efc0e977Douglas Gregor RecanonicalizeArg = true; 428f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 429f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4310b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // The argument type can not be less qualified than the parameter 4320b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // type. 433508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { 434f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 435f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[Index]; 436f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = TemplateArgument(SourceLocation(), Arg); 437f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_InconsistentQuals; 438f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 4390b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 4400b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); 4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall QualType DeducedType = Arg; 4430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall DeducedType.removeCVRQualifiers(Param.getCVRQualifiers()); 444f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (RecanonicalizeArg) 445f290e0db835a619014538c41ed552696efc0e977Douglas Gregor DeducedType = Context.getCanonicalType(DeducedType); 4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4470b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor if (Deduced[Index].isNull()) 4480b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); 4490b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor else { 4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // C++ [temp.deduct.type]p2: 4510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // [...] If type deduction cannot be done for any P/A pair, or if for 4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // any pair the deduction leads to more than one possible set of 4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // deduced values, or if different pairs yield different deduced 4541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // values, or if any template argument remains neither deduced nor 4550b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // explicitly specified, template argument deduction fails. 456f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[Index].getAsType() != DeducedType) { 4571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Info.Param 458f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 459f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[Index]; 460f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = TemplateArgument(SourceLocation(), Arg); 461f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 462f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 4630b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 464f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 4650b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 4660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 467f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // Set up the template argument deduction information for a failure. 468f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); 469f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); 470f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 471508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // Check the cv-qualifiers on the parameter and argument types. 472508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (!(TDF & TDF_IgnoreQualifiers)) { 473508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 474508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 475508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor return Sema::TDK_NonDeducedMismatch; 476508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } else { 477508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) 4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Sema::TDK_NonDeducedMismatch; 479508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 480508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 4810b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 482d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor switch (Param->getTypeClass()) { 483199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // No deduction possible for these types 484199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::Builtin: 485f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 487199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T * 488d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::Pointer: { 4896217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *PointerArg = Arg->getAs<PointerType>(); 490d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!PointerArg) 491f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4934112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); 494f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 495d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<PointerType>(Param)->getPointeeType(), 496d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor PointerArg->getPointeeType(), 4974112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor Info, Deduced, SubTDF); 498d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 500199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T & 501d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::LValueReference: { 5026217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); 503d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 504f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 506f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 507d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<LValueReferenceType>(Param)->getPointeeType(), 508d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 509508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 510d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 5110b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 512199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T && [C++0x] 513d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::RValueReference: { 5146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); 515d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 516f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 518f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 519d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<RValueReferenceType>(Param)->getPointeeType(), 520d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 521508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 522d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 5231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 524199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [] (implied, but not stated explicitly) 5254d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::IncompleteArray: { 5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const IncompleteArrayType *IncompleteArrayArg = 5274d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsIncompleteArrayType(Arg); 5284d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!IncompleteArrayArg) 529f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 531f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 5324d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsIncompleteArrayType(Param)->getElementType(), 5334d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson IncompleteArrayArg->getElementType(), 534508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 5354d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 536199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 537199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [integer-constant] 5384d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::ConstantArray: { 5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayArg = 5404d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsConstantArrayType(Arg); 5414d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!ConstantArrayArg) 542f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayParm = 5454d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson Context.getAsConstantArrayType(Param); 5464d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) 547f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 549f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 5504d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayParm->getElementType(), 5514d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayArg->getElementType(), 552508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 5534d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 5544d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson 555199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // type [i] 556199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::DependentSizedArray: { 557199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); 558199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!ArrayArg) 559f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 561199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Check the element type of the arrays 562199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const DependentSizedArrayType *DependentArrayParm 563199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = cast<DependentSizedArrayType>(Param); 564f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 565f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 566f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor DependentArrayParm->getElementType(), 567f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor ArrayArg->getElementType(), 568508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 569f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 571199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Determine the array bound is something we can deduce. 5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP 573199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); 574199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!NTTP) 575f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We can perform template argument deduction for the given non-type 578199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // template parameter. 5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 580199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument at depth > 0"); 5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const ConstantArrayType *ConstantArrayArg 582335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson = dyn_cast<ConstantArrayType>(ArrayArg)) { 583335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson llvm::APSInt Size(ConstantArrayArg->getSize()); 584335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson return DeduceNonTypeTemplateArgument(Context, NTTP, Size, 585f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 586335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson } 587199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (const DependentSizedArrayType *DependentArrayArg 588199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = dyn_cast<DependentSizedArrayType>(ArrayArg)) 589199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return DeduceNonTypeTemplateArgument(Context, NTTP, 590199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor DependentArrayArg->getSizeExpr(), 591f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 593199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Incomplete type does not match a dependently-sized array type 594f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 595199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(*)(T) 5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)() 5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)(T) 600a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson case Type::FunctionProto: { 6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoArg = 602a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson dyn_cast<FunctionProtoType>(Arg); 603a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson if (!FunctionProtoArg) 604f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoParam = 607a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson cast<FunctionProtoType>(Param); 608994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FunctionProtoParam->getTypeQuals() != 610994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson FunctionProtoArg->getTypeQuals()) 611f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 613994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) 614f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 616994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) 617f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 618994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 619a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check return types. 620f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 621f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 622f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getResultType(), 623f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getResultType(), 624508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 625f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 627a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { 628a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check argument types. 629f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 630f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 631f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getArgType(I), 632f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getArgType(I), 633508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 634f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 635a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 637f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 638a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 640f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template-name<T> (where template-name refers to a class template) 641d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // template-name<i> 642d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // TT<T> (TODO) 643d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // TT<i> (TODO) 644d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // TT<> (TODO) 645d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor case Type::TemplateSpecialization: { 646d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor const TemplateSpecializationType *SpecParam 647d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor = cast<TemplateSpecializationType>(Param); 6481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 649de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Try to deduce template arguments from the template-id. 650de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult Result 6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg, 652de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced); 6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6544a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor if (Result && (TDF & TDF_DerivedClass)) { 655de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // C++ [temp.deduct.call]p3b3: 656de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If P is a class, and P has the form template-id, then A can be a 657de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // derived class of the deduced A. Likewise, if P is a pointer to a 6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // class of the form template-id, A can be a pointer to a derived 659de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // class pointed to by the deduced A. 660de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // 661de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // More importantly: 6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // These alternatives are considered only if type deduction would 663de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // otherwise fail. 664de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) { 665de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Use data recursion to crawl through the list of base classes. 6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Visited contains the set of nodes we have already visited, while 667de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // ToVisit is our stack of records that we still need to visit. 668de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallPtrSet<const RecordType *, 8> Visited; 669de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallVector<const RecordType *, 8> ToVisit; 670de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.push_back(RecordT); 671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor bool Successful = false; 672de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor while (!ToVisit.empty()) { 673de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Retrieve the next class in the inheritance hierarchy. 674de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *NextT = ToVisit.back(); 675de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.pop_back(); 6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 677de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If we have already seen this type, skip it. 678de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!Visited.insert(NextT)) 679de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor continue; 6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 681de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If this is a base class, try to perform template argument 682de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // deduction from it. 683de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NextT != RecordT) { 684de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult BaseResult 685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = DeduceTemplateArguments(Context, TemplateParams, SpecParam, 686de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType(NextT, 0), Info, Deduced); 6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 688de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If template argument deduction for this base was successful, 689de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // note that we had some success. 690de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (BaseResult == Sema::TDK_Success) 691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Successful = true; 692de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 6931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 694de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Visit base classes 695de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); 696de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), 697de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor BaseEnd = Next->bases_end(); 6989994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl Base != BaseEnd; ++Base) { 6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(Base->getType()->isRecordType() && 700de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor "Base class that isn't a record?"); 7016217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ToVisit.push_back(Base->getType()->getAs<RecordType>()); 702de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 703de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 705de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Successful) 706de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 707de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 709de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 711de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 712d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor } 713d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 714637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T type::* 715637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T T::* 716637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)() 717637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)() 718637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (type::*)(T) 719637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)(T) 720637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)(T) 721637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)() 722637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)(T) 723637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::MemberPointer: { 724637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); 725637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); 726637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (!MemPtrArg) 727f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 728f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 729f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 730f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 731f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrParam->getPointeeType(), 732f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrArg->getPointeeType(), 733508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 734508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF & TDF_IgnoreQualifiers)) 735f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 736f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 737f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 738f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrParam->getClass(), 0), 739f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrArg->getClass(), 0), 740508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 741637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor } 742637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 7439a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // (clang extension) 7449a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // 7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(^)(T) 7461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)() 7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)(T) 748859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson case Type::BlockPointer: { 749859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); 750859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); 7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 752859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson if (!BlockPtrArg) 753f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 755f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, 756859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson BlockPtrParam->getPointeeType(), 757f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor BlockPtrArg->getPointeeType(), Info, 758508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Deduced, 0); 759859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson } 760859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson 761637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOfExpr: 762637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOf: 763637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::Typename: 764637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // No template argument deduction for these types 765f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 766637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 767d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor default: 768d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor break; 7690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 7700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 7710b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // FIXME: Many more cases to go (to go). 772f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 7730b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 7740b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 775f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 7761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDeduceTemplateArguments(ASTContext &Context, 777f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 778f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 7790b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgument &Arg, 780f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 7810b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 7820b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor switch (Param.getKind()) { 783199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Null: 784199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Null template argument in parameter list"); 785199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor break; 7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case TemplateArgument::Type: 788199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); 789508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), 790508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Arg.getAsType(), Info, Deduced, 0); 7910b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 792199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Declaration: 793199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Implement this check 794199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Unimplemented template argument deduction case"); 795f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 796f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 797f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 799199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Integral: 800199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) { 801199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Zero extension + sign checking here? 802f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (*Param.getAsIntegral() == *Arg.getAsIntegral()) 803f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 804f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 805f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 806f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 807f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 808f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 809f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 810f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) { 811f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 812f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 813f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 814199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 815199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 816199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Type/value mismatch"); 817f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 818f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 819f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 821199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Expression: { 8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 823199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(Param.getAsExpr())) { 824199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) 825199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // FIXME: Sign problems here 8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return DeduceNonTypeTemplateArgument(Context, NTTP, 8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *Arg.getAsIntegral(), 828f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 829199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) 830199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), 831f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 833199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Type/value mismatch"); 834f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 835f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 836f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 837199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 839199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Can't deduce anything, but that's okay. 840f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 841199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 842d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 843d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson assert(0 && "FIXME: Implement!"); 844d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 8450b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 8461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 847f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 8480b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 8490b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult 8510b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorDeduceTemplateArguments(ASTContext &Context, 852f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 8530b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgumentList &ParamList, 8540b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgumentList &ArgList, 855f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 8560b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 8570b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(ParamList.size() == ArgList.size()); 8580b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { 859f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 860f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = DeduceTemplateArguments(Context, TemplateParams, 8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ParamList[I], ArgList[I], 862f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced)) 863f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 8640b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 865f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 8660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 8670b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 868f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same. 8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context, 870f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &X, 871f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &Y) { 872f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.getKind() != Y.getKind()) 873f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 875f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor switch (X.getKind()) { 876f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Null: 877f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor assert(false && "Comparing NULL template argument"); 878f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor break; 8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 880f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Type: 881f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Context.getCanonicalType(X.getAsType()) == 882f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Context.getCanonicalType(Y.getAsType()); 8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 884f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Declaration: 88597fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis return X.getAsDecl()->getCanonicalDecl() == 88697fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis Y.getAsDecl()->getCanonicalDecl(); 8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 888f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Integral: 889f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return *X.getAsIntegral() == *Y.getAsIntegral(); 8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 891f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Expression: 892f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // FIXME: We assume that all expressions are distinct, but we should 893f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // really check their canonical forms. 894f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 896f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Pack: 897f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.pack_size() != Y.pack_size()) 898f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (TemplateArgument::pack_iterator XP = X.pack_begin(), 9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XPEnd = X.pack_end(), 902f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor YP = Y.pack_begin(); 9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XP != XPEnd; ++XP, ++YP) 904f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, *XP, *YP)) 905f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 906f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 907f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return true; 908f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 909f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 910f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 911f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 912f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 913f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't 914f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically. 915f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) { 916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 917f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(TTP); 918f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 919f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(NTTP); 9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 921f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 922f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 923f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 924c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// \brief Perform template argument deduction to determine whether 925c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// the given template arguments match the given class template 926c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// partial specialization per C++ [temp.class.spec.match]. 927f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorSema::TemplateDeductionResult 9280b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 929f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgumentList &TemplateArgs, 930f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateDeductionInfo &Info) { 931c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // C++ [temp.class.spec.match]p2: 932c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // A partial specialization matches a given actual template 933c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // argument list if the template arguments of the partial 934c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // specialization can be deduced from the actual template argument 935c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // list (14.8.2). 936bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor SFINAETrap Trap(*this); 9370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 9380b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor Deduced.resize(Partial->getTemplateParameters()->size()); 939f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (TemplateDeductionResult Result 9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = ::DeduceTemplateArguments(Context, 941f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Partial->getTemplateParameters(), 9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Partial->getTemplateArgs(), 943f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateArgs, Info, Deduced)) 944f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 945637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 946637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, 947637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor Deduced.data(), Deduced.size()); 948637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (Inst) 949f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_InstantiationDepth; 950199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 95102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // C++ [temp.deduct.type]p2: 95202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // [...] or if any template argument remains neither deduced nor 95302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // explicitly specified, template argument deduction fails. 954fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), 955fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson Deduced.size()); 95602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 957f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[I].isNull()) { 9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Decl *Param 959bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor = const_cast<NamedDecl *>( 960bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Partial->getTemplateParameters()->getParam(I)); 961f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 962f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = TTP; 9631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump else if (NonTypeTemplateParmDecl *NTTP 964f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) 965f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 966f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor else 967f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = cast<TemplateTemplateParmDecl>(Param); 968f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_Incomplete; 969f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 97002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 971fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson Builder.Append(Deduced[I]); 97202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 97302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 97402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Form the template argument list from the deduced template arguments. 9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 976fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 977f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.reset(DeducedArgumentList); 97802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 97902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Substitute the deduced template arguments into the template 98002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // arguments of the class template partial specialization, and 98102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // verify that the instantiated template arguments are both valid 98202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // and are equivalent to the template arguments originally provided 9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to the class template. 98402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); 98502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); 98602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { 987bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Decl *Param = const_cast<NamedDecl *>( 988c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor ClassTemplate->getTemplateParameters()->getParam(I)); 9891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgument InstArg 990357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor = Subst(PartialTemplateArgs[I], 991357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*DeducedArgumentList)); 992f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (InstArg.isNull()) { 993f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 994f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = PartialTemplateArgs[I]; 9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 996f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 998f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (InstArg.getKind() == TemplateArgument::Expression) { 9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // When the argument is an expression, check the expression result 1000f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // against the actual template parameter to get down to the canonical 1001f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template argument. 1002f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Expr *InstExpr = InstArg.getAsExpr(); 10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 1004f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 1005f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { 1006f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1007f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = PartialTemplateArgs[I]; 10081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 1009f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 10101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (TemplateTemplateParmDecl *TTP 1011f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor = dyn_cast<TemplateTemplateParmDecl>(Param)) { 1012f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // FIXME: template template arguments should really resolve to decls 1013f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); 1014f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!DRE || CheckTemplateArgument(TTP, DRE)) { 1015f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1016f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = PartialTemplateArgs[I]; 10171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 1018f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 1019f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 102002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 10211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1022f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { 1023f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1024f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = TemplateArgs[I]; 1025f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.SecondArg = InstArg; 1026f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TDK_NonDeducedMismatch; 1027f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 102802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 102902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 1030bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor if (Trap.hasErrorOccurred()) 1031bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor return TDK_SubstitutionFailure; 1032bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor 1033f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_Success; 10340b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 1035031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 10364112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type. 10374112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) { 10381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *Spec 1039183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = T->getAs<TemplateSpecializationType>()) 10404112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return Spec->getTemplateName().getAsTemplateDecl() != 0; 10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10424112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return false; 10434112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor} 104483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 104583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the 104683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit]. 104783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 104883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit 104983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted. 105083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template 105283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments. 105383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param NumExplicitTemplateArguments the number of explicitly-specified 105583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments in @p ExplicitTemplateArguments. This value may be zero. 105683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated 105883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments. 105983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function 106183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters. 106283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 106383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template 106483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with 106583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type. 106683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 106783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be 106883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure. 106983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 107083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure 107183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition. 107283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 107383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments( 107483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplateDecl *FunctionTemplate, 107583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor const TemplateArgument *ExplicitTemplateArgs, 107683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor unsigned NumExplicitTemplateArgs, 107783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 107883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<QualType> &ParamTypes, 107983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType *FunctionType, 108083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 108183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 108283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 108383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 108483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 108583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (NumExplicitTemplateArgs == 0) { 108683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // No arguments to substitute; just copy over the parameter types and 108783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // fill in the function type. 108883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 108983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 109083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 109183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) 109283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back((*P)->getType()); 10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 109483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) 109583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor *FunctionType = Function->getType(); 109683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 109783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 109983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitution of the explicit template arguments into a function template 110083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor /// is a SFINAE context. Trap any errors that might occur. 11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 110383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p3: 11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Template arguments that are present shall be specified in the 11051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // declaration order of their corresponding template-parameters. The 110683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template argument list shall not specify more template-arguments than 11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // there are corresponding template-parameters. 11081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentListBuilder Builder(TemplateParams, 110983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs); 11101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Enter a new template instantiation context where we check the 111283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments against this function template, 111383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // and then substitute them into the function parameter types. 11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 111583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 111683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); 111783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Inst) 111883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InstantiationDepth; 11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (CheckTemplateArgumentList(FunctionTemplate, 112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SourceLocation(), SourceLocation(), 112283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ExplicitTemplateArgs, 112383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs, 112483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SourceLocation(), 112583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor true, 112683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Builder) || Trap.hasErrorOccurred()) 112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InvalidExplicitArguments; 11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the explicitly-specified 113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments. 11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *ExplicitArgumentList 113283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 113383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(ExplicitArgumentList); 11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Instantiate the types of each of the function parameters given the 113683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments. 113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 113883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 113983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 114083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) { 11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ParamType 11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstType((*P)->getType(), 1143357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 1144357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor (*P)->getLocation(), (*P)->getDeclName()); 114583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ParamType.isNull() || Trap.hasErrorOccurred()) 114683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back(ParamType); 114983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 115083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 115183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // If the caller wants a full function type back, instantiate the return 115283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // type and form that function type. 115383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) { 115483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // FIXME: exception-specifications? 11551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 1156183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Function->getType()->getAs<FunctionProtoType>(); 115783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor assert(Proto && "Function template does not have a prototype?"); 11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ResultType 1160357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor = SubstType(Proto->getResultType(), 1161357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 1162357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getTypeSpecStartLoc(), 1163357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getDeclName()); 116483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ResultType.isNull() || Trap.hasErrorOccurred()) 116583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *FunctionType = BuildFunctionType(ResultType, 116883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.data(), ParamTypes.size(), 116983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->isVariadic(), 117083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->getTypeQuals(), 117183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getLocation(), 117283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getDeclName()); 117383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType->isNull() || Trap.hasErrorOccurred()) 117483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 117583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 117783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p2: 11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Trailing template arguments that can be deduced (14.8.2) may be 11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // omitted from the list of explicit template-arguments. If all of the 118083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments can be deduced, they may all be omitted; in this 118183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // case, the empty template argument list <> itself may also be omitted. 118283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // 118383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Take all of the explicitly-specified arguments and put them into the 11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // set of deduced template arguments. 118583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced.reserve(TemplateParams->size()); 118683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) 11871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.push_back(ExplicitArgumentList->get(I)); 11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 118983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 119083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 119183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 11921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template, 119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming 119483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization. 11951eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult 119683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, 119783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 119883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 119983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 120083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 120183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.deduct.type]p2: 120483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // [...] or if any template argument remains neither deduced nor 120583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly specified, template argument deduction fails. 120683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); 120783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 120883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Deduced[I].isNull()) { 120983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.Param = makeTemplateParameter( 1210bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor const_cast<NamedDecl *>(TemplateParams->getParam(I))); 121183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Incomplete; 121283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Builder.Append(Deduced[I]); 121583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the deduced template arguments. 12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 121983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 122083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(DeducedArgumentList); 12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 122283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Template argument deduction for function templates in a SFINAE context. 122383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Trap any errors that might occur. 12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 122683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Enter a new template instantiation context while we instantiate the 122783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // actual function declaration. 12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 122983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 123083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); 123183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Inst) 12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_InstantiationDepth; 12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Substitute the deduced template arguments into the function template 123583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration to produce the function template specialization. 123683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization = cast_or_null<FunctionDecl>( 1237ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall SubstDecl(FunctionTemplate->getTemplatedDecl(), 1238ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall FunctionTemplate->getDeclContext(), 1239357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*DeducedArgumentList))); 124083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (!Specialization) 124183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1243f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == 1244f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor FunctionTemplate->getCanonicalDecl()); 1245f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor 12461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If the template argument list is owned by the function template 124783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // specialization, release it. 124883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) 124983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.take(); 12501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 125183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // There may have been an error that did not prevent us from constructing a 125283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration. Mark the declaration invalid and return with a substitution 125383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // failure. 125483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Trap.hasErrorOccurred()) { 125583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization->setInvalidDecl(true); 125683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 125783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_Success; 126083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 126183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 1262e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call 1263e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]). 1264e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1265e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing 1266e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 1267e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param HasExplicitTemplateArgs whether any template arguments were 12696db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// explicitly specified. 12706db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 12716db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 12726db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// the explicitly-specified template arguments. 12736db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 12746db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 12751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// the number of explicitly-specified template arguments in 12766db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// @p ExplicitTemplateArguments. This value may be zero. 12776db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 1278e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments 1279e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1280e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args 1281e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1282e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful, 12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 1284e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 1285e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1286e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information 1287e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction. 1288e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1289e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction. 1290e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult 1291e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 12926db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor bool HasExplicitTemplateArgs, 12936db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor const TemplateArgument *ExplicitTemplateArgs, 12946db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor unsigned NumExplicitTemplateArgs, 1295e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Expr **Args, unsigned NumArgs, 1296e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *&Specialization, 1297e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateDeductionInfo &Info) { 1298e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 12996db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor 1300e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p1: 1301e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Template argument deduction is done by comparing each function template 1302e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // parameter type (call it P) with the type of the corresponding argument 1303e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of the call (call it A) as described below. 1304e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor unsigned CheckArgs = NumArgs; 13056db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor if (NumArgs < Function->getMinRequiredArguments()) 1306e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooFewArguments; 1307e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (NumArgs > Function->getNumParams()) { 13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 1309183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Function->getType()->getAs<FunctionProtoType>(); 1310e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (!Proto->isVariadic()) 1311e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooManyArguments; 13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1313e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor CheckArgs = Function->getNumParams(); 1314e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13166db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // The types of the parameters from which we will perform template argument 13176db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // deduction. 1318e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateParameterList *TemplateParams 1319e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = FunctionTemplate->getTemplateParameters(); 13206db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 13216db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 13226db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor if (NumExplicitTemplateArgs) { 132383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionResult Result = 132483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SubstituteExplicitTemplateArguments(FunctionTemplate, 132583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ExplicitTemplateArgs, 132683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs, 132783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced, 132883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes, 132983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 0, 133083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info); 133183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Result) 133283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 13336db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } else { 13346db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Just fill in the parameter types from the function declaration. 13356db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor for (unsigned I = 0; I != CheckArgs; ++I) 13366db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor ParamTypes.push_back(Function->getParamDecl(I)->getType()); 13376db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } 13381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13396db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Deduce template arguments from the function parameters. 13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 1341e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor for (unsigned I = 0; I != CheckArgs; ++I) { 13426db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor QualType ParamType = ParamTypes[I]; 1343e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType ArgType = Args[I]->getType(); 13441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1345e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p2: 1346e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // If P is not a reference type: 1347e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType CanonParamType = Context.getCanonicalType(ParamType); 1348500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor bool ParamWasReference = isa<ReferenceType>(CanonParamType); 1349500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor if (!ParamWasReference) { 13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If A is an array type, the pointer type produced by the 13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place of 1352e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // A for type deduction; otherwise, 1353e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (ArgType->isArrayType()) 1354e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getArrayDecayedType(ArgType); 13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If A is a function type, the pointer type produced by the 13561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // function-to-pointer standard conversion (4.3) is used in place 1357e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of A for type deduction; otherwise, 1358e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (ArgType->isFunctionType()) 1359e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getPointerType(ArgType); 1360e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else { 1361e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // - If A is a cv-qualified type, the top level cv-qualifiers of A’s 1362e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // type are ignored for type deduction. 1363e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType CanonArgType = Context.getCanonicalType(ArgType); 1364e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (CanonArgType.getCVRQualifiers()) 1365e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = CanonArgType.getUnqualifiedType(); 1366e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 1367e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 13681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1369e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++0x [temp.deduct.call]p3: 1370e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // If P is a cv-qualified type, the top level cv-qualifiers of P’s type 13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // are ignored for type deduction. 1372e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (CanonParamType.getCVRQualifiers()) 1373e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ParamType = CanonParamType.getUnqualifiedType(); 13746217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { 13751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] If P is a reference type, the type referred to by P is used 13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // for type deduction. 1377e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ParamType = ParamRefType->getPointeeType(); 13781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] If P is of the form T&&, where T is a template parameter, and 13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // the argument is an lvalue, the type A& is used in place of A for 1381e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // type deduction. 1382e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (isa<RValueReferenceType>(ParamRefType) && 1383183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall ParamRefType->getAs<TemplateTypeParmType>() && 1384e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Args[I]->isLvalue(Context) == Expr::LV_Valid) 1385e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getLValueReferenceType(ArgType); 1386e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 13871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1388e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++0x [temp.deduct.call]p4: 1389e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // In general, the deduction process attempts to find template argument 1390e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // values that will make the deduced A identical to A (after the type A 1391e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // is transformed as described above). [...] 13921282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor unsigned TDF = TDF_SkipNonDependent; 13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1394508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // - If the original P is a reference type, the deduced A (i.e., the 1395508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // type referred to by the reference) can be more cv-qualified than 1396508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // the transformed A. 1397508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (ParamWasReference) 1398508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF |= TDF_ParamWithReferenceType; 13991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - The transformed A can be another pointer or pointer to member 14001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type that can be converted to the deduced A via a qualification 1401508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // conversion (4.4). 1402508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (ArgType->isPointerType() || ArgType->isMemberPointerType()) 1403508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF |= TDF_IgnoreQualifiers; 14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If P is a class and P has the form simple-template-id, then the 14054112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // transformed A can be a derived class of the deduced A. Likewise, 14064112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // if P is a pointer to a class of the form simple-template-id, the 14074112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // transformed A can be a pointer to a derived class pointed to by 14084112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // the deduced A. 14094112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor if (isSimpleTemplateIdType(ParamType) || 14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (isa<PointerType>(ParamType) && 14114112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor isSimpleTemplateIdType( 14126217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ParamType->getAs<PointerType>()->getPointeeType()))) 14134112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor TDF |= TDF_DerivedClass; 14141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1415e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (TemplateDeductionResult Result 1416e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = ::DeduceTemplateArguments(Context, TemplateParams, 1417500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor ParamType, ArgType, Info, Deduced, 1418508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF)) 1419e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return Result; 14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14218fdc3c49e3c8178222a35b16426dc5a08a0efb6dDouglas Gregor // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function 14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // pointer parameters. 142365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 142465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 142565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 1426e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 142765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 142983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 143083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 1431127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 143283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function 1433b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a 143483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 143583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing 143683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 143783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 14381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param HasExplicitTemplateArgs whether any template arguments were 143983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// explicitly specified. 144083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 144183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 144283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the explicitly-specified template arguments. 144383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 144483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// the number of explicitly-specified template arguments in 144683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// @p ExplicitTemplateArguments. This value may be zero. 144783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 144883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the 144983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the 145083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// function template's function type. 145183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 145283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful, 14531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 145483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 145583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 145683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information 145783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction. 145883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 145983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction. 146083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 146183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 146283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor bool HasExplicitTemplateArgs, 146383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor const TemplateArgument *ExplicitTemplateArgs, 146483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor unsigned NumExplicitTemplateArgs, 146583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType ArgFunctionType, 146683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 146783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 146883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 146983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 147083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 147183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType FunctionType = Function->getType(); 14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 147383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitute any explicit template arguments. 147483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 147583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 147683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (HasExplicitTemplateArgs) { 14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateDeductionResult Result 14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstituteExplicitTemplateArguments(FunctionTemplate, 14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ExplicitTemplateArgs, 148083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor NumExplicitTemplateArgs, 14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced, ParamTypes, 148283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor &FunctionType, Info)) 148383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 1484127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 148583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 148683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Template argument deduction for function templates in a SFINAE context. 148783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Trap any errors that might occur. 14881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 14891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 149083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Deduce template arguments from the function type. 14911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 149283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (TemplateDeductionResult Result 149383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = ::DeduceTemplateArguments(Context, TemplateParams, 14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FunctionType, ArgFunctionType, Info, 149583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced, 0)) 149683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 149983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 1500e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 1501e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 150265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion 150365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a 150465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization. 150565ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult 150665ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 150765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType ToType, 150865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor CXXConversionDecl *&Specialization, 150965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionInfo &Info) { 15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CXXConversionDecl *Conv 151165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); 151265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType FromType = Conv->getConversionType(); 151365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 151465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Canonicalize the types for deduction. 151565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType P = Context.getCanonicalType(FromType); 151665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType A = Context.getCanonicalType(ToType); 151765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 151865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 151965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If P is a reference type, the type referred to by P is used for 152065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type deduction. 152165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 152265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = PRef->getPointeeType(); 152365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 152465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 152565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a reference type, the type referred to by A is used 152665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // for type deduction. 152765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 152865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = ARef->getPointeeType(); 152965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p2: 153065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If A is not a reference type: 153265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else { 153365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor assert(!A->isReferenceType() && "Reference types were handled above"); 153465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 153565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is an array type, the pointer type produced by the 15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place 153765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // of P for type deduction; otherwise, 153865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (P->isArrayType()) 153965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getArrayDecayedType(P); 154065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a function type, the pointer type produced by the 154165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // function-to-pointer standard conversion (4.3) is used in 154265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // place of P for type deduction; otherwise, 154365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else if (P->isFunctionType()) 154465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getPointerType(P); 154565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a cv-qualified type, the top level cv-qualifiers of 154665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // P’s type are ignored for type deduction. 154765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else 154865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = P.getUnqualifiedType(); 154965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 155065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 155165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a cv-qualified type, the top level cv-qualifiers of A’s 155265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type are ignored for type deduction. 155365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = A.getUnqualifiedType(); 155465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor } 155565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 155665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction for function templates in a SFINAE context. 155765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Trap any errors that might occur. 15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 155965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 156065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p1: 156165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction is done by comparing the return 156265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type of the template conversion function (call it P) with the 156365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that is required as the result of the conversion (call it 156465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A) as described in 14.8.2.4. 156565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateParameterList *TemplateParams 156665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = FunctionTemplate->getTemplateParameters(); 156765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 156965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 157065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p4: 157165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // In general, the deduction process attempts to find template 157265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // argument values that will make the deduced A identical to 157365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A. However, there are two cases that allow a difference: 157465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor unsigned TDF = 0; 157565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If the original A is a reference type, A can be more 157665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // cv-qualified than the deduced A (i.e., the type referred to 157765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // by the reference) 157865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (ToType->isReferenceType()) 157965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_ParamWithReferenceType; 158065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - The deduced A can be another pointer or pointer to member 158165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that can be converted to A via a qualification 158265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // conversion. 158365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 158465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 158565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // both P and A are pointers or member pointers. In this case, we 158665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // just ignore cv-qualifiers completely). 158765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if ((P->isPointerType() && A->isPointerType()) || 158865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor (P->isMemberPointerType() && P->isMemberPointerType())) 158965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_IgnoreQualifiers; 159065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (TemplateDeductionResult Result 159165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = ::DeduceTemplateArguments(Context, TemplateParams, 159265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P, A, Info, Deduced, TDF)) 159365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 159465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 159565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 159665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 15971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 159865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Finish template argument deduction. 159965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionDecl *Spec = 0; 160065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionResult Result 160165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info); 160265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Specialization = cast_or_null<CXXConversionDecl>(Spec); 160365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 160465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 160565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 16068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types. 16078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison { 16088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor NeitherMoreQualified = 0, 16098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ParamMoreQualified, 16108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ArgMoreQualified 16118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}; 16128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing 16148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]). 16158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Context the AST context in which this deduction occurs. 16178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 16198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type 16218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type 16238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself 16258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments 16278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 16288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 16298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 16308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons. 16318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult 16328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas GregorDeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context, 16338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParameterList *TemplateParams, 16348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualType ParamIn, QualType ArgIn, 16358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Sema::TemplateDeductionInfo &Info, 16368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Deduced, 16378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { 16388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQualType Param = Context.getCanonicalType(ParamIn); 16398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQualType Arg = Context.getCanonicalType(ArgIn); 16408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p5: 16428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Before the partial ordering is done, certain transformations are 16438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // performed on the types used for partial ordering: 16448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If P is a reference type, P is replaced by the type referred to. 16458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); 1646e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall if (!ParamRef.isNull()) 16478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Param = ParamRef->getPointeeType(); 16488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If A is a reference type, A is replaced by the type referred to. 16508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); 1651e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall if (!ArgRef.isNull()) 16528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Arg = ArgRef->getPointeeType(); 16538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 1654e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) { 16558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p6: 16568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If both P and A were reference types (before being replaced with the 16578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // type referred to above), determine which of the two types (if any) is 16588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // more cv-qualified than the other; otherwise the types are considered to 16598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // be equally cv-qualified for partial ordering purposes. The result of this 16608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // determination will be used below. 16618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // 16628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // We save this information for later, using it only when deduction 16638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // succeeds in both directions. 16648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor DeductionQualifierComparison QualifierResult = NeitherMoreQualified; 16658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 16668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierResult = ParamMoreQualified; 16678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Arg.isMoreQualifiedThan(Param)) 16688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierResult = ArgMoreQualified; 16698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons->push_back(QualifierResult); 16708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 16718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p7: 16738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Remove any top-level cv-qualifiers: 16748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If P is a cv-qualified type, P is replaced by the cv-unqualified 16758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // version of P. 16768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Param = Param.getUnqualifiedType(); 16778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If A is a cv-qualified type, A is replaced by the cv-unqualified 16788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // version of A. 16798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Arg = Arg.getUnqualifiedType(); 16808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p8: 16828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Using the resulting types P and A the deduction is then done as 16838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // described in 14.9.2.5. If deduction succeeds for a given type, the type 16848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // from the argument template is considered to be at least as specialized 16858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as the type from the parameter template. 16868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info, 16878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, TDF_None); 16888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 16898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void 1691e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T, 1692e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 1693e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced); 16948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 16958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as 16968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2. 16978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S, 16988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT1, 16998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT2, 17008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC, 17018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { 17028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD1 = FT1->getTemplatedDecl(); 17038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD2 = FT2->getTemplatedDecl(); 17048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 17058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 17068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(Proto1 && Proto2 && "Function templates must have prototypes"); 17088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 17098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 17108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced.resize(TemplateParams->size()); 17118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p3: 17138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // The types used to determine the ordering depend on the context in which 17148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the partial ordering is done: 17158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Sema::TemplateDeductionInfo Info(S.Context); 17168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 17178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 17188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a function call, the function parameter types are 17198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used. 17208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); 17218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0; I != NumParams; ++I) 17228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, 17238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 17248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto2->getArgType(I), 17258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto1->getArgType(I), 17268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 17278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 17288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 17298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 17358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a call to a conversion operator, the return types 17368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // of the conversion function templates are used. 17378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, 17388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 17398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto2->getResultType(), 17408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto1->getResultType(), 17418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 17428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 17438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 17448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 17488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In other contexts (14.6.6.2) the function template’s function type 17498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // is used. 17508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, 17518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 17528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FD2->getType(), 17538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FD1->getType(), 17548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 17558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 17568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 17578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 17588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p11: 17628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // In most cases, all template parameters must have values in order for 17638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // deduction to succeed, but for partial ordering purposes a template 17648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter may remain without a value provided it is not used in the 17658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types being used for partial ordering. [ Note: a template parameter used 17668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // in a non-deduced context is considered used. -end note] 17678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned ArgIdx = 0, NumArgs = Deduced.size(); 17688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 17698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull()) 17708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (ArgIdx == NumArgs) { 17738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // All template arguments were deduced. FT1 is at least as specialized 17748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as FT2. 17758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 17768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 1778e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // Figure out which template parameters were used. 17798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<bool, 4> UsedParameters; 17808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor UsedParameters.resize(TemplateParams->size()); 17818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 17828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 17838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); 17848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0; I != NumParams; ++I) 1785e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, 1786e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor UsedParameters); 17878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 1791e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, 1792e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor UsedParameters); 17938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 17958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 1796e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor ::MarkUsedTemplateParameters(S, FD2->getType(), false, UsedParameters); 17978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 17988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 17998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 18018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If this argument had no value deduced but was used in one of the types 18028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used for partial ordering, then deduction fails. 18038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 18048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 18058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 18078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 18088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 1810bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according 181165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]). 181265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 181365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template 181465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 181565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template 181665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 18178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of 18188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates. 18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 1820bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither 182165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL. 182265ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl * 182365ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, 182465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionTemplateDecl *FT2, 18258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC) { 18268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; 18278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0); 18288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC, 18298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor &QualifierComparisons); 18308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1 != Better2) // We have a clear winner 18328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return Better1? FT1 : FT2; 18338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (!Better1 && !Better2) // Neither is better than the other 183565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return 0; 18368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p10: 18398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If for each type being considered a given template is at least as 18408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized for all types and more specialized for some set of types and 18418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the other template is not more specialized for any types or is not at 18428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // least as specialized for any types, then the given template is more 18438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other template. Otherwise, neither template is more 18448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other. 18458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = false; 18468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = false; 18478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { 18488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p9: 18498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If, for a given type, deduction succeeds in both directions (i.e., the 18508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types are identical after the transformations above) and if the type 18518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // from the argument template is more cv-qualified than the type from the 18528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter template (as described above) that type is considered to be 18538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // more specialized than the other. If neither type is more cv-qualified 18548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // than the other then neither type is more specialized than the other. 18558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (QualifierComparisons[I]) { 18568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case NeitherMoreQualified: 18578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 18588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case ParamMoreQualified: 18608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = true; 18618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better2) 18628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 18638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 18648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case ArgMoreQualified: 18668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = true; 18678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1) 18688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 18698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 18708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 18718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 18728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 18738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(!(Better1 && Better2) && "Should have broken out in the loop above"); 187465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (Better1) 187565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return FT1; 18768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Better2) 18778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return FT2; 18788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else 18798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 188065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 188183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 1882d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent. 1883d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { 1884d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (T1 == T2) 1885d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return true; 1886d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1887d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (!T1 || !T2) 1888d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return false; 1889d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1890d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return T1->getCanonicalDecl() == T2->getCanonicalDecl(); 1891d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor} 1892d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1893d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template 1894d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations. 1895d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1896d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Specializations the set of function template specializations that 1897d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// we will be comparing. 1898d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1899d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NumSpecializations the number of function template specializations in 1900d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \p Specializations 1901d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1902d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function 1903d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations. 1904d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1905d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations 1906d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur. 1907d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1908d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are 1909d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates. 1910d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1911d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one 1912d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs. 1913d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1914d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template 1915d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter 1916d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string 1917d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization. 1918d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1919d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL, 1920d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template 1921d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization. 1922d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1923d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if 1924d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// found. Otherwise, returns NULL. 1925d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 1926d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed 1927d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction. 1928d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas GregorFunctionDecl *Sema::getMostSpecialized(FunctionDecl **Specializations, 1929d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor unsigned NumSpecializations, 1930d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor TemplatePartialOrderingContext TPOC, 1931d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor SourceLocation Loc, 1932d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor const PartialDiagnostic &NoneDiag, 1933d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor const PartialDiagnostic &AmbigDiag, 1934d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor const PartialDiagnostic &CandidateDiag, 1935d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor unsigned *Index) { 1936d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (NumSpecializations == 0) { 1937d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Loc, NoneDiag); 1938d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return 0; 1939d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1940d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1941d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (NumSpecializations == 1) { 1942d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (Index) 1943d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor *Index = 0; 1944d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1945d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return Specializations[0]; 1946d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1947d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1948d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1949d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Find the function template that is better than all of the templates it 1950d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // has been compared to. 1951d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor unsigned Best = 0; 1952d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor FunctionTemplateDecl *BestTemplate 1953d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor = Specializations[Best]->getPrimaryTemplate(); 1954d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor assert(BestTemplate && "Not a function template specialization?"); 1955d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor for (unsigned I = 1; I != NumSpecializations; ++I) { 1956d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate(); 1957d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor assert(Challenger && "Not a function template specialization?"); 1958d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 1959d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor TPOC), 1960d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Challenger)) { 1961d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Best = I; 1962d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor BestTemplate = Challenger; 1963d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1964d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1965d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1966d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Make sure that the "best" function template is more specialized than all 1967d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // of the others. 1968d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor bool Ambiguous = false; 1969d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor for (unsigned I = 0; I != NumSpecializations; ++I) { 1970d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate(); 1971d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (I != Best && 1972d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 1973d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor TPOC), 1974d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor BestTemplate)) { 1975d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Ambiguous = true; 1976d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor break; 1977d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1978d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1979d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1980d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (!Ambiguous) { 1981d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // We found an answer. Return it. 1982d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (Index) 1983d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor *Index = Best; 1984d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return Specializations[Best]; 1985d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 1986d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1987d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Diagnose the ambiguity. 1988d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Loc, AmbigDiag); 1989d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1990d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // FIXME: Can we order the candidates in some sane way? 1991d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor for (unsigned I = 0; I != NumSpecializations; ++I) 1992d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Specializations[I]->getLocation(), CandidateDiag) 1993d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor << getTemplateArgumentBindingsText( 1994d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Specializations[I]->getPrimaryTemplate()->getTemplateParameters(), 1995d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor *Specializations[I]->getTemplateSpecializationArgs()); 1996d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 1997d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return 0; 1998d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor} 1999d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2000bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization 2001bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial 2002bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]). 2003bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 2004bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization 2005bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 2006bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization 2007bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 2008bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If 2009bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL. 2010bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl * 2011bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization( 2012bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor ClassTemplatePartialSpecializationDecl *PS1, 2013bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor ClassTemplatePartialSpecializationDecl *PS2) { 2014bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // C++ [temp.class.order]p1: 2015bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // For two class template partial specializations, the first is at least as 2016bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // specialized as the second if, given the following rewrite to two 2017bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // function templates, the first function template is at least as 2018bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // specialized as the second according to the ordering rules for function 2019bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // templates (14.6.6.2): 2020bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // - the first function template has the same template parameters as the 2021bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // first partial specialization and has a single function parameter 2022bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // whose type is a class template specialization with the template 2023bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // arguments of the first partial specialization, and 2024bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // - the second function template has the same template parameters as the 2025bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // second partial specialization and has a single function parameter 2026bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // whose type is a class template specialization with the template 2027bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // arguments of the second partial specialization. 2028bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // 2029bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Rather than synthesize function templates, we merely perform the 2030bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // equivalent partial ordering by performing deduction directly on the 2031bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // template arguments of the class template partial specializations. This 2032bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // computation is slightly simpler than the general problem of function 2033bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // template partial ordering, because class template partial specializations 2034bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // are more constrained. We know that every template parameter is deduc 2035bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Deduced; 2036bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Sema::TemplateDeductionInfo Info(Context); 2037bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2038bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Determine whether PS1 is at least as specialized as PS2 2039bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced.resize(PS2->getTemplateParameters()->size()); 2040bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, 2041bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor PS2->getTemplateParameters(), 2042bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Context.getTypeDeclType(PS2), 2043bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Context.getTypeDeclType(PS1), 2044bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Info, 2045bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced, 2046bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 0); 2047bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2048bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Determine whether PS2 is at least as specialized as PS1 2049bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced.resize(PS1->getTemplateParameters()->size()); 2050bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, 2051bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor PS1->getTemplateParameters(), 2052bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Context.getTypeDeclType(PS1), 2053bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Context.getTypeDeclType(PS2), 2054bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Info, 2055bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced, 2056bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 0); 2057bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2058bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor if (Better1 == Better2) 2059bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor return 0; 2060bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2061bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor return Better1? PS1 : PS2; 2062bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor} 2063bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 20641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2065e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2066e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const TemplateArgument &TemplateArg, 2067e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2068e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used); 2069031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2070e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2071031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression. 20721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2073e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2074e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const Expr *E, 2075e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2076e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2077e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to 2078e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // find other occurrences of template parameters. 2079f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 2080031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!E) 2081031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 2082031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 20831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const NonTypeTemplateParmDecl *NTTP 2084031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 2085031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!NTTP) 2086031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 2087031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2088e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used[NTTP->getIndex()] = true; 2089031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2090031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2091e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2092e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier. 2093e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void 2094e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2095e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor NestedNameSpecifier *NNS, 2096e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2097e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2098e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!NNS) 2099e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 2100e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2101e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Used); 2102e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), 2103e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2104e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor} 2105e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2106e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2107e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name. 2108e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void 2109e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2110e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor TemplateName Name, 2111e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2112e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2113e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 2114e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (TemplateTemplateParmDecl *TTP 2115e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(Template)) 2116e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used[TTP->getIndex()] = true; 2117e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 2118e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor } 2119e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2120e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) 2121e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, Used); 2122e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor} 2123e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2124e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2125031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type. 21261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2127e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T, 2128e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2129e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2130e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (T.isNull()) 2131e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 2132e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2133031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Non-dependent types have nothing deducible 2134031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!T->isDependentType()) 2135031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 2136031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2137031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor T = SemaRef.Context.getCanonicalType(T); 2138031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (T->getTypeClass()) { 2139031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Pointer: 2140e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2141e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<PointerType>(T)->getPointeeType(), 2142e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 2143e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2144031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2145031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2146031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::BlockPointer: 2147e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2148e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<BlockPointerType>(T)->getPointeeType(), 2149e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 2150e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2151031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2152031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2153031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::LValueReference: 2154031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::RValueReference: 2155e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2156e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ReferenceType>(T)->getPointeeType(), 2157e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 2158e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2159031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2160031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2161031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::MemberPointer: { 2162031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 2163e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, 2164e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2165e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), 2166e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2167031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2168031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2169031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2170031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::DependentSizedArray: 2171e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2172e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<DependentSizedArrayType>(T)->getSizeExpr(), 2173e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2174031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Fall through to check the element type 2175031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2176031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ConstantArray: 2177031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::IncompleteArray: 2178e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2179e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ArrayType>(T)->getElementType(), 2180e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2181031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2182031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2183031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Vector: 2184031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ExtVector: 2185e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2186e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<VectorType>(T)->getElementType(), 2187e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2188031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2189031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 21909cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor case Type::DependentSizedExtVector: { 21919cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor const DependentSizedExtVectorType *VecType 2192f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<DependentSizedExtVectorType>(T); 2193e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, 2194e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2195e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, 2196e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 21979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor break; 21989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor } 21999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor 2200031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionProto: { 2201f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 2202e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, 2203e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2204031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) 2205e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, 2206e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2207031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2208031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2209031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2210031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::TemplateTypeParm: 2211e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used[cast<TemplateTypeParmType>(T)->getIndex()] = true; 2212031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2213031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2214031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::TemplateSpecialization: { 22151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const TemplateSpecializationType *Spec 2216f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<TemplateSpecializationType>(T); 2217e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, 2218e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2219e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 2220e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Used); 2221e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor break; 2222e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor } 22231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2224e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor case Type::Complex: 2225e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!OnlyDeduced) 2226e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2227e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ComplexType>(T)->getElementType(), 2228e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2229e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor break; 2230031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2231e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor case Type::Typename: 2232e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!OnlyDeduced) 2233e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2234e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<TypenameType>(T)->getQualifier(), 2235e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, Used); 2236031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2237031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2238e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // None of these types have any template parameters in them. 2239031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Builtin: 2240031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FixedWidthInt: 2241031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::VariableArray: 2242031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionNoProto: 2243031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Record: 2244031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Enum: 2245031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ObjCInterface: 2246d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff case Type::ObjCObjectPointer: 2247031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base) 2248031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 2249031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base) 2250031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2251031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def" 2252031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2253031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2254031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2255031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2256e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this 2257031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument. 22581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2259e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2260e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const TemplateArgument &TemplateArg, 2261e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2262e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2263031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (TemplateArg.getKind()) { 2264031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Null: 2265031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Integral: 2266031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 22671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2268031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Type: 2269e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, 2270e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2271031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2272031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2273031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Declaration: 22741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateTemplateParmDecl *TTP 2275031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) 2276e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used[TTP->getIndex()] = true; 2277031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2278031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2279031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Expression: 2280e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, 2281e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2282031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2283e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2284d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 2285e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), 2286e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor PEnd = TemplateArg.pack_end(); 2287e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor P != PEnd; ++P) 2288e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Used); 2289d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 2290031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2291031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2292031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2293031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given 2294031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list. 2295031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 2296031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template 2297031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced. 2298031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 2299031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true 2300031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be 2301031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced. 23021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 2303e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, 2304e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2305e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2306031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 2307e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, Used); 2308031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 230963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor 231063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a 231163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template. 231263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregorvoid Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate, 231363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 231463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor TemplateParameterList *TemplateParams 231563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor = FunctionTemplate->getTemplateParameters(); 231663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor Deduced.clear(); 231763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor Deduced.resize(TemplateParams->size()); 231863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor 231963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 232063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 232163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(), 232263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor true, Deduced); 232363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor} 2324