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