Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template argument deduction. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
| 13 | #include "Sema.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/StmtVisitor.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
| 19 | #include "clang/Parse/DeclSpec.h" |
| 20 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | /// \brief Various flags that control template argument deduction. |
| 25 | /// |
| 26 | /// These flags can be bitwise-OR'd together. |
| 27 | enum TemplateDeductionFlags { |
| 28 | /// \brief No template argument deduction flags, which indicates the |
| 29 | /// strictest results for template argument deduction (as used for, e.g., |
| 30 | /// matching class template partial specializations). |
| 31 | TDF_None = 0, |
| 32 | /// \brief Within template argument deduction from a function call, we are |
| 33 | /// matching with a parameter type for which the original parameter was |
| 34 | /// a reference. |
| 35 | TDF_ParamWithReferenceType = 0x1, |
| 36 | /// \brief Within template argument deduction from a function call, we |
| 37 | /// are matching in a case where we ignore cv-qualifiers. |
| 38 | TDF_IgnoreQualifiers = 0x02, |
| 39 | /// \brief Within template argument deduction from a function call, |
| 40 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 41 | /// deduction from a template-id of a derived class of the argument type. |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 42 | TDF_DerivedClass = 0x04, |
| 43 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 44 | /// template argument deduction from a function call where conversions |
| 45 | /// may apply. |
| 46 | TDF_SkipNonDependent = 0x08 |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 47 | }; |
| 48 | } |
| 49 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 50 | using namespace clang; |
| 51 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 52 | static Sema::TemplateDeductionResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 54 | TemplateParameterList *TemplateParams, |
| 55 | const TemplateArgument &Param, |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 56 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 57 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 58 | llvm::SmallVectorImpl<TemplateArgument> &Deduced); |
| 59 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 60 | /// \brief If the given expression is of a form that permits the deduction |
| 61 | /// of a non-type template parameter, return the declaration of that |
| 62 | /// non-type template parameter. |
| 63 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
| 64 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 65 | E = IC->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 67 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 68 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 74 | /// from the given constant. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 75 | static Sema::TemplateDeductionResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 77 | NonTypeTemplateParmDecl *NTTP, |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 78 | llvm::APSInt Value, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 79 | Sema::TemplateDeductionInfo &Info, |
| 80 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 82 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 84 | if (Deduced[NTTP->getIndex()].isNull()) { |
Anders Carlsson | 25af1ed | 2009-06-16 23:08:29 +0000 | [diff] [blame] | 85 | QualType T = NTTP->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Anders Carlsson | 25af1ed | 2009-06-16 23:08:29 +0000 | [diff] [blame] | 87 | // FIXME: Make sure we didn't overflow our data type! |
| 88 | unsigned AllowedBits = Context.getTypeSize(T); |
| 89 | if (Value.getBitWidth() != AllowedBits) |
| 90 | Value.extOrTrunc(AllowedBits); |
| 91 | Value.setIsSigned(T->isSignedIntegerType()); |
| 92 | |
| 93 | Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 94 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 95 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 97 | assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
| 99 | // If the template argument was previously deduced to a negative value, |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 100 | // then our deduction fails. |
| 101 | const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral(); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 102 | if (PrevValuePtr->isNegative()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 103 | Info.Param = NTTP; |
| 104 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 105 | Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 106 | return Sema::TDK_Inconsistent; |
| 107 | } |
| 108 | |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 109 | llvm::APSInt PrevValue = *PrevValuePtr; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 110 | if (Value.getBitWidth() > PrevValue.getBitWidth()) |
| 111 | PrevValue.zext(Value.getBitWidth()); |
| 112 | else if (Value.getBitWidth() < PrevValue.getBitWidth()) |
| 113 | Value.zext(PrevValue.getBitWidth()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 114 | |
| 115 | if (Value != PrevValue) { |
| 116 | Info.Param = NTTP; |
| 117 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 118 | Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 119 | return Sema::TDK_Inconsistent; |
| 120 | } |
| 121 | |
| 122 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 126 | /// from the given type- or value-dependent expression. |
| 127 | /// |
| 128 | /// \returns true if deduction succeeded, false otherwise. |
| 129 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 130 | static Sema::TemplateDeductionResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 132 | NonTypeTemplateParmDecl *NTTP, |
| 133 | Expr *Value, |
| 134 | Sema::TemplateDeductionInfo &Info, |
| 135 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 137 | "Cannot deduce non-type template argument with depth > 0"); |
| 138 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 139 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 141 | if (Deduced[NTTP->getIndex()].isNull()) { |
| 142 | // FIXME: Clone the Value? |
| 143 | Deduced[NTTP->getIndex()] = TemplateArgument(Value); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 144 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 147 | if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | // Okay, we deduced a constant in one case and a dependent expression |
| 149 | // in another case. FIXME: Later, we will check that instantiating the |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 150 | // dependent expression gives us the constant value. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 151 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 9eea08b | 2009-09-15 16:51:42 +0000 | [diff] [blame] | 154 | if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) { |
| 155 | // Compare the expressions for equality |
| 156 | llvm::FoldingSetNodeID ID1, ID2; |
| 157 | Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true); |
| 158 | Value->Profile(ID2, Context, true); |
| 159 | if (ID1 == ID2) |
| 160 | return Sema::TDK_Success; |
| 161 | |
| 162 | // FIXME: Fill in argument mismatch information |
| 163 | return Sema::TDK_NonDeducedMismatch; |
| 164 | } |
| 165 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 166 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 169 | static Sema::TemplateDeductionResult |
| 170 | DeduceTemplateArguments(ASTContext &Context, |
| 171 | TemplateName Param, |
| 172 | TemplateName Arg, |
| 173 | Sema::TemplateDeductionInfo &Info, |
| 174 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 175 | // FIXME: Implement template argument deduction for template |
| 176 | // template parameters. |
| 177 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 178 | // FIXME: this routine does not have enough information to produce |
| 179 | // good diagnostics. |
| 180 | |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 181 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
| 182 | TemplateDecl *ArgDecl = Arg.getAsTemplateDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 184 | if (!ParamDecl || !ArgDecl) { |
| 185 | // FIXME: fill in Info.Param/Info.FirstArg |
| 186 | return Sema::TDK_Inconsistent; |
| 187 | } |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 188 | |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 189 | ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl()); |
| 190 | ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 191 | if (ParamDecl != ArgDecl) { |
| 192 | // FIXME: fill in Info.Param/Info.FirstArg |
| 193 | return Sema::TDK_Inconsistent; |
| 194 | } |
| 195 | |
| 196 | return Sema::TDK_Success; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 200 | /// type (which is a template-id) with the template argument type. |
| 201 | /// |
| 202 | /// \param Context the AST context in which this deduction occurs. |
| 203 | /// |
| 204 | /// \param TemplateParams the template parameters that we are deducing |
| 205 | /// |
| 206 | /// \param Param the parameter type |
| 207 | /// |
| 208 | /// \param Arg the argument type |
| 209 | /// |
| 210 | /// \param Info information about the template argument deduction itself |
| 211 | /// |
| 212 | /// \param Deduced the deduced template arguments |
| 213 | /// |
| 214 | /// \returns the result of template argument deduction so far. Note that a |
| 215 | /// "success" result means that template argument deduction has not yet failed, |
| 216 | /// but it may still fail, later, for other reasons. |
| 217 | static Sema::TemplateDeductionResult |
| 218 | DeduceTemplateArguments(ASTContext &Context, |
| 219 | TemplateParameterList *TemplateParams, |
| 220 | const TemplateSpecializationType *Param, |
| 221 | QualType Arg, |
| 222 | Sema::TemplateDeductionInfo &Info, |
| 223 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 224 | assert(Arg->isCanonical() && "Argument type must be canonical"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 226 | // Check whether the template argument is a dependent template-id. |
| 227 | // FIXME: This is untested code; it can be tested when we implement |
| 228 | // partial ordering of class template partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 230 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 231 | // Perform template argument deduction for the template name. |
| 232 | if (Sema::TemplateDeductionResult Result |
| 233 | = DeduceTemplateArguments(Context, |
| 234 | Param->getTemplateName(), |
| 235 | SpecArg->getTemplateName(), |
| 236 | Info, Deduced)) |
| 237 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 239 | unsigned NumArgs = Param->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 241 | // FIXME: When one of the template-names refers to a |
| 242 | // declaration with default template arguments, do we need to |
| 243 | // fill in those default template arguments here? Most likely, |
| 244 | // the answer is "yes", but I don't see any references. This |
| 245 | // issue may be resolved elsewhere, because we may want to |
| 246 | // instantiate default template arguments when we actually write |
| 247 | // the template-id. |
| 248 | if (SpecArg->getNumArgs() != NumArgs) |
| 249 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 251 | // Perform template argument deduction on each template |
| 252 | // argument. |
| 253 | for (unsigned I = 0; I != NumArgs; ++I) |
| 254 | if (Sema::TemplateDeductionResult Result |
| 255 | = DeduceTemplateArguments(Context, TemplateParams, |
| 256 | Param->getArg(I), |
| 257 | SpecArg->getArg(I), |
| 258 | Info, Deduced)) |
| 259 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 261 | return Sema::TDK_Success; |
| 262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 264 | // If the argument type is a class template specialization, we |
| 265 | // perform template argument deduction using its template |
| 266 | // arguments. |
| 267 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| 268 | if (!RecordArg) |
| 269 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
| 271 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 272 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| 273 | if (!SpecArg) |
| 274 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 276 | // Perform template argument deduction for the template name. |
| 277 | if (Sema::TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | = DeduceTemplateArguments(Context, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 279 | Param->getTemplateName(), |
| 280 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 281 | Info, Deduced)) |
| 282 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 284 | // FIXME: Can the # of arguments in the parameter and the argument |
| 285 | // differ due to default arguments? |
| 286 | unsigned NumArgs = Param->getNumArgs(); |
| 287 | const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); |
| 288 | if (NumArgs != ArgArgs.size()) |
| 289 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 291 | for (unsigned I = 0; I != NumArgs; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 293 | = DeduceTemplateArguments(Context, TemplateParams, |
| 294 | Param->getArg(I), |
| 295 | ArgArgs.get(I), |
| 296 | Info, Deduced)) |
| 297 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 299 | return Sema::TDK_Success; |
| 300 | } |
| 301 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | /// \brief Returns a completely-unqualified array type, capturing the |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 303 | /// qualifiers in CVRQuals. |
| 304 | /// |
| 305 | /// \param Context the AST context in which the array type was built. |
| 306 | /// |
| 307 | /// \param T a canonical type that may be an array type. |
| 308 | /// |
| 309 | /// \param CVRQuals will receive the set of const/volatile/restrict qualifiers |
| 310 | /// that were applied to the element type of the array. |
| 311 | /// |
| 312 | /// \returns if \p T is an array type, the completely unqualified array type |
| 313 | /// that corresponds to T. Otherwise, returns T. |
| 314 | static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T, |
| 315 | unsigned &CVRQuals) { |
| 316 | assert(T->isCanonical() && "Only operates on canonical types"); |
| 317 | if (!isa<ArrayType>(T)) { |
| 318 | CVRQuals = T.getCVRQualifiers(); |
| 319 | return T.getUnqualifiedType(); |
| 320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 322 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { |
| 323 | QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(), |
| 324 | CVRQuals); |
| 325 | if (Elt == CAT->getElementType()) |
| 326 | return T; |
| 327 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | return Context.getConstantArrayType(Elt, CAT->getSize(), |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 329 | CAT->getSizeModifier(), 0); |
| 330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 332 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { |
| 333 | QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(), |
| 334 | CVRQuals); |
| 335 | if (Elt == IAT->getElementType()) |
| 336 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 338 | return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0); |
| 339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 341 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); |
| 342 | QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(), |
| 343 | CVRQuals); |
| 344 | if (Elt == DSAT->getElementType()) |
| 345 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Anders Carlsson | d497206 | 2009-08-08 02:50:17 +0000 | [diff] [blame] | 347 | return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(), |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 348 | DSAT->getSizeModifier(), 0, |
| 349 | SourceRange()); |
| 350 | } |
| 351 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 352 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 353 | /// the argument type (C++ [temp.deduct.type]). |
| 354 | /// |
| 355 | /// \param Context the AST context in which this deduction occurs. |
| 356 | /// |
| 357 | /// \param TemplateParams the template parameters that we are deducing |
| 358 | /// |
| 359 | /// \param ParamIn the parameter type |
| 360 | /// |
| 361 | /// \param ArgIn the argument type |
| 362 | /// |
| 363 | /// \param Info information about the template argument deduction itself |
| 364 | /// |
| 365 | /// \param Deduced the deduced template arguments |
| 366 | /// |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 367 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | /// how template argument deduction is performed. |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 369 | /// |
| 370 | /// \returns the result of template argument deduction so far. Note that a |
| 371 | /// "success" result means that template argument deduction has not yet failed, |
| 372 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 373 | static Sema::TemplateDeductionResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 375 | TemplateParameterList *TemplateParams, |
| 376 | QualType ParamIn, QualType ArgIn, |
| 377 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 378 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 379 | unsigned TDF) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 380 | // We only want to look at the canonical types, since typedefs and |
| 381 | // sugar are not part of template argument deduction. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 382 | QualType Param = Context.getCanonicalType(ParamIn); |
| 383 | QualType Arg = Context.getCanonicalType(ArgIn); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 385 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 386 | // - If the original P is a reference type, the deduced A (i.e., the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | // referred to by the reference) can be more cv-qualified than the |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 388 | // transformed A. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 389 | if (TDF & TDF_ParamWithReferenceType) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | unsigned ExtraQualsOnParam |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 391 | = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers(); |
| 392 | Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam); |
| 393 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 395 | // If the parameter type is not dependent, there is nothing to deduce. |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 396 | if (!Param->isDependentType()) { |
| 397 | if (!(TDF & TDF_SkipNonDependent) && Param != Arg) { |
| 398 | |
| 399 | return Sema::TDK_NonDeducedMismatch; |
| 400 | } |
| 401 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 402 | return Sema::TDK_Success; |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 403 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 405 | // C++ [temp.deduct.type]p9: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | // A template type argument T, a template template argument TT or a |
| 407 | // template non-type argument i can be deduced if P and A have one of |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 408 | // the following forms: |
| 409 | // |
| 410 | // T |
| 411 | // cv-list T |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | if (const TemplateTypeParmType *TemplateTypeParm |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 413 | = Param->getAsTemplateTypeParmType()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 414 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 415 | bool RecanonicalizeArg = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | 9e9fae4 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 417 | // If the argument type is an array type, move the qualifiers up to the |
| 418 | // top level, so they can be matched with the qualifiers on the parameter. |
| 419 | // FIXME: address spaces, ObjC GC qualifiers |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 420 | if (isa<ArrayType>(Arg)) { |
| 421 | unsigned CVRQuals = 0; |
| 422 | Arg = getUnqualifiedArrayType(Context, Arg, CVRQuals); |
| 423 | if (CVRQuals) { |
| 424 | Arg = Arg.getWithAdditionalQualifiers(CVRQuals); |
| 425 | RecanonicalizeArg = true; |
| 426 | } |
| 427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 429 | // The argument type can not be less qualified than the parameter |
| 430 | // type. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 431 | if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 432 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 433 | Info.FirstArg = Deduced[Index]; |
| 434 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 435 | return Sema::TDK_InconsistentQuals; |
| 436 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 437 | |
| 438 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 440 | unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 441 | QualType DeducedType = Arg.getQualifiedType(Quals); |
| 442 | if (RecanonicalizeArg) |
| 443 | DeducedType = Context.getCanonicalType(DeducedType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 445 | if (Deduced[Index].isNull()) |
| 446 | Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); |
| 447 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | // C++ [temp.deduct.type]p2: |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 449 | // [...] If type deduction cannot be done for any P/A pair, or if for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | // any pair the deduction leads to more than one possible set of |
| 451 | // deduced values, or if different pairs yield different deduced |
| 452 | // values, or if any template argument remains neither deduced nor |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 453 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 454 | if (Deduced[Index].getAsType() != DeducedType) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | Info.Param |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 456 | = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 457 | Info.FirstArg = Deduced[Index]; |
| 458 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 459 | return Sema::TDK_Inconsistent; |
| 460 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 461 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 462 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 465 | // Set up the template argument deduction information for a failure. |
| 466 | Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); |
| 467 | Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); |
| 468 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 469 | // Check the cv-qualifiers on the parameter and argument types. |
| 470 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 471 | if (TDF & TDF_ParamWithReferenceType) { |
| 472 | if (Param.isMoreQualifiedThan(Arg)) |
| 473 | return Sema::TDK_NonDeducedMismatch; |
| 474 | } else { |
| 475 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 479 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 480 | switch (Param->getTypeClass()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 481 | // No deduction possible for these types |
| 482 | case Type::Builtin: |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 483 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 485 | // T * |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 486 | case Type::Pointer: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 487 | const PointerType *PointerArg = Arg->getAs<PointerType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 488 | if (!PointerArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 489 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 491 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 492 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 493 | cast<PointerType>(Param)->getPointeeType(), |
| 494 | PointerArg->getPointeeType(), |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 495 | Info, Deduced, SubTDF); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 498 | // T & |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 499 | case Type::LValueReference: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 500 | const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 501 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 502 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 504 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 505 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 506 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 507 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 508 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 509 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 510 | // T && [C++0x] |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 511 | case Type::RValueReference: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 512 | const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 513 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 514 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 516 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 517 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 518 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 519 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 522 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 523 | case Type::IncompleteArray: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | const IncompleteArrayType *IncompleteArrayArg = |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 525 | Context.getAsIncompleteArrayType(Arg); |
| 526 | if (!IncompleteArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 527 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 529 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 530 | Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 531 | IncompleteArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 532 | Info, Deduced, 0); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 533 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 534 | |
| 535 | // T [integer-constant] |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 536 | case Type::ConstantArray: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | const ConstantArrayType *ConstantArrayArg = |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 538 | Context.getAsConstantArrayType(Arg); |
| 539 | if (!ConstantArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 540 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
| 542 | const ConstantArrayType *ConstantArrayParm = |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 543 | Context.getAsConstantArrayType(Param); |
| 544 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 545 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 547 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 548 | ConstantArrayParm->getElementType(), |
| 549 | ConstantArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 550 | Info, Deduced, 0); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 553 | // type [i] |
| 554 | case Type::DependentSizedArray: { |
| 555 | const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); |
| 556 | if (!ArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 557 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 559 | // Check the element type of the arrays |
| 560 | const DependentSizedArrayType *DependentArrayParm |
| 561 | = cast<DependentSizedArrayType>(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 562 | if (Sema::TemplateDeductionResult Result |
| 563 | = DeduceTemplateArguments(Context, TemplateParams, |
| 564 | DependentArrayParm->getElementType(), |
| 565 | ArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 566 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 567 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 569 | // Determine the array bound is something we can deduce. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 571 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 572 | if (!NTTP) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 573 | return Sema::TDK_Success; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
| 575 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 576 | // template parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 578 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 580 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 581 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
| 582 | return DeduceNonTypeTemplateArgument(Context, NTTP, Size, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 583 | Info, Deduced); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 584 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 585 | if (const DependentSizedArrayType *DependentArrayArg |
| 586 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| 587 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 588 | DependentArrayArg->getSizeExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 589 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 591 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 592 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 593 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
| 595 | // type(*)(T) |
| 596 | // T(*)() |
| 597 | // T(*)(T) |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 598 | case Type::FunctionProto: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 600 | dyn_cast<FunctionProtoType>(Arg); |
| 601 | if (!FunctionProtoArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 602 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | |
| 604 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 605 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 606 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | if (FunctionProtoParam->getTypeQuals() != |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 608 | FunctionProtoArg->getTypeQuals()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 609 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 611 | if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 612 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 614 | if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 615 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 616 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 617 | // Check return types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 618 | if (Sema::TemplateDeductionResult Result |
| 619 | = DeduceTemplateArguments(Context, TemplateParams, |
| 620 | FunctionProtoParam->getResultType(), |
| 621 | FunctionProtoArg->getResultType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 622 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 623 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 625 | for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { |
| 626 | // Check argument types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 627 | if (Sema::TemplateDeductionResult Result |
| 628 | = DeduceTemplateArguments(Context, TemplateParams, |
| 629 | FunctionProtoParam->getArgType(I), |
| 630 | FunctionProtoArg->getArgType(I), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 631 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 632 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 635 | return Sema::TDK_Success; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 638 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 639 | // template-name<i> |
| 640 | // TT<T> (TODO) |
| 641 | // TT<i> (TODO) |
| 642 | // TT<> (TODO) |
| 643 | case Type::TemplateSpecialization: { |
| 644 | const TemplateSpecializationType *SpecParam |
| 645 | = cast<TemplateSpecializationType>(Param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 647 | // Try to deduce template arguments from the template-id. |
| 648 | Sema::TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 650 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
| 652 | if (Result && (TDF & TDF_DerivedClass) && |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 653 | Result != Sema::TDK_Inconsistent) { |
| 654 | // C++ [temp.deduct.call]p3b3: |
| 655 | // If P is a class, and P has the form template-id, then A can be a |
| 656 | // derived class of the deduced A. Likewise, if P is a pointer to a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | // class of the form template-id, A can be a pointer to a derived |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 658 | // class pointed to by the deduced A. |
| 659 | // |
| 660 | // More importantly: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | // These alternatives are considered only if type deduction would |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 662 | // otherwise fail. |
| 663 | if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) { |
| 664 | // Use data recursion to crawl through the list of base classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | // Visited contains the set of nodes we have already visited, while |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 666 | // ToVisit is our stack of records that we still need to visit. |
| 667 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 668 | llvm::SmallVector<const RecordType *, 8> ToVisit; |
| 669 | ToVisit.push_back(RecordT); |
| 670 | bool Successful = false; |
| 671 | while (!ToVisit.empty()) { |
| 672 | // Retrieve the next class in the inheritance hierarchy. |
| 673 | const RecordType *NextT = ToVisit.back(); |
| 674 | ToVisit.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 676 | // If we have already seen this type, skip it. |
| 677 | if (!Visited.insert(NextT)) |
| 678 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 680 | // If this is a base class, try to perform template argument |
| 681 | // deduction from it. |
| 682 | if (NextT != RecordT) { |
| 683 | Sema::TemplateDeductionResult BaseResult |
| 684 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, |
| 685 | QualType(NextT, 0), Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 687 | // If template argument deduction for this base was successful, |
| 688 | // note that we had some success. |
| 689 | if (BaseResult == Sema::TDK_Success) |
| 690 | Successful = true; |
| 691 | // If deduction against this base resulted in an inconsistent |
| 692 | // set of deduced template arguments, template argument |
| 693 | // deduction fails. |
| 694 | else if (BaseResult == Sema::TDK_Inconsistent) |
| 695 | return BaseResult; |
| 696 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 698 | // Visit base classes |
| 699 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 700 | for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), |
| 701 | BaseEnd = Next->bases_end(); |
| 702 | Base != BaseEnd; ++Base) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | assert(Base->getType()->isRecordType() && |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 704 | "Base class that isn't a record?"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 705 | ToVisit.push_back(Base->getType()->getAs<RecordType>()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 709 | if (Successful) |
| 710 | return Sema::TDK_Success; |
| 711 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 715 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 718 | // T type::* |
| 719 | // T T::* |
| 720 | // T (type::*)() |
| 721 | // type (T::*)() |
| 722 | // type (type::*)(T) |
| 723 | // type (T::*)(T) |
| 724 | // T (type::*)(T) |
| 725 | // T (T::*)() |
| 726 | // T (T::*)(T) |
| 727 | case Type::MemberPointer: { |
| 728 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 729 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 730 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 731 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 733 | if (Sema::TemplateDeductionResult Result |
| 734 | = DeduceTemplateArguments(Context, TemplateParams, |
| 735 | MemPtrParam->getPointeeType(), |
| 736 | MemPtrArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 737 | Info, Deduced, |
| 738 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 739 | return Result; |
| 740 | |
| 741 | return DeduceTemplateArguments(Context, TemplateParams, |
| 742 | QualType(MemPtrParam->getClass(), 0), |
| 743 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 744 | Info, Deduced, 0); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 747 | // (clang extension) |
| 748 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | // type(^)(T) |
| 750 | // T(^)() |
| 751 | // T(^)(T) |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 752 | case Type::BlockPointer: { |
| 753 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 754 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 756 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 757 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 759 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 760 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 761 | BlockPtrArg->getPointeeType(), Info, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 762 | Deduced, 0); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 765 | case Type::TypeOfExpr: |
| 766 | case Type::TypeOf: |
| 767 | case Type::Typename: |
| 768 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 769 | return Sema::TDK_Success; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 770 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 771 | default: |
| 772 | break; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 776 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 779 | static Sema::TemplateDeductionResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 781 | TemplateParameterList *TemplateParams, |
| 782 | const TemplateArgument &Param, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 783 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 784 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 785 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 786 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 787 | case TemplateArgument::Null: |
| 788 | assert(false && "Null template argument in parameter list"); |
| 789 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
| 791 | case TemplateArgument::Type: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 792 | assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 793 | return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), |
| 794 | Arg.getAsType(), Info, Deduced, 0); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 796 | case TemplateArgument::Declaration: |
| 797 | // FIXME: Implement this check |
| 798 | assert(false && "Unimplemented template argument deduction case"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 799 | Info.FirstArg = Param; |
| 800 | Info.SecondArg = Arg; |
| 801 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 803 | case TemplateArgument::Integral: |
| 804 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 805 | // FIXME: Zero extension + sign checking here? |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 806 | if (*Param.getAsIntegral() == *Arg.getAsIntegral()) |
| 807 | return Sema::TDK_Success; |
| 808 | |
| 809 | Info.FirstArg = Param; |
| 810 | Info.SecondArg = Arg; |
| 811 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 812 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 813 | |
| 814 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 815 | Info.FirstArg = Param; |
| 816 | Info.SecondArg = Arg; |
| 817 | return Sema::TDK_NonDeducedMismatch; |
| 818 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 819 | |
| 820 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 821 | Info.FirstArg = Param; |
| 822 | Info.SecondArg = Arg; |
| 823 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 825 | case TemplateArgument::Expression: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 827 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 828 | if (Arg.getKind() == TemplateArgument::Integral) |
| 829 | // FIXME: Sign problems here |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 831 | *Arg.getAsIntegral(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 832 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 833 | if (Arg.getKind() == TemplateArgument::Expression) |
| 834 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 835 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 837 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 838 | Info.FirstArg = Param; |
| 839 | Info.SecondArg = Arg; |
| 840 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 843 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 844 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 845 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 846 | case TemplateArgument::Pack: |
| 847 | assert(0 && "FIXME: Implement!"); |
| 848 | break; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 851 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | static Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 855 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 856 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 857 | const TemplateArgumentList &ParamList, |
| 858 | const TemplateArgumentList &ArgList, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 859 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 860 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 861 | assert(ParamList.size() == ArgList.size()); |
| 862 | for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 863 | if (Sema::TemplateDeductionResult Result |
| 864 | = DeduceTemplateArguments(Context, TemplateParams, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | ParamList[I], ArgList[I], |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 866 | Info, Deduced)) |
| 867 | return Result; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 869 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 872 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 874 | const TemplateArgument &X, |
| 875 | const TemplateArgument &Y) { |
| 876 | if (X.getKind() != Y.getKind()) |
| 877 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 879 | switch (X.getKind()) { |
| 880 | case TemplateArgument::Null: |
| 881 | assert(false && "Comparing NULL template argument"); |
| 882 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 884 | case TemplateArgument::Type: |
| 885 | return Context.getCanonicalType(X.getAsType()) == |
| 886 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 888 | case TemplateArgument::Declaration: |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 889 | return X.getAsDecl()->getCanonicalDecl() == |
| 890 | Y.getAsDecl()->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 892 | case TemplateArgument::Integral: |
| 893 | return *X.getAsIntegral() == *Y.getAsIntegral(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 895 | case TemplateArgument::Expression: |
| 896 | // FIXME: We assume that all expressions are distinct, but we should |
| 897 | // really check their canonical forms. |
| 898 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 900 | case TemplateArgument::Pack: |
| 901 | if (X.pack_size() != Y.pack_size()) |
| 902 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
| 904 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 905 | XPEnd = X.pack_end(), |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 906 | YP = Y.pack_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 908 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 909 | return false; |
| 910 | |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | /// \brief Helper function to build a TemplateParameter when we don't |
| 918 | /// know its type statically. |
| 919 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 920 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 921 | return TemplateParameter(TTP); |
| 922 | else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 923 | return TemplateParameter(NTTP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 925 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 926 | } |
| 927 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 928 | /// \brief Perform template argument deduction to determine whether |
| 929 | /// the given template arguments match the given class template |
| 930 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 931 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 932 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 933 | const TemplateArgumentList &TemplateArgs, |
| 934 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 935 | // C++ [temp.class.spec.match]p2: |
| 936 | // A partial specialization matches a given actual template |
| 937 | // argument list if the template arguments of the partial |
| 938 | // specialization can be deduced from the actual template argument |
| 939 | // list (14.8.2). |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 940 | SFINAETrap Trap(*this); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 941 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 942 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 943 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | = ::DeduceTemplateArguments(Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 945 | Partial->getTemplateParameters(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | Partial->getTemplateArgs(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 947 | TemplateArgs, Info, Deduced)) |
| 948 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 950 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
| 951 | Deduced.data(), Deduced.size()); |
| 952 | if (Inst) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 953 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 955 | // C++ [temp.deduct.type]p2: |
| 956 | // [...] or if any template argument remains neither deduced nor |
| 957 | // explicitly specified, template argument deduction fails. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 958 | TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), |
| 959 | Deduced.size()); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 960 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 961 | if (Deduced[I].isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | Decl *Param |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 963 | = const_cast<NamedDecl *>( |
| 964 | Partial->getTemplateParameters()->getParam(I)); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 965 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 966 | Info.Param = TTP; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 968 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 969 | Info.Param = NTTP; |
| 970 | else |
| 971 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 972 | return TDK_Incomplete; |
| 973 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 974 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 975 | Builder.Append(Deduced[I]); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | // Form the template argument list from the deduced template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | TemplateArgumentList *DeducedArgumentList |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 980 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 981 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 983 | // Substitute the deduced template arguments into the template |
| 984 | // arguments of the class template partial specialization, and |
| 985 | // verify that the instantiated template arguments are both valid |
| 986 | // and are equivalent to the template arguments originally provided |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | // to the class template. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 988 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 989 | const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); |
| 990 | for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 991 | Decl *Param = const_cast<NamedDecl *>( |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 992 | ClassTemplate->getTemplateParameters()->getParam(I)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | TemplateArgument InstArg |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 994 | = Subst(PartialTemplateArgs[I], |
| 995 | MultiLevelTemplateArgumentList(*DeducedArgumentList)); |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 996 | if (InstArg.isNull()) { |
| 997 | Info.Param = makeTemplateParameter(Param); |
| 998 | Info.FirstArg = PartialTemplateArgs[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1002 | if (InstArg.getKind() == TemplateArgument::Expression) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | // When the argument is an expression, check the expression result |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1004 | // against the actual template parameter to get down to the canonical |
| 1005 | // template argument. |
| 1006 | Expr *InstExpr = InstArg.getAsExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1008 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1009 | if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { |
| 1010 | Info.Param = makeTemplateParameter(Param); |
| 1011 | Info.FirstArg = PartialTemplateArgs[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | return TDK_SubstitutionFailure; |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | } else if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1015 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 1016 | // FIXME: template template arguments should really resolve to decls |
| 1017 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); |
| 1018 | if (!DRE || CheckTemplateArgument(TTP, DRE)) { |
| 1019 | Info.Param = makeTemplateParameter(Param); |
| 1020 | Info.FirstArg = PartialTemplateArgs[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | return TDK_SubstitutionFailure; |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1026 | if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { |
| 1027 | Info.Param = makeTemplateParameter(Param); |
| 1028 | Info.FirstArg = TemplateArgs[I]; |
| 1029 | Info.SecondArg = InstArg; |
| 1030 | return TDK_NonDeducedMismatch; |
| 1031 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 1034 | if (Trap.hasErrorOccurred()) |
| 1035 | return TDK_SubstitutionFailure; |
| 1036 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1037 | return TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1038 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1040 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 1041 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | if (const TemplateSpecializationType *Spec |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1043 | = T->getAsTemplateSpecializationType()) |
| 1044 | return Spec->getTemplateName().getAsTemplateDecl() != 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1048 | |
| 1049 | /// \brief Substitute the explicitly-provided template arguments into the |
| 1050 | /// given function template according to C++ [temp.arg.explicit]. |
| 1051 | /// |
| 1052 | /// \param FunctionTemplate the function template into which the explicit |
| 1053 | /// template arguments will be substituted. |
| 1054 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | /// \param ExplicitTemplateArguments the explicitly-specified template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1056 | /// arguments. |
| 1057 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | /// \param NumExplicitTemplateArguments the number of explicitly-specified |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1059 | /// template arguments in @p ExplicitTemplateArguments. This value may be zero. |
| 1060 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1062 | /// with the converted and checked explicit template arguments. |
| 1063 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1065 | /// parameters. |
| 1066 | /// |
| 1067 | /// \param FunctionType if non-NULL, the result type of the function template |
| 1068 | /// will also be instantiated and the pointed-to value will be updated with |
| 1069 | /// the instantiated function type. |
| 1070 | /// |
| 1071 | /// \param Info if substitution fails for any reason, this object will be |
| 1072 | /// populated with more information about the failure. |
| 1073 | /// |
| 1074 | /// \returns TDK_Success if substitution was successful, or some failure |
| 1075 | /// condition. |
| 1076 | Sema::TemplateDeductionResult |
| 1077 | Sema::SubstituteExplicitTemplateArguments( |
| 1078 | FunctionTemplateDecl *FunctionTemplate, |
| 1079 | const TemplateArgument *ExplicitTemplateArgs, |
| 1080 | unsigned NumExplicitTemplateArgs, |
| 1081 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1082 | llvm::SmallVectorImpl<QualType> &ParamTypes, |
| 1083 | QualType *FunctionType, |
| 1084 | TemplateDeductionInfo &Info) { |
| 1085 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1086 | TemplateParameterList *TemplateParams |
| 1087 | = FunctionTemplate->getTemplateParameters(); |
| 1088 | |
| 1089 | if (NumExplicitTemplateArgs == 0) { |
| 1090 | // No arguments to substitute; just copy over the parameter types and |
| 1091 | // fill in the function type. |
| 1092 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1093 | PEnd = Function->param_end(); |
| 1094 | P != PEnd; |
| 1095 | ++P) |
| 1096 | ParamTypes.push_back((*P)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1098 | if (FunctionType) |
| 1099 | *FunctionType = Function->getType(); |
| 1100 | return TDK_Success; |
| 1101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1103 | // Substitution of the explicit template arguments into a function template |
| 1104 | /// is a SFINAE context. Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | SFINAETrap Trap(*this); |
| 1106 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1107 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | // Template arguments that are present shall be specified in the |
| 1109 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1110 | // template argument list shall not specify more template-arguments than |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | // there are corresponding template-parameters. |
| 1112 | TemplateArgumentListBuilder Builder(TemplateParams, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1113 | NumExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
| 1115 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1116 | // explicitly-specified template arguments against this function template, |
| 1117 | // and then substitute them into the function parameter types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1119 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1120 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); |
| 1121 | if (Inst) |
| 1122 | return TDK_InstantiationDepth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1124 | if (CheckTemplateArgumentList(FunctionTemplate, |
| 1125 | SourceLocation(), SourceLocation(), |
| 1126 | ExplicitTemplateArgs, |
| 1127 | NumExplicitTemplateArgs, |
| 1128 | SourceLocation(), |
| 1129 | true, |
| 1130 | Builder) || Trap.hasErrorOccurred()) |
| 1131 | return TDK_InvalidExplicitArguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1133 | // Form the template argument list from the explicitly-specified |
| 1134 | // template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1136 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1137 | Info.reset(ExplicitArgumentList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1139 | // Instantiate the types of each of the function parameters given the |
| 1140 | // explicitly-specified template arguments. |
| 1141 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1142 | PEnd = Function->param_end(); |
| 1143 | P != PEnd; |
| 1144 | ++P) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | QualType ParamType |
| 1146 | = SubstType((*P)->getType(), |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1147 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1148 | (*P)->getLocation(), (*P)->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1149 | if (ParamType.isNull() || Trap.hasErrorOccurred()) |
| 1150 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1152 | ParamTypes.push_back(ParamType); |
| 1153 | } |
| 1154 | |
| 1155 | // If the caller wants a full function type back, instantiate the return |
| 1156 | // type and form that function type. |
| 1157 | if (FunctionType) { |
| 1158 | // FIXME: exception-specifications? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | const FunctionProtoType *Proto |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1160 | = Function->getType()->getAsFunctionProtoType(); |
| 1161 | assert(Proto && "Function template does not have a prototype?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
| 1163 | QualType ResultType |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1164 | = SubstType(Proto->getResultType(), |
| 1165 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1166 | Function->getTypeSpecStartLoc(), |
| 1167 | Function->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1168 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 1169 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
| 1171 | *FunctionType = BuildFunctionType(ResultType, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1172 | ParamTypes.data(), ParamTypes.size(), |
| 1173 | Proto->isVariadic(), |
| 1174 | Proto->getTypeQuals(), |
| 1175 | Function->getLocation(), |
| 1176 | Function->getDeclName()); |
| 1177 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 1178 | return TDK_SubstitutionFailure; |
| 1179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1181 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 1183 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1184 | // template arguments can be deduced, they may all be omitted; in this |
| 1185 | // case, the empty template argument list <> itself may also be omitted. |
| 1186 | // |
| 1187 | // Take all of the explicitly-specified arguments and put them into the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | // set of deduced template arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1189 | Deduced.reserve(TemplateParams->size()); |
| 1190 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | Deduced.push_back(ExplicitArgumentList->get(I)); |
| 1192 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1193 | return TDK_Success; |
| 1194 | } |
| 1195 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1197 | /// checking the deduced template arguments for completeness and forming |
| 1198 | /// the function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | Sema::TemplateDeductionResult |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1200 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
| 1201 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1202 | FunctionDecl *&Specialization, |
| 1203 | TemplateDeductionInfo &Info) { |
| 1204 | TemplateParameterList *TemplateParams |
| 1205 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1207 | // C++ [temp.deduct.type]p2: |
| 1208 | // [...] or if any template argument remains neither deduced nor |
| 1209 | // explicitly specified, template argument deduction fails. |
| 1210 | TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); |
| 1211 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 1212 | if (Deduced[I].isNull()) { |
| 1213 | Info.Param = makeTemplateParameter( |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1214 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1215 | return TDK_Incomplete; |
| 1216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1218 | Builder.Append(Deduced[I]); |
| 1219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1221 | // Form the template argument list from the deduced template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1223 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1224 | Info.reset(DeducedArgumentList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1226 | // Template argument deduction for function templates in a SFINAE context. |
| 1227 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | SFINAETrap Trap(*this); |
| 1229 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1230 | // Enter a new template instantiation context while we instantiate the |
| 1231 | // actual function declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1233 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1234 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); |
| 1235 | if (Inst) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | return TDK_InstantiationDepth; |
| 1237 | |
| 1238 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1239 | // declaration to produce the function template specialization. |
| 1240 | Specialization = cast_or_null<FunctionDecl>( |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1241 | SubstDecl(FunctionTemplate->getTemplatedDecl(), |
| 1242 | FunctionTemplate->getDeclContext(), |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1243 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1244 | if (!Specialization) |
| 1245 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | f882574 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 1247 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
| 1248 | FunctionTemplate->getCanonicalDecl()); |
| 1249 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | // If the template argument list is owned by the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1251 | // specialization, release it. |
| 1252 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) |
| 1253 | Info.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1255 | // There may have been an error that did not prevent us from constructing a |
| 1256 | // declaration. Mark the declaration invalid and return with a substitution |
| 1257 | // failure. |
| 1258 | if (Trap.hasErrorOccurred()) { |
| 1259 | Specialization->setInvalidDecl(true); |
| 1260 | return TDK_SubstitutionFailure; |
| 1261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
| 1263 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1266 | /// \brief Perform template argument deduction from a function call |
| 1267 | /// (C++ [temp.deduct.call]). |
| 1268 | /// |
| 1269 | /// \param FunctionTemplate the function template for which we are performing |
| 1270 | /// template argument deduction. |
| 1271 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | /// \param HasExplicitTemplateArgs whether any template arguments were |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1273 | /// explicitly specified. |
| 1274 | /// |
| 1275 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1276 | /// the explicitly-specified template arguments. |
| 1277 | /// |
| 1278 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | /// the number of explicitly-specified template arguments in |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1280 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 1281 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1282 | /// \param Args the function call arguments |
| 1283 | /// |
| 1284 | /// \param NumArgs the number of arguments in Args |
| 1285 | /// |
| 1286 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | /// this will be set to the function template specialization produced by |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1288 | /// template argument deduction. |
| 1289 | /// |
| 1290 | /// \param Info the argument will be updated to provide additional information |
| 1291 | /// about template argument deduction. |
| 1292 | /// |
| 1293 | /// \returns the result of template argument deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1294 | Sema::TemplateDeductionResult |
| 1295 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1296 | bool HasExplicitTemplateArgs, |
| 1297 | const TemplateArgument *ExplicitTemplateArgs, |
| 1298 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1299 | Expr **Args, unsigned NumArgs, |
| 1300 | FunctionDecl *&Specialization, |
| 1301 | TemplateDeductionInfo &Info) { |
| 1302 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1304 | // C++ [temp.deduct.call]p1: |
| 1305 | // Template argument deduction is done by comparing each function template |
| 1306 | // parameter type (call it P) with the type of the corresponding argument |
| 1307 | // of the call (call it A) as described below. |
| 1308 | unsigned CheckArgs = NumArgs; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1309 | if (NumArgs < Function->getMinRequiredArguments()) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1310 | return TDK_TooFewArguments; |
| 1311 | else if (NumArgs > Function->getNumParams()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | const FunctionProtoType *Proto |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1313 | = Function->getType()->getAsFunctionProtoType(); |
| 1314 | if (!Proto->isVariadic()) |
| 1315 | return TDK_TooManyArguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1317 | CheckArgs = Function->getNumParams(); |
| 1318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1320 | // The types of the parameters from which we will perform template argument |
| 1321 | // deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1322 | TemplateParameterList *TemplateParams |
| 1323 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1324 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1325 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1326 | if (NumExplicitTemplateArgs) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1327 | TemplateDeductionResult Result = |
| 1328 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
| 1329 | ExplicitTemplateArgs, |
| 1330 | NumExplicitTemplateArgs, |
| 1331 | Deduced, |
| 1332 | ParamTypes, |
| 1333 | 0, |
| 1334 | Info); |
| 1335 | if (Result) |
| 1336 | return Result; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1337 | } else { |
| 1338 | // Just fill in the parameter types from the function declaration. |
| 1339 | for (unsigned I = 0; I != CheckArgs; ++I) |
| 1340 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 1341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1343 | // Deduce template arguments from the function parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1345 | for (unsigned I = 0; I != CheckArgs; ++I) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1346 | QualType ParamType = ParamTypes[I]; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1347 | QualType ArgType = Args[I]->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1349 | // C++ [temp.deduct.call]p2: |
| 1350 | // If P is not a reference type: |
| 1351 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1352 | bool ParamWasReference = isa<ReferenceType>(CanonParamType); |
| 1353 | if (!ParamWasReference) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | // - If A is an array type, the pointer type produced by the |
| 1355 | // array-to-pointer standard conversion (4.2) is used in place of |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1356 | // A for type deduction; otherwise, |
| 1357 | if (ArgType->isArrayType()) |
| 1358 | ArgType = Context.getArrayDecayedType(ArgType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | // - If A is a function type, the pointer type produced by the |
| 1360 | // function-to-pointer standard conversion (4.3) is used in place |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1361 | // of A for type deduction; otherwise, |
| 1362 | else if (ArgType->isFunctionType()) |
| 1363 | ArgType = Context.getPointerType(ArgType); |
| 1364 | else { |
| 1365 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1366 | // type are ignored for type deduction. |
| 1367 | QualType CanonArgType = Context.getCanonicalType(ArgType); |
| 1368 | if (CanonArgType.getCVRQualifiers()) |
| 1369 | ArgType = CanonArgType.getUnqualifiedType(); |
| 1370 | } |
| 1371 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1373 | // C++0x [temp.deduct.call]p3: |
| 1374 | // If P is a cv-qualified type, the top level cv-qualifiers of P’s type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | // are ignored for type deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1376 | if (CanonParamType.getCVRQualifiers()) |
| 1377 | ParamType = CanonParamType.getUnqualifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1378 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | // [...] If P is a reference type, the type referred to by P is used |
| 1380 | // for type deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1381 | ParamType = ParamRefType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
| 1383 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 1384 | // the argument is an lvalue, the type A& is used in place of A for |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1385 | // type deduction. |
| 1386 | if (isa<RValueReferenceType>(ParamRefType) && |
| 1387 | ParamRefType->getAsTemplateTypeParmType() && |
| 1388 | Args[I]->isLvalue(Context) == Expr::LV_Valid) |
| 1389 | ArgType = Context.getLValueReferenceType(ArgType); |
| 1390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1392 | // C++0x [temp.deduct.call]p4: |
| 1393 | // In general, the deduction process attempts to find template argument |
| 1394 | // values that will make the deduced A identical to A (after the type A |
| 1395 | // is transformed as described above). [...] |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 1396 | unsigned TDF = TDF_SkipNonDependent; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1398 | // - If the original P is a reference type, the deduced A (i.e., the |
| 1399 | // type referred to by the reference) can be more cv-qualified than |
| 1400 | // the transformed A. |
| 1401 | if (ParamWasReference) |
| 1402 | TDF |= TDF_ParamWithReferenceType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | // - The transformed A can be another pointer or pointer to member |
| 1404 | // type that can be converted to the deduced A via a qualification |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1405 | // conversion (4.4). |
| 1406 | if (ArgType->isPointerType() || ArgType->isMemberPointerType()) |
| 1407 | TDF |= TDF_IgnoreQualifiers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | // - If P is a class and P has the form simple-template-id, then the |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1409 | // transformed A can be a derived class of the deduced A. Likewise, |
| 1410 | // if P is a pointer to a class of the form simple-template-id, the |
| 1411 | // transformed A can be a pointer to a derived class pointed to by |
| 1412 | // the deduced A. |
| 1413 | if (isSimpleTemplateIdType(ParamType) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | (isa<PointerType>(ParamType) && |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1415 | isSimpleTemplateIdType( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1416 | ParamType->getAs<PointerType>()->getPointeeType()))) |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1417 | TDF |= TDF_DerivedClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1419 | if (TemplateDeductionResult Result |
| 1420 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1421 | ParamType, ArgType, Info, Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1422 | TDF)) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1423 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | 8fdc3c4 | 2009-07-07 23:12:18 +0000 | [diff] [blame] | 1425 | // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | // pointer parameters. |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1427 | |
| 1428 | // FIXME: we need to check that the deduced A is the same as A, |
| 1429 | // modulo the various allowed differences. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1430 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1431 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1433 | Specialization, Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1436 | /// \brief Deduce template arguments when taking the address of a function |
| 1437 | /// template (C++ [temp.deduct.funcaddr]). |
| 1438 | /// |
| 1439 | /// \param FunctionTemplate the function template for which we are performing |
| 1440 | /// template argument deduction. |
| 1441 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | /// \param HasExplicitTemplateArgs whether any template arguments were |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1443 | /// explicitly specified. |
| 1444 | /// |
| 1445 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1446 | /// the explicitly-specified template arguments. |
| 1447 | /// |
| 1448 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | /// the number of explicitly-specified template arguments in |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1450 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 1451 | /// |
| 1452 | /// \param ArgFunctionType the function type that will be used as the |
| 1453 | /// "argument" type (A) when performing template argument deduction from the |
| 1454 | /// function template's function type. |
| 1455 | /// |
| 1456 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1458 | /// template argument deduction. |
| 1459 | /// |
| 1460 | /// \param Info the argument will be updated to provide additional information |
| 1461 | /// about template argument deduction. |
| 1462 | /// |
| 1463 | /// \returns the result of template argument deduction. |
| 1464 | Sema::TemplateDeductionResult |
| 1465 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1466 | bool HasExplicitTemplateArgs, |
| 1467 | const TemplateArgument *ExplicitTemplateArgs, |
| 1468 | unsigned NumExplicitTemplateArgs, |
| 1469 | QualType ArgFunctionType, |
| 1470 | FunctionDecl *&Specialization, |
| 1471 | TemplateDeductionInfo &Info) { |
| 1472 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1473 | TemplateParameterList *TemplateParams |
| 1474 | = FunctionTemplate->getTemplateParameters(); |
| 1475 | QualType FunctionType = Function->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1477 | // Substitute any explicit template arguments. |
| 1478 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1479 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1480 | if (HasExplicitTemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | if (TemplateDeductionResult Result |
| 1482 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
| 1483 | ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1484 | NumExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | Deduced, ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1486 | &FunctionType, Info)) |
| 1487 | return Result; |
| 1488 | } |
| 1489 | |
| 1490 | // Template argument deduction for function templates in a SFINAE context. |
| 1491 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | SFINAETrap Trap(*this); |
| 1493 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1494 | // Deduce template arguments from the function type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1496 | if (TemplateDeductionResult Result |
| 1497 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | FunctionType, ArgFunctionType, Info, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1499 | Deduced, 0)) |
| 1500 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
| 1502 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1503 | Specialization, Info); |
| 1504 | } |
| 1505 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1506 | /// \brief Deduce template arguments for a templated conversion |
| 1507 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 1508 | /// conversion function template specialization. |
| 1509 | Sema::TemplateDeductionResult |
| 1510 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1511 | QualType ToType, |
| 1512 | CXXConversionDecl *&Specialization, |
| 1513 | TemplateDeductionInfo &Info) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | CXXConversionDecl *Conv |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1515 | = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); |
| 1516 | QualType FromType = Conv->getConversionType(); |
| 1517 | |
| 1518 | // Canonicalize the types for deduction. |
| 1519 | QualType P = Context.getCanonicalType(FromType); |
| 1520 | QualType A = Context.getCanonicalType(ToType); |
| 1521 | |
| 1522 | // C++0x [temp.deduct.conv]p3: |
| 1523 | // If P is a reference type, the type referred to by P is used for |
| 1524 | // type deduction. |
| 1525 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 1526 | P = PRef->getPointeeType(); |
| 1527 | |
| 1528 | // C++0x [temp.deduct.conv]p3: |
| 1529 | // If A is a reference type, the type referred to by A is used |
| 1530 | // for type deduction. |
| 1531 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 1532 | A = ARef->getPointeeType(); |
| 1533 | // C++ [temp.deduct.conv]p2: |
| 1534 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | // If A is not a reference type: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1536 | else { |
| 1537 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 1538 | |
| 1539 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1541 | // of P for type deduction; otherwise, |
| 1542 | if (P->isArrayType()) |
| 1543 | P = Context.getArrayDecayedType(P); |
| 1544 | // - If P is a function type, the pointer type produced by the |
| 1545 | // function-to-pointer standard conversion (4.3) is used in |
| 1546 | // place of P for type deduction; otherwise, |
| 1547 | else if (P->isFunctionType()) |
| 1548 | P = Context.getPointerType(P); |
| 1549 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
| 1550 | // P’s type are ignored for type deduction. |
| 1551 | else |
| 1552 | P = P.getUnqualifiedType(); |
| 1553 | |
| 1554 | // C++0x [temp.deduct.conv]p3: |
| 1555 | // If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1556 | // type are ignored for type deduction. |
| 1557 | A = A.getUnqualifiedType(); |
| 1558 | } |
| 1559 | |
| 1560 | // Template argument deduction for function templates in a SFINAE context. |
| 1561 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | SFINAETrap Trap(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1563 | |
| 1564 | // C++ [temp.deduct.conv]p1: |
| 1565 | // Template argument deduction is done by comparing the return |
| 1566 | // type of the template conversion function (call it P) with the |
| 1567 | // type that is required as the result of the conversion (call it |
| 1568 | // A) as described in 14.8.2.4. |
| 1569 | TemplateParameterList *TemplateParams |
| 1570 | = FunctionTemplate->getTemplateParameters(); |
| 1571 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1573 | |
| 1574 | // C++0x [temp.deduct.conv]p4: |
| 1575 | // In general, the deduction process attempts to find template |
| 1576 | // argument values that will make the deduced A identical to |
| 1577 | // A. However, there are two cases that allow a difference: |
| 1578 | unsigned TDF = 0; |
| 1579 | // - If the original A is a reference type, A can be more |
| 1580 | // cv-qualified than the deduced A (i.e., the type referred to |
| 1581 | // by the reference) |
| 1582 | if (ToType->isReferenceType()) |
| 1583 | TDF |= TDF_ParamWithReferenceType; |
| 1584 | // - The deduced A can be another pointer or pointer to member |
| 1585 | // type that can be converted to A via a qualification |
| 1586 | // conversion. |
| 1587 | // |
| 1588 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 1589 | // both P and A are pointers or member pointers. In this case, we |
| 1590 | // just ignore cv-qualifiers completely). |
| 1591 | if ((P->isPointerType() && A->isPointerType()) || |
| 1592 | (P->isMemberPointerType() && P->isMemberPointerType())) |
| 1593 | TDF |= TDF_IgnoreQualifiers; |
| 1594 | if (TemplateDeductionResult Result |
| 1595 | = ::DeduceTemplateArguments(Context, TemplateParams, |
| 1596 | P, A, Info, Deduced, TDF)) |
| 1597 | return Result; |
| 1598 | |
| 1599 | // FIXME: we need to check that the deduced A is the same as A, |
| 1600 | // modulo the various allowed differences. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1602 | // Finish template argument deduction. |
| 1603 | FunctionDecl *Spec = 0; |
| 1604 | TemplateDeductionResult Result |
| 1605 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info); |
| 1606 | Specialization = cast_or_null<CXXConversionDecl>(Spec); |
| 1607 | return Result; |
| 1608 | } |
| 1609 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1610 | /// \brief Stores the result of comparing the qualifiers of two types. |
| 1611 | enum DeductionQualifierComparison { |
| 1612 | NeitherMoreQualified = 0, |
| 1613 | ParamMoreQualified, |
| 1614 | ArgMoreQualified |
| 1615 | }; |
| 1616 | |
| 1617 | /// \brief Deduce the template arguments during partial ordering by comparing |
| 1618 | /// the parameter type and the argument type (C++0x [temp.deduct.partial]). |
| 1619 | /// |
| 1620 | /// \param Context the AST context in which this deduction occurs. |
| 1621 | /// |
| 1622 | /// \param TemplateParams the template parameters that we are deducing |
| 1623 | /// |
| 1624 | /// \param ParamIn the parameter type |
| 1625 | /// |
| 1626 | /// \param ArgIn the argument type |
| 1627 | /// |
| 1628 | /// \param Info information about the template argument deduction itself |
| 1629 | /// |
| 1630 | /// \param Deduced the deduced template arguments |
| 1631 | /// |
| 1632 | /// \returns the result of template argument deduction so far. Note that a |
| 1633 | /// "success" result means that template argument deduction has not yet failed, |
| 1634 | /// but it may still fail, later, for other reasons. |
| 1635 | static Sema::TemplateDeductionResult |
| 1636 | DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context, |
| 1637 | TemplateParameterList *TemplateParams, |
| 1638 | QualType ParamIn, QualType ArgIn, |
| 1639 | Sema::TemplateDeductionInfo &Info, |
| 1640 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1641 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 1642 | CanQualType Param = Context.getCanonicalType(ParamIn); |
| 1643 | CanQualType Arg = Context.getCanonicalType(ArgIn); |
| 1644 | |
| 1645 | // C++0x [temp.deduct.partial]p5: |
| 1646 | // Before the partial ordering is done, certain transformations are |
| 1647 | // performed on the types used for partial ordering: |
| 1648 | // - If P is a reference type, P is replaced by the type referred to. |
| 1649 | CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); |
| 1650 | if (ParamRef) |
| 1651 | Param = ParamRef->getPointeeType(); |
| 1652 | |
| 1653 | // - If A is a reference type, A is replaced by the type referred to. |
| 1654 | CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); |
| 1655 | if (ArgRef) |
| 1656 | Arg = ArgRef->getPointeeType(); |
| 1657 | |
| 1658 | if (QualifierComparisons && ParamRef && ArgRef) { |
| 1659 | // C++0x [temp.deduct.partial]p6: |
| 1660 | // If both P and A were reference types (before being replaced with the |
| 1661 | // type referred to above), determine which of the two types (if any) is |
| 1662 | // more cv-qualified than the other; otherwise the types are considered to |
| 1663 | // be equally cv-qualified for partial ordering purposes. The result of this |
| 1664 | // determination will be used below. |
| 1665 | // |
| 1666 | // We save this information for later, using it only when deduction |
| 1667 | // succeeds in both directions. |
| 1668 | DeductionQualifierComparison QualifierResult = NeitherMoreQualified; |
| 1669 | if (Param.isMoreQualifiedThan(Arg)) |
| 1670 | QualifierResult = ParamMoreQualified; |
| 1671 | else if (Arg.isMoreQualifiedThan(Param)) |
| 1672 | QualifierResult = ArgMoreQualified; |
| 1673 | QualifierComparisons->push_back(QualifierResult); |
| 1674 | } |
| 1675 | |
| 1676 | // C++0x [temp.deduct.partial]p7: |
| 1677 | // Remove any top-level cv-qualifiers: |
| 1678 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
| 1679 | // version of P. |
| 1680 | Param = Param.getUnqualifiedType(); |
| 1681 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
| 1682 | // version of A. |
| 1683 | Arg = Arg.getUnqualifiedType(); |
| 1684 | |
| 1685 | // C++0x [temp.deduct.partial]p8: |
| 1686 | // Using the resulting types P and A the deduction is then done as |
| 1687 | // described in 14.9.2.5. If deduction succeeds for a given type, the type |
| 1688 | // from the argument template is considered to be at least as specialized |
| 1689 | // as the type from the parameter template. |
| 1690 | return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info, |
| 1691 | Deduced, TDF_None); |
| 1692 | } |
| 1693 | |
| 1694 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1695 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 1696 | bool OnlyDeduced, |
| 1697 | llvm::SmallVectorImpl<bool> &Deduced); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1698 | |
| 1699 | /// \brief Determine whether the function template \p FT1 is at least as |
| 1700 | /// specialized as \p FT2. |
| 1701 | static bool isAtLeastAsSpecializedAs(Sema &S, |
| 1702 | FunctionTemplateDecl *FT1, |
| 1703 | FunctionTemplateDecl *FT2, |
| 1704 | TemplatePartialOrderingContext TPOC, |
| 1705 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 1706 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
| 1707 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
| 1708 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 1709 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
| 1710 | |
| 1711 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 1712 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
| 1713 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1714 | Deduced.resize(TemplateParams->size()); |
| 1715 | |
| 1716 | // C++0x [temp.deduct.partial]p3: |
| 1717 | // The types used to determine the ordering depend on the context in which |
| 1718 | // the partial ordering is done: |
| 1719 | Sema::TemplateDeductionInfo Info(S.Context); |
| 1720 | switch (TPOC) { |
| 1721 | case TPOC_Call: { |
| 1722 | // - In the context of a function call, the function parameter types are |
| 1723 | // used. |
| 1724 | unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); |
| 1725 | for (unsigned I = 0; I != NumParams; ++I) |
| 1726 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1727 | TemplateParams, |
| 1728 | Proto2->getArgType(I), |
| 1729 | Proto1->getArgType(I), |
| 1730 | Info, |
| 1731 | Deduced, |
| 1732 | QualifierComparisons)) |
| 1733 | return false; |
| 1734 | |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | case TPOC_Conversion: |
| 1739 | // - In the context of a call to a conversion operator, the return types |
| 1740 | // of the conversion function templates are used. |
| 1741 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1742 | TemplateParams, |
| 1743 | Proto2->getResultType(), |
| 1744 | Proto1->getResultType(), |
| 1745 | Info, |
| 1746 | Deduced, |
| 1747 | QualifierComparisons)) |
| 1748 | return false; |
| 1749 | break; |
| 1750 | |
| 1751 | case TPOC_Other: |
| 1752 | // - In other contexts (14.6.6.2) the function template’s function type |
| 1753 | // is used. |
| 1754 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1755 | TemplateParams, |
| 1756 | FD2->getType(), |
| 1757 | FD1->getType(), |
| 1758 | Info, |
| 1759 | Deduced, |
| 1760 | QualifierComparisons)) |
| 1761 | return false; |
| 1762 | break; |
| 1763 | } |
| 1764 | |
| 1765 | // C++0x [temp.deduct.partial]p11: |
| 1766 | // In most cases, all template parameters must have values in order for |
| 1767 | // deduction to succeed, but for partial ordering purposes a template |
| 1768 | // parameter may remain without a value provided it is not used in the |
| 1769 | // types being used for partial ordering. [ Note: a template parameter used |
| 1770 | // in a non-deduced context is considered used. -end note] |
| 1771 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 1772 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 1773 | if (Deduced[ArgIdx].isNull()) |
| 1774 | break; |
| 1775 | |
| 1776 | if (ArgIdx == NumArgs) { |
| 1777 | // All template arguments were deduced. FT1 is at least as specialized |
| 1778 | // as FT2. |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1782 | // Figure out which template parameters were used. |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1783 | llvm::SmallVector<bool, 4> UsedParameters; |
| 1784 | UsedParameters.resize(TemplateParams->size()); |
| 1785 | switch (TPOC) { |
| 1786 | case TPOC_Call: { |
| 1787 | unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); |
| 1788 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1789 | ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, |
| 1790 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | case TPOC_Conversion: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1795 | ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, |
| 1796 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1797 | break; |
| 1798 | |
| 1799 | case TPOC_Other: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1800 | ::MarkUsedTemplateParameters(S, FD2->getType(), false, UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | } |
| 1803 | |
| 1804 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 1805 | // If this argument had no value deduced but was used in one of the types |
| 1806 | // used for partial ordering, then deduction fails. |
| 1807 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 1808 | return false; |
| 1809 | |
| 1810 | return true; |
| 1811 | } |
| 1812 | |
| 1813 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1814 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1815 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 1816 | /// |
| 1817 | /// \param FT1 the first function template |
| 1818 | /// |
| 1819 | /// \param FT2 the second function template |
| 1820 | /// |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1821 | /// \param TPOC the context in which we are performing partial ordering of |
| 1822 | /// function templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | /// |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1824 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1825 | /// template is more specialized, returns NULL. |
| 1826 | FunctionTemplateDecl * |
| 1827 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 1828 | FunctionTemplateDecl *FT2, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1829 | TemplatePartialOrderingContext TPOC) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1830 | llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; |
| 1831 | bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0); |
| 1832 | bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC, |
| 1833 | &QualifierComparisons); |
| 1834 | |
| 1835 | if (Better1 != Better2) // We have a clear winner |
| 1836 | return Better1? FT1 : FT2; |
| 1837 | |
| 1838 | if (!Better1 && !Better2) // Neither is better than the other |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1839 | return 0; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1840 | |
| 1841 | |
| 1842 | // C++0x [temp.deduct.partial]p10: |
| 1843 | // If for each type being considered a given template is at least as |
| 1844 | // specialized for all types and more specialized for some set of types and |
| 1845 | // the other template is not more specialized for any types or is not at |
| 1846 | // least as specialized for any types, then the given template is more |
| 1847 | // specialized than the other template. Otherwise, neither template is more |
| 1848 | // specialized than the other. |
| 1849 | Better1 = false; |
| 1850 | Better2 = false; |
| 1851 | for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { |
| 1852 | // C++0x [temp.deduct.partial]p9: |
| 1853 | // If, for a given type, deduction succeeds in both directions (i.e., the |
| 1854 | // types are identical after the transformations above) and if the type |
| 1855 | // from the argument template is more cv-qualified than the type from the |
| 1856 | // parameter template (as described above) that type is considered to be |
| 1857 | // more specialized than the other. If neither type is more cv-qualified |
| 1858 | // than the other then neither type is more specialized than the other. |
| 1859 | switch (QualifierComparisons[I]) { |
| 1860 | case NeitherMoreQualified: |
| 1861 | break; |
| 1862 | |
| 1863 | case ParamMoreQualified: |
| 1864 | Better1 = true; |
| 1865 | if (Better2) |
| 1866 | return 0; |
| 1867 | break; |
| 1868 | |
| 1869 | case ArgMoreQualified: |
| 1870 | Better2 = true; |
| 1871 | if (Better1) |
| 1872 | return 0; |
| 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | assert(!(Better1 && Better2) && "Should have broken out in the loop above"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1878 | if (Better1) |
| 1879 | return FT1; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1880 | else if (Better2) |
| 1881 | return FT2; |
| 1882 | else |
| 1883 | return 0; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1884 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1885 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1886 | /// \brief Returns the more specialized class template partial specialization |
| 1887 | /// according to the rules of partial ordering of class template partial |
| 1888 | /// specializations (C++ [temp.class.order]). |
| 1889 | /// |
| 1890 | /// \param PS1 the first class template partial specialization |
| 1891 | /// |
| 1892 | /// \param PS2 the second class template partial specialization |
| 1893 | /// |
| 1894 | /// \returns the more specialized class template partial specialization. If |
| 1895 | /// neither partial specialization is more specialized, returns NULL. |
| 1896 | ClassTemplatePartialSpecializationDecl * |
| 1897 | Sema::getMoreSpecializedPartialSpecialization( |
| 1898 | ClassTemplatePartialSpecializationDecl *PS1, |
| 1899 | ClassTemplatePartialSpecializationDecl *PS2) { |
| 1900 | // C++ [temp.class.order]p1: |
| 1901 | // For two class template partial specializations, the first is at least as |
| 1902 | // specialized as the second if, given the following rewrite to two |
| 1903 | // function templates, the first function template is at least as |
| 1904 | // specialized as the second according to the ordering rules for function |
| 1905 | // templates (14.6.6.2): |
| 1906 | // - the first function template has the same template parameters as the |
| 1907 | // first partial specialization and has a single function parameter |
| 1908 | // whose type is a class template specialization with the template |
| 1909 | // arguments of the first partial specialization, and |
| 1910 | // - the second function template has the same template parameters as the |
| 1911 | // second partial specialization and has a single function parameter |
| 1912 | // whose type is a class template specialization with the template |
| 1913 | // arguments of the second partial specialization. |
| 1914 | // |
| 1915 | // Rather than synthesize function templates, we merely perform the |
| 1916 | // equivalent partial ordering by performing deduction directly on the |
| 1917 | // template arguments of the class template partial specializations. This |
| 1918 | // computation is slightly simpler than the general problem of function |
| 1919 | // template partial ordering, because class template partial specializations |
| 1920 | // are more constrained. We know that every template parameter is deduc |
| 1921 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1922 | Sema::TemplateDeductionInfo Info(Context); |
| 1923 | |
| 1924 | // Determine whether PS1 is at least as specialized as PS2 |
| 1925 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 1926 | bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, |
| 1927 | PS2->getTemplateParameters(), |
| 1928 | Context.getTypeDeclType(PS2), |
| 1929 | Context.getTypeDeclType(PS1), |
| 1930 | Info, |
| 1931 | Deduced, |
| 1932 | 0); |
| 1933 | |
| 1934 | // Determine whether PS2 is at least as specialized as PS1 |
| 1935 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 1936 | bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, |
| 1937 | PS1->getTemplateParameters(), |
| 1938 | Context.getTypeDeclType(PS1), |
| 1939 | Context.getTypeDeclType(PS2), |
| 1940 | Info, |
| 1941 | Deduced, |
| 1942 | 0); |
| 1943 | |
| 1944 | if (Better1 == Better2) |
| 1945 | return 0; |
| 1946 | |
| 1947 | return Better1? PS1 : PS2; |
| 1948 | } |
| 1949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1951 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1952 | const TemplateArgument &TemplateArg, |
| 1953 | bool OnlyDeduced, |
| 1954 | llvm::SmallVectorImpl<bool> &Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1956 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1957 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1959 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1960 | const Expr *E, |
| 1961 | bool OnlyDeduced, |
| 1962 | llvm::SmallVectorImpl<bool> &Used) { |
| 1963 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
| 1964 | // find other occurrences of template parameters. |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1965 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1966 | if (!E) |
| 1967 | return; |
| 1968 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1970 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 1971 | if (!NTTP) |
| 1972 | return; |
| 1973 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1974 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1977 | /// \brief Mark the template parameters that are used by the given |
| 1978 | /// nested name specifier. |
| 1979 | static void |
| 1980 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1981 | NestedNameSpecifier *NNS, |
| 1982 | bool OnlyDeduced, |
| 1983 | llvm::SmallVectorImpl<bool> &Used) { |
| 1984 | if (!NNS) |
| 1985 | return; |
| 1986 | |
| 1987 | MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Used); |
| 1988 | MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), |
| 1989 | OnlyDeduced, Used); |
| 1990 | } |
| 1991 | |
| 1992 | /// \brief Mark the template parameters that are used by the given |
| 1993 | /// template name. |
| 1994 | static void |
| 1995 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1996 | TemplateName Name, |
| 1997 | bool OnlyDeduced, |
| 1998 | llvm::SmallVectorImpl<bool> &Used) { |
| 1999 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2000 | if (TemplateTemplateParmDecl *TTP |
| 2001 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 2002 | Used[TTP->getIndex()] = true; |
| 2003 | return; |
| 2004 | } |
| 2005 | |
| 2006 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
| 2007 | MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, Used); |
| 2008 | } |
| 2009 | |
| 2010 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2011 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2013 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 2014 | bool OnlyDeduced, |
| 2015 | llvm::SmallVectorImpl<bool> &Used) { |
| 2016 | if (T.isNull()) |
| 2017 | return; |
| 2018 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2019 | // Non-dependent types have nothing deducible |
| 2020 | if (!T->isDependentType()) |
| 2021 | return; |
| 2022 | |
| 2023 | T = SemaRef.Context.getCanonicalType(T); |
| 2024 | switch (T->getTypeClass()) { |
| 2025 | case Type::ExtQual: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2026 | MarkUsedTemplateParameters(SemaRef, |
| 2027 | QualType(cast<ExtQualType>(T)->getBaseType(), 0), |
| 2028 | OnlyDeduced, |
| 2029 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | |
| 2032 | case Type::Pointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2033 | MarkUsedTemplateParameters(SemaRef, |
| 2034 | cast<PointerType>(T)->getPointeeType(), |
| 2035 | OnlyDeduced, |
| 2036 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | |
| 2039 | case Type::BlockPointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2040 | MarkUsedTemplateParameters(SemaRef, |
| 2041 | cast<BlockPointerType>(T)->getPointeeType(), |
| 2042 | OnlyDeduced, |
| 2043 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2044 | break; |
| 2045 | |
| 2046 | case Type::LValueReference: |
| 2047 | case Type::RValueReference: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2048 | MarkUsedTemplateParameters(SemaRef, |
| 2049 | cast<ReferenceType>(T)->getPointeeType(), |
| 2050 | OnlyDeduced, |
| 2051 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2052 | break; |
| 2053 | |
| 2054 | case Type::MemberPointer: { |
| 2055 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2056 | MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, |
| 2057 | Used); |
| 2058 | MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
| 2059 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2060 | break; |
| 2061 | } |
| 2062 | |
| 2063 | case Type::DependentSizedArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2064 | MarkUsedTemplateParameters(SemaRef, |
| 2065 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
| 2066 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2067 | // Fall through to check the element type |
| 2068 | |
| 2069 | case Type::ConstantArray: |
| 2070 | case Type::IncompleteArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2071 | MarkUsedTemplateParameters(SemaRef, |
| 2072 | cast<ArrayType>(T)->getElementType(), |
| 2073 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | |
| 2076 | case Type::Vector: |
| 2077 | case Type::ExtVector: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2078 | MarkUsedTemplateParameters(SemaRef, |
| 2079 | cast<VectorType>(T)->getElementType(), |
| 2080 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2081 | break; |
| 2082 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2083 | case Type::DependentSizedExtVector: { |
| 2084 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2085 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2086 | MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, |
| 2087 | Used); |
| 2088 | MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, |
| 2089 | Used); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2090 | break; |
| 2091 | } |
| 2092 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2093 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2094 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2095 | MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, |
| 2096 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2097 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2098 | MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, |
| 2099 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | } |
| 2102 | |
| 2103 | case Type::TemplateTypeParm: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2104 | Used[cast<TemplateTypeParmType>(T)->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2105 | break; |
| 2106 | |
| 2107 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2109 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2110 | MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, |
| 2111 | Used); |
| 2112 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 2113 | MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2114 | break; |
| 2115 | } |
| 2116 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2117 | case Type::Complex: |
| 2118 | if (!OnlyDeduced) |
| 2119 | MarkUsedTemplateParameters(SemaRef, |
| 2120 | cast<ComplexType>(T)->getElementType(), |
| 2121 | OnlyDeduced, Used); |
| 2122 | break; |
| 2123 | |
| 2124 | case Type::Typename: |
| 2125 | if (!OnlyDeduced) |
| 2126 | MarkUsedTemplateParameters(SemaRef, |
| 2127 | cast<TypenameType>(T)->getQualifier(), |
| 2128 | OnlyDeduced, Used); |
| 2129 | break; |
| 2130 | |
| 2131 | // None of these types have any template parameters in them. |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2132 | case Type::Builtin: |
| 2133 | case Type::FixedWidthInt: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2134 | case Type::VariableArray: |
| 2135 | case Type::FunctionNoProto: |
| 2136 | case Type::Record: |
| 2137 | case Type::Enum: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2138 | case Type::ObjCInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2139 | case Type::ObjCObjectPointer: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2140 | #define TYPE(Class, Base) |
| 2141 | #define ABSTRACT_TYPE(Class, Base) |
| 2142 | #define DEPENDENT_TYPE(Class, Base) |
| 2143 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2144 | #include "clang/AST/TypeNodes.def" |
| 2145 | break; |
| 2146 | } |
| 2147 | } |
| 2148 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2149 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2150 | /// template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2152 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2153 | const TemplateArgument &TemplateArg, |
| 2154 | bool OnlyDeduced, |
| 2155 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2156 | switch (TemplateArg.getKind()) { |
| 2157 | case TemplateArgument::Null: |
| 2158 | case TemplateArgument::Integral: |
| 2159 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2161 | case TemplateArgument::Type: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2162 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, |
| 2163 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2164 | break; |
| 2165 | |
| 2166 | case TemplateArgument::Declaration: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2168 | = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2169 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | |
| 2172 | case TemplateArgument::Expression: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2173 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, |
| 2174 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2175 | break; |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2176 | |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2177 | case TemplateArgument::Pack: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2178 | for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), |
| 2179 | PEnd = TemplateArg.pack_end(); |
| 2180 | P != PEnd; ++P) |
| 2181 | MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Used); |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2182 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | /// \brief Mark the template parameters can be deduced by the given |
| 2187 | /// template argument list. |
| 2188 | /// |
| 2189 | /// \param TemplateArgs the template argument list from which template |
| 2190 | /// parameters will be deduced. |
| 2191 | /// |
| 2192 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 2193 | /// to indicate when the corresponding template parameter will be |
| 2194 | /// deduced. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2196 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 2197 | bool OnlyDeduced, |
| 2198 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2199 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2200 | ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2201 | } |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame^] | 2202 | |
| 2203 | /// \brief Marks all of the template parameters that will be deduced by a |
| 2204 | /// call to the given function template. |
| 2205 | void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate, |
| 2206 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 2207 | TemplateParameterList *TemplateParams |
| 2208 | = FunctionTemplate->getTemplateParameters(); |
| 2209 | Deduced.clear(); |
| 2210 | Deduced.resize(TemplateParams->size()); |
| 2211 | |
| 2212 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2213 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
| 2214 | ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(), |
| 2215 | true, Deduced); |
| 2216 | } |