Douglas Gregor | bf23a8a | 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 | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 21 | |
| 22 | namespace clang { |
| 23 | /// \brief Various flags that control template argument deduction. |
| 24 | /// |
| 25 | /// These flags can be bitwise-OR'd together. |
| 26 | enum TemplateDeductionFlags { |
| 27 | /// \brief No template argument deduction flags, which indicates the |
| 28 | /// strictest results for template argument deduction (as used for, e.g., |
| 29 | /// matching class template partial specializations). |
| 30 | TDF_None = 0, |
| 31 | /// \brief Within template argument deduction from a function call, we are |
| 32 | /// matching with a parameter type for which the original parameter was |
| 33 | /// a reference. |
| 34 | TDF_ParamWithReferenceType = 0x1, |
| 35 | /// \brief Within template argument deduction from a function call, we |
| 36 | /// are matching in a case where we ignore cv-qualifiers. |
| 37 | TDF_IgnoreQualifiers = 0x02, |
| 38 | /// \brief Within template argument deduction from a function call, |
| 39 | /// we are matching in a case where we can perform template argument |
| 40 | /// deduction from a derived class of the argument type. |
| 41 | /// FIXME: this is completely unsupported right now. |
| 42 | TDF_DerivedClass = 0x04 |
| 43 | }; |
| 44 | } |
| 45 | |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 46 | using namespace clang; |
| 47 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 48 | static Sema::TemplateDeductionResult |
| 49 | DeduceTemplateArguments(ASTContext &Context, |
| 50 | TemplateParameterList *TemplateParams, |
| 51 | const TemplateArgument &Param, |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 52 | const TemplateArgument &Arg, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 53 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 54 | llvm::SmallVectorImpl<TemplateArgument> &Deduced); |
| 55 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 56 | /// \brief If the given expression is of a form that permits the deduction |
| 57 | /// of a non-type template parameter, return the declaration of that |
| 58 | /// non-type template parameter. |
| 59 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
| 60 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 61 | E = IC->getSubExpr(); |
| 62 | |
| 63 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 64 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /// \brief Deduce the value of the given non-type template parameter |
| 70 | /// from the given constant. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 71 | static Sema::TemplateDeductionResult |
| 72 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 73 | NonTypeTemplateParmDecl *NTTP, |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 74 | llvm::APSInt Value, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 75 | Sema::TemplateDeductionInfo &Info, |
| 76 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 77 | assert(NTTP->getDepth() == 0 && |
| 78 | "Cannot deduce non-type template argument with depth > 0"); |
| 79 | |
| 80 | if (Deduced[NTTP->getIndex()].isNull()) { |
Anders Carlsson | 1de7089 | 2009-06-16 23:08:29 +0000 | [diff] [blame] | 81 | QualType T = NTTP->getType(); |
| 82 | |
| 83 | // FIXME: Make sure we didn't overflow our data type! |
| 84 | unsigned AllowedBits = Context.getTypeSize(T); |
| 85 | if (Value.getBitWidth() != AllowedBits) |
| 86 | Value.extOrTrunc(AllowedBits); |
| 87 | Value.setIsSigned(T->isSignedIntegerType()); |
| 88 | |
| 89 | Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 90 | return Sema::TDK_Success; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 93 | assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral); |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 94 | |
| 95 | // If the template argument was previously deduced to a negative value, |
| 96 | // then our deduction fails. |
| 97 | const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral(); |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 98 | if (PrevValuePtr->isNegative()) { |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 99 | Info.Param = NTTP; |
| 100 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 101 | Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 102 | return Sema::TDK_Inconsistent; |
| 103 | } |
| 104 | |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 105 | llvm::APSInt PrevValue = *PrevValuePtr; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 106 | if (Value.getBitWidth() > PrevValue.getBitWidth()) |
| 107 | PrevValue.zext(Value.getBitWidth()); |
| 108 | else if (Value.getBitWidth() < PrevValue.getBitWidth()) |
| 109 | Value.zext(PrevValue.getBitWidth()); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 110 | |
| 111 | if (Value != PrevValue) { |
| 112 | Info.Param = NTTP; |
| 113 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 114 | Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 115 | return Sema::TDK_Inconsistent; |
| 116 | } |
| 117 | |
| 118 | return Sema::TDK_Success; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /// \brief Deduce the value of the given non-type template parameter |
| 122 | /// from the given type- or value-dependent expression. |
| 123 | /// |
| 124 | /// \returns true if deduction succeeded, false otherwise. |
| 125 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 126 | static Sema::TemplateDeductionResult |
| 127 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 128 | NonTypeTemplateParmDecl *NTTP, |
| 129 | Expr *Value, |
| 130 | Sema::TemplateDeductionInfo &Info, |
| 131 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 132 | assert(NTTP->getDepth() == 0 && |
| 133 | "Cannot deduce non-type template argument with depth > 0"); |
| 134 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 135 | "Expression template argument must be type- or value-dependent."); |
| 136 | |
| 137 | if (Deduced[NTTP->getIndex()].isNull()) { |
| 138 | // FIXME: Clone the Value? |
| 139 | Deduced[NTTP->getIndex()] = TemplateArgument(Value); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 140 | return Sema::TDK_Success; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) { |
| 144 | // Okay, we deduced a constant in one case and a dependent expression |
| 145 | // in another case. FIXME: Later, we will check that instantiating the |
| 146 | // dependent expression gives us the constant value. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 147 | return Sema::TDK_Success; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // FIXME: Compare the expressions for equality! |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 151 | return Sema::TDK_Success; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 154 | static Sema::TemplateDeductionResult |
| 155 | DeduceTemplateArguments(ASTContext &Context, |
| 156 | TemplateName Param, |
| 157 | TemplateName Arg, |
| 158 | Sema::TemplateDeductionInfo &Info, |
| 159 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 160 | // FIXME: Implement template argument deduction for template |
| 161 | // template parameters. |
| 162 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 163 | // FIXME: this routine does not have enough information to produce |
| 164 | // good diagnostics. |
| 165 | |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 166 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
| 167 | TemplateDecl *ArgDecl = Arg.getAsTemplateDecl(); |
| 168 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 169 | if (!ParamDecl || !ArgDecl) { |
| 170 | // FIXME: fill in Info.Param/Info.FirstArg |
| 171 | return Sema::TDK_Inconsistent; |
| 172 | } |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 173 | |
| 174 | ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl)); |
| 175 | ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl)); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 176 | if (ParamDecl != ArgDecl) { |
| 177 | // FIXME: fill in Info.Param/Info.FirstArg |
| 178 | return Sema::TDK_Inconsistent; |
| 179 | } |
| 180 | |
| 181 | return Sema::TDK_Success; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 184 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 185 | /// the argument type (C++ [temp.deduct.type]). |
| 186 | /// |
| 187 | /// \param Context the AST context in which this deduction occurs. |
| 188 | /// |
| 189 | /// \param TemplateParams the template parameters that we are deducing |
| 190 | /// |
| 191 | /// \param ParamIn the parameter type |
| 192 | /// |
| 193 | /// \param ArgIn the argument type |
| 194 | /// |
| 195 | /// \param Info information about the template argument deduction itself |
| 196 | /// |
| 197 | /// \param Deduced the deduced template arguments |
| 198 | /// |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 199 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 200 | /// how template argument deduction is performed. |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 201 | /// |
| 202 | /// \returns the result of template argument deduction so far. Note that a |
| 203 | /// "success" result means that template argument deduction has not yet failed, |
| 204 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 205 | static Sema::TemplateDeductionResult |
| 206 | DeduceTemplateArguments(ASTContext &Context, |
| 207 | TemplateParameterList *TemplateParams, |
| 208 | QualType ParamIn, QualType ArgIn, |
| 209 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 210 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 211 | unsigned TDF) { |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 212 | // We only want to look at the canonical types, since typedefs and |
| 213 | // sugar are not part of template argument deduction. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 214 | QualType Param = Context.getCanonicalType(ParamIn); |
| 215 | QualType Arg = Context.getCanonicalType(ArgIn); |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 217 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 218 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 219 | // referred to by the reference) can be more cv-qualified than the |
| 220 | // transformed A. |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 221 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 222 | unsigned ExtraQualsOnParam |
| 223 | = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers(); |
| 224 | Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam); |
| 225 | } |
| 226 | |
Douglas Gregor | ad538e5 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 227 | // If the parameter type is not dependent, there is nothing to deduce. |
| 228 | if (!Param->isDependentType()) |
| 229 | return Sema::TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 231 | // C++ [temp.deduct.type]p9: |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 232 | // A template type argument T, a template template argument TT or a |
| 233 | // template non-type argument i can be deduced if P and A have one of |
| 234 | // the following forms: |
| 235 | // |
| 236 | // T |
| 237 | // cv-list T |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 238 | if (const TemplateTypeParmType *TemplateTypeParm |
| 239 | = Param->getAsTemplateTypeParmType()) { |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 240 | unsigned Index = TemplateTypeParm->getIndex(); |
| 241 | |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 242 | // The argument type can not be less qualified than the parameter |
| 243 | // type. |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 244 | if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 245 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 246 | Info.FirstArg = Deduced[Index]; |
| 247 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 248 | return Sema::TDK_InconsistentQuals; |
| 249 | } |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 250 | |
| 251 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
| 252 | |
| 253 | unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); |
| 254 | QualType DeducedType = Arg.getQualifiedType(Quals); |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 255 | |
| 256 | if (Deduced[Index].isNull()) |
| 257 | Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); |
| 258 | else { |
| 259 | // C++ [temp.deduct.type]p2: |
| 260 | // [...] If type deduction cannot be done for any P/A pair, or if for |
| 261 | // any pair the deduction leads to more than one possible set of |
| 262 | // deduced values, or if different pairs yield different deduced |
| 263 | // values, or if any template argument remains neither deduced nor |
| 264 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 265 | if (Deduced[Index].getAsType() != DeducedType) { |
| 266 | Info.Param |
| 267 | = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 268 | Info.FirstArg = Deduced[Index]; |
| 269 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 270 | return Sema::TDK_Inconsistent; |
| 271 | } |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 272 | } |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 273 | return Sema::TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 276 | // Set up the template argument deduction information for a failure. |
| 277 | Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); |
| 278 | Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); |
| 279 | |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 280 | // Check the cv-qualifiers on the parameter and argument types. |
| 281 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 282 | if (TDF & TDF_ParamWithReferenceType) { |
| 283 | if (Param.isMoreQualifiedThan(Arg)) |
| 284 | return Sema::TDK_NonDeducedMismatch; |
| 285 | } else { |
| 286 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
| 287 | return Sema::TDK_NonDeducedMismatch; |
| 288 | } |
| 289 | } |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 291 | switch (Param->getTypeClass()) { |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 292 | // No deduction possible for these types |
| 293 | case Type::Builtin: |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 294 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 295 | |
| 296 | // T * |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 297 | case Type::Pointer: { |
| 298 | const PointerType *PointerArg = Arg->getAsPointerType(); |
| 299 | if (!PointerArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 300 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 302 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 303 | cast<PointerType>(Param)->getPointeeType(), |
| 304 | PointerArg->getPointeeType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 305 | Info, Deduced, |
| 306 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 309 | // T & |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 310 | case Type::LValueReference: { |
| 311 | const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType(); |
| 312 | if (!ReferenceArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 313 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 314 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 315 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 316 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 317 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 318 | Info, Deduced, 0); |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 319 | } |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 321 | // T && [C++0x] |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 322 | case Type::RValueReference: { |
| 323 | const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType(); |
| 324 | if (!ReferenceArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 325 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 326 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 327 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 328 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 329 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 330 | Info, Deduced, 0); |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 333 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 334 | case Type::IncompleteArray: { |
| 335 | const IncompleteArrayType *IncompleteArrayArg = |
| 336 | Context.getAsIncompleteArrayType(Arg); |
| 337 | if (!IncompleteArrayArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 338 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 340 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 341 | Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 342 | IncompleteArrayArg->getElementType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 343 | Info, Deduced, 0); |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 345 | |
| 346 | // T [integer-constant] |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 347 | case Type::ConstantArray: { |
| 348 | const ConstantArrayType *ConstantArrayArg = |
| 349 | Context.getAsConstantArrayType(Arg); |
| 350 | if (!ConstantArrayArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 351 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 352 | |
| 353 | const ConstantArrayType *ConstantArrayParm = |
| 354 | Context.getAsConstantArrayType(Param); |
| 355 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 356 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 358 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 359 | ConstantArrayParm->getElementType(), |
| 360 | ConstantArrayArg->getElementType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 361 | Info, Deduced, 0); |
Anders Carlsson | 0bcee30 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 364 | // type [i] |
| 365 | case Type::DependentSizedArray: { |
| 366 | const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); |
| 367 | if (!ArrayArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 368 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 369 | |
| 370 | // Check the element type of the arrays |
| 371 | const DependentSizedArrayType *DependentArrayParm |
| 372 | = cast<DependentSizedArrayType>(Param); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 373 | if (Sema::TemplateDeductionResult Result |
| 374 | = DeduceTemplateArguments(Context, TemplateParams, |
| 375 | DependentArrayParm->getElementType(), |
| 376 | ArrayArg->getElementType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 377 | Info, Deduced, 0)) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 378 | return Result; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 379 | |
| 380 | // Determine the array bound is something we can deduce. |
| 381 | NonTypeTemplateParmDecl *NTTP |
| 382 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 383 | if (!NTTP) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 384 | return Sema::TDK_Success; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 385 | |
| 386 | // We can perform template argument deduction for the given non-type |
| 387 | // template parameter. |
| 388 | assert(NTTP->getDepth() == 0 && |
| 389 | "Cannot deduce non-type template argument at depth > 0"); |
| 390 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 391 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 392 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
| 393 | return DeduceNonTypeTemplateArgument(Context, NTTP, Size, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 394 | Info, Deduced); |
Anders Carlsson | cfb830e | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 395 | } |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 396 | if (const DependentSizedArrayType *DependentArrayArg |
| 397 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| 398 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 399 | DependentArrayArg->getSizeExpr(), |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 400 | Info, Deduced); |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 401 | |
| 402 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 403 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Douglas Gregor | 1c67c00 | 2009-06-08 15:59:14 +0000 | [diff] [blame] | 406 | // type(*)(T) |
| 407 | // T(*)() |
| 408 | // T(*)(T) |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 409 | case Type::FunctionProto: { |
| 410 | const FunctionProtoType *FunctionProtoArg = |
| 411 | dyn_cast<FunctionProtoType>(Arg); |
| 412 | if (!FunctionProtoArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 413 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 414 | |
| 415 | const FunctionProtoType *FunctionProtoParam = |
| 416 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 3e39f92 | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 417 | |
| 418 | if (FunctionProtoParam->getTypeQuals() != |
| 419 | FunctionProtoArg->getTypeQuals()) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 420 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 421 | |
Anders Carlsson | 3e39f92 | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 422 | if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 423 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 3e39f92 | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 424 | |
| 425 | if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 426 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 3e39f92 | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 427 | |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 428 | // Check return types. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 429 | if (Sema::TemplateDeductionResult Result |
| 430 | = DeduceTemplateArguments(Context, TemplateParams, |
| 431 | FunctionProtoParam->getResultType(), |
| 432 | FunctionProtoArg->getResultType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 433 | Info, Deduced, 0)) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 434 | return Result; |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 435 | |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 436 | for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { |
| 437 | // Check argument types. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 438 | if (Sema::TemplateDeductionResult Result |
| 439 | = DeduceTemplateArguments(Context, TemplateParams, |
| 440 | FunctionProtoParam->getArgType(I), |
| 441 | FunctionProtoArg->getArgType(I), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 442 | Info, Deduced, 0)) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 443 | return Result; |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 446 | return Sema::TDK_Success; |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 447 | } |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | ad538e5 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 449 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 450 | // template-name<i> |
| 451 | // TT<T> (TODO) |
| 452 | // TT<i> (TODO) |
| 453 | // TT<> (TODO) |
| 454 | case Type::TemplateSpecialization: { |
| 455 | const TemplateSpecializationType *SpecParam |
| 456 | = cast<TemplateSpecializationType>(Param); |
| 457 | |
| 458 | // Check whether the template argument is a dependent template-id. |
| 459 | // FIXME: This is untested code; it can be tested when we implement |
| 460 | // partial ordering of class template partial specializations. |
| 461 | if (const TemplateSpecializationType *SpecArg |
| 462 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 463 | // Perform template argument deduction for the template name. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 464 | if (Sema::TemplateDeductionResult Result |
| 465 | = DeduceTemplateArguments(Context, |
| 466 | SpecParam->getTemplateName(), |
| 467 | SpecArg->getTemplateName(), |
| 468 | Info, Deduced)) |
| 469 | return Result; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 470 | |
| 471 | unsigned NumArgs = SpecParam->getNumArgs(); |
| 472 | |
| 473 | // FIXME: When one of the template-names refers to a |
| 474 | // declaration with default template arguments, do we need to |
| 475 | // fill in those default template arguments here? Most likely, |
| 476 | // the answer is "yes", but I don't see any references. This |
| 477 | // issue may be resolved elsewhere, because we may want to |
| 478 | // instantiate default template arguments when |
| 479 | if (SpecArg->getNumArgs() != NumArgs) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 480 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 481 | |
| 482 | // Perform template argument deduction on each template |
| 483 | // argument. |
| 484 | for (unsigned I = 0; I != NumArgs; ++I) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 485 | if (Sema::TemplateDeductionResult Result |
| 486 | = DeduceTemplateArguments(Context, TemplateParams, |
| 487 | SpecParam->getArg(I), |
| 488 | SpecArg->getArg(I), |
| 489 | Info, Deduced)) |
| 490 | return Result; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 492 | return Sema::TDK_Success; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // If the argument type is a class template specialization, we |
| 496 | // perform template argument deduction using its template |
| 497 | // arguments. |
| 498 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| 499 | if (!RecordArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 500 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 501 | |
| 502 | ClassTemplateSpecializationDecl *SpecArg |
| 503 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| 504 | if (!SpecArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 505 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 506 | |
| 507 | // Perform template argument deduction for the template name. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 508 | if (Sema::TemplateDeductionResult Result |
| 509 | = DeduceTemplateArguments(Context, |
| 510 | SpecParam->getTemplateName(), |
| 511 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 512 | Info, Deduced)) |
| 513 | return Result; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 514 | |
| 515 | // FIXME: Can the # of arguments in the parameter and the argument differ? |
| 516 | unsigned NumArgs = SpecParam->getNumArgs(); |
| 517 | const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); |
| 518 | if (NumArgs != ArgArgs.size()) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 519 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 520 | |
| 521 | for (unsigned I = 0; I != NumArgs; ++I) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 522 | if (Sema::TemplateDeductionResult Result |
| 523 | = DeduceTemplateArguments(Context, TemplateParams, |
| 524 | SpecParam->getArg(I), |
| 525 | ArgArgs.get(I), |
| 526 | Info, Deduced)) |
| 527 | return Result; |
Anders Carlsson | 8574e7f | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 529 | return Sema::TDK_Success; |
Douglas Gregor | 5069ae3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 532 | // T type::* |
| 533 | // T T::* |
| 534 | // T (type::*)() |
| 535 | // type (T::*)() |
| 536 | // type (type::*)(T) |
| 537 | // type (T::*)(T) |
| 538 | // T (type::*)(T) |
| 539 | // T (T::*)() |
| 540 | // T (T::*)(T) |
| 541 | case Type::MemberPointer: { |
| 542 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 543 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 544 | if (!MemPtrArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 545 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 547 | if (Sema::TemplateDeductionResult Result |
| 548 | = DeduceTemplateArguments(Context, TemplateParams, |
| 549 | MemPtrParam->getPointeeType(), |
| 550 | MemPtrArg->getPointeeType(), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 551 | Info, Deduced, |
| 552 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 553 | return Result; |
| 554 | |
| 555 | return DeduceTemplateArguments(Context, TemplateParams, |
| 556 | QualType(MemPtrParam->getClass(), 0), |
| 557 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 558 | Info, Deduced, 0); |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Anders Carlsson | db52d34 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 561 | // (clang extension) |
| 562 | // |
Anders Carlsson | 9d914d3 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 563 | // type(^)(T) |
| 564 | // T(^)() |
| 565 | // T(^)(T) |
| 566 | case Type::BlockPointer: { |
| 567 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 568 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
| 569 | |
| 570 | if (!BlockPtrArg) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 571 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 9d914d3 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 572 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 573 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 9d914d3 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 574 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 575 | BlockPtrArg->getPointeeType(), Info, |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 576 | Deduced, 0); |
Anders Carlsson | 9d914d3 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 579 | case Type::TypeOfExpr: |
| 580 | case Type::TypeOf: |
| 581 | case Type::Typename: |
| 582 | // No template argument deduction for these types |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 583 | return Sema::TDK_Success; |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 4ea653d | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 585 | default: |
| 586 | break; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | ad538e5 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 590 | return Sema::TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 593 | static Sema::TemplateDeductionResult |
| 594 | DeduceTemplateArguments(ASTContext &Context, |
| 595 | TemplateParameterList *TemplateParams, |
| 596 | const TemplateArgument &Param, |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 597 | const TemplateArgument &Arg, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 598 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 599 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 600 | switch (Param.getKind()) { |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 601 | case TemplateArgument::Null: |
| 602 | assert(false && "Null template argument in parameter list"); |
| 603 | break; |
| 604 | |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 605 | case TemplateArgument::Type: |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 606 | assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 607 | return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), |
| 608 | Arg.getAsType(), Info, Deduced, 0); |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 610 | case TemplateArgument::Declaration: |
| 611 | // FIXME: Implement this check |
| 612 | assert(false && "Unimplemented template argument deduction case"); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 613 | Info.FirstArg = Param; |
| 614 | Info.SecondArg = Arg; |
| 615 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 616 | |
| 617 | case TemplateArgument::Integral: |
| 618 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 619 | // FIXME: Zero extension + sign checking here? |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 620 | if (*Param.getAsIntegral() == *Arg.getAsIntegral()) |
| 621 | return Sema::TDK_Success; |
| 622 | |
| 623 | Info.FirstArg = Param; |
| 624 | Info.SecondArg = Arg; |
| 625 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 626 | } |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 627 | |
| 628 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 629 | Info.FirstArg = Param; |
| 630 | Info.SecondArg = Arg; |
| 631 | return Sema::TDK_NonDeducedMismatch; |
| 632 | } |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 633 | |
| 634 | assert(false && "Type/value mismatch"); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 635 | Info.FirstArg = Param; |
| 636 | Info.SecondArg = Arg; |
| 637 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 638 | |
| 639 | case TemplateArgument::Expression: { |
| 640 | if (NonTypeTemplateParmDecl *NTTP |
| 641 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 642 | if (Arg.getKind() == TemplateArgument::Integral) |
| 643 | // FIXME: Sign problems here |
| 644 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 645 | *Arg.getAsIntegral(), |
| 646 | Info, Deduced); |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 647 | if (Arg.getKind() == TemplateArgument::Expression) |
| 648 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 649 | Info, Deduced); |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 650 | |
| 651 | assert(false && "Type/value mismatch"); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 652 | Info.FirstArg = Param; |
| 653 | Info.SecondArg = Arg; |
| 654 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 658 | return Sema::TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 659 | } |
Anders Carlsson | 584b506 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 660 | case TemplateArgument::Pack: |
| 661 | assert(0 && "FIXME: Implement!"); |
| 662 | break; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 665 | return Sema::TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 668 | static Sema::TemplateDeductionResult |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 669 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 670 | TemplateParameterList *TemplateParams, |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 671 | const TemplateArgumentList &ParamList, |
| 672 | const TemplateArgumentList &ArgList, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 673 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 674 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 675 | assert(ParamList.size() == ArgList.size()); |
| 676 | for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 677 | if (Sema::TemplateDeductionResult Result |
| 678 | = DeduceTemplateArguments(Context, TemplateParams, |
| 679 | ParamList[I], ArgList[I], |
| 680 | Info, Deduced)) |
| 681 | return Result; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 682 | } |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 683 | return Sema::TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Douglas Gregor | ad538e5 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 686 | /// \brief Determine whether two template arguments are the same. |
| 687 | static bool isSameTemplateArg(ASTContext &Context, |
| 688 | const TemplateArgument &X, |
| 689 | const TemplateArgument &Y) { |
| 690 | if (X.getKind() != Y.getKind()) |
| 691 | return false; |
| 692 | |
| 693 | switch (X.getKind()) { |
| 694 | case TemplateArgument::Null: |
| 695 | assert(false && "Comparing NULL template argument"); |
| 696 | break; |
| 697 | |
| 698 | case TemplateArgument::Type: |
| 699 | return Context.getCanonicalType(X.getAsType()) == |
| 700 | Context.getCanonicalType(Y.getAsType()); |
| 701 | |
| 702 | case TemplateArgument::Declaration: |
| 703 | return Context.getCanonicalDecl(X.getAsDecl()) == |
| 704 | Context.getCanonicalDecl(Y.getAsDecl()); |
| 705 | |
| 706 | case TemplateArgument::Integral: |
| 707 | return *X.getAsIntegral() == *Y.getAsIntegral(); |
| 708 | |
| 709 | case TemplateArgument::Expression: |
| 710 | // FIXME: We assume that all expressions are distinct, but we should |
| 711 | // really check their canonical forms. |
| 712 | return false; |
| 713 | |
| 714 | case TemplateArgument::Pack: |
| 715 | if (X.pack_size() != Y.pack_size()) |
| 716 | return false; |
| 717 | |
| 718 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 719 | XPEnd = X.pack_end(), |
| 720 | YP = Y.pack_begin(); |
| 721 | XP != XPEnd; ++XP, ++YP) |
| 722 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 723 | return false; |
| 724 | |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | /// \brief Helper function to build a TemplateParameter when we don't |
| 732 | /// know its type statically. |
| 733 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 734 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 735 | return TemplateParameter(TTP); |
| 736 | else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 737 | return TemplateParameter(NTTP); |
| 738 | |
| 739 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 740 | } |
| 741 | |
Douglas Gregor | 21530c5 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 742 | /// \brief Perform template argument deduction to determine whether |
| 743 | /// the given template arguments match the given class template |
| 744 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 745 | Sema::TemplateDeductionResult |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 746 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 747 | const TemplateArgumentList &TemplateArgs, |
| 748 | TemplateDeductionInfo &Info) { |
Douglas Gregor | 21530c5 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 749 | // C++ [temp.class.spec.match]p2: |
| 750 | // A partial specialization matches a given actual template |
| 751 | // argument list if the template arguments of the partial |
| 752 | // specialization can be deduced from the actual template argument |
| 753 | // list (14.8.2). |
Douglas Gregor | 003d740 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 754 | SFINAETrap Trap(*this); |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 755 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 756 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 757 | if (TemplateDeductionResult Result |
| 758 | = ::DeduceTemplateArguments(Context, |
| 759 | Partial->getTemplateParameters(), |
| 760 | Partial->getTemplateArgs(), |
| 761 | TemplateArgs, Info, Deduced)) |
| 762 | return Result; |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 763 | |
Douglas Gregor | c5e01af | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 764 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
| 765 | Deduced.data(), Deduced.size()); |
| 766 | if (Inst) |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 767 | return TDK_InstantiationDepth; |
Douglas Gregor | ab9f71a | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 768 | |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 769 | // C++ [temp.deduct.type]p2: |
| 770 | // [...] or if any template argument remains neither deduced nor |
| 771 | // explicitly specified, template argument deduction fails. |
Anders Carlsson | b0fc999 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 772 | TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), |
| 773 | Deduced.size()); |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 774 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 775 | if (Deduced[I].isNull()) { |
| 776 | Decl *Param |
| 777 | = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I)); |
| 778 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 779 | Info.Param = TTP; |
| 780 | else if (NonTypeTemplateParmDecl *NTTP |
| 781 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 782 | Info.Param = NTTP; |
| 783 | else |
| 784 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 785 | return TDK_Incomplete; |
| 786 | } |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 787 | |
Anders Carlsson | b0fc999 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 788 | Builder.Append(Deduced[I]); |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | // Form the template argument list from the deduced template arguments. |
| 792 | TemplateArgumentList *DeducedArgumentList |
Anders Carlsson | b0fc999 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 793 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 794 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 796 | // Substitute the deduced template arguments into the template |
| 797 | // arguments of the class template partial specialization, and |
| 798 | // verify that the instantiated template arguments are both valid |
| 799 | // and are equivalent to the template arguments originally provided |
Douglas Gregor | 7573b3b | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 800 | // to the class template. |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 801 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 802 | const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); |
| 803 | for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { |
Douglas Gregor | 7573b3b | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 804 | Decl *Param = const_cast<Decl *>( |
| 805 | ClassTemplate->getTemplateParameters()->getParam(I)); |
Douglas Gregor | ad538e5 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 806 | TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I], |
| 807 | *DeducedArgumentList); |
| 808 | if (InstArg.isNull()) { |
| 809 | Info.Param = makeTemplateParameter(Param); |
| 810 | Info.FirstArg = PartialTemplateArgs[I]; |
| 811 | return TDK_SubstitutionFailure; |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 812 | } |
Douglas Gregor | ad538e5 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 813 | |
| 814 | if (InstArg.getKind() == TemplateArgument::Expression) { |
| 815 | // When the argument is an expression, check the expression result |
| 816 | // against the actual template parameter to get down to the canonical |
| 817 | // template argument. |
| 818 | Expr *InstExpr = InstArg.getAsExpr(); |
| 819 | if (NonTypeTemplateParmDecl *NTTP |
| 820 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 821 | if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { |
| 822 | Info.Param = makeTemplateParameter(Param); |
| 823 | Info.FirstArg = PartialTemplateArgs[I]; |
| 824 | return TDK_SubstitutionFailure; |
| 825 | } |
| 826 | } else if (TemplateTemplateParmDecl *TTP |
| 827 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 828 | // FIXME: template template arguments should really resolve to decls |
| 829 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); |
| 830 | if (!DRE || CheckTemplateArgument(TTP, DRE)) { |
| 831 | Info.Param = makeTemplateParameter(Param); |
| 832 | Info.FirstArg = PartialTemplateArgs[I]; |
| 833 | return TDK_SubstitutionFailure; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { |
| 839 | Info.Param = makeTemplateParameter(Param); |
| 840 | Info.FirstArg = TemplateArgs[I]; |
| 841 | Info.SecondArg = InstArg; |
| 842 | return TDK_NonDeducedMismatch; |
| 843 | } |
Douglas Gregor | 8f378a9 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Douglas Gregor | 003d740 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 846 | if (Trap.hasErrorOccurred()) |
| 847 | return TDK_SubstitutionFailure; |
| 848 | |
Douglas Gregor | 623e2e0 | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 849 | return TDK_Success; |
Douglas Gregor | bf23a8a | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 850 | } |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 852 | /// \brief Perform template argument deduction from a function call |
| 853 | /// (C++ [temp.deduct.call]). |
| 854 | /// |
| 855 | /// \param FunctionTemplate the function template for which we are performing |
| 856 | /// template argument deduction. |
| 857 | /// |
| 858 | /// \param Args the function call arguments |
| 859 | /// |
| 860 | /// \param NumArgs the number of arguments in Args |
| 861 | /// |
| 862 | /// \param Specialization if template argument deduction was successful, |
| 863 | /// this will be set to the function template specialization produced by |
| 864 | /// template argument deduction. |
| 865 | /// |
| 866 | /// \param Info the argument will be updated to provide additional information |
| 867 | /// about template argument deduction. |
| 868 | /// |
| 869 | /// \returns the result of template argument deduction. |
| 870 | /// |
| 871 | /// FIXME: We will also need to pass in any explicitly-specified template |
| 872 | /// arguments. |
| 873 | Sema::TemplateDeductionResult |
| 874 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 875 | Expr **Args, unsigned NumArgs, |
| 876 | FunctionDecl *&Specialization, |
| 877 | TemplateDeductionInfo &Info) { |
| 878 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 879 | |
| 880 | // C++ [temp.deduct.call]p1: |
| 881 | // Template argument deduction is done by comparing each function template |
| 882 | // parameter type (call it P) with the type of the corresponding argument |
| 883 | // of the call (call it A) as described below. |
| 884 | unsigned CheckArgs = NumArgs; |
| 885 | if (NumArgs < Function->getNumParams()) |
| 886 | return TDK_TooFewArguments; |
| 887 | else if (NumArgs > Function->getNumParams()) { |
| 888 | const FunctionProtoType *Proto |
| 889 | = Function->getType()->getAsFunctionProtoType(); |
| 890 | if (!Proto->isVariadic()) |
| 891 | return TDK_TooManyArguments; |
| 892 | |
| 893 | CheckArgs = Function->getNumParams(); |
| 894 | } |
| 895 | |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 896 | // Template argument deduction for function templates in a SFINAE context. |
| 897 | // Trap any errors that might occur. |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 898 | SFINAETrap Trap(*this); |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 899 | |
| 900 | // Deduce template arguments from the function parameters. |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 901 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 902 | Deduced.resize(FunctionTemplate->getTemplateParameters()->size()); |
| 903 | TemplateParameterList *TemplateParams |
| 904 | = FunctionTemplate->getTemplateParameters(); |
| 905 | for (unsigned I = 0; I != CheckArgs; ++I) { |
| 906 | QualType ParamType = Function->getParamDecl(I)->getType(); |
| 907 | QualType ArgType = Args[I]->getType(); |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 909 | // C++ [temp.deduct.call]p2: |
| 910 | // If P is not a reference type: |
| 911 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 912 | bool ParamWasReference = isa<ReferenceType>(CanonParamType); |
| 913 | if (!ParamWasReference) { |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 914 | // - If A is an array type, the pointer type produced by the |
| 915 | // array-to-pointer standard conversion (4.2) is used in place of |
| 916 | // A for type deduction; otherwise, |
| 917 | if (ArgType->isArrayType()) |
| 918 | ArgType = Context.getArrayDecayedType(ArgType); |
| 919 | // - If A is a function type, the pointer type produced by the |
| 920 | // function-to-pointer standard conversion (4.3) is used in place |
| 921 | // of A for type deduction; otherwise, |
| 922 | else if (ArgType->isFunctionType()) |
| 923 | ArgType = Context.getPointerType(ArgType); |
| 924 | else { |
| 925 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 926 | // type are ignored for type deduction. |
| 927 | QualType CanonArgType = Context.getCanonicalType(ArgType); |
| 928 | if (CanonArgType.getCVRQualifiers()) |
| 929 | ArgType = CanonArgType.getUnqualifiedType(); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | // C++0x [temp.deduct.call]p3: |
| 934 | // If P is a cv-qualified type, the top level cv-qualifiers of P’s type |
| 935 | // are ignored for type deduction. |
| 936 | if (CanonParamType.getCVRQualifiers()) |
| 937 | ParamType = CanonParamType.getUnqualifiedType(); |
| 938 | if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) { |
| 939 | // [...] If P is a reference type, the type referred to by P is used |
| 940 | // for type deduction. |
| 941 | ParamType = ParamRefType->getPointeeType(); |
| 942 | |
| 943 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 944 | // the argument is an lvalue, the type A& is used in place of A for |
| 945 | // type deduction. |
| 946 | if (isa<RValueReferenceType>(ParamRefType) && |
| 947 | ParamRefType->getAsTemplateTypeParmType() && |
| 948 | Args[I]->isLvalue(Context) == Expr::LV_Valid) |
| 949 | ArgType = Context.getLValueReferenceType(ArgType); |
| 950 | } |
| 951 | |
| 952 | // C++0x [temp.deduct.call]p4: |
| 953 | // In general, the deduction process attempts to find template argument |
| 954 | // values that will make the deduced A identical to A (after the type A |
| 955 | // is transformed as described above). [...] |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 956 | unsigned TDF = 0; |
| 957 | |
| 958 | // - If the original P is a reference type, the deduced A (i.e., the |
| 959 | // type referred to by the reference) can be more cv-qualified than |
| 960 | // the transformed A. |
| 961 | if (ParamWasReference) |
| 962 | TDF |= TDF_ParamWithReferenceType; |
| 963 | // - The transformed A can be another pointer or pointer to member |
| 964 | // type that can be converted to the deduced A via a qualification |
| 965 | // conversion (4.4). |
| 966 | if (ArgType->isPointerType() || ArgType->isMemberPointerType()) |
| 967 | TDF |= TDF_IgnoreQualifiers; |
| 968 | // FIXME: derived -> base checks |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 969 | if (TemplateDeductionResult Result |
| 970 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 78b6f56 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 971 | ParamType, ArgType, Info, Deduced, |
Douglas Gregor | 6111276 | 2009-06-26 23:10:12 +0000 | [diff] [blame^] | 972 | TDF)) |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 973 | return Result; |
| 974 | |
| 975 | // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function |
| 976 | // pointer parameters. |
| 977 | } |
| 978 | |
| 979 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
| 980 | FunctionTemplate, Deduced.data(), Deduced.size()); |
| 981 | if (Inst) |
| 982 | return TDK_InstantiationDepth; |
| 983 | |
| 984 | // C++ [temp.deduct.type]p2: |
| 985 | // [...] or if any template argument remains neither deduced nor |
| 986 | // explicitly specified, template argument deduction fails. |
| 987 | TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); |
| 988 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 989 | if (Deduced[I].isNull()) { |
| 990 | Decl *Param |
| 991 | = const_cast<Decl *>(TemplateParams->getParam(I)); |
| 992 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 993 | Info.Param = TTP; |
| 994 | else if (NonTypeTemplateParmDecl *NTTP |
| 995 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 996 | Info.Param = NTTP; |
| 997 | else |
| 998 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 999 | return TDK_Incomplete; |
| 1000 | } |
| 1001 | |
| 1002 | Builder.Append(Deduced[I]); |
| 1003 | } |
| 1004 | |
| 1005 | // Form the template argument list from the deduced template arguments. |
| 1006 | TemplateArgumentList *DeducedArgumentList |
| 1007 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1008 | Info.reset(DeducedArgumentList); |
| 1009 | |
| 1010 | // Substitute the deduced template arguments into the function template |
| 1011 | // declaration to produce the function template specialization. |
| 1012 | Specialization = cast_or_null<FunctionDecl>( |
| 1013 | InstantiateDecl(FunctionTemplate->getTemplatedDecl(), |
| 1014 | FunctionTemplate->getDeclContext(), |
| 1015 | *DeducedArgumentList)); |
| 1016 | |
| 1017 | if (!Specialization || Trap.hasErrorOccurred()) |
| 1018 | return TDK_SubstitutionFailure; |
| 1019 | |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1020 | // Turn the specialization into an actual function template specialization. |
| 1021 | Specialization->setFunctionTemplateSpecialization(Context, |
| 1022 | FunctionTemplate, |
| 1023 | Info.take()); |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1024 | return TDK_Success; |
| 1025 | } |
| 1026 | |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1027 | static void |
| 1028 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 1029 | const TemplateArgument &TemplateArg, |
| 1030 | llvm::SmallVectorImpl<bool> &Deduced); |
| 1031 | |
| 1032 | /// \brief Mark the template arguments that are deduced by the given |
| 1033 | /// expression. |
| 1034 | static void |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1035 | MarkDeducedTemplateParameters(const Expr *E, |
| 1036 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1037 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1038 | if (!E) |
| 1039 | return; |
| 1040 | |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1041 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1042 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 1043 | if (!NTTP) |
| 1044 | return; |
| 1045 | |
| 1046 | Deduced[NTTP->getIndex()] = true; |
| 1047 | } |
| 1048 | |
| 1049 | /// \brief Mark the template parameters that are deduced by the given |
| 1050 | /// type. |
| 1051 | static void |
| 1052 | MarkDeducedTemplateParameters(Sema &SemaRef, QualType T, |
| 1053 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1054 | // Non-dependent types have nothing deducible |
| 1055 | if (!T->isDependentType()) |
| 1056 | return; |
| 1057 | |
| 1058 | T = SemaRef.Context.getCanonicalType(T); |
| 1059 | switch (T->getTypeClass()) { |
| 1060 | case Type::ExtQual: |
| 1061 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1062 | QualType(cast<ExtQualType>(T)->getBaseType(), 0), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1063 | Deduced); |
| 1064 | break; |
| 1065 | |
| 1066 | case Type::Pointer: |
| 1067 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1068 | cast<PointerType>(T)->getPointeeType(), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1069 | Deduced); |
| 1070 | break; |
| 1071 | |
| 1072 | case Type::BlockPointer: |
| 1073 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1074 | cast<BlockPointerType>(T)->getPointeeType(), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1075 | Deduced); |
| 1076 | break; |
| 1077 | |
| 1078 | case Type::LValueReference: |
| 1079 | case Type::RValueReference: |
| 1080 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1081 | cast<ReferenceType>(T)->getPointeeType(), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1082 | Deduced); |
| 1083 | break; |
| 1084 | |
| 1085 | case Type::MemberPointer: { |
| 1086 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
| 1087 | MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); |
| 1088 | MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
| 1089 | Deduced); |
| 1090 | break; |
| 1091 | } |
| 1092 | |
| 1093 | case Type::DependentSizedArray: |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1094 | MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1095 | Deduced); |
| 1096 | // Fall through to check the element type |
| 1097 | |
| 1098 | case Type::ConstantArray: |
| 1099 | case Type::IncompleteArray: |
| 1100 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1101 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1102 | Deduced); |
| 1103 | break; |
| 1104 | |
| 1105 | case Type::Vector: |
| 1106 | case Type::ExtVector: |
| 1107 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1108 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1109 | Deduced); |
| 1110 | break; |
| 1111 | |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1112 | case Type::DependentSizedExtVector: { |
| 1113 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1114 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1115 | MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced); |
| 1116 | MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced); |
| 1117 | break; |
| 1118 | } |
| 1119 | |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1120 | case Type::FunctionProto: { |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1121 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1122 | MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); |
| 1123 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
| 1124 | MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); |
| 1125 | break; |
| 1126 | } |
| 1127 | |
| 1128 | case Type::TemplateTypeParm: |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1129 | Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true; |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | |
| 1132 | case Type::TemplateSpecialization: { |
| 1133 | const TemplateSpecializationType *Spec |
Douglas Gregor | 68a7a6d | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1134 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1135 | if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) |
| 1136 | if (TemplateTemplateParmDecl *TTP |
| 1137 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 1138 | Deduced[TTP->getIndex()] = true; |
| 1139 | |
| 1140 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 1141 | MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); |
| 1142 | |
| 1143 | break; |
| 1144 | } |
| 1145 | |
| 1146 | // None of these types have any deducible parts. |
| 1147 | case Type::Builtin: |
| 1148 | case Type::FixedWidthInt: |
| 1149 | case Type::Complex: |
| 1150 | case Type::VariableArray: |
| 1151 | case Type::FunctionNoProto: |
| 1152 | case Type::Record: |
| 1153 | case Type::Enum: |
| 1154 | case Type::Typename: |
| 1155 | case Type::ObjCInterface: |
| 1156 | case Type::ObjCQualifiedInterface: |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1157 | case Type::ObjCObjectPointer: |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1158 | #define TYPE(Class, Base) |
| 1159 | #define ABSTRACT_TYPE(Class, Base) |
| 1160 | #define DEPENDENT_TYPE(Class, Base) |
| 1161 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1162 | #include "clang/AST/TypeNodes.def" |
| 1163 | break; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | /// \brief Mark the template parameters that are deduced by this |
| 1168 | /// template argument. |
| 1169 | static void |
| 1170 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 1171 | const TemplateArgument &TemplateArg, |
| 1172 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1173 | switch (TemplateArg.getKind()) { |
| 1174 | case TemplateArgument::Null: |
| 1175 | case TemplateArgument::Integral: |
| 1176 | break; |
| 1177 | |
| 1178 | case TemplateArgument::Type: |
| 1179 | MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); |
| 1180 | break; |
| 1181 | |
| 1182 | case TemplateArgument::Declaration: |
| 1183 | if (TemplateTemplateParmDecl *TTP |
| 1184 | = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) |
| 1185 | Deduced[TTP->getIndex()] = true; |
| 1186 | break; |
| 1187 | |
| 1188 | case TemplateArgument::Expression: |
| 1189 | MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); |
| 1190 | break; |
Anders Carlsson | 584b506 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1191 | case TemplateArgument::Pack: |
| 1192 | assert(0 && "FIXME: Implement!"); |
| 1193 | break; |
Douglas Gregor | f90c213 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | /// \brief Mark the template parameters can be deduced by the given |
| 1198 | /// template argument list. |
| 1199 | /// |
| 1200 | /// \param TemplateArgs the template argument list from which template |
| 1201 | /// parameters will be deduced. |
| 1202 | /// |
| 1203 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 1204 | /// to indicate when the corresponding template parameter will be |
| 1205 | /// deduced. |
| 1206 | void |
| 1207 | Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 1208 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1209 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 1210 | ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); |
| 1211 | } |