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, |
| 49 | llvm::APInt Value, |
| 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()) { |
| 56 | Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), |
| 57 | llvm::APSInt(Value), |
| 58 | NTTP->getType()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 59 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 62 | assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 63 | |
| 64 | // If the template argument was previously deduced to a negative value, |
| 65 | // then our deduction fails. |
| 66 | const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral(); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 67 | if (PrevValuePtr->isSigned() && PrevValuePtr->isNegative()) { |
| 68 | // FIXME: This is wacky; we should be dealing with APSInts and |
| 69 | // checking the actual signs. |
| 70 | Info.Param = NTTP; |
| 71 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 72 | Info.SecondArg = TemplateArgument(SourceLocation(), |
| 73 | llvm::APSInt(Value), |
| 74 | NTTP->getType()); |
| 75 | return Sema::TDK_Inconsistent; |
| 76 | } |
| 77 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 78 | llvm::APInt PrevValue = *PrevValuePtr; |
| 79 | if (Value.getBitWidth() > PrevValue.getBitWidth()) |
| 80 | PrevValue.zext(Value.getBitWidth()); |
| 81 | else if (Value.getBitWidth() < PrevValue.getBitWidth()) |
| 82 | Value.zext(PrevValue.getBitWidth()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 83 | |
| 84 | if (Value != PrevValue) { |
| 85 | Info.Param = NTTP; |
| 86 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 87 | Info.SecondArg = TemplateArgument(SourceLocation(), |
| 88 | llvm::APSInt(Value), |
| 89 | NTTP->getType()); |
| 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 | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 159 | static Sema::TemplateDeductionResult |
| 160 | DeduceTemplateArguments(ASTContext &Context, |
| 161 | TemplateParameterList *TemplateParams, |
| 162 | QualType ParamIn, QualType ArgIn, |
| 163 | Sema::TemplateDeductionInfo &Info, |
| 164 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 165 | // We only want to look at the canonical types, since typedefs and |
| 166 | // sugar are not part of template argument deduction. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 167 | QualType Param = Context.getCanonicalType(ParamIn); |
| 168 | QualType Arg = Context.getCanonicalType(ArgIn); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 169 | |
| 170 | // If the parameter type is not dependent, just compare the types |
| 171 | // directly. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 172 | if (!Param->isDependentType()) { |
| 173 | if (Param == Arg) |
| 174 | return Sema::TDK_Success; |
| 175 | |
| 176 | Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); |
| 177 | Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); |
| 178 | return Sema::TDK_NonDeducedMismatch; |
| 179 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 180 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 181 | // C++ [temp.deduct.type]p9: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 182 | // A template type argument T, a template template argument TT or a |
| 183 | // template non-type argument i can be deduced if P and A have one of |
| 184 | // the following forms: |
| 185 | // |
| 186 | // T |
| 187 | // cv-list T |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 188 | if (const TemplateTypeParmType *TemplateTypeParm |
| 189 | = Param->getAsTemplateTypeParmType()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 190 | unsigned Index = TemplateTypeParm->getIndex(); |
| 191 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 192 | // The argument type can not be less qualified than the parameter |
| 193 | // type. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 194 | if (Param.isMoreQualifiedThan(Arg)) { |
| 195 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 196 | Info.FirstArg = Deduced[Index]; |
| 197 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 198 | return Sema::TDK_InconsistentQuals; |
| 199 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 200 | |
| 201 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
| 202 | |
| 203 | unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); |
| 204 | QualType DeducedType = Arg.getQualifiedType(Quals); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 205 | |
| 206 | if (Deduced[Index].isNull()) |
| 207 | Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); |
| 208 | else { |
| 209 | // C++ [temp.deduct.type]p2: |
| 210 | // [...] If type deduction cannot be done for any P/A pair, or if for |
| 211 | // any pair the deduction leads to more than one possible set of |
| 212 | // deduced values, or if different pairs yield different deduced |
| 213 | // values, or if any template argument remains neither deduced nor |
| 214 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 215 | if (Deduced[Index].getAsType() != DeducedType) { |
| 216 | Info.Param |
| 217 | = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 218 | Info.FirstArg = Deduced[Index]; |
| 219 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 220 | return Sema::TDK_Inconsistent; |
| 221 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 222 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 223 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 226 | // Set up the template argument deduction information for a failure. |
| 227 | Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); |
| 228 | Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); |
| 229 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 230 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 231 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 233 | switch (Param->getTypeClass()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 234 | // No deduction possible for these types |
| 235 | case Type::Builtin: |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 236 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 237 | |
| 238 | // T * |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 239 | case Type::Pointer: { |
| 240 | const PointerType *PointerArg = Arg->getAsPointerType(); |
| 241 | if (!PointerArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 242 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 244 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 245 | cast<PointerType>(Param)->getPointeeType(), |
| 246 | PointerArg->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 247 | Info, Deduced); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 250 | // T & |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 251 | case Type::LValueReference: { |
| 252 | const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType(); |
| 253 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 254 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 256 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 257 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 258 | ReferenceArg->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 259 | Info, Deduced); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 260 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 262 | // T && [C++0x] |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 263 | case Type::RValueReference: { |
| 264 | const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType(); |
| 265 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 266 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 268 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 269 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 270 | ReferenceArg->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 271 | Info, Deduced); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 274 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 275 | case Type::IncompleteArray: { |
| 276 | const IncompleteArrayType *IncompleteArrayArg = |
| 277 | Context.getAsIncompleteArrayType(Arg); |
| 278 | if (!IncompleteArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 279 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 281 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 282 | Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 283 | IncompleteArrayArg->getElementType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 284 | Info, Deduced); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 285 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 286 | |
| 287 | // T [integer-constant] |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 288 | case Type::ConstantArray: { |
| 289 | const ConstantArrayType *ConstantArrayArg = |
| 290 | Context.getAsConstantArrayType(Arg); |
| 291 | if (!ConstantArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 292 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 293 | |
| 294 | const ConstantArrayType *ConstantArrayParm = |
| 295 | Context.getAsConstantArrayType(Param); |
| 296 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 297 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 299 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 300 | ConstantArrayParm->getElementType(), |
| 301 | ConstantArrayArg->getElementType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 302 | Info, Deduced); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 305 | // type [i] |
| 306 | case Type::DependentSizedArray: { |
| 307 | const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); |
| 308 | if (!ArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 309 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 310 | |
| 311 | // Check the element type of the arrays |
| 312 | const DependentSizedArrayType *DependentArrayParm |
| 313 | = cast<DependentSizedArrayType>(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 314 | if (Sema::TemplateDeductionResult Result |
| 315 | = DeduceTemplateArguments(Context, TemplateParams, |
| 316 | DependentArrayParm->getElementType(), |
| 317 | ArrayArg->getElementType(), |
| 318 | Info, Deduced)) |
| 319 | return Result; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 320 | |
| 321 | // Determine the array bound is something we can deduce. |
| 322 | NonTypeTemplateParmDecl *NTTP |
| 323 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 324 | if (!NTTP) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 325 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 326 | |
| 327 | // We can perform template argument deduction for the given non-type |
| 328 | // template parameter. |
| 329 | assert(NTTP->getDepth() == 0 && |
| 330 | "Cannot deduce non-type template argument at depth > 0"); |
| 331 | if (const ConstantArrayType *ConstantArrayArg |
| 332 | = dyn_cast<ConstantArrayType>(ArrayArg)) |
| 333 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 334 | ConstantArrayArg->getSize(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 335 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 336 | if (const DependentSizedArrayType *DependentArrayArg |
| 337 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| 338 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 339 | DependentArrayArg->getSizeExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 340 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 341 | |
| 342 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 343 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Douglas Gregor | 0fce0ae | 2009-06-08 15:59:14 +0000 | [diff] [blame] | 346 | // type(*)(T) |
| 347 | // T(*)() |
| 348 | // T(*)(T) |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 349 | case Type::FunctionProto: { |
| 350 | const FunctionProtoType *FunctionProtoArg = |
| 351 | dyn_cast<FunctionProtoType>(Arg); |
| 352 | if (!FunctionProtoArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 353 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 354 | |
| 355 | const FunctionProtoType *FunctionProtoParam = |
| 356 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 357 | |
| 358 | if (FunctionProtoParam->getTypeQuals() != |
| 359 | FunctionProtoArg->getTypeQuals()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 360 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 361 | |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 362 | if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 363 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 364 | |
| 365 | if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 366 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 367 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 368 | // Check return types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 369 | if (Sema::TemplateDeductionResult Result |
| 370 | = DeduceTemplateArguments(Context, TemplateParams, |
| 371 | FunctionProtoParam->getResultType(), |
| 372 | FunctionProtoArg->getResultType(), |
| 373 | Info, Deduced)) |
| 374 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 375 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 376 | for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { |
| 377 | // Check argument types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 378 | if (Sema::TemplateDeductionResult Result |
| 379 | = DeduceTemplateArguments(Context, TemplateParams, |
| 380 | FunctionProtoParam->getArgType(I), |
| 381 | FunctionProtoArg->getArgType(I), |
| 382 | Info, Deduced)) |
| 383 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 386 | return Sema::TDK_Success; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 387 | } |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 388 | |
| 389 | // template-name<T> (wheretemplate-name refers to a class template) |
| 390 | // template-name<i> |
| 391 | // TT<T> (TODO) |
| 392 | // TT<i> (TODO) |
| 393 | // TT<> (TODO) |
| 394 | case Type::TemplateSpecialization: { |
| 395 | const TemplateSpecializationType *SpecParam |
| 396 | = cast<TemplateSpecializationType>(Param); |
| 397 | |
| 398 | // Check whether the template argument is a dependent template-id. |
| 399 | // FIXME: This is untested code; it can be tested when we implement |
| 400 | // partial ordering of class template partial specializations. |
| 401 | if (const TemplateSpecializationType *SpecArg |
| 402 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 403 | // Perform template argument deduction for the template name. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 404 | if (Sema::TemplateDeductionResult Result |
| 405 | = DeduceTemplateArguments(Context, |
| 406 | SpecParam->getTemplateName(), |
| 407 | SpecArg->getTemplateName(), |
| 408 | Info, Deduced)) |
| 409 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 410 | |
| 411 | unsigned NumArgs = SpecParam->getNumArgs(); |
| 412 | |
| 413 | // FIXME: When one of the template-names refers to a |
| 414 | // declaration with default template arguments, do we need to |
| 415 | // fill in those default template arguments here? Most likely, |
| 416 | // the answer is "yes", but I don't see any references. This |
| 417 | // issue may be resolved elsewhere, because we may want to |
| 418 | // instantiate default template arguments when |
| 419 | if (SpecArg->getNumArgs() != NumArgs) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 420 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 421 | |
| 422 | // Perform template argument deduction on each template |
| 423 | // argument. |
| 424 | for (unsigned I = 0; I != NumArgs; ++I) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 425 | if (Sema::TemplateDeductionResult Result |
| 426 | = DeduceTemplateArguments(Context, TemplateParams, |
| 427 | SpecParam->getArg(I), |
| 428 | SpecArg->getArg(I), |
| 429 | Info, Deduced)) |
| 430 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 432 | return Sema::TDK_Success; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // If the argument type is a class template specialization, we |
| 436 | // perform template argument deduction using its template |
| 437 | // arguments. |
| 438 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| 439 | if (!RecordArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 440 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 441 | |
| 442 | ClassTemplateSpecializationDecl *SpecArg |
| 443 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| 444 | if (!SpecArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 445 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 446 | |
| 447 | // Perform template argument deduction for the template name. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 448 | if (Sema::TemplateDeductionResult Result |
| 449 | = DeduceTemplateArguments(Context, |
| 450 | SpecParam->getTemplateName(), |
| 451 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 452 | Info, Deduced)) |
| 453 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 454 | |
| 455 | // FIXME: Can the # of arguments in the parameter and the argument differ? |
| 456 | unsigned NumArgs = SpecParam->getNumArgs(); |
| 457 | const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); |
| 458 | if (NumArgs != ArgArgs.size()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 459 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 460 | |
| 461 | for (unsigned I = 0; I != NumArgs; ++I) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 462 | if (Sema::TemplateDeductionResult Result |
| 463 | = DeduceTemplateArguments(Context, TemplateParams, |
| 464 | SpecParam->getArg(I), |
| 465 | ArgArgs.get(I), |
| 466 | Info, Deduced)) |
| 467 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 469 | return Sema::TDK_Success; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 472 | // T type::* |
| 473 | // T T::* |
| 474 | // T (type::*)() |
| 475 | // type (T::*)() |
| 476 | // type (type::*)(T) |
| 477 | // type (T::*)(T) |
| 478 | // T (type::*)(T) |
| 479 | // T (T::*)() |
| 480 | // T (T::*)(T) |
| 481 | case Type::MemberPointer: { |
| 482 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 483 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 484 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 485 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 487 | if (Sema::TemplateDeductionResult Result |
| 488 | = DeduceTemplateArguments(Context, TemplateParams, |
| 489 | MemPtrParam->getPointeeType(), |
| 490 | MemPtrArg->getPointeeType(), |
| 491 | Info, Deduced)) |
| 492 | return Result; |
| 493 | |
| 494 | return DeduceTemplateArguments(Context, TemplateParams, |
| 495 | QualType(MemPtrParam->getClass(), 0), |
| 496 | QualType(MemPtrArg->getClass(), 0), |
| 497 | Info, Deduced); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 500 | // (clang extension) |
| 501 | // |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 502 | // type(^)(T) |
| 503 | // T(^)() |
| 504 | // T(^)(T) |
| 505 | case Type::BlockPointer: { |
| 506 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 507 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
| 508 | |
| 509 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 510 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 512 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 513 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 514 | BlockPtrArg->getPointeeType(), Info, |
| 515 | Deduced); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 518 | case Type::TypeOfExpr: |
| 519 | case Type::TypeOf: |
| 520 | case Type::Typename: |
| 521 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 522 | return Sema::TDK_Success; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 524 | default: |
| 525 | break; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 529 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 532 | static Sema::TemplateDeductionResult |
| 533 | DeduceTemplateArguments(ASTContext &Context, |
| 534 | TemplateParameterList *TemplateParams, |
| 535 | const TemplateArgument &Param, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 536 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 537 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 538 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 539 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 540 | case TemplateArgument::Null: |
| 541 | assert(false && "Null template argument in parameter list"); |
| 542 | break; |
| 543 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 544 | case TemplateArgument::Type: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 545 | assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 546 | return DeduceTemplateArguments(Context, TemplateParams, |
| 547 | Param.getAsType(), |
| 548 | Arg.getAsType(), Info, Deduced); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 550 | case TemplateArgument::Declaration: |
| 551 | // FIXME: Implement this check |
| 552 | assert(false && "Unimplemented template argument deduction case"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 553 | Info.FirstArg = Param; |
| 554 | Info.SecondArg = Arg; |
| 555 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 556 | |
| 557 | case TemplateArgument::Integral: |
| 558 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 559 | // FIXME: Zero extension + sign checking here? |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 560 | if (*Param.getAsIntegral() == *Arg.getAsIntegral()) |
| 561 | return Sema::TDK_Success; |
| 562 | |
| 563 | Info.FirstArg = Param; |
| 564 | Info.SecondArg = Arg; |
| 565 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 566 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 567 | |
| 568 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 569 | Info.FirstArg = Param; |
| 570 | Info.SecondArg = Arg; |
| 571 | return Sema::TDK_NonDeducedMismatch; |
| 572 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 573 | |
| 574 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 575 | Info.FirstArg = Param; |
| 576 | Info.SecondArg = Arg; |
| 577 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 578 | |
| 579 | case TemplateArgument::Expression: { |
| 580 | if (NonTypeTemplateParmDecl *NTTP |
| 581 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 582 | if (Arg.getKind() == TemplateArgument::Integral) |
| 583 | // FIXME: Sign problems here |
| 584 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 585 | *Arg.getAsIntegral(), |
| 586 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 587 | if (Arg.getKind() == TemplateArgument::Expression) |
| 588 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 589 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 590 | |
| 591 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 592 | Info.FirstArg = Param; |
| 593 | Info.SecondArg = Arg; |
| 594 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 598 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 599 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame^] | 600 | case TemplateArgument::Pack: |
| 601 | assert(0 && "FIXME: Implement!"); |
| 602 | break; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 605 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 608 | static Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 609 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 610 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 611 | const TemplateArgumentList &ParamList, |
| 612 | const TemplateArgumentList &ArgList, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 613 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 614 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 615 | assert(ParamList.size() == ArgList.size()); |
| 616 | for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 617 | if (Sema::TemplateDeductionResult Result |
| 618 | = DeduceTemplateArguments(Context, TemplateParams, |
| 619 | ParamList[I], ArgList[I], |
| 620 | Info, Deduced)) |
| 621 | return Result; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 622 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 623 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 626 | /// \brief Perform template argument deduction to determine whether |
| 627 | /// the given template arguments match the given class template |
| 628 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 629 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 630 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 631 | const TemplateArgumentList &TemplateArgs, |
| 632 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 633 | // C++ [temp.class.spec.match]p2: |
| 634 | // A partial specialization matches a given actual template |
| 635 | // argument list if the template arguments of the partial |
| 636 | // specialization can be deduced from the actual template argument |
| 637 | // list (14.8.2). |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 638 | SFINAETrap Trap(*this); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 639 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 640 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 641 | if (TemplateDeductionResult Result |
| 642 | = ::DeduceTemplateArguments(Context, |
| 643 | Partial->getTemplateParameters(), |
| 644 | Partial->getTemplateArgs(), |
| 645 | TemplateArgs, Info, Deduced)) |
| 646 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 647 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 648 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
| 649 | Deduced.data(), Deduced.size()); |
| 650 | if (Inst) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 651 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 652 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 653 | // C++ [temp.deduct.type]p2: |
| 654 | // [...] or if any template argument remains neither deduced nor |
| 655 | // explicitly specified, template argument deduction fails. |
| 656 | TemplateArgumentListBuilder Builder(Context); |
| 657 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 658 | if (Deduced[I].isNull()) { |
| 659 | Decl *Param |
| 660 | = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I)); |
| 661 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 662 | Info.Param = TTP; |
| 663 | else if (NonTypeTemplateParmDecl *NTTP |
| 664 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 665 | Info.Param = NTTP; |
| 666 | else |
| 667 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 668 | return TDK_Incomplete; |
| 669 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 670 | |
| 671 | Builder.push_back(Deduced[I]); |
| 672 | } |
| 673 | |
| 674 | // Form the template argument list from the deduced template arguments. |
| 675 | TemplateArgumentList *DeducedArgumentList |
| 676 | = new (Context) TemplateArgumentList(Context, Builder, /*CopyArgs=*/true, |
| 677 | /*FlattenArgs=*/true); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 678 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 679 | |
| 680 | // Now that we have all of the deduced template arguments, take |
| 681 | // another pass through them to convert any integral template |
| 682 | // arguments to the appropriate type. |
| 683 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 684 | TemplateArgument &Arg = Deduced[I]; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 685 | if (Arg.getKind() == TemplateArgument::Integral) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 686 | const NonTypeTemplateParmDecl *Parm |
| 687 | = cast<NonTypeTemplateParmDecl>(Partial->getTemplateParameters() |
| 688 | ->getParam(I)); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 689 | QualType T = InstantiateType(Parm->getType(), *DeducedArgumentList, |
| 690 | Parm->getLocation(), Parm->getDeclName()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 691 | if (T.isNull()) { |
| 692 | Info.Param = const_cast<NonTypeTemplateParmDecl*>(Parm); |
| 693 | Info.FirstArg = TemplateArgument(Parm->getLocation(), Parm->getType()); |
| 694 | return TDK_SubstitutionFailure; |
| 695 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 696 | |
| 697 | // FIXME: Make sure we didn't overflow our data type! |
| 698 | llvm::APSInt &Value = *Arg.getAsIntegral(); |
| 699 | unsigned AllowedBits = Context.getTypeSize(T); |
| 700 | if (Value.getBitWidth() != AllowedBits) |
| 701 | Value.extOrTrunc(AllowedBits); |
| 702 | Value.setIsSigned(T->isSignedIntegerType()); |
| 703 | Arg.setIntegralType(T); |
| 704 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 705 | |
| 706 | (*DeducedArgumentList)[I] = Arg; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 707 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 708 | |
| 709 | // Substitute the deduced template arguments into the template |
| 710 | // arguments of the class template partial specialization, and |
| 711 | // verify that the instantiated template arguments are both valid |
| 712 | // and are equivalent to the template arguments originally provided |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 713 | // to the class template. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 714 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 715 | const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); |
| 716 | for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 717 | Decl *Param = const_cast<Decl *>( |
| 718 | ClassTemplate->getTemplateParameters()->getParam(I)); |
| 719 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 720 | TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I], |
| 721 | *DeducedArgumentList); |
| 722 | if (InstArg.getKind() != TemplateArgument::Type) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 723 | Info.Param = TTP; |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 724 | Info.FirstArg = PartialTemplateArgs[I]; |
| 725 | return TDK_SubstitutionFailure; |
| 726 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 727 | |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 728 | if (Context.getCanonicalType(InstArg.getAsType()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 729 | != Context.getCanonicalType(TemplateArgs[I].getAsType())) { |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 730 | Info.Param = TTP; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 731 | Info.FirstArg = TemplateArgs[I]; |
| 732 | Info.SecondArg = InstArg; |
| 733 | return TDK_NonDeducedMismatch; |
| 734 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 736 | continue; |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 737 | } |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 738 | |
| 739 | // FIXME: Check template template arguments? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 742 | if (Trap.hasErrorOccurred()) |
| 743 | return TDK_SubstitutionFailure; |
| 744 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 745 | return TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 746 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 747 | |
| 748 | static void |
| 749 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 750 | const TemplateArgument &TemplateArg, |
| 751 | llvm::SmallVectorImpl<bool> &Deduced); |
| 752 | |
| 753 | /// \brief Mark the template arguments that are deduced by the given |
| 754 | /// expression. |
| 755 | static void |
| 756 | MarkDeducedTemplateParameters(Expr *E, llvm::SmallVectorImpl<bool> &Deduced) { |
| 757 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 758 | if (!E) |
| 759 | return; |
| 760 | |
| 761 | NonTypeTemplateParmDecl *NTTP |
| 762 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 763 | if (!NTTP) |
| 764 | return; |
| 765 | |
| 766 | Deduced[NTTP->getIndex()] = true; |
| 767 | } |
| 768 | |
| 769 | /// \brief Mark the template parameters that are deduced by the given |
| 770 | /// type. |
| 771 | static void |
| 772 | MarkDeducedTemplateParameters(Sema &SemaRef, QualType T, |
| 773 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 774 | // Non-dependent types have nothing deducible |
| 775 | if (!T->isDependentType()) |
| 776 | return; |
| 777 | |
| 778 | T = SemaRef.Context.getCanonicalType(T); |
| 779 | switch (T->getTypeClass()) { |
| 780 | case Type::ExtQual: |
| 781 | MarkDeducedTemplateParameters(SemaRef, |
| 782 | QualType(cast<ExtQualType>(T.getTypePtr())->getBaseType(), 0), |
| 783 | Deduced); |
| 784 | break; |
| 785 | |
| 786 | case Type::Pointer: |
| 787 | MarkDeducedTemplateParameters(SemaRef, |
| 788 | cast<PointerType>(T.getTypePtr())->getPointeeType(), |
| 789 | Deduced); |
| 790 | break; |
| 791 | |
| 792 | case Type::BlockPointer: |
| 793 | MarkDeducedTemplateParameters(SemaRef, |
| 794 | cast<BlockPointerType>(T.getTypePtr())->getPointeeType(), |
| 795 | Deduced); |
| 796 | break; |
| 797 | |
| 798 | case Type::LValueReference: |
| 799 | case Type::RValueReference: |
| 800 | MarkDeducedTemplateParameters(SemaRef, |
| 801 | cast<ReferenceType>(T.getTypePtr())->getPointeeType(), |
| 802 | Deduced); |
| 803 | break; |
| 804 | |
| 805 | case Type::MemberPointer: { |
| 806 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
| 807 | MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); |
| 808 | MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
| 809 | Deduced); |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | case Type::DependentSizedArray: |
| 814 | MarkDeducedTemplateParameters( |
| 815 | cast<DependentSizedArrayType>(T.getTypePtr())->getSizeExpr(), |
| 816 | Deduced); |
| 817 | // Fall through to check the element type |
| 818 | |
| 819 | case Type::ConstantArray: |
| 820 | case Type::IncompleteArray: |
| 821 | MarkDeducedTemplateParameters(SemaRef, |
| 822 | cast<ArrayType>(T.getTypePtr())->getElementType(), |
| 823 | Deduced); |
| 824 | break; |
| 825 | |
| 826 | case Type::Vector: |
| 827 | case Type::ExtVector: |
| 828 | MarkDeducedTemplateParameters(SemaRef, |
| 829 | cast<VectorType>(T.getTypePtr())->getElementType(), |
| 830 | Deduced); |
| 831 | break; |
| 832 | |
| 833 | case Type::FunctionProto: { |
| 834 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T.getTypePtr()); |
| 835 | MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); |
| 836 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
| 837 | MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | case Type::TemplateTypeParm: |
| 842 | Deduced[cast<TemplateTypeParmType>(T.getTypePtr())->getIndex()] = true; |
| 843 | break; |
| 844 | |
| 845 | case Type::TemplateSpecialization: { |
| 846 | const TemplateSpecializationType *Spec |
| 847 | = cast<TemplateSpecializationType>(T.getTypePtr()); |
| 848 | if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) |
| 849 | if (TemplateTemplateParmDecl *TTP |
| 850 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 851 | Deduced[TTP->getIndex()] = true; |
| 852 | |
| 853 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 854 | MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); |
| 855 | |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | // None of these types have any deducible parts. |
| 860 | case Type::Builtin: |
| 861 | case Type::FixedWidthInt: |
| 862 | case Type::Complex: |
| 863 | case Type::VariableArray: |
| 864 | case Type::FunctionNoProto: |
| 865 | case Type::Record: |
| 866 | case Type::Enum: |
| 867 | case Type::Typename: |
| 868 | case Type::ObjCInterface: |
| 869 | case Type::ObjCQualifiedInterface: |
| 870 | case Type::ObjCQualifiedId: |
| 871 | #define TYPE(Class, Base) |
| 872 | #define ABSTRACT_TYPE(Class, Base) |
| 873 | #define DEPENDENT_TYPE(Class, Base) |
| 874 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 875 | #include "clang/AST/TypeNodes.def" |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /// \brief Mark the template parameters that are deduced by this |
| 881 | /// template argument. |
| 882 | static void |
| 883 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 884 | const TemplateArgument &TemplateArg, |
| 885 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 886 | switch (TemplateArg.getKind()) { |
| 887 | case TemplateArgument::Null: |
| 888 | case TemplateArgument::Integral: |
| 889 | break; |
| 890 | |
| 891 | case TemplateArgument::Type: |
| 892 | MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); |
| 893 | break; |
| 894 | |
| 895 | case TemplateArgument::Declaration: |
| 896 | if (TemplateTemplateParmDecl *TTP |
| 897 | = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) |
| 898 | Deduced[TTP->getIndex()] = true; |
| 899 | break; |
| 900 | |
| 901 | case TemplateArgument::Expression: |
| 902 | MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); |
| 903 | break; |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame^] | 904 | case TemplateArgument::Pack: |
| 905 | assert(0 && "FIXME: Implement!"); |
| 906 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | /// \brief Mark the template parameters can be deduced by the given |
| 911 | /// template argument list. |
| 912 | /// |
| 913 | /// \param TemplateArgs the template argument list from which template |
| 914 | /// parameters will be deduced. |
| 915 | /// |
| 916 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 917 | /// to indicate when the corresponding template parameter will be |
| 918 | /// deduced. |
| 919 | void |
| 920 | Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 921 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 922 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 923 | ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); |
| 924 | } |