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