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