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 | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame^] | 952 | = const_cast<NamedDecl *>( |
| 953 | Partial->getTemplateParameters()->getParam(I)); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 954 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 955 | Info.Param = TTP; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 957 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 958 | Info.Param = NTTP; |
| 959 | else |
| 960 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 961 | return TDK_Incomplete; |
| 962 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 963 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 964 | Builder.Append(Deduced[I]); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | // Form the template argument list from the deduced template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | TemplateArgumentList *DeducedArgumentList |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 969 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 970 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 972 | // Substitute the deduced template arguments into the template |
| 973 | // arguments of the class template partial specialization, and |
| 974 | // verify that the instantiated template arguments are both valid |
| 975 | // and are equivalent to the template arguments originally provided |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | // to the class template. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 977 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 978 | const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); |
| 979 | for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame^] | 980 | Decl *Param = const_cast<NamedDecl *>( |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 981 | ClassTemplate->getTemplateParameters()->getParam(I)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | TemplateArgument InstArg |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 983 | = Subst(PartialTemplateArgs[I], |
| 984 | MultiLevelTemplateArgumentList(*DeducedArgumentList)); |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 985 | if (InstArg.isNull()) { |
| 986 | Info.Param = makeTemplateParameter(Param); |
| 987 | Info.FirstArg = PartialTemplateArgs[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 989 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 991 | if (InstArg.getKind() == TemplateArgument::Expression) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | // When the argument is an expression, check the expression result |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 993 | // against the actual template parameter to get down to the canonical |
| 994 | // template argument. |
| 995 | Expr *InstExpr = InstArg.getAsExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 997 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 998 | if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { |
| 999 | Info.Param = makeTemplateParameter(Param); |
| 1000 | Info.FirstArg = PartialTemplateArgs[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | return TDK_SubstitutionFailure; |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | } else if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1004 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 1005 | // FIXME: template template arguments should really resolve to decls |
| 1006 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); |
| 1007 | if (!DRE || CheckTemplateArgument(TTP, DRE)) { |
| 1008 | Info.Param = makeTemplateParameter(Param); |
| 1009 | Info.FirstArg = PartialTemplateArgs[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | return TDK_SubstitutionFailure; |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1015 | if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { |
| 1016 | Info.Param = makeTemplateParameter(Param); |
| 1017 | Info.FirstArg = TemplateArgs[I]; |
| 1018 | Info.SecondArg = InstArg; |
| 1019 | return TDK_NonDeducedMismatch; |
| 1020 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 1023 | if (Trap.hasErrorOccurred()) |
| 1024 | return TDK_SubstitutionFailure; |
| 1025 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1026 | return TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1027 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1028 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1029 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 1030 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | if (const TemplateSpecializationType *Spec |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1032 | = T->getAsTemplateSpecializationType()) |
| 1033 | return Spec->getTemplateName().getAsTemplateDecl() != 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1035 | return false; |
| 1036 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1037 | |
| 1038 | /// \brief Substitute the explicitly-provided template arguments into the |
| 1039 | /// given function template according to C++ [temp.arg.explicit]. |
| 1040 | /// |
| 1041 | /// \param FunctionTemplate the function template into which the explicit |
| 1042 | /// template arguments will be substituted. |
| 1043 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | /// \param ExplicitTemplateArguments the explicitly-specified template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1045 | /// arguments. |
| 1046 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | /// \param NumExplicitTemplateArguments the number of explicitly-specified |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1048 | /// template arguments in @p ExplicitTemplateArguments. This value may be zero. |
| 1049 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1051 | /// with the converted and checked explicit template arguments. |
| 1052 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1054 | /// parameters. |
| 1055 | /// |
| 1056 | /// \param FunctionType if non-NULL, the result type of the function template |
| 1057 | /// will also be instantiated and the pointed-to value will be updated with |
| 1058 | /// the instantiated function type. |
| 1059 | /// |
| 1060 | /// \param Info if substitution fails for any reason, this object will be |
| 1061 | /// populated with more information about the failure. |
| 1062 | /// |
| 1063 | /// \returns TDK_Success if substitution was successful, or some failure |
| 1064 | /// condition. |
| 1065 | Sema::TemplateDeductionResult |
| 1066 | Sema::SubstituteExplicitTemplateArguments( |
| 1067 | FunctionTemplateDecl *FunctionTemplate, |
| 1068 | const TemplateArgument *ExplicitTemplateArgs, |
| 1069 | unsigned NumExplicitTemplateArgs, |
| 1070 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1071 | llvm::SmallVectorImpl<QualType> &ParamTypes, |
| 1072 | QualType *FunctionType, |
| 1073 | TemplateDeductionInfo &Info) { |
| 1074 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1075 | TemplateParameterList *TemplateParams |
| 1076 | = FunctionTemplate->getTemplateParameters(); |
| 1077 | |
| 1078 | if (NumExplicitTemplateArgs == 0) { |
| 1079 | // No arguments to substitute; just copy over the parameter types and |
| 1080 | // fill in the function type. |
| 1081 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1082 | PEnd = Function->param_end(); |
| 1083 | P != PEnd; |
| 1084 | ++P) |
| 1085 | ParamTypes.push_back((*P)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1087 | if (FunctionType) |
| 1088 | *FunctionType = Function->getType(); |
| 1089 | return TDK_Success; |
| 1090 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1092 | // Substitution of the explicit template arguments into a function template |
| 1093 | /// is a SFINAE context. Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | SFINAETrap Trap(*this); |
| 1095 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1096 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | // Template arguments that are present shall be specified in the |
| 1098 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1099 | // template argument list shall not specify more template-arguments than |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | // there are corresponding template-parameters. |
| 1101 | TemplateArgumentListBuilder Builder(TemplateParams, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1102 | NumExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1105 | // explicitly-specified template arguments against this function template, |
| 1106 | // and then substitute them into the function parameter types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1108 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1109 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); |
| 1110 | if (Inst) |
| 1111 | return TDK_InstantiationDepth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1113 | if (CheckTemplateArgumentList(FunctionTemplate, |
| 1114 | SourceLocation(), SourceLocation(), |
| 1115 | ExplicitTemplateArgs, |
| 1116 | NumExplicitTemplateArgs, |
| 1117 | SourceLocation(), |
| 1118 | true, |
| 1119 | Builder) || Trap.hasErrorOccurred()) |
| 1120 | return TDK_InvalidExplicitArguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1122 | // Form the template argument list from the explicitly-specified |
| 1123 | // template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1125 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1126 | Info.reset(ExplicitArgumentList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1128 | // Instantiate the types of each of the function parameters given the |
| 1129 | // explicitly-specified template arguments. |
| 1130 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1131 | PEnd = Function->param_end(); |
| 1132 | P != PEnd; |
| 1133 | ++P) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | QualType ParamType |
| 1135 | = SubstType((*P)->getType(), |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1136 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1137 | (*P)->getLocation(), (*P)->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1138 | if (ParamType.isNull() || Trap.hasErrorOccurred()) |
| 1139 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1141 | ParamTypes.push_back(ParamType); |
| 1142 | } |
| 1143 | |
| 1144 | // If the caller wants a full function type back, instantiate the return |
| 1145 | // type and form that function type. |
| 1146 | if (FunctionType) { |
| 1147 | // FIXME: exception-specifications? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | const FunctionProtoType *Proto |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1149 | = Function->getType()->getAsFunctionProtoType(); |
| 1150 | assert(Proto && "Function template does not have a prototype?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | |
| 1152 | QualType ResultType |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1153 | = SubstType(Proto->getResultType(), |
| 1154 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1155 | Function->getTypeSpecStartLoc(), |
| 1156 | Function->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1157 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 1158 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
| 1160 | *FunctionType = BuildFunctionType(ResultType, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1161 | ParamTypes.data(), ParamTypes.size(), |
| 1162 | Proto->isVariadic(), |
| 1163 | Proto->getTypeQuals(), |
| 1164 | Function->getLocation(), |
| 1165 | Function->getDeclName()); |
| 1166 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 1167 | return TDK_SubstitutionFailure; |
| 1168 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1170 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 1172 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1173 | // template arguments can be deduced, they may all be omitted; in this |
| 1174 | // case, the empty template argument list <> itself may also be omitted. |
| 1175 | // |
| 1176 | // Take all of the explicitly-specified arguments and put them into the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | // set of deduced template arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1178 | Deduced.reserve(TemplateParams->size()); |
| 1179 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | Deduced.push_back(ExplicitArgumentList->get(I)); |
| 1181 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1182 | return TDK_Success; |
| 1183 | } |
| 1184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1186 | /// checking the deduced template arguments for completeness and forming |
| 1187 | /// the function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | Sema::TemplateDeductionResult |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1189 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
| 1190 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1191 | FunctionDecl *&Specialization, |
| 1192 | TemplateDeductionInfo &Info) { |
| 1193 | TemplateParameterList *TemplateParams |
| 1194 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1196 | // C++ [temp.deduct.type]p2: |
| 1197 | // [...] or if any template argument remains neither deduced nor |
| 1198 | // explicitly specified, template argument deduction fails. |
| 1199 | TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); |
| 1200 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 1201 | if (Deduced[I].isNull()) { |
| 1202 | Info.Param = makeTemplateParameter( |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame^] | 1203 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1204 | return TDK_Incomplete; |
| 1205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1207 | Builder.Append(Deduced[I]); |
| 1208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1210 | // Form the template argument list from the deduced template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1212 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1213 | Info.reset(DeducedArgumentList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1215 | // Template argument deduction for function templates in a SFINAE context. |
| 1216 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | SFINAETrap Trap(*this); |
| 1218 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1219 | // Enter a new template instantiation context while we instantiate the |
| 1220 | // actual function declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1222 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1223 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); |
| 1224 | if (Inst) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | return TDK_InstantiationDepth; |
| 1226 | |
| 1227 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1228 | // declaration to produce the function template specialization. |
| 1229 | Specialization = cast_or_null<FunctionDecl>( |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1230 | SubstDecl(FunctionTemplate->getTemplatedDecl(), |
| 1231 | FunctionTemplate->getDeclContext(), |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1232 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1233 | if (!Specialization) |
| 1234 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | |
| 1236 | // If the template argument list is owned by the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1237 | // specialization, release it. |
| 1238 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) |
| 1239 | Info.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1241 | // There may have been an error that did not prevent us from constructing a |
| 1242 | // declaration. Mark the declaration invalid and return with a substitution |
| 1243 | // failure. |
| 1244 | if (Trap.hasErrorOccurred()) { |
| 1245 | Specialization->setInvalidDecl(true); |
| 1246 | return TDK_SubstitutionFailure; |
| 1247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | |
| 1249 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1252 | /// \brief Perform template argument deduction from a function call |
| 1253 | /// (C++ [temp.deduct.call]). |
| 1254 | /// |
| 1255 | /// \param FunctionTemplate the function template for which we are performing |
| 1256 | /// template argument deduction. |
| 1257 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | /// \param HasExplicitTemplateArgs whether any template arguments were |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1259 | /// explicitly specified. |
| 1260 | /// |
| 1261 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1262 | /// the explicitly-specified template arguments. |
| 1263 | /// |
| 1264 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | /// the number of explicitly-specified template arguments in |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1266 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 1267 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1268 | /// \param Args the function call arguments |
| 1269 | /// |
| 1270 | /// \param NumArgs the number of arguments in Args |
| 1271 | /// |
| 1272 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | /// this will be set to the function template specialization produced by |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1274 | /// template argument deduction. |
| 1275 | /// |
| 1276 | /// \param Info the argument will be updated to provide additional information |
| 1277 | /// about template argument deduction. |
| 1278 | /// |
| 1279 | /// \returns the result of template argument deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1280 | Sema::TemplateDeductionResult |
| 1281 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1282 | bool HasExplicitTemplateArgs, |
| 1283 | const TemplateArgument *ExplicitTemplateArgs, |
| 1284 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1285 | Expr **Args, unsigned NumArgs, |
| 1286 | FunctionDecl *&Specialization, |
| 1287 | TemplateDeductionInfo &Info) { |
| 1288 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1290 | // C++ [temp.deduct.call]p1: |
| 1291 | // Template argument deduction is done by comparing each function template |
| 1292 | // parameter type (call it P) with the type of the corresponding argument |
| 1293 | // of the call (call it A) as described below. |
| 1294 | unsigned CheckArgs = NumArgs; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1295 | if (NumArgs < Function->getMinRequiredArguments()) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1296 | return TDK_TooFewArguments; |
| 1297 | else if (NumArgs > Function->getNumParams()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | const FunctionProtoType *Proto |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1299 | = Function->getType()->getAsFunctionProtoType(); |
| 1300 | if (!Proto->isVariadic()) |
| 1301 | return TDK_TooManyArguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1303 | CheckArgs = Function->getNumParams(); |
| 1304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1306 | // The types of the parameters from which we will perform template argument |
| 1307 | // deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1308 | TemplateParameterList *TemplateParams |
| 1309 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1310 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1311 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1312 | if (NumExplicitTemplateArgs) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1313 | TemplateDeductionResult Result = |
| 1314 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
| 1315 | ExplicitTemplateArgs, |
| 1316 | NumExplicitTemplateArgs, |
| 1317 | Deduced, |
| 1318 | ParamTypes, |
| 1319 | 0, |
| 1320 | Info); |
| 1321 | if (Result) |
| 1322 | return Result; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1323 | } else { |
| 1324 | // Just fill in the parameter types from the function declaration. |
| 1325 | for (unsigned I = 0; I != CheckArgs; ++I) |
| 1326 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 1327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1329 | // Deduce template arguments from the function parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1331 | for (unsigned I = 0; I != CheckArgs; ++I) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1332 | QualType ParamType = ParamTypes[I]; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1333 | QualType ArgType = Args[I]->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1335 | // C++ [temp.deduct.call]p2: |
| 1336 | // If P is not a reference type: |
| 1337 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1338 | bool ParamWasReference = isa<ReferenceType>(CanonParamType); |
| 1339 | if (!ParamWasReference) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | // - If A is an array type, the pointer type produced by the |
| 1341 | // array-to-pointer standard conversion (4.2) is used in place of |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1342 | // A for type deduction; otherwise, |
| 1343 | if (ArgType->isArrayType()) |
| 1344 | ArgType = Context.getArrayDecayedType(ArgType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | // - If A is a function type, the pointer type produced by the |
| 1346 | // function-to-pointer standard conversion (4.3) is used in place |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1347 | // of A for type deduction; otherwise, |
| 1348 | else if (ArgType->isFunctionType()) |
| 1349 | ArgType = Context.getPointerType(ArgType); |
| 1350 | else { |
| 1351 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1352 | // type are ignored for type deduction. |
| 1353 | QualType CanonArgType = Context.getCanonicalType(ArgType); |
| 1354 | if (CanonArgType.getCVRQualifiers()) |
| 1355 | ArgType = CanonArgType.getUnqualifiedType(); |
| 1356 | } |
| 1357 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1359 | // C++0x [temp.deduct.call]p3: |
| 1360 | // 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] | 1361 | // are ignored for type deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1362 | if (CanonParamType.getCVRQualifiers()) |
| 1363 | ParamType = CanonParamType.getUnqualifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1364 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | // [...] If P is a reference type, the type referred to by P is used |
| 1366 | // for type deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1367 | ParamType = ParamRefType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
| 1369 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 1370 | // 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] | 1371 | // type deduction. |
| 1372 | if (isa<RValueReferenceType>(ParamRefType) && |
| 1373 | ParamRefType->getAsTemplateTypeParmType() && |
| 1374 | Args[I]->isLvalue(Context) == Expr::LV_Valid) |
| 1375 | ArgType = Context.getLValueReferenceType(ArgType); |
| 1376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1378 | // C++0x [temp.deduct.call]p4: |
| 1379 | // In general, the deduction process attempts to find template argument |
| 1380 | // values that will make the deduced A identical to A (after the type A |
| 1381 | // is transformed as described above). [...] |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 1382 | unsigned TDF = TDF_SkipNonDependent; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1384 | // - If the original P is a reference type, the deduced A (i.e., the |
| 1385 | // type referred to by the reference) can be more cv-qualified than |
| 1386 | // the transformed A. |
| 1387 | if (ParamWasReference) |
| 1388 | TDF |= TDF_ParamWithReferenceType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | // - The transformed A can be another pointer or pointer to member |
| 1390 | // type that can be converted to the deduced A via a qualification |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1391 | // conversion (4.4). |
| 1392 | if (ArgType->isPointerType() || ArgType->isMemberPointerType()) |
| 1393 | TDF |= TDF_IgnoreQualifiers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | // - 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] | 1395 | // transformed A can be a derived class of the deduced A. Likewise, |
| 1396 | // if P is a pointer to a class of the form simple-template-id, the |
| 1397 | // transformed A can be a pointer to a derived class pointed to by |
| 1398 | // the deduced A. |
| 1399 | if (isSimpleTemplateIdType(ParamType) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | (isa<PointerType>(ParamType) && |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1401 | isSimpleTemplateIdType( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1402 | ParamType->getAs<PointerType>()->getPointeeType()))) |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1403 | TDF |= TDF_DerivedClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1405 | if (TemplateDeductionResult Result |
| 1406 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1407 | ParamType, ArgType, Info, Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1408 | TDF)) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1409 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 8fdc3c4 | 2009-07-07 23:12:18 +0000 | [diff] [blame] | 1411 | // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | // pointer parameters. |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1413 | |
| 1414 | // FIXME: we need to check that the deduced A is the same as A, |
| 1415 | // modulo the various allowed differences. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1416 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1419 | Specialization, Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1422 | /// \brief Deduce template arguments when taking the address of a function |
| 1423 | /// template (C++ [temp.deduct.funcaddr]). |
| 1424 | /// |
| 1425 | /// \param FunctionTemplate the function template for which we are performing |
| 1426 | /// template argument deduction. |
| 1427 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | /// \param HasExplicitTemplateArgs whether any template arguments were |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1429 | /// explicitly specified. |
| 1430 | /// |
| 1431 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1432 | /// the explicitly-specified template arguments. |
| 1433 | /// |
| 1434 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | /// the number of explicitly-specified template arguments in |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1436 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 1437 | /// |
| 1438 | /// \param ArgFunctionType the function type that will be used as the |
| 1439 | /// "argument" type (A) when performing template argument deduction from the |
| 1440 | /// function template's function type. |
| 1441 | /// |
| 1442 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1444 | /// template argument deduction. |
| 1445 | /// |
| 1446 | /// \param Info the argument will be updated to provide additional information |
| 1447 | /// about template argument deduction. |
| 1448 | /// |
| 1449 | /// \returns the result of template argument deduction. |
| 1450 | Sema::TemplateDeductionResult |
| 1451 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1452 | bool HasExplicitTemplateArgs, |
| 1453 | const TemplateArgument *ExplicitTemplateArgs, |
| 1454 | unsigned NumExplicitTemplateArgs, |
| 1455 | QualType ArgFunctionType, |
| 1456 | FunctionDecl *&Specialization, |
| 1457 | TemplateDeductionInfo &Info) { |
| 1458 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1459 | TemplateParameterList *TemplateParams |
| 1460 | = FunctionTemplate->getTemplateParameters(); |
| 1461 | QualType FunctionType = Function->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1463 | // Substitute any explicit template arguments. |
| 1464 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1465 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1466 | if (HasExplicitTemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | if (TemplateDeductionResult Result |
| 1468 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
| 1469 | ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1470 | NumExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | Deduced, ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1472 | &FunctionType, Info)) |
| 1473 | return Result; |
| 1474 | } |
| 1475 | |
| 1476 | // Template argument deduction for function templates in a SFINAE context. |
| 1477 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | SFINAETrap Trap(*this); |
| 1479 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1480 | // Deduce template arguments from the function type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1482 | if (TemplateDeductionResult Result |
| 1483 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | FunctionType, ArgFunctionType, Info, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1485 | Deduced, 0)) |
| 1486 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
| 1488 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1489 | Specialization, Info); |
| 1490 | } |
| 1491 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1492 | /// \brief Deduce template arguments for a templated conversion |
| 1493 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 1494 | /// conversion function template specialization. |
| 1495 | Sema::TemplateDeductionResult |
| 1496 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1497 | QualType ToType, |
| 1498 | CXXConversionDecl *&Specialization, |
| 1499 | TemplateDeductionInfo &Info) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | CXXConversionDecl *Conv |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1501 | = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); |
| 1502 | QualType FromType = Conv->getConversionType(); |
| 1503 | |
| 1504 | // Canonicalize the types for deduction. |
| 1505 | QualType P = Context.getCanonicalType(FromType); |
| 1506 | QualType A = Context.getCanonicalType(ToType); |
| 1507 | |
| 1508 | // C++0x [temp.deduct.conv]p3: |
| 1509 | // If P is a reference type, the type referred to by P is used for |
| 1510 | // type deduction. |
| 1511 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 1512 | P = PRef->getPointeeType(); |
| 1513 | |
| 1514 | // C++0x [temp.deduct.conv]p3: |
| 1515 | // If A is a reference type, the type referred to by A is used |
| 1516 | // for type deduction. |
| 1517 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 1518 | A = ARef->getPointeeType(); |
| 1519 | // C++ [temp.deduct.conv]p2: |
| 1520 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | // If A is not a reference type: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1522 | else { |
| 1523 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 1524 | |
| 1525 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1527 | // of P for type deduction; otherwise, |
| 1528 | if (P->isArrayType()) |
| 1529 | P = Context.getArrayDecayedType(P); |
| 1530 | // - If P is a function type, the pointer type produced by the |
| 1531 | // function-to-pointer standard conversion (4.3) is used in |
| 1532 | // place of P for type deduction; otherwise, |
| 1533 | else if (P->isFunctionType()) |
| 1534 | P = Context.getPointerType(P); |
| 1535 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
| 1536 | // P’s type are ignored for type deduction. |
| 1537 | else |
| 1538 | P = P.getUnqualifiedType(); |
| 1539 | |
| 1540 | // C++0x [temp.deduct.conv]p3: |
| 1541 | // If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1542 | // type are ignored for type deduction. |
| 1543 | A = A.getUnqualifiedType(); |
| 1544 | } |
| 1545 | |
| 1546 | // Template argument deduction for function templates in a SFINAE context. |
| 1547 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | SFINAETrap Trap(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1549 | |
| 1550 | // C++ [temp.deduct.conv]p1: |
| 1551 | // Template argument deduction is done by comparing the return |
| 1552 | // type of the template conversion function (call it P) with the |
| 1553 | // type that is required as the result of the conversion (call it |
| 1554 | // A) as described in 14.8.2.4. |
| 1555 | TemplateParameterList *TemplateParams |
| 1556 | = FunctionTemplate->getTemplateParameters(); |
| 1557 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1559 | |
| 1560 | // C++0x [temp.deduct.conv]p4: |
| 1561 | // In general, the deduction process attempts to find template |
| 1562 | // argument values that will make the deduced A identical to |
| 1563 | // A. However, there are two cases that allow a difference: |
| 1564 | unsigned TDF = 0; |
| 1565 | // - If the original A is a reference type, A can be more |
| 1566 | // cv-qualified than the deduced A (i.e., the type referred to |
| 1567 | // by the reference) |
| 1568 | if (ToType->isReferenceType()) |
| 1569 | TDF |= TDF_ParamWithReferenceType; |
| 1570 | // - The deduced A can be another pointer or pointer to member |
| 1571 | // type that can be converted to A via a qualification |
| 1572 | // conversion. |
| 1573 | // |
| 1574 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 1575 | // both P and A are pointers or member pointers. In this case, we |
| 1576 | // just ignore cv-qualifiers completely). |
| 1577 | if ((P->isPointerType() && A->isPointerType()) || |
| 1578 | (P->isMemberPointerType() && P->isMemberPointerType())) |
| 1579 | TDF |= TDF_IgnoreQualifiers; |
| 1580 | if (TemplateDeductionResult Result |
| 1581 | = ::DeduceTemplateArguments(Context, TemplateParams, |
| 1582 | P, A, Info, Deduced, TDF)) |
| 1583 | return Result; |
| 1584 | |
| 1585 | // FIXME: we need to check that the deduced A is the same as A, |
| 1586 | // modulo the various allowed differences. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1588 | // Finish template argument deduction. |
| 1589 | FunctionDecl *Spec = 0; |
| 1590 | TemplateDeductionResult Result |
| 1591 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info); |
| 1592 | Specialization = cast_or_null<CXXConversionDecl>(Spec); |
| 1593 | return Result; |
| 1594 | } |
| 1595 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1596 | /// \brief Stores the result of comparing the qualifiers of two types. |
| 1597 | enum DeductionQualifierComparison { |
| 1598 | NeitherMoreQualified = 0, |
| 1599 | ParamMoreQualified, |
| 1600 | ArgMoreQualified |
| 1601 | }; |
| 1602 | |
| 1603 | /// \brief Deduce the template arguments during partial ordering by comparing |
| 1604 | /// the parameter type and the argument type (C++0x [temp.deduct.partial]). |
| 1605 | /// |
| 1606 | /// \param Context the AST context in which this deduction occurs. |
| 1607 | /// |
| 1608 | /// \param TemplateParams the template parameters that we are deducing |
| 1609 | /// |
| 1610 | /// \param ParamIn the parameter type |
| 1611 | /// |
| 1612 | /// \param ArgIn the argument type |
| 1613 | /// |
| 1614 | /// \param Info information about the template argument deduction itself |
| 1615 | /// |
| 1616 | /// \param Deduced the deduced template arguments |
| 1617 | /// |
| 1618 | /// \returns the result of template argument deduction so far. Note that a |
| 1619 | /// "success" result means that template argument deduction has not yet failed, |
| 1620 | /// but it may still fail, later, for other reasons. |
| 1621 | static Sema::TemplateDeductionResult |
| 1622 | DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context, |
| 1623 | TemplateParameterList *TemplateParams, |
| 1624 | QualType ParamIn, QualType ArgIn, |
| 1625 | Sema::TemplateDeductionInfo &Info, |
| 1626 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1627 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 1628 | CanQualType Param = Context.getCanonicalType(ParamIn); |
| 1629 | CanQualType Arg = Context.getCanonicalType(ArgIn); |
| 1630 | |
| 1631 | // C++0x [temp.deduct.partial]p5: |
| 1632 | // Before the partial ordering is done, certain transformations are |
| 1633 | // performed on the types used for partial ordering: |
| 1634 | // - If P is a reference type, P is replaced by the type referred to. |
| 1635 | CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); |
| 1636 | if (ParamRef) |
| 1637 | Param = ParamRef->getPointeeType(); |
| 1638 | |
| 1639 | // - If A is a reference type, A is replaced by the type referred to. |
| 1640 | CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); |
| 1641 | if (ArgRef) |
| 1642 | Arg = ArgRef->getPointeeType(); |
| 1643 | |
| 1644 | if (QualifierComparisons && ParamRef && ArgRef) { |
| 1645 | // C++0x [temp.deduct.partial]p6: |
| 1646 | // If both P and A were reference types (before being replaced with the |
| 1647 | // type referred to above), determine which of the two types (if any) is |
| 1648 | // more cv-qualified than the other; otherwise the types are considered to |
| 1649 | // be equally cv-qualified for partial ordering purposes. The result of this |
| 1650 | // determination will be used below. |
| 1651 | // |
| 1652 | // We save this information for later, using it only when deduction |
| 1653 | // succeeds in both directions. |
| 1654 | DeductionQualifierComparison QualifierResult = NeitherMoreQualified; |
| 1655 | if (Param.isMoreQualifiedThan(Arg)) |
| 1656 | QualifierResult = ParamMoreQualified; |
| 1657 | else if (Arg.isMoreQualifiedThan(Param)) |
| 1658 | QualifierResult = ArgMoreQualified; |
| 1659 | QualifierComparisons->push_back(QualifierResult); |
| 1660 | } |
| 1661 | |
| 1662 | // C++0x [temp.deduct.partial]p7: |
| 1663 | // Remove any top-level cv-qualifiers: |
| 1664 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
| 1665 | // version of P. |
| 1666 | Param = Param.getUnqualifiedType(); |
| 1667 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
| 1668 | // version of A. |
| 1669 | Arg = Arg.getUnqualifiedType(); |
| 1670 | |
| 1671 | // C++0x [temp.deduct.partial]p8: |
| 1672 | // Using the resulting types P and A the deduction is then done as |
| 1673 | // described in 14.9.2.5. If deduction succeeds for a given type, the type |
| 1674 | // from the argument template is considered to be at least as specialized |
| 1675 | // as the type from the parameter template. |
| 1676 | return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info, |
| 1677 | Deduced, TDF_None); |
| 1678 | } |
| 1679 | |
| 1680 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1681 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 1682 | bool OnlyDeduced, |
| 1683 | llvm::SmallVectorImpl<bool> &Deduced); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1684 | |
| 1685 | /// \brief Determine whether the function template \p FT1 is at least as |
| 1686 | /// specialized as \p FT2. |
| 1687 | static bool isAtLeastAsSpecializedAs(Sema &S, |
| 1688 | FunctionTemplateDecl *FT1, |
| 1689 | FunctionTemplateDecl *FT2, |
| 1690 | TemplatePartialOrderingContext TPOC, |
| 1691 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 1692 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
| 1693 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
| 1694 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 1695 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
| 1696 | |
| 1697 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 1698 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
| 1699 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1700 | Deduced.resize(TemplateParams->size()); |
| 1701 | |
| 1702 | // C++0x [temp.deduct.partial]p3: |
| 1703 | // The types used to determine the ordering depend on the context in which |
| 1704 | // the partial ordering is done: |
| 1705 | Sema::TemplateDeductionInfo Info(S.Context); |
| 1706 | switch (TPOC) { |
| 1707 | case TPOC_Call: { |
| 1708 | // - In the context of a function call, the function parameter types are |
| 1709 | // used. |
| 1710 | unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); |
| 1711 | for (unsigned I = 0; I != NumParams; ++I) |
| 1712 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1713 | TemplateParams, |
| 1714 | Proto2->getArgType(I), |
| 1715 | Proto1->getArgType(I), |
| 1716 | Info, |
| 1717 | Deduced, |
| 1718 | QualifierComparisons)) |
| 1719 | return false; |
| 1720 | |
| 1721 | break; |
| 1722 | } |
| 1723 | |
| 1724 | case TPOC_Conversion: |
| 1725 | // - In the context of a call to a conversion operator, the return types |
| 1726 | // of the conversion function templates are used. |
| 1727 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1728 | TemplateParams, |
| 1729 | Proto2->getResultType(), |
| 1730 | Proto1->getResultType(), |
| 1731 | Info, |
| 1732 | Deduced, |
| 1733 | QualifierComparisons)) |
| 1734 | return false; |
| 1735 | break; |
| 1736 | |
| 1737 | case TPOC_Other: |
| 1738 | // - In other contexts (14.6.6.2) the function template’s function type |
| 1739 | // is used. |
| 1740 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1741 | TemplateParams, |
| 1742 | FD2->getType(), |
| 1743 | FD1->getType(), |
| 1744 | Info, |
| 1745 | Deduced, |
| 1746 | QualifierComparisons)) |
| 1747 | return false; |
| 1748 | break; |
| 1749 | } |
| 1750 | |
| 1751 | // C++0x [temp.deduct.partial]p11: |
| 1752 | // In most cases, all template parameters must have values in order for |
| 1753 | // deduction to succeed, but for partial ordering purposes a template |
| 1754 | // parameter may remain without a value provided it is not used in the |
| 1755 | // types being used for partial ordering. [ Note: a template parameter used |
| 1756 | // in a non-deduced context is considered used. -end note] |
| 1757 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 1758 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 1759 | if (Deduced[ArgIdx].isNull()) |
| 1760 | break; |
| 1761 | |
| 1762 | if (ArgIdx == NumArgs) { |
| 1763 | // All template arguments were deduced. FT1 is at least as specialized |
| 1764 | // as FT2. |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1768 | // Figure out which template parameters were used. |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 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) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1775 | ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, |
| 1776 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1777 | break; |
| 1778 | } |
| 1779 | |
| 1780 | case TPOC_Conversion: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1781 | ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, |
| 1782 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1783 | break; |
| 1784 | |
| 1785 | case TPOC_Other: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1786 | ::MarkUsedTemplateParameters(S, FD2->getType(), false, UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1787 | break; |
| 1788 | } |
| 1789 | |
| 1790 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 1791 | // If this argument had no value deduced but was used in one of the types |
| 1792 | // used for partial ordering, then deduction fails. |
| 1793 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 1794 | return false; |
| 1795 | |
| 1796 | return true; |
| 1797 | } |
| 1798 | |
| 1799 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame^] | 1800 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1801 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 1802 | /// |
| 1803 | /// \param FT1 the first function template |
| 1804 | /// |
| 1805 | /// \param FT2 the second function template |
| 1806 | /// |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1807 | /// \param TPOC the context in which we are performing partial ordering of |
| 1808 | /// function templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | /// |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame^] | 1810 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1811 | /// template is more specialized, returns NULL. |
| 1812 | FunctionTemplateDecl * |
| 1813 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 1814 | FunctionTemplateDecl *FT2, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1815 | TemplatePartialOrderingContext TPOC) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1816 | llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; |
| 1817 | bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0); |
| 1818 | bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC, |
| 1819 | &QualifierComparisons); |
| 1820 | |
| 1821 | if (Better1 != Better2) // We have a clear winner |
| 1822 | return Better1? FT1 : FT2; |
| 1823 | |
| 1824 | if (!Better1 && !Better2) // Neither is better than the other |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1825 | return 0; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1826 | |
| 1827 | |
| 1828 | // C++0x [temp.deduct.partial]p10: |
| 1829 | // If for each type being considered a given template is at least as |
| 1830 | // specialized for all types and more specialized for some set of types and |
| 1831 | // the other template is not more specialized for any types or is not at |
| 1832 | // least as specialized for any types, then the given template is more |
| 1833 | // specialized than the other template. Otherwise, neither template is more |
| 1834 | // specialized than the other. |
| 1835 | Better1 = false; |
| 1836 | Better2 = false; |
| 1837 | for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { |
| 1838 | // C++0x [temp.deduct.partial]p9: |
| 1839 | // If, for a given type, deduction succeeds in both directions (i.e., the |
| 1840 | // types are identical after the transformations above) and if the type |
| 1841 | // from the argument template is more cv-qualified than the type from the |
| 1842 | // parameter template (as described above) that type is considered to be |
| 1843 | // more specialized than the other. If neither type is more cv-qualified |
| 1844 | // than the other then neither type is more specialized than the other. |
| 1845 | switch (QualifierComparisons[I]) { |
| 1846 | case NeitherMoreQualified: |
| 1847 | break; |
| 1848 | |
| 1849 | case ParamMoreQualified: |
| 1850 | Better1 = true; |
| 1851 | if (Better2) |
| 1852 | return 0; |
| 1853 | break; |
| 1854 | |
| 1855 | case ArgMoreQualified: |
| 1856 | Better2 = true; |
| 1857 | if (Better1) |
| 1858 | return 0; |
| 1859 | break; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | assert(!(Better1 && Better2) && "Should have broken out in the loop above"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1864 | if (Better1) |
| 1865 | return FT1; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1866 | else if (Better2) |
| 1867 | return FT2; |
| 1868 | else |
| 1869 | return 0; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1870 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1871 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame^] | 1872 | /// \brief Returns the more specialized class template partial specialization |
| 1873 | /// according to the rules of partial ordering of class template partial |
| 1874 | /// specializations (C++ [temp.class.order]). |
| 1875 | /// |
| 1876 | /// \param PS1 the first class template partial specialization |
| 1877 | /// |
| 1878 | /// \param PS2 the second class template partial specialization |
| 1879 | /// |
| 1880 | /// \returns the more specialized class template partial specialization. If |
| 1881 | /// neither partial specialization is more specialized, returns NULL. |
| 1882 | ClassTemplatePartialSpecializationDecl * |
| 1883 | Sema::getMoreSpecializedPartialSpecialization( |
| 1884 | ClassTemplatePartialSpecializationDecl *PS1, |
| 1885 | ClassTemplatePartialSpecializationDecl *PS2) { |
| 1886 | // C++ [temp.class.order]p1: |
| 1887 | // For two class template partial specializations, the first is at least as |
| 1888 | // specialized as the second if, given the following rewrite to two |
| 1889 | // function templates, the first function template is at least as |
| 1890 | // specialized as the second according to the ordering rules for function |
| 1891 | // templates (14.6.6.2): |
| 1892 | // - the first function template has the same template parameters as the |
| 1893 | // first partial specialization and has a single function parameter |
| 1894 | // whose type is a class template specialization with the template |
| 1895 | // arguments of the first partial specialization, and |
| 1896 | // - the second function template has the same template parameters as the |
| 1897 | // second partial specialization and has a single function parameter |
| 1898 | // whose type is a class template specialization with the template |
| 1899 | // arguments of the second partial specialization. |
| 1900 | // |
| 1901 | // Rather than synthesize function templates, we merely perform the |
| 1902 | // equivalent partial ordering by performing deduction directly on the |
| 1903 | // template arguments of the class template partial specializations. This |
| 1904 | // computation is slightly simpler than the general problem of function |
| 1905 | // template partial ordering, because class template partial specializations |
| 1906 | // are more constrained. We know that every template parameter is deduc |
| 1907 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1908 | Sema::TemplateDeductionInfo Info(Context); |
| 1909 | |
| 1910 | // Determine whether PS1 is at least as specialized as PS2 |
| 1911 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 1912 | bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, |
| 1913 | PS2->getTemplateParameters(), |
| 1914 | Context.getTypeDeclType(PS2), |
| 1915 | Context.getTypeDeclType(PS1), |
| 1916 | Info, |
| 1917 | Deduced, |
| 1918 | 0); |
| 1919 | |
| 1920 | // Determine whether PS2 is at least as specialized as PS1 |
| 1921 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 1922 | bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, |
| 1923 | PS1->getTemplateParameters(), |
| 1924 | Context.getTypeDeclType(PS1), |
| 1925 | Context.getTypeDeclType(PS2), |
| 1926 | Info, |
| 1927 | Deduced, |
| 1928 | 0); |
| 1929 | |
| 1930 | if (Better1 == Better2) |
| 1931 | return 0; |
| 1932 | |
| 1933 | return Better1? PS1 : PS2; |
| 1934 | } |
| 1935 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1937 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1938 | const TemplateArgument &TemplateArg, |
| 1939 | bool OnlyDeduced, |
| 1940 | llvm::SmallVectorImpl<bool> &Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1942 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1943 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1945 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1946 | const Expr *E, |
| 1947 | bool OnlyDeduced, |
| 1948 | llvm::SmallVectorImpl<bool> &Used) { |
| 1949 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
| 1950 | // find other occurrences of template parameters. |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1951 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1952 | if (!E) |
| 1953 | return; |
| 1954 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1956 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 1957 | if (!NTTP) |
| 1958 | return; |
| 1959 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1960 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1963 | /// \brief Mark the template parameters that are used by the given |
| 1964 | /// nested name specifier. |
| 1965 | static void |
| 1966 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1967 | NestedNameSpecifier *NNS, |
| 1968 | bool OnlyDeduced, |
| 1969 | llvm::SmallVectorImpl<bool> &Used) { |
| 1970 | if (!NNS) |
| 1971 | return; |
| 1972 | |
| 1973 | MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Used); |
| 1974 | MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), |
| 1975 | OnlyDeduced, Used); |
| 1976 | } |
| 1977 | |
| 1978 | /// \brief Mark the template parameters that are used by the given |
| 1979 | /// template name. |
| 1980 | static void |
| 1981 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 1982 | TemplateName Name, |
| 1983 | bool OnlyDeduced, |
| 1984 | llvm::SmallVectorImpl<bool> &Used) { |
| 1985 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1986 | if (TemplateTemplateParmDecl *TTP |
| 1987 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 1988 | Used[TTP->getIndex()] = true; |
| 1989 | return; |
| 1990 | } |
| 1991 | |
| 1992 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
| 1993 | MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, Used); |
| 1994 | } |
| 1995 | |
| 1996 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1997 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1999 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 2000 | bool OnlyDeduced, |
| 2001 | llvm::SmallVectorImpl<bool> &Used) { |
| 2002 | if (T.isNull()) |
| 2003 | return; |
| 2004 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2005 | // Non-dependent types have nothing deducible |
| 2006 | if (!T->isDependentType()) |
| 2007 | return; |
| 2008 | |
| 2009 | T = SemaRef.Context.getCanonicalType(T); |
| 2010 | switch (T->getTypeClass()) { |
| 2011 | case Type::ExtQual: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2012 | MarkUsedTemplateParameters(SemaRef, |
| 2013 | QualType(cast<ExtQualType>(T)->getBaseType(), 0), |
| 2014 | OnlyDeduced, |
| 2015 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2016 | break; |
| 2017 | |
| 2018 | case Type::Pointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2019 | MarkUsedTemplateParameters(SemaRef, |
| 2020 | cast<PointerType>(T)->getPointeeType(), |
| 2021 | OnlyDeduced, |
| 2022 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2023 | break; |
| 2024 | |
| 2025 | case Type::BlockPointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2026 | MarkUsedTemplateParameters(SemaRef, |
| 2027 | cast<BlockPointerType>(T)->getPointeeType(), |
| 2028 | OnlyDeduced, |
| 2029 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | |
| 2032 | case Type::LValueReference: |
| 2033 | case Type::RValueReference: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2034 | MarkUsedTemplateParameters(SemaRef, |
| 2035 | cast<ReferenceType>(T)->getPointeeType(), |
| 2036 | OnlyDeduced, |
| 2037 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2038 | break; |
| 2039 | |
| 2040 | case Type::MemberPointer: { |
| 2041 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2042 | MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, |
| 2043 | Used); |
| 2044 | MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
| 2045 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2046 | break; |
| 2047 | } |
| 2048 | |
| 2049 | case Type::DependentSizedArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2050 | MarkUsedTemplateParameters(SemaRef, |
| 2051 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
| 2052 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2053 | // Fall through to check the element type |
| 2054 | |
| 2055 | case Type::ConstantArray: |
| 2056 | case Type::IncompleteArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2057 | MarkUsedTemplateParameters(SemaRef, |
| 2058 | cast<ArrayType>(T)->getElementType(), |
| 2059 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2060 | break; |
| 2061 | |
| 2062 | case Type::Vector: |
| 2063 | case Type::ExtVector: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2064 | MarkUsedTemplateParameters(SemaRef, |
| 2065 | cast<VectorType>(T)->getElementType(), |
| 2066 | OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2067 | break; |
| 2068 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2069 | case Type::DependentSizedExtVector: { |
| 2070 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2071 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2072 | MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, |
| 2073 | Used); |
| 2074 | MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, |
| 2075 | Used); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2076 | break; |
| 2077 | } |
| 2078 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2079 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2080 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2081 | MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, |
| 2082 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2083 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2084 | MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, |
| 2085 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2086 | break; |
| 2087 | } |
| 2088 | |
| 2089 | case Type::TemplateTypeParm: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2090 | Used[cast<TemplateTypeParmType>(T)->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2091 | break; |
| 2092 | |
| 2093 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2095 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2096 | MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, |
| 2097 | Used); |
| 2098 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 2099 | MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | } |
| 2102 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2103 | case Type::Complex: |
| 2104 | if (!OnlyDeduced) |
| 2105 | MarkUsedTemplateParameters(SemaRef, |
| 2106 | cast<ComplexType>(T)->getElementType(), |
| 2107 | OnlyDeduced, Used); |
| 2108 | break; |
| 2109 | |
| 2110 | case Type::Typename: |
| 2111 | if (!OnlyDeduced) |
| 2112 | MarkUsedTemplateParameters(SemaRef, |
| 2113 | cast<TypenameType>(T)->getQualifier(), |
| 2114 | OnlyDeduced, Used); |
| 2115 | break; |
| 2116 | |
| 2117 | // None of these types have any template parameters in them. |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2118 | case Type::Builtin: |
| 2119 | case Type::FixedWidthInt: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2120 | case Type::VariableArray: |
| 2121 | case Type::FunctionNoProto: |
| 2122 | case Type::Record: |
| 2123 | case Type::Enum: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2124 | case Type::ObjCInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2125 | case Type::ObjCObjectPointer: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2126 | #define TYPE(Class, Base) |
| 2127 | #define ABSTRACT_TYPE(Class, Base) |
| 2128 | #define DEPENDENT_TYPE(Class, Base) |
| 2129 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2130 | #include "clang/AST/TypeNodes.def" |
| 2131 | break; |
| 2132 | } |
| 2133 | } |
| 2134 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2135 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2136 | /// template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2138 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2139 | const TemplateArgument &TemplateArg, |
| 2140 | bool OnlyDeduced, |
| 2141 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2142 | switch (TemplateArg.getKind()) { |
| 2143 | case TemplateArgument::Null: |
| 2144 | case TemplateArgument::Integral: |
| 2145 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2147 | case TemplateArgument::Type: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2148 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, |
| 2149 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2150 | break; |
| 2151 | |
| 2152 | case TemplateArgument::Declaration: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2154 | = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2155 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | |
| 2158 | case TemplateArgument::Expression: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2159 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, |
| 2160 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2161 | break; |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2162 | |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2163 | case TemplateArgument::Pack: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2164 | for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), |
| 2165 | PEnd = TemplateArg.pack_end(); |
| 2166 | P != PEnd; ++P) |
| 2167 | MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Used); |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2168 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | /// \brief Mark the template parameters can be deduced by the given |
| 2173 | /// template argument list. |
| 2174 | /// |
| 2175 | /// \param TemplateArgs the template argument list from which template |
| 2176 | /// parameters will be deduced. |
| 2177 | /// |
| 2178 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 2179 | /// to indicate when the corresponding template parameter will be |
| 2180 | /// deduced. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2182 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 2183 | bool OnlyDeduced, |
| 2184 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2185 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2186 | ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2187 | } |