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