clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaTemplateDeduction.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaTemplateDeduction.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/Sema/TemplateDeduction.h" |
| 14 | #include "TreeTransform.h" |
| 15 | #include "TypeLocBuilder.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/ASTLambda.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclAccessPair.h" |
| 20 | #include "clang/AST/DeclBase.h" |
| 21 | #include "clang/AST/DeclCXX.h" |
| 22 | #include "clang/AST/DeclTemplate.h" |
| 23 | #include "clang/AST/DeclarationName.h" |
| 24 | #include "clang/AST/Expr.h" |
| 25 | #include "clang/AST/ExprCXX.h" |
| 26 | #include "clang/AST/NestedNameSpecifier.h" |
| 27 | #include "clang/AST/RecursiveASTVisitor.h" |
| 28 | #include "clang/AST/TemplateBase.h" |
| 29 | #include "clang/AST/TemplateName.h" |
| 30 | #include "clang/AST/Type.h" |
| 31 | #include "clang/AST/TypeLoc.h" |
| 32 | #include "clang/AST/UnresolvedSet.h" |
| 33 | #include "clang/Basic/AddressSpaces.h" |
| 34 | #include "clang/Basic/ExceptionSpecificationType.h" |
| 35 | #include "clang/Basic/LLVM.h" |
| 36 | #include "clang/Basic/LangOptions.h" |
| 37 | #include "clang/Basic/PartialDiagnostic.h" |
| 38 | #include "clang/Basic/SourceLocation.h" |
| 39 | #include "clang/Basic/Specifiers.h" |
| 40 | #include "clang/Sema/Ownership.h" |
| 41 | #include "clang/Sema/Sema.h" |
| 42 | #include "clang/Sema/Template.h" |
| 43 | #include "llvm/ADT/APInt.h" |
| 44 | #include "llvm/ADT/APSInt.h" |
| 45 | #include "llvm/ADT/ArrayRef.h" |
| 46 | #include "llvm/ADT/DenseMap.h" |
| 47 | #include "llvm/ADT/FoldingSet.h" |
| 48 | #include "llvm/ADT/Optional.h" |
| 49 | #include "llvm/ADT/SmallBitVector.h" |
| 50 | #include "llvm/ADT/SmallPtrSet.h" |
| 51 | #include "llvm/ADT/SmallVector.h" |
| 52 | #include "llvm/Support/Casting.h" |
| 53 | #include "llvm/Support/Compiler.h" |
| 54 | #include "llvm/Support/ErrorHandling.h" |
| 55 | #include <algorithm> |
| 56 | #include <cassert> |
| 57 | #include <tuple> |
| 58 | #include <utility> |
| 59 | |
| 60 | namespace clang { |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | enum TemplateDeductionFlags { |
| 66 | |
| 67 | |
| 68 | |
| 69 | TDF_None = 0, |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | TDF_ParamWithReferenceType = 0x1, |
| 75 | |
| 76 | |
| 77 | |
| 78 | TDF_IgnoreQualifiers = 0x02, |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | TDF_DerivedClass = 0x04, |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | TDF_SkipNonDependent = 0x08, |
| 89 | |
| 90 | |
| 91 | |
| 92 | TDF_TopLevelParameterTypeList = 0x10, |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | TDF_AllowCompatibleFunctionType = 0x20, |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | TDF_ArgWithReferenceType = 0x40, |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | using namespace clang; |
| 111 | using namespace sema; |
| 112 | |
| 113 | |
| 114 | |
| 115 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 116 | if (Y.getBitWidth() > X.getBitWidth()) |
| 117 | X = X.extend(Y.getBitWidth()); |
| 118 | else if (Y.getBitWidth() < X.getBitWidth()) |
| 119 | Y = Y.extend(X.getBitWidth()); |
| 120 | |
| 121 | |
| 122 | if (X.isSigned() != Y.isSigned()) { |
| 123 | |
| 124 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 125 | return false; |
| 126 | |
| 127 | Y.setIsSigned(true); |
| 128 | X.setIsSigned(true); |
| 129 | } |
| 130 | |
| 131 | return X == Y; |
| 132 | } |
| 133 | |
| 134 | static Sema::TemplateDeductionResult |
| 135 | DeduceTemplateArguments(Sema &S, |
| 136 | TemplateParameterList *TemplateParams, |
| 137 | const TemplateArgument &Param, |
| 138 | TemplateArgument Arg, |
| 139 | TemplateDeductionInfo &Info, |
| 140 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
| 141 | |
| 142 | static Sema::TemplateDeductionResult |
| 143 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 144 | TemplateParameterList *TemplateParams, |
| 145 | QualType Param, |
| 146 | QualType Arg, |
| 147 | TemplateDeductionInfo &Info, |
| 148 | SmallVectorImpl<DeducedTemplateArgument> & |
| 149 | Deduced, |
| 150 | unsigned TDF, |
| 151 | bool PartialOrdering = false, |
| 152 | bool DeducedFromArrayBound = false); |
| 153 | |
| 154 | static Sema::TemplateDeductionResult |
| 155 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
| 156 | ArrayRef<TemplateArgument> Params, |
| 157 | ArrayRef<TemplateArgument> Args, |
| 158 | TemplateDeductionInfo &Info, |
| 159 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 160 | bool NumberOfArgumentsMustMatch); |
| 161 | |
| 162 | static void MarkUsedTemplateParameters(ASTContext &Ctx, |
| 163 | const TemplateArgument &TemplateArg, |
| 164 | bool OnlyDeduced, unsigned Depth, |
| 165 | llvm::SmallBitVector &Used); |
| 166 | |
| 167 | static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| 168 | bool OnlyDeduced, unsigned Level, |
| 169 | llvm::SmallBitVector &Deduced); |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | static const NonTypeTemplateParmDecl * |
| 175 | getDeducedParameterFromExpr(const Expr *E, unsigned Depth) { |
| 176 | |
| 177 | |
| 178 | while (true) { |
| 179 | if (const auto *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 180 | E = IC->getSubExpr(); |
| 181 | else if (const auto *CE = dyn_cast<ConstantExpr>(E)) |
| 182 | E = CE->getSubExpr(); |
| 183 | else if (const auto *Subst = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 184 | E = Subst->getReplacement(); |
| 185 | else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) { |
| 186 | |
| 187 | if (CCE->getParenOrBraceRange().isValid()) |
| 188 | break; |
| 189 | |
| 190 | assert(CCE->getNumArgs() >= 1 && "implicit construct expr should have 1 arg"); |
| 191 | E = CCE->getArg(0); |
| 192 | } else |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 197 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 198 | if (NTTP->getDepth() == Depth) |
| 199 | return NTTP; |
| 200 | |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
| 204 | static const NonTypeTemplateParmDecl * |
| 205 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
| 206 | return getDeducedParameterFromExpr(E, Info.getDeducedDepth()); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
| 212 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 213 | X = NX->getUnderlyingDecl(); |
| 214 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 215 | Y = NY->getUnderlyingDecl(); |
| 216 | |
| 217 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | static DeducedTemplateArgument |
| 225 | checkDeducedTemplateArguments(ASTContext &Context, |
| 226 | const DeducedTemplateArgument &X, |
| 227 | const DeducedTemplateArgument &Y) { |
| 228 | |
| 229 | if (X.isNull()) |
| 230 | return Y; |
| 231 | if (Y.isNull()) |
| 232 | return X; |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 240 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 241 | if (!XType.isNull()) { |
| 242 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 243 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 244 | return DeducedTemplateArgument(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | switch (X.getKind()) { |
| 249 | case TemplateArgument::Null: |
| 250 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 251 | |
| 252 | case TemplateArgument::Type: |
| 253 | |
| 254 | if (Y.getKind() == TemplateArgument::Type && |
| 255 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 256 | return X; |
| 257 | |
| 258 | |
| 259 | |
| 260 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 261 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 262 | |
| 263 | |
| 264 | return DeducedTemplateArgument(); |
| 265 | |
| 266 | case TemplateArgument::Integral: |
| 267 | |
| 268 | |
| 269 | |
| 270 | if (Y.getKind() == TemplateArgument::Expression || |
| 271 | Y.getKind() == TemplateArgument::Declaration || |
| 272 | (Y.getKind() == TemplateArgument::Integral && |
| 273 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
| 274 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 275 | |
| 276 | |
| 277 | return DeducedTemplateArgument(); |
| 278 | |
| 279 | case TemplateArgument::Template: |
| 280 | if (Y.getKind() == TemplateArgument::Template && |
| 281 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 282 | return X; |
| 283 | |
| 284 | |
| 285 | return DeducedTemplateArgument(); |
| 286 | |
| 287 | case TemplateArgument::TemplateExpansion: |
| 288 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
| 289 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
| 290 | Y.getAsTemplateOrTemplatePattern())) |
| 291 | return X; |
| 292 | |
| 293 | |
| 294 | return DeducedTemplateArgument(); |
| 295 | |
| 296 | case TemplateArgument::Expression: { |
| 297 | if (Y.getKind() != TemplateArgument::Expression) |
| 298 | return checkDeducedTemplateArguments(Context, Y, X); |
| 299 | |
| 300 | |
| 301 | llvm::FoldingSetNodeID ID1, ID2; |
| 302 | X.getAsExpr()->Profile(ID1, Context, true); |
| 303 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 304 | if (ID1 == ID2) |
| 305 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 306 | |
| 307 | |
| 308 | return DeducedTemplateArgument(); |
| 309 | } |
| 310 | |
| 311 | case TemplateArgument::Declaration: |
| 312 | assert(!X.wasDeducedFromArrayBound()); |
| 313 | |
| 314 | |
| 315 | |
| 316 | if (Y.getKind() == TemplateArgument::Expression) |
| 317 | return X; |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | if (Y.getKind() == TemplateArgument::Integral) { |
| 323 | if (Y.wasDeducedFromArrayBound()) |
| 324 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 325 | X.getParamTypeForDecl()); |
| 326 | return Y; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | if (Y.getKind() == TemplateArgument::Declaration && |
| 332 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
| 333 | return X; |
| 334 | |
| 335 | |
| 336 | return DeducedTemplateArgument(); |
| 337 | |
| 338 | case TemplateArgument::NullPtr: |
| 339 | |
| 340 | |
| 341 | if (Y.getKind() == TemplateArgument::Expression) |
| 342 | return X; |
| 343 | |
| 344 | |
| 345 | |
| 346 | if (Y.getKind() == TemplateArgument::Integral) |
| 347 | return Y; |
| 348 | |
| 349 | |
| 350 | if (Y.getKind() == TemplateArgument::NullPtr) |
| 351 | return X; |
| 352 | |
| 353 | |
| 354 | return DeducedTemplateArgument(); |
| 355 | |
| 356 | case TemplateArgument::Pack: { |
| 357 | if (Y.getKind() != TemplateArgument::Pack || |
| 358 | X.pack_size() != Y.pack_size()) |
| 359 | return DeducedTemplateArgument(); |
| 360 | |
| 361 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
| 362 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
| 363 | XAEnd = X.pack_end(), |
| 364 | YA = Y.pack_begin(); |
| 365 | XA != XAEnd; ++XA, ++YA) { |
| 366 | TemplateArgument Merged = checkDeducedTemplateArguments( |
| 367 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
| 368 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
| 369 | if (Merged.isNull() && !(XA->isNull() && YA->isNull())) |
| 370 | return DeducedTemplateArgument(); |
| 371 | NewPack.push_back(Merged); |
| 372 | } |
| 373 | |
| 374 | return DeducedTemplateArgument( |
| 375 | TemplateArgument::CreatePackCopy(Context, NewPack), |
| 376 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 387 | Sema &S, TemplateParameterList *TemplateParams, |
| 388 | const NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
| 389 | QualType ValueType, TemplateDeductionInfo &Info, |
| 390 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 391 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 392 | "deducing non-type template argument with wrong depth"); |
| 393 | |
| 394 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 395 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
| 396 | if (Result.isNull()) { |
| 397 | Info.Param = const_cast<NonTypeTemplateParmDecl*>(NTTP); |
| 398 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 399 | Info.SecondArg = NewDeduced; |
| 400 | return Sema::TDK_Inconsistent; |
| 401 | } |
| 402 | |
| 403 | Deduced[NTTP->getIndex()] = Result; |
| 404 | if (!S.getLangOpts().CPlusPlus17) |
| 405 | return Sema::TDK_Success; |
| 406 | |
| 407 | if (NTTP->isExpandedParameterPack()) |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | return Sema::TDK_Success; |
| 413 | |
| 414 | |
| 415 | |
| 416 | QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); |
| 417 | if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) |
| 418 | ParamType = Expansion->getPattern(); |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | ValueType = ValueType.getNonReferenceType(); |
| 425 | if (ParamType->isReferenceType()) |
| 426 | ParamType = ParamType.getNonReferenceType(); |
| 427 | else |
| 428 | |
| 429 | ValueType = ValueType.getUnqualifiedType(); |
| 430 | |
| 431 | return DeduceTemplateArgumentsByTypeMatch( |
| 432 | S, TemplateParams, ParamType, ValueType, Info, Deduced, |
| 433 | TDF_SkipNonDependent, false, |
| 434 | NewDeduced.wasDeducedFromArrayBound()); |
| 435 | } |
| 436 | |
| 437 | |
| 438 | |
| 439 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 440 | Sema &S, TemplateParameterList *TemplateParams, |
| 441 | const NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 442 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 443 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 444 | return DeduceNonTypeTemplateArgument( |
| 445 | S, TemplateParams, NTTP, |
| 446 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 447 | DeducedFromArrayBound), |
| 448 | ValueType, Info, Deduced); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | |
| 453 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
| 454 | Sema &S, TemplateParameterList *TemplateParams, |
| 455 | const NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
| 456 | TemplateDeductionInfo &Info, |
| 457 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 458 | Expr *Value = S.ImpCastExprToType( |
| 459 | new (S.Context) CXXNullPtrLiteralExpr(S.Context.NullPtrTy, |
| 460 | NTTP->getLocation()), |
| 461 | NullPtrType, |
| 462 | NullPtrType->isMemberPointerType() ? CK_NullToMemberPointer |
| 463 | : CK_NullToPointer) |
| 464 | .get(); |
| 465 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 466 | DeducedTemplateArgument(Value), |
| 467 | Value->getType(), Info, Deduced); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 475 | Sema &S, TemplateParameterList *TemplateParams, |
| 476 | const NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 477 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 478 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 479 | DeducedTemplateArgument(Value), |
| 480 | Value->getType(), Info, Deduced); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 488 | Sema &S, TemplateParameterList *TemplateParams, |
| 489 | const NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 490 | TemplateDeductionInfo &Info, |
| 491 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 492 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
| 493 | TemplateArgument New(D, T); |
| 494 | return DeduceNonTypeTemplateArgument( |
| 495 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
| 496 | } |
| 497 | |
| 498 | static Sema::TemplateDeductionResult |
| 499 | DeduceTemplateArguments(Sema &S, |
| 500 | TemplateParameterList *TemplateParams, |
| 501 | TemplateName Param, |
| 502 | TemplateName Arg, |
| 503 | TemplateDeductionInfo &Info, |
| 504 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 505 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
| 506 | if (!ParamDecl) { |
| 507 | |
| 508 | |
| 509 | return Sema::TDK_Success; |
| 510 | } |
| 511 | |
| 512 | if (TemplateTemplateParmDecl *TempParam |
| 513 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
| 514 | |
| 515 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 516 | return Sema::TDK_Success; |
| 517 | |
| 518 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
| 519 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 520 | Deduced[TempParam->getIndex()], |
| 521 | NewDeduced); |
| 522 | if (Result.isNull()) { |
| 523 | Info.Param = TempParam; |
| 524 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 525 | Info.SecondArg = NewDeduced; |
| 526 | return Sema::TDK_Inconsistent; |
| 527 | } |
| 528 | |
| 529 | Deduced[TempParam->getIndex()] = Result; |
| 530 | return Sema::TDK_Success; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | if (S.Context.hasSameTemplateName(Param, Arg)) |
| 535 | return Sema::TDK_Success; |
| 536 | |
| 537 | |
| 538 | Info.FirstArg = TemplateArgument(Param); |
| 539 | Info.SecondArg = TemplateArgument(Arg); |
| 540 | return Sema::TDK_NonDeducedMismatch; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | static Sema::TemplateDeductionResult |
| 562 | DeduceTemplateArguments(Sema &S, |
| 563 | TemplateParameterList *TemplateParams, |
| 564 | const TemplateSpecializationType *Param, |
| 565 | QualType Arg, |
| 566 | TemplateDeductionInfo &Info, |
| 567 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 568 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
| 569 | |
| 570 | |
| 571 | if (auto *Injected = dyn_cast<InjectedClassNameType>(Arg)) |
| 572 | Arg = Injected->getInjectedSpecializationType(); |
| 573 | |
| 574 | |
| 575 | if (const TemplateSpecializationType *SpecArg |
| 576 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 577 | |
| 578 | if (Sema::TemplateDeductionResult Result |
| 579 | = DeduceTemplateArguments(S, TemplateParams, |
| 580 | Param->getTemplateName(), |
| 581 | SpecArg->getTemplateName(), |
| 582 | Info, Deduced)) |
| 583 | return Result; |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | return DeduceTemplateArguments(S, TemplateParams, |
| 590 | Param->template_arguments(), |
| 591 | SpecArg->template_arguments(), Info, Deduced, |
| 592 | false); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| 599 | if (!RecordArg) { |
| 600 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 601 | Info.SecondArg = TemplateArgument(Arg); |
| 602 | return Sema::TDK_NonDeducedMismatch; |
| 603 | } |
| 604 | |
| 605 | ClassTemplateSpecializationDecl *SpecArg |
| 606 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| 607 | if (!SpecArg) { |
| 608 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 609 | Info.SecondArg = TemplateArgument(Arg); |
| 610 | return Sema::TDK_NonDeducedMismatch; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | if (Sema::TemplateDeductionResult Result |
| 615 | = DeduceTemplateArguments(S, |
| 616 | TemplateParams, |
| 617 | Param->getTemplateName(), |
| 618 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 619 | Info, Deduced)) |
| 620 | return Result; |
| 621 | |
| 622 | |
| 623 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 624 | SpecArg->getTemplateArgs().asArray(), Info, |
| 625 | Deduced, true); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | |
| 630 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 631 | switch (T->getTypeClass()) { |
| 632 | case Type::TypeOfExpr: |
| 633 | case Type::TypeOf: |
| 634 | case Type::DependentName: |
| 635 | case Type::Decltype: |
| 636 | case Type::UnresolvedUsing: |
| 637 | case Type::TemplateTypeParm: |
| 638 | return true; |
| 639 | |
| 640 | case Type::ConstantArray: |
| 641 | case Type::IncompleteArray: |
| 642 | case Type::VariableArray: |
| 643 | case Type::DependentSizedArray: |
| 644 | return IsPossiblyOpaquelyQualifiedType( |
| 645 | cast<ArrayType>(T)->getElementType()); |
| 646 | |
| 647 | default: |
| 648 | return false; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | |
| 653 | |
| 654 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 655 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 656 | return TemplateParameter(TTP); |
| 657 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 658 | return TemplateParameter(NTTP); |
| 659 | |
| 660 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 661 | } |
| 662 | |
| 663 | |
| 664 | struct clang::DeducedPack { |
| 665 | |
| 666 | unsigned Index; |
| 667 | |
| 668 | |
| 669 | DeducedTemplateArgument Saved; |
| 670 | |
| 671 | |
| 672 | |
| 673 | DeducedTemplateArgument DeferredDeduction; |
| 674 | |
| 675 | |
| 676 | SmallVector<DeducedTemplateArgument, 4> New; |
| 677 | |
| 678 | |
| 679 | DeducedPack *Outer = nullptr; |
| 680 | |
| 681 | DeducedPack(unsigned Index) : Index(Index) {} |
| 682 | }; |
| 683 | |
| 684 | namespace { |
| 685 | |
| 686 | |
| 687 | class PackDeductionScope { |
| 688 | public: |
| 689 | |
| 690 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 691 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 692 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 693 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 694 | unsigned NumNamedPacks = addPacks(Pattern); |
| 695 | finishConstruction(NumNamedPacks); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 700 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 701 | TemplateDeductionInfo &Info, unsigned Index) |
| 702 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 703 | addPack(Index); |
| 704 | finishConstruction(1); |
| 705 | } |
| 706 | |
| 707 | private: |
| 708 | void addPack(unsigned Index) { |
| 709 | |
| 710 | |
| 711 | DeducedPack Pack(Index); |
| 712 | Pack.Saved = Deduced[Index]; |
| 713 | Deduced[Index] = TemplateArgument(); |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | if (Optional<unsigned> ExpandedPackExpansions = |
| 719 | getExpandedPackSize(TemplateParams->getParam(Index))) |
| 720 | FixedNumExpansions = ExpandedPackExpansions; |
| 721 | |
| 722 | Packs.push_back(Pack); |
| 723 | } |
| 724 | |
| 725 | unsigned addPacks(TemplateArgument Pattern) { |
| 726 | |
| 727 | |
| 728 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 729 | llvm::SmallVector<TemplateArgument, 4> ExtraDeductions; |
| 730 | |
| 731 | auto AddPack = [&](unsigned Index) { |
| 732 | if (SawIndices[Index]) |
| 733 | return; |
| 734 | SawIndices[Index] = true; |
| 735 | addPack(Index); |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>( |
| 742 | TemplateParams->getParam(Index))) { |
| 743 | if (!NTTP->isExpandedParameterPack()) |
| 744 | if (auto *Expansion = dyn_cast<PackExpansionType>(NTTP->getType())) |
| 745 | ExtraDeductions.push_back(Expansion->getPattern()); |
| 746 | } |
| 747 | |
| 748 | |
| 749 | }; |
| 750 | |
| 751 | auto Collect = [&](TemplateArgument Pattern) { |
| 752 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 753 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 754 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 755 | unsigned Depth, Index; |
| 756 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 757 | if (Depth == Info.getDeducedDepth()) |
| 758 | AddPack(Index); |
| 759 | } |
| 760 | }; |
| 761 | |
| 762 | |
| 763 | Collect(Pattern); |
| 764 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 765 | |
| 766 | unsigned NumNamedPacks = Packs.size(); |
| 767 | |
| 768 | |
| 769 | |
| 770 | while (!ExtraDeductions.empty()) |
| 771 | Collect(ExtraDeductions.pop_back_val()); |
| 772 | |
| 773 | return NumNamedPacks; |
| 774 | } |
| 775 | |
| 776 | void finishConstruction(unsigned NumNamedPacks) { |
| 777 | |
| 778 | const TemplateArgument *PartialPackArgs = nullptr; |
| 779 | unsigned NumPartialPackArgs = 0; |
| 780 | std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); |
| 781 | if (auto *Scope = S.CurrentInstantiationScope) |
| 782 | if (auto *Partial = Scope->getPartiallySubstitutedPack( |
| 783 | &PartialPackArgs, &NumPartialPackArgs)) |
| 784 | PartialPackDepthIndex = getDepthAndIndex(Partial); |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | bool IsExpanded = true; |
| 790 | for (unsigned I = 0; I != NumNamedPacks; ++I) { |
| 791 | if (Packs[I].Index >= Info.getNumExplicitArgs()) { |
| 792 | IsExpanded = false; |
| 793 | IsPartiallyExpanded = false; |
| 794 | break; |
| 795 | } |
| 796 | if (PartialPackDepthIndex == |
| 797 | std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { |
| 798 | IsPartiallyExpanded = true; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | |
| 803 | |
| 804 | if (IsPartiallyExpanded) |
| 805 | PackElements += NumPartialPackArgs; |
| 806 | else if (IsExpanded) |
| 807 | PackElements += *FixedNumExpansions; |
| 808 | |
| 809 | for (auto &Pack : Packs) { |
| 810 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 811 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 812 | else |
| 813 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 814 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 815 | |
| 816 | if (PartialPackDepthIndex == |
| 817 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) { |
| 818 | Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | if (!IsPartiallyExpanded) |
| 828 | Deduced[Pack.Index] = Pack.New[PackElements]; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | public: |
| 834 | ~PackDeductionScope() { |
| 835 | for (auto &Pack : Packs) |
| 836 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | |
| 841 | bool isPartiallyExpanded() { return IsPartiallyExpanded; } |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | bool hasFixedArity() { return FixedNumExpansions.hasValue(); } |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | bool hasNextElement() { |
| 852 | return !FixedNumExpansions || *FixedNumExpansions > PackElements; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | void nextPackElement() { |
| 857 | |
| 858 | |
| 859 | |
| 860 | for (auto &Pack : Packs) { |
| 861 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 862 | if (!Pack.New.empty() || !DeducedArg.isNull()) { |
| 863 | while (Pack.New.size() < PackElements) |
| 864 | Pack.New.push_back(DeducedTemplateArgument()); |
| 865 | if (Pack.New.size() == PackElements) |
| 866 | Pack.New.push_back(DeducedArg); |
| 867 | else |
| 868 | Pack.New[PackElements] = DeducedArg; |
| 869 | DeducedArg = Pack.New.size() > PackElements + 1 |
| 870 | ? Pack.New[PackElements + 1] |
| 871 | : DeducedTemplateArgument(); |
| 872 | } |
| 873 | } |
| 874 | ++PackElements; |
| 875 | } |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | Sema::TemplateDeductionResult finish() { |
| 881 | |
| 882 | |
| 883 | for (auto &Pack : Packs) { |
| 884 | |
| 885 | Deduced[Pack.Index] = Pack.Saved; |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | Pack.New.resize(PackElements); |
| 901 | |
| 902 | |
| 903 | DeducedTemplateArgument NewPack; |
| 904 | if (Pack.New.empty()) { |
| 905 | |
| 906 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 907 | } else { |
| 908 | TemplateArgument *ArgumentPack = |
| 909 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 910 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 911 | NewPack = DeducedTemplateArgument( |
| 912 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | Pack.New[0].wasDeducedFromArrayBound()); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | DeducedTemplateArgument *Loc; |
| 923 | if (Pack.Outer) { |
| 924 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 925 | |
| 926 | |
| 927 | Pack.Outer->DeferredDeduction = NewPack; |
| 928 | continue; |
| 929 | } |
| 930 | Loc = &Pack.Outer->DeferredDeduction; |
| 931 | } else { |
| 932 | Loc = &Deduced[Pack.Index]; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | DeducedTemplateArgument OldPack = *Loc; |
| 937 | DeducedTemplateArgument Result = |
| 938 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 939 | |
| 940 | |
| 941 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 942 | OldPack = Result; |
| 943 | NewPack = Pack.DeferredDeduction; |
| 944 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 945 | } |
| 946 | |
| 947 | NamedDecl *Param = TemplateParams->getParam(Pack.Index); |
| 948 | if (Result.isNull()) { |
| 949 | Info.Param = makeTemplateParameter(Param); |
| 950 | Info.FirstArg = OldPack; |
| 951 | Info.SecondArg = NewPack; |
| 952 | return Sema::TDK_Inconsistent; |
| 953 | } |
| 954 | |
| 955 | |
| 956 | |
| 957 | if (Optional<unsigned> Expansions = getExpandedPackSize(Param)) { |
| 958 | if (*Expansions != PackElements) { |
| 959 | Info.Param = makeTemplateParameter(Param); |
| 960 | Info.FirstArg = Result; |
| 961 | return Sema::TDK_IncompletePack; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | *Loc = Result; |
| 966 | } |
| 967 | |
| 968 | return Sema::TDK_Success; |
| 969 | } |
| 970 | |
| 971 | private: |
| 972 | Sema &S; |
| 973 | TemplateParameterList *TemplateParams; |
| 974 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 975 | TemplateDeductionInfo &Info; |
| 976 | unsigned PackElements = 0; |
| 977 | bool IsPartiallyExpanded = false; |
| 978 | |
| 979 | Optional<unsigned> FixedNumExpansions; |
| 980 | |
| 981 | SmallVector<DeducedPack, 2> Packs; |
| 982 | }; |
| 983 | |
| 984 | } |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | static Sema::TemplateDeductionResult |
| 1017 | DeduceTemplateArguments(Sema &S, |
| 1018 | TemplateParameterList *TemplateParams, |
| 1019 | const QualType *Params, unsigned NumParams, |
| 1020 | const QualType *Args, unsigned NumArgs, |
| 1021 | TemplateDeductionInfo &Info, |
| 1022 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1023 | unsigned TDF, |
| 1024 | bool PartialOrdering = false) { |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 1031 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 1032 | |
| 1033 | const PackExpansionType *Expansion |
| 1034 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 1035 | if (!Expansion) { |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | if (ArgIdx >= NumArgs) |
| 1040 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1041 | |
| 1042 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1048 | } |
| 1049 | |
| 1050 | if (Sema::TemplateDeductionResult Result |
| 1051 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1052 | Params[ParamIdx], Args[ArgIdx], |
| 1053 | Info, Deduced, TDF, |
| 1054 | PartialOrdering)) |
| 1055 | return Result; |
| 1056 | |
| 1057 | ++ArgIdx; |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | QualType Pattern = Expansion->getPattern(); |
| 1069 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()) { |
| 1074 | for (; ArgIdx < NumArgs && PackScope.hasNextElement(); ++ArgIdx) { |
| 1075 | |
| 1076 | if (Sema::TemplateDeductionResult Result |
| 1077 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 1078 | Args[ArgIdx], Info, Deduced, |
| 1079 | TDF, PartialOrdering)) |
| 1080 | return Result; |
| 1081 | |
| 1082 | PackScope.nextPackElement(); |
| 1083 | } |
| 1084 | } else { |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | Optional<unsigned> NumExpansions = Expansion->getNumExpansions(); |
| 1103 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
| 1104 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; |
| 1105 | ++I, ++ArgIdx) |
| 1106 | PackScope.nextPackElement(); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | if (auto Result = PackScope.finish()) |
| 1113 | return Result; |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | if (ArgIdx < NumArgs) |
| 1118 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1119 | |
| 1120 | return Sema::TDK_Success; |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 1128 | QualType ArgType) { |
| 1129 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 1130 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 1131 | |
| 1132 | if (ParamQs == ArgQs) |
| 1133 | return false; |
| 1134 | |
| 1135 | |
| 1136 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
| 1137 | ParamQs.hasObjCGCAttr()) |
| 1138 | return true; |
| 1139 | |
| 1140 | |
| 1141 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 1142 | ParamQs.hasAddressSpace()) |
| 1143 | return true; |
| 1144 | |
| 1145 | |
| 1146 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 1147 | ParamQs.hasObjCLifetime()) |
| 1148 | return true; |
| 1149 | |
| 1150 | |
| 1151 | return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 1162 | CanQualType Arg) { |
| 1163 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 1164 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 1165 | |
| 1166 | |
| 1167 | if (!ParamFunction || !ArgFunction) |
| 1168 | return Param == Arg; |
| 1169 | |
| 1170 | |
| 1171 | QualType AdjustedParam; |
| 1172 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
| 1173 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | return Param == Arg; |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { |
| 1185 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); |
| 1186 | if (!Guide || !Guide->isImplicit()) |
| 1187 | return 0; |
| 1188 | return Guide->getDeducedTemplate()->getTemplateParameters()->size(); |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { |
| 1198 | if (ParamRef->getPointeeType().getQualifiers()) |
| 1199 | return false; |
| 1200 | auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); |
| 1201 | return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; |
| 1202 | } |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | static Sema::TemplateDeductionResult DeduceTemplateBases( |
| 1225 | Sema &S, const RecordType *RecordT, TemplateParameterList *TemplateParams, |
| 1226 | const TemplateSpecializationType *SpecParam, TemplateDeductionInfo &Info, |
| 1227 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1248 | SmallVector<const RecordType *, 8> ToVisit; |
| 1249 | |
| 1250 | |
| 1251 | llvm::MapVector<const RecordType *, SmallVector<DeducedTemplateArgument, 8>> |
| 1252 | Matches; |
| 1253 | |
| 1254 | auto AddBases = [&Visited, &ToVisit](const RecordType *RT) { |
| 1255 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1256 | for (const auto &Base : RD->bases()) { |
| 1257 | assert(Base.getType()->isRecordType() && |
| 1258 | "Base class that isn't a record?"); |
| 1259 | const RecordType *RT = Base.getType()->getAs<RecordType>(); |
| 1260 | if (Visited.insert(RT).second) |
| 1261 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1262 | } |
| 1263 | }; |
| 1264 | |
| 1265 | |
| 1266 | AddBases(RecordT); |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | while (!ToVisit.empty()) { |
| 1271 | const RecordType *NextT = ToVisit.pop_back_val(); |
| 1272 | |
| 1273 | SmallVector<DeducedTemplateArgument, 8> DeducedCopy(Deduced.begin(), |
| 1274 | Deduced.end()); |
| 1275 | TemplateDeductionInfo BaseInfo(TemplateDeductionInfo::ForBase, Info); |
| 1276 | Sema::TemplateDeductionResult BaseResult = |
| 1277 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1278 | QualType(NextT, 0), BaseInfo, DeducedCopy); |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | if (BaseResult == Sema::TDK_Success) |
| 1283 | Matches.insert({NextT, DeducedCopy}); |
| 1284 | else |
| 1285 | AddBases(NextT); |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | if (Matches.size() > 1) { |
| 1295 | Visited.clear(); |
| 1296 | for (const auto &Match : Matches) |
| 1297 | AddBases(Match.first); |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | while (Matches.size() > 1 && !ToVisit.empty()) { |
| 1302 | const RecordType *NextT = ToVisit.pop_back_val(); |
| 1303 | Matches.erase(NextT); |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | AddBases(NextT); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | if (Matches.empty()) |
| 1312 | return Sema::TDK_Invalid; |
| 1313 | if (Matches.size() > 1) |
| 1314 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1315 | |
| 1316 | std::swap(Matches.front().second, Deduced); |
| 1317 | return Sema::TDK_Success; |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | static Sema::TemplateDeductionResult |
| 1345 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 1346 | TemplateParameterList *TemplateParams, |
| 1347 | QualType ParamIn, QualType ArgIn, |
| 1348 | TemplateDeductionInfo &Info, |
| 1349 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1350 | unsigned TDF, |
| 1351 | bool PartialOrdering, |
| 1352 | bool DeducedFromArrayBound) { |
| 1353 | |
| 1354 | |
| 1355 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1356 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | if (const PackExpansionType *ArgExpansion |
| |
| 1361 | = dyn_cast<PackExpansionType>(Arg)) |
| 1362 | Arg = ArgExpansion->getPattern(); |
| 1363 | |
| 1364 | if (PartialOrdering) { |
| 2 | | Assuming 'PartialOrdering' is false | |
|
| |
| 1365 | |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1370 | if (ParamRef) |
| 1371 | Param = ParamRef->getPointeeType(); |
| 1372 | |
| 1373 | |
| 1374 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1375 | if (ArgRef) |
| 1376 | Arg = ArgRef->getPointeeType(); |
| 1377 | |
| 1378 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1399 | Qualifiers ArgQuals = Arg.getQualifiers(); |
| 1400 | if ((ParamRef->isLValueReferenceType() && |
| 1401 | !ArgRef->isLValueReferenceType()) || |
| 1402 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1403 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1404 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1405 | ParamQuals.withoutObjCLifetime() == |
| 1406 | ArgQuals.withoutObjCLifetime())) { |
| 1407 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1408 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1409 | return Sema::TDK_NonDeducedMismatch; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | Param = Param.getUnqualifiedType(); |
| 1418 | |
| 1419 | |
| 1420 | Arg = Arg.getUnqualifiedType(); |
| 1421 | } else { |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | if (TDF & TDF_ParamWithReferenceType) { |
| 4 | | Assuming the condition is false | |
|
| |
| 1427 | Qualifiers Quals; |
| 1428 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1429 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
| 1430 | Arg.getCVRQualifiers()); |
| 1431 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1432 | } |
| 1433 | |
| 1434 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 6 | | Assuming the condition is false | |
|
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | TDF &= ~TDF_TopLevelParameterTypeList; |
| 1447 | if (isForwardingReference(Param, 0) && Arg->isLValueReferenceType()) |
| 1448 | Param = Param->getPointeeType(); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | |
| 1459 | if (const TemplateTypeParmType *TemplateTypeParm |
| |
| 1460 | = Param->getAs<TemplateTypeParmType>()) { |
| 7 | | Assuming the object is not a 'TemplateTypeParmType' | |
|
| 1461 | |
| 1462 | |
| 1463 | if (Arg->isPlaceholderType() || |
| 1464 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
| 1465 | return Sema::TDK_Success; |
| 1466 | |
| 1467 | unsigned Index = TemplateTypeParm->getIndex(); |
| 1468 | bool RecanonicalizeArg = false; |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | if (isa<ArrayType>(Arg)) { |
| 1473 | Qualifiers Quals; |
| 1474 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
| 1475 | if (Quals) { |
| 1476 | Arg = S.Context.getQualifiedType(Arg, Quals); |
| 1477 | RecanonicalizeArg = true; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1484 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
| 1485 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1486 | Info.FirstArg = TemplateArgument(Param); |
| 1487 | Info.SecondArg = TemplateArgument(Arg); |
| 1488 | return Sema::TDK_Underqualified; |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | if (Arg->isFunctionType() && Param.hasQualifiers()) { |
| 1494 | return Sema::TDK_NonDeducedMismatch; |
| 1495 | } |
| 1496 | |
| 1497 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1498 | "saw template type parameter with wrong depth"); |
| 1499 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
| 1500 | QualType DeducedType = Arg; |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1505 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1506 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1507 | if (ParamQs.hasObjCGCAttr()) |
| 1508 | DeducedQs.removeObjCGCAttr(); |
| 1509 | if (ParamQs.hasAddressSpace()) |
| 1510 | DeducedQs.removeAddressSpace(); |
| 1511 | if (ParamQs.hasObjCLifetime()) |
| 1512 | DeducedQs.removeObjCLifetime(); |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1518 | !DeducedType->isDependentType()) { |
| 1519 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1520 | Info.FirstArg = TemplateArgument(Param); |
| 1521 | Info.SecondArg = TemplateArgument(Arg); |
| 1522 | return Sema::TDK_Underqualified; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | if (S.getLangOpts().ObjCAutoRefCount && |
| 1529 | DeducedType->isObjCLifetimeType() && |
| 1530 | !DeducedQs.hasObjCLifetime()) |
| 1531 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 1532 | |
| 1533 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1534 | DeducedQs); |
| 1535 | |
| 1536 | if (RecanonicalizeArg) |
| 1537 | DeducedType = S.Context.getCanonicalType(DeducedType); |
| 1538 | |
| 1539 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
| 1540 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 1541 | Deduced[Index], |
| 1542 | NewDeduced); |
| 1543 | if (Result.isNull()) { |
| 1544 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1545 | Info.FirstArg = Deduced[Index]; |
| 1546 | Info.SecondArg = NewDeduced; |
| 1547 | return Sema::TDK_Inconsistent; |
| 1548 | } |
| 1549 | |
| 1550 | Deduced[Index] = Result; |
| 1551 | return Sema::TDK_Success; |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1556 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 9 | | Assuming 'Param' is not a 'SubstTemplateTypeParmPackType' | |
|
| |
| 1563 | return Sema::TDK_Success; |
| 1564 | |
| 1565 | |
| 1566 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1567 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
| 1568 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 11 | | Assuming the condition is false | |
|
| |
| 1569 | if (TDF & TDF_ParamWithReferenceType) { |
| 1570 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
| 1571 | return Sema::TDK_NonDeducedMismatch; |
| 1572 | } else if (TDF & TDF_ArgWithReferenceType) { |
| 1573 | |
| 1574 | |
| 1575 | |
| 1576 | if (!Arg.getQualifiers().compatiblyIncludes(Param.getQualifiers())) |
| 1577 | return Sema::TDK_NonDeducedMismatch; |
| 1578 | |
| 1579 | |
| 1580 | |
| 1581 | Qualifiers Quals; |
| 1582 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
| 1583 | Arg = S.Context.getQualifiedType(Arg, Param.getQualifiers()); |
| 1584 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
| 1585 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
| 1586 | return Sema::TDK_NonDeducedMismatch; |
| 1587 | } |
| 1588 | |
| 1589 | |
| 1590 | if (!Param->isDependentType()) { |
| 1591 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1592 | bool NonDeduced = |
| 1593 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1594 | ? !S.isSameOrCompatibleFunctionType(CanParam, CanArg) |
| 1595 | : Param != Arg; |
| 1596 | if (NonDeduced) { |
| 1597 | return Sema::TDK_NonDeducedMismatch; |
| 1598 | } |
| 1599 | } |
| 1600 | return Sema::TDK_Success; |
| 1601 | } |
| 1602 | } else if (!Param->isDependentType()) { |
| 13 | | Assuming the condition is false | |
|
| |
| 1603 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1604 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1605 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1606 | bool Success = |
| 1607 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1608 | ? S.isSameOrCompatibleFunctionType(ParamUnqualType, ArgUnqualType) |
| 1609 | : ParamUnqualType == ArgUnqualType; |
| 1610 | if (Success) |
| 1611 | return Sema::TDK_Success; |
| 1612 | } else { |
| 1613 | return Sema::TDK_Success; |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | switch (Param->getTypeClass()) { |
| 15 | | Control jumps to 'case DependentSizedMatrix:' at line 2137 | |
|
| 1618 | |
| 1619 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1620 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1621 | #define TYPE(Class, Base) |
| 1622 | #include "clang/AST/TypeNodes.inc" |
| 1623 | |
| 1624 | case Type::TemplateTypeParm: |
| 1625 | case Type::SubstTemplateTypeParmPack: |
| 1626 | llvm_unreachable("Type nodes handled above"); |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | case Type::Builtin: |
| 1631 | case Type::VariableArray: |
| 1632 | case Type::Vector: |
| 1633 | case Type::FunctionNoProto: |
| 1634 | case Type::Record: |
| 1635 | case Type::Enum: |
| 1636 | case Type::ObjCObject: |
| 1637 | case Type::ObjCInterface: |
| 1638 | case Type::ObjCObjectPointer: |
| 1639 | case Type::ExtInt: |
| 1640 | if (TDF & TDF_SkipNonDependent) |
| 1641 | return Sema::TDK_Success; |
| 1642 | |
| 1643 | if (TDF & TDF_IgnoreQualifiers) { |
| 1644 | Param = Param.getUnqualifiedType(); |
| 1645 | Arg = Arg.getUnqualifiedType(); |
| 1646 | } |
| 1647 | |
| 1648 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1649 | |
| 1650 | |
| 1651 | case Type::Complex: |
| 1652 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
| 1653 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1654 | cast<ComplexType>(Param)->getElementType(), |
| 1655 | ComplexArg->getElementType(), |
| 1656 | Info, Deduced, TDF); |
| 1657 | |
| 1658 | return Sema::TDK_NonDeducedMismatch; |
| 1659 | |
| 1660 | |
| 1661 | case Type::Atomic: |
| 1662 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
| 1663 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1664 | cast<AtomicType>(Param)->getValueType(), |
| 1665 | AtomicArg->getValueType(), |
| 1666 | Info, Deduced, TDF); |
| 1667 | |
| 1668 | return Sema::TDK_NonDeducedMismatch; |
| 1669 | |
| 1670 | |
| 1671 | case Type::Pointer: { |
| 1672 | QualType PointeeType; |
| 1673 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1674 | PointeeType = PointerArg->getPointeeType(); |
| 1675 | } else if (const ObjCObjectPointerType *PointerArg |
| 1676 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1677 | PointeeType = PointerArg->getPointeeType(); |
| 1678 | } else { |
| 1679 | return Sema::TDK_NonDeducedMismatch; |
| 1680 | } |
| 1681 | |
| 1682 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
| 1683 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1684 | cast<PointerType>(Param)->getPointeeType(), |
| 1685 | PointeeType, |
| 1686 | Info, Deduced, SubTDF); |
| 1687 | } |
| 1688 | |
| 1689 | |
| 1690 | case Type::LValueReference: { |
| 1691 | const LValueReferenceType *ReferenceArg = |
| 1692 | Arg->getAs<LValueReferenceType>(); |
| 1693 | if (!ReferenceArg) |
| 1694 | return Sema::TDK_NonDeducedMismatch; |
| 1695 | |
| 1696 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1697 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 1698 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | case Type::RValueReference: { |
| 1703 | const RValueReferenceType *ReferenceArg = |
| 1704 | Arg->getAs<RValueReferenceType>(); |
| 1705 | if (!ReferenceArg) |
| 1706 | return Sema::TDK_NonDeducedMismatch; |
| 1707 | |
| 1708 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1709 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1710 | ReferenceArg->getPointeeType(), |
| 1711 | Info, Deduced, 0); |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | case Type::IncompleteArray: { |
| 1716 | const IncompleteArrayType *IncompleteArrayArg = |
| 1717 | S.Context.getAsIncompleteArrayType(Arg); |
| 1718 | if (!IncompleteArrayArg) |
| 1719 | return Sema::TDK_NonDeducedMismatch; |
| 1720 | |
| 1721 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1722 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1723 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1724 | IncompleteArrayArg->getElementType(), |
| 1725 | Info, Deduced, SubTDF); |
| 1726 | } |
| 1727 | |
| 1728 | |
| 1729 | case Type::ConstantArray: { |
| 1730 | const ConstantArrayType *ConstantArrayArg = |
| 1731 | S.Context.getAsConstantArrayType(Arg); |
| 1732 | if (!ConstantArrayArg) |
| 1733 | return Sema::TDK_NonDeducedMismatch; |
| 1734 | |
| 1735 | const ConstantArrayType *ConstantArrayParm = |
| 1736 | S.Context.getAsConstantArrayType(Param); |
| 1737 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
| 1738 | return Sema::TDK_NonDeducedMismatch; |
| 1739 | |
| 1740 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1741 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1742 | ConstantArrayParm->getElementType(), |
| 1743 | ConstantArrayArg->getElementType(), |
| 1744 | Info, Deduced, SubTDF); |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | case Type::DependentSizedArray: { |
| 1749 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
| 1750 | if (!ArrayArg) |
| 1751 | return Sema::TDK_NonDeducedMismatch; |
| 1752 | |
| 1753 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1754 | |
| 1755 | |
| 1756 | const DependentSizedArrayType *DependentArrayParm |
| 1757 | = S.Context.getAsDependentSizedArrayType(Param); |
| 1758 | if (Sema::TemplateDeductionResult Result |
| 1759 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1760 | DependentArrayParm->getElementType(), |
| 1761 | ArrayArg->getElementType(), |
| 1762 | Info, Deduced, SubTDF)) |
| 1763 | return Result; |
| 1764 | |
| 1765 | |
| 1766 | const NonTypeTemplateParmDecl *NTTP |
| 1767 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
| 1768 | if (!NTTP) |
| 1769 | return Sema::TDK_Success; |
| 1770 | |
| 1771 | |
| 1772 | |
| 1773 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1774 | "saw non-type template parameter with wrong depth"); |
| 1775 | if (const ConstantArrayType *ConstantArrayArg |
| 1776 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1777 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
| 1778 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
| 1779 | S.Context.getSizeType(), |
| 1780 | true, |
| 1781 | Info, Deduced); |
| 1782 | } |
| 1783 | if (const DependentSizedArrayType *DependentArrayArg |
| 1784 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| 1785 | if (DependentArrayArg->getSizeExpr()) |
| 1786 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1787 | DependentArrayArg->getSizeExpr(), |
| 1788 | Info, Deduced); |
| 1789 | |
| 1790 | |
| 1791 | return Sema::TDK_NonDeducedMismatch; |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | case Type::FunctionProto: { |
| 1798 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
| 1799 | const FunctionProtoType *FunctionProtoArg = |
| 1800 | dyn_cast<FunctionProtoType>(Arg); |
| 1801 | if (!FunctionProtoArg) |
| 1802 | return Sema::TDK_NonDeducedMismatch; |
| 1803 | |
| 1804 | const FunctionProtoType *FunctionProtoParam = |
| 1805 | cast<FunctionProtoType>(Param); |
| 1806 | |
| 1807 | if (FunctionProtoParam->getMethodQuals() |
| 1808 | != FunctionProtoArg->getMethodQuals() || |
| 1809 | FunctionProtoParam->getRefQualifier() |
| 1810 | != FunctionProtoArg->getRefQualifier() || |
| 1811 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
| 1812 | return Sema::TDK_NonDeducedMismatch; |
| 1813 | |
| 1814 | |
| 1815 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
| 1816 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1817 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
| 1818 | return Result; |
| 1819 | |
| 1820 | |
| 1821 | if (auto Result = DeduceTemplateArguments( |
| 1822 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1823 | FunctionProtoParam->getNumParams(), |
| 1824 | FunctionProtoArg->param_type_begin(), |
| 1825 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF)) |
| 1826 | return Result; |
| 1827 | |
| 1828 | if (TDF & TDF_AllowCompatibleFunctionType) |
| 1829 | return Sema::TDK_Success; |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | Expr *NoexceptExpr = FunctionProtoParam->getNoexceptExpr(); |
| 1835 | if (const NonTypeTemplateParmDecl *NTTP = |
| 1836 | NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) |
| 1837 | : nullptr) { |
| 1838 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1839 | "saw non-type template parameter with wrong depth"); |
| 1840 | |
| 1841 | llvm::APSInt Noexcept(1); |
| 1842 | switch (FunctionProtoArg->canThrow()) { |
| 1843 | case CT_Cannot: |
| 1844 | Noexcept = 1; |
| 1845 | LLVM_FALLTHROUGH; |
| 1846 | |
| 1847 | case CT_Can: |
| 1848 | |
| 1849 | |
| 1850 | return DeduceNonTypeTemplateArgument( |
| 1851 | S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, |
| 1852 | true, Info, Deduced); |
| 1853 | |
| 1854 | case CT_Dependent: |
| 1855 | if (Expr *ArgNoexceptExpr = FunctionProtoArg->getNoexceptExpr()) |
| 1856 | return DeduceNonTypeTemplateArgument( |
| 1857 | S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); |
| 1858 | |
| 1859 | break; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | return Sema::TDK_Success; |
| 1868 | } |
| 1869 | |
| 1870 | case Type::InjectedClassName: |
| 1871 | |
| 1872 | |
| 1873 | Param = cast<InjectedClassNameType>(Param) |
| 1874 | ->getInjectedSpecializationType(); |
| 1875 | assert(isa<TemplateSpecializationType>(Param) && |
| 1876 | "injected class name is not a template specialization type"); |
| 1877 | LLVM_FALLTHROUGH; |
| 1878 | |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | case Type::TemplateSpecialization: { |
| 1885 | const TemplateSpecializationType *SpecParam = |
| 1886 | cast<TemplateSpecializationType>(Param); |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1891 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1892 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1893 | Deduced); |
| 1894 | |
| 1895 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1896 | Deduced.end()); |
| 1897 | |
| 1898 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1899 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
| 1900 | |
| 1901 | if (Result == Sema::TDK_Success) |
| 1902 | return Result; |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1908 | return Result; |
| 1909 | |
| 1910 | |
| 1911 | Deduced = DeducedOrig; |
| 1912 | |
| 1913 | |
| 1914 | Sema::TemplateDeductionResult BaseResult = DeduceTemplateBases( |
| 1915 | S, RecordT, TemplateParams, SpecParam, Info, Deduced); |
| 1916 | |
| 1917 | if (BaseResult != Sema::TDK_Invalid) |
| 1918 | return BaseResult; |
| 1919 | return Result; |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | |
| 1924 | |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | case Type::MemberPointer: { |
| 1932 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1933 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1934 | if (!MemPtrArg) |
| 1935 | return Sema::TDK_NonDeducedMismatch; |
| 1936 | |
| 1937 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1938 | if (ParamPointeeType->isFunctionType()) |
| 1939 | S.adjustMemberFunctionCC(ParamPointeeType, true, |
| 1940 | false, Info.getLocation()); |
| 1941 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1942 | if (ArgPointeeType->isFunctionType()) |
| 1943 | S.adjustMemberFunctionCC(ArgPointeeType, true, |
| 1944 | false, Info.getLocation()); |
| 1945 | |
| 1946 | if (Sema::TemplateDeductionResult Result |
| 1947 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1948 | ParamPointeeType, |
| 1949 | ArgPointeeType, |
| 1950 | Info, Deduced, |
| 1951 | TDF & TDF_IgnoreQualifiers)) |
| 1952 | return Result; |
| 1953 | |
| 1954 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1955 | QualType(MemPtrParam->getClass(), 0), |
| 1956 | QualType(MemPtrArg->getClass(), 0), |
| 1957 | Info, Deduced, |
| 1958 | TDF & TDF_IgnoreQualifiers); |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | |
| 1966 | case Type::BlockPointer: { |
| 1967 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1968 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
| 1969 | |
| 1970 | if (!BlockPtrArg) |
| 1971 | return Sema::TDK_NonDeducedMismatch; |
| 1972 | |
| 1973 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1974 | BlockPtrParam->getPointeeType(), |
| 1975 | BlockPtrArg->getPointeeType(), |
| 1976 | Info, Deduced, 0); |
| 1977 | } |
| 1978 | |
| 1979 | |
| 1980 | |
| 1981 | |
| 1982 | case Type::ExtVector: { |
| 1983 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1984 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1985 | |
| 1986 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1987 | return Sema::TDK_NonDeducedMismatch; |
| 1988 | |
| 1989 | |
| 1990 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1991 | VectorParam->getElementType(), |
| 1992 | VectorArg->getElementType(), |
| 1993 | Info, Deduced, TDF); |
| 1994 | } |
| 1995 | |
| 1996 | if (const DependentSizedExtVectorType *VectorArg |
| 1997 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1998 | |
| 1999 | |
| 2000 | |
| 2001 | |
| 2002 | |
| 2003 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2004 | VectorParam->getElementType(), |
| 2005 | VectorArg->getElementType(), |
| 2006 | Info, Deduced, TDF); |
| 2007 | } |
| 2008 | |
| 2009 | return Sema::TDK_NonDeducedMismatch; |
| 2010 | } |
| 2011 | |
| 2012 | case Type::DependentVector: { |
| 2013 | const auto *VectorParam = cast<DependentVectorType>(Param); |
| 2014 | |
| 2015 | if (const auto *VectorArg = dyn_cast<VectorType>(Arg)) { |
| 2016 | |
| 2017 | if (Sema::TemplateDeductionResult Result = |
| 2018 | DeduceTemplateArgumentsByTypeMatch( |
| 2019 | S, TemplateParams, VectorParam->getElementType(), |
| 2020 | VectorArg->getElementType(), Info, Deduced, TDF)) |
| 2021 | return Result; |
| 2022 | |
| 2023 | |
| 2024 | const NonTypeTemplateParmDecl *NTTP = |
| 2025 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
| 2026 | if (!NTTP) |
| 2027 | return Sema::TDK_Success; |
| 2028 | |
| 2029 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 2030 | ArgSize = VectorArg->getNumElements(); |
| 2031 | |
| 2032 | |
| 2033 | |
| 2034 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
| 2035 | S.Context.UnsignedIntTy, true, |
| 2036 | Info, Deduced); |
| 2037 | } |
| 2038 | |
| 2039 | if (const auto *VectorArg = dyn_cast<DependentVectorType>(Arg)) { |
| 2040 | |
| 2041 | if (Sema::TemplateDeductionResult Result = |
| 2042 | DeduceTemplateArgumentsByTypeMatch( |
| 2043 | S, TemplateParams, VectorParam->getElementType(), |
| 2044 | VectorArg->getElementType(), Info, Deduced, TDF)) |
| 2045 | return Result; |
| 2046 | |
| 2047 | |
| 2048 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 2049 | Info, VectorParam->getSizeExpr()); |
| 2050 | if (!NTTP) |
| 2051 | return Sema::TDK_Success; |
| 2052 | |
| 2053 | return DeduceNonTypeTemplateArgument( |
| 2054 | S, TemplateParams, NTTP, VectorArg->getSizeExpr(), Info, Deduced); |
| 2055 | } |
| 2056 | |
| 2057 | return Sema::TDK_NonDeducedMismatch; |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | |
| 2063 | case Type::DependentSizedExtVector: { |
| 2064 | const DependentSizedExtVectorType *VectorParam |
| 2065 | = cast<DependentSizedExtVectorType>(Param); |
| 2066 | |
| 2067 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 2068 | |
| 2069 | if (Sema::TemplateDeductionResult Result |
| 2070 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2071 | VectorParam->getElementType(), |
| 2072 | VectorArg->getElementType(), |
| 2073 | Info, Deduced, TDF)) |
| 2074 | return Result; |
| 2075 | |
| 2076 | |
| 2077 | const NonTypeTemplateParmDecl *NTTP = |
| 2078 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
| 2079 | if (!NTTP) |
| 2080 | return Sema::TDK_Success; |
| 2081 | |
| 2082 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 2083 | ArgSize = VectorArg->getNumElements(); |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
| 2088 | S.Context.IntTy, true, Info, |
| 2089 | Deduced); |
| 2090 | } |
| 2091 | |
| 2092 | if (const DependentSizedExtVectorType *VectorArg |
| 2093 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 2094 | |
| 2095 | if (Sema::TemplateDeductionResult Result |
| 2096 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2097 | VectorParam->getElementType(), |
| 2098 | VectorArg->getElementType(), |
| 2099 | Info, Deduced, TDF)) |
| 2100 | return Result; |
| 2101 | |
| 2102 | |
| 2103 | const NonTypeTemplateParmDecl *NTTP = |
| 2104 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
| 2105 | if (!NTTP) |
| 2106 | return Sema::TDK_Success; |
| 2107 | |
| 2108 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2109 | VectorArg->getSizeExpr(), |
| 2110 | Info, Deduced); |
| 2111 | } |
| 2112 | |
| 2113 | return Sema::TDK_NonDeducedMismatch; |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | case Type::ConstantMatrix: { |
| 2121 | const ConstantMatrixType *MatrixArg = dyn_cast<ConstantMatrixType>(Arg); |
| 2122 | if (!MatrixArg) |
| 2123 | return Sema::TDK_NonDeducedMismatch; |
| 2124 | |
| 2125 | const ConstantMatrixType *MatrixParam = cast<ConstantMatrixType>(Param); |
| 2126 | |
| 2127 | if (MatrixParam->getNumRows() != MatrixArg->getNumRows() || |
| 2128 | MatrixParam->getNumColumns() != MatrixArg->getNumColumns()) { |
| 2129 | return Sema::TDK_NonDeducedMismatch; |
| 2130 | } |
| 2131 | |
| 2132 | return DeduceTemplateArgumentsByTypeMatch( |
| 2133 | S, TemplateParams, MatrixParam->getElementType(), |
| 2134 | MatrixArg->getElementType(), Info, Deduced, TDF); |
| 2135 | } |
| 2136 | |
| 2137 | case Type::DependentSizedMatrix: { |
| 2138 | const MatrixType *MatrixArg = dyn_cast<MatrixType>(Arg); |
| 2139 | if (!MatrixArg) |
| 16 | | Assuming 'MatrixArg' is non-null | |
|
| |
| 2140 | return Sema::TDK_NonDeducedMismatch; |
| 2141 | |
| 2142 | |
| 2143 | const DependentSizedMatrixType *MatrixParam = |
| 2144 | cast<DependentSizedMatrixType>(Param); |
| 2145 | if (Sema::TemplateDeductionResult Result = |
| |
| |
| 2146 | DeduceTemplateArgumentsByTypeMatch( |
| 2147 | S, TemplateParams, MatrixParam->getElementType(), |
| 2148 | MatrixArg->getElementType(), Info, Deduced, TDF)) |
| 2149 | return Result; |
| 2150 | |
| 2151 | |
| 2152 | auto DeduceMatrixArg = |
| 2153 | [&S, &Info, &Deduced, &TemplateParams]( |
| 2154 | Expr *ParamExpr, const MatrixType *Arg, |
| 2155 | unsigned (ConstantMatrixType::*GetArgDimension)() const, |
| 2156 | Expr *(DependentSizedMatrixType::*GetArgDimensionExpr)() const) { |
| 2157 | const auto *ArgConstMatrix = dyn_cast<ConstantMatrixType>(Arg); |
| 21 | | Assuming 'Arg' is not a 'ConstantMatrixType' | |
|
| 2158 | const auto *ArgDepMatrix = dyn_cast<DependentSizedMatrixType>(Arg); |
| 22 | | Assuming 'Arg' is not a 'DependentSizedMatrixType' | |
|
| 23 | | 'ArgDepMatrix' initialized to a null pointer value | |
|
| 2159 | if (!ParamExpr->isValueDependent()) { |
| 24 | | Assuming the condition is true | |
|
| |
| 2160 | Optional<llvm::APSInt> ParamConst = |
| 2161 | ParamExpr->getIntegerConstantExpr(S.Context); |
| 2162 | if (!ParamConst) |
| 26 | | Assuming the condition is false | |
|
| |
| 2163 | return Sema::TDK_NonDeducedMismatch; |
| 2164 | |
| 2165 | if (ArgConstMatrix) { |
| |
| 2166 | if ((ArgConstMatrix->*GetArgDimension)() == *ParamConst) |
| 2167 | return Sema::TDK_Success; |
| 2168 | return Sema::TDK_NonDeducedMismatch; |
| 2169 | } |
| 2170 | |
| 2171 | Expr *ArgExpr = (ArgDepMatrix->*GetArgDimensionExpr)(); |
| 29 | | Called C++ object pointer is null |
|
| 2172 | if (!ArgExpr->isValueDependent()) |
| 2173 | if (Optional<llvm::APSInt> ArgConst = |
| 2174 | ArgExpr->getIntegerConstantExpr(S.Context)) |
| 2175 | if (*ArgConst == *ParamConst) |
| 2176 | return Sema::TDK_Success; |
| 2177 | return Sema::TDK_NonDeducedMismatch; |
| 2178 | } |
| 2179 | |
| 2180 | const NonTypeTemplateParmDecl *NTTP = |
| 2181 | getDeducedParameterFromExpr(Info, ParamExpr); |
| 2182 | if (!NTTP) |
| 2183 | return Sema::TDK_Success; |
| 2184 | |
| 2185 | if (ArgConstMatrix) { |
| 2186 | llvm::APSInt ArgConst( |
| 2187 | S.Context.getTypeSize(S.Context.getSizeType())); |
| 2188 | ArgConst = (ArgConstMatrix->*GetArgDimension)(); |
| 2189 | return DeduceNonTypeTemplateArgument( |
| 2190 | S, TemplateParams, NTTP, ArgConst, S.Context.getSizeType(), |
| 2191 | true, Info, Deduced); |
| 2192 | } |
| 2193 | |
| 2194 | return DeduceNonTypeTemplateArgument( |
| 2195 | S, TemplateParams, NTTP, (ArgDepMatrix->*GetArgDimensionExpr)(), |
| 2196 | Info, Deduced); |
| 2197 | }; |
| 2198 | |
| 2199 | auto Result = DeduceMatrixArg(MatrixParam->getRowExpr(), MatrixArg, |
| |
| 2200 | &ConstantMatrixType::getNumRows, |
| 2201 | &DependentSizedMatrixType::getRowExpr); |
| 2202 | if (Result) |
| 2203 | return Result; |
| 2204 | |
| 2205 | return DeduceMatrixArg(MatrixParam->getColumnExpr(), MatrixArg, |
| 2206 | &ConstantMatrixType::getNumColumns, |
| 2207 | &DependentSizedMatrixType::getColumnExpr); |
| 2208 | } |
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | |
| 2213 | case Type::DependentAddressSpace: { |
| 2214 | const DependentAddressSpaceType *AddressSpaceParam = |
| 2215 | cast<DependentAddressSpaceType>(Param); |
| 2216 | |
| 2217 | if (const DependentAddressSpaceType *AddressSpaceArg = |
| 2218 | dyn_cast<DependentAddressSpaceType>(Arg)) { |
| 2219 | |
| 2220 | if (Sema::TemplateDeductionResult Result = |
| 2221 | DeduceTemplateArgumentsByTypeMatch( |
| 2222 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 2223 | AddressSpaceArg->getPointeeType(), Info, Deduced, TDF)) |
| 2224 | return Result; |
| 2225 | |
| 2226 | |
| 2227 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 2228 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 2229 | if (!NTTP) |
| 2230 | return Sema::TDK_Success; |
| 2231 | |
| 2232 | return DeduceNonTypeTemplateArgument( |
| 2233 | S, TemplateParams, NTTP, AddressSpaceArg->getAddrSpaceExpr(), Info, |
| 2234 | Deduced); |
| 2235 | } |
| 2236 | |
| 2237 | if (isTargetAddressSpace(Arg.getAddressSpace())) { |
| 2238 | llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), |
| 2239 | false); |
| 2240 | ArgAddressSpace = toTargetAddressSpace(Arg.getAddressSpace()); |
| 2241 | |
| 2242 | |
| 2243 | if (Sema::TemplateDeductionResult Result = |
| 2244 | DeduceTemplateArgumentsByTypeMatch( |
| 2245 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 2246 | S.Context.removeAddrSpaceQualType(Arg), Info, Deduced, TDF)) |
| 2247 | return Result; |
| 2248 | |
| 2249 | |
| 2250 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 2251 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 2252 | if (!NTTP) |
| 2253 | return Sema::TDK_Success; |
| 2254 | |
| 2255 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2256 | ArgAddressSpace, S.Context.IntTy, |
| 2257 | true, Info, Deduced); |
| 2258 | } |
| 2259 | |
| 2260 | return Sema::TDK_NonDeducedMismatch; |
| 2261 | } |
| 2262 | case Type::DependentExtInt: { |
| 2263 | const auto *IntParam = cast<DependentExtIntType>(Param); |
| 2264 | |
| 2265 | if (const auto *IntArg = dyn_cast<ExtIntType>(Arg)){ |
| 2266 | if (IntParam->isUnsigned() != IntArg->isUnsigned()) |
| 2267 | return Sema::TDK_NonDeducedMismatch; |
| 2268 | |
| 2269 | const NonTypeTemplateParmDecl *NTTP = |
| 2270 | getDeducedParameterFromExpr(Info, IntParam->getNumBitsExpr()); |
| 2271 | if (!NTTP) |
| 2272 | return Sema::TDK_Success; |
| 2273 | |
| 2274 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 2275 | ArgSize = IntArg->getNumBits(); |
| 2276 | |
| 2277 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
| 2278 | S.Context.IntTy, true, Info, |
| 2279 | Deduced); |
| 2280 | } |
| 2281 | |
| 2282 | if (const auto *IntArg = dyn_cast<DependentExtIntType>(Arg)) { |
| 2283 | if (IntParam->isUnsigned() != IntArg->isUnsigned()) |
| 2284 | return Sema::TDK_NonDeducedMismatch; |
| 2285 | return Sema::TDK_Success; |
| 2286 | } |
| 2287 | return Sema::TDK_NonDeducedMismatch; |
| 2288 | } |
| 2289 | |
| 2290 | case Type::TypeOfExpr: |
| 2291 | case Type::TypeOf: |
| 2292 | case Type::DependentName: |
| 2293 | case Type::UnresolvedUsing: |
| 2294 | case Type::Decltype: |
| 2295 | case Type::UnaryTransform: |
| 2296 | case Type::Auto: |
| 2297 | case Type::DeducedTemplateSpecialization: |
| 2298 | case Type::DependentTemplateSpecialization: |
| 2299 | case Type::PackExpansion: |
| 2300 | case Type::Pipe: |
| 2301 | |
| 2302 | return Sema::TDK_Success; |
| 2303 | } |
| 2304 | |
| 2305 | llvm_unreachable("Invalid Type Class!"); |
| 2306 | } |
| 2307 | |
| 2308 | static Sema::TemplateDeductionResult |
| 2309 | DeduceTemplateArguments(Sema &S, |
| 2310 | TemplateParameterList *TemplateParams, |
| 2311 | const TemplateArgument &Param, |
| 2312 | TemplateArgument Arg, |
| 2313 | TemplateDeductionInfo &Info, |
| 2314 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 2315 | |
| 2316 | |
| 2317 | |
| 2318 | if (Arg.isPackExpansion()) |
| 2319 | Arg = Arg.getPackExpansionPattern(); |
| 2320 | |
| 2321 | switch (Param.getKind()) { |
| 2322 | case TemplateArgument::Null: |
| 2323 | llvm_unreachable("Null template argument in parameter list"); |
| 2324 | |
| 2325 | case TemplateArgument::Type: |
| 2326 | if (Arg.getKind() == TemplateArgument::Type) |
| 2327 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2328 | Param.getAsType(), |
| 2329 | Arg.getAsType(), |
| 2330 | Info, Deduced, 0); |
| 2331 | Info.FirstArg = Param; |
| 2332 | Info.SecondArg = Arg; |
| 2333 | return Sema::TDK_NonDeducedMismatch; |
| 2334 | |
| 2335 | case TemplateArgument::Template: |
| 2336 | if (Arg.getKind() == TemplateArgument::Template) |
| 2337 | return DeduceTemplateArguments(S, TemplateParams, |
| 2338 | Param.getAsTemplate(), |
| 2339 | Arg.getAsTemplate(), Info, Deduced); |
| 2340 | Info.FirstArg = Param; |
| 2341 | Info.SecondArg = Arg; |
| 2342 | return Sema::TDK_NonDeducedMismatch; |
| 2343 | |
| 2344 | case TemplateArgument::TemplateExpansion: |
| 2345 | llvm_unreachable("caller should handle pack expansions"); |
| 2346 | |
| 2347 | case TemplateArgument::Declaration: |
| 2348 | if (Arg.getKind() == TemplateArgument::Declaration && |
| 2349 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
| 2350 | return Sema::TDK_Success; |
| 2351 | |
| 2352 | Info.FirstArg = Param; |
| 2353 | Info.SecondArg = Arg; |
| 2354 | return Sema::TDK_NonDeducedMismatch; |
| 2355 | |
| 2356 | case TemplateArgument::NullPtr: |
| 2357 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 2358 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
| 2359 | return Sema::TDK_Success; |
| 2360 | |
| 2361 | Info.FirstArg = Param; |
| 2362 | Info.SecondArg = Arg; |
| 2363 | return Sema::TDK_NonDeducedMismatch; |
| 2364 | |
| 2365 | case TemplateArgument::Integral: |
| 2366 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 2367 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
| 2368 | return Sema::TDK_Success; |
| 2369 | |
| 2370 | Info.FirstArg = Param; |
| 2371 | Info.SecondArg = Arg; |
| 2372 | return Sema::TDK_NonDeducedMismatch; |
| 2373 | } |
| 2374 | |
| 2375 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 2376 | Info.FirstArg = Param; |
| 2377 | Info.SecondArg = Arg; |
| 2378 | return Sema::TDK_NonDeducedMismatch; |
| 2379 | } |
| 2380 | |
| 2381 | Info.FirstArg = Param; |
| 2382 | Info.SecondArg = Arg; |
| 2383 | return Sema::TDK_NonDeducedMismatch; |
| 2384 | |
| 2385 | case TemplateArgument::Expression: |
| 2386 | if (const NonTypeTemplateParmDecl *NTTP = |
| 2387 | getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
| 2388 | if (Arg.getKind() == TemplateArgument::Integral) |
| 2389 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2390 | Arg.getAsIntegral(), |
| 2391 | Arg.getIntegralType(), |
| 2392 | false, |
| 2393 | Info, Deduced); |
| 2394 | if (Arg.getKind() == TemplateArgument::NullPtr) |
| 2395 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 2396 | Arg.getNullPtrType(), |
| 2397 | Info, Deduced); |
| 2398 | if (Arg.getKind() == TemplateArgument::Expression) |
| 2399 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2400 | Arg.getAsExpr(), Info, Deduced); |
| 2401 | if (Arg.getKind() == TemplateArgument::Declaration) |
| 2402 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2403 | Arg.getAsDecl(), |
| 2404 | Arg.getParamTypeForDecl(), |
| 2405 | Info, Deduced); |
| 2406 | |
| 2407 | Info.FirstArg = Param; |
| 2408 | Info.SecondArg = Arg; |
| 2409 | return Sema::TDK_NonDeducedMismatch; |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | return Sema::TDK_Success; |
| 2414 | |
| 2415 | case TemplateArgument::Pack: |
| 2416 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
| 2417 | } |
| 2418 | |
| 2419 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| 2420 | } |
| 2421 | |
| 2422 | |
| 2423 | |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | |
| 2428 | |
| 2429 | |
| 2430 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 2431 | unsigned &ArgIdx) { |
| 2432 | if (ArgIdx == Args.size()) |
| 2433 | return false; |
| 2434 | |
| 2435 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 2436 | if (Arg.getKind() != TemplateArgument::Pack) |
| 2437 | return true; |
| 2438 | |
| 2439 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 2440 | Args = Arg.pack_elements(); |
| 2441 | ArgIdx = 0; |
| 2442 | return ArgIdx < Args.size(); |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | |
| 2447 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 2448 | bool FoundPackExpansion = false; |
| 2449 | for (const auto &A : Args) { |
| 2450 | if (FoundPackExpansion) |
| 2451 | return true; |
| 2452 | |
| 2453 | if (A.getKind() == TemplateArgument::Pack) |
| 2454 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 2455 | |
| 2456 | |
| 2457 | |
| 2458 | if (A.isPackExpansion()) |
| 2459 | FoundPackExpansion = true; |
| 2460 | } |
| 2461 | |
| 2462 | return false; |
| 2463 | } |
| 2464 | |
| 2465 | static Sema::TemplateDeductionResult |
| 2466 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
| 2467 | ArrayRef<TemplateArgument> Params, |
| 2468 | ArrayRef<TemplateArgument> Args, |
| 2469 | TemplateDeductionInfo &Info, |
| 2470 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2471 | bool NumberOfArgumentsMustMatch) { |
| 2472 | |
| 2473 | |
| 2474 | |
| 2475 | |
| 2476 | if (hasPackExpansionBeforeEnd(Params)) |
| 2477 | return Sema::TDK_Success; |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | |
| 2482 | |
| 2483 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 2484 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
| 2485 | if (!Params[ParamIdx].isPackExpansion()) { |
| 2486 | |
| 2487 | |
| 2488 | |
| 2489 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
| 2490 | return NumberOfArgumentsMustMatch |
| 2491 | ? Sema::TDK_MiscellaneousDeductionFailure |
| 2492 | : Sema::TDK_Success; |
| 2493 | |
| 2494 | |
| 2495 | |
| 2496 | |
| 2497 | if (Args[ArgIdx].isPackExpansion()) |
| 2498 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 2499 | |
| 2500 | |
| 2501 | if (Sema::TemplateDeductionResult Result |
| 2502 | = DeduceTemplateArguments(S, TemplateParams, |
| 2503 | Params[ParamIdx], Args[ArgIdx], |
| 2504 | Info, Deduced)) |
| 2505 | return Result; |
| 2506 | |
| 2507 | |
| 2508 | ++ArgIdx; |
| 2509 | continue; |
| 2510 | } |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 | |
| 2519 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
| 2520 | |
| 2521 | |
| 2522 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
| 2523 | |
| 2524 | |
| 2525 | |
| 2526 | |
| 2527 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx) && |
| 2528 | PackScope.hasNextElement(); |
| 2529 | ++ArgIdx) { |
| 2530 | |
| 2531 | if (Sema::TemplateDeductionResult Result |
| 2532 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 2533 | Info, Deduced)) |
| 2534 | return Result; |
| 2535 | |
| 2536 | PackScope.nextPackElement(); |
| 2537 | } |
| 2538 | |
| 2539 | |
| 2540 | |
| 2541 | if (auto Result = PackScope.finish()) |
| 2542 | return Result; |
| 2543 | } |
| 2544 | |
| 2545 | return Sema::TDK_Success; |
| 2546 | } |
| 2547 | |
| 2548 | static Sema::TemplateDeductionResult |
| 2549 | DeduceTemplateArguments(Sema &S, |
| 2550 | TemplateParameterList *TemplateParams, |
| 2551 | const TemplateArgumentList &ParamList, |
| 2552 | const TemplateArgumentList &ArgList, |
| 2553 | TemplateDeductionInfo &Info, |
| 2554 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 2555 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
| 2556 | ArgList.asArray(), Info, Deduced, |
| 2557 | false); |
| 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | static bool isSameTemplateArg(ASTContext &Context, |
| 2562 | TemplateArgument X, |
| 2563 | const TemplateArgument &Y, |
| 2564 | bool PackExpansionMatchesPack = false) { |
| 2565 | |
| 2566 | |
| 2567 | if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) |
| 2568 | X = X.getPackExpansionPattern(); |
| 2569 | |
| 2570 | if (X.getKind() != Y.getKind()) |
| 2571 | return false; |
| 2572 | |
| 2573 | switch (X.getKind()) { |
| 2574 | case TemplateArgument::Null: |
| 2575 | llvm_unreachable("Comparing NULL template argument"); |
| 2576 | |
| 2577 | case TemplateArgument::Type: |
| 2578 | return Context.getCanonicalType(X.getAsType()) == |
| 2579 | Context.getCanonicalType(Y.getAsType()); |
| 2580 | |
| 2581 | case TemplateArgument::Declaration: |
| 2582 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
| 2583 | |
| 2584 | case TemplateArgument::NullPtr: |
| 2585 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
| 2586 | |
| 2587 | case TemplateArgument::Template: |
| 2588 | case TemplateArgument::TemplateExpansion: |
| 2589 | return Context.getCanonicalTemplateName( |
| 2590 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2591 | Context.getCanonicalTemplateName( |
| 2592 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
| 2593 | |
| 2594 | case TemplateArgument::Integral: |
| 2595 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
| 2596 | |
| 2597 | case TemplateArgument::Expression: { |
| 2598 | llvm::FoldingSetNodeID XID, YID; |
| 2599 | X.getAsExpr()->Profile(XID, Context, true); |
| 2600 | Y.getAsExpr()->Profile(YID, Context, true); |
| 2601 | return XID == YID; |
| 2602 | } |
| 2603 | |
| 2604 | case TemplateArgument::Pack: |
| 2605 | if (X.pack_size() != Y.pack_size()) |
| 2606 | return false; |
| 2607 | |
| 2608 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2609 | XPEnd = X.pack_end(), |
| 2610 | YP = Y.pack_begin(); |
| 2611 | XP != XPEnd; ++XP, ++YP) |
| 2612 | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
| 2613 | return false; |
| 2614 | |
| 2615 | return true; |
| 2616 | } |
| 2617 | |
| 2618 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| 2619 | } |
| 2620 | |
| 2621 | |
| 2622 | |
| 2623 | |
| 2624 | |
| 2625 | |
| 2626 | |
| 2627 | |
| 2628 | |
| 2629 | |
| 2630 | |
| 2631 | |
| 2632 | |
| 2633 | |
| 2634 | TemplateArgumentLoc |
| 2635 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2636 | QualType NTTPType, SourceLocation Loc) { |
| 2637 | switch (Arg.getKind()) { |
| 2638 | case TemplateArgument::Null: |
| 2639 | llvm_unreachable("Can't get a NULL template argument here"); |
| 2640 | |
| 2641 | case TemplateArgument::Type: |
| 2642 | return TemplateArgumentLoc( |
| 2643 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
| 2644 | |
| 2645 | case TemplateArgument::Declaration: { |
| 2646 | if (NTTPType.isNull()) |
| 2647 | NTTPType = Arg.getParamTypeForDecl(); |
| 2648 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2649 | .getAs<Expr>(); |
| 2650 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2651 | } |
| 2652 | |
| 2653 | case TemplateArgument::NullPtr: { |
| 2654 | if (NTTPType.isNull()) |
| 2655 | NTTPType = Arg.getNullPtrType(); |
| 2656 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2657 | .getAs<Expr>(); |
| 2658 | return TemplateArgumentLoc(TemplateArgument(NTTPType, true), |
| 2659 | E); |
| 2660 | } |
| 2661 | |
| 2662 | case TemplateArgument::Integral: { |
| 2663 | Expr *E = |
| 2664 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
| 2665 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2666 | } |
| 2667 | |
| 2668 | case TemplateArgument::Template: |
| 2669 | case TemplateArgument::TemplateExpansion: { |
| 2670 | NestedNameSpecifierLocBuilder Builder; |
| 2671 | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); |
| 2672 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
| 2673 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
| 2674 | else if (QualifiedTemplateName *QTN = |
| 2675 | Template.getAsQualifiedTemplateName()) |
| 2676 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
| 2677 | |
| 2678 | if (Arg.getKind() == TemplateArgument::Template) |
| 2679 | return TemplateArgumentLoc(Context, Arg, |
| 2680 | Builder.getWithLocInContext(Context), Loc); |
| 2681 | |
| 2682 | return TemplateArgumentLoc( |
| 2683 | Context, Arg, Builder.getWithLocInContext(Context), Loc, Loc); |
| 2684 | } |
| 2685 | |
| 2686 | case TemplateArgument::Expression: |
| 2687 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
| 2688 | |
| 2689 | case TemplateArgument::Pack: |
| 2690 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2691 | } |
| 2692 | |
| 2693 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| 2694 | } |
| 2695 | |
| 2696 | TemplateArgumentLoc |
| 2697 | Sema::getIdentityTemplateArgumentLoc(NamedDecl *TemplateParm, |
| 2698 | SourceLocation Location) { |
| 2699 | return getTrivialTemplateArgumentLoc( |
| 2700 | Context.getInjectedTemplateArg(TemplateParm), QualType(), Location); |
| 2701 | } |
| 2702 | |
| 2703 | |
| 2704 | |
| 2705 | static bool |
| 2706 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2707 | DeducedTemplateArgument Arg, |
| 2708 | NamedDecl *Template, |
| 2709 | TemplateDeductionInfo &Info, |
| 2710 | bool IsDeduced, |
| 2711 | SmallVectorImpl<TemplateArgument> &Output) { |
| 2712 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2713 | unsigned ArgumentPackIndex) { |
| 2714 | |
| 2715 | |
| 2716 | |
| 2717 | TemplateArgumentLoc ArgLoc = |
| 2718 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
| 2719 | |
| 2720 | |
| 2721 | return S.CheckTemplateArgument( |
| 2722 | Param, ArgLoc, Template, Template->getLocation(), |
| 2723 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
| 2724 | IsDeduced |
| 2725 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2726 | : Sema::CTAK_Deduced) |
| 2727 | : Sema::CTAK_Specified); |
| 2728 | }; |
| 2729 | |
| 2730 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2731 | |
| 2732 | |
| 2733 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
| 2734 | for (const auto &P : Arg.pack_elements()) { |
| 2735 | |
| 2736 | |
| 2737 | |
| 2738 | DeducedTemplateArgument InnerArg(P); |
| 2739 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
| 2740 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2741 | "deduced nested pack"); |
| 2742 | if (P.isNull()) { |
| 2743 | |
| 2744 | |
| 2745 | |
| 2746 | |
| 2747 | S.Diag(Param->getLocation(), |
| 2748 | diag::err_template_arg_deduced_incomplete_pack) |
| 2749 | << Arg << Param; |
| 2750 | return true; |
| 2751 | } |
| 2752 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
| 2753 | return true; |
| 2754 | |
| 2755 | |
| 2756 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
| 2757 | } |
| 2758 | |
| 2759 | |
| 2760 | |
| 2761 | if (PackedArgsBuilder.empty()) { |
| 2762 | LocalInstantiationScope Scope(S); |
| 2763 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
| 2764 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2765 | |
| 2766 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2767 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2768 | NTTP, Output, |
| 2769 | Template->getSourceRange()); |
| 2770 | if (Inst.isInvalid() || |
| 2771 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2772 | NTTP->getDeclName()).isNull()) |
| 2773 | return true; |
| 2774 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2775 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2776 | TTP, Output, |
| 2777 | Template->getSourceRange()); |
| 2778 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | Output.push_back( |
| 2786 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
| 2787 | return false; |
| 2788 | } |
| 2789 | |
| 2790 | return ConvertArg(Arg, 0); |
| 2791 | } |
| 2792 | |
| 2793 | |
| 2794 | |
| 2795 | |
| 2796 | template<typename TemplateDeclT> |
| 2797 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
| 2798 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
| 2799 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2800 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2801 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2802 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2803 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2804 | |
| 2805 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2806 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2807 | |
| 2808 | |
| 2809 | |
| 2810 | |
| 2811 | |
| 2812 | if (Deduced[I].isNull() && Param->isTemplateParameterPack()) { |
| 2813 | if (auto Result = |
| 2814 | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) |
| 2815 | return Result; |
| 2816 | } |
| 2817 | |
| 2818 | if (!Deduced[I].isNull()) { |
| 2819 | if (I < NumAlreadyConverted) { |
| 2820 | |
| 2821 | |
| 2822 | |
| 2823 | if (Param->isParameterPack() && CurrentInstantiationScope && |
| 2824 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { |
| 2825 | |
| 2826 | |
| 2827 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2828 | |
| 2829 | |
| 2830 | } else { |
| 2831 | |
| 2832 | |
| 2833 | |
| 2834 | Builder.push_back(Deduced[I]); |
| 2835 | continue; |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | |
| 2840 | |
| 2841 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
| 2842 | IsDeduced, Builder)) { |
| 2843 | Info.Param = makeTemplateParameter(Param); |
| 2844 | |
| 2845 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2846 | return Sema::TDK_SubstitutionFailure; |
| 2847 | } |
| 2848 | |
| 2849 | continue; |
| 2850 | } |
| 2851 | |
| 2852 | |
| 2853 | bool HasDefaultArg = false; |
| 2854 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2855 | if (!TD) { |
| 2856 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || |
| 2857 | isa<VarTemplatePartialSpecializationDecl>(Template)); |
| 2858 | return Sema::TDK_Incomplete; |
| 2859 | } |
| 2860 | |
| 2861 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2862 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2863 | HasDefaultArg); |
| 2864 | |
| 2865 | |
| 2866 | if (DefArg.getArgument().isNull()) { |
| 2867 | Info.Param = makeTemplateParameter( |
| 2868 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2869 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2870 | if (PartialOverloading) break; |
| 2871 | |
| 2872 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2873 | : Sema::TDK_Incomplete; |
| 2874 | } |
| 2875 | |
| 2876 | |
| 2877 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2878 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2879 | Sema::CTAK_Specified)) { |
| 2880 | Info.Param = makeTemplateParameter( |
| 2881 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2882 | |
| 2883 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2884 | return Sema::TDK_SubstitutionFailure; |
| 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | } |
| 2889 | |
| 2890 | return Sema::TDK_Success; |
| 2891 | } |
| 2892 | |
| 2893 | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
| 2894 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2895 | return DC; |
| 2896 | return D->getDeclContext(); |
| 2897 | } |
| 2898 | |
| 2899 | template<typename T> struct IsPartialSpecialization { |
| 2900 | static constexpr bool value = false; |
| 2901 | }; |
| 2902 | template<> |
| 2903 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2904 | static constexpr bool value = true; |
| 2905 | }; |
| 2906 | template<> |
| 2907 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2908 | static constexpr bool value = true; |
| 2909 | }; |
| 2910 | |
| 2911 | template<typename TemplateDeclT> |
| 2912 | static Sema::TemplateDeductionResult |
| 2913 | CheckDeducedArgumentConstraints(Sema& S, TemplateDeclT *Template, |
| 2914 | ArrayRef<TemplateArgument> DeducedArgs, |
| 2915 | TemplateDeductionInfo& Info) { |
| 2916 | llvm::SmallVector<const Expr *, 3> AssociatedConstraints; |
| 2917 | Template->getAssociatedConstraints(AssociatedConstraints); |
| 2918 | if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, |
| 2919 | DeducedArgs, Info.getLocation(), |
| 2920 | Info.AssociatedConstraintsSatisfaction) || |
| 2921 | !Info.AssociatedConstraintsSatisfaction.IsSatisfied) { |
| 2922 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs)); |
| 2923 | return Sema::TDK_ConstraintsNotSatisfied; |
| 2924 | } |
| 2925 | return Sema::TDK_Success; |
| 2926 | } |
| 2927 | |
| 2928 | |
| 2929 | template <typename T> |
| 2930 | static std::enable_if_t<IsPartialSpecialization<T>::value, |
| 2931 | Sema::TemplateDeductionResult> |
| 2932 | FinishTemplateArgumentDeduction( |
| 2933 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2934 | const TemplateArgumentList &TemplateArgs, |
| 2935 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2936 | TemplateDeductionInfo &Info) { |
| 2937 | |
| 2938 | EnterExpressionEvaluationContext Unevaluated( |
| 2939 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
| 2940 | Sema::SFINAETrap Trap(S); |
| 2941 | |
| 2942 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
| 2943 | |
| 2944 | |
| 2945 | |
| 2946 | |
| 2947 | SmallVector<TemplateArgument, 4> Builder; |
| 2948 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2949 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
| 2950 | return Result; |
| 2951 | |
| 2952 | |
| 2953 | TemplateArgumentList *DeducedArgumentList |
| 2954 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
| 2955 | |
| 2956 | Info.reset(DeducedArgumentList); |
| 2957 | |
| 2958 | |
| 2959 | |
| 2960 | |
| 2961 | |
| 2962 | |
| 2963 | LocalInstantiationScope InstScope(S); |
| 2964 | auto *Template = Partial->getSpecializedTemplate(); |
| 2965 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2966 | Partial->getTemplateArgsAsWritten(); |
| 2967 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2968 | PartialTemplArgInfo->getTemplateArgs(); |
| 2969 | |
| 2970 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2971 | PartialTemplArgInfo->RAngleLoc); |
| 2972 | |
| 2973 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
| 2974 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2975 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2976 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2977 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2978 | |
| 2979 | Decl *Param = const_cast<NamedDecl *>( |
| 2980 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2981 | Info.Param = makeTemplateParameter(Param); |
| 2982 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2983 | return Sema::TDK_SubstitutionFailure; |
| 2984 | } |
| 2985 | |
| 2986 | bool ConstraintsNotSatisfied; |
| 2987 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2988 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2989 | false, ConvertedInstArgs, |
| 2990 | true, |
| 2991 | &ConstraintsNotSatisfied)) |
| 2992 | return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied : |
| 2993 | Sema::TDK_SubstitutionFailure; |
| 2994 | |
| 2995 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2996 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2997 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2998 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2999 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 3000 | Info.FirstArg = TemplateArgs[I]; |
| 3001 | Info.SecondArg = InstArg; |
| 3002 | return Sema::TDK_NonDeducedMismatch; |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | if (Trap.hasErrorOccurred()) |
| 3007 | return Sema::TDK_SubstitutionFailure; |
| 3008 | |
| 3009 | if (auto Result = CheckDeducedArgumentConstraints(S, Partial, Builder, Info)) |
| 3010 | return Result; |
| 3011 | |
| 3012 | return Sema::TDK_Success; |
| 3013 | } |
| 3014 | |
| 3015 | |
| 3016 | |
| 3017 | |
| 3018 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 3019 | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
| 3020 | const TemplateArgumentList &TemplateArgs, |
| 3021 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3022 | TemplateDeductionInfo &Info) { |
| 3023 | |
| 3024 | EnterExpressionEvaluationContext Unevaluated( |
| 3025 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
| 3026 | Sema::SFINAETrap Trap(S); |
| 3027 | |
| 3028 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
| 3029 | |
| 3030 | |
| 3031 | |
| 3032 | |
| 3033 | SmallVector<TemplateArgument, 4> Builder; |
| 3034 | if (auto Result = ConvertDeducedTemplateArguments( |
| 3035 | S, Template, PartialOrdering, Deduced, Info, Builder)) |
| 3036 | return Result; |
| 3037 | |
| 3038 | |
| 3039 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 3040 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 3041 | TemplateArgument InstArg = Builder[I]; |
| 3042 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
| 3043 | true)) { |
| 3044 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 3045 | Info.FirstArg = TemplateArgs[I]; |
| 3046 | Info.SecondArg = InstArg; |
| 3047 | return Sema::TDK_NonDeducedMismatch; |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | if (Trap.hasErrorOccurred()) |
| 3052 | return Sema::TDK_SubstitutionFailure; |
| 3053 | |
| 3054 | if (auto Result = CheckDeducedArgumentConstraints(S, Template, Builder, |
| 3055 | Info)) |
| 3056 | return Result; |
| 3057 | |
| 3058 | return Sema::TDK_Success; |
| 3059 | } |
| 3060 | |
| 3061 | |
| 3062 | |
| 3063 | |
| 3064 | Sema::TemplateDeductionResult |
| 3065 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
| 3066 | const TemplateArgumentList &TemplateArgs, |
| 3067 | TemplateDeductionInfo &Info) { |
| 3068 | if (Partial->isInvalidDecl()) |
| 3069 | return TDK_Invalid; |
| 3070 | |
| 3071 | |
| 3072 | |
| 3073 | |
| 3074 | |
| 3075 | |
| 3076 | |
| 3077 | |
| 3078 | EnterExpressionEvaluationContext Unevaluated( |
| 3079 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| 3080 | SFINAETrap Trap(*this); |
| 3081 | |
| 3082 | |
| 3083 | |
| 3084 | LocalInstantiationScope InstantiationScope(*this); |
| 3085 | |
| 3086 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3087 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 3088 | if (TemplateDeductionResult Result |
| 3089 | = ::DeduceTemplateArguments(*this, |
| 3090 | Partial->getTemplateParameters(), |
| 3091 | Partial->getTemplateArgs(), |
| 3092 | TemplateArgs, Info, Deduced)) |
| 3093 | return Result; |
| 3094 | |
| 3095 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
| 3096 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 3097 | Info); |
| 3098 | if (Inst.isInvalid()) |
| 3099 | return TDK_InstantiationDepth; |
| 3100 | |
| 3101 | if (Trap.hasErrorOccurred()) |
| 3102 | return Sema::TDK_SubstitutionFailure; |
| 3103 | |
| 3104 | TemplateDeductionResult Result; |
| 3105 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 3106 | Result = ::FinishTemplateArgumentDeduction(*this, Partial, |
| 3107 | false, |
| 3108 | TemplateArgs, Deduced, Info); |
| 3109 | }); |
| 3110 | return Result; |
| 3111 | } |
| 3112 | |
| 3113 | |
| 3114 | |
| 3115 | |
| 3116 | Sema::TemplateDeductionResult |
| 3117 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 3118 | const TemplateArgumentList &TemplateArgs, |
| 3119 | TemplateDeductionInfo &Info) { |
| 3120 | if (Partial->isInvalidDecl()) |
| 3121 | return TDK_Invalid; |
| 3122 | |
| 3123 | |
| 3124 | |
| 3125 | |
| 3126 | |
| 3127 | |
| 3128 | |
| 3129 | |
| 3130 | EnterExpressionEvaluationContext Unevaluated( |
| 3131 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| 3132 | SFINAETrap Trap(*this); |
| 3133 | |
| 3134 | |
| 3135 | |
| 3136 | LocalInstantiationScope InstantiationScope(*this); |
| 3137 | |
| 3138 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3139 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 3140 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 3141 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 3142 | TemplateArgs, Info, Deduced)) |
| 3143 | return Result; |
| 3144 | |
| 3145 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
| 3146 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 3147 | Info); |
| 3148 | if (Inst.isInvalid()) |
| 3149 | return TDK_InstantiationDepth; |
| 3150 | |
| 3151 | if (Trap.hasErrorOccurred()) |
| 3152 | return Sema::TDK_SubstitutionFailure; |
| 3153 | |
| 3154 | TemplateDeductionResult Result; |
| 3155 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 3156 | Result = ::FinishTemplateArgumentDeduction(*this, Partial, |
| 3157 | false, |
| 3158 | TemplateArgs, Deduced, Info); |
| 3159 | }); |
| 3160 | return Result; |
| 3161 | } |
| 3162 | |
| 3163 | |
| 3164 | static bool isSimpleTemplateIdType(QualType T) { |
| 3165 | if (const TemplateSpecializationType *Spec |
| 3166 | = T->getAs<TemplateSpecializationType>()) |
| 3167 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
| 3168 | |
| 3169 | |
| 3170 | |
| 3171 | |
| 3172 | |
| 3173 | |
| 3174 | |
| 3175 | |
| 3176 | |
| 3177 | if (T->getAs<InjectedClassNameType>()) |
| 3178 | return true; |
| 3179 | |
| 3180 | return false; |
| 3181 | } |
| 3182 | |
| 3183 | |
| 3184 | |
| 3185 | |
| 3186 | |
| 3187 | |
| 3188 | |
| 3189 | |
| 3190 | |
| 3191 | |
| 3192 | |
| 3193 | |
| 3194 | |
| 3195 | |
| 3196 | |
| 3197 | |
| 3198 | |
| 3199 | |
| 3200 | |
| 3201 | |
| 3202 | |
| 3203 | |
| 3204 | |
| 3205 | |
| 3206 | |
| 3207 | Sema::TemplateDeductionResult |
| 3208 | Sema::SubstituteExplicitTemplateArguments( |
| 3209 | FunctionTemplateDecl *FunctionTemplate, |
| 3210 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 3211 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3212 | SmallVectorImpl<QualType> &ParamTypes, |
| 3213 | QualType *FunctionType, |
| 3214 | TemplateDeductionInfo &Info) { |
| 3215 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3216 | TemplateParameterList *TemplateParams |
| 3217 | = FunctionTemplate->getTemplateParameters(); |
| 3218 | |
| 3219 | if (ExplicitTemplateArgs.size() == 0) { |
| 3220 | |
| 3221 | |
| 3222 | for (auto P : Function->parameters()) |
| 3223 | ParamTypes.push_back(P->getType()); |
| 3224 | |
| 3225 | if (FunctionType) |
| 3226 | *FunctionType = Function->getType(); |
| 3227 | return TDK_Success; |
| 3228 | } |
| 3229 | |
| 3230 | |
| 3231 | EnterExpressionEvaluationContext Unevaluated( |
| 3232 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| 3233 | SFINAETrap Trap(*this); |
| 3234 | |
| 3235 | |
| 3236 | |
| 3237 | |
| 3238 | |
| 3239 | |
| 3240 | SmallVector<TemplateArgument, 4> Builder; |
| 3241 | |
| 3242 | |
| 3243 | |
| 3244 | |
| 3245 | SmallVector<TemplateArgument, 4> DeducedArgs; |
| 3246 | InstantiatingTemplate Inst( |
| 3247 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 3248 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| 3249 | if (Inst.isInvalid()) |
| 3250 | return TDK_InstantiationDepth; |
| 3251 | |
| 3252 | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
| 3253 | ExplicitTemplateArgs, true, Builder, false) || |
| 3254 | Trap.hasErrorOccurred()) { |
| 3255 | unsigned Index = Builder.size(); |
| 3256 | if (Index >= TemplateParams->size()) |
| 3257 | return TDK_SubstitutionFailure; |
| 3258 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
| 3259 | return TDK_InvalidExplicitArguments; |
| 3260 | } |
| 3261 | |
| 3262 | |
| 3263 | |
| 3264 | TemplateArgumentList *ExplicitArgumentList |
| 3265 | = TemplateArgumentList::CreateCopy(Context, Builder); |
| 3266 | Info.setExplicitArgs(ExplicitArgumentList); |
| 3267 | |
| 3268 | |
| 3269 | |
| 3270 | |
| 3271 | |
| 3272 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 3273 | |
| 3274 | |
| 3275 | |
| 3276 | |
| 3277 | |
| 3278 | unsigned PartiallySubstitutedPackIndex = -1u; |
| 3279 | if (!Builder.empty()) { |
| 3280 | const TemplateArgument &Arg = Builder.back(); |
| 3281 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 3282 | auto *Param = TemplateParams->getParam(Builder.size() - 1); |
| 3283 | |
| 3284 | |
| 3285 | Optional<unsigned> Expansions = getExpandedPackSize(Param); |
| 3286 | if (!Expansions || Arg.pack_size() < *Expansions) { |
| 3287 | PartiallySubstitutedPackIndex = Builder.size() - 1; |
| 3288 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
| 3289 | Param, Arg.pack_begin(), Arg.pack_size()); |
| 3290 | } |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | const FunctionProtoType *Proto |
| 3295 | = Function->getType()->getAs<FunctionProtoType>(); |
| 3296 | assert(Proto && "Function template does not have a prototype?"); |
| 3297 | |
| 3298 | |
| 3299 | LocalInstantiationScope InstScope(*this, true); |
| 3300 | |
| 3301 | ExtParameterInfoBuilder ExtParamInfos; |
| 3302 | |
| 3303 | |
| 3304 | |
| 3305 | |
| 3306 | |
| 3307 | if (Proto->hasTrailingReturn()) { |
| 3308 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
| 3309 | Proto->getExtParameterInfosOrNull(), |
| 3310 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 3311 | ParamTypes, nullptr, ExtParamInfos)) |
| 3312 | return TDK_SubstitutionFailure; |
| 3313 | } |
| 3314 | |
| 3315 | |
| 3316 | QualType ResultType; |
| 3317 | { |
| 3318 | |
| 3319 | |
| 3320 | |
| 3321 | |
| 3322 | |
| 3323 | |
| 3324 | Qualifiers ThisTypeQuals; |
| 3325 | CXXRecordDecl *ThisContext = nullptr; |
| 3326 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 3327 | ThisContext = Method->getParent(); |
| 3328 | ThisTypeQuals = Method->getMethodQualifiers(); |
| 3329 | } |
| 3330 | |
| 3331 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
| 3332 | getLangOpts().CPlusPlus11); |
| 3333 | |
| 3334 | ResultType = |
| 3335 | SubstType(Proto->getReturnType(), |
| 3336 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 3337 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
| 3338 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 3339 | return TDK_SubstitutionFailure; |
| 3340 | |
| 3341 | if (getLangOpts().CUDA) |
| 3342 | if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()) { |
| 3343 | Diag(Function->getLocation(), diag::err_kern_type_not_void_return) |
| 3344 | << Function->getType() << Function->getSourceRange(); |
| 3345 | return TDK_SubstitutionFailure; |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | |
| 3350 | |
| 3351 | if (!Proto->hasTrailingReturn() && |
| 3352 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
| 3353 | Proto->getExtParameterInfosOrNull(), |
| 3354 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 3355 | ParamTypes, nullptr, ExtParamInfos)) |
| 3356 | return TDK_SubstitutionFailure; |
| 3357 | |
| 3358 | if (FunctionType) { |
| 3359 | auto EPI = Proto->getExtProtoInfo(); |
| 3360 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
| 3361 | |
| 3362 | |
| 3363 | |
| 3364 | |
| 3365 | SmallVector<QualType, 4> ExceptionStorage; |
| 3366 | if (getLangOpts().CPlusPlus17 && |
| 3367 | SubstExceptionSpec( |
| 3368 | Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, |
| 3369 | MultiLevelTemplateArgumentList(*ExplicitArgumentList))) |
| 3370 | return TDK_SubstitutionFailure; |
| 3371 | |
| 3372 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
| 3373 | Function->getLocation(), |
| 3374 | Function->getDeclName(), |
| 3375 | EPI); |
| 3376 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 3377 | return TDK_SubstitutionFailure; |
| 3378 | } |
| 3379 | |
| 3380 | |
| 3381 | |
| 3382 | |
| 3383 | |
| 3384 | |
| 3385 | |
| 3386 | |
| 3387 | |
| 3388 | |
| 3389 | |
| 3390 | Deduced.reserve(TemplateParams->size()); |
| 3391 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 3392 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 3393 | if (I == PartiallySubstitutedPackIndex) |
| 3394 | Deduced.push_back(DeducedTemplateArgument()); |
| 3395 | else |
| 3396 | Deduced.push_back(Arg); |
| 3397 | } |
| 3398 | |
| 3399 | return TDK_Success; |
| 3400 | } |
| 3401 | |
| 3402 | |
| 3403 | |
| 3404 | static Sema::TemplateDeductionResult |
| 3405 | CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, |
| 3406 | Sema::OriginalCallArg OriginalArg, |
| 3407 | QualType DeducedA) { |
| 3408 | ASTContext &Context = S.Context; |
| 3409 | |
| 3410 | auto Failed = [&]() -> Sema::TemplateDeductionResult { |
| 3411 | Info.FirstArg = TemplateArgument(DeducedA); |
| 3412 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 3413 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 3414 | return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested |
| 3415 | : Sema::TDK_DeducedMismatch; |
| 3416 | }; |
| 3417 | |
| 3418 | QualType A = OriginalArg.OriginalArgType; |
| 3419 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
| 3420 | |
| 3421 | |
| 3422 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 3423 | return Sema::TDK_Success; |
| 3424 | |
| 3425 | |
| 3426 | |
| 3427 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 3428 | DeducedA = DeducedARef->getPointeeType(); |
| 3429 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 3430 | A = ARef->getPointeeType(); |
| 3431 | |
| 3432 | |
| 3433 | |
| 3434 | |
| 3435 | |
| 3436 | |
| 3437 | if (const ReferenceType *OriginalParamRef |
| 3438 | = OriginalParamType->getAs<ReferenceType>()) { |
| 3439 | |
| 3440 | OriginalParamType = OriginalParamRef->getPointeeType(); |
| 3441 | |
| 3442 | |
| 3443 | |
| 3444 | |
| 3445 | QualType Tmp; |
| 3446 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 3447 | return Sema::TDK_Success; |
| 3448 | |
| 3449 | Qualifiers AQuals = A.getQualifiers(); |
| 3450 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
| 3451 | |
| 3452 | |
| 3453 | |
| 3454 | |
| 3455 | |
| 3456 | if (S.getLangOpts().ObjCAutoRefCount && |
| 3457 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 3458 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 3459 | (DeducedAQuals.hasConst() && |
| 3460 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 3461 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
| 3462 | } |
| 3463 | |
| 3464 | if (AQuals == DeducedAQuals) { |
| 3465 | |
| 3466 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
| 3467 | return Failed(); |
| 3468 | } else { |
| 3469 | |
| 3470 | |
| 3471 | |
| 3472 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | |
| 3477 | |
| 3478 | |
| 3479 | |
| 3480 | |
| 3481 | |
| 3482 | bool ObjCLifetimeConversion = false; |
| 3483 | QualType ResultTy; |
| 3484 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
| 3485 | (S.IsQualificationConversion(A, DeducedA, false, |
| 3486 | ObjCLifetimeConversion) || |
| 3487 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
| 3488 | return Sema::TDK_Success; |
| 3489 | |
| 3490 | |
| 3491 | |
| 3492 | |
| 3493 | |
| 3494 | |
| 3495 | if (const PointerType *OriginalParamPtr |
| 3496 | = OriginalParamType->getAs<PointerType>()) { |
| 3497 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 3498 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 3499 | if (A->getPointeeType()->isRecordType()) { |
| 3500 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 3501 | DeducedA = DeducedAPtr->getPointeeType(); |
| 3502 | A = APtr->getPointeeType(); |
| 3503 | } |
| 3504 | } |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 3509 | return Sema::TDK_Success; |
| 3510 | |
| 3511 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
| 3512 | S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) |
| 3513 | return Sema::TDK_Success; |
| 3514 | |
| 3515 | return Failed(); |
| 3516 | } |
| 3517 | |
| 3518 | |
| 3519 | |
| 3520 | |
| 3521 | |
| 3522 | |
| 3523 | |
| 3524 | |
| 3525 | |
| 3526 | static unsigned getPackIndexForParam(Sema &S, |
| 3527 | FunctionTemplateDecl *FunctionTemplate, |
| 3528 | const MultiLevelTemplateArgumentList &Args, |
| 3529 | unsigned ParamIdx) { |
| 3530 | unsigned Idx = 0; |
| 3531 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
| 3532 | if (PD->isParameterPack()) { |
| 3533 | unsigned NumExpansions = |
| 3534 | S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); |
| 3535 | if (Idx + NumExpansions > ParamIdx) |
| 3536 | return ParamIdx - Idx; |
| 3537 | Idx += NumExpansions; |
| 3538 | } else { |
| 3539 | if (Idx == ParamIdx) |
| 3540 | return -1; |
| 3541 | ++Idx; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | llvm_unreachable("parameter index would not be produced from template"); |
| 3546 | } |
| 3547 | |
| 3548 | |
| 3549 | |
| 3550 | |
| 3551 | |
| 3552 | |
| 3553 | |
| 3554 | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
| 3555 | FunctionTemplateDecl *FunctionTemplate, |
| 3556 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3557 | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
| 3558 | TemplateDeductionInfo &Info, |
| 3559 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 3560 | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
| 3561 | |
| 3562 | EnterExpressionEvaluationContext Unevaluated( |
| 3563 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| 3564 | SFINAETrap Trap(*this); |
| 3565 | |
| 3566 | |
| 3567 | |
| 3568 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
| 3569 | InstantiatingTemplate Inst( |
| 3570 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 3571 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); |
| 3572 | if (Inst.isInvalid()) |
| 3573 | return TDK_InstantiationDepth; |
| 3574 | |
| 3575 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 3576 | |
| 3577 | |
| 3578 | |
| 3579 | |
| 3580 | SmallVector<TemplateArgument, 4> Builder; |
| 3581 | if (auto Result = ConvertDeducedTemplateArguments( |
| 3582 | *this, FunctionTemplate, true, Deduced, Info, Builder, |
| 3583 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 3584 | PartialOverloading)) |
| 3585 | return Result; |
| 3586 | |
| 3587 | |
| 3588 | |
| 3589 | |
| 3590 | |
| 3591 | |
| 3592 | |
| 3593 | |
| 3594 | |
| 3595 | |
| 3596 | if (CheckNonDependent()) |
| 3597 | return TDK_NonDependentConversionFailure; |
| 3598 | |
| 3599 | |
| 3600 | TemplateArgumentList *DeducedArgumentList |
| 3601 | = TemplateArgumentList::CreateCopy(Context, Builder); |
| 3602 | Info.reset(DeducedArgumentList); |
| 3603 | |
| 3604 | |
| 3605 | |
| 3606 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 3607 | if (FunctionTemplate->getFriendObjectKind()) |
| 3608 | Owner = FunctionTemplate->getLexicalDeclContext(); |
| 3609 | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
| 3610 | Specialization = cast_or_null<FunctionDecl>( |
| 3611 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
| 3612 | if (!Specialization || Specialization->isInvalidDecl()) |
| 3613 | return TDK_SubstitutionFailure; |
| 3614 | |
| 3615 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
| 3616 | FunctionTemplate->getCanonicalDecl()); |
| 3617 | |
| 3618 | |
| 3619 | |
| 3620 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 3621 | !Trap.hasErrorOccurred()) |
| 3622 | Info.take(); |
| 3623 | |
| 3624 | |
| 3625 | |
| 3626 | |
| 3627 | if (Trap.hasErrorOccurred()) { |
| 3628 | Specialization->setInvalidDecl(true); |
| 3629 | return TDK_SubstitutionFailure; |
| 3630 | } |
| 3631 | |
| 3632 | |
| 3633 | |
| 3634 | |
| 3635 | |
| 3636 | |
| 3637 | |
| 3638 | |
| 3639 | |
| 3640 | if (!PartialOverloading || |
| 3641 | (Builder.size() == FunctionTemplate->getTemplateParameters()->size())) { |
| 3642 | if (CheckInstantiatedFunctionTemplateConstraints(Info.getLocation(), |
| 3643 | Specialization, Builder, Info.AssociatedConstraintsSatisfaction)) |
| 3644 | return TDK_MiscellaneousDeductionFailure; |
| 3645 | |
| 3646 | if (!Info.AssociatedConstraintsSatisfaction.IsSatisfied) { |
| 3647 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
| 3648 | return TDK_ConstraintsNotSatisfied; |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | if (OriginalCallArgs) { |
| 3653 | |
| 3654 | |
| 3655 | |
| 3656 | |
| 3657 | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
| 3658 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 3659 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
| 3660 | |
| 3661 | auto ParamIdx = OriginalArg.ArgIdx; |
| 3662 | if (ParamIdx >= Specialization->getNumParams()) |
| 3663 | |
| 3664 | |
| 3665 | |
| 3666 | continue; |
| 3667 | |
| 3668 | QualType DeducedA; |
| 3669 | if (!OriginalArg.DecomposedParam) { |
| 3670 | |
| 3671 | |
| 3672 | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
| 3673 | } else { |
| 3674 | |
| 3675 | |
| 3676 | |
| 3677 | QualType &CacheEntry = |
| 3678 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
| 3679 | if (CacheEntry.isNull()) { |
| 3680 | ArgumentPackSubstitutionIndexRAII PackIndex( |
| 3681 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
| 3682 | ParamIdx)); |
| 3683 | CacheEntry = |
| 3684 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
| 3685 | Specialization->getTypeSpecStartLoc(), |
| 3686 | Specialization->getDeclName()); |
| 3687 | } |
| 3688 | DeducedA = CacheEntry; |
| 3689 | } |
| 3690 | |
| 3691 | if (auto TDK = |
| 3692 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) |
| 3693 | return TDK; |
| 3694 | } |
| 3695 | } |
| 3696 | |
| 3697 | |
| 3698 | |
| 3699 | |
| 3700 | |
| 3701 | if (Info.diag_begin() != Info.diag_end()) { |
| 3702 | SuppressedDiagnosticsMap::iterator |
| 3703 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3704 | if (Pos == SuppressedDiagnostics.end()) |
| 3705 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3706 | .append(Info.diag_begin(), Info.diag_end()); |
| 3707 | } |
| 3708 | |
| 3709 | return TDK_Success; |
| 3710 | } |
| 3711 | |
| 3712 | |
| 3713 | |
| 3714 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
| 3715 | FunctionDecl *Fn) { |
| 3716 | |
| 3717 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
| 3718 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), false)) |
| 3719 | return {}; |
| 3720 | |
| 3721 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 3722 | if (Method->isInstance()) { |
| 3723 | |
| 3724 | |
| 3725 | if (!R.HasFormOfMemberPointer) |
| 3726 | return {}; |
| 3727 | |
| 3728 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3729 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
| 3730 | } |
| 3731 | |
| 3732 | if (!R.IsAddressOfOperand) return Fn->getType(); |
| 3733 | return S.Context.getPointerType(Fn->getType()); |
| 3734 | } |
| 3735 | |
| 3736 | |
| 3737 | |
| 3738 | |
| 3739 | |
| 3740 | static QualType |
| 3741 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
| 3742 | Expr *Arg, QualType ParamType, |
| 3743 | bool ParamWasReference) { |
| 3744 | |
| 3745 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
| 3746 | |
| 3747 | OverloadExpr *Ovl = R.Expression; |
| 3748 | |
| 3749 | |
| 3750 | unsigned TDF = 0; |
| 3751 | if (ParamWasReference) |
| 3752 | TDF |= TDF_ParamWithReferenceType; |
| 3753 | if (R.IsAddressOfOperand) |
| 3754 | TDF |= TDF_IgnoreQualifiers; |
| 3755 | |
| 3756 | |
| 3757 | |
| 3758 | |
| 3759 | |
| 3760 | if (!ParamType->isFunctionType() && |
| 3761 | !ParamType->isFunctionPointerType() && |
| 3762 | !ParamType->isMemberFunctionPointerType()) { |
| 3763 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3764 | |
| 3765 | if (FunctionDecl *ExplicitSpec |
| 3766 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
| 3767 | return GetTypeOfFunction(S, R, ExplicitSpec); |
| 3768 | } |
| 3769 | |
| 3770 | DeclAccessPair DAP; |
| 3771 | if (FunctionDecl *Viable = |
| 3772 | S.resolveAddressOfSingleOverloadCandidate(Arg, DAP)) |
| 3773 | return GetTypeOfFunction(S, R, Viable); |
| 3774 | |
| 3775 | return {}; |
| 3776 | } |
| 3777 | |
| 3778 | |
| 3779 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3780 | if (Ovl->hasExplicitTemplateArgs()) |
| 3781 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 3782 | QualType Match; |
| 3783 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3784 | E = Ovl->decls_end(); I != E; ++I) { |
| 3785 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3786 | |
| 3787 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3788 | |
| 3789 | |
| 3790 | |
| 3791 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3792 | return {}; |
| 3793 | |
| 3794 | |
| 3795 | FunctionDecl *Specialization = nullptr; |
| 3796 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
| 3797 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3798 | Specialization, Info)) |
| 3799 | continue; |
| 3800 | |
| 3801 | D = Specialization; |
| 3802 | } |
| 3803 | |
| 3804 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 3805 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
| 3806 | if (ArgType.isNull()) continue; |
| 3807 | |
| 3808 | |
| 3809 | if (!ParamWasReference && ParamType->isPointerType() && |
| 3810 | ArgType->isFunctionType()) |
| 3811 | ArgType = S.Context.getPointerType(ArgType); |
| 3812 | |
| 3813 | |
| 3814 | |
| 3815 | |
| 3816 | |
| 3817 | |
| 3818 | |
| 3819 | |
| 3820 | |
| 3821 | |
| 3822 | |
| 3823 | |
| 3824 | |
| 3825 | SmallVector<DeducedTemplateArgument, 8> |
| 3826 | Deduced(TemplateParams->size()); |
| 3827 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
| 3828 | Sema::TemplateDeductionResult Result |
| 3829 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3830 | ArgType, Info, Deduced, TDF); |
| 3831 | if (Result) continue; |
| 3832 | if (!Match.isNull()) |
| 3833 | return {}; |
| 3834 | Match = ArgType; |
| 3835 | } |
| 3836 | |
| 3837 | return Match; |
| 3838 | } |
| 3839 | |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | |
| 3844 | |
| 3845 | |
| 3846 | static bool AdjustFunctionParmAndArgTypesForDeduction( |
| 3847 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3848 | QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { |
| 3849 | |
| 3850 | |
| 3851 | |
| 3852 | if (ParamType.hasQualifiers()) |
| 3853 | ParamType = ParamType.getUnqualifiedType(); |
| 3854 | |
| 3855 | |
| 3856 | |
| 3857 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
| 3858 | if (ParamRefType) |
| 3859 | ParamType = ParamRefType->getPointeeType(); |
| 3860 | |
| 3861 | |
| 3862 | |
| 3863 | |
| 3864 | if (ArgType == S.Context.OverloadTy) { |
| 3865 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3866 | Arg, ParamType, |
| 3867 | ParamRefType != nullptr); |
| 3868 | if (ArgType.isNull()) |
| 3869 | return true; |
| 3870 | } |
| 3871 | |
| 3872 | if (ParamRefType) { |
| 3873 | |
| 3874 | if (ArgType->isIncompleteArrayType()) |
| 3875 | ArgType = S.getCompletedType(Arg); |
| 3876 | |
| 3877 | |
| 3878 | |
| 3879 | |
| 3880 | if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && |
| 3881 | Arg->isLValue()) { |
| 3882 | if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()) |
| 3883 | ArgType = S.Context.getAddrSpaceQualType(ArgType, LangAS::opencl_generic); |
| 3884 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3885 | } |
| 3886 | } else { |
| 3887 | |
| 3888 | |
| 3889 | |
| 3890 | |
| 3891 | |
| 3892 | if (ArgType->isArrayType()) |
| 3893 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3894 | |
| 3895 | |
| 3896 | |
| 3897 | else if (ArgType->isFunctionType()) |
| 3898 | ArgType = S.Context.getPointerType(ArgType); |
| 3899 | else { |
| 3900 | |
| 3901 | |
| 3902 | ArgType = ArgType.getUnqualifiedType(); |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | |
| 3907 | |
| 3908 | |
| 3909 | |
| 3910 | TDF = TDF_SkipNonDependent; |
| 3911 | |
| 3912 | |
| 3913 | |
| 3914 | |
| 3915 | if (ParamRefType) |
| 3916 | TDF |= TDF_ParamWithReferenceType; |
| 3917 | |
| 3918 | |
| 3919 | |
| 3920 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3921 | ArgType->isObjCObjectPointerType()) |
| 3922 | TDF |= TDF_IgnoreQualifiers; |
| 3923 | |
| 3924 | |
| 3925 | |
| 3926 | |
| 3927 | |
| 3928 | if (isSimpleTemplateIdType(ParamType) || |
| 3929 | (isa<PointerType>(ParamType) && |
| 3930 | isSimpleTemplateIdType( |
| 3931 | ParamType->castAs<PointerType>()->getPointeeType()))) |
| 3932 | TDF |= TDF_DerivedClass; |
| 3933 | |
| 3934 | return false; |
| 3935 | } |
| 3936 | |
| 3937 | static bool |
| 3938 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3939 | QualType T); |
| 3940 | |
| 3941 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
| 3942 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3943 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
| 3944 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3945 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
| 3946 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
| 3947 | |
| 3948 | |
| 3949 | |
| 3950 | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
| 3951 | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
| 3952 | InitListExpr *ILE, TemplateDeductionInfo &Info, |
| 3953 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3954 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
| 3955 | unsigned TDF) { |
| 3956 | |
| 3957 | |
| 3958 | |
| 3959 | |
| 3960 | |
| 3961 | |
| 3962 | |
| 3963 | |
| 3964 | if (!ILE->getNumInits()) |
| 3965 | return Sema::TDK_Success; |
| 3966 | |
| 3967 | QualType ElTy; |
| 3968 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
| 3969 | if (ArrTy) |
| 3970 | ElTy = ArrTy->getElementType(); |
| 3971 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
| 3972 | |
| 3973 | |
| 3974 | return Sema::TDK_Success; |
| 3975 | } |
| 3976 | |
| 3977 | |
| 3978 | |
| 3979 | for (Expr *E : ILE->inits()) |
| 3980 | if (isa<DesignatedInitExpr>(E)) |
| 3981 | return Sema::TDK_Success; |
| 3982 | |
| 3983 | |
| 3984 | if (ElTy->isDependentType()) { |
| 3985 | for (Expr *E : ILE->inits()) { |
| 3986 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
| 3987 | S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
| 3988 | ArgIdx, TDF)) |
| 3989 | return Result; |
| 3990 | } |
| 3991 | } |
| 3992 | |
| 3993 | |
| 3994 | |
| 3995 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
| 3996 | |
| 3997 | if (const NonTypeTemplateParmDecl *NTTP = |
| 3998 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
| 3999 | |
| 4000 | |
| 4001 | |
| 4002 | |
| 4003 | QualType T = S.Context.getSizeType(); |
| 4004 | llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); |
| 4005 | if (auto Result = DeduceNonTypeTemplateArgument( |
| 4006 | S, TemplateParams, NTTP, llvm::APSInt(Size), T, |
| 4007 | true, Info, Deduced)) |
| 4008 | return Result; |
| 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | return Sema::TDK_Success; |
| 4013 | } |
| 4014 | |
| 4015 | |
| 4016 | |
| 4017 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
| 4018 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 4019 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
| 4020 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 4021 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
| 4022 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
| 4023 | QualType ArgType = Arg->getType(); |
| 4024 | QualType OrigParamType = ParamType; |
| 4025 | |
| 4026 | |
| 4027 | |
| 4028 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4029 | S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) |
| 4030 | return Sema::TDK_Success; |
| 4031 | |
| 4032 | |
| 4033 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
| 4034 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
| 4035 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
| 4036 | |
| 4037 | |
| 4038 | |
| 4039 | |
| 4040 | |
| 4041 | |
| 4042 | OriginalCallArgs.push_back( |
| 4043 | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
| 4044 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 4045 | ArgType, Info, Deduced, TDF); |
| 4046 | } |
| 4047 | |
| 4048 | |
| 4049 | |
| 4050 | |
| 4051 | |
| 4052 | |
| 4053 | |
| 4054 | |
| 4055 | |
| 4056 | |
| 4057 | |
| 4058 | |
| 4059 | |
| 4060 | |
| 4061 | |
| 4062 | |
| 4063 | |
| 4064 | |
| 4065 | |
| 4066 | |
| 4067 | |
| 4068 | |
| 4069 | |
| 4070 | |
| 4071 | |
| 4072 | |
| 4073 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4074 | FunctionTemplateDecl *FunctionTemplate, |
| 4075 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
| 4076 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4077 | bool PartialOverloading, |
| 4078 | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
| 4079 | if (FunctionTemplate->isInvalidDecl()) |
| 4080 | return TDK_Invalid; |
| 4081 | |
| 4082 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 4083 | unsigned NumParams = Function->getNumParams(); |
| 4084 | |
| 4085 | unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); |
| 4086 | |
| 4087 | |
| 4088 | |
| 4089 | |
| 4090 | |
| 4091 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
| 4092 | return TDK_TooFewArguments; |
| 4093 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
| 4094 | const auto *Proto = Function->getType()->castAs<FunctionProtoType>(); |
| 4095 | if (Proto->isTemplateVariadic()) |
| 4096 | ; |
| 4097 | else if (!Proto->isVariadic()) |
| 4098 | return TDK_TooManyArguments; |
| 4099 | } |
| 4100 | |
| 4101 | |
| 4102 | |
| 4103 | LocalInstantiationScope InstScope(*this); |
| 4104 | TemplateParameterList *TemplateParams |
| 4105 | = FunctionTemplate->getTemplateParameters(); |
| 4106 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4107 | SmallVector<QualType, 8> ParamTypes; |
| 4108 | unsigned NumExplicitlySpecified = 0; |
| 4109 | if (ExplicitTemplateArgs) { |
| 4110 | TemplateDeductionResult Result; |
| 4111 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 4112 | Result = SubstituteExplicitTemplateArguments( |
| 4113 | FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, nullptr, |
| 4114 | Info); |
| 4115 | }); |
| 4116 | if (Result) |
| 4117 | return Result; |
| 4118 | |
| 4119 | NumExplicitlySpecified = Deduced.size(); |
| 4120 | } else { |
| 4121 | |
| 4122 | for (unsigned I = 0; I != NumParams; ++I) |
| 4123 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 4124 | } |
| 4125 | |
| 4126 | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
| 4127 | |
| 4128 | |
| 4129 | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
| 4130 | |
| 4131 | |
| 4132 | |
| 4133 | |
| 4134 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 4135 | return Sema::TDK_Success; |
| 4136 | |
| 4137 | |
| 4138 | return DeduceTemplateArgumentsFromCallArgument( |
| 4139 | *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, |
| 4140 | OriginalCallArgs, false, ArgIdx, 0); |
| 4141 | }; |
| 4142 | |
| 4143 | |
| 4144 | Deduced.resize(TemplateParams->size()); |
| 4145 | SmallVector<QualType, 8> ParamTypesForArgChecking; |
| 4146 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
| 4147 | ParamIdx != NumParamTypes; ++ParamIdx) { |
| 4148 | QualType ParamType = ParamTypes[ParamIdx]; |
| 4149 | |
| 4150 | const PackExpansionType *ParamExpansion = |
| 4151 | dyn_cast<PackExpansionType>(ParamType); |
| 4152 | if (!ParamExpansion) { |
| 4153 | |
| 4154 | if (ArgIdx >= Args.size()) |
| 4155 | break; |
| 4156 | |
| 4157 | ParamTypesForArgChecking.push_back(ParamType); |
| 4158 | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
| 4159 | return Result; |
| 4160 | |
| 4161 | continue; |
| 4162 | } |
| 4163 | |
| 4164 | QualType ParamPattern = ParamExpansion->getPattern(); |
| 4165 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 4166 | ParamPattern); |
| 4167 | |
| 4168 | |
| 4169 | |
| 4170 | |
| 4171 | |
| 4172 | |
| 4173 | |
| 4174 | |
| 4175 | |
| 4176 | |
| 4177 | |
| 4178 | |
| 4179 | |
| 4180 | |
| 4181 | |
| 4182 | |
| 4183 | |
| 4184 | if (ParamIdx + 1 == NumParamTypes || PackScope.hasFixedArity()) { |
| 4185 | for (; ArgIdx < Args.size() && PackScope.hasNextElement(); |
| 4186 | PackScope.nextPackElement(), ++ArgIdx) { |
| 4187 | ParamTypesForArgChecking.push_back(ParamPattern); |
| 4188 | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
| 4189 | return Result; |
| 4190 | } |
| 4191 | } else { |
| 4192 | |
| 4193 | |
| 4194 | |
| 4195 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
| 4196 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
| 4197 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); |
| 4198 | ++I, ++ArgIdx) { |
| 4199 | ParamTypesForArgChecking.push_back(ParamPattern); |
| 4200 | |
| 4201 | |
| 4202 | PackScope.nextPackElement(); |
| 4203 | } |
| 4204 | } |
| 4205 | } |
| 4206 | |
| 4207 | |
| 4208 | |
| 4209 | if (auto Result = PackScope.finish()) |
| 4210 | return Result; |
| 4211 | } |
| 4212 | |
| 4213 | |
| 4214 | |
| 4215 | DeclContext *CallingCtx = CurContext; |
| 4216 | |
| 4217 | TemplateDeductionResult Result; |
| 4218 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 4219 | Result = FinishTemplateArgumentDeduction( |
| 4220 | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
| 4221 | &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { |
| 4222 | ContextRAII SavedContext(*this, CallingCtx); |
| 4223 | return CheckNonDependent(ParamTypesForArgChecking); |
| 4224 | }); |
| 4225 | }); |
| 4226 | return Result; |
| 4227 | } |
| 4228 | |
| 4229 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
| 4230 | QualType FunctionType, |
| 4231 | bool AdjustExceptionSpec) { |
| 4232 | if (ArgFunctionType.isNull()) |
| 4233 | return ArgFunctionType; |
| 4234 | |
| 4235 | const auto *FunctionTypeP = FunctionType->castAs<FunctionProtoType>(); |
| 4236 | const auto *ArgFunctionTypeP = ArgFunctionType->castAs<FunctionProtoType>(); |
| 4237 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 4238 | bool Rebuild = false; |
| 4239 | |
| 4240 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 4241 | if (EPI.ExtInfo.getCC() != CC) { |
| 4242 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 4243 | Rebuild = true; |
| 4244 | } |
| 4245 | |
| 4246 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 4247 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 4248 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 4249 | Rebuild = true; |
| 4250 | } |
| 4251 | |
| 4252 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 4253 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 4254 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 4255 | Rebuild = true; |
| 4256 | } |
| 4257 | |
| 4258 | if (!Rebuild) |
| 4259 | return ArgFunctionType; |
| 4260 | |
| 4261 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 4262 | ArgFunctionTypeP->getParamTypes(), EPI); |
| 4263 | } |
| 4264 | |
| 4265 | |
| 4266 | |
| 4267 | |
| 4268 | |
| 4269 | |
| 4270 | |
| 4271 | |
| 4272 | |
| 4273 | |
| 4274 | |
| 4275 | |
| 4276 | |
| 4277 | |
| 4278 | |
| 4279 | |
| 4280 | |
| 4281 | |
| 4282 | |
| 4283 | |
| 4284 | |
| 4285 | |
| 4286 | |
| 4287 | |
| 4288 | |
| 4289 | |
| 4290 | |
| 4291 | |
| 4292 | |
| 4293 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4294 | FunctionTemplateDecl *FunctionTemplate, |
| 4295 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 4296 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4297 | bool IsAddressOfFunction) { |
| 4298 | if (FunctionTemplate->isInvalidDecl()) |
| 4299 | return TDK_Invalid; |
| 4300 | |
| 4301 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 4302 | TemplateParameterList *TemplateParams |
| 4303 | = FunctionTemplate->getTemplateParameters(); |
| 4304 | QualType FunctionType = Function->getType(); |
| 4305 | |
| 4306 | |
| 4307 | LocalInstantiationScope InstScope(*this); |
| 4308 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4309 | unsigned NumExplicitlySpecified = 0; |
| 4310 | SmallVector<QualType, 4> ParamTypes; |
| 4311 | if (ExplicitTemplateArgs) { |
| 4312 | TemplateDeductionResult Result; |
| 4313 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 4314 | Result = SubstituteExplicitTemplateArguments( |
| 4315 | FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, |
| 4316 | &FunctionType, Info); |
| 4317 | }); |
| 4318 | if (Result) |
| 4319 | return Result; |
| 4320 | |
| 4321 | NumExplicitlySpecified = Deduced.size(); |
| 4322 | } |
| 4323 | |
| 4324 | |
| 4325 | |
| 4326 | |
| 4327 | if (!IsAddressOfFunction) |
| 4328 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 4329 | false); |
| 4330 | |
| 4331 | |
| 4332 | EnterExpressionEvaluationContext Unevaluated( |
| 4333 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| 4334 | SFINAETrap Trap(*this); |
| 4335 | |
| 4336 | Deduced.resize(TemplateParams->size()); |
| 4337 | |
| 4338 | |
| 4339 | |
| 4340 | |
| 4341 | |
| 4342 | bool HasDeducedReturnType = false; |
| 4343 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
| 4344 | Function->getReturnType()->getContainedAutoType()) { |
| 4345 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
| 4346 | HasDeducedReturnType = true; |
| 4347 | } |
| 4348 | |
| 4349 | if (!ArgFunctionType.isNull()) { |
| 4350 | unsigned TDF = |
| 4351 | TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; |
| 4352 | |
| 4353 | if (TemplateDeductionResult Result |
| 4354 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 4355 | FunctionType, ArgFunctionType, |
| 4356 | Info, Deduced, TDF)) |
| 4357 | return Result; |
| 4358 | } |
| 4359 | |
| 4360 | TemplateDeductionResult Result; |
| 4361 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 4362 | Result = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 4363 | NumExplicitlySpecified, |
| 4364 | Specialization, Info); |
| 4365 | }); |
| 4366 | if (Result) |
| 4367 | return Result; |
| 4368 | |
| 4369 | |
| 4370 | |
| 4371 | if (HasDeducedReturnType && |
| 4372 | Specialization->getReturnType()->isUndeducedType() && |
| 4373 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 4374 | return TDK_MiscellaneousDeductionFailure; |
| 4375 | |
| 4376 | |
| 4377 | |
| 4378 | auto *SpecializationFPT = |
| 4379 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 4380 | if (getLangOpts().CPlusPlus17 && |
| 4381 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 4382 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 4383 | return TDK_MiscellaneousDeductionFailure; |
| 4384 | |
| 4385 | |
| 4386 | |
| 4387 | |
| 4388 | |
| 4389 | QualType SpecializationType = Specialization->getType(); |
| 4390 | if (!IsAddressOfFunction) |
| 4391 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 4392 | true); |
| 4393 | |
| 4394 | |
| 4395 | |
| 4396 | |
| 4397 | if (!ArgFunctionType.isNull()) { |
| 4398 | if (IsAddressOfFunction && |
| 4399 | !isSameOrCompatibleFunctionType( |
| 4400 | Context.getCanonicalType(SpecializationType), |
| 4401 | Context.getCanonicalType(ArgFunctionType))) |
| 4402 | return TDK_MiscellaneousDeductionFailure; |
| 4403 | |
| 4404 | if (!IsAddressOfFunction && |
| 4405 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
| 4406 | return TDK_MiscellaneousDeductionFailure; |
| 4407 | } |
| 4408 | |
| 4409 | return TDK_Success; |
| 4410 | } |
| 4411 | |
| 4412 | |
| 4413 | |
| 4414 | |
| 4415 | Sema::TemplateDeductionResult |
| 4416 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
| 4417 | QualType ToType, |
| 4418 | CXXConversionDecl *&Specialization, |
| 4419 | TemplateDeductionInfo &Info) { |
| 4420 | if (ConversionTemplate->isInvalidDecl()) |
| 4421 | return TDK_Invalid; |
| 4422 | |
| 4423 | CXXConversionDecl *ConversionGeneric |
| 4424 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 4425 | |
| 4426 | QualType FromType = ConversionGeneric->getConversionType(); |
| 4427 | |
| 4428 | |
| 4429 | QualType P = Context.getCanonicalType(FromType); |
| 4430 | QualType A = Context.getCanonicalType(ToType); |
| 4431 | |
| 4432 | |
| 4433 | |
| 4434 | |
| 4435 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 4436 | P = PRef->getPointeeType(); |
| 4437 | |
| 4438 | |
| 4439 | |
| 4440 | |
| 4441 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { |
| 4442 | A = ARef->getPointeeType(); |
| 4443 | |
| 4444 | |
| 4445 | |
| 4446 | if (!FromType->getAs<ReferenceType>()) { |
| 4447 | A = A.getUnqualifiedType(); |
| 4448 | P = P.getUnqualifiedType(); |
| 4449 | } |
| 4450 | |
| 4451 | |
| 4452 | |
| 4453 | |
| 4454 | } else { |
| 4455 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 4456 | |
| 4457 | |
| 4458 | |
| 4459 | |
| 4460 | if (P->isArrayType()) |
| 4461 | P = Context.getArrayDecayedType(P); |
| 4462 | |
| 4463 | |
| 4464 | |
| 4465 | else if (P->isFunctionType()) |
| 4466 | P = Context.getPointerType(P); |
| 4467 | |
| 4468 | |
| 4469 | else |
| 4470 | P = P.getUnqualifiedType(); |
| 4471 | |
| 4472 | |
| 4473 | |
| 4474 | |
| 4475 | |
| 4476 | A = A.getUnqualifiedType(); |
| 4477 | } |
| 4478 | |
| 4479 | |
| 4480 | EnterExpressionEvaluationContext Unevaluated( |
| 4481 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| 4482 | SFINAETrap Trap(*this); |
| 4483 | |
| 4484 | |
| 4485 | |
| 4486 | |
| 4487 | |
| 4488 | |
| 4489 | TemplateParameterList *TemplateParams |
| 4490 | = ConversionTemplate->getTemplateParameters(); |
| 4491 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4492 | Deduced.resize(TemplateParams->size()); |
| 4493 | |
| 4494 | |
| 4495 | |
| 4496 | |
| 4497 | |
| 4498 | unsigned TDF = 0; |
| 4499 | |
| 4500 | |
| 4501 | |
| 4502 | if (ToType->isReferenceType()) |
| 4503 | TDF |= TDF_ArgWithReferenceType; |
| 4504 | |
| 4505 | |
| 4506 | |
| 4507 | |
| 4508 | |
| 4509 | |
| 4510 | |
| 4511 | if ((P->isPointerType() && A->isPointerType()) || |
| 4512 | (P->isMemberPointerType() && A->isMemberPointerType())) |
| 4513 | TDF |= TDF_IgnoreQualifiers; |
| 4514 | if (TemplateDeductionResult Result |
| 4515 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 4516 | P, A, Info, Deduced, TDF)) |
| 4517 | return Result; |
| 4518 | |
| 4519 | |
| 4520 | LocalInstantiationScope InstScope(*this); |
| 4521 | |
| 4522 | FunctionDecl *ConversionSpecialized = nullptr; |
| 4523 | TemplateDeductionResult Result; |
| 4524 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 4525 | Result = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
| 4526 | ConversionSpecialized, Info); |
| 4527 | }); |
| 4528 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 4529 | return Result; |
| 4530 | } |
| 4531 | |
| 4532 | |
| 4533 | |
| 4534 | |
| 4535 | |
| 4536 | |
| 4537 | |
| 4538 | |
| 4539 | |
| 4540 | |
| 4541 | |
| 4542 | |
| 4543 | |
| 4544 | |
| 4545 | |
| 4546 | |
| 4547 | |
| 4548 | |
| 4549 | |
| 4550 | |
| 4551 | |
| 4552 | |
| 4553 | |
| 4554 | |
| 4555 | |
| 4556 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4557 | FunctionTemplateDecl *FunctionTemplate, |
| 4558 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4559 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4560 | bool IsAddressOfFunction) { |
| 4561 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
| 4562 | QualType(), Specialization, Info, |
| 4563 | IsAddressOfFunction); |
| 4564 | } |
| 4565 | |
| 4566 | namespace { |
| 4567 | struct DependentAuto { bool IsPack; }; |
| 4568 | |
| 4569 | |
| 4570 | |
| 4571 | class SubstituteDeducedTypeTransform : |
| 4572 | public TreeTransform<SubstituteDeducedTypeTransform> { |
| 4573 | QualType Replacement; |
| 4574 | bool ReplacementIsPack; |
| 4575 | bool UseTypeSugar; |
| 4576 | |
| 4577 | public: |
| 4578 | SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) |
| 4579 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), Replacement(), |
| 4580 | ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} |
| 4581 | |
| 4582 | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
| 4583 | bool UseTypeSugar = true) |
| 4584 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
| 4585 | Replacement(Replacement), ReplacementIsPack(false), |
| 4586 | UseTypeSugar(UseTypeSugar) {} |
| 4587 | |
| 4588 | QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { |
| 4589 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 4590 | "unexpected unsugared replacement kind"); |
| 4591 | QualType Result = Replacement; |
| 4592 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 4593 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4594 | return Result; |
| 4595 | } |
| 4596 | |
| 4597 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4598 | |
| 4599 | |
| 4600 | |
| 4601 | |
| 4602 | |
| 4603 | |
| 4604 | |
| 4605 | |
| 4606 | |
| 4607 | if (!UseTypeSugar) |
| 4608 | return TransformDesugared(TLB, TL); |
| 4609 | |
| 4610 | QualType Result = SemaRef.Context.getAutoType( |
| 4611 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull(), |
| 4612 | ReplacementIsPack, TL.getTypePtr()->getTypeConstraintConcept(), |
| 4613 | TL.getTypePtr()->getTypeConstraintArguments()); |
| 4614 | auto NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4615 | NewTL.copy(TL); |
| 4616 | return Result; |
| 4617 | } |
| 4618 | |
| 4619 | QualType TransformDeducedTemplateSpecializationType( |
| 4620 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { |
| 4621 | if (!UseTypeSugar) |
| 4622 | return TransformDesugared(TLB, TL); |
| 4623 | |
| 4624 | QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( |
| 4625 | TL.getTypePtr()->getTemplateName(), |
| 4626 | Replacement, Replacement.isNull()); |
| 4627 | auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); |
| 4628 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4629 | return Result; |
| 4630 | } |
| 4631 | |
| 4632 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4633 | |
| 4634 | return E; |
| 4635 | } |
| 4636 | |
| 4637 | QualType Apply(TypeLoc TL) { |
| 4638 | |
| 4639 | |
| 4640 | TypeLocBuilder TLB; |
| 4641 | TLB.reserve(TL.getFullDataSize()); |
| 4642 | return TransformType(TLB, TL); |
| 4643 | } |
| 4644 | }; |
| 4645 | |
| 4646 | } |
| 4647 | |
| 4648 | Sema::DeduceAutoResult |
| 4649 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 4650 | Optional<unsigned> DependentDeductionDepth, |
| 4651 | bool IgnoreConstraints) { |
| 4652 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 4653 | DependentDeductionDepth, IgnoreConstraints); |
| 4654 | } |
| 4655 | |
| 4656 | |
| 4657 | |
| 4658 | |
| 4659 | static bool diagnoseAutoDeductionFailure(Sema &S, |
| 4660 | Sema::TemplateDeductionResult TDK, |
| 4661 | TemplateDeductionInfo &Info, |
| 4662 | ArrayRef<SourceRange> Ranges) { |
| 4663 | switch (TDK) { |
| 4664 | case Sema::TDK_Inconsistent: { |
| 4665 | |
| 4666 | auto D = S.Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction); |
| 4667 | D << Info.FirstArg << Info.SecondArg; |
| 4668 | for (auto R : Ranges) |
| 4669 | D << R; |
| 4670 | return true; |
| 4671 | } |
| 4672 | |
| 4673 | |
| 4674 | |
| 4675 | |
| 4676 | default: |
| 4677 | return false; |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | static Sema::DeduceAutoResult |
| 4682 | CheckDeducedPlaceholderConstraints(Sema &S, const AutoType &Type, |
| 4683 | AutoTypeLoc TypeLoc, QualType Deduced) { |
| 4684 | ConstraintSatisfaction Satisfaction; |
| 4685 | ConceptDecl *Concept = Type.getTypeConstraintConcept(); |
| 4686 | TemplateArgumentListInfo TemplateArgs(TypeLoc.getLAngleLoc(), |
| 4687 | TypeLoc.getRAngleLoc()); |
| 4688 | TemplateArgs.addArgument( |
| 4689 | TemplateArgumentLoc(TemplateArgument(Deduced), |
| 4690 | S.Context.getTrivialTypeSourceInfo( |
| 4691 | Deduced, TypeLoc.getNameLoc()))); |
| 4692 | for (unsigned I = 0, C = TypeLoc.getNumArgs(); I != C; ++I) |
| 4693 | TemplateArgs.addArgument(TypeLoc.getArgLoc(I)); |
| 4694 | |
| 4695 | llvm::SmallVector<TemplateArgument, 4> Converted; |
| 4696 | if (S.CheckTemplateArgumentList(Concept, SourceLocation(), TemplateArgs, |
| 4697 | false, Converted)) |
| 4698 | return Sema::DAR_FailedAlreadyDiagnosed; |
| 4699 | if (S.CheckConstraintSatisfaction(Concept, {Concept->getConstraintExpr()}, |
| 4700 | Converted, TypeLoc.getLocalSourceRange(), |
| 4701 | Satisfaction)) |
| 4702 | return Sema::DAR_FailedAlreadyDiagnosed; |
| 4703 | if (!Satisfaction.IsSatisfied) { |
| 4704 | std::string Buf; |
| 4705 | llvm::raw_string_ostream OS(Buf); |
| 4706 | OS << "'" << Concept->getName(); |
| 4707 | if (TypeLoc.hasExplicitTemplateArgs()) { |
| 4708 | printTemplateArgumentList( |
| 4709 | OS, Type.getTypeConstraintArguments(), S.getPrintingPolicy(), |
| 4710 | Type.getTypeConstraintConcept()->getTemplateParameters()); |
| 4711 | } |
| 4712 | OS << "'"; |
| 4713 | OS.flush(); |
| 4714 | S.Diag(TypeLoc.getConceptNameLoc(), |
| 4715 | diag::err_placeholder_constraints_not_satisfied) |
| 4716 | << Deduced << Buf << TypeLoc.getLocalSourceRange(); |
| 4717 | S.DiagnoseUnsatisfiedConstraint(Satisfaction); |
| 4718 | return Sema::DAR_FailedAlreadyDiagnosed; |
| 4719 | } |
| 4720 | return Sema::DAR_Succeeded; |
| 4721 | } |
| 4722 | |
| 4723 | |
| 4724 | |
| 4725 | |
| 4726 | |
| 4727 | |
| 4728 | |
| 4729 | |
| 4730 | |
| 4731 | |
| 4732 | |
| 4733 | |
| 4734 | |
| 4735 | |
| 4736 | |
| 4737 | |
| 4738 | |
| 4739 | Sema::DeduceAutoResult |
| 4740 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 4741 | Optional<unsigned> DependentDeductionDepth, |
| 4742 | bool IgnoreConstraints) { |
| 4743 | if (Init->containsErrors()) |
| 4744 | return DAR_FailedAlreadyDiagnosed; |
| 4745 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
| 4746 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4747 | if (NonPlaceholder.isInvalid()) |
| 4748 | return DAR_FailedAlreadyDiagnosed; |
| 4749 | Init = NonPlaceholder.get(); |
| 4750 | } |
| 4751 | |
| 4752 | DependentAuto DependentResult = { |
| 4753 | (bool)Type.getAs<PackExpansionTypeLoc>()}; |
| 4754 | |
| 4755 | if (!DependentDeductionDepth && |
| 4756 | (Type.getType()->isDependentType() || Init->isTypeDependent() || |
| 4757 | Init->containsUnexpandedParameterPack())) { |
| 4758 | Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); |
| 4759 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4760 | return DAR_Succeeded; |
| 4761 | } |
| 4762 | |
| 4763 | |
| 4764 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4765 | |
| 4766 | |
| 4767 | |
| 4768 | |
| 4769 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
| 4770 | if (AT->isDecltypeAuto()) { |
| 4771 | if (isa<InitListExpr>(Init)) { |
| 4772 | Diag(Init->getBeginLoc(), diag::err_decltype_auto_initializer_list); |
| 4773 | return DAR_FailedAlreadyDiagnosed; |
| 4774 | } |
| 4775 | |
| 4776 | ExprResult ER = CheckPlaceholderExpr(Init); |
| 4777 | if (ER.isInvalid()) |
| 4778 | return DAR_FailedAlreadyDiagnosed; |
| 4779 | Init = ER.get(); |
| 4780 | QualType Deduced = BuildDecltypeType(Init, Init->getBeginLoc(), false); |
| 4781 | if (Deduced.isNull()) |
| 4782 | return DAR_FailedAlreadyDiagnosed; |
| 4783 | |
| 4784 | Deduced = Context.getCanonicalType(Deduced); |
| 4785 | if (AT->isConstrained() && !IgnoreConstraints) { |
| 4786 | auto ConstraintsResult = |
| 4787 | CheckDeducedPlaceholderConstraints(*this, *AT, |
| 4788 | Type.getContainedAutoTypeLoc(), |
| 4789 | Deduced); |
| 4790 | if (ConstraintsResult != DAR_Succeeded) |
| 4791 | return ConstraintsResult; |
| 4792 | } |
| 4793 | Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); |
| 4794 | if (Result.isNull()) |
| 4795 | return DAR_FailedAlreadyDiagnosed; |
| 4796 | return DAR_Succeeded; |
| 4797 | } else if (!getLangOpts().CPlusPlus) { |
| 4798 | if (isa<InitListExpr>(Init)) { |
| 4799 | Diag(Init->getBeginLoc(), diag::err_auto_init_list_from_c); |
| 4800 | return DAR_FailedAlreadyDiagnosed; |
| 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | |
| 4805 | SourceLocation Loc = Init->getExprLoc(); |
| 4806 | |
| 4807 | LocalInstantiationScope InstScope(*this); |
| 4808 | |
| 4809 | |
| 4810 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4811 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false, |
| 4812 | false); |
| 4813 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4814 | NamedDecl *TemplParamPtr = TemplParam; |
| 4815 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4816 | Context, Loc, Loc, TemplParamPtr, Loc, nullptr); |
| 4817 | |
| 4818 | QualType FuncParam = |
| 4819 | SubstituteDeducedTypeTransform(*this, TemplArg, false) |
| 4820 | .Apply(Type); |
| 4821 | assert(!FuncParam.isNull() && |
| 4822 | "substituting template parameter for 'auto' failed"); |
| 4823 | |
| 4824 | |
| 4825 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
| 4826 | Deduced.resize(1); |
| 4827 | |
| 4828 | TemplateDeductionInfo Info(Loc, Depth); |
| 4829 | |
| 4830 | |
| 4831 | |
| 4832 | auto DeductionFailed = [&](TemplateDeductionResult TDK, |
| 4833 | ArrayRef<SourceRange> Ranges) -> DeduceAutoResult { |
| 4834 | if (Init->isTypeDependent()) { |
| 4835 | Result = |
| 4836 | SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); |
| 4837 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4838 | return DAR_Succeeded; |
| 4839 | } |
| 4840 | if (diagnoseAutoDeductionFailure(*this, TDK, Info, Ranges)) |
| 4841 | return DAR_FailedAlreadyDiagnosed; |
| 4842 | return DAR_Failed; |
| 4843 | }; |
| 4844 | |
| 4845 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
| 4846 | |
| 4847 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 4848 | if (InitList) { |
| 4849 | |
| 4850 | |
| 4851 | |
| 4852 | if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) |
| 4853 | return DAR_Failed; |
| 4854 | |
| 4855 | |
| 4856 | |
| 4857 | for (Expr *E : InitList->inits()) |
| 4858 | if (isa<DesignatedInitExpr>(E)) |
| 4859 | return DAR_Failed; |
| 4860 | |
| 4861 | SourceRange DeducedFromInitRange; |
| 4862 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
| 4863 | Expr *Init = InitList->getInit(i); |
| 4864 | |
| 4865 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
| 4866 | *this, TemplateParamsSt.get(), 0, TemplArg, Init, |
| 4867 | Info, Deduced, OriginalCallArgs, true, |
| 4868 | 0, 0)) |
| 4869 | return DeductionFailed(TDK, {DeducedFromInitRange, |
| 4870 | Init->getSourceRange()}); |
| 4871 | |
| 4872 | if (DeducedFromInitRange.isInvalid() && |
| 4873 | Deduced[0].getKind() != TemplateArgument::Null) |
| 4874 | DeducedFromInitRange = Init->getSourceRange(); |
| 4875 | } |
| 4876 | } else { |
| 4877 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4878 | Diag(Loc, diag::err_auto_bitfield); |
| 4879 | return DAR_FailedAlreadyDiagnosed; |
| 4880 | } |
| 4881 | |
| 4882 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
| 4883 | *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, |
| 4884 | OriginalCallArgs, false, 0, 0)) |
| 4885 | return DeductionFailed(TDK, {}); |
| 4886 | } |
| 4887 | |
| 4888 | |
| 4889 | if (Deduced[0].getKind() != TemplateArgument::Type) |
| 4890 | return DeductionFailed(TDK_Incomplete, {}); |
| 4891 | |
| 4892 | QualType DeducedType = Deduced[0].getAsType(); |
| 4893 | |
| 4894 | if (InitList) { |
| 4895 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4896 | if (DeducedType.isNull()) |
| 4897 | return DAR_FailedAlreadyDiagnosed; |
| 4898 | } |
| 4899 | |
| 4900 | if (const auto *AT = Type.getType()->getAs<AutoType>()) { |
| 4901 | if (AT->isConstrained() && !IgnoreConstraints) { |
| 4902 | auto ConstraintsResult = |
| 4903 | CheckDeducedPlaceholderConstraints(*this, *AT, |
| 4904 | Type.getContainedAutoTypeLoc(), |
| 4905 | DeducedType); |
| 4906 | if (ConstraintsResult != DAR_Succeeded) |
| 4907 | return ConstraintsResult; |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); |
| 4912 | if (Result.isNull()) |
| 4913 | return DAR_FailedAlreadyDiagnosed; |
| 4914 | |
| 4915 | |
| 4916 | |
| 4917 | QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; |
| 4918 | for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { |
| 4919 | assert((bool)InitList == OriginalArg.DecomposedParam && |
| 4920 | "decomposed non-init-list in auto deduction?"); |
| 4921 | if (auto TDK = |
| 4922 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { |
| 4923 | Result = QualType(); |
| 4924 | return DeductionFailed(TDK, {}); |
| 4925 | } |
| 4926 | } |
| 4927 | |
| 4928 | return DAR_Succeeded; |
| 4929 | } |
| 4930 | |
| 4931 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
| 4932 | QualType TypeToReplaceAuto) { |
| 4933 | if (TypeToReplaceAuto->isDependentType()) |
| 4934 | return SubstituteDeducedTypeTransform( |
| 4935 | *this, DependentAuto{ |
| 4936 | TypeToReplaceAuto->containsUnexpandedParameterPack()}) |
| 4937 | .TransformType(TypeWithAuto); |
| 4938 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
| 4939 | .TransformType(TypeWithAuto); |
| 4940 | } |
| 4941 | |
| 4942 | TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4943 | QualType TypeToReplaceAuto) { |
| 4944 | if (TypeToReplaceAuto->isDependentType()) |
| 4945 | return SubstituteDeducedTypeTransform( |
| 4946 | *this, |
| 4947 | DependentAuto{ |
| 4948 | TypeToReplaceAuto->containsUnexpandedParameterPack()}) |
| 4949 | .TransformType(TypeWithAuto); |
| 4950 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
| 4951 | .TransformType(TypeWithAuto); |
| 4952 | } |
| 4953 | |
| 4954 | QualType Sema::ReplaceAutoType(QualType TypeWithAuto, |
| 4955 | QualType TypeToReplaceAuto) { |
| 4956 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
| 4957 | false) |
| 4958 | .TransformType(TypeWithAuto); |
| 4959 | } |
| 4960 | |
| 4961 | TypeSourceInfo *Sema::ReplaceAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4962 | QualType TypeToReplaceAuto) { |
| 4963 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
| 4964 | false) |
| 4965 | .TransformType(TypeWithAuto); |
| 4966 | } |
| 4967 | |
| 4968 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4969 | if (isa<InitListExpr>(Init)) |
| 4970 | Diag(VDecl->getLocation(), |
| 4971 | VDecl->isInitCapture() |
| 4972 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4973 | : diag::err_auto_var_deduction_failure_from_init_list) |
| 4974 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4975 | else |
| 4976 | Diag(VDecl->getLocation(), |
| 4977 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4978 | : diag::err_auto_var_deduction_failure) |
| 4979 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4980 | << Init->getSourceRange(); |
| 4981 | } |
| 4982 | |
| 4983 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4984 | bool Diagnose) { |
| 4985 | assert(FD->getReturnType()->isUndeducedType()); |
| 4986 | |
| 4987 | |
| 4988 | |
| 4989 | if (isLambdaConversionOperator(FD)) { |
| 4990 | CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); |
| 4991 | FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 4992 | |
| 4993 | |
| 4994 | if (auto *Args = FD->getTemplateSpecializationArgs()) { |
| 4995 | CallOp = InstantiateFunctionDeclaration( |
| 4996 | CallOp->getDescribedFunctionTemplate(), Args, Loc); |
| 4997 | if (!CallOp || CallOp->isInvalidDecl()) |
| 4998 | return true; |
| 4999 | |
| 5000 | |
| 5001 | |
| 5002 | if (CallOp->getReturnType()->isUndeducedType()) { |
| 5003 | runWithSufficientStackSpace(Loc, [&] { |
| 5004 | InstantiateFunctionDefinition(Loc, CallOp); |
| 5005 | }); |
| 5006 | } |
| 5007 | } |
| 5008 | |
| 5009 | if (CallOp->isInvalidDecl()) |
| 5010 | return true; |
| 5011 | assert(!CallOp->getReturnType()->isUndeducedType() && |
| 5012 | "failed to deduce lambda return type"); |
| 5013 | |
| 5014 | |
| 5015 | CallingConv RetTyCC = FD->getReturnType() |
| 5016 | ->getPointeeType() |
| 5017 | ->castAs<FunctionType>() |
| 5018 | ->getCallConv(); |
| 5019 | QualType RetType = getLambdaConversionFunctionResultType( |
| 5020 | CallOp->getType()->castAs<FunctionProtoType>(), RetTyCC); |
| 5021 | if (FD->getReturnType()->getAs<PointerType>()) |
| 5022 | RetType = Context.getPointerType(RetType); |
| 5023 | else { |
| 5024 | assert(FD->getReturnType()->getAs<BlockPointerType>()); |
| 5025 | RetType = Context.getBlockPointerType(RetType); |
| 5026 | } |
| 5027 | Context.adjustDeducedFunctionResultType(FD, RetType); |
| 5028 | return false; |
| 5029 | } |
| 5030 | |
| 5031 | if (FD->getTemplateInstantiationPattern()) { |
| 5032 | runWithSufficientStackSpace(Loc, [&] { |
| 5033 | InstantiateFunctionDefinition(Loc, FD); |
| 5034 | }); |
| 5035 | } |
| 5036 | |
| 5037 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
| 5038 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 5039 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 5040 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 5041 | } |
| 5042 | |
| 5043 | return StillUndeduced; |
| 5044 | } |
| 5045 | |
| 5046 | |
| 5047 | static void |
| 5048 | AddImplicitObjectParameterType(ASTContext &Context, |
| 5049 | CXXMethodDecl *Method, |
| 5050 | SmallVectorImpl<QualType> &ArgTypes) { |
| 5051 | |
| 5052 | |
| 5053 | |
| 5054 | |
| 5055 | |
| 5056 | |
| 5057 | |
| 5058 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 5059 | ArgTy = Context.getQualifiedType(ArgTy, Method->getMethodQualifiers()); |
| 5060 | if (Method->getRefQualifier() == RQ_RValue) |
| 5061 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 5062 | else |
| 5063 | ArgTy = Context.getLValueReferenceType(ArgTy); |
| 5064 | ArgTypes.push_back(ArgTy); |
| 5065 | } |
| 5066 | |
| 5067 | |
| 5068 | |
| 5069 | static bool isAtLeastAsSpecializedAs(Sema &S, |
| 5070 | SourceLocation Loc, |
| 5071 | FunctionTemplateDecl *FT1, |
| 5072 | FunctionTemplateDecl *FT2, |
| 5073 | TemplatePartialOrderingContext TPOC, |
| 5074 | unsigned NumCallArguments1, |
| 5075 | bool Reversed) { |
| 5076 | assert(!Reversed || TPOC == TPOC_Call); |
| 5077 | |
| 5078 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
| 5079 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
| 5080 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 5081 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
| 5082 | |
| 5083 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 5084 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
| 5085 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 5086 | Deduced.resize(TemplateParams->size()); |
| 5087 | |
| 5088 | |
| 5089 | |
| 5090 | |
| 5091 | TemplateDeductionInfo Info(Loc); |
| 5092 | SmallVector<QualType, 4> Args2; |
| 5093 | switch (TPOC) { |
| 5094 | case TPOC_Call: { |
| 5095 | |
| 5096 | |
| 5097 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 5098 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
| 5099 | |
| 5100 | |
| 5101 | |
| 5102 | |
| 5103 | |
| 5104 | |
| 5105 | |
| 5106 | |
| 5107 | |
| 5108 | |
| 5109 | |
| 5110 | |
| 5111 | |
| 5112 | |
| 5113 | |
| 5114 | |
| 5115 | SmallVector<QualType, 4> Args1; |
| 5116 | |
| 5117 | unsigned NumComparedArguments = NumCallArguments1; |
| 5118 | |
| 5119 | if (!Method2 && Method1 && !Method1->isStatic()) { |
| 5120 | |
| 5121 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 5122 | ++NumComparedArguments; |
| 5123 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
| 5124 | |
| 5125 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
| 5126 | } else if (Method1 && Method2 && Reversed) { |
| 5127 | |
| 5128 | |
| 5129 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 5130 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
| 5131 | ++NumComparedArguments; |
| 5132 | } |
| 5133 | |
| 5134 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
| 5135 | Proto1->param_type_end()); |
| 5136 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
| 5137 | Proto2->param_type_end()); |
| 5138 | |
| 5139 | |
| 5140 | |
| 5141 | |
| 5142 | if (Args1.size() > NumComparedArguments) |
| 5143 | Args1.resize(NumComparedArguments); |
| 5144 | if (Args2.size() > NumComparedArguments) |
| 5145 | Args2.resize(NumComparedArguments); |
| 5146 | if (Reversed) |
| 5147 | std::reverse(Args2.begin(), Args2.end()); |
| 5148 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 5149 | Args1.data(), Args1.size(), Info, Deduced, |
| 5150 | TDF_None, true)) |
| 5151 | return false; |
| 5152 | |
| 5153 | break; |
| 5154 | } |
| 5155 | |
| 5156 | case TPOC_Conversion: |
| 5157 | |
| 5158 | |
| 5159 | if (DeduceTemplateArgumentsByTypeMatch( |
| 5160 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 5161 | Info, Deduced, TDF_None, |
| 5162 | true)) |
| 5163 | return false; |
| 5164 | break; |
| 5165 | |
| 5166 | case TPOC_Other: |
| 5167 | |
| 5168 | |
| 5169 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 5170 | FD2->getType(), FD1->getType(), |
| 5171 | Info, Deduced, TDF_None, |
| 5172 | true)) |
| 5173 | return false; |
| 5174 | break; |
| 5175 | } |
| 5176 | |
| 5177 | |
| 5178 | |
| 5179 | |
| 5180 | |
| 5181 | |
| 5182 | |
| 5183 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 5184 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 5185 | if (Deduced[ArgIdx].isNull()) |
| 5186 | break; |
| 5187 | |
| 5188 | |
| 5189 | |
| 5190 | |
| 5191 | |
| 5192 | if (ArgIdx == NumArgs) { |
| 5193 | |
| 5194 | |
| 5195 | return true; |
| 5196 | } |
| 5197 | |
| 5198 | |
| 5199 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
| 5200 | switch (TPOC) { |
| 5201 | case TPOC_Call: |
| 5202 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 5203 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
| 5204 | TemplateParams->getDepth(), |
| 5205 | UsedParameters); |
| 5206 | break; |
| 5207 | |
| 5208 | case TPOC_Conversion: |
| 5209 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 5210 | TemplateParams->getDepth(), UsedParameters); |
| 5211 | break; |
| 5212 | |
| 5213 | case TPOC_Other: |
| 5214 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
| 5215 | TemplateParams->getDepth(), |
| 5216 | UsedParameters); |
| 5217 | break; |
| 5218 | } |
| 5219 | |
| 5220 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 5221 | |
| 5222 | |
| 5223 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 5224 | return false; |
| 5225 | |
| 5226 | return true; |
| 5227 | } |
| 5228 | |
| 5229 | |
| 5230 | |
| 5231 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 5232 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 5233 | unsigned NumParams = Function->getNumParams(); |
| 5234 | if (NumParams == 0) |
| 5235 | return false; |
| 5236 | |
| 5237 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 5238 | if (!Last->isParameterPack()) |
| 5239 | return false; |
| 5240 | |
| 5241 | |
| 5242 | while (--NumParams > 0) { |
| 5243 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 5244 | return false; |
| 5245 | } |
| 5246 | |
| 5247 | return true; |
| 5248 | } |
| 5249 | |
| 5250 | |
| 5251 | |
| 5252 | |
| 5253 | |
| 5254 | |
| 5255 | |
| 5256 | |
| 5257 | |
| 5258 | |
| 5259 | |
| 5260 | |
| 5261 | |
| 5262 | |
| 5263 | |
| 5264 | |
| 5265 | |
| 5266 | |
| 5267 | |
| 5268 | |
| 5269 | |
| 5270 | |
| 5271 | |
| 5272 | FunctionTemplateDecl * |
| 5273 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 5274 | FunctionTemplateDecl *FT2, |
| 5275 | SourceLocation Loc, |
| 5276 | TemplatePartialOrderingContext TPOC, |
| 5277 | unsigned NumCallArguments1, |
| 5278 | unsigned NumCallArguments2, |
| 5279 | bool Reversed) { |
| 5280 | |
| 5281 | auto JudgeByConstraints = [&] () -> FunctionTemplateDecl * { |
| 5282 | llvm::SmallVector<const Expr *, 3> AC1, AC2; |
| 5283 | FT1->getAssociatedConstraints(AC1); |
| 5284 | FT2->getAssociatedConstraints(AC2); |
| 5285 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
| 5286 | if (IsAtLeastAsConstrained(FT1, AC1, FT2, AC2, AtLeastAsConstrained1)) |
| 5287 | return nullptr; |
| 5288 | if (IsAtLeastAsConstrained(FT2, AC2, FT1, AC1, AtLeastAsConstrained2)) |
| 5289 | return nullptr; |
| 5290 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
| 5291 | return nullptr; |
| 5292 | return AtLeastAsConstrained1 ? FT1 : FT2; |
| 5293 | }; |
| 5294 | |
| 5295 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
| 5296 | NumCallArguments1, Reversed); |
| 5297 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
| 5298 | NumCallArguments2, Reversed); |
| 5299 | |
| 5300 | if (Better1 != Better2) |
| 5301 | return Better1 ? FT1 : FT2; |
| 5302 | |
| 5303 | if (!Better1 && !Better2) |
| 5304 | return JudgeByConstraints(); |
| 5305 | |
| 5306 | |
| 5307 | |
| 5308 | |
| 5309 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 5310 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 5311 | if (Variadic1 != Variadic2) |
| 5312 | return Variadic1? FT2 : FT1; |
| 5313 | |
| 5314 | return JudgeByConstraints(); |
| 5315 | } |
| 5316 | |
| 5317 | |
| 5318 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 5319 | if (T1 == T2) |
| 5320 | return true; |
| 5321 | |
| 5322 | if (!T1 || !T2) |
| 5323 | return false; |
| 5324 | |
| 5325 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 5326 | } |
| 5327 | |
| 5328 | |
| 5329 | |
| 5330 | |
| 5331 | |
| 5332 | |
| 5333 | |
| 5334 | |
| 5335 | |
| 5336 | |
| 5337 | |
| 5338 | |
| 5339 | |
| 5340 | |
| 5341 | |
| 5342 | |
| 5343 | |
| 5344 | |
| 5345 | |
| 5346 | |
| 5347 | |
| 5348 | |
| 5349 | |
| 5350 | |
| 5351 | |
| 5352 | |
| 5353 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 5354 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 5355 | TemplateSpecCandidateSet &FailedCandidates, |
| 5356 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 5357 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 5358 | bool Complain, QualType TargetType) { |
| 5359 | if (SpecBegin == SpecEnd) { |
| 5360 | if (Complain) { |
| 5361 | Diag(Loc, NoneDiag); |
| 5362 | FailedCandidates.NoteCandidates(*this, Loc); |
| 5363 | } |
| 5364 | return SpecEnd; |
| 5365 | } |
| 5366 | |
| 5367 | if (SpecBegin + 1 == SpecEnd) |
| 5368 | return SpecBegin; |
| 5369 | |
| 5370 | |
| 5371 | |
| 5372 | UnresolvedSetIterator Best = SpecBegin; |
| 5373 | FunctionTemplateDecl *BestTemplate |
| 5374 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
| 5375 | assert(BestTemplate && "Not a function template specialization?"); |
| 5376 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 5377 | FunctionTemplateDecl *Challenger |
| 5378 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
| 5379 | assert(Challenger && "Not a function template specialization?"); |
| 5380 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
| 5381 | Loc, TPOC_Other, 0, 0), |
| 5382 | Challenger)) { |
| 5383 | Best = I; |
| 5384 | BestTemplate = Challenger; |
| 5385 | } |
| 5386 | } |
| 5387 | |
| 5388 | |
| 5389 | |
| 5390 | bool Ambiguous = false; |
| 5391 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 5392 | FunctionTemplateDecl *Challenger |
| 5393 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
| 5394 | if (I != Best && |
| 5395 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
| 5396 | Loc, TPOC_Other, 0, 0), |
| 5397 | BestTemplate)) { |
| 5398 | Ambiguous = true; |
| 5399 | break; |
| 5400 | } |
| 5401 | } |
| 5402 | |
| 5403 | if (!Ambiguous) { |
| 5404 | |
| 5405 | return Best; |
| 5406 | } |
| 5407 | |
| 5408 | |
| 5409 | if (Complain) { |
| 5410 | Diag(Loc, AmbigDiag); |
| 5411 | |
| 5412 | |
| 5413 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 5414 | PartialDiagnostic PD = CandidateDiag; |
| 5415 | const auto *FD = cast<FunctionDecl>(*I); |
| 5416 | PD << FD << getTemplateArgumentBindingsText( |
| 5417 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 5418 | *FD->getTemplateSpecializationArgs()); |
| 5419 | if (!TargetType.isNull()) |
| 5420 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
| 5421 | Diag((*I)->getLocation(), PD); |
| 5422 | } |
| 5423 | } |
| 5424 | |
| 5425 | return SpecEnd; |
| 5426 | } |
| 5427 | |
| 5428 | |
| 5429 | |
| 5430 | |
| 5431 | |
| 5432 | |
| 5433 | |
| 5434 | |
| 5435 | template<typename TemplateLikeDecl> |
| 5436 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
| 5437 | TemplateLikeDecl *P2, |
| 5438 | TemplateDeductionInfo &Info) { |
| 5439 | |
| 5440 | |
| 5441 | |
| 5442 | |
| 5443 | |
| 5444 | |
| 5445 | |
| 5446 | |
| 5447 | |
| 5448 | |
| 5449 | |
| 5450 | |
| 5451 | |
| 5452 | |
| 5453 | |
| 5454 | |
| 5455 | |
| 5456 | |
| 5457 | |
| 5458 | |
| 5459 | |
| 5460 | |
| 5461 | |
| 5462 | |
| 5463 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 5464 | |
| 5465 | |
| 5466 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 5467 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 5468 | T2, T1, Info, Deduced, TDF_None, |
| 5469 | true)) |
| 5470 | return false; |
| 5471 | |
| 5472 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 5473 | Deduced.end()); |
| 5474 | Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, |
| 5475 | Info); |
| 5476 | if (Inst.isInvalid()) |
| 5477 | return false; |
| 5478 | |
| 5479 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 5480 | bool AtLeastAsSpecialized; |
| 5481 | S.runWithSufficientStackSpace(Info.getLocation(), [&] { |
| 5482 | AtLeastAsSpecialized = !FinishTemplateArgumentDeduction( |
| 5483 | S, P2, true, |
| 5484 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 5485 | TST1->template_arguments()), |
| 5486 | Deduced, Info); |
| 5487 | }); |
| 5488 | return AtLeastAsSpecialized; |
| 5489 | } |
| 5490 | |
| 5491 | |
| 5492 | |
| 5493 | |
| 5494 | |
| 5495 | |
| 5496 | |
| 5497 | |
| 5498 | |
| 5499 | |
| 5500 | |
| 5501 | ClassTemplatePartialSpecializationDecl * |
| 5502 | Sema::getMoreSpecializedPartialSpecialization( |
| 5503 | ClassTemplatePartialSpecializationDecl *PS1, |
| 5504 | ClassTemplatePartialSpecializationDecl *PS2, |
| 5505 | SourceLocation Loc) { |
| 5506 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 5507 | QualType PT2 = PS2->getInjectedSpecializationType(); |
| 5508 | |
| 5509 | TemplateDeductionInfo Info(Loc); |
| 5510 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 5511 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
| 5512 | |
| 5513 | if (!Better1 && !Better2) |
| 5514 | return nullptr; |
| 5515 | if (Better1 && Better2) { |
| 5516 | llvm::SmallVector<const Expr *, 3> AC1, AC2; |
| 5517 | PS1->getAssociatedConstraints(AC1); |
| 5518 | PS2->getAssociatedConstraints(AC2); |
| 5519 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
| 5520 | if (IsAtLeastAsConstrained(PS1, AC1, PS2, AC2, AtLeastAsConstrained1)) |
| 5521 | return nullptr; |
| 5522 | if (IsAtLeastAsConstrained(PS2, AC2, PS1, AC1, AtLeastAsConstrained2)) |
| 5523 | return nullptr; |
| 5524 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
| 5525 | return nullptr; |
| 5526 | return AtLeastAsConstrained1 ? PS1 : PS2; |
| 5527 | } |
| 5528 | |
| 5529 | return Better1 ? PS1 : PS2; |
| 5530 | } |
| 5531 | |
| 5532 | bool Sema::isMoreSpecializedThanPrimary( |
| 5533 | ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 5534 | ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 5535 | QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); |
| 5536 | QualType PartialT = Spec->getInjectedSpecializationType(); |
| 5537 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 5538 | return false; |
| 5539 | if (!isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) |
| 5540 | return true; |
| 5541 | Info.clearSFINAEDiagnostic(); |
| 5542 | llvm::SmallVector<const Expr *, 3> PrimaryAC, SpecAC; |
| 5543 | Primary->getAssociatedConstraints(PrimaryAC); |
| 5544 | Spec->getAssociatedConstraints(SpecAC); |
| 5545 | bool AtLeastAsConstrainedPrimary, AtLeastAsConstrainedSpec; |
| 5546 | if (IsAtLeastAsConstrained(Spec, SpecAC, Primary, PrimaryAC, |
| 5547 | AtLeastAsConstrainedSpec)) |
| 5548 | return false; |
| 5549 | if (!AtLeastAsConstrainedSpec) |
| 5550 | return false; |
| 5551 | if (IsAtLeastAsConstrained(Primary, PrimaryAC, Spec, SpecAC, |
| 5552 | AtLeastAsConstrainedPrimary)) |
| 5553 | return false; |
| 5554 | return !AtLeastAsConstrainedPrimary; |
| 5555 | } |
| 5556 | |
| 5557 | VarTemplatePartialSpecializationDecl * |
| 5558 | Sema::getMoreSpecializedPartialSpecialization( |
| 5559 | VarTemplatePartialSpecializationDecl *PS1, |
| 5560 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 5561 | |
| 5562 | |
| 5563 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
| 5564 | "the partial specializations being compared should specialize" |
| 5565 | " the same template."); |
| 5566 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 5567 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5568 | QualType PT1 = Context.getTemplateSpecializationType( |
| 5569 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
| 5570 | QualType PT2 = Context.getTemplateSpecializationType( |
| 5571 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
| 5572 | |
| 5573 | TemplateDeductionInfo Info(Loc); |
| 5574 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 5575 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
| 5576 | |
| 5577 | if (!Better1 && !Better2) |
| 5578 | return nullptr; |
| 5579 | if (Better1 && Better2) { |
| 5580 | llvm::SmallVector<const Expr *, 3> AC1, AC2; |
| 5581 | PS1->getAssociatedConstraints(AC1); |
| 5582 | PS2->getAssociatedConstraints(AC2); |
| 5583 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
| 5584 | if (IsAtLeastAsConstrained(PS1, AC1, PS2, AC2, AtLeastAsConstrained1)) |
| 5585 | return nullptr; |
| 5586 | if (IsAtLeastAsConstrained(PS2, AC2, PS1, AC1, AtLeastAsConstrained2)) |
| 5587 | return nullptr; |
| 5588 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
| 5589 | return nullptr; |
| 5590 | return AtLeastAsConstrained1 ? PS1 : PS2; |
| 5591 | } |
| 5592 | |
| 5593 | return Better1 ? PS1 : PS2; |
| 5594 | } |
| 5595 | |
| 5596 | bool Sema::isMoreSpecializedThanPrimary( |
| 5597 | VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 5598 | TemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 5599 | |
| 5600 | |
| 5601 | SmallVector<TemplateArgument, 8> PrimaryArgs; |
| 5602 | Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), |
| 5603 | PrimaryArgs); |
| 5604 | |
| 5605 | TemplateName CanonTemplate = |
| 5606 | Context.getCanonicalTemplateName(TemplateName(Primary)); |
| 5607 | QualType PrimaryT = Context.getTemplateSpecializationType( |
| 5608 | CanonTemplate, PrimaryArgs); |
| 5609 | QualType PartialT = Context.getTemplateSpecializationType( |
| 5610 | CanonTemplate, Spec->getTemplateArgs().asArray()); |
| 5611 | |
| 5612 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 5613 | return false; |
| 5614 | if (!isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) |
| 5615 | return true; |
| 5616 | Info.clearSFINAEDiagnostic(); |
| 5617 | llvm::SmallVector<const Expr *, 3> PrimaryAC, SpecAC; |
| 5618 | Primary->getAssociatedConstraints(PrimaryAC); |
| 5619 | Spec->getAssociatedConstraints(SpecAC); |
| 5620 | bool AtLeastAsConstrainedPrimary, AtLeastAsConstrainedSpec; |
| 5621 | if (IsAtLeastAsConstrained(Spec, SpecAC, Primary, PrimaryAC, |
| 5622 | AtLeastAsConstrainedSpec)) |
| 5623 | return false; |
| 5624 | if (!AtLeastAsConstrainedSpec) |
| 5625 | return false; |
| 5626 | if (IsAtLeastAsConstrained(Primary, PrimaryAC, Spec, SpecAC, |
| 5627 | AtLeastAsConstrainedPrimary)) |
| 5628 | return false; |
| 5629 | return !AtLeastAsConstrainedPrimary; |
| 5630 | } |
| 5631 | |
| 5632 | bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( |
| 5633 | TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { |
| 5634 | |
| 5635 | |
| 5636 | |
| 5637 | |
| 5638 | |
| 5639 | |
| 5640 | |
| 5641 | |
| 5642 | |
| 5643 | |
| 5644 | |
| 5645 | TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); |
| 5646 | TemplateParameterList *A = AArg->getTemplateParameters(); |
| 5647 | |
| 5648 | |
| 5649 | |
| 5650 | |
| 5651 | SmallVector<TemplateArgument, 8> AArgs; |
| 5652 | Context.getInjectedTemplateArgs(A, AArgs); |
| 5653 | |
| 5654 | |
| 5655 | |
| 5656 | |
| 5657 | |
| 5658 | SmallVector<TemplateArgument, 4> PArgs; |
| 5659 | { |
| 5660 | SFINAETrap Trap(*this); |
| 5661 | |
| 5662 | Context.getInjectedTemplateArgs(P, PArgs); |
| 5663 | TemplateArgumentListInfo PArgList(P->getLAngleLoc(), |
| 5664 | P->getRAngleLoc()); |
| 5665 | for (unsigned I = 0, N = P->size(); I != N; ++I) { |
| 5666 | |
| 5667 | |
| 5668 | TemplateArgument Arg = PArgs[I]; |
| 5669 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 5670 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); |
| 5671 | Arg = *Arg.pack_begin(); |
| 5672 | } |
| 5673 | PArgList.addArgument(getTrivialTemplateArgumentLoc( |
| 5674 | Arg, QualType(), P->getParam(I)->getLocation())); |
| 5675 | } |
| 5676 | PArgs.clear(); |
| 5677 | |
| 5678 | |
| 5679 | |
| 5680 | |
| 5681 | if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || |
| 5682 | Trap.hasErrorOccurred()) |
| 5683 | return false; |
| 5684 | } |
| 5685 | |
| 5686 | QualType AType = Context.getTemplateSpecializationType(X, AArgs); |
| 5687 | QualType PType = Context.getTemplateSpecializationType(X, PArgs); |
| 5688 | |
| 5689 | |
| 5690 | |
| 5691 | |
| 5692 | TemplateDeductionInfo Info(Loc, A->getDepth()); |
| 5693 | return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); |
| 5694 | } |
| 5695 | |
| 5696 | namespace { |
| 5697 | struct MarkUsedTemplateParameterVisitor : |
| 5698 | RecursiveASTVisitor<MarkUsedTemplateParameterVisitor> { |
| 5699 | llvm::SmallBitVector &Used; |
| 5700 | unsigned Depth; |
| 5701 | |
| 5702 | MarkUsedTemplateParameterVisitor(llvm::SmallBitVector &Used, |
| 5703 | unsigned Depth) |
| 5704 | : Used(Used), Depth(Depth) { } |
| 5705 | |
| 5706 | bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { |
| 5707 | if (T->getDepth() == Depth) |
| 5708 | Used[T->getIndex()] = true; |
| 5709 | return true; |
| 5710 | } |
| 5711 | |
| 5712 | bool TraverseTemplateName(TemplateName Template) { |
| 5713 | if (auto *TTP = |
| 5714 | dyn_cast<TemplateTemplateParmDecl>(Template.getAsTemplateDecl())) |
| 5715 | if (TTP->getDepth() == Depth) |
| 5716 | Used[TTP->getIndex()] = true; |
| 5717 | RecursiveASTVisitor<MarkUsedTemplateParameterVisitor>:: |
| 5718 | TraverseTemplateName(Template); |
| 5719 | return true; |
| 5720 | } |
| 5721 | |
| 5722 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 5723 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 5724 | if (NTTP->getDepth() == Depth) |
| 5725 | Used[NTTP->getIndex()] = true; |
| 5726 | return true; |
| 5727 | } |
| 5728 | }; |
| 5729 | } |
| 5730 | |
| 5731 | |
| 5732 | |
| 5733 | static void |
| 5734 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| 5735 | const Expr *E, |
| 5736 | bool OnlyDeduced, |
| 5737 | unsigned Depth, |
| 5738 | llvm::SmallBitVector &Used) { |
| 5739 | if (!OnlyDeduced) { |
| 5740 | MarkUsedTemplateParameterVisitor(Used, Depth) |
| 5741 | .TraverseStmt(const_cast<Expr *>(E)); |
| 5742 | return; |
| 5743 | } |
| 5744 | |
| 5745 | |
| 5746 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 5747 | E = Expansion->getPattern(); |
| 5748 | |
| 5749 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr(E, Depth); |
| 5750 | if (!NTTP) |
| 5751 | return; |
| 5752 | |
| 5753 | if (NTTP->getDepth() == Depth) |
| 5754 | Used[NTTP->getIndex()] = true; |
| 5755 | |
| 5756 | |
| 5757 | |
| 5758 | if (Ctx.getLangOpts().CPlusPlus17) |
| 5759 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
| 5760 | } |
| 5761 | |
| 5762 | |
| 5763 | |
| 5764 | static void |
| 5765 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| 5766 | NestedNameSpecifier *NNS, |
| 5767 | bool OnlyDeduced, |
| 5768 | unsigned Depth, |
| 5769 | llvm::SmallBitVector &Used) { |
| 5770 | if (!NNS) |
| 5771 | return; |
| 5772 | |
| 5773 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
| 5774 | Used); |
| 5775 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
| 5776 | OnlyDeduced, Depth, Used); |
| 5777 | } |
| 5778 | |
| 5779 | |
| 5780 | |
| 5781 | static void |
| 5782 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| 5783 | TemplateName Name, |
| 5784 | bool OnlyDeduced, |
| 5785 | unsigned Depth, |
| 5786 | llvm::SmallBitVector &Used) { |
| 5787 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 5788 | if (TemplateTemplateParmDecl *TTP |
| 5789 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 5790 | if (TTP->getDepth() == Depth) |
| 5791 | Used[TTP->getIndex()] = true; |
| 5792 | } |
| 5793 | return; |
| 5794 | } |
| 5795 | |
| 5796 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
| 5797 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
| 5798 | Depth, Used); |
| 5799 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
| 5800 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
| 5801 | Depth, Used); |
| 5802 | } |
| 5803 | |
| 5804 | |
| 5805 | |
| 5806 | static void |
| 5807 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| 5808 | bool OnlyDeduced, |
| 5809 | unsigned Depth, |
| 5810 | llvm::SmallBitVector &Used) { |
| 5811 | if (T.isNull()) |
| 5812 | return; |
| 5813 | |
| 5814 | |
| 5815 | if (!T->isDependentType()) |
| 5816 | return; |
| 5817 | |
| 5818 | T = Ctx.getCanonicalType(T); |
| 5819 | switch (T->getTypeClass()) { |
| 5820 | case Type::Pointer: |
| 5821 | MarkUsedTemplateParameters(Ctx, |
| 5822 | cast<PointerType>(T)->getPointeeType(), |
| 5823 | OnlyDeduced, |
| 5824 | Depth, |
| 5825 | Used); |
| 5826 | break; |
| 5827 | |
| 5828 | case Type::BlockPointer: |
| 5829 | MarkUsedTemplateParameters(Ctx, |
| 5830 | cast<BlockPointerType>(T)->getPointeeType(), |
| 5831 | OnlyDeduced, |
| 5832 | Depth, |
| 5833 | Used); |
| 5834 | break; |
| 5835 | |
| 5836 | case Type::LValueReference: |
| 5837 | case Type::RValueReference: |
| 5838 | MarkUsedTemplateParameters(Ctx, |
| 5839 | cast<ReferenceType>(T)->getPointeeType(), |
| 5840 | OnlyDeduced, |
| 5841 | Depth, |
| 5842 | Used); |
| 5843 | break; |
| 5844 | |
| 5845 | case Type::MemberPointer: { |
| 5846 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
| 5847 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
| 5848 | Depth, Used); |
| 5849 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
| 5850 | OnlyDeduced, Depth, Used); |
| 5851 | break; |
| 5852 | } |
| 5853 | |
| 5854 | case Type::DependentSizedArray: |
| 5855 | MarkUsedTemplateParameters(Ctx, |
| 5856 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
| 5857 | OnlyDeduced, Depth, Used); |
| 5858 | |
| 5859 | LLVM_FALLTHROUGH; |
| 5860 | |
| 5861 | case Type::ConstantArray: |
| 5862 | case Type::IncompleteArray: |
| 5863 | MarkUsedTemplateParameters(Ctx, |
| 5864 | cast<ArrayType>(T)->getElementType(), |
| 5865 | OnlyDeduced, Depth, Used); |
| 5866 | break; |
| 5867 | |
| 5868 | case Type::Vector: |
| 5869 | case Type::ExtVector: |
| 5870 | MarkUsedTemplateParameters(Ctx, |
| 5871 | cast<VectorType>(T)->getElementType(), |
| 5872 | OnlyDeduced, Depth, Used); |
| 5873 | break; |
| 5874 | |
| 5875 | case Type::DependentVector: { |
| 5876 | const auto *VecType = cast<DependentVectorType>(T); |
| 5877 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
| 5878 | Depth, Used); |
| 5879 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, Depth, |
| 5880 | Used); |
| 5881 | break; |
| 5882 | } |
| 5883 | case Type::DependentSizedExtVector: { |
| 5884 | const DependentSizedExtVectorType *VecType |
| 5885 | = cast<DependentSizedExtVectorType>(T); |
| 5886 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
| 5887 | Depth, Used); |
| 5888 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
| 5889 | Depth, Used); |
| 5890 | break; |
| 5891 | } |
| 5892 | |
| 5893 | case Type::DependentAddressSpace: { |
| 5894 | const DependentAddressSpaceType *DependentASType = |
| 5895 | cast<DependentAddressSpaceType>(T); |
| 5896 | MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), |
| 5897 | OnlyDeduced, Depth, Used); |
| 5898 | MarkUsedTemplateParameters(Ctx, |
| 5899 | DependentASType->getAddrSpaceExpr(), |
| 5900 | OnlyDeduced, Depth, Used); |
| 5901 | break; |
| 5902 | } |
| 5903 | |
| 5904 | case Type::ConstantMatrix: { |
| 5905 | const ConstantMatrixType *MatType = cast<ConstantMatrixType>(T); |
| 5906 | MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, |
| 5907 | Depth, Used); |
| 5908 | break; |
| 5909 | } |
| 5910 | |
| 5911 | case Type::DependentSizedMatrix: { |
| 5912 | const DependentSizedMatrixType *MatType = cast<DependentSizedMatrixType>(T); |
| 5913 | MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, |
| 5914 | Depth, Used); |
| 5915 | MarkUsedTemplateParameters(Ctx, MatType->getRowExpr(), OnlyDeduced, Depth, |
| 5916 | Used); |
| 5917 | MarkUsedTemplateParameters(Ctx, MatType->getColumnExpr(), OnlyDeduced, |
| 5918 | Depth, Used); |
| 5919 | break; |
| 5920 | } |
| 5921 | |
| 5922 | case Type::FunctionProto: { |
| 5923 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 5924 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 5925 | Used); |
| 5926 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) { |
| 5927 | |
| 5928 | |
| 5929 | |
| 5930 | |
| 5931 | if (!OnlyDeduced || I + 1 == N || |
| 5932 | !Proto->getParamType(I)->getAs<PackExpansionType>()) { |
| 5933 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
| 5934 | Depth, Used); |
| 5935 | } else { |
| 5936 | |
| 5937 | |
| 5938 | |
| 5939 | |
| 5940 | |
| 5941 | |
| 5942 | } |
| 5943 | } |
| 5944 | if (auto *E = Proto->getNoexceptExpr()) |
| 5945 | MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); |
| 5946 | break; |
| 5947 | } |
| 5948 | |
| 5949 | case Type::TemplateTypeParm: { |
| 5950 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 5951 | if (TTP->getDepth() == Depth) |
| 5952 | Used[TTP->getIndex()] = true; |
| 5953 | break; |
| 5954 | } |
| 5955 | |
| 5956 | case Type::SubstTemplateTypeParmPack: { |
| 5957 | const SubstTemplateTypeParmPackType *Subst |
| 5958 | = cast<SubstTemplateTypeParmPackType>(T); |
| 5959 | MarkUsedTemplateParameters(Ctx, |
| 5960 | QualType(Subst->getReplacedParameter(), 0), |
| 5961 | OnlyDeduced, Depth, Used); |
| 5962 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
| 5963 | OnlyDeduced, Depth, Used); |
| 5964 | break; |
| 5965 | } |
| 5966 | |
| 5967 | case Type::InjectedClassName: |
| 5968 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 5969 | LLVM_FALLTHROUGH; |
| 5970 | |
| 5971 | case Type::TemplateSpecialization: { |
| 5972 | const TemplateSpecializationType *Spec |
| 5973 | = cast<TemplateSpecializationType>(T); |
| 5974 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
| 5975 | Depth, Used); |
| 5976 | |
| 5977 | |
| 5978 | |
| 5979 | |
| 5980 | |
| 5981 | if (OnlyDeduced && |
| 5982 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
| 5983 | break; |
| 5984 | |
| 5985 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 5986 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
| 5987 | Used); |
| 5988 | break; |
| 5989 | } |
| 5990 | |
| 5991 | case Type::Complex: |
| 5992 | if (!OnlyDeduced) |
| 5993 | MarkUsedTemplateParameters(Ctx, |
| 5994 | cast<ComplexType>(T)->getElementType(), |
| 5995 | OnlyDeduced, Depth, Used); |
| 5996 | break; |
| 5997 | |
| 5998 | case Type::Atomic: |
| 5999 | if (!OnlyDeduced) |
| 6000 | MarkUsedTemplateParameters(Ctx, |
| 6001 | cast<AtomicType>(T)->getValueType(), |
| 6002 | OnlyDeduced, Depth, Used); |
| 6003 | break; |
| 6004 | |
| 6005 | case Type::DependentName: |
| 6006 | if (!OnlyDeduced) |
| 6007 | MarkUsedTemplateParameters(Ctx, |
| 6008 | cast<DependentNameType>(T)->getQualifier(), |
| 6009 | OnlyDeduced, Depth, Used); |
| 6010 | break; |
| 6011 | |
| 6012 | case Type::DependentTemplateSpecialization: { |
| 6013 | |
| 6014 | |
| 6015 | |
| 6016 | |
| 6017 | |
| 6018 | |
| 6019 | |
| 6020 | |
| 6021 | |
| 6022 | if (OnlyDeduced) |
| 6023 | break; |
| 6024 | |
| 6025 | const DependentTemplateSpecializationType *Spec |
| 6026 | = cast<DependentTemplateSpecializationType>(T); |
| 6027 | |
| 6028 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 6029 | OnlyDeduced, Depth, Used); |
| 6030 | |
| 6031 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 6032 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
| 6033 | Used); |
| 6034 | break; |
| 6035 | } |
| 6036 | |
| 6037 | case Type::TypeOf: |
| 6038 | if (!OnlyDeduced) |
| 6039 | MarkUsedTemplateParameters(Ctx, |
| 6040 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 6041 | OnlyDeduced, Depth, Used); |
| 6042 | break; |
| 6043 | |
| 6044 | case Type::TypeOfExpr: |
| 6045 | if (!OnlyDeduced) |
| 6046 | MarkUsedTemplateParameters(Ctx, |
| 6047 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 6048 | OnlyDeduced, Depth, Used); |
| 6049 | break; |
| 6050 | |
| 6051 | case Type::Decltype: |
| 6052 | if (!OnlyDeduced) |
| 6053 | MarkUsedTemplateParameters(Ctx, |
| 6054 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 6055 | OnlyDeduced, Depth, Used); |
| 6056 | break; |
| 6057 | |
| 6058 | case Type::UnaryTransform: |
| 6059 | if (!OnlyDeduced) |
| 6060 | MarkUsedTemplateParameters(Ctx, |
| 6061 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
| 6062 | OnlyDeduced, Depth, Used); |
| 6063 | break; |
| 6064 | |
| 6065 | case Type::PackExpansion: |
| 6066 | MarkUsedTemplateParameters(Ctx, |
| 6067 | cast<PackExpansionType>(T)->getPattern(), |
| 6068 | OnlyDeduced, Depth, Used); |
| 6069 | break; |
| 6070 | |
| 6071 | case Type::Auto: |
| 6072 | case Type::DeducedTemplateSpecialization: |
| 6073 | MarkUsedTemplateParameters(Ctx, |
| 6074 | cast<DeducedType>(T)->getDeducedType(), |
| 6075 | OnlyDeduced, Depth, Used); |
| 6076 | break; |
| 6077 | case Type::DependentExtInt: |
| 6078 | MarkUsedTemplateParameters(Ctx, |
| 6079 | cast<DependentExtIntType>(T)->getNumBitsExpr(), |
| 6080 | OnlyDeduced, Depth, Used); |
| 6081 | break; |
| 6082 | |
| 6083 | |
| 6084 | case Type::Builtin: |
| 6085 | case Type::VariableArray: |
| 6086 | case Type::FunctionNoProto: |
| 6087 | case Type::Record: |
| 6088 | case Type::Enum: |
| 6089 | case Type::ObjCInterface: |
| 6090 | case Type::ObjCObject: |
| 6091 | case Type::ObjCObjectPointer: |
| 6092 | case Type::UnresolvedUsing: |
| 6093 | case Type::Pipe: |
| 6094 | case Type::ExtInt: |
| 6095 | #define TYPE(Class, Base) |
| 6096 | #define ABSTRACT_TYPE(Class, Base) |
| 6097 | #define DEPENDENT_TYPE(Class, Base) |
| 6098 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 6099 | #include "clang/AST/TypeNodes.inc" |
| 6100 | break; |
| 6101 | } |
| 6102 | } |
| 6103 | |
| 6104 | |
| 6105 | |
| 6106 | static void |
| 6107 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| 6108 | const TemplateArgument &TemplateArg, |
| 6109 | bool OnlyDeduced, |
| 6110 | unsigned Depth, |
| 6111 | llvm::SmallBitVector &Used) { |
| 6112 | switch (TemplateArg.getKind()) { |
| 6113 | case TemplateArgument::Null: |
| 6114 | case TemplateArgument::Integral: |
| 6115 | case TemplateArgument::Declaration: |
| 6116 | break; |
| 6117 | |
| 6118 | case TemplateArgument::NullPtr: |
| 6119 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 6120 | Depth, Used); |
| 6121 | break; |
| 6122 | |
| 6123 | case TemplateArgument::Type: |
| 6124 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
| 6125 | Depth, Used); |
| 6126 | break; |
| 6127 | |
| 6128 | case TemplateArgument::Template: |
| 6129 | case TemplateArgument::TemplateExpansion: |
| 6130 | MarkUsedTemplateParameters(Ctx, |
| 6131 | TemplateArg.getAsTemplateOrTemplatePattern(), |
| 6132 | OnlyDeduced, Depth, Used); |
| 6133 | break; |
| 6134 | |
| 6135 | case TemplateArgument::Expression: |
| 6136 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
| 6137 | Depth, Used); |
| 6138 | break; |
| 6139 | |
| 6140 | case TemplateArgument::Pack: |
| 6141 | for (const auto &P : TemplateArg.pack_elements()) |
| 6142 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
| 6143 | break; |
| 6144 | } |
| 6145 | } |
| 6146 | |
| 6147 | |
| 6148 | |
| 6149 | |
| 6150 | |
| 6151 | |
| 6152 | |
| 6153 | |
| 6154 | void |
| 6155 | Sema::MarkUsedTemplateParameters(const Expr *E, bool OnlyDeduced, |
| 6156 | unsigned Depth, |
| 6157 | llvm::SmallBitVector &Used) { |
| 6158 | ::MarkUsedTemplateParameters(Context, E, OnlyDeduced, Depth, Used); |
| 6159 | } |
| 6160 | |
| 6161 | |
| 6162 | |
| 6163 | |
| 6164 | |
| 6165 | |
| 6166 | |
| 6167 | |
| 6168 | |
| 6169 | |
| 6170 | void |
| 6171 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 6172 | bool OnlyDeduced, unsigned Depth, |
| 6173 | llvm::SmallBitVector &Used) { |
| 6174 | |
| 6175 | |
| 6176 | |
| 6177 | |
| 6178 | if (OnlyDeduced && |
| 6179 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
| 6180 | return; |
| 6181 | |
| 6182 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6183 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
| 6184 | Depth, Used); |
| 6185 | } |
| 6186 | |
| 6187 | |
| 6188 | |
| 6189 | void Sema::MarkDeducedTemplateParameters( |
| 6190 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 6191 | llvm::SmallBitVector &Deduced) { |
| 6192 | TemplateParameterList *TemplateParams |
| 6193 | = FunctionTemplate->getTemplateParameters(); |
| 6194 | Deduced.clear(); |
| 6195 | Deduced.resize(TemplateParams->size()); |
| 6196 | |
| 6197 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 6198 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
| 6199 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
| 6200 | true, TemplateParams->getDepth(), Deduced); |
| 6201 | } |
| 6202 | |
| 6203 | bool hasDeducibleTemplateParameters(Sema &S, |
| 6204 | FunctionTemplateDecl *FunctionTemplate, |
| 6205 | QualType T) { |
| 6206 | if (!T->isDependentType()) |
| 6207 | return false; |
| 6208 | |
| 6209 | TemplateParameterList *TemplateParams |
| 6210 | = FunctionTemplate->getTemplateParameters(); |
| 6211 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
| 6212 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
| 6213 | Deduced); |
| 6214 | |
| 6215 | return Deduced.any(); |
| 6216 | } |