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