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