| 1 |
//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
//===----------------------------------------------------------------------===/ |
| 7 |
// |
| 8 |
// This file implements C++ template instantiation. |
| 9 |
// |
| 10 |
//===----------------------------------------------------------------------===/ |
| 11 |
|
| 12 |
#include "TreeTransform.h" |
| 13 |
#include "clang/AST/ASTConcept.h" |
| 14 |
#include "clang/AST/ASTConsumer.h" |
| 15 |
#include "clang/AST/ASTContext.h" |
| 16 |
#include "clang/AST/ASTLambda.h" |
| 17 |
#include "clang/AST/ASTMutationListener.h" |
| 18 |
#include "clang/AST/DeclBase.h" |
| 19 |
#include "clang/AST/DeclTemplate.h" |
| 20 |
#include "clang/AST/DynamicRecursiveASTVisitor.h" |
| 21 |
#include "clang/AST/Expr.h" |
| 22 |
#include "clang/AST/ExprConcepts.h" |
| 23 |
#include "clang/AST/PrettyDeclStackTrace.h" |
| 24 |
#include "clang/AST/Type.h" |
| 25 |
#include "clang/AST/TypeLoc.h" |
| 26 |
#include "clang/AST/TypeVisitor.h" |
| 27 |
#include "clang/Basic/LangOptions.h" |
| 28 |
#include "clang/Basic/TargetInfo.h" |
| 29 |
#include "clang/Sema/DeclSpec.h" |
| 30 |
#include "clang/Sema/EnterExpressionEvaluationContext.h" |
| 31 |
#include "clang/Sema/Initialization.h" |
| 32 |
#include "clang/Sema/Sema.h" |
| 33 |
#include "clang/Sema/SemaConcept.h" |
| 34 |
#include "clang/Sema/SemaInternal.h" |
| 35 |
#include "clang/Sema/Template.h" |
| 36 |
#include "clang/Sema/TemplateDeduction.h" |
| 37 |
#include "clang/Sema/TemplateInstCallback.h" |
| 38 |
#include "llvm/ADT/SmallVectorExtras.h" |
| 39 |
#include "llvm/ADT/StringExtras.h" |
| 40 |
#include "llvm/Support/ErrorHandling.h" |
| 41 |
#include "llvm/Support/SaveAndRestore.h" |
| 42 |
#include "llvm/Support/TimeProfiler.h" |
| 43 |
#include <optional> |
| 44 |
|
| 45 |
using namespace clang; |
| 46 |
using namespace sema; |
| 47 |
|
| 48 |
//===----------------------------------------------------------------------===/ |
| 49 |
// Template Instantiation Support |
| 50 |
//===----------------------------------------------------------------------===/ |
| 51 |
|
| 52 |
namespace { |
| 53 |
namespace TemplateInstArgsHelpers { |
| 54 |
struct Response { |
| 55 |
const Decl *NextDecl = nullptr; |
| 56 |
bool IsDone = false; |
| 57 |
bool ClearRelativeToPrimary = true; |
| 58 |
static Response Done() { |
| 59 |
Response R; |
| 60 |
R.IsDone = true; |
| 61 |
return R; |
| 62 |
} |
| 63 |
static Response ChangeDecl(const Decl *ND) { |
| 64 |
Response R; |
| 65 |
R.NextDecl = ND; |
| 66 |
return R; |
| 67 |
} |
| 68 |
static Response ChangeDecl(const DeclContext *Ctx) { |
| 69 |
Response R; |
| 70 |
R.NextDecl = Decl::castFromDeclContext(Ctx); |
| 71 |
return R; |
| 72 |
} |
| 73 |
|
| 74 |
static Response UseNextDecl(const Decl *CurDecl) { |
| 75 |
return ChangeDecl(CurDecl->getDeclContext()); |
| 76 |
} |
| 77 |
|
| 78 |
static Response DontClearRelativeToPrimaryNextDecl(const Decl *CurDecl) { |
| 79 |
Response R = Response::UseNextDecl(CurDecl); |
| 80 |
R.ClearRelativeToPrimary = false; |
| 81 |
return R; |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
// Retrieve the primary template for a lambda call operator. It's |
| 86 |
// unfortunate that we only have the mappings of call operators rather |
| 87 |
// than lambda classes. |
| 88 |
const FunctionDecl * |
| 89 |
getPrimaryTemplateOfGenericLambda(const FunctionDecl *LambdaCallOperator) { |
| 90 |
if (!isLambdaCallOperator(LambdaCallOperator)) |
| 91 |
return LambdaCallOperator; |
| 92 |
while (true) { |
| 93 |
if (auto *FTD = dyn_cast_if_present<FunctionTemplateDecl>( |
| 94 |
LambdaCallOperator->getDescribedTemplate()); |
| 95 |
FTD && FTD->getInstantiatedFromMemberTemplate()) { |
| 96 |
LambdaCallOperator = |
| 97 |
FTD->getInstantiatedFromMemberTemplate()->getTemplatedDecl(); |
| 98 |
} else if (LambdaCallOperator->getPrimaryTemplate()) { |
| 99 |
// Cases where the lambda operator is instantiated in |
| 100 |
// TemplateDeclInstantiator::VisitCXXMethodDecl. |
| 101 |
LambdaCallOperator = |
| 102 |
LambdaCallOperator->getPrimaryTemplate()->getTemplatedDecl(); |
| 103 |
} else if (auto *Prev = cast<CXXMethodDecl>(LambdaCallOperator) |
| 104 |
->getInstantiatedFromMemberFunction()) |
| 105 |
LambdaCallOperator = Prev; |
| 106 |
else |
| 107 |
break; |
| 108 |
} |
| 109 |
return LambdaCallOperator; |
| 110 |
} |
| 111 |
|
| 112 |
struct EnclosingTypeAliasTemplateDetails { |
| 113 |
TypeAliasTemplateDecl *Template = nullptr; |
| 114 |
TypeAliasTemplateDecl *PrimaryTypeAliasDecl = nullptr; |
| 115 |
ArrayRef<TemplateArgument> AssociatedTemplateArguments; |
| 116 |
|
| 117 |
explicit operator bool() noexcept { return Template; } |
| 118 |
}; |
| 119 |
|
| 120 |
// Find the enclosing type alias template Decl from CodeSynthesisContexts, as |
| 121 |
// well as its primary template and instantiating template arguments. |
| 122 |
EnclosingTypeAliasTemplateDetails |
| 123 |
getEnclosingTypeAliasTemplateDecl(Sema &SemaRef) { |
| 124 |
for (auto &CSC : llvm::reverse(SemaRef.CodeSynthesisContexts)) { |
| 125 |
if (CSC.Kind != Sema::CodeSynthesisContext::SynthesisKind:: |
| 126 |
TypeAliasTemplateInstantiation) |
| 127 |
continue; |
| 128 |
EnclosingTypeAliasTemplateDetails Result; |
| 129 |
auto *TATD = cast<TypeAliasTemplateDecl>(CSC.Entity), |
| 130 |
*Next = TATD->getInstantiatedFromMemberTemplate(); |
| 131 |
Result = { |
| 132 |
/*Template=*/TATD, |
| 133 |
/*PrimaryTypeAliasDecl=*/TATD, |
| 134 |
/*AssociatedTemplateArguments=*/CSC.template_arguments(), |
| 135 |
}; |
| 136 |
while (Next) { |
| 137 |
Result.PrimaryTypeAliasDecl = Next; |
| 138 |
Next = Next->getInstantiatedFromMemberTemplate(); |
| 139 |
} |
| 140 |
return Result; |
| 141 |
} |
| 142 |
return {}; |
| 143 |
} |
| 144 |
|
| 145 |
// Check if we are currently inside of a lambda expression that is |
| 146 |
// surrounded by a using alias declaration. e.g. |
| 147 |
// template <class> using type = decltype([](auto) { ^ }()); |
| 148 |
// We have to do so since a TypeAliasTemplateDecl (or a TypeAliasDecl) is never |
| 149 |
// a DeclContext, nor does it have an associated specialization Decl from which |
| 150 |
// we could collect these template arguments. |
| 151 |
bool isLambdaEnclosedByTypeAliasDecl( |
| 152 |
const FunctionDecl *LambdaCallOperator, |
| 153 |
const TypeAliasTemplateDecl *PrimaryTypeAliasDecl) { |
| 154 |
struct Visitor : DynamicRecursiveASTVisitor { |
| 155 |
Visitor(const FunctionDecl *CallOperator) : CallOperator(CallOperator) {} |
| 156 |
bool VisitLambdaExpr(LambdaExpr *LE) override { |
| 157 |
// Return true to bail out of the traversal, implying the Decl contains |
| 158 |
// the lambda. |
| 159 |
return getPrimaryTemplateOfGenericLambda(LE->getCallOperator()) != |
| 160 |
CallOperator; |
| 161 |
} |
| 162 |
const FunctionDecl *CallOperator; |
| 163 |
}; |
| 164 |
|
| 165 |
QualType Underlying = |
| 166 |
PrimaryTypeAliasDecl->getTemplatedDecl()->getUnderlyingType(); |
| 167 |
|
| 168 |
return !Visitor(getPrimaryTemplateOfGenericLambda(LambdaCallOperator)) |
| 169 |
.TraverseType(Underlying); |
| 170 |
} |
| 171 |
|
| 172 |
// Add template arguments from a variable template instantiation. |
| 173 |
Response |
| 174 |
HandleVarTemplateSpec(const VarTemplateSpecializationDecl *VarTemplSpec, |
| 175 |
MultiLevelTemplateArgumentList &Result, |
| 176 |
bool SkipForSpecialization) { |
| 177 |
// For a class-scope explicit specialization, there are no template arguments |
| 178 |
// at this level, but there may be enclosing template arguments. |
| 179 |
if (VarTemplSpec->isClassScopeExplicitSpecialization()) |
| 180 |
return Response::DontClearRelativeToPrimaryNextDecl(VarTemplSpec); |
| 181 |
|
| 182 |
// We're done when we hit an explicit specialization. |
| 183 |
if (VarTemplSpec->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 184 |
!isa<VarTemplatePartialSpecializationDecl>(VarTemplSpec)) |
| 185 |
return Response::Done(); |
| 186 |
|
| 187 |
// If this variable template specialization was instantiated from a |
| 188 |
// specialized member that is a variable template, we're done. |
| 189 |
assert(VarTemplSpec->getSpecializedTemplate() && "No variable template?"); |
| 190 |
llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> |
| 191 |
Specialized = VarTemplSpec->getSpecializedTemplateOrPartial(); |
| 192 |
if (VarTemplatePartialSpecializationDecl *Partial = |
| 193 |
dyn_cast<VarTemplatePartialSpecializationDecl *>(Specialized)) { |
| 194 |
if (!SkipForSpecialization) |
| 195 |
Result.addOuterTemplateArguments( |
| 196 |
Partial, VarTemplSpec->getTemplateInstantiationArgs().asArray(), |
| 197 |
/*Final=*/false); |
| 198 |
if (Partial->isMemberSpecialization()) |
| 199 |
return Response::Done(); |
| 200 |
} else { |
| 201 |
VarTemplateDecl *Tmpl = cast<VarTemplateDecl *>(Specialized); |
| 202 |
if (!SkipForSpecialization) |
| 203 |
Result.addOuterTemplateArguments( |
| 204 |
Tmpl, VarTemplSpec->getTemplateInstantiationArgs().asArray(), |
| 205 |
/*Final=*/false); |
| 206 |
if (Tmpl->isMemberSpecialization()) |
| 207 |
return Response::Done(); |
| 208 |
} |
| 209 |
return Response::DontClearRelativeToPrimaryNextDecl(VarTemplSpec); |
| 210 |
} |
| 211 |
|
| 212 |
// If we have a template template parameter with translation unit context, |
| 213 |
// then we're performing substitution into a default template argument of |
| 214 |
// this template template parameter before we've constructed the template |
| 215 |
// that will own this template template parameter. In this case, we |
| 216 |
// use empty template parameter lists for all of the outer templates |
| 217 |
// to avoid performing any substitutions. |
| 218 |
Response |
| 219 |
HandleDefaultTempArgIntoTempTempParam(const TemplateTemplateParmDecl *TTP, |
| 220 |
MultiLevelTemplateArgumentList &Result) { |
| 221 |
for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I) |
| 222 |
Result.addOuterTemplateArguments(std::nullopt); |
| 223 |
return Response::Done(); |
| 224 |
} |
| 225 |
|
| 226 |
Response HandlePartialClassTemplateSpec( |
| 227 |
const ClassTemplatePartialSpecializationDecl *PartialClassTemplSpec, |
| 228 |
MultiLevelTemplateArgumentList &Result, bool SkipForSpecialization) { |
| 229 |
if (!SkipForSpecialization) |
| 230 |
Result.addOuterRetainedLevels(PartialClassTemplSpec->getTemplateDepth()); |
| 231 |
return Response::Done(); |
| 232 |
} |
| 233 |
|
| 234 |
// Add template arguments from a class template instantiation. |
| 235 |
Response |
| 236 |
HandleClassTemplateSpec(const ClassTemplateSpecializationDecl *ClassTemplSpec, |
| 237 |
MultiLevelTemplateArgumentList &Result, |
| 238 |
bool SkipForSpecialization) { |
| 239 |
if (!ClassTemplSpec->isClassScopeExplicitSpecialization()) { |
| 240 |
// We're done when we hit an explicit specialization. |
| 241 |
if (ClassTemplSpec->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 242 |
!isa<ClassTemplatePartialSpecializationDecl>(ClassTemplSpec)) |
| 243 |
return Response::Done(); |
| 244 |
|
| 245 |
if (!SkipForSpecialization) |
| 246 |
Result.addOuterTemplateArguments( |
| 247 |
const_cast<ClassTemplateSpecializationDecl *>(ClassTemplSpec), |
| 248 |
ClassTemplSpec->getTemplateInstantiationArgs().asArray(), |
| 249 |
/*Final=*/false); |
| 250 |
|
| 251 |
// If this class template specialization was instantiated from a |
| 252 |
// specialized member that is a class template, we're done. |
| 253 |
assert(ClassTemplSpec->getSpecializedTemplate() && "No class template?"); |
| 254 |
if (ClassTemplSpec->getSpecializedTemplate()->isMemberSpecialization()) |
| 255 |
return Response::Done(); |
| 256 |
|
| 257 |
// If this was instantiated from a partial template specialization, we need |
| 258 |
// to get the next level of declaration context from the partial |
| 259 |
// specialization, as the ClassTemplateSpecializationDecl's |
| 260 |
// DeclContext/LexicalDeclContext will be for the primary template. |
| 261 |
if (auto *InstFromPartialTempl = |
| 262 |
ClassTemplSpec->getSpecializedTemplateOrPartial() |
| 263 |
.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) |
| 264 |
return Response::ChangeDecl( |
| 265 |
InstFromPartialTempl->getLexicalDeclContext()); |
| 266 |
} |
| 267 |
return Response::UseNextDecl(ClassTemplSpec); |
| 268 |
} |
| 269 |
|
| 270 |
Response HandleFunction(Sema &SemaRef, const FunctionDecl *Function, |
| 271 |
MultiLevelTemplateArgumentList &Result, |
| 272 |
const FunctionDecl *Pattern, bool RelativeToPrimary, |
| 273 |
bool ForConstraintInstantiation, |
| 274 |
bool ForDefaultArgumentSubstitution) { |
| 275 |
// Add template arguments from a function template specialization. |
| 276 |
if (!RelativeToPrimary && |
| 277 |
Function->getTemplateSpecializationKindForInstantiation() == |
| 278 |
TSK_ExplicitSpecialization) |
| 279 |
return Response::Done(); |
| 280 |
|
| 281 |
if (!RelativeToPrimary && |
| 282 |
Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 283 |
// This is an implicit instantiation of an explicit specialization. We |
| 284 |
// don't get any template arguments from this function but might get |
| 285 |
// some from an enclosing template. |
| 286 |
return Response::UseNextDecl(Function); |
| 287 |
} else if (const TemplateArgumentList *TemplateArgs = |
| 288 |
Function->getTemplateSpecializationArgs()) { |
| 289 |
// Add the template arguments for this specialization. |
| 290 |
Result.addOuterTemplateArguments(const_cast<FunctionDecl *>(Function), |
| 291 |
TemplateArgs->asArray(), |
| 292 |
/*Final=*/false); |
| 293 |
|
| 294 |
if (RelativeToPrimary && |
| 295 |
(Function->getTemplateSpecializationKind() == |
| 296 |
TSK_ExplicitSpecialization || |
| 297 |
(Function->getFriendObjectKind() && |
| 298 |
!Function->getPrimaryTemplate()->getFriendObjectKind()))) |
| 299 |
return Response::UseNextDecl(Function); |
| 300 |
|
| 301 |
// If this function was instantiated from a specialized member that is |
| 302 |
// a function template, we're done. |
| 303 |
assert(Function->getPrimaryTemplate() && "No function template?"); |
| 304 |
if (!ForDefaultArgumentSubstitution && |
| 305 |
Function->getPrimaryTemplate()->isMemberSpecialization()) |
| 306 |
return Response::Done(); |
| 307 |
|
| 308 |
// If this function is a generic lambda specialization, we are done. |
| 309 |
if (!ForConstraintInstantiation && |
| 310 |
isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) |
| 311 |
return Response::Done(); |
| 312 |
|
| 313 |
} else if (auto *Template = Function->getDescribedFunctionTemplate()) { |
| 314 |
assert( |
| 315 |
(ForConstraintInstantiation || Result.getNumSubstitutedLevels() == 0) && |
| 316 |
"Outer template not instantiated?"); |
| 317 |
if (ForConstraintInstantiation) { |
| 318 |
for (auto &Inst : llvm::reverse(SemaRef.CodeSynthesisContexts)) { |
| 319 |
if (Inst.Kind == Sema::CodeSynthesisContext::ConstraintsCheck && |
| 320 |
Inst.Entity == Template) { |
| 321 |
// After CWG2369, the outer templates are not instantiated when |
| 322 |
// checking its associated constraints. So add them back through the |
| 323 |
// synthesis context; this is useful for e.g. nested constraints |
| 324 |
// involving lambdas. |
| 325 |
Result.addOuterTemplateArguments(Template, Inst.template_arguments(), |
| 326 |
/*Final=*/false); |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
// If this is a friend or local declaration and it declares an entity at |
| 333 |
// namespace scope, take arguments from its lexical parent |
| 334 |
// instead of its semantic parent, unless of course the pattern we're |
| 335 |
// instantiating actually comes from the file's context! |
| 336 |
if ((Function->getFriendObjectKind() || Function->isLocalExternDecl()) && |
| 337 |
Function->getNonTransparentDeclContext()->isFileContext() && |
| 338 |
(!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { |
| 339 |
return Response::ChangeDecl(Function->getLexicalDeclContext()); |
| 340 |
} |
| 341 |
|
| 342 |
if (ForConstraintInstantiation && Function->getFriendObjectKind()) |
| 343 |
return Response::ChangeDecl(Function->getLexicalDeclContext()); |
| 344 |
return Response::UseNextDecl(Function); |
| 345 |
} |
| 346 |
|
| 347 |
Response HandleFunctionTemplateDecl(Sema &SemaRef, |
| 348 |
const FunctionTemplateDecl *FTD, |
| 349 |
MultiLevelTemplateArgumentList &Result) { |
| 350 |
if (!isa<ClassTemplateSpecializationDecl>(FTD->getDeclContext())) { |
| 351 |
Result.addOuterTemplateArguments( |
| 352 |
const_cast<FunctionTemplateDecl *>(FTD), |
| 353 |
const_cast<FunctionTemplateDecl *>(FTD)->getInjectedTemplateArgs( |
| 354 |
SemaRef.Context), |
| 355 |
/*Final=*/false); |
| 356 |
|
| 357 |
NestedNameSpecifier NNS = FTD->getTemplatedDecl()->getQualifier(); |
| 358 |
|
| 359 |
for (const Type *Ty = NNS.getKind() == NestedNameSpecifier::Kind::Type |
| 360 |
? NNS.getAsType() |
| 361 |
: nullptr, |
| 362 |
*NextTy = nullptr; |
| 363 |
Ty && Ty->isInstantiationDependentType(); |
| 364 |
Ty = std::exchange(NextTy, nullptr)) { |
| 365 |
if (NestedNameSpecifier P = Ty->getPrefix(); |
| 366 |
P.getKind() == NestedNameSpecifier::Kind::Type) |
| 367 |
NextTy = P.getAsType(); |
| 368 |
const auto *TSTy = dyn_cast<TemplateSpecializationType>(Ty); |
| 369 |
if (!TSTy) |
| 370 |
continue; |
| 371 |
|
| 372 |
ArrayRef<TemplateArgument> Arguments = TSTy->template_arguments(); |
| 373 |
// Prefer template arguments from the injected-class-type if possible. |
| 374 |
// For example, |
| 375 |
// ```cpp |
| 376 |
// template <class... Pack> struct S { |
| 377 |
// template <class T> void foo(); |
| 378 |
// }; |
| 379 |
// template <class... Pack> template <class T> |
| 380 |
// ^^^^^^^^^^^^^ InjectedTemplateArgs |
| 381 |
// They're of kind TemplateArgument::Pack, not of |
| 382 |
// TemplateArgument::Type. |
| 383 |
// void S<Pack...>::foo() {} |
| 384 |
// ^^^^^^^ |
| 385 |
// TSTy->template_arguments() (which are of PackExpansionType) |
| 386 |
// ``` |
| 387 |
// This meets the contract in |
| 388 |
// TreeTransform::TryExpandParameterPacks that the template arguments |
| 389 |
// for unexpanded parameters should be of a Pack kind. |
| 390 |
if (TSTy->isCurrentInstantiation()) { |
| 391 |
auto *RD = TSTy->getCanonicalTypeInternal()->getAsCXXRecordDecl(); |
| 392 |
if (ClassTemplateDecl *CTD = RD->getDescribedClassTemplate()) |
| 393 |
Arguments = CTD->getInjectedTemplateArgs(SemaRef.Context); |
| 394 |
else if (auto *Specialization = |
| 395 |
dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
| 396 |
Arguments = Specialization->getTemplateInstantiationArgs().asArray(); |
| 397 |
} |
| 398 |
Result.addOuterTemplateArguments( |
| 399 |
TSTy->getTemplateName().getAsTemplateDecl(), Arguments, |
| 400 |
/*Final=*/false); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
return Response::ChangeDecl(FTD->getLexicalDeclContext()); |
| 405 |
} |
| 406 |
|
| 407 |
Response HandleRecordDecl(Sema &SemaRef, const CXXRecordDecl *Rec, |
| 408 |
MultiLevelTemplateArgumentList &Result, |
| 409 |
ASTContext &Context, |
| 410 |
bool ForConstraintInstantiation) { |
| 411 |
if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) { |
| 412 |
assert( |
| 413 |
(ForConstraintInstantiation || Result.getNumSubstitutedLevels() == 0) && |
| 414 |
"Outer template not instantiated?"); |
| 415 |
if (ClassTemplate->isMemberSpecialization()) |
| 416 |
return Response::Done(); |
| 417 |
if (ForConstraintInstantiation) |
| 418 |
Result.addOuterTemplateArguments( |
| 419 |
const_cast<CXXRecordDecl *>(Rec), |
| 420 |
ClassTemplate->getInjectedTemplateArgs(SemaRef.Context), |
| 421 |
/*Final=*/false); |
| 422 |
} |
| 423 |
|
| 424 |
if (const MemberSpecializationInfo *MSInfo = |
| 425 |
Rec->getMemberSpecializationInfo()) |
| 426 |
if (MSInfo->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 427 |
return Response::Done(); |
| 428 |
|
| 429 |
bool IsFriend = Rec->getFriendObjectKind() || |
| 430 |
(Rec->getDescribedClassTemplate() && |
| 431 |
Rec->getDescribedClassTemplate()->getFriendObjectKind()); |
| 432 |
if (ForConstraintInstantiation && IsFriend && |
| 433 |
Rec->getNonTransparentDeclContext()->isFileContext()) { |
| 434 |
return Response::ChangeDecl(Rec->getLexicalDeclContext()); |
| 435 |
} |
| 436 |
|
| 437 |
// This is to make sure we pick up the VarTemplateSpecializationDecl or the |
| 438 |
// TypeAliasTemplateDecl that this lambda is defined inside of. |
| 439 |
if (Rec->isLambda()) { |
| 440 |
if (const Decl *LCD = Rec->getLambdaContextDecl()) |
| 441 |
return Response::ChangeDecl(LCD); |
| 442 |
// Retrieve the template arguments for a using alias declaration. |
| 443 |
// This is necessary for constraint checking, since we always keep |
| 444 |
// constraints relative to the primary template. |
| 445 |
if (auto TypeAlias = getEnclosingTypeAliasTemplateDecl(SemaRef); |
| 446 |
ForConstraintInstantiation && TypeAlias) { |
| 447 |
if (isLambdaEnclosedByTypeAliasDecl(Rec->getLambdaCallOperator(), |
| 448 |
TypeAlias.PrimaryTypeAliasDecl)) { |
| 449 |
Result.addOuterTemplateArguments(TypeAlias.Template, |
| 450 |
TypeAlias.AssociatedTemplateArguments, |
| 451 |
/*Final=*/false); |
| 452 |
// Visit the parent of the current type alias declaration rather than |
| 453 |
// the lambda thereof. |
| 454 |
// E.g., in the following example: |
| 455 |
// struct S { |
| 456 |
// template <class> using T = decltype([]<Concept> {} ()); |
| 457 |
// }; |
| 458 |
// void foo() { |
| 459 |
// S::T var; |
| 460 |
// } |
| 461 |
// The instantiated lambda expression (which we're visiting at 'var') |
| 462 |
// has a function DeclContext 'foo' rather than the Record DeclContext |
| 463 |
// S. This seems to be an oversight to me that we may want to set a |
| 464 |
// Sema Context from the CXXScopeSpec before substituting into T. |
| 465 |
return Response::ChangeDecl(TypeAlias.Template->getDeclContext()); |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return Response::UseNextDecl(Rec); |
| 471 |
} |
| 472 |
|
| 473 |
Response HandleImplicitConceptSpecializationDecl( |
| 474 |
const ImplicitConceptSpecializationDecl *CSD, |
| 475 |
MultiLevelTemplateArgumentList &Result) { |
| 476 |
Result.addOuterTemplateArguments( |
| 477 |
const_cast<ImplicitConceptSpecializationDecl *>(CSD), |
| 478 |
CSD->getTemplateArguments(), |
| 479 |
/*Final=*/false); |
| 480 |
return Response::UseNextDecl(CSD); |
| 481 |
} |
| 482 |
|
| 483 |
Response HandleGenericDeclContext(const Decl *CurDecl) { |
| 484 |
return Response::UseNextDecl(CurDecl); |
| 485 |
} |
| 486 |
} // namespace TemplateInstArgsHelpers |
| 487 |
} // namespace |
| 488 |
|
| 489 |
MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs( |
| 490 |
const NamedDecl *ND, const DeclContext *DC, bool Final, |
| 491 |
std::optional<ArrayRef<TemplateArgument>> Innermost, bool RelativeToPrimary, |
| 492 |
const FunctionDecl *Pattern, bool ForConstraintInstantiation, |
| 493 |
bool SkipForSpecialization, bool ForDefaultArgumentSubstitution) { |
| 494 |
assert((ND || DC) && "Can't find arguments for a decl if one isn't provided"); |
| 495 |
// Accumulate the set of template argument lists in this structure. |
| 496 |
MultiLevelTemplateArgumentList Result; |
| 497 |
|
| 498 |
using namespace TemplateInstArgsHelpers; |
| 499 |
const Decl *CurDecl = ND; |
| 500 |
|
| 501 |
if (Innermost) { |
| 502 |
Result.addOuterTemplateArguments(const_cast<NamedDecl *>(ND), *Innermost, |
| 503 |
Final); |
| 504 |
// Populate placeholder template arguments for TemplateTemplateParmDecls. |
| 505 |
// This is essential for the case e.g. |
| 506 |
// |
| 507 |
// template <class> concept Concept = false; |
| 508 |
// template <template <Concept C> class T> void foo(T<int>) |
| 509 |
// |
| 510 |
// where parameter C has a depth of 1 but the substituting argument `int` |
| 511 |
// has a depth of 0. |
| 512 |
if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl)) |
| 513 |
HandleDefaultTempArgIntoTempTempParam(TTP, Result); |
| 514 |
CurDecl = DC ? Decl::castFromDeclContext(DC) |
| 515 |
: Response::UseNextDecl(CurDecl).NextDecl; |
| 516 |
} else if (!CurDecl) |
| 517 |
CurDecl = Decl::castFromDeclContext(DC); |
| 518 |
|
| 519 |
while (!CurDecl->isFileContextDecl()) { |
| 520 |
Response R; |
| 521 |
if (const auto *VarTemplSpec = |
| 522 |
dyn_cast<VarTemplateSpecializationDecl>(CurDecl)) { |
| 523 |
R = HandleVarTemplateSpec(VarTemplSpec, Result, SkipForSpecialization); |
| 524 |
} else if (const auto *PartialClassTemplSpec = |
| 525 |
dyn_cast<ClassTemplatePartialSpecializationDecl>(CurDecl)) { |
| 526 |
R = HandlePartialClassTemplateSpec(PartialClassTemplSpec, Result, |
| 527 |
SkipForSpecialization); |
| 528 |
} else if (const auto *ClassTemplSpec = |
| 529 |
dyn_cast<ClassTemplateSpecializationDecl>(CurDecl)) { |
| 530 |
R = HandleClassTemplateSpec(ClassTemplSpec, Result, |
| 531 |
SkipForSpecialization); |
| 532 |
} else if (const auto *Function = dyn_cast<FunctionDecl>(CurDecl)) { |
| 533 |
R = HandleFunction(*this, Function, Result, Pattern, RelativeToPrimary, |
| 534 |
ForConstraintInstantiation, |
| 535 |
ForDefaultArgumentSubstitution); |
| 536 |
} else if (const auto *Rec = dyn_cast<CXXRecordDecl>(CurDecl)) { |
| 537 |
R = HandleRecordDecl(*this, Rec, Result, Context, |
| 538 |
ForConstraintInstantiation); |
| 539 |
} else if (const auto *CSD = |
| 540 |
dyn_cast<ImplicitConceptSpecializationDecl>(CurDecl)) { |
| 541 |
R = HandleImplicitConceptSpecializationDecl(CSD, Result); |
| 542 |
} else if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(CurDecl)) { |
| 543 |
R = HandleFunctionTemplateDecl(*this, FTD, Result); |
| 544 |
} else if (const auto *CTD = dyn_cast<ClassTemplateDecl>(CurDecl)) { |
| 545 |
R = Response::ChangeDecl(CTD->getLexicalDeclContext()); |
| 546 |
} else if (!isa<DeclContext>(CurDecl)) { |
| 547 |
R = Response::DontClearRelativeToPrimaryNextDecl(CurDecl); |
| 548 |
if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl)) { |
| 549 |
R = HandleDefaultTempArgIntoTempTempParam(TTP, Result); |
| 550 |
} |
| 551 |
} else { |
| 552 |
R = HandleGenericDeclContext(CurDecl); |
| 553 |
} |
| 554 |
|
| 555 |
if (R.IsDone) |
| 556 |
return Result; |
| 557 |
if (R.ClearRelativeToPrimary) |
| 558 |
RelativeToPrimary = false; |
| 559 |
assert(R.NextDecl); |
| 560 |
CurDecl = R.NextDecl; |
| 561 |
} |
| 562 |
return Result; |
| 563 |
} |
| 564 |
|
| 565 |
bool Sema::CodeSynthesisContext::isInstantiationRecord() const { |
| 566 |
switch (Kind) { |
| 567 |
case TemplateInstantiation: |
| 568 |
case ExceptionSpecInstantiation: |
| 569 |
case DefaultTemplateArgumentInstantiation: |
| 570 |
case DefaultFunctionArgumentInstantiation: |
| 571 |
case ExplicitTemplateArgumentSubstitution: |
| 572 |
case DeducedTemplateArgumentSubstitution: |
| 573 |
case PriorTemplateArgumentSubstitution: |
| 574 |
case ConstraintsCheck: |
| 575 |
case NestedRequirementConstraintsCheck: |
| 576 |
return true; |
| 577 |
|
| 578 |
case RequirementInstantiation: |
| 579 |
case RequirementParameterInstantiation: |
| 580 |
case DefaultTemplateArgumentChecking: |
| 581 |
case DeclaringSpecialMember: |
| 582 |
case DeclaringImplicitEqualityComparison: |
| 583 |
case DefiningSynthesizedFunction: |
| 584 |
case ExceptionSpecEvaluation: |
| 585 |
case ConstraintSubstitution: |
| 586 |
case ParameterMappingSubstitution: |
| 587 |
case ConstraintNormalization: |
| 588 |
case RewritingOperatorAsSpaceship: |
| 589 |
case InitializingStructuredBinding: |
| 590 |
case MarkingClassDllexported: |
| 591 |
case BuildingBuiltinDumpStructCall: |
| 592 |
case LambdaExpressionSubstitution: |
| 593 |
case BuildingDeductionGuides: |
| 594 |
case TypeAliasTemplateInstantiation: |
| 595 |
case PartialOrderingTTP: |
| 596 |
return false; |
| 597 |
|
| 598 |
// This function should never be called when Kind's value is Memoization. |
| 599 |
case Memoization: |
| 600 |
break; |
| 601 |
} |
| 602 |
|
| 603 |
llvm_unreachable("Invalid SynthesisKind!"); |
| 604 |
} |
| 605 |
|
| 606 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 607 |
Sema &SemaRef, CodeSynthesisContext::SynthesisKind Kind, |
| 608 |
SourceLocation PointOfInstantiation, SourceRange InstantiationRange, |
| 609 |
Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs) |
| 610 |
: SemaRef(SemaRef) { |
| 611 |
// Don't allow further instantiation if a fatal error and an uncompilable |
| 612 |
// error have occurred. Any diagnostics we might have raised will not be |
| 613 |
// visible, and we do not need to construct a correct AST. |
| 614 |
if (SemaRef.Diags.hasFatalErrorOccurred() && |
| 615 |
SemaRef.hasUncompilableErrorOccurred()) { |
| 616 |
Invalid = true; |
| 617 |
return; |
| 618 |
} |
| 619 |
|
| 620 |
CodeSynthesisContext Inst; |
| 621 |
Inst.Kind = Kind; |
| 622 |
Inst.PointOfInstantiation = PointOfInstantiation; |
| 623 |
Inst.Entity = Entity; |
| 624 |
Inst.Template = Template; |
| 625 |
Inst.TemplateArgs = TemplateArgs.data(); |
| 626 |
Inst.NumTemplateArgs = TemplateArgs.size(); |
| 627 |
Inst.InstantiationRange = InstantiationRange; |
| 628 |
Inst.InConstraintSubstitution = |
| 629 |
Inst.Kind == CodeSynthesisContext::ConstraintSubstitution; |
| 630 |
Inst.InParameterMappingSubstitution = |
| 631 |
Inst.Kind == CodeSynthesisContext::ParameterMappingSubstitution; |
| 632 |
if (!SemaRef.CodeSynthesisContexts.empty()) { |
| 633 |
Inst.InConstraintSubstitution |= |
| 634 |
SemaRef.CodeSynthesisContexts.back().InConstraintSubstitution; |
| 635 |
Inst.InParameterMappingSubstitution |= |
| 636 |
SemaRef.CodeSynthesisContexts.back().InParameterMappingSubstitution; |
| 637 |
} |
| 638 |
|
| 639 |
Invalid = SemaRef.pushCodeSynthesisContext(Inst); |
| 640 |
if (!Invalid) |
| 641 |
atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, Inst); |
| 642 |
} |
| 643 |
|
| 644 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 645 |
Sema &SemaRef, SourceLocation PointOfInstantiation, Decl *Entity, |
| 646 |
SourceRange InstantiationRange) |
| 647 |
: InstantiatingTemplate(SemaRef, |
| 648 |
CodeSynthesisContext::TemplateInstantiation, |
| 649 |
PointOfInstantiation, InstantiationRange, Entity) {} |
| 650 |
|
| 651 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 652 |
Sema &SemaRef, SourceLocation PointOfInstantiation, FunctionDecl *Entity, |
| 653 |
ExceptionSpecification, SourceRange InstantiationRange) |
| 654 |
: InstantiatingTemplate( |
| 655 |
SemaRef, CodeSynthesisContext::ExceptionSpecInstantiation, |
| 656 |
PointOfInstantiation, InstantiationRange, Entity) {} |
| 657 |
|
| 658 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 659 |
Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateParameter Param, |
| 660 |
TemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
| 661 |
SourceRange InstantiationRange) |
| 662 |
: InstantiatingTemplate( |
| 663 |
SemaRef, |
| 664 |
CodeSynthesisContext::DefaultTemplateArgumentInstantiation, |
| 665 |
PointOfInstantiation, InstantiationRange, getAsNamedDecl(Param), |
| 666 |
Template, TemplateArgs) {} |
| 667 |
|
| 668 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 669 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 670 |
FunctionTemplateDecl *FunctionTemplate, |
| 671 |
ArrayRef<TemplateArgument> TemplateArgs, |
| 672 |
CodeSynthesisContext::SynthesisKind Kind, SourceRange InstantiationRange) |
| 673 |
: InstantiatingTemplate(SemaRef, Kind, PointOfInstantiation, |
| 674 |
InstantiationRange, FunctionTemplate, nullptr, |
| 675 |
TemplateArgs) { |
| 676 |
assert(Kind == CodeSynthesisContext::ExplicitTemplateArgumentSubstitution || |
| 677 |
Kind == CodeSynthesisContext::DeducedTemplateArgumentSubstitution || |
| 678 |
Kind == CodeSynthesisContext::BuildingDeductionGuides); |
| 679 |
} |
| 680 |
|
| 681 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 682 |
Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template, |
| 683 |
ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
| 684 |
: InstantiatingTemplate( |
| 685 |
SemaRef, CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
| 686 |
PointOfInstantiation, InstantiationRange, Template, nullptr, |
| 687 |
TemplateArgs) {} |
| 688 |
|
| 689 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 690 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 691 |
ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 692 |
ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
| 693 |
: InstantiatingTemplate( |
| 694 |
SemaRef, CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
| 695 |
PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
| 696 |
TemplateArgs) {} |
| 697 |
|
| 698 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 699 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 700 |
VarTemplatePartialSpecializationDecl *PartialSpec, |
| 701 |
ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
| 702 |
: InstantiatingTemplate( |
| 703 |
SemaRef, CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
| 704 |
PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
| 705 |
TemplateArgs) {} |
| 706 |
|
| 707 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 708 |
Sema &SemaRef, SourceLocation PointOfInstantiation, ParmVarDecl *Param, |
| 709 |
ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
| 710 |
: InstantiatingTemplate( |
| 711 |
SemaRef, |
| 712 |
CodeSynthesisContext::DefaultFunctionArgumentInstantiation, |
| 713 |
PointOfInstantiation, InstantiationRange, Param, nullptr, |
| 714 |
TemplateArgs) {} |
| 715 |
|
| 716 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 717 |
Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
| 718 |
NonTypeTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
| 719 |
SourceRange InstantiationRange) |
| 720 |
: InstantiatingTemplate( |
| 721 |
SemaRef, |
| 722 |
CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
| 723 |
PointOfInstantiation, InstantiationRange, Param, Template, |
| 724 |
TemplateArgs) {} |
| 725 |
|
| 726 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 727 |
Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
| 728 |
TemplateTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
| 729 |
SourceRange InstantiationRange) |
| 730 |
: InstantiatingTemplate( |
| 731 |
SemaRef, |
| 732 |
CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
| 733 |
PointOfInstantiation, InstantiationRange, Param, Template, |
| 734 |
TemplateArgs) {} |
| 735 |
|
| 736 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 737 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 738 |
TypeAliasTemplateDecl *Entity, ArrayRef<TemplateArgument> TemplateArgs, |
| 739 |
SourceRange InstantiationRange) |
| 740 |
: InstantiatingTemplate( |
| 741 |
SemaRef, CodeSynthesisContext::TypeAliasTemplateInstantiation, |
| 742 |
PointOfInstantiation, InstantiationRange, /*Entity=*/Entity, |
| 743 |
/*Template=*/nullptr, TemplateArgs) {} |
| 744 |
|
| 745 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 746 |
Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template, |
| 747 |
NamedDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
| 748 |
SourceRange InstantiationRange) |
| 749 |
: InstantiatingTemplate( |
| 750 |
SemaRef, CodeSynthesisContext::DefaultTemplateArgumentChecking, |
| 751 |
PointOfInstantiation, InstantiationRange, Param, Template, |
| 752 |
TemplateArgs) {} |
| 753 |
|
| 754 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 755 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 756 |
concepts::Requirement *Req, SourceRange InstantiationRange) |
| 757 |
: InstantiatingTemplate( |
| 758 |
SemaRef, CodeSynthesisContext::RequirementInstantiation, |
| 759 |
PointOfInstantiation, InstantiationRange, /*Entity=*/nullptr, |
| 760 |
/*Template=*/nullptr, /*TemplateArgs=*/{}) {} |
| 761 |
|
| 762 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 763 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 764 |
concepts::NestedRequirement *Req, ConstraintsCheck, |
| 765 |
SourceRange InstantiationRange) |
| 766 |
: InstantiatingTemplate( |
| 767 |
SemaRef, CodeSynthesisContext::NestedRequirementConstraintsCheck, |
| 768 |
PointOfInstantiation, InstantiationRange, /*Entity=*/nullptr, |
| 769 |
/*Template=*/nullptr, /*TemplateArgs=*/{}) {} |
| 770 |
|
| 771 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 772 |
Sema &SemaRef, SourceLocation PointOfInstantiation, const RequiresExpr *RE, |
| 773 |
SourceRange InstantiationRange) |
| 774 |
: InstantiatingTemplate( |
| 775 |
SemaRef, CodeSynthesisContext::RequirementParameterInstantiation, |
| 776 |
PointOfInstantiation, InstantiationRange, /*Entity=*/nullptr, |
| 777 |
/*Template=*/nullptr, /*TemplateArgs=*/{}) {} |
| 778 |
|
| 779 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 780 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 781 |
ConstraintsCheck, NamedDecl *Template, |
| 782 |
ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
| 783 |
: InstantiatingTemplate( |
| 784 |
SemaRef, CodeSynthesisContext::ConstraintsCheck, |
| 785 |
PointOfInstantiation, InstantiationRange, Template, nullptr, |
| 786 |
TemplateArgs) {} |
| 787 |
|
| 788 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 789 |
Sema &SemaRef, SourceLocation PointOfInstantiation, ConstraintSubstitution, |
| 790 |
NamedDecl *Template, SourceRange InstantiationRange) |
| 791 |
: InstantiatingTemplate( |
| 792 |
SemaRef, CodeSynthesisContext::ConstraintSubstitution, |
| 793 |
PointOfInstantiation, InstantiationRange, Template, nullptr, {}) {} |
| 794 |
|
| 795 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 796 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 797 |
ConstraintNormalization, NamedDecl *Template, |
| 798 |
SourceRange InstantiationRange) |
| 799 |
: InstantiatingTemplate( |
| 800 |
SemaRef, CodeSynthesisContext::ConstraintNormalization, |
| 801 |
PointOfInstantiation, InstantiationRange, Template) {} |
| 802 |
|
| 803 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 804 |
Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 805 |
ParameterMappingSubstitution, NamedDecl *Template, |
| 806 |
SourceRange InstantiationRange) |
| 807 |
: InstantiatingTemplate( |
| 808 |
SemaRef, CodeSynthesisContext::ParameterMappingSubstitution, |
| 809 |
PointOfInstantiation, InstantiationRange, Template) {} |
| 810 |
|
| 811 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 812 |
Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Entity, |
| 813 |
BuildingDeductionGuidesTag, SourceRange InstantiationRange) |
| 814 |
: InstantiatingTemplate( |
| 815 |
SemaRef, CodeSynthesisContext::BuildingDeductionGuides, |
| 816 |
PointOfInstantiation, InstantiationRange, Entity) {} |
| 817 |
|
| 818 |
Sema::InstantiatingTemplate::InstantiatingTemplate( |
| 819 |
Sema &SemaRef, SourceLocation ArgLoc, PartialOrderingTTP, |
| 820 |
TemplateDecl *PArg, SourceRange InstantiationRange) |
| 821 |
: InstantiatingTemplate(SemaRef, CodeSynthesisContext::PartialOrderingTTP, |
| 822 |
ArgLoc, InstantiationRange, PArg) {} |
| 823 |
|
| 824 |
bool Sema::pushCodeSynthesisContext(CodeSynthesisContext Ctx) { |
| 825 |
if (!Ctx.isInstantiationRecord()) { |
| 826 |
++NonInstantiationEntries; |
| 827 |
} else { |
| 828 |
assert(SemaRef.NonInstantiationEntries <= |
| 829 |
SemaRef.CodeSynthesisContexts.size()); |
| 830 |
if ((SemaRef.CodeSynthesisContexts.size() - |
| 831 |
SemaRef.NonInstantiationEntries) > |
| 832 |
SemaRef.getLangOpts().InstantiationDepth) { |
| 833 |
SemaRef.Diag(Ctx.PointOfInstantiation, |
| 834 |
diag::err_template_recursion_depth_exceeded) |
| 835 |
<< SemaRef.getLangOpts().InstantiationDepth << Ctx.InstantiationRange; |
| 836 |
SemaRef.Diag(Ctx.PointOfInstantiation, |
| 837 |
diag::note_template_recursion_depth) |
| 838 |
<< SemaRef.getLangOpts().InstantiationDepth; |
| 839 |
return true; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
CodeSynthesisContexts.push_back(Ctx); |
| 844 |
|
| 845 |
// Check to see if we're low on stack space. We can't do anything about this |
| 846 |
// from here, but we can at least warn the user. |
| 847 |
StackHandler.warnOnStackNearlyExhausted(Ctx.PointOfInstantiation); |
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
void Sema::popCodeSynthesisContext() { |
| 852 |
auto &Active = CodeSynthesisContexts.back(); |
| 853 |
if (!Active.isInstantiationRecord()) { |
| 854 |
assert(NonInstantiationEntries > 0); |
| 855 |
--NonInstantiationEntries; |
| 856 |
} |
| 857 |
|
| 858 |
// Name lookup no longer looks in this template's defining module. |
| 859 |
assert(CodeSynthesisContexts.size() >= |
| 860 |
CodeSynthesisContextLookupModules.size() && |
| 861 |
"forgot to remove a lookup module for a template instantiation"); |
| 862 |
if (CodeSynthesisContexts.size() == |
| 863 |
CodeSynthesisContextLookupModules.size()) { |
| 864 |
if (Module *M = CodeSynthesisContextLookupModules.back()) |
| 865 |
LookupModulesCache.erase(M); |
| 866 |
CodeSynthesisContextLookupModules.pop_back(); |
| 867 |
} |
| 868 |
|
| 869 |
// If we've left the code synthesis context for the current context stack, |
| 870 |
// stop remembering that we've emitted that stack. |
| 871 |
if (CodeSynthesisContexts.size() == |
| 872 |
LastEmittedCodeSynthesisContextDepth) |
| 873 |
LastEmittedCodeSynthesisContextDepth = 0; |
| 874 |
|
| 875 |
CodeSynthesisContexts.pop_back(); |
| 876 |
} |
| 877 |
|
| 878 |
void Sema::InstantiatingTemplate::Clear() { |
| 879 |
if (!Invalid) { |
| 880 |
atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, |
| 881 |
SemaRef.CodeSynthesisContexts.back()); |
| 882 |
|
| 883 |
SemaRef.popCodeSynthesisContext(); |
| 884 |
Invalid = true; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
static std::string convertCallArgsToString(Sema &S, |
| 889 |
llvm::ArrayRef<const Expr *> Args) { |
| 890 |
std::string Result; |
| 891 |
llvm::raw_string_ostream OS(Result); |
| 892 |
llvm::ListSeparator Comma; |
| 893 |
for (const Expr *Arg : Args) { |
| 894 |
OS << Comma; |
| 895 |
Arg->IgnoreParens()->printPretty(OS, nullptr, |
| 896 |
S.Context.getPrintingPolicy()); |
| 897 |
} |
| 898 |
return Result; |
| 899 |
} |
| 900 |
|
| 901 |
void Sema::PrintInstantiationStack(InstantiationContextDiagFuncRef DiagFunc) { |
| 902 |
// Determine which template instantiations to skip, if any. |
| 903 |
unsigned SkipStart = CodeSynthesisContexts.size(), SkipEnd = SkipStart; |
| 904 |
unsigned Limit = Diags.getTemplateBacktraceLimit(); |
| 905 |
if (Limit && Limit < CodeSynthesisContexts.size()) { |
| 906 |
SkipStart = Limit / 2 + Limit % 2; |
| 907 |
SkipEnd = CodeSynthesisContexts.size() - Limit / 2; |
| 908 |
} |
| 909 |
|
| 910 |
// FIXME: In all of these cases, we need to show the template arguments |
| 911 |
unsigned InstantiationIdx = 0; |
| 912 |
for (SmallVectorImpl<CodeSynthesisContext>::reverse_iterator |
| 913 |
Active = CodeSynthesisContexts.rbegin(), |
| 914 |
ActiveEnd = CodeSynthesisContexts.rend(); |
| 915 |
Active != ActiveEnd; |
| 916 |
++Active, ++InstantiationIdx) { |
| 917 |
// Skip this instantiation? |
| 918 |
if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { |
| 919 |
if (InstantiationIdx == SkipStart) { |
| 920 |
// Note that we're skipping instantiations. |
| 921 |
DiagFunc(Active->PointOfInstantiation, |
| 922 |
PDiag(diag::note_instantiation_contexts_suppressed) |
| 923 |
<< unsigned(CodeSynthesisContexts.size() - Limit)); |
| 924 |
} |
| 925 |
continue; |
| 926 |
} |
| 927 |
|
| 928 |
switch (Active->Kind) { |
| 929 |
case CodeSynthesisContext::TemplateInstantiation: { |
| 930 |
Decl *D = Active->Entity; |
| 931 |
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 932 |
unsigned DiagID = diag::note_template_member_class_here; |
| 933 |
if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 934 |
DiagID = diag::note_template_class_instantiation_here; |
| 935 |
DiagFunc(Active->PointOfInstantiation, |
| 936 |
PDiag(DiagID) << Record << Active->InstantiationRange); |
| 937 |
} else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 938 |
unsigned DiagID; |
| 939 |
if (Function->getPrimaryTemplate()) |
| 940 |
DiagID = diag::note_function_template_spec_here; |
| 941 |
else |
| 942 |
DiagID = diag::note_template_member_function_here; |
| 943 |
DiagFunc(Active->PointOfInstantiation, |
| 944 |
PDiag(DiagID) << Function << Active->InstantiationRange); |
| 945 |
} else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 946 |
DiagFunc(Active->PointOfInstantiation, |
| 947 |
PDiag(VD->isStaticDataMember() |
| 948 |
? diag::note_template_static_data_member_def_here |
| 949 |
: diag::note_template_variable_def_here) |
| 950 |
<< VD << Active->InstantiationRange); |
| 951 |
} else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 952 |
DiagFunc(Active->PointOfInstantiation, |
| 953 |
PDiag(diag::note_template_enum_def_here) |
| 954 |
<< ED << Active->InstantiationRange); |
| 955 |
} else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 956 |
DiagFunc(Active->PointOfInstantiation, |
| 957 |
PDiag(diag::note_template_nsdmi_here) |
| 958 |
<< FD << Active->InstantiationRange); |
| 959 |
} else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(D)) { |
| 960 |
DiagFunc(Active->PointOfInstantiation, |
| 961 |
PDiag(diag::note_template_class_instantiation_here) |
| 962 |
<< CTD << Active->InstantiationRange); |
| 963 |
} |
| 964 |
break; |
| 965 |
} |
| 966 |
|
| 967 |
case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: { |
| 968 |
TemplateDecl *Template = cast<TemplateDecl>(Active->Template); |
| 969 |
SmallString<128> TemplateArgsStr; |
| 970 |
llvm::raw_svector_ostream OS(TemplateArgsStr); |
| 971 |
Template->printName(OS, getPrintingPolicy()); |
| 972 |
printTemplateArgumentList(OS, Active->template_arguments(), |
| 973 |
getPrintingPolicy()); |
| 974 |
DiagFunc(Active->PointOfInstantiation, |
| 975 |
PDiag(diag::note_default_arg_instantiation_here) |
| 976 |
<< OS.str() << Active->InstantiationRange); |
| 977 |
break; |
| 978 |
} |
| 979 |
|
| 980 |
case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: { |
| 981 |
FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity); |
| 982 |
DiagFunc(Active->PointOfInstantiation, |
| 983 |
PDiag(diag::note_explicit_template_arg_substitution_here) |
| 984 |
<< FnTmpl |
| 985 |
<< getTemplateArgumentBindingsText( |
| 986 |
FnTmpl->getTemplateParameters(), Active->TemplateArgs, |
| 987 |
Active->NumTemplateArgs) |
| 988 |
<< Active->InstantiationRange); |
| 989 |
break; |
| 990 |
} |
| 991 |
|
| 992 |
case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: { |
| 993 |
if (FunctionTemplateDecl *FnTmpl = |
| 994 |
dyn_cast<FunctionTemplateDecl>(Active->Entity)) { |
| 995 |
DiagFunc( |
| 996 |
Active->PointOfInstantiation, |
| 997 |
PDiag(diag::note_function_template_deduction_instantiation_here) |
| 998 |
<< FnTmpl |
| 999 |
<< getTemplateArgumentBindingsText( |
| 1000 |
FnTmpl->getTemplateParameters(), Active->TemplateArgs, |
| 1001 |
Active->NumTemplateArgs) |
| 1002 |
<< Active->InstantiationRange); |
| 1003 |
} else { |
| 1004 |
bool IsVar = isa<VarTemplateDecl>(Active->Entity) || |
| 1005 |
isa<VarTemplateSpecializationDecl>(Active->Entity); |
| 1006 |
bool IsTemplate = false; |
| 1007 |
TemplateParameterList *Params; |
| 1008 |
if (auto *D = dyn_cast<TemplateDecl>(Active->Entity)) { |
| 1009 |
IsTemplate = true; |
| 1010 |
Params = D->getTemplateParameters(); |
| 1011 |
} else if (auto *D = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
| 1012 |
Active->Entity)) { |
| 1013 |
Params = D->getTemplateParameters(); |
| 1014 |
} else if (auto *D = dyn_cast<VarTemplatePartialSpecializationDecl>( |
| 1015 |
Active->Entity)) { |
| 1016 |
Params = D->getTemplateParameters(); |
| 1017 |
} else { |
| 1018 |
llvm_unreachable("unexpected template kind"); |
| 1019 |
} |
| 1020 |
|
| 1021 |
DiagFunc(Active->PointOfInstantiation, |
| 1022 |
PDiag(diag::note_deduced_template_arg_substitution_here) |
| 1023 |
<< IsVar << IsTemplate << cast<NamedDecl>(Active->Entity) |
| 1024 |
<< getTemplateArgumentBindingsText(Params, |
| 1025 |
Active->TemplateArgs, |
| 1026 |
Active->NumTemplateArgs) |
| 1027 |
<< Active->InstantiationRange); |
| 1028 |
} |
| 1029 |
break; |
| 1030 |
} |
| 1031 |
|
| 1032 |
case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: { |
| 1033 |
ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity); |
| 1034 |
FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
| 1035 |
|
| 1036 |
SmallString<128> TemplateArgsStr; |
| 1037 |
llvm::raw_svector_ostream OS(TemplateArgsStr); |
| 1038 |
FD->printName(OS, getPrintingPolicy()); |
| 1039 |
printTemplateArgumentList(OS, Active->template_arguments(), |
| 1040 |
getPrintingPolicy()); |
| 1041 |
DiagFunc(Active->PointOfInstantiation, |
| 1042 |
PDiag(diag::note_default_function_arg_instantiation_here) |
| 1043 |
<< OS.str() << Active->InstantiationRange); |
| 1044 |
break; |
| 1045 |
} |
| 1046 |
|
| 1047 |
case CodeSynthesisContext::PriorTemplateArgumentSubstitution: { |
| 1048 |
NamedDecl *Parm = cast<NamedDecl>(Active->Entity); |
| 1049 |
std::string Name; |
| 1050 |
if (!Parm->getName().empty()) |
| 1051 |
Name = std::string(" '") + Parm->getName().str() + "'"; |
| 1052 |
|
| 1053 |
TemplateParameterList *TemplateParams = nullptr; |
| 1054 |
if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
| 1055 |
TemplateParams = Template->getTemplateParameters(); |
| 1056 |
else |
| 1057 |
TemplateParams = |
| 1058 |
cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
| 1059 |
->getTemplateParameters(); |
| 1060 |
DiagFunc(Active->PointOfInstantiation, |
| 1061 |
PDiag(diag::note_prior_template_arg_substitution) |
| 1062 |
<< isa<TemplateTemplateParmDecl>(Parm) << Name |
| 1063 |
<< getTemplateArgumentBindingsText(TemplateParams, |
| 1064 |
Active->TemplateArgs, |
| 1065 |
Active->NumTemplateArgs) |
| 1066 |
<< Active->InstantiationRange); |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
|
| 1070 |
case CodeSynthesisContext::DefaultTemplateArgumentChecking: { |
| 1071 |
TemplateParameterList *TemplateParams = nullptr; |
| 1072 |
if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
| 1073 |
TemplateParams = Template->getTemplateParameters(); |
| 1074 |
else |
| 1075 |
TemplateParams = |
| 1076 |
cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
| 1077 |
->getTemplateParameters(); |
| 1078 |
|
| 1079 |
DiagFunc(Active->PointOfInstantiation, |
| 1080 |
PDiag(diag::note_template_default_arg_checking) |
| 1081 |
<< getTemplateArgumentBindingsText(TemplateParams, |
| 1082 |
Active->TemplateArgs, |
| 1083 |
Active->NumTemplateArgs) |
| 1084 |
<< Active->InstantiationRange); |
| 1085 |
break; |
| 1086 |
} |
| 1087 |
|
| 1088 |
case CodeSynthesisContext::ExceptionSpecEvaluation: |
| 1089 |
DiagFunc(Active->PointOfInstantiation, |
| 1090 |
PDiag(diag::note_evaluating_exception_spec_here) |
| 1091 |
<< cast<FunctionDecl>(Active->Entity)); |
| 1092 |
break; |
| 1093 |
|
| 1094 |
case CodeSynthesisContext::ExceptionSpecInstantiation: |
| 1095 |
DiagFunc(Active->PointOfInstantiation, |
| 1096 |
PDiag(diag::note_template_exception_spec_instantiation_here) |
| 1097 |
<< cast<FunctionDecl>(Active->Entity) |
| 1098 |
<< Active->InstantiationRange); |
| 1099 |
break; |
| 1100 |
|
| 1101 |
case CodeSynthesisContext::RequirementInstantiation: |
| 1102 |
DiagFunc(Active->PointOfInstantiation, |
| 1103 |
PDiag(diag::note_template_requirement_instantiation_here) |
| 1104 |
<< Active->InstantiationRange); |
| 1105 |
break; |
| 1106 |
case CodeSynthesisContext::RequirementParameterInstantiation: |
| 1107 |
DiagFunc(Active->PointOfInstantiation, |
| 1108 |
PDiag(diag::note_template_requirement_params_instantiation_here) |
| 1109 |
<< Active->InstantiationRange); |
| 1110 |
break; |
| 1111 |
|
| 1112 |
case CodeSynthesisContext::NestedRequirementConstraintsCheck: |
| 1113 |
DiagFunc(Active->PointOfInstantiation, |
| 1114 |
PDiag(diag::note_nested_requirement_here) |
| 1115 |
<< Active->InstantiationRange); |
| 1116 |
break; |
| 1117 |
|
| 1118 |
case CodeSynthesisContext::DeclaringSpecialMember: |
| 1119 |
DiagFunc(Active->PointOfInstantiation, |
| 1120 |
PDiag(diag::note_in_declaration_of_implicit_special_member) |
| 1121 |
<< cast<CXXRecordDecl>(Active->Entity) |
| 1122 |
<< Active->SpecialMember); |
| 1123 |
break; |
| 1124 |
|
| 1125 |
case CodeSynthesisContext::DeclaringImplicitEqualityComparison: |
| 1126 |
DiagFunc( |
| 1127 |
Active->Entity->getLocation(), |
| 1128 |
PDiag(diag::note_in_declaration_of_implicit_equality_comparison)); |
| 1129 |
break; |
| 1130 |
|
| 1131 |
case CodeSynthesisContext::DefiningSynthesizedFunction: { |
| 1132 |
// FIXME: For synthesized functions that are not defaulted, |
| 1133 |
// produce a note. |
| 1134 |
auto *FD = dyn_cast<FunctionDecl>(Active->Entity); |
| 1135 |
// Note: if FD is nullptr currently setting DFK to DefaultedFunctionKind() |
| 1136 |
// will ensure that DFK.isComparison() is false. This is important because |
| 1137 |
// we will uncondtionally dereference FD in the else if. |
| 1138 |
DefaultedFunctionKind DFK = |
| 1139 |
FD ? getDefaultedFunctionKind(FD) : DefaultedFunctionKind(); |
| 1140 |
if (DFK.isSpecialMember()) { |
| 1141 |
auto *MD = cast<CXXMethodDecl>(FD); |
| 1142 |
DiagFunc(Active->PointOfInstantiation, |
| 1143 |
PDiag(diag::note_member_synthesized_at) |
| 1144 |
<< MD->isExplicitlyDefaulted() << DFK.asSpecialMember() |
| 1145 |
<< Context.getCanonicalTagType(MD->getParent())); |
| 1146 |
} else if (DFK.isComparison()) { |
| 1147 |
QualType RecordType = FD->getParamDecl(0) |
| 1148 |
->getType() |
| 1149 |
.getNonReferenceType() |
| 1150 |
.getUnqualifiedType(); |
| 1151 |
DiagFunc(Active->PointOfInstantiation, |
| 1152 |
PDiag(diag::note_comparison_synthesized_at) |
| 1153 |
<< (int)DFK.asComparison() << RecordType); |
| 1154 |
} |
| 1155 |
break; |
| 1156 |
} |
| 1157 |
|
| 1158 |
case CodeSynthesisContext::RewritingOperatorAsSpaceship: |
| 1159 |
DiagFunc(Active->Entity->getLocation(), |
| 1160 |
PDiag(diag::note_rewriting_operator_as_spaceship)); |
| 1161 |
break; |
| 1162 |
|
| 1163 |
case CodeSynthesisContext::InitializingStructuredBinding: |
| 1164 |
DiagFunc(Active->PointOfInstantiation, |
| 1165 |
PDiag(diag::note_in_binding_decl_init) |
| 1166 |
<< cast<BindingDecl>(Active->Entity)); |
| 1167 |
break; |
| 1168 |
|
| 1169 |
case CodeSynthesisContext::MarkingClassDllexported: |
| 1170 |
DiagFunc(Active->PointOfInstantiation, |
| 1171 |
PDiag(diag::note_due_to_dllexported_class) |
| 1172 |
<< cast<CXXRecordDecl>(Active->Entity) |
| 1173 |
<< !getLangOpts().CPlusPlus11); |
| 1174 |
break; |
| 1175 |
|
| 1176 |
case CodeSynthesisContext::BuildingBuiltinDumpStructCall: |
| 1177 |
DiagFunc(Active->PointOfInstantiation, |
| 1178 |
PDiag(diag::note_building_builtin_dump_struct_call) |
| 1179 |
<< convertCallArgsToString( |
| 1180 |
*this, llvm::ArrayRef(Active->CallArgs, |
| 1181 |
Active->NumCallArgs))); |
| 1182 |
break; |
| 1183 |
|
| 1184 |
case CodeSynthesisContext::Memoization: |
| 1185 |
break; |
| 1186 |
|
| 1187 |
case CodeSynthesisContext::LambdaExpressionSubstitution: |
| 1188 |
DiagFunc(Active->PointOfInstantiation, |
| 1189 |
PDiag(diag::note_lambda_substitution_here)); |
| 1190 |
break; |
| 1191 |
case CodeSynthesisContext::ConstraintsCheck: { |
| 1192 |
unsigned DiagID = 0; |
| 1193 |
if (!Active->Entity) { |
| 1194 |
DiagFunc(Active->PointOfInstantiation, |
| 1195 |
PDiag(diag::note_nested_requirement_here) |
| 1196 |
<< Active->InstantiationRange); |
| 1197 |
break; |
| 1198 |
} |
| 1199 |
if (isa<ConceptDecl>(Active->Entity)) |
| 1200 |
DiagID = diag::note_concept_specialization_here; |
| 1201 |
else if (isa<TemplateDecl>(Active->Entity)) |
| 1202 |
DiagID = diag::note_checking_constraints_for_template_id_here; |
| 1203 |
else if (isa<VarTemplatePartialSpecializationDecl>(Active->Entity)) |
| 1204 |
DiagID = diag::note_checking_constraints_for_var_spec_id_here; |
| 1205 |
else if (isa<ClassTemplatePartialSpecializationDecl>(Active->Entity)) |
| 1206 |
DiagID = diag::note_checking_constraints_for_class_spec_id_here; |
| 1207 |
else { |
| 1208 |
assert(isa<FunctionDecl>(Active->Entity)); |
| 1209 |
DiagID = diag::note_checking_constraints_for_function_here; |
| 1210 |
} |
| 1211 |
SmallString<128> TemplateArgsStr; |
| 1212 |
llvm::raw_svector_ostream OS(TemplateArgsStr); |
| 1213 |
cast<NamedDecl>(Active->Entity)->printName(OS, getPrintingPolicy()); |
| 1214 |
if (!isa<FunctionDecl>(Active->Entity)) { |
| 1215 |
printTemplateArgumentList(OS, Active->template_arguments(), |
| 1216 |
getPrintingPolicy()); |
| 1217 |
} |
| 1218 |
DiagFunc(Active->PointOfInstantiation, |
| 1219 |
PDiag(DiagID) << OS.str() << Active->InstantiationRange); |
| 1220 |
break; |
| 1221 |
} |
| 1222 |
case CodeSynthesisContext::ConstraintSubstitution: |
| 1223 |
DiagFunc(Active->PointOfInstantiation, |
| 1224 |
PDiag(diag::note_constraint_substitution_here) |
| 1225 |
<< Active->InstantiationRange); |
| 1226 |
break; |
| 1227 |
case CodeSynthesisContext::ConstraintNormalization: |
| 1228 |
DiagFunc(Active->PointOfInstantiation, |
| 1229 |
PDiag(diag::note_constraint_normalization_here) |
| 1230 |
<< cast<NamedDecl>(Active->Entity) |
| 1231 |
<< Active->InstantiationRange); |
| 1232 |
break; |
| 1233 |
case CodeSynthesisContext::ParameterMappingSubstitution: |
| 1234 |
DiagFunc(Active->PointOfInstantiation, |
| 1235 |
PDiag(diag::note_parameter_mapping_substitution_here) |
| 1236 |
<< Active->InstantiationRange); |
| 1237 |
break; |
| 1238 |
case CodeSynthesisContext::BuildingDeductionGuides: |
| 1239 |
DiagFunc(Active->PointOfInstantiation, |
| 1240 |
PDiag(diag::note_building_deduction_guide_here)); |
| 1241 |
break; |
| 1242 |
case CodeSynthesisContext::TypeAliasTemplateInstantiation: |
| 1243 |
// Workaround for a workaround: don't produce a note if we are merely |
| 1244 |
// instantiating some other template which contains this alias template. |
| 1245 |
// This would be redundant either with the error itself, or some other |
| 1246 |
// context note attached to it. |
| 1247 |
if (Active->NumTemplateArgs == 0) |
| 1248 |
break; |
| 1249 |
DiagFunc(Active->PointOfInstantiation, |
| 1250 |
PDiag(diag::note_template_type_alias_instantiation_here) |
| 1251 |
<< cast<TypeAliasTemplateDecl>(Active->Entity) |
| 1252 |
<< Active->InstantiationRange); |
| 1253 |
break; |
| 1254 |
case CodeSynthesisContext::PartialOrderingTTP: |
| 1255 |
DiagFunc(Active->PointOfInstantiation, |
| 1256 |
PDiag(diag::note_template_arg_template_params_mismatch)); |
| 1257 |
if (SourceLocation ParamLoc = Active->Entity->getLocation(); |
| 1258 |
ParamLoc.isValid()) |
| 1259 |
DiagFunc(ParamLoc, PDiag(diag::note_template_prev_declaration) |
| 1260 |
<< /*isTemplateTemplateParam=*/true |
| 1261 |
<< Active->InstantiationRange); |
| 1262 |
break; |
| 1263 |
} |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
//===----------------------------------------------------------------------===/ |
| 1268 |
// Template Instantiation for Types |
| 1269 |
//===----------------------------------------------------------------------===/ |
| 1270 |
namespace { |
| 1271 |
|
| 1272 |
class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
| 1273 |
const MultiLevelTemplateArgumentList &TemplateArgs; |
| 1274 |
SourceLocation Loc; |
| 1275 |
DeclarationName Entity; |
| 1276 |
// Whether to evaluate the C++20 constraints or simply substitute into them. |
| 1277 |
bool EvaluateConstraints = true; |
| 1278 |
// Whether Substitution was Incomplete, that is, we tried to substitute in |
| 1279 |
// any user provided template arguments which were null. |
| 1280 |
bool IsIncomplete = false; |
| 1281 |
// Whether an incomplete substituion should be treated as an error. |
| 1282 |
bool BailOutOnIncomplete; |
| 1283 |
|
| 1284 |
// CWG2770: Function parameters should be instantiated when they are |
| 1285 |
// needed by a satisfaction check of an atomic constraint or |
| 1286 |
// (recursively) by another function parameter. |
| 1287 |
bool maybeInstantiateFunctionParameterToScope(ParmVarDecl *OldParm); |
| 1288 |
|
| 1289 |
public: |
| 1290 |
typedef TreeTransform<TemplateInstantiator> inherited; |
| 1291 |
|
| 1292 |
TemplateInstantiator(Sema &SemaRef, |
| 1293 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 1294 |
SourceLocation Loc, DeclarationName Entity, |
| 1295 |
bool BailOutOnIncomplete = false) |
| 1296 |
: inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
| 1297 |
Entity(Entity), BailOutOnIncomplete(BailOutOnIncomplete) {} |
| 1298 |
|
| 1299 |
void setEvaluateConstraints(bool B) { |
| 1300 |
EvaluateConstraints = B; |
| 1301 |
} |
| 1302 |
bool getEvaluateConstraints() { |
| 1303 |
return EvaluateConstraints; |
| 1304 |
} |
| 1305 |
|
| 1306 |
inline static struct ForParameterMappingSubstitution_t { |
| 1307 |
} ForParameterMappingSubstitution; |
| 1308 |
|
| 1309 |
TemplateInstantiator(ForParameterMappingSubstitution_t, Sema &SemaRef, |
| 1310 |
SourceLocation Loc, |
| 1311 |
const MultiLevelTemplateArgumentList &TemplateArgs) |
| 1312 |
: inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
| 1313 |
BailOutOnIncomplete(false) {} |
| 1314 |
|
| 1315 |
/// Determine whether the given type \p T has already been |
| 1316 |
/// transformed. |
| 1317 |
/// |
| 1318 |
/// For the purposes of template instantiation, a type has already been |
| 1319 |
/// transformed if it is NULL or if it is not dependent. |
| 1320 |
bool AlreadyTransformed(QualType T); |
| 1321 |
|
| 1322 |
/// Returns the location of the entity being instantiated, if known. |
| 1323 |
SourceLocation getBaseLocation() { return Loc; } |
| 1324 |
|
| 1325 |
/// Returns the name of the entity being instantiated, if any. |
| 1326 |
DeclarationName getBaseEntity() { return Entity; } |
| 1327 |
|
| 1328 |
/// Returns whether any substitution so far was incomplete. |
| 1329 |
bool getIsIncomplete() const { return IsIncomplete; } |
| 1330 |
|
| 1331 |
/// Sets the "base" location and entity when that |
| 1332 |
/// information is known based on another transformation. |
| 1333 |
void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 1334 |
this->Loc = Loc; |
| 1335 |
this->Entity = Entity; |
| 1336 |
} |
| 1337 |
|
| 1338 |
unsigned TransformTemplateDepth(unsigned Depth) { |
| 1339 |
return TemplateArgs.getNewDepth(Depth); |
| 1340 |
} |
| 1341 |
|
| 1342 |
bool TryExpandParameterPacks(SourceLocation EllipsisLoc, |
| 1343 |
SourceRange PatternRange, |
| 1344 |
ArrayRef<UnexpandedParameterPack> Unexpanded, |
| 1345 |
bool FailOnPackProducingTemplates, |
| 1346 |
bool &ShouldExpand, bool &RetainExpansion, |
| 1347 |
UnsignedOrNone &NumExpansions) { |
| 1348 |
if (SemaRef.CurrentInstantiationScope && |
| 1349 |
(SemaRef.inConstraintSubstitution() || |
| 1350 |
SemaRef.inParameterMappingSubstitution())) { |
| 1351 |
for (UnexpandedParameterPack ParmPack : Unexpanded) { |
| 1352 |
NamedDecl *VD = ParmPack.first.dyn_cast<NamedDecl *>(); |
| 1353 |
if (auto *PVD = dyn_cast_if_present<ParmVarDecl>(VD); |
| 1354 |
PVD && maybeInstantiateFunctionParameterToScope(PVD)) |
| 1355 |
return true; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
return getSema().CheckParameterPacksForExpansion( |
| 1360 |
EllipsisLoc, PatternRange, Unexpanded, TemplateArgs, |
| 1361 |
FailOnPackProducingTemplates, ShouldExpand, RetainExpansion, |
| 1362 |
NumExpansions); |
| 1363 |
} |
| 1364 |
|
| 1365 |
void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { |
| 1366 |
SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack); |
| 1367 |
} |
| 1368 |
|
| 1369 |
TemplateArgument ForgetPartiallySubstitutedPack() { |
| 1370 |
TemplateArgument Result; |
| 1371 |
if (NamedDecl *PartialPack = SemaRef.CurrentInstantiationScope |
| 1372 |
->getPartiallySubstitutedPack()) { |
| 1373 |
MultiLevelTemplateArgumentList &TemplateArgs = |
| 1374 |
const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 1375 |
unsigned Depth, Index; |
| 1376 |
std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
| 1377 |
if (TemplateArgs.hasTemplateArgument(Depth, Index)) { |
| 1378 |
Result = TemplateArgs(Depth, Index); |
| 1379 |
TemplateArgs.setArgument(Depth, Index, TemplateArgument()); |
| 1380 |
} else { |
| 1381 |
IsIncomplete = true; |
| 1382 |
if (BailOutOnIncomplete) |
| 1383 |
return TemplateArgument(); |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
return Result; |
| 1388 |
} |
| 1389 |
|
| 1390 |
void RememberPartiallySubstitutedPack(TemplateArgument Arg) { |
| 1391 |
if (Arg.isNull()) |
| 1392 |
return; |
| 1393 |
|
| 1394 |
if (NamedDecl *PartialPack = SemaRef.CurrentInstantiationScope |
| 1395 |
->getPartiallySubstitutedPack()) { |
| 1396 |
MultiLevelTemplateArgumentList &TemplateArgs = |
| 1397 |
const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 1398 |
unsigned Depth, Index; |
| 1399 |
std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
| 1400 |
TemplateArgs.setArgument(Depth, Index, Arg); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
MultiLevelTemplateArgumentList ForgetSubstitution() { |
| 1405 |
MultiLevelTemplateArgumentList New; |
| 1406 |
New.addOuterRetainedLevels(this->TemplateArgs.getNumLevels()); |
| 1407 |
|
| 1408 |
MultiLevelTemplateArgumentList Old = |
| 1409 |
const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
| 1410 |
const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs) = |
| 1411 |
std::move(New); |
| 1412 |
return Old; |
| 1413 |
} |
| 1414 |
|
| 1415 |
void RememberSubstitution(MultiLevelTemplateArgumentList Old) { |
| 1416 |
const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs) = Old; |
| 1417 |
} |
| 1418 |
|
| 1419 |
TemplateArgument |
| 1420 |
getTemplateArgumentPackPatternForRewrite(const TemplateArgument &TA) { |
| 1421 |
if (TA.getKind() != TemplateArgument::Pack) |
| 1422 |
return TA; |
| 1423 |
if (SemaRef.ArgPackSubstIndex) |
| 1424 |
return SemaRef.getPackSubstitutedTemplateArgument(TA); |
| 1425 |
assert(TA.pack_size() == 1 && TA.pack_begin()->isPackExpansion() && |
| 1426 |
"unexpected pack arguments in template rewrite"); |
| 1427 |
TemplateArgument Arg = *TA.pack_begin(); |
| 1428 |
if (Arg.isPackExpansion()) |
| 1429 |
Arg = Arg.getPackExpansionPattern(); |
| 1430 |
return Arg; |
| 1431 |
} |
| 1432 |
|
| 1433 |
/// Transform the given declaration by instantiating a reference to |
| 1434 |
/// this declaration. |
| 1435 |
Decl *TransformDecl(SourceLocation Loc, Decl *D); |
| 1436 |
|
| 1437 |
void transformAttrs(Decl *Old, Decl *New) { |
| 1438 |
SemaRef.InstantiateAttrs(TemplateArgs, Old, New); |
| 1439 |
} |
| 1440 |
|
| 1441 |
void transformedLocalDecl(Decl *Old, ArrayRef<Decl *> NewDecls) { |
| 1442 |
if (Old->isParameterPack() && |
| 1443 |
(NewDecls.size() != 1 || !NewDecls.front()->isParameterPack())) { |
| 1444 |
SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Old); |
| 1445 |
for (auto *New : NewDecls) |
| 1446 |
SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg( |
| 1447 |
Old, cast<VarDecl>(New)); |
| 1448 |
return; |
| 1449 |
} |
| 1450 |
|
| 1451 |
assert(NewDecls.size() == 1 && |
| 1452 |
"should only have multiple expansions for a pack"); |
| 1453 |
Decl *New = NewDecls.front(); |
| 1454 |
|
| 1455 |
// If we've instantiated the call operator of a lambda or the call |
| 1456 |
// operator template of a generic lambda, update the "instantiation of" |
| 1457 |
// information. |
| 1458 |
auto *NewMD = dyn_cast<CXXMethodDecl>(New); |
| 1459 |
if (NewMD && isLambdaCallOperator(NewMD)) { |
| 1460 |
auto *OldMD = dyn_cast<CXXMethodDecl>(Old); |
| 1461 |
if (auto *NewTD = NewMD->getDescribedFunctionTemplate()) |
| 1462 |
NewTD->setInstantiatedFromMemberTemplate( |
| 1463 |
OldMD->getDescribedFunctionTemplate()); |
| 1464 |
else |
| 1465 |
NewMD->setInstantiationOfMemberFunction(OldMD, |
| 1466 |
TSK_ImplicitInstantiation); |
| 1467 |
} |
| 1468 |
|
| 1469 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New); |
| 1470 |
|
| 1471 |
// We recreated a local declaration, but not by instantiating it. There |
| 1472 |
// may be pending dependent diagnostics to produce. |
| 1473 |
if (auto *DC = dyn_cast<DeclContext>(Old); |
| 1474 |
DC && DC->isDependentContext() && DC->isFunctionOrMethod()) |
| 1475 |
SemaRef.PerformDependentDiagnostics(DC, TemplateArgs); |
| 1476 |
} |
| 1477 |
|
| 1478 |
/// Transform the definition of the given declaration by |
| 1479 |
/// instantiating it. |
| 1480 |
Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
| 1481 |
|
| 1482 |
/// Transform the first qualifier within a scope by instantiating the |
| 1483 |
/// declaration. |
| 1484 |
NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
| 1485 |
|
| 1486 |
bool TransformExceptionSpec(SourceLocation Loc, |
| 1487 |
FunctionProtoType::ExceptionSpecInfo &ESI, |
| 1488 |
SmallVectorImpl<QualType> &Exceptions, |
| 1489 |
bool &Changed); |
| 1490 |
|
| 1491 |
/// Rebuild the exception declaration and register the declaration |
| 1492 |
/// as an instantiated local. |
| 1493 |
VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, |
| 1494 |
TypeSourceInfo *Declarator, |
| 1495 |
SourceLocation StartLoc, |
| 1496 |
SourceLocation NameLoc, |
| 1497 |
IdentifierInfo *Name); |
| 1498 |
|
| 1499 |
/// Rebuild the Objective-C exception declaration and register the |
| 1500 |
/// declaration as an instantiated local. |
| 1501 |
VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 1502 |
TypeSourceInfo *TSInfo, QualType T); |
| 1503 |
|
| 1504 |
TemplateName |
| 1505 |
TransformTemplateName(NestedNameSpecifierLoc &QualifierLoc, |
| 1506 |
SourceLocation TemplateKWLoc, TemplateName Name, |
| 1507 |
SourceLocation NameLoc, |
| 1508 |
QualType ObjectType = QualType(), |
| 1509 |
NamedDecl *FirstQualifierInScope = nullptr, |
| 1510 |
bool AllowInjectedClassName = false); |
| 1511 |
|
| 1512 |
const AnnotateAttr *TransformAnnotateAttr(const AnnotateAttr *AA); |
| 1513 |
const CXXAssumeAttr *TransformCXXAssumeAttr(const CXXAssumeAttr *AA); |
| 1514 |
const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH); |
| 1515 |
const NoInlineAttr *TransformStmtNoInlineAttr(const Stmt *OrigS, |
| 1516 |
const Stmt *InstS, |
| 1517 |
const NoInlineAttr *A); |
| 1518 |
const AlwaysInlineAttr * |
| 1519 |
TransformStmtAlwaysInlineAttr(const Stmt *OrigS, const Stmt *InstS, |
| 1520 |
const AlwaysInlineAttr *A); |
| 1521 |
const CodeAlignAttr *TransformCodeAlignAttr(const CodeAlignAttr *CA); |
| 1522 |
const OpenACCRoutineDeclAttr * |
| 1523 |
TransformOpenACCRoutineDeclAttr(const OpenACCRoutineDeclAttr *A); |
| 1524 |
ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
| 1525 |
ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
| 1526 |
ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| 1527 |
|
| 1528 |
ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
| 1529 |
NonTypeTemplateParmDecl *D); |
| 1530 |
|
| 1531 |
/// Rebuild a DeclRefExpr for a VarDecl reference. |
| 1532 |
ExprResult RebuildVarDeclRefExpr(ValueDecl *PD, SourceLocation Loc); |
| 1533 |
|
| 1534 |
/// Transform a reference to a function or init-capture parameter pack. |
| 1535 |
ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E, ValueDecl *PD); |
| 1536 |
|
| 1537 |
/// Transform a FunctionParmPackExpr which was built when we couldn't |
| 1538 |
/// expand a function parameter pack reference which refers to an expanded |
| 1539 |
/// pack. |
| 1540 |
ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E); |
| 1541 |
|
| 1542 |
QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 1543 |
FunctionProtoTypeLoc TL) { |
| 1544 |
// Call the base version; it will forward to our overridden version below. |
| 1545 |
return inherited::TransformFunctionProtoType(TLB, TL); |
| 1546 |
} |
| 1547 |
|
| 1548 |
QualType TransformTagType(TypeLocBuilder &TLB, TagTypeLoc TL) { |
| 1549 |
auto Type = inherited::TransformTagType(TLB, TL); |
| 1550 |
if (!Type.isNull()) |
| 1551 |
return Type; |
| 1552 |
// Special case for transforming a deduction guide, we return a |
| 1553 |
// transformed TemplateSpecializationType. |
| 1554 |
// FIXME: Why is this hack necessary? |
| 1555 |
if (const auto *ICNT = dyn_cast<InjectedClassNameType>(TL.getTypePtr()); |
| 1556 |
ICNT && SemaRef.CodeSynthesisContexts.back().Kind == |
| 1557 |
Sema::CodeSynthesisContext::BuildingDeductionGuides) { |
| 1558 |
Type = inherited::TransformType( |
| 1559 |
ICNT->getDecl()->getCanonicalTemplateSpecializationType( |
| 1560 |
SemaRef.Context)); |
| 1561 |
TLB.pushTrivial(SemaRef.Context, Type, TL.getNameLoc()); |
| 1562 |
} |
| 1563 |
return Type; |
| 1564 |
} |
| 1565 |
// Override the default version to handle a rewrite-template-arg-pack case |
| 1566 |
// for building a deduction guide. |
| 1567 |
bool TransformTemplateArgument(const TemplateArgumentLoc &Input, |
| 1568 |
TemplateArgumentLoc &Output, |
| 1569 |
bool Uneval = false) { |
| 1570 |
const TemplateArgument &Arg = Input.getArgument(); |
| 1571 |
std::vector<TemplateArgument> TArgs; |
| 1572 |
switch (Arg.getKind()) { |
| 1573 |
case TemplateArgument::Pack: |
| 1574 |
assert(SemaRef.CodeSynthesisContexts.empty() || |
| 1575 |
SemaRef.CodeSynthesisContexts.back().Kind == |
| 1576 |
Sema::CodeSynthesisContext::BuildingDeductionGuides); |
| 1577 |
// Literally rewrite the template argument pack, instead of unpacking |
| 1578 |
// it. |
| 1579 |
for (auto &pack : Arg.getPackAsArray()) { |
| 1580 |
TemplateArgumentLoc Input = SemaRef.getTrivialTemplateArgumentLoc( |
| 1581 |
pack, QualType(), SourceLocation{}); |
| 1582 |
TemplateArgumentLoc Output; |
| 1583 |
if (TransformTemplateArgument(Input, Output, Uneval)) |
| 1584 |
return true; // fails |
| 1585 |
TArgs.push_back(Output.getArgument()); |
| 1586 |
} |
| 1587 |
Output = SemaRef.getTrivialTemplateArgumentLoc( |
| 1588 |
TemplateArgument(llvm::ArrayRef(TArgs).copy(SemaRef.Context)), |
| 1589 |
QualType(), SourceLocation{}); |
| 1590 |
return false; |
| 1591 |
default: |
| 1592 |
break; |
| 1593 |
} |
| 1594 |
return inherited::TransformTemplateArgument(Input, Output, Uneval); |
| 1595 |
} |
| 1596 |
|
| 1597 |
using TreeTransform::TransformTemplateSpecializationType; |
| 1598 |
QualType |
| 1599 |
TransformTemplateSpecializationType(TypeLocBuilder &TLB, |
| 1600 |
TemplateSpecializationTypeLoc TL) { |
| 1601 |
auto *T = TL.getTypePtr(); |
| 1602 |
if (!getSema().ArgPackSubstIndex || !T->isSugared() || |
| 1603 |
!isPackProducingBuiltinTemplateName(T->getTemplateName())) |
| 1604 |
return TreeTransform::TransformTemplateSpecializationType(TLB, TL); |
| 1605 |
// Look through sugar to get to the SubstBuiltinTemplatePackType that we |
| 1606 |
// need to substitute into. |
| 1607 |
|
| 1608 |
// `TransformType` code below will handle picking the element from a pack |
| 1609 |
// with the index `ArgPackSubstIndex`. |
| 1610 |
// FIXME: add ability to represent sugarred type for N-th element of a |
| 1611 |
// builtin pack and produce the sugar here. |
| 1612 |
QualType R = TransformType(T->desugar()); |
| 1613 |
TLB.pushTrivial(getSema().getASTContext(), R, TL.getBeginLoc()); |
| 1614 |
return R; |
| 1615 |
} |
| 1616 |
|
| 1617 |
UnsignedOrNone ComputeSizeOfPackExprWithoutSubstitution( |
| 1618 |
ArrayRef<TemplateArgument> PackArgs) { |
| 1619 |
// Don't do this when rewriting template parameters for CTAD: |
| 1620 |
// 1) The heuristic needs the unpacked Subst* nodes to figure out the |
| 1621 |
// expanded size, but this never applies since Subst* nodes are not |
| 1622 |
// created in rewrite scenarios. |
| 1623 |
// |
| 1624 |
// 2) The heuristic substitutes into the pattern with pack expansion |
| 1625 |
// suppressed, which does not meet the requirements for argument |
| 1626 |
// rewriting when template arguments include a non-pack matching against |
| 1627 |
// a pack, particularly when rewriting an alias CTAD. |
| 1628 |
if (TemplateArgs.isRewrite()) |
| 1629 |
return std::nullopt; |
| 1630 |
|
| 1631 |
return inherited::ComputeSizeOfPackExprWithoutSubstitution(PackArgs); |
| 1632 |
} |
| 1633 |
|
| 1634 |
template<typename Fn> |
| 1635 |
QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 1636 |
FunctionProtoTypeLoc TL, |
| 1637 |
CXXRecordDecl *ThisContext, |
| 1638 |
Qualifiers ThisTypeQuals, |
| 1639 |
Fn TransformExceptionSpec); |
| 1640 |
|
| 1641 |
ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm, |
| 1642 |
int indexAdjustment, |
| 1643 |
UnsignedOrNone NumExpansions, |
| 1644 |
bool ExpectParameterPack); |
| 1645 |
|
| 1646 |
using inherited::TransformTemplateTypeParmType; |
| 1647 |
/// Transforms a template type parameter type by performing |
| 1648 |
/// substitution of the corresponding template type argument. |
| 1649 |
QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 1650 |
TemplateTypeParmTypeLoc TL, |
| 1651 |
bool SuppressObjCLifetime); |
| 1652 |
|
| 1653 |
QualType BuildSubstTemplateTypeParmType( |
| 1654 |
TypeLocBuilder &TLB, bool SuppressObjCLifetime, bool Final, |
| 1655 |
Decl *AssociatedDecl, unsigned Index, UnsignedOrNone PackIndex, |
| 1656 |
TemplateArgument Arg, SourceLocation NameLoc); |
| 1657 |
|
| 1658 |
/// Transforms an already-substituted template type parameter pack |
| 1659 |
/// into either itself (if we aren't substituting into its pack expansion) |
| 1660 |
/// or the appropriate substituted argument. |
| 1661 |
using inherited::TransformSubstTemplateTypeParmPackType; |
| 1662 |
QualType |
| 1663 |
TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB, |
| 1664 |
SubstTemplateTypeParmPackTypeLoc TL, |
| 1665 |
bool SuppressObjCLifetime); |
| 1666 |
QualType |
| 1667 |
TransformSubstBuiltinTemplatePackType(TypeLocBuilder &TLB, |
| 1668 |
SubstBuiltinTemplatePackTypeLoc TL); |
| 1669 |
|
| 1670 |
CXXRecordDecl::LambdaDependencyKind |
| 1671 |
ComputeLambdaDependency(LambdaScopeInfo *LSI) { |
| 1672 |
if (auto TypeAlias = |
| 1673 |
TemplateInstArgsHelpers::getEnclosingTypeAliasTemplateDecl( |
| 1674 |
getSema()); |
| 1675 |
TypeAlias && TemplateInstArgsHelpers::isLambdaEnclosedByTypeAliasDecl( |
| 1676 |
LSI->CallOperator, TypeAlias.PrimaryTypeAliasDecl)) { |
| 1677 |
unsigned TypeAliasDeclDepth = TypeAlias.Template->getTemplateDepth(); |
| 1678 |
if (TypeAliasDeclDepth >= TemplateArgs.getNumSubstitutedLevels()) |
| 1679 |
return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent; |
| 1680 |
for (const TemplateArgument &TA : TypeAlias.AssociatedTemplateArguments) |
| 1681 |
if (TA.isDependent()) |
| 1682 |
return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent; |
| 1683 |
} |
| 1684 |
return inherited::ComputeLambdaDependency(LSI); |
| 1685 |
} |
| 1686 |
|
| 1687 |
ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 1688 |
// Do not rebuild lambdas to avoid creating a new type. |
| 1689 |
// Lambdas have already been processed inside their eval contexts. |
| 1690 |
if (SemaRef.RebuildingImmediateInvocation) |
| 1691 |
return E; |
| 1692 |
LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true, |
| 1693 |
/*InstantiatingLambdaOrBlock=*/true); |
| 1694 |
Sema::ConstraintEvalRAII<TemplateInstantiator> RAII(*this); |
| 1695 |
|
| 1696 |
return inherited::TransformLambdaExpr(E); |
| 1697 |
} |
| 1698 |
|
| 1699 |
ExprResult TransformBlockExpr(BlockExpr *E) { |
| 1700 |
LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true, |
| 1701 |
/*InstantiatingLambdaOrBlock=*/true); |
| 1702 |
return inherited::TransformBlockExpr(E); |
| 1703 |
} |
| 1704 |
|
| 1705 |
ExprResult RebuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc, |
| 1706 |
LambdaScopeInfo *LSI) { |
| 1707 |
CXXMethodDecl *MD = LSI->CallOperator; |
| 1708 |
for (ParmVarDecl *PVD : MD->parameters()) { |
| 1709 |
assert(PVD && "null in a parameter list"); |
| 1710 |
if (!PVD->hasDefaultArg()) |
| 1711 |
continue; |
| 1712 |
Expr *UninstExpr = PVD->getUninstantiatedDefaultArg(); |
| 1713 |
// FIXME: Obtain the source location for the '=' token. |
| 1714 |
SourceLocation EqualLoc = UninstExpr->getBeginLoc(); |
| 1715 |
if (SemaRef.SubstDefaultArgument(EqualLoc, PVD, TemplateArgs)) { |
| 1716 |
// If substitution fails, the default argument is set to a |
| 1717 |
// RecoveryExpr that wraps the uninstantiated default argument so |
| 1718 |
// that downstream diagnostics are omitted. |
| 1719 |
ExprResult ErrorResult = SemaRef.CreateRecoveryExpr( |
| 1720 |
UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(), {UninstExpr}, |
| 1721 |
UninstExpr->getType()); |
| 1722 |
if (ErrorResult.isUsable()) |
| 1723 |
PVD->setDefaultArg(ErrorResult.get()); |
| 1724 |
} |
| 1725 |
} |
| 1726 |
return inherited::RebuildLambdaExpr(StartLoc, EndLoc, LSI); |
| 1727 |
} |
| 1728 |
|
| 1729 |
StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body) { |
| 1730 |
// Currently, we instantiate the body when instantiating the lambda |
| 1731 |
// expression. However, `EvaluateConstraints` is disabled during the |
| 1732 |
// instantiation of the lambda expression, causing the instantiation |
| 1733 |
// failure of the return type requirement in the body. If p0588r1 is fully |
| 1734 |
// implemented, the body will be lazily instantiated, and this problem |
| 1735 |
// will not occur. Here, `EvaluateConstraints` is temporarily set to |
| 1736 |
// `true` to temporarily fix this issue. |
| 1737 |
// FIXME: This temporary fix can be removed after fully implementing |
| 1738 |
// p0588r1. |
| 1739 |
llvm::SaveAndRestore _(EvaluateConstraints, true); |
| 1740 |
return inherited::TransformLambdaBody(E, Body); |
| 1741 |
} |
| 1742 |
|
| 1743 |
ExprResult TransformRequiresExpr(RequiresExpr *E) { |
| 1744 |
LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
| 1745 |
ExprResult TransReq = inherited::TransformRequiresExpr(E); |
| 1746 |
if (TransReq.isInvalid()) |
| 1747 |
return TransReq; |
| 1748 |
assert(TransReq.get() != E && |
| 1749 |
"Do not change value of isSatisfied for the existing expression. " |
| 1750 |
"Create a new expression instead."); |
| 1751 |
if (E->getBody()->isDependentContext()) { |
| 1752 |
Sema::SFINAETrap Trap(SemaRef); |
| 1753 |
// We recreate the RequiresExpr body, but not by instantiating it. |
| 1754 |
// Produce pending diagnostics for dependent access check. |
| 1755 |
SemaRef.PerformDependentDiagnostics(E->getBody(), TemplateArgs); |
| 1756 |
// FIXME: Store SFINAE diagnostics in RequiresExpr for diagnosis. |
| 1757 |
if (Trap.hasErrorOccurred()) |
| 1758 |
TransReq.getAs<RequiresExpr>()->setSatisfied(false); |
| 1759 |
} |
| 1760 |
return TransReq; |
| 1761 |
} |
| 1762 |
|
| 1763 |
bool TransformRequiresExprRequirements( |
| 1764 |
ArrayRef<concepts::Requirement *> Reqs, |
| 1765 |
SmallVectorImpl<concepts::Requirement *> &Transformed) { |
| 1766 |
bool SatisfactionDetermined = false; |
| 1767 |
for (concepts::Requirement *Req : Reqs) { |
| 1768 |
concepts::Requirement *TransReq = nullptr; |
| 1769 |
if (!SatisfactionDetermined) { |
| 1770 |
if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) |
| 1771 |
TransReq = TransformTypeRequirement(TypeReq); |
| 1772 |
else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) |
| 1773 |
TransReq = TransformExprRequirement(ExprReq); |
| 1774 |
else |
| 1775 |
TransReq = TransformNestedRequirement( |
| 1776 |
cast<concepts::NestedRequirement>(Req)); |
| 1777 |
if (!TransReq) |
| 1778 |
return true; |
| 1779 |
if (!TransReq->isDependent() && !TransReq->isSatisfied()) |
| 1780 |
// [expr.prim.req]p6 |
| 1781 |
// [...] The substitution and semantic constraint checking |
| 1782 |
// proceeds in lexical order and stops when a condition that |
| 1783 |
// determines the result of the requires-expression is |
| 1784 |
// encountered. [..] |
| 1785 |
SatisfactionDetermined = true; |
| 1786 |
} else |
| 1787 |
TransReq = Req; |
| 1788 |
Transformed.push_back(TransReq); |
| 1789 |
} |
| 1790 |
return false; |
| 1791 |
} |
| 1792 |
|
| 1793 |
TemplateParameterList *TransformTemplateParameterList( |
| 1794 |
TemplateParameterList *OrigTPL) { |
| 1795 |
if (!OrigTPL || !OrigTPL->size()) return OrigTPL; |
| 1796 |
|
| 1797 |
DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext(); |
| 1798 |
TemplateDeclInstantiator DeclInstantiator(getSema(), |
| 1799 |
/* DeclContext *Owner */ Owner, TemplateArgs); |
| 1800 |
DeclInstantiator.setEvaluateConstraints(EvaluateConstraints); |
| 1801 |
return DeclInstantiator.SubstTemplateParams(OrigTPL); |
| 1802 |
} |
| 1803 |
|
| 1804 |
concepts::TypeRequirement * |
| 1805 |
TransformTypeRequirement(concepts::TypeRequirement *Req); |
| 1806 |
concepts::ExprRequirement * |
| 1807 |
TransformExprRequirement(concepts::ExprRequirement *Req); |
| 1808 |
concepts::NestedRequirement * |
| 1809 |
TransformNestedRequirement(concepts::NestedRequirement *Req); |
| 1810 |
ExprResult TransformRequiresTypeParams( |
| 1811 |
SourceLocation KWLoc, SourceLocation RBraceLoc, const RequiresExpr *RE, |
| 1812 |
RequiresExprBodyDecl *Body, ArrayRef<ParmVarDecl *> Params, |
| 1813 |
SmallVectorImpl<QualType> &PTypes, |
| 1814 |
SmallVectorImpl<ParmVarDecl *> &TransParams, |
| 1815 |
Sema::ExtParameterInfoBuilder &PInfos); |
| 1816 |
}; |
| 1817 |
} |
| 1818 |
|
| 1819 |
bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
| 1820 |
if (T.isNull()) |
| 1821 |
return true; |
| 1822 |
|
| 1823 |
if (T->isInstantiationDependentType() || T->isVariablyModifiedType() || |
| 1824 |
T->containsUnexpandedParameterPack()) |
| 1825 |
return false; |
| 1826 |
|
| 1827 |
getSema().MarkDeclarationsReferencedInType(Loc, T); |
| 1828 |
return true; |
| 1829 |
} |
| 1830 |
|
| 1831 |
Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
| 1832 |
if (!D) |
| 1833 |
return nullptr; |
| 1834 |
|
| 1835 |
if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
| 1836 |
if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 1837 |
// If the corresponding template argument is NULL or non-existent, it's |
| 1838 |
// because we are performing instantiation from explicitly-specified |
| 1839 |
// template arguments in a function template, but there were some |
| 1840 |
// arguments left unspecified. |
| 1841 |
if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 1842 |
TTP->getPosition())) { |
| 1843 |
IsIncomplete = true; |
| 1844 |
return BailOutOnIncomplete ? nullptr : D; |
| 1845 |
} |
| 1846 |
|
| 1847 |
TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| 1848 |
|
| 1849 |
if (TTP->isParameterPack()) { |
| 1850 |
assert(Arg.getKind() == TemplateArgument::Pack && |
| 1851 |
"Missing argument pack"); |
| 1852 |
Arg = SemaRef.getPackSubstitutedTemplateArgument(Arg); |
| 1853 |
} |
| 1854 |
|
| 1855 |
TemplateName Template = Arg.getAsTemplate(); |
| 1856 |
assert(!Template.isNull() && Template.getAsTemplateDecl() && |
| 1857 |
"Wrong kind of template template argument"); |
| 1858 |
return Template.getAsTemplateDecl(); |
| 1859 |
} |
| 1860 |
|
| 1861 |
// Fall through to find the instantiated declaration for this template |
| 1862 |
// template parameter. |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D); |
| 1866 |
PVD && SemaRef.CurrentInstantiationScope && |
| 1867 |
(SemaRef.inConstraintSubstitution() || |
| 1868 |
SemaRef.inParameterMappingSubstitution()) && |
| 1869 |
maybeInstantiateFunctionParameterToScope(PVD)) |
| 1870 |
return nullptr; |
| 1871 |
|
| 1872 |
return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
| 1873 |
} |
| 1874 |
|
| 1875 |
bool TemplateInstantiator::maybeInstantiateFunctionParameterToScope( |
| 1876 |
ParmVarDecl *OldParm) { |
| 1877 |
if (SemaRef.CurrentInstantiationScope->getInstantiationOfIfExists(OldParm)) |
| 1878 |
return false; |
| 1879 |
|
| 1880 |
if (!OldParm->isParameterPack()) |
| 1881 |
return !TransformFunctionTypeParam(OldParm, /*indexAdjustment=*/0, |
| 1882 |
/*NumExpansions=*/std::nullopt, |
| 1883 |
/*ExpectParameterPack=*/false); |
| 1884 |
|
| 1885 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 1886 |
|
| 1887 |
// Find the parameter packs that could be expanded. |
| 1888 |
TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc(); |
| 1889 |
PackExpansionTypeLoc ExpansionTL = TL.castAs<PackExpansionTypeLoc>(); |
| 1890 |
TypeLoc Pattern = ExpansionTL.getPatternLoc(); |
| 1891 |
SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 1892 |
assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 1893 |
|
| 1894 |
bool ShouldExpand = false; |
| 1895 |
bool RetainExpansion = false; |
| 1896 |
UnsignedOrNone OrigNumExpansions = |
| 1897 |
ExpansionTL.getTypePtr()->getNumExpansions(); |
| 1898 |
UnsignedOrNone NumExpansions = OrigNumExpansions; |
| 1899 |
if (TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(), |
| 1900 |
Pattern.getSourceRange(), Unexpanded, |
| 1901 |
/*FailOnPackProducingTemplates=*/true, |
| 1902 |
ShouldExpand, RetainExpansion, NumExpansions)) |
| 1903 |
return true; |
| 1904 |
|
| 1905 |
assert(ShouldExpand && !RetainExpansion && |
| 1906 |
"Shouldn't preserve pack expansion when evaluating constraints"); |
| 1907 |
ExpandingFunctionParameterPack(OldParm); |
| 1908 |
for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 1909 |
Sema::ArgPackSubstIndexRAII SubstIndex(getSema(), I); |
| 1910 |
if (!TransformFunctionTypeParam(OldParm, /*indexAdjustment=*/0, |
| 1911 |
/*NumExpansions=*/OrigNumExpansions, |
| 1912 |
/*ExpectParameterPack=*/false)) |
| 1913 |
return true; |
| 1914 |
} |
| 1915 |
return false; |
| 1916 |
} |
| 1917 |
|
| 1918 |
Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
| 1919 |
Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
| 1920 |
if (!Inst) |
| 1921 |
return nullptr; |
| 1922 |
|
| 1923 |
getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 1924 |
return Inst; |
| 1925 |
} |
| 1926 |
|
| 1927 |
bool TemplateInstantiator::TransformExceptionSpec( |
| 1928 |
SourceLocation Loc, FunctionProtoType::ExceptionSpecInfo &ESI, |
| 1929 |
SmallVectorImpl<QualType> &Exceptions, bool &Changed) { |
| 1930 |
if (ESI.Type == EST_Uninstantiated) { |
| 1931 |
ESI.instantiate(); |
| 1932 |
Changed = true; |
| 1933 |
} |
| 1934 |
return inherited::TransformExceptionSpec(Loc, ESI, Exceptions, Changed); |
| 1935 |
} |
| 1936 |
|
| 1937 |
NamedDecl * |
| 1938 |
TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
| 1939 |
SourceLocation Loc) { |
| 1940 |
// If the first part of the nested-name-specifier was a template type |
| 1941 |
// parameter, instantiate that type parameter down to a tag type. |
| 1942 |
if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
| 1943 |
const TemplateTypeParmType *TTP |
| 1944 |
= cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
| 1945 |
|
| 1946 |
if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 1947 |
// FIXME: This needs testing w/ member access expressions. |
| 1948 |
TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); |
| 1949 |
|
| 1950 |
if (TTP->isParameterPack()) { |
| 1951 |
assert(Arg.getKind() == TemplateArgument::Pack && |
| 1952 |
"Missing argument pack"); |
| 1953 |
|
| 1954 |
if (!getSema().ArgPackSubstIndex) |
| 1955 |
return nullptr; |
| 1956 |
|
| 1957 |
Arg = SemaRef.getPackSubstitutedTemplateArgument(Arg); |
| 1958 |
} |
| 1959 |
|
| 1960 |
QualType T = Arg.getAsType(); |
| 1961 |
if (T.isNull()) |
| 1962 |
return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
| 1963 |
|
| 1964 |
if (const TagType *Tag = T->getAs<TagType>()) |
| 1965 |
return Tag->getDecl(); |
| 1966 |
|
| 1967 |
// The resulting type is not a tag; complain. |
| 1968 |
getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
| 1969 |
return nullptr; |
| 1970 |
} |
| 1971 |
} |
| 1972 |
|
| 1973 |
return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
| 1974 |
} |
| 1975 |
|
| 1976 |
VarDecl * |
| 1977 |
TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
| 1978 |
TypeSourceInfo *Declarator, |
| 1979 |
SourceLocation StartLoc, |
| 1980 |
SourceLocation NameLoc, |
| 1981 |
IdentifierInfo *Name) { |
| 1982 |
VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, |
| 1983 |
StartLoc, NameLoc, Name); |
| 1984 |
if (Var) |
| 1985 |
getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 1986 |
return Var; |
| 1987 |
} |
| 1988 |
|
| 1989 |
VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
| 1990 |
TypeSourceInfo *TSInfo, |
| 1991 |
QualType T) { |
| 1992 |
VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
| 1993 |
if (Var) |
| 1994 |
getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
| 1995 |
return Var; |
| 1996 |
} |
| 1997 |
|
| 1998 |
TemplateName TemplateInstantiator::TransformTemplateName( |
| 1999 |
NestedNameSpecifierLoc &QualifierLoc, SourceLocation TemplateKWLoc, |
| 2000 |
TemplateName Name, SourceLocation NameLoc, QualType ObjectType, |
| 2001 |
NamedDecl *FirstQualifierInScope, bool AllowInjectedClassName) { |
| 2002 |
if (Name.getKind() == TemplateName::Template) { |
| 2003 |
assert(!QualifierLoc && "Unexpected qualifier"); |
| 2004 |
if (auto *TTP = |
| 2005 |
dyn_cast<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
| 2006 |
TTP && TTP->getDepth() < TemplateArgs.getNumLevels()) { |
| 2007 |
// If the corresponding template argument is NULL or non-existent, it's |
| 2008 |
// because we are performing instantiation from explicitly-specified |
| 2009 |
// template arguments in a function template, but there were some |
| 2010 |
// arguments left unspecified. |
| 2011 |
if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
| 2012 |
TTP->getPosition())) { |
| 2013 |
IsIncomplete = true; |
| 2014 |
return BailOutOnIncomplete ? TemplateName() : Name; |
| 2015 |
} |
| 2016 |
|
| 2017 |
TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
| 2018 |
|
| 2019 |
if (TemplateArgs.isRewrite()) { |
| 2020 |
// We're rewriting the template parameter as a reference to another |
| 2021 |
// template parameter. |
| 2022 |
Arg = getTemplateArgumentPackPatternForRewrite(Arg); |
| 2023 |
assert(Arg.getKind() == TemplateArgument::Template && |
| 2024 |
"unexpected nontype template argument kind in template rewrite"); |
| 2025 |
return Arg.getAsTemplate(); |
| 2026 |
} |
| 2027 |
|
| 2028 |
auto [AssociatedDecl, Final] = |
| 2029 |
TemplateArgs.getAssociatedDecl(TTP->getDepth()); |
| 2030 |
UnsignedOrNone PackIndex = std::nullopt; |
| 2031 |
if (TTP->isParameterPack()) { |
| 2032 |
assert(Arg.getKind() == TemplateArgument::Pack && |
| 2033 |
"Missing argument pack"); |
| 2034 |
|
| 2035 |
if (!getSema().ArgPackSubstIndex) { |
| 2036 |
// We have the template argument pack to substitute, but we're not |
| 2037 |
// actually expanding the enclosing pack expansion yet. So, just |
| 2038 |
// keep the entire argument pack. |
| 2039 |
return getSema().Context.getSubstTemplateTemplateParmPack( |
| 2040 |
Arg, AssociatedDecl, TTP->getIndex(), Final); |
| 2041 |
} |
| 2042 |
|
| 2043 |
PackIndex = SemaRef.getPackIndex(Arg); |
| 2044 |
Arg = SemaRef.getPackSubstitutedTemplateArgument(Arg); |
| 2045 |
} |
| 2046 |
|
| 2047 |
TemplateName Template = Arg.getAsTemplate(); |
| 2048 |
assert(!Template.isNull() && "Null template template argument"); |
| 2049 |
return getSema().Context.getSubstTemplateTemplateParm( |
| 2050 |
Template, AssociatedDecl, TTP->getIndex(), PackIndex, Final); |
| 2051 |
} |
| 2052 |
} |
| 2053 |
|
| 2054 |
if (SubstTemplateTemplateParmPackStorage *SubstPack |
| 2055 |
= Name.getAsSubstTemplateTemplateParmPack()) { |
| 2056 |
if (!getSema().ArgPackSubstIndex) |
| 2057 |
return Name; |
| 2058 |
|
| 2059 |
TemplateArgument Pack = SubstPack->getArgumentPack(); |
| 2060 |
TemplateName Template = |
| 2061 |
SemaRef.getPackSubstitutedTemplateArgument(Pack).getAsTemplate(); |
| 2062 |
return getSema().Context.getSubstTemplateTemplateParm( |
| 2063 |
Template, SubstPack->getAssociatedDecl(), SubstPack->getIndex(), |
| 2064 |
SemaRef.getPackIndex(Pack), SubstPack->getFinal()); |
| 2065 |
} |
| 2066 |
|
| 2067 |
return inherited::TransformTemplateName( |
| 2068 |
QualifierLoc, TemplateKWLoc, Name, NameLoc, ObjectType, |
| 2069 |
FirstQualifierInScope, AllowInjectedClassName); |
| 2070 |
} |
| 2071 |
|
| 2072 |
ExprResult |
| 2073 |
TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
| 2074 |
if (!E->isTypeDependent()) |
| 2075 |
return E; |
| 2076 |
|
| 2077 |
return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentKind()); |
| 2078 |
} |
| 2079 |
|
| 2080 |
ExprResult |
| 2081 |
TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
| 2082 |
NonTypeTemplateParmDecl *NTTP) { |
| 2083 |
// If the corresponding template argument is NULL or non-existent, it's |
| 2084 |
// because we are performing instantiation from explicitly-specified |
| 2085 |
// template arguments in a function template, but there were some |
| 2086 |
// arguments left unspecified. |
| 2087 |
if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
| 2088 |
NTTP->getPosition())) { |
| 2089 |
IsIncomplete = true; |
| 2090 |
return BailOutOnIncomplete ? ExprError() : E; |
| 2091 |
} |
| 2092 |
|
| 2093 |
TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); |
| 2094 |
|
| 2095 |
if (TemplateArgs.isRewrite()) { |
| 2096 |
// We're rewriting the template parameter as a reference to another |
| 2097 |
// template parameter. |
| 2098 |
Arg = getTemplateArgumentPackPatternForRewrite(Arg); |
| 2099 |
assert(Arg.getKind() == TemplateArgument::Expression && |
| 2100 |
"unexpected nontype template argument kind in template rewrite"); |
| 2101 |
// FIXME: This can lead to the same subexpression appearing multiple times |
| 2102 |
// in a complete expression. |
| 2103 |
return Arg.getAsExpr(); |
| 2104 |
} |
| 2105 |
|
| 2106 |
auto [AssociatedDecl, Final] = |
| 2107 |
TemplateArgs.getAssociatedDecl(NTTP->getDepth()); |
| 2108 |
UnsignedOrNone PackIndex = std::nullopt; |
| 2109 |
if (NTTP->isParameterPack()) { |
| 2110 |
assert(Arg.getKind() == TemplateArgument::Pack && |
| 2111 |
"Missing argument pack"); |
| 2112 |
|
| 2113 |
if (!getSema().ArgPackSubstIndex) { |
| 2114 |
// We have an argument pack, but we can't select a particular argument |
| 2115 |
// out of it yet. Therefore, we'll build an expression to hold on to that |
| 2116 |
// argument pack. |
| 2117 |
QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
| 2118 |
E->getLocation(), |
| 2119 |
NTTP->getDeclName()); |
| 2120 |
if (TargetType.isNull()) |
| 2121 |
return ExprError(); |
| 2122 |
|
| 2123 |
QualType ExprType = TargetType.getNonLValueExprType(SemaRef.Context); |
| 2124 |
if (TargetType->isRecordType()) |
| 2125 |
ExprType.addConst(); |
| 2126 |
return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr( |
| 2127 |
ExprType, TargetType->isReferenceType() ? VK_LValue : VK_PRValue, |
| 2128 |
E->getLocation(), Arg, AssociatedDecl, NTTP->getPosition(), Final); |
| 2129 |
} |
| 2130 |
PackIndex = SemaRef.getPackIndex(Arg); |
| 2131 |
Arg = SemaRef.getPackSubstitutedTemplateArgument(Arg); |
| 2132 |
} |
| 2133 |
return SemaRef.BuildSubstNonTypeTemplateParmExpr( |
| 2134 |
AssociatedDecl, NTTP, E->getLocation(), Arg, PackIndex, Final); |
| 2135 |
} |
| 2136 |
|
| 2137 |
const AnnotateAttr * |
| 2138 |
TemplateInstantiator::TransformAnnotateAttr(const AnnotateAttr *AA) { |
| 2139 |
SmallVector<Expr *> Args; |
| 2140 |
for (Expr *Arg : AA->args()) { |
| 2141 |
ExprResult Res = getDerived().TransformExpr(Arg); |
| 2142 |
if (Res.isUsable()) |
| 2143 |
Args.push_back(Res.get()); |
| 2144 |
} |
| 2145 |
return AnnotateAttr::CreateImplicit(getSema().Context, AA->getAnnotation(), |
| 2146 |
Args.data(), Args.size(), AA->getRange()); |
| 2147 |
} |
| 2148 |
|
| 2149 |
const CXXAssumeAttr * |
| 2150 |
TemplateInstantiator::TransformCXXAssumeAttr(const CXXAssumeAttr *AA) { |
| 2151 |
ExprResult Res = getDerived().TransformExpr(AA->getAssumption()); |
| 2152 |
if (!Res.isUsable()) |
| 2153 |
return AA; |
| 2154 |
|
| 2155 |
if (!(Res.get()->getDependence() & ExprDependence::TypeValueInstantiation)) { |
| 2156 |
Res = getSema().BuildCXXAssumeExpr(Res.get(), AA->getAttrName(), |
| 2157 |
AA->getRange()); |
| 2158 |
if (!Res.isUsable()) |
| 2159 |
return AA; |
| 2160 |
} |
| 2161 |
|
| 2162 |
return CXXAssumeAttr::CreateImplicit(getSema().Context, Res.get(), |
| 2163 |
AA->getRange()); |
| 2164 |
} |
| 2165 |
|
| 2166 |
const LoopHintAttr * |
| 2167 |
TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) { |
| 2168 |
Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get(); |
| 2169 |
|
| 2170 |
if (TransformedExpr == LH->getValue()) |
| 2171 |
return LH; |
| 2172 |
|
| 2173 |
// Generate error if there is a problem with the value. |
| 2174 |
if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation(), |
| 2175 |
LH->getSemanticSpelling() == |
| 2176 |
LoopHintAttr::Pragma_unroll)) |
| 2177 |
return LH; |
| 2178 |
|
| 2179 |
LoopHintAttr::OptionType Option = LH->getOption(); |
| 2180 |
LoopHintAttr::LoopHintState State = LH->getState(); |
| 2181 |
|
| 2182 |
llvm::APSInt ValueAPS = |
| 2183 |
TransformedExpr->EvaluateKnownConstInt(getSema().getASTContext()); |
| 2184 |
// The values of 0 and 1 block any unrolling of the loop. |
| 2185 |
if (ValueAPS.isZero() || ValueAPS.isOne()) { |
| 2186 |
Option = LoopHintAttr::Unroll; |
| 2187 |
State = LoopHintAttr::Disable; |
| 2188 |
} |
| 2189 |
|
| 2190 |
// Create new LoopHintValueAttr with integral expression in place of the |
| 2191 |
// non-type template parameter. |
| 2192 |
return LoopHintAttr::CreateImplicit(getSema().Context, Option, State, |
| 2193 |
TransformedExpr, *LH); |
| 2194 |
} |
| 2195 |
const NoInlineAttr *TemplateInstantiator::TransformStmtNoInlineAttr( |
| 2196 |
const Stmt *OrigS, const Stmt *InstS, const NoInlineAttr *A) { |
| 2197 |
if (!A || getSema().CheckNoInlineAttr(OrigS, InstS, *A)) |
| 2198 |
return nullptr; |
| 2199 |
|
| 2200 |
return A; |
| 2201 |
} |
| 2202 |
const AlwaysInlineAttr *TemplateInstantiator::TransformStmtAlwaysInlineAttr( |
| 2203 |
const Stmt *OrigS, const Stmt *InstS, const AlwaysInlineAttr *A) { |
| 2204 |
if (!A || getSema().CheckAlwaysInlineAttr(OrigS, InstS, *A)) |
| 2205 |
return nullptr; |
| 2206 |
|
| 2207 |
return A; |
| 2208 |
} |
| 2209 |
|
| 2210 |
const CodeAlignAttr * |
| 2211 |
TemplateInstantiator::TransformCodeAlignAttr(const CodeAlignAttr *CA) { |
| 2212 |
Expr *TransformedExpr = getDerived().TransformExpr(CA->getAlignment()).get(); |
| 2213 |
return getSema().BuildCodeAlignAttr(*CA, TransformedExpr); |
| 2214 |
} |
| 2215 |
const OpenACCRoutineDeclAttr * |
| 2216 |
TemplateInstantiator::TransformOpenACCRoutineDeclAttr( |
| 2217 |
const OpenACCRoutineDeclAttr *A) { |
| 2218 |
llvm_unreachable("RoutineDecl should only be a declaration attribute, as it " |
| 2219 |
"applies to a Function Decl (and a few places for VarDecl)"); |
| 2220 |
} |
| 2221 |
|
| 2222 |
ExprResult TemplateInstantiator::RebuildVarDeclRefExpr(ValueDecl *PD, |
| 2223 |
SourceLocation Loc) { |
| 2224 |
DeclarationNameInfo NameInfo(PD->getDeclName(), Loc); |
| 2225 |
return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD); |
| 2226 |
} |
| 2227 |
|
| 2228 |
ExprResult |
| 2229 |
TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) { |
| 2230 |
if (getSema().ArgPackSubstIndex) { |
| 2231 |
// We can expand this parameter pack now. |
| 2232 |
ValueDecl *D = E->getExpansion(*getSema().ArgPackSubstIndex); |
| 2233 |
ValueDecl *VD = cast_or_null<ValueDecl>(TransformDecl(E->getExprLoc(), D)); |
| 2234 |
if (!VD) |
| 2235 |
return ExprError(); |
| 2236 |
return RebuildVarDeclRefExpr(VD, E->getExprLoc()); |
| 2237 |
} |
| 2238 |
|
| 2239 |
QualType T = TransformType(E->getType()); |
| 2240 |
if (T.isNull()) |
| 2241 |
return ExprError(); |
| 2242 |
|
| 2243 |
// Transform each of the parameter expansions into the corresponding |
| 2244 |
// parameters in the instantiation of the function decl. |
| 2245 |
SmallVector<ValueDecl *, 8> Vars; |
| 2246 |
Vars.reserve(E->getNumExpansions()); |
| 2247 |
for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end(); |
| 2248 |
I != End; ++I) { |
| 2249 |
ValueDecl *D = cast_or_null<ValueDecl>(TransformDecl(E->getExprLoc(), *I)); |
| 2250 |
if (!D) |
| 2251 |
return ExprError(); |
| 2252 |
Vars.push_back(D); |
| 2253 |
} |
| 2254 |
|
| 2255 |
auto *PackExpr = |
| 2256 |
FunctionParmPackExpr::Create(getSema().Context, T, E->getParameterPack(), |
| 2257 |
E->getParameterPackLocation(), Vars); |
| 2258 |
getSema().MarkFunctionParmPackReferenced(PackExpr); |
| 2259 |
return PackExpr; |
| 2260 |
} |
| 2261 |
|
| 2262 |
ExprResult |
| 2263 |
TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E, |
| 2264 |
ValueDecl *PD) { |
| 2265 |
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 2266 |
llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
| 2267 |
= getSema().CurrentInstantiationScope->findInstantiationOf(PD); |
| 2268 |
assert(Found && "no instantiation for parameter pack"); |
| 2269 |
|
| 2270 |
Decl *TransformedDecl; |
| 2271 |
if (DeclArgumentPack *Pack = dyn_cast<DeclArgumentPack *>(*Found)) { |
| 2272 |
// If this is a reference to a function parameter pack which we can |
| 2273 |
// substitute but can't yet expand, build a FunctionParmPackExpr for it. |
| 2274 |
if (!getSema().ArgPackSubstIndex) { |
| 2275 |
QualType T = TransformType(E->getType()); |
| 2276 |
if (T.isNull()) |
| 2277 |
return ExprError(); |
| 2278 |
auto *PackExpr = FunctionParmPackExpr::Create(getSema().Context, T, PD, |
| 2279 |
E->getExprLoc(), *Pack); |
| 2280 |
getSema().MarkFunctionParmPackReferenced(PackExpr); |
| 2281 |
return PackExpr; |
| 2282 |
} |
| 2283 |
|
| 2284 |
TransformedDecl = (*Pack)[*getSema().ArgPackSubstIndex]; |
| 2285 |
} else { |
| 2286 |
TransformedDecl = cast<Decl *>(*Found); |
| 2287 |
} |
| 2288 |
|
| 2289 |
// We have either an unexpanded pack or a specific expansion. |
| 2290 |
return RebuildVarDeclRefExpr(cast<ValueDecl>(TransformedDecl), |
| 2291 |
E->getExprLoc()); |
| 2292 |
} |
| 2293 |
|
| 2294 |
ExprResult |
| 2295 |
TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
| 2296 |
NamedDecl *D = E->getDecl(); |
| 2297 |
|
| 2298 |
// Handle references to non-type template parameters and non-type template |
| 2299 |
// parameter packs. |
| 2300 |
if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 2301 |
if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
| 2302 |
return TransformTemplateParmRefExpr(E, NTTP); |
| 2303 |
|
| 2304 |
// We have a non-type template parameter that isn't fully substituted; |
| 2305 |
// FindInstantiatedDecl will find it in the local instantiation scope. |
| 2306 |
} |
| 2307 |
|
| 2308 |
// Handle references to function parameter packs. |
| 2309 |
if (VarDecl *PD = dyn_cast<VarDecl>(D)) |
| 2310 |
if (PD->isParameterPack()) |
| 2311 |
return TransformFunctionParmPackRefExpr(E, PD); |
| 2312 |
|
| 2313 |
return inherited::TransformDeclRefExpr(E); |
| 2314 |
} |
| 2315 |
|
| 2316 |
ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
| 2317 |
CXXDefaultArgExpr *E) { |
| 2318 |
assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
| 2319 |
getDescribedFunctionTemplate() && |
| 2320 |
"Default arg expressions are never formed in dependent cases."); |
| 2321 |
return SemaRef.BuildCXXDefaultArgExpr( |
| 2322 |
E->getUsedLocation(), cast<FunctionDecl>(E->getParam()->getDeclContext()), |
| 2323 |
E->getParam()); |
| 2324 |
} |
| 2325 |
|
| 2326 |
template<typename Fn> |
| 2327 |
QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
| 2328 |
FunctionProtoTypeLoc TL, |
| 2329 |
CXXRecordDecl *ThisContext, |
| 2330 |
Qualifiers ThisTypeQuals, |
| 2331 |
Fn TransformExceptionSpec) { |
| 2332 |
// If this is a lambda or block, the transformation MUST be done in the |
| 2333 |
// CurrentInstantiationScope since it introduces a mapping of |
| 2334 |
// the original to the newly created transformed parameters. |
| 2335 |
// |
| 2336 |
// In that case, TemplateInstantiator::TransformLambdaExpr will |
| 2337 |
// have already pushed a scope for this prototype, so don't create |
| 2338 |
// a second one. |
| 2339 |
LocalInstantiationScope *Current = getSema().CurrentInstantiationScope; |
| 2340 |
std::optional<LocalInstantiationScope> Scope; |
| 2341 |
if (!Current || !Current->isLambdaOrBlock()) |
| 2342 |
Scope.emplace(SemaRef, /*CombineWithOuterScope=*/true); |
| 2343 |
|
| 2344 |
return inherited::TransformFunctionProtoType( |
| 2345 |
TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec); |
| 2346 |
} |
| 2347 |
|
| 2348 |
ParmVarDecl *TemplateInstantiator::TransformFunctionTypeParam( |
| 2349 |
ParmVarDecl *OldParm, int indexAdjustment, UnsignedOrNone NumExpansions, |
| 2350 |
bool ExpectParameterPack) { |
| 2351 |
auto NewParm = SemaRef.SubstParmVarDecl( |
| 2352 |
OldParm, TemplateArgs, indexAdjustment, NumExpansions, |
| 2353 |
ExpectParameterPack, EvaluateConstraints); |
| 2354 |
if (NewParm && SemaRef.getLangOpts().OpenCL) |
| 2355 |
SemaRef.deduceOpenCLAddressSpace(NewParm); |
| 2356 |
return NewParm; |
| 2357 |
} |
| 2358 |
|
| 2359 |
QualType TemplateInstantiator::BuildSubstTemplateTypeParmType( |
| 2360 |
TypeLocBuilder &TLB, bool SuppressObjCLifetime, bool Final, |
| 2361 |
Decl *AssociatedDecl, unsigned Index, UnsignedOrNone PackIndex, |
| 2362 |
TemplateArgument Arg, SourceLocation NameLoc) { |
| 2363 |
QualType Replacement = Arg.getAsType(); |
| 2364 |
|
| 2365 |
// If the template parameter had ObjC lifetime qualifiers, |
| 2366 |
// then any such qualifiers on the replacement type are ignored. |
| 2367 |
if (SuppressObjCLifetime) { |
| 2368 |
Qualifiers RQs; |
| 2369 |
RQs = Replacement.getQualifiers(); |
| 2370 |
RQs.removeObjCLifetime(); |
| 2371 |
Replacement = |
| 2372 |
SemaRef.Context.getQualifiedType(Replacement.getUnqualifiedType(), RQs); |
| 2373 |
} |
| 2374 |
|
| 2375 |
// TODO: only do this uniquing once, at the start of instantiation. |
| 2376 |
QualType Result = getSema().Context.getSubstTemplateTypeParmType( |
| 2377 |
Replacement, AssociatedDecl, Index, PackIndex, Final); |
| 2378 |
SubstTemplateTypeParmTypeLoc NewTL = |
| 2379 |
TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
| 2380 |
NewTL.setNameLoc(NameLoc); |
| 2381 |
return Result; |
| 2382 |
} |
| 2383 |
|
| 2384 |
QualType |
| 2385 |
TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
| 2386 |
TemplateTypeParmTypeLoc TL, |
| 2387 |
bool SuppressObjCLifetime) { |
| 2388 |
const TemplateTypeParmType *T = TL.getTypePtr(); |
| 2389 |
if (T->getDepth() < TemplateArgs.getNumLevels()) { |
| 2390 |
// Replace the template type parameter with its corresponding |
| 2391 |
// template argument. |
| 2392 |
|
| 2393 |
// If the corresponding template argument is NULL or doesn't exist, it's |
| 2394 |
// because we are performing instantiation from explicitly-specified |
| 2395 |
// template arguments in a function template class, but there were some |
| 2396 |
// arguments left unspecified. |
| 2397 |
if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
| 2398 |
IsIncomplete = true; |
| 2399 |
if (BailOutOnIncomplete) |
| 2400 |
return QualType(); |
| 2401 |
|
| 2402 |
TemplateTypeParmTypeLoc NewTL |
| 2403 |
= TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
| 2404 |
NewTL.setNameLoc(TL.getNameLoc()); |
| 2405 |
return TL.getType(); |
| 2406 |
} |
| 2407 |
|
| 2408 |
TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); |
| 2409 |
|
| 2410 |
if (TemplateArgs.isRewrite()) { |
| 2411 |
// We're rewriting the template parameter as a reference to another |
| 2412 |
// template parameter. |
| 2413 |
Arg = getTemplateArgumentPackPatternForRewrite(Arg); |
| 2414 |
assert(Arg.getKind() == TemplateArgument::Type && |
| 2415 |
"unexpected nontype template argument kind in template rewrite"); |
| 2416 |
QualType NewT = Arg.getAsType(); |
| 2417 |
TLB.pushTrivial(SemaRef.Context, NewT, TL.getNameLoc()); |
| 2418 |
return NewT; |
| 2419 |
} |
| 2420 |
|
| 2421 |
auto [AssociatedDecl, Final] = |
| 2422 |
TemplateArgs.getAssociatedDecl(T->getDepth()); |
| 2423 |
UnsignedOrNone PackIndex = std::nullopt; |
| 2424 |
if (T->isParameterPack() || |
| 2425 |
// In concept parameter mapping for fold expressions, packs that aren't |
| 2426 |
// expanded in place are treated as having non-pack dependency, so that |
| 2427 |
// a PackExpansionType won't prevent expanding the packs outside the |
| 2428 |
// TreeTransform. However, we still need to unpack the arguments during |
| 2429 |
// any template argument substitution, so we check the associated |
| 2430 |
// declaration instead. |
| 2431 |
(T->getDecl() && T->getDecl()->isTemplateParameterPack())) { |
| 2432 |
assert(Arg.getKind() == TemplateArgument::Pack && |
| 2433 |
"Missing argument pack"); |
| 2434 |
|
| 2435 |
if (!getSema().ArgPackSubstIndex) { |
| 2436 |
// We have the template argument pack, but we're not expanding the |
| 2437 |
// enclosing pack expansion yet. Just save the template argument |
| 2438 |
// pack for later substitution. |
| 2439 |
QualType Result = getSema().Context.getSubstTemplateTypeParmPackType( |
| 2440 |
AssociatedDecl, T->getIndex(), Final, Arg); |
| 2441 |
SubstTemplateTypeParmPackTypeLoc NewTL |
| 2442 |
= TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
| 2443 |
NewTL.setNameLoc(TL.getNameLoc()); |
| 2444 |
return Result; |
| 2445 |
} |
| 2446 |
|
| 2447 |
// PackIndex starts from last element. |
| 2448 |
PackIndex = SemaRef.getPackIndex(Arg); |
| 2449 |
Arg = SemaRef.getPackSubstitutedTemplateArgument(Arg); |
| 2450 |
} |
| 2451 |
|
| 2452 |
assert(Arg.getKind() == TemplateArgument::Type && |
| 2453 |
"Template argument kind mismatch"); |
| 2454 |
|
| 2455 |
return BuildSubstTemplateTypeParmType(TLB, SuppressObjCLifetime, Final, |
| 2456 |
AssociatedDecl, T->getIndex(), |
| 2457 |
PackIndex, Arg, TL.getNameLoc()); |
| 2458 |
} |
| 2459 |
|
| 2460 |
// The template type parameter comes from an inner template (e.g., |
| 2461 |
// the template parameter list of a member template inside the |
| 2462 |
// template we are instantiating). Create a new template type |
| 2463 |
// parameter with the template "level" reduced by one. |
| 2464 |
TemplateTypeParmDecl *NewTTPDecl = nullptr; |
| 2465 |
if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl()) |
| 2466 |
NewTTPDecl = cast_or_null<TemplateTypeParmDecl>( |
| 2467 |
TransformDecl(TL.getNameLoc(), OldTTPDecl)); |
| 2468 |
QualType Result = getSema().Context.getTemplateTypeParmType( |
| 2469 |
T->getDepth() - TemplateArgs.getNumSubstitutedLevels(), T->getIndex(), |
| 2470 |
T->isParameterPack(), NewTTPDecl); |
| 2471 |
TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 2472 |
NewTL.setNameLoc(TL.getNameLoc()); |
| 2473 |
return Result; |
| 2474 |
} |
| 2475 |
|
| 2476 |
QualType TemplateInstantiator::TransformSubstTemplateTypeParmPackType( |
| 2477 |
TypeLocBuilder &TLB, SubstTemplateTypeParmPackTypeLoc TL, |
| 2478 |
bool SuppressObjCLifetime) { |
| 2479 |
const SubstTemplateTypeParmPackType *T = TL.getTypePtr(); |
| 2480 |
|
| 2481 |
Decl *NewReplaced = TransformDecl(TL.getNameLoc(), T->getAssociatedDecl()); |
| 2482 |
|
| 2483 |
if (!getSema().ArgPackSubstIndex) { |
| 2484 |
// We aren't expanding the parameter pack, so just return ourselves. |
| 2485 |
QualType Result = TL.getType(); |
| 2486 |
if (NewReplaced != T->getAssociatedDecl()) |
| 2487 |
Result = getSema().Context.getSubstTemplateTypeParmPackType( |
| 2488 |
NewReplaced, T->getIndex(), T->getFinal(), T->getArgumentPack()); |
| 2489 |
SubstTemplateTypeParmPackTypeLoc NewTL = |
| 2490 |
TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
| 2491 |
NewTL.setNameLoc(TL.getNameLoc()); |
| 2492 |
return Result; |
| 2493 |
} |
| 2494 |
|
| 2495 |
TemplateArgument Pack = T->getArgumentPack(); |
| 2496 |
TemplateArgument Arg = SemaRef.getPackSubstitutedTemplateArgument(Pack); |
| 2497 |
return BuildSubstTemplateTypeParmType( |
| 2498 |
TLB, SuppressObjCLifetime, T->getFinal(), NewReplaced, T->getIndex(), |
| 2499 |
SemaRef.getPackIndex(Pack), Arg, TL.getNameLoc()); |
| 2500 |
} |
| 2501 |
|
| 2502 |
QualType TemplateInstantiator::TransformSubstBuiltinTemplatePackType( |
| 2503 |
TypeLocBuilder &TLB, SubstBuiltinTemplatePackTypeLoc TL) { |
| 2504 |
if (!getSema().ArgPackSubstIndex) |
| 2505 |
return TreeTransform::TransformSubstBuiltinTemplatePackType(TLB, TL); |
| 2506 |
TemplateArgument Result = SemaRef.getPackSubstitutedTemplateArgument( |
| 2507 |
TL.getTypePtr()->getArgumentPack()); |
| 2508 |
TLB.pushTrivial(SemaRef.getASTContext(), Result.getAsType(), |
| 2509 |
TL.getBeginLoc()); |
| 2510 |
return Result.getAsType(); |
| 2511 |
} |
| 2512 |
|
| 2513 |
static concepts::Requirement::SubstitutionDiagnostic * |
| 2514 |
createSubstDiag(Sema &S, TemplateDeductionInfo &Info, |
| 2515 |
Sema::EntityPrinter Printer) { |
| 2516 |
SmallString<128> Message; |
| 2517 |
SourceLocation ErrorLoc; |
| 2518 |
if (Info.hasSFINAEDiagnostic()) { |
| 2519 |
PartialDiagnosticAt PDA(SourceLocation(), |
| 2520 |
PartialDiagnostic::NullDiagnostic{}); |
| 2521 |
Info.takeSFINAEDiagnostic(PDA); |
| 2522 |
PDA.second.EmitToString(S.getDiagnostics(), Message); |
| 2523 |
ErrorLoc = PDA.first; |
| 2524 |
} else { |
| 2525 |
ErrorLoc = Info.getLocation(); |
| 2526 |
} |
| 2527 |
SmallString<128> Entity; |
| 2528 |
llvm::raw_svector_ostream OS(Entity); |
| 2529 |
Printer(OS); |
| 2530 |
const ASTContext &C = S.Context; |
| 2531 |
return new (C) concepts::Requirement::SubstitutionDiagnostic{ |
| 2532 |
C.backupStr(Entity), ErrorLoc, C.backupStr(Message)}; |
| 2533 |
} |
| 2534 |
|
| 2535 |
concepts::Requirement::SubstitutionDiagnostic * |
| 2536 |
Sema::createSubstDiagAt(SourceLocation Location, EntityPrinter Printer) { |
| 2537 |
SmallString<128> Entity; |
| 2538 |
llvm::raw_svector_ostream OS(Entity); |
| 2539 |
Printer(OS); |
| 2540 |
const ASTContext &C = Context; |
| 2541 |
return new (C) concepts::Requirement::SubstitutionDiagnostic{ |
| 2542 |
/*SubstitutedEntity=*/C.backupStr(Entity), |
| 2543 |
/*DiagLoc=*/Location, /*DiagMessage=*/StringRef()}; |
| 2544 |
} |
| 2545 |
|
| 2546 |
ExprResult TemplateInstantiator::TransformRequiresTypeParams( |
| 2547 |
SourceLocation KWLoc, SourceLocation RBraceLoc, const RequiresExpr *RE, |
| 2548 |
RequiresExprBodyDecl *Body, ArrayRef<ParmVarDecl *> Params, |
| 2549 |
SmallVectorImpl<QualType> &PTypes, |
| 2550 |
SmallVectorImpl<ParmVarDecl *> &TransParams, |
| 2551 |
Sema::ExtParameterInfoBuilder &PInfos) { |
| 2552 |
|
| 2553 |
TemplateDeductionInfo Info(KWLoc); |
| 2554 |
Sema::InstantiatingTemplate TypeInst(SemaRef, KWLoc, RE, |
| 2555 |
SourceRange{KWLoc, RBraceLoc}); |
| 2556 |
Sema::SFINAETrap Trap(SemaRef, Info); |
| 2557 |
|
| 2558 |
unsigned ErrorIdx; |
| 2559 |
if (getDerived().TransformFunctionTypeParams( |
| 2560 |
KWLoc, Params, /*ParamTypes=*/nullptr, /*ParamInfos=*/nullptr, PTypes, |
| 2561 |
&TransParams, PInfos, &ErrorIdx) || |
| 2562 |
Trap.hasErrorOccurred()) { |
| 2563 |
SmallVector<concepts::Requirement *, 4> TransReqs; |
| 2564 |
ParmVarDecl *FailedDecl = Params[ErrorIdx]; |
| 2565 |
// Add a 'failed' Requirement to contain the error that caused the failure |
| 2566 |
// here. |
| 2567 |
TransReqs.push_back(RebuildTypeRequirement(createSubstDiag( |
| 2568 |
SemaRef, Info, [&](llvm::raw_ostream &OS) { OS << *FailedDecl; }))); |
| 2569 |
return getDerived().RebuildRequiresExpr(KWLoc, Body, RE->getLParenLoc(), |
| 2570 |
TransParams, RE->getRParenLoc(), |
| 2571 |
TransReqs, RBraceLoc); |
| 2572 |
} |
| 2573 |
|
| 2574 |
return ExprResult{}; |
| 2575 |
} |
| 2576 |
|
| 2577 |
concepts::TypeRequirement * |
| 2578 |
TemplateInstantiator::TransformTypeRequirement(concepts::TypeRequirement *Req) { |
| 2579 |
if (!Req->isDependent() && !AlwaysRebuild()) |
| 2580 |
return Req; |
| 2581 |
if (Req->isSubstitutionFailure()) { |
| 2582 |
if (AlwaysRebuild()) |
| 2583 |
return RebuildTypeRequirement( |
| 2584 |
Req->getSubstitutionDiagnostic()); |
| 2585 |
return Req; |
| 2586 |
} |
| 2587 |
|
| 2588 |
TemplateDeductionInfo Info(Req->getType()->getTypeLoc().getBeginLoc()); |
| 2589 |
Sema::SFINAETrap Trap(SemaRef, Info); |
| 2590 |
Sema::InstantiatingTemplate TypeInst( |
| 2591 |
SemaRef, Req->getType()->getTypeLoc().getBeginLoc(), Req, |
| 2592 |
Req->getType()->getTypeLoc().getSourceRange()); |
| 2593 |
if (TypeInst.isInvalid()) |
| 2594 |
return nullptr; |
| 2595 |
TypeSourceInfo *TransType = TransformType(Req->getType()); |
| 2596 |
if (!TransType || Trap.hasErrorOccurred()) |
| 2597 |
return RebuildTypeRequirement(createSubstDiag(SemaRef, Info, |
| 2598 |
[&] (llvm::raw_ostream& OS) { |
| 2599 |
Req->getType()->getType().print(OS, SemaRef.getPrintingPolicy()); |
| 2600 |
})); |
| 2601 |
return RebuildTypeRequirement(TransType); |
| 2602 |
} |
| 2603 |
|
| 2604 |
concepts::ExprRequirement * |
| 2605 |
TemplateInstantiator::TransformExprRequirement(concepts::ExprRequirement *Req) { |
| 2606 |
if (!Req->isDependent() && !AlwaysRebuild()) |
| 2607 |
return Req; |
| 2608 |
|
| 2609 |
llvm::PointerUnion<Expr *, concepts::Requirement::SubstitutionDiagnostic *> |
| 2610 |
TransExpr; |
| 2611 |
if (Req->isExprSubstitutionFailure()) |
| 2612 |
TransExpr = Req->getExprSubstitutionDiagnostic(); |
| 2613 |
else { |
| 2614 |
Expr *E = Req->getExpr(); |
| 2615 |
TemplateDeductionInfo Info(E->getBeginLoc()); |
| 2616 |
Sema::SFINAETrap Trap(SemaRef, Info); |
| 2617 |
Sema::InstantiatingTemplate ExprInst(SemaRef, E->getBeginLoc(), Req, |
| 2618 |
E->getSourceRange()); |
| 2619 |
if (ExprInst.isInvalid()) |
| 2620 |
return nullptr; |
| 2621 |
ExprResult TransExprRes = TransformExpr(E); |
| 2622 |
if (!TransExprRes.isInvalid() && !Trap.hasErrorOccurred() && |
| 2623 |
TransExprRes.get()->hasPlaceholderType()) |
| 2624 |
TransExprRes = SemaRef.CheckPlaceholderExpr(TransExprRes.get()); |
| 2625 |
if (TransExprRes.isInvalid() || Trap.hasErrorOccurred()) |
| 2626 |
TransExpr = createSubstDiag(SemaRef, Info, [&](llvm::raw_ostream &OS) { |
| 2627 |
E->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
| 2628 |
}); |
| 2629 |
else |
| 2630 |
TransExpr = TransExprRes.get(); |
| 2631 |
} |
| 2632 |
|
| 2633 |
std::optional<concepts::ExprRequirement::ReturnTypeRequirement> TransRetReq; |
| 2634 |
const auto &RetReq = Req->getReturnTypeRequirement(); |
| 2635 |
if (RetReq.isEmpty()) |
| 2636 |
TransRetReq.emplace(); |
| 2637 |
else if (RetReq.isSubstitutionFailure()) |
| 2638 |
TransRetReq.emplace(RetReq.getSubstitutionDiagnostic()); |
| 2639 |
else if (RetReq.isTypeConstraint()) { |
| 2640 |
TemplateParameterList *OrigTPL = |
| 2641 |
RetReq.getTypeConstraintTemplateParameterList(); |
| 2642 |
TemplateDeductionInfo Info(OrigTPL->getTemplateLoc()); |
| 2643 |
Sema::SFINAETrap Trap(SemaRef, Info); |
| 2644 |
Sema::InstantiatingTemplate TPLInst(SemaRef, OrigTPL->getTemplateLoc(), Req, |
| 2645 |
OrigTPL->getSourceRange()); |
| 2646 |
if (TPLInst.isInvalid()) |
| 2647 |
return nullptr; |
| 2648 |
TemplateParameterList *TPL = TransformTemplateParameterList(OrigTPL); |
| 2649 |
if (!TPL || Trap.hasErrorOccurred()) |
| 2650 |
TransRetReq.emplace(createSubstDiag(SemaRef, Info, |
| 2651 |
[&] (llvm::raw_ostream& OS) { |
| 2652 |
RetReq.getTypeConstraint()->getImmediatelyDeclaredConstraint() |
| 2653 |
->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
| 2654 |
})); |
| 2655 |
else { |
| 2656 |
TPLInst.Clear(); |
| 2657 |
TransRetReq.emplace(TPL); |
| 2658 |
} |
| 2659 |
} |
| 2660 |
assert(TransRetReq && "All code paths leading here must set TransRetReq"); |
| 2661 |
if (Expr *E = TransExpr.dyn_cast<Expr *>()) |
| 2662 |
return RebuildExprRequirement(E, Req->isSimple(), Req->getNoexceptLoc(), |
| 2663 |
std::move(*TransRetReq)); |
| 2664 |
return RebuildExprRequirement( |
| 2665 |
cast<concepts::Requirement::SubstitutionDiagnostic *>(TransExpr), |
| 2666 |
Req->isSimple(), Req->getNoexceptLoc(), std::move(*TransRetReq)); |
| 2667 |
} |
| 2668 |
|
| 2669 |
concepts::NestedRequirement * |
| 2670 |
TemplateInstantiator::TransformNestedRequirement( |
| 2671 |
concepts::NestedRequirement *Req) { |
| 2672 |
|
| 2673 |
ASTContext &C = SemaRef.Context; |
| 2674 |
|
| 2675 |
Expr *Constraint = Req->getConstraintExpr(); |
| 2676 |
ConstraintSatisfaction Satisfaction; |
| 2677 |
|
| 2678 |
auto NestedReqWithDiag = [&C, this](Expr *E, |
| 2679 |
ConstraintSatisfaction Satisfaction) { |
| 2680 |
Satisfaction.IsSatisfied = false; |
| 2681 |
SmallString<128> Entity; |
| 2682 |
llvm::raw_svector_ostream OS(Entity); |
| 2683 |
E->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
| 2684 |
return new (C) concepts::NestedRequirement( |
| 2685 |
SemaRef.Context, C.backupStr(Entity), std::move(Satisfaction)); |
| 2686 |
}; |
| 2687 |
|
| 2688 |
if (Req->hasInvalidConstraint()) { |
| 2689 |
if (AlwaysRebuild()) |
| 2690 |
return RebuildNestedRequirement(Req->getInvalidConstraintEntity(), |
| 2691 |
Req->getConstraintSatisfaction()); |
| 2692 |
return Req; |
| 2693 |
} |
| 2694 |
|
| 2695 |
if (!getEvaluateConstraints()) { |
| 2696 |
ExprResult TransConstraint = TransformExpr(Req->getConstraintExpr()); |
| 2697 |
if (TransConstraint.isInvalid() || !TransConstraint.get()) |
| 2698 |
return nullptr; |
| 2699 |
if (TransConstraint.get()->isInstantiationDependent()) |
| 2700 |
return new (SemaRef.Context) |
| 2701 |
concepts::NestedRequirement(TransConstraint.get()); |
| 2702 |
ConstraintSatisfaction Satisfaction; |
| 2703 |
return new (SemaRef.Context) concepts::NestedRequirement( |
| 2704 |
SemaRef.Context, TransConstraint.get(), Satisfaction); |
| 2705 |
} |
| 2706 |
|
| 2707 |
bool Success; |
| 2708 |
Expr *NewConstraint; |
| 2709 |
{ |
| 2710 |
EnterExpressionEvaluationContext ContextRAII( |
| 2711 |
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 2712 |
Sema::InstantiatingTemplate ConstrInst( |
| 2713 |
SemaRef, Constraint->getBeginLoc(), Req, |
| 2714 |
Sema::InstantiatingTemplate::ConstraintsCheck(), |
| 2715 |
Constraint->getSourceRange()); |
| 2716 |
|
| 2717 |
if (ConstrInst.isInvalid()) |
| 2718 |
return nullptr; |
| 2719 |
|
| 2720 |
Success = !SemaRef.CheckConstraintSatisfaction( |
| 2721 |
Req, AssociatedConstraint(Constraint), TemplateArgs, |
| 2722 |
Constraint->getSourceRange(), Satisfaction, |
| 2723 |
/*TopLevelConceptId=*/nullptr, &NewConstraint); |
| 2724 |
} |
| 2725 |
|
| 2726 |
if (!Success || Satisfaction.HasSubstitutionFailure()) |
| 2727 |
return NestedReqWithDiag(Constraint, Satisfaction); |
| 2728 |
|
| 2729 |
// FIXME: const correctness |
| 2730 |
// MLTAL might be dependent. |
| 2731 |
if (!NewConstraint) { |
| 2732 |
if (!Satisfaction.IsSatisfied) |
| 2733 |
return NestedReqWithDiag(Constraint, Satisfaction); |
| 2734 |
|
| 2735 |
NewConstraint = Constraint; |
| 2736 |
} |
| 2737 |
return new (C) concepts::NestedRequirement(C, NewConstraint, Satisfaction); |
| 2738 |
} |
| 2739 |
|
| 2740 |
TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
| 2741 |
const MultiLevelTemplateArgumentList &Args, |
| 2742 |
SourceLocation Loc, |
| 2743 |
DeclarationName Entity, |
| 2744 |
bool AllowDeducedTST) { |
| 2745 |
assert(!CodeSynthesisContexts.empty() && |
| 2746 |
"Cannot perform an instantiation without some context on the " |
| 2747 |
"instantiation stack"); |
| 2748 |
|
| 2749 |
if (!T->getType()->isInstantiationDependentType() && |
| 2750 |
!T->getType()->isVariablyModifiedType()) |
| 2751 |
return T; |
| 2752 |
|
| 2753 |
TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 2754 |
return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(T) |
| 2755 |
: Instantiator.TransformType(T); |
| 2756 |
} |
| 2757 |
|
| 2758 |
TypeSourceInfo *Sema::SubstType(TypeLoc TL, |
| 2759 |
const MultiLevelTemplateArgumentList &Args, |
| 2760 |
SourceLocation Loc, |
| 2761 |
DeclarationName Entity) { |
| 2762 |
assert(!CodeSynthesisContexts.empty() && |
| 2763 |
"Cannot perform an instantiation without some context on the " |
| 2764 |
"instantiation stack"); |
| 2765 |
|
| 2766 |
if (TL.getType().isNull()) |
| 2767 |
return nullptr; |
| 2768 |
|
| 2769 |
if (!TL.getType()->isInstantiationDependentType() && |
| 2770 |
!TL.getType()->isVariablyModifiedType()) { |
| 2771 |
// FIXME: Make a copy of the TypeLoc data here, so that we can |
| 2772 |
// return a new TypeSourceInfo. Inefficient! |
| 2773 |
TypeLocBuilder TLB; |
| 2774 |
TLB.pushFullCopy(TL); |
| 2775 |
return TLB.getTypeSourceInfo(Context, TL.getType()); |
| 2776 |
} |
| 2777 |
|
| 2778 |
TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 2779 |
TypeLocBuilder TLB; |
| 2780 |
TLB.reserve(TL.getFullDataSize()); |
| 2781 |
QualType Result = Instantiator.TransformType(TLB, TL); |
| 2782 |
if (Result.isNull()) |
| 2783 |
return nullptr; |
| 2784 |
|
| 2785 |
return TLB.getTypeSourceInfo(Context, Result); |
| 2786 |
} |
| 2787 |
|
| 2788 |
/// Deprecated form of the above. |
| 2789 |
QualType Sema::SubstType(QualType T, |
| 2790 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 2791 |
SourceLocation Loc, DeclarationName Entity, |
| 2792 |
bool *IsIncompleteSubstitution) { |
| 2793 |
assert(!CodeSynthesisContexts.empty() && |
| 2794 |
"Cannot perform an instantiation without some context on the " |
| 2795 |
"instantiation stack"); |
| 2796 |
|
| 2797 |
// If T is not a dependent type or a variably-modified type, there |
| 2798 |
// is nothing to do. |
| 2799 |
if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType()) |
| 2800 |
return T; |
| 2801 |
|
| 2802 |
TemplateInstantiator Instantiator( |
| 2803 |
*this, TemplateArgs, Loc, Entity, |
| 2804 |
/*BailOutOnIncomplete=*/IsIncompleteSubstitution != nullptr); |
| 2805 |
QualType QT = Instantiator.TransformType(T); |
| 2806 |
if (IsIncompleteSubstitution && Instantiator.getIsIncomplete()) |
| 2807 |
*IsIncompleteSubstitution = true; |
| 2808 |
return QT; |
| 2809 |
} |
| 2810 |
|
| 2811 |
static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
| 2812 |
if (T->getType()->isInstantiationDependentType() || |
| 2813 |
T->getType()->isVariablyModifiedType()) |
| 2814 |
return true; |
| 2815 |
|
| 2816 |
TypeLoc TL = T->getTypeLoc().IgnoreParens(); |
| 2817 |
if (!TL.getAs<FunctionProtoTypeLoc>()) |
| 2818 |
return false; |
| 2819 |
|
| 2820 |
FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>(); |
| 2821 |
for (ParmVarDecl *P : FP.getParams()) { |
| 2822 |
// This must be synthesized from a typedef. |
| 2823 |
if (!P) continue; |
| 2824 |
|
| 2825 |
// If there are any parameters, a new TypeSourceInfo that refers to the |
| 2826 |
// instantiated parameters must be built. |
| 2827 |
return true; |
| 2828 |
} |
| 2829 |
|
| 2830 |
return false; |
| 2831 |
} |
| 2832 |
|
| 2833 |
TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
| 2834 |
const MultiLevelTemplateArgumentList &Args, |
| 2835 |
SourceLocation Loc, |
| 2836 |
DeclarationName Entity, |
| 2837 |
CXXRecordDecl *ThisContext, |
| 2838 |
Qualifiers ThisTypeQuals, |
| 2839 |
bool EvaluateConstraints) { |
| 2840 |
assert(!CodeSynthesisContexts.empty() && |
| 2841 |
"Cannot perform an instantiation without some context on the " |
| 2842 |
"instantiation stack"); |
| 2843 |
|
| 2844 |
if (!NeedsInstantiationAsFunctionType(T)) |
| 2845 |
return T; |
| 2846 |
|
| 2847 |
TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
| 2848 |
Instantiator.setEvaluateConstraints(EvaluateConstraints); |
| 2849 |
|
| 2850 |
TypeLocBuilder TLB; |
| 2851 |
|
| 2852 |
TypeLoc TL = T->getTypeLoc(); |
| 2853 |
TLB.reserve(TL.getFullDataSize()); |
| 2854 |
|
| 2855 |
QualType Result; |
| 2856 |
|
| 2857 |
if (FunctionProtoTypeLoc Proto = |
| 2858 |
TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
| 2859 |
// Instantiate the type, other than its exception specification. The |
| 2860 |
// exception specification is instantiated in InitFunctionInstantiation |
| 2861 |
// once we've built the FunctionDecl. |
| 2862 |
// FIXME: Set the exception specification to EST_Uninstantiated here, |
| 2863 |
// instead of rebuilding the function type again later. |
| 2864 |
Result = Instantiator.TransformFunctionProtoType( |
| 2865 |
TLB, Proto, ThisContext, ThisTypeQuals, |
| 2866 |
[](FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2867 |
bool &Changed) { return false; }); |
| 2868 |
} else { |
| 2869 |
Result = Instantiator.TransformType(TLB, TL); |
| 2870 |
} |
| 2871 |
// When there are errors resolving types, clang may use IntTy as a fallback, |
| 2872 |
// breaking our assumption that function declarations have function types. |
| 2873 |
if (Result.isNull() || !Result->isFunctionType()) |
| 2874 |
return nullptr; |
| 2875 |
|
| 2876 |
return TLB.getTypeSourceInfo(Context, Result); |
| 2877 |
} |
| 2878 |
|
| 2879 |
bool Sema::SubstExceptionSpec(SourceLocation Loc, |
| 2880 |
FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2881 |
SmallVectorImpl<QualType> &ExceptionStorage, |
| 2882 |
const MultiLevelTemplateArgumentList &Args) { |
| 2883 |
bool Changed = false; |
| 2884 |
TemplateInstantiator Instantiator(*this, Args, Loc, DeclarationName()); |
| 2885 |
return Instantiator.TransformExceptionSpec(Loc, ESI, ExceptionStorage, |
| 2886 |
Changed); |
| 2887 |
} |
| 2888 |
|
| 2889 |
void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto, |
| 2890 |
const MultiLevelTemplateArgumentList &Args) { |
| 2891 |
FunctionProtoType::ExceptionSpecInfo ESI = |
| 2892 |
Proto->getExtProtoInfo().ExceptionSpec; |
| 2893 |
|
| 2894 |
SmallVector<QualType, 4> ExceptionStorage; |
| 2895 |
if (SubstExceptionSpec(New->getTypeSourceInfo()->getTypeLoc().getEndLoc(), |
| 2896 |
ESI, ExceptionStorage, Args)) |
| 2897 |
// On error, recover by dropping the exception specification. |
| 2898 |
ESI.Type = EST_None; |
| 2899 |
|
| 2900 |
UpdateExceptionSpec(New, ESI); |
| 2901 |
} |
| 2902 |
|
| 2903 |
namespace { |
| 2904 |
|
| 2905 |
struct GetContainedInventedTypeParmVisitor : |
| 2906 |
public TypeVisitor<GetContainedInventedTypeParmVisitor, |
| 2907 |
TemplateTypeParmDecl *> { |
| 2908 |
using TypeVisitor<GetContainedInventedTypeParmVisitor, |
| 2909 |
TemplateTypeParmDecl *>::Visit; |
| 2910 |
|
| 2911 |
TemplateTypeParmDecl *Visit(QualType T) { |
| 2912 |
if (T.isNull()) |
| 2913 |
return nullptr; |
| 2914 |
return Visit(T.getTypePtr()); |
| 2915 |
} |
| 2916 |
// The deduced type itself. |
| 2917 |
TemplateTypeParmDecl *VisitTemplateTypeParmType( |
| 2918 |
const TemplateTypeParmType *T) { |
| 2919 |
if (!T->getDecl() || !T->getDecl()->isImplicit()) |
| 2920 |
return nullptr; |
| 2921 |
return T->getDecl(); |
| 2922 |
} |
| 2923 |
|
| 2924 |
// Only these types can contain 'auto' types, and subsequently be replaced |
| 2925 |
// by references to invented parameters. |
| 2926 |
|
| 2927 |
TemplateTypeParmDecl *VisitPointerType(const PointerType *T) { |
| 2928 |
return Visit(T->getPointeeType()); |
| 2929 |
} |
| 2930 |
|
| 2931 |
TemplateTypeParmDecl *VisitBlockPointerType(const BlockPointerType *T) { |
| 2932 |
return Visit(T->getPointeeType()); |
| 2933 |
} |
| 2934 |
|
| 2935 |
TemplateTypeParmDecl *VisitReferenceType(const ReferenceType *T) { |
| 2936 |
return Visit(T->getPointeeTypeAsWritten()); |
| 2937 |
} |
| 2938 |
|
| 2939 |
TemplateTypeParmDecl *VisitMemberPointerType(const MemberPointerType *T) { |
| 2940 |
return Visit(T->getPointeeType()); |
| 2941 |
} |
| 2942 |
|
| 2943 |
TemplateTypeParmDecl *VisitArrayType(const ArrayType *T) { |
| 2944 |
return Visit(T->getElementType()); |
| 2945 |
} |
| 2946 |
|
| 2947 |
TemplateTypeParmDecl *VisitDependentSizedExtVectorType( |
| 2948 |
const DependentSizedExtVectorType *T) { |
| 2949 |
return Visit(T->getElementType()); |
| 2950 |
} |
| 2951 |
|
| 2952 |
TemplateTypeParmDecl *VisitVectorType(const VectorType *T) { |
| 2953 |
return Visit(T->getElementType()); |
| 2954 |
} |
| 2955 |
|
| 2956 |
TemplateTypeParmDecl *VisitFunctionProtoType(const FunctionProtoType *T) { |
| 2957 |
return VisitFunctionType(T); |
| 2958 |
} |
| 2959 |
|
| 2960 |
TemplateTypeParmDecl *VisitFunctionType(const FunctionType *T) { |
| 2961 |
return Visit(T->getReturnType()); |
| 2962 |
} |
| 2963 |
|
| 2964 |
TemplateTypeParmDecl *VisitParenType(const ParenType *T) { |
| 2965 |
return Visit(T->getInnerType()); |
| 2966 |
} |
| 2967 |
|
| 2968 |
TemplateTypeParmDecl *VisitAttributedType(const AttributedType *T) { |
| 2969 |
return Visit(T->getModifiedType()); |
| 2970 |
} |
| 2971 |
|
| 2972 |
TemplateTypeParmDecl *VisitMacroQualifiedType(const MacroQualifiedType *T) { |
| 2973 |
return Visit(T->getUnderlyingType()); |
| 2974 |
} |
| 2975 |
|
| 2976 |
TemplateTypeParmDecl *VisitAdjustedType(const AdjustedType *T) { |
| 2977 |
return Visit(T->getOriginalType()); |
| 2978 |
} |
| 2979 |
|
| 2980 |
TemplateTypeParmDecl *VisitPackExpansionType(const PackExpansionType *T) { |
| 2981 |
return Visit(T->getPattern()); |
| 2982 |
} |
| 2983 |
}; |
| 2984 |
|
| 2985 |
} // namespace |
| 2986 |
|
| 2987 |
bool Sema::SubstTypeConstraint( |
| 2988 |
TemplateTypeParmDecl *Inst, const TypeConstraint *TC, |
| 2989 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 2990 |
bool EvaluateConstraints) { |
| 2991 |
const ASTTemplateArgumentListInfo *TemplArgInfo = |
| 2992 |
TC->getTemplateArgsAsWritten(); |
| 2993 |
|
| 2994 |
if (!EvaluateConstraints && !inParameterMappingSubstitution()) { |
| 2995 |
UnsignedOrNone Index = TC->getArgPackSubstIndex(); |
| 2996 |
bool ContainsUnexpandedPack = |
| 2997 |
TemplArgInfo && |
| 2998 |
llvm::any_of( |
| 2999 |
TemplArgInfo->arguments(), [](const TemplateArgumentLoc &TA) { |
| 3000 |
return TA.getArgument().containsUnexpandedParameterPack(); |
| 3001 |
}); |
| 3002 |
if (!Index && ContainsUnexpandedPack) |
| 3003 |
Index = SemaRef.ArgPackSubstIndex; |
| 3004 |
Inst->setTypeConstraint(TC->getConceptReference(), |
| 3005 |
TC->getImmediatelyDeclaredConstraint(), Index); |
| 3006 |
return false; |
| 3007 |
} |
| 3008 |
|
| 3009 |
TemplateArgumentListInfo InstArgs; |
| 3010 |
|
| 3011 |
if (TemplArgInfo) { |
| 3012 |
InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc); |
| 3013 |
InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc); |
| 3014 |
if (SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, |
| 3015 |
InstArgs)) |
| 3016 |
return true; |
| 3017 |
} |
| 3018 |
return AttachTypeConstraint( |
| 3019 |
TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), |
| 3020 |
TC->getNamedConcept(), |
| 3021 |
/*FoundDecl=*/TC->getConceptReference()->getFoundDecl(), &InstArgs, Inst, |
| 3022 |
Inst->isParameterPack() |
| 3023 |
? cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint()) |
| 3024 |
->getEllipsisLoc() |
| 3025 |
: SourceLocation()); |
| 3026 |
} |
| 3027 |
|
| 3028 |
ParmVarDecl * |
| 3029 |
Sema::SubstParmVarDecl(ParmVarDecl *OldParm, |
| 3030 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3031 |
int indexAdjustment, UnsignedOrNone NumExpansions, |
| 3032 |
bool ExpectParameterPack, bool EvaluateConstraint) { |
| 3033 |
TypeSourceInfo *OldTSI = OldParm->getTypeSourceInfo(); |
| 3034 |
TypeSourceInfo *NewTSI = nullptr; |
| 3035 |
|
| 3036 |
TypeLoc OldTL = OldTSI->getTypeLoc(); |
| 3037 |
if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) { |
| 3038 |
|
| 3039 |
// We have a function parameter pack. Substitute into the pattern of the |
| 3040 |
// expansion. |
| 3041 |
NewTSI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, |
| 3042 |
OldParm->getLocation(), OldParm->getDeclName()); |
| 3043 |
if (!NewTSI) |
| 3044 |
return nullptr; |
| 3045 |
|
| 3046 |
if (NewTSI->getType()->containsUnexpandedParameterPack()) { |
| 3047 |
// We still have unexpanded parameter packs, which means that |
| 3048 |
// our function parameter is still a function parameter pack. |
| 3049 |
// Therefore, make its type a pack expansion type. |
| 3050 |
NewTSI = CheckPackExpansion(NewTSI, ExpansionTL.getEllipsisLoc(), |
| 3051 |
NumExpansions); |
| 3052 |
} else if (ExpectParameterPack) { |
| 3053 |
// We expected to get a parameter pack but didn't (because the type |
| 3054 |
// itself is not a pack expansion type), so complain. This can occur when |
| 3055 |
// the substitution goes through an alias template that "loses" the |
| 3056 |
// pack expansion. |
| 3057 |
Diag(OldParm->getLocation(), |
| 3058 |
diag::err_function_parameter_pack_without_parameter_packs) |
| 3059 |
<< NewTSI->getType(); |
| 3060 |
return nullptr; |
| 3061 |
} |
| 3062 |
} else { |
| 3063 |
NewTSI = SubstType(OldTSI, TemplateArgs, OldParm->getLocation(), |
| 3064 |
OldParm->getDeclName()); |
| 3065 |
} |
| 3066 |
|
| 3067 |
if (!NewTSI) |
| 3068 |
return nullptr; |
| 3069 |
|
| 3070 |
if (NewTSI->getType()->isVoidType()) { |
| 3071 |
Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
| 3072 |
return nullptr; |
| 3073 |
} |
| 3074 |
|
| 3075 |
// In abbreviated templates, TemplateTypeParmDecls with possible |
| 3076 |
// TypeConstraints are created when the parameter list is originally parsed. |
| 3077 |
// The TypeConstraints can therefore reference other functions parameters in |
| 3078 |
// the abbreviated function template, which is why we must instantiate them |
| 3079 |
// here, when the instantiated versions of those referenced parameters are in |
| 3080 |
// scope. |
| 3081 |
if (TemplateTypeParmDecl *TTP = |
| 3082 |
GetContainedInventedTypeParmVisitor().Visit(OldTSI->getType())) { |
| 3083 |
if (const TypeConstraint *TC = TTP->getTypeConstraint()) { |
| 3084 |
auto *Inst = cast_or_null<TemplateTypeParmDecl>( |
| 3085 |
FindInstantiatedDecl(TTP->getLocation(), TTP, TemplateArgs)); |
| 3086 |
// We will first get here when instantiating the abbreviated function |
| 3087 |
// template's described function, but we might also get here later. |
| 3088 |
// Make sure we do not instantiate the TypeConstraint more than once. |
| 3089 |
if (Inst && !Inst->getTypeConstraint()) { |
| 3090 |
if (SubstTypeConstraint(Inst, TC, TemplateArgs, EvaluateConstraint)) |
| 3091 |
return nullptr; |
| 3092 |
} |
| 3093 |
} |
| 3094 |
} |
| 3095 |
|
| 3096 |
ParmVarDecl *NewParm = CheckParameter( |
| 3097 |
Context.getTranslationUnitDecl(), OldParm->getInnerLocStart(), |
| 3098 |
OldParm->getLocation(), OldParm->getIdentifier(), NewTSI->getType(), |
| 3099 |
NewTSI, OldParm->getStorageClass()); |
| 3100 |
if (!NewParm) |
| 3101 |
return nullptr; |
| 3102 |
|
| 3103 |
// Mark the (new) default argument as uninstantiated (if any). |
| 3104 |
if (OldParm->hasUninstantiatedDefaultArg()) { |
| 3105 |
Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
| 3106 |
NewParm->setUninstantiatedDefaultArg(Arg); |
| 3107 |
} else if (OldParm->hasUnparsedDefaultArg()) { |
| 3108 |
NewParm->setUnparsedDefaultArg(); |
| 3109 |
UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); |
| 3110 |
} else if (Expr *Arg = OldParm->getDefaultArg()) { |
| 3111 |
// Default arguments cannot be substituted until the declaration context |
| 3112 |
// for the associated function or lambda capture class is available. |
| 3113 |
// This is necessary for cases like the following where construction of |
| 3114 |
// the lambda capture class for the outer lambda is dependent on the |
| 3115 |
// parameter types but where the default argument is dependent on the |
| 3116 |
// outer lambda's declaration context. |
| 3117 |
// template <typename T> |
| 3118 |
// auto f() { |
| 3119 |
// return [](T = []{ return T{}; }()) { return 0; }; |
| 3120 |
// } |
| 3121 |
NewParm->setUninstantiatedDefaultArg(Arg); |
| 3122 |
} |
| 3123 |
|
| 3124 |
NewParm->setExplicitObjectParameterLoc( |
| 3125 |
OldParm->getExplicitObjectParamThisLoc()); |
| 3126 |
NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
| 3127 |
|
| 3128 |
if (OldParm->isParameterPack() && !NewParm->isParameterPack()) { |
| 3129 |
// Add the new parameter to the instantiated parameter pack. |
| 3130 |
CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm); |
| 3131 |
} else { |
| 3132 |
// Introduce an Old -> New mapping |
| 3133 |
CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
| 3134 |
} |
| 3135 |
|
| 3136 |
// FIXME: OldParm may come from a FunctionProtoType, in which case CurContext |
| 3137 |
// can be anything, is this right ? |
| 3138 |
NewParm->setDeclContext(CurContext); |
| 3139 |
|
| 3140 |
NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(), |
| 3141 |
OldParm->getFunctionScopeIndex() + indexAdjustment); |
| 3142 |
|
| 3143 |
InstantiateAttrs(TemplateArgs, OldParm, NewParm); |
| 3144 |
|
| 3145 |
return NewParm; |
| 3146 |
} |
| 3147 |
|
| 3148 |
bool Sema::SubstParmTypes( |
| 3149 |
SourceLocation Loc, ArrayRef<ParmVarDecl *> Params, |
| 3150 |
const FunctionProtoType::ExtParameterInfo *ExtParamInfos, |
| 3151 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3152 |
SmallVectorImpl<QualType> &ParamTypes, |
| 3153 |
SmallVectorImpl<ParmVarDecl *> *OutParams, |
| 3154 |
ExtParameterInfoBuilder &ParamInfos) { |
| 3155 |
assert(!CodeSynthesisContexts.empty() && |
| 3156 |
"Cannot perform an instantiation without some context on the " |
| 3157 |
"instantiation stack"); |
| 3158 |
|
| 3159 |
TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
| 3160 |
DeclarationName()); |
| 3161 |
return Instantiator.TransformFunctionTypeParams( |
| 3162 |
Loc, Params, nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos); |
| 3163 |
} |
| 3164 |
|
| 3165 |
bool Sema::SubstDefaultArgument( |
| 3166 |
SourceLocation Loc, |
| 3167 |
ParmVarDecl *Param, |
| 3168 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3169 |
bool ForCallExpr) { |
| 3170 |
FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
| 3171 |
Expr *PatternExpr = Param->getUninstantiatedDefaultArg(); |
| 3172 |
|
| 3173 |
RecursiveInstGuard AlreadyInstantiating( |
| 3174 |
*this, Param, RecursiveInstGuard::Kind::DefaultArgument); |
| 3175 |
if (AlreadyInstantiating) { |
| 3176 |
Param->setInvalidDecl(); |
| 3177 |
return Diag(Param->getBeginLoc(), diag::err_recursive_default_argument) |
| 3178 |
<< FD << PatternExpr->getSourceRange(); |
| 3179 |
} |
| 3180 |
|
| 3181 |
EnterExpressionEvaluationContext EvalContext( |
| 3182 |
*this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); |
| 3183 |
NonSFINAEContext _(*this); |
| 3184 |
InstantiatingTemplate Inst(*this, Loc, Param, TemplateArgs.getInnermost()); |
| 3185 |
if (Inst.isInvalid()) |
| 3186 |
return true; |
| 3187 |
|
| 3188 |
ExprResult Result; |
| 3189 |
// C++ [dcl.fct.default]p5: |
| 3190 |
// The names in the [default argument] expression are bound, and |
| 3191 |
// the semantic constraints are checked, at the point where the |
| 3192 |
// default argument expression appears. |
| 3193 |
ContextRAII SavedContext(*this, FD); |
| 3194 |
{ |
| 3195 |
std::optional<LocalInstantiationScope> LIS; |
| 3196 |
|
| 3197 |
if (ForCallExpr) { |
| 3198 |
// When instantiating a default argument due to use in a call expression, |
| 3199 |
// an instantiation scope that includes the parameters of the callee is |
| 3200 |
// required to satisfy references from the default argument. For example: |
| 3201 |
// template<typename T> void f(T a, int = decltype(a)()); |
| 3202 |
// void g() { f(0); } |
| 3203 |
LIS.emplace(*this); |
| 3204 |
FunctionDecl *PatternFD = FD->getTemplateInstantiationPattern( |
| 3205 |
/*ForDefinition*/ false); |
| 3206 |
if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs)) |
| 3207 |
return true; |
| 3208 |
} |
| 3209 |
|
| 3210 |
runWithSufficientStackSpace(Loc, [&] { |
| 3211 |
Result = SubstInitializer(PatternExpr, TemplateArgs, |
| 3212 |
/*DirectInit*/ false); |
| 3213 |
}); |
| 3214 |
} |
| 3215 |
if (Result.isInvalid()) |
| 3216 |
return true; |
| 3217 |
|
| 3218 |
if (ForCallExpr) { |
| 3219 |
// Check the expression as an initializer for the parameter. |
| 3220 |
InitializedEntity Entity |
| 3221 |
= InitializedEntity::InitializeParameter(Context, Param); |
| 3222 |
InitializationKind Kind = InitializationKind::CreateCopy( |
| 3223 |
Param->getLocation(), |
| 3224 |
/*FIXME:EqualLoc*/ PatternExpr->getBeginLoc()); |
| 3225 |
Expr *ResultE = Result.getAs<Expr>(); |
| 3226 |
|
| 3227 |
InitializationSequence InitSeq(*this, Entity, Kind, ResultE); |
| 3228 |
Result = InitSeq.Perform(*this, Entity, Kind, ResultE); |
| 3229 |
if (Result.isInvalid()) |
| 3230 |
return true; |
| 3231 |
|
| 3232 |
Result = |
| 3233 |
ActOnFinishFullExpr(Result.getAs<Expr>(), Param->getOuterLocStart(), |
| 3234 |
/*DiscardedValue*/ false); |
| 3235 |
} else { |
| 3236 |
// FIXME: Obtain the source location for the '=' token. |
| 3237 |
SourceLocation EqualLoc = PatternExpr->getBeginLoc(); |
| 3238 |
Result = ConvertParamDefaultArgument(Param, Result.getAs<Expr>(), EqualLoc); |
| 3239 |
} |
| 3240 |
if (Result.isInvalid()) |
| 3241 |
return true; |
| 3242 |
|
| 3243 |
// Remember the instantiated default argument. |
| 3244 |
Param->setDefaultArg(Result.getAs<Expr>()); |
| 3245 |
|
| 3246 |
return false; |
| 3247 |
} |
| 3248 |
|
| 3249 |
// See TreeTransform::PreparePackForExpansion for the relevant comment. |
| 3250 |
// This function implements the same concept for base specifiers. |
| 3251 |
static bool |
| 3252 |
PreparePackForExpansion(Sema &S, const CXXBaseSpecifier &Base, |
| 3253 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3254 |
TypeSourceInfo *&Out, UnexpandedInfo &Info) { |
| 3255 |
SourceRange BaseSourceRange = Base.getSourceRange(); |
| 3256 |
SourceLocation BaseEllipsisLoc = Base.getEllipsisLoc(); |
| 3257 |
Info.Ellipsis = Base.getEllipsisLoc(); |
| 3258 |
auto ComputeInfo = [&S, &TemplateArgs, BaseSourceRange, BaseEllipsisLoc]( |
| 3259 |
TypeSourceInfo *BaseTypeInfo, |
| 3260 |
bool IsLateExpansionAttempt, UnexpandedInfo &Info) { |
| 3261 |
// This is a pack expansion. See whether we should expand it now, or |
| 3262 |
// wait until later. |
| 3263 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 3264 |
S.collectUnexpandedParameterPacks(BaseTypeInfo->getTypeLoc(), Unexpanded); |
| 3265 |
if (IsLateExpansionAttempt) { |
| 3266 |
// Request expansion only when there is an opportunity to expand a pack |
| 3267 |
// that required a substituion first. |
| 3268 |
bool SawPackTypes = |
| 3269 |
llvm::any_of(Unexpanded, [](UnexpandedParameterPack P) { |
| 3270 |
return P.first.dyn_cast<const SubstBuiltinTemplatePackType *>(); |
| 3271 |
}); |
| 3272 |
if (!SawPackTypes) { |
| 3273 |
Info.Expand = false; |
| 3274 |
return false; |
| 3275 |
} |
| 3276 |
} |
| 3277 |
|
| 3278 |
// Determine whether the set of unexpanded parameter packs can and should be |
| 3279 |
// expanded. |
| 3280 |
Info.Expand = false; |
| 3281 |
Info.RetainExpansion = false; |
| 3282 |
Info.NumExpansions = std::nullopt; |
| 3283 |
return S.CheckParameterPacksForExpansion( |
| 3284 |
BaseEllipsisLoc, BaseSourceRange, Unexpanded, TemplateArgs, |
| 3285 |
/*FailOnPackProducingTemplates=*/false, Info.Expand, |
| 3286 |
Info.RetainExpansion, Info.NumExpansions); |
| 3287 |
}; |
| 3288 |
|
| 3289 |
if (ComputeInfo(Base.getTypeSourceInfo(), false, Info)) |
| 3290 |
return true; |
| 3291 |
|
| 3292 |
if (Info.Expand) { |
| 3293 |
Out = Base.getTypeSourceInfo(); |
| 3294 |
return false; |
| 3295 |
} |
| 3296 |
|
| 3297 |
// The resulting base specifier will (still) be a pack expansion. |
| 3298 |
{ |
| 3299 |
Sema::ArgPackSubstIndexRAII SubstIndex(S, std::nullopt); |
| 3300 |
Out = S.SubstType(Base.getTypeSourceInfo(), TemplateArgs, |
| 3301 |
BaseSourceRange.getBegin(), DeclarationName()); |
| 3302 |
} |
| 3303 |
if (!Out->getType()->containsUnexpandedParameterPack()) |
| 3304 |
return false; |
| 3305 |
|
| 3306 |
// Some packs will learn their length after substitution. |
| 3307 |
// We may need to request their expansion. |
| 3308 |
if (ComputeInfo(Out, /*IsLateExpansionAttempt=*/true, Info)) |
| 3309 |
return true; |
| 3310 |
if (Info.Expand) |
| 3311 |
Info.ExpandUnderForgetSubstitions = true; |
| 3312 |
return false; |
| 3313 |
} |
| 3314 |
|
| 3315 |
bool |
| 3316 |
Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 3317 |
CXXRecordDecl *Pattern, |
| 3318 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3319 |
bool Invalid = false; |
| 3320 |
SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
| 3321 |
for (const auto &Base : Pattern->bases()) { |
| 3322 |
if (!Base.getType()->isDependentType()) { |
| 3323 |
if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) { |
| 3324 |
if (RD->isInvalidDecl()) |
| 3325 |
Instantiation->setInvalidDecl(); |
| 3326 |
} |
| 3327 |
InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base)); |
| 3328 |
continue; |
| 3329 |
} |
| 3330 |
|
| 3331 |
SourceLocation EllipsisLoc; |
| 3332 |
TypeSourceInfo *BaseTypeLoc = nullptr; |
| 3333 |
if (Base.isPackExpansion()) { |
| 3334 |
UnexpandedInfo Info; |
| 3335 |
if (PreparePackForExpansion(*this, Base, TemplateArgs, BaseTypeLoc, |
| 3336 |
Info)) { |
| 3337 |
Invalid = true; |
| 3338 |
continue; |
| 3339 |
} |
| 3340 |
|
| 3341 |
// If we should expand this pack expansion now, do so. |
| 3342 |
MultiLevelTemplateArgumentList EmptyList; |
| 3343 |
const MultiLevelTemplateArgumentList *ArgsForSubst = &TemplateArgs; |
| 3344 |
if (Info.ExpandUnderForgetSubstitions) |
| 3345 |
ArgsForSubst = &EmptyList; |
| 3346 |
|
| 3347 |
if (Info.Expand) { |
| 3348 |
for (unsigned I = 0; I != *Info.NumExpansions; ++I) { |
| 3349 |
Sema::ArgPackSubstIndexRAII SubstIndex(*this, I); |
| 3350 |
|
| 3351 |
TypeSourceInfo *Expanded = |
| 3352 |
SubstType(BaseTypeLoc, *ArgsForSubst, |
| 3353 |
Base.getSourceRange().getBegin(), DeclarationName()); |
| 3354 |
if (!Expanded) { |
| 3355 |
Invalid = true; |
| 3356 |
continue; |
| 3357 |
} |
| 3358 |
|
| 3359 |
if (CXXBaseSpecifier *InstantiatedBase = CheckBaseSpecifier( |
| 3360 |
Instantiation, Base.getSourceRange(), Base.isVirtual(), |
| 3361 |
Base.getAccessSpecifierAsWritten(), Expanded, |
| 3362 |
SourceLocation())) |
| 3363 |
InstantiatedBases.push_back(InstantiatedBase); |
| 3364 |
else |
| 3365 |
Invalid = true; |
| 3366 |
} |
| 3367 |
|
| 3368 |
continue; |
| 3369 |
} |
| 3370 |
|
| 3371 |
// The resulting base specifier will (still) be a pack expansion. |
| 3372 |
EllipsisLoc = Base.getEllipsisLoc(); |
| 3373 |
Sema::ArgPackSubstIndexRAII SubstIndex(*this, std::nullopt); |
| 3374 |
BaseTypeLoc = |
| 3375 |
SubstType(BaseTypeLoc, *ArgsForSubst, |
| 3376 |
Base.getSourceRange().getBegin(), DeclarationName()); |
| 3377 |
} else { |
| 3378 |
BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
| 3379 |
TemplateArgs, |
| 3380 |
Base.getSourceRange().getBegin(), |
| 3381 |
DeclarationName()); |
| 3382 |
} |
| 3383 |
|
| 3384 |
if (!BaseTypeLoc) { |
| 3385 |
Invalid = true; |
| 3386 |
continue; |
| 3387 |
} |
| 3388 |
|
| 3389 |
if (CXXBaseSpecifier *InstantiatedBase |
| 3390 |
= CheckBaseSpecifier(Instantiation, |
| 3391 |
Base.getSourceRange(), |
| 3392 |
Base.isVirtual(), |
| 3393 |
Base.getAccessSpecifierAsWritten(), |
| 3394 |
BaseTypeLoc, |
| 3395 |
EllipsisLoc)) |
| 3396 |
InstantiatedBases.push_back(InstantiatedBase); |
| 3397 |
else |
| 3398 |
Invalid = true; |
| 3399 |
} |
| 3400 |
|
| 3401 |
if (!Invalid && AttachBaseSpecifiers(Instantiation, InstantiatedBases)) |
| 3402 |
Invalid = true; |
| 3403 |
|
| 3404 |
return Invalid; |
| 3405 |
} |
| 3406 |
|
| 3407 |
// Defined via #include from SemaTemplateInstantiateDecl.cpp |
| 3408 |
namespace clang { |
| 3409 |
namespace sema { |
| 3410 |
Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S, |
| 3411 |
const MultiLevelTemplateArgumentList &TemplateArgs); |
| 3412 |
Attr *instantiateTemplateAttributeForDecl( |
| 3413 |
const Attr *At, ASTContext &C, Sema &S, |
| 3414 |
const MultiLevelTemplateArgumentList &TemplateArgs); |
| 3415 |
} |
| 3416 |
} |
| 3417 |
|
| 3418 |
bool Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 3419 |
CXXRecordDecl *Instantiation, |
| 3420 |
CXXRecordDecl *Pattern, |
| 3421 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3422 |
TemplateSpecializationKind TSK, bool Complain) { |
| 3423 |
#ifndef NDEBUG |
| 3424 |
RecursiveInstGuard AlreadyInstantiating(*this, Instantiation, |
| 3425 |
RecursiveInstGuard::Kind::Template); |
| 3426 |
assert(!AlreadyInstantiating && "should have been caught by caller"); |
| 3427 |
#endif |
| 3428 |
|
| 3429 |
return InstantiateClassImpl(PointOfInstantiation, Instantiation, Pattern, |
| 3430 |
TemplateArgs, TSK, Complain); |
| 3431 |
} |
| 3432 |
|
| 3433 |
bool Sema::InstantiateClassImpl( |
| 3434 |
SourceLocation PointOfInstantiation, CXXRecordDecl *Instantiation, |
| 3435 |
CXXRecordDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3436 |
TemplateSpecializationKind TSK, bool Complain) { |
| 3437 |
|
| 3438 |
CXXRecordDecl *PatternDef |
| 3439 |
= cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
| 3440 |
if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
| 3441 |
Instantiation->getInstantiatedFromMemberClass(), |
| 3442 |
Pattern, PatternDef, TSK, Complain)) |
| 3443 |
return true; |
| 3444 |
|
| 3445 |
llvm::TimeTraceScope TimeScope("InstantiateClass", [&]() { |
| 3446 |
llvm::TimeTraceMetadata M; |
| 3447 |
llvm::raw_string_ostream OS(M.Detail); |
| 3448 |
Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), |
| 3449 |
/*Qualified=*/true); |
| 3450 |
if (llvm::isTimeTraceVerbose()) { |
| 3451 |
auto Loc = SourceMgr.getExpansionLoc(Instantiation->getLocation()); |
| 3452 |
M.File = SourceMgr.getFilename(Loc); |
| 3453 |
M.Line = SourceMgr.getExpansionLineNumber(Loc); |
| 3454 |
} |
| 3455 |
return M; |
| 3456 |
}); |
| 3457 |
|
| 3458 |
Pattern = PatternDef; |
| 3459 |
|
| 3460 |
// Record the point of instantiation. |
| 3461 |
if (MemberSpecializationInfo *MSInfo |
| 3462 |
= Instantiation->getMemberSpecializationInfo()) { |
| 3463 |
MSInfo->setTemplateSpecializationKind(TSK); |
| 3464 |
MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3465 |
} else if (ClassTemplateSpecializationDecl *Spec |
| 3466 |
= dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 3467 |
Spec->setTemplateSpecializationKind(TSK); |
| 3468 |
Spec->setPointOfInstantiation(PointOfInstantiation); |
| 3469 |
} |
| 3470 |
|
| 3471 |
NonSFINAEContext _(*this); |
| 3472 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| 3473 |
if (Inst.isInvalid()) |
| 3474 |
return true; |
| 3475 |
PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
| 3476 |
"instantiating class definition"); |
| 3477 |
|
| 3478 |
// Enter the scope of this instantiation. We don't use |
| 3479 |
// PushDeclContext because we don't have a scope. |
| 3480 |
ContextRAII SavedContext(*this, Instantiation); |
| 3481 |
EnterExpressionEvaluationContext EvalContext( |
| 3482 |
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 3483 |
|
| 3484 |
// If this is an instantiation of a local class, merge this local |
| 3485 |
// instantiation scope with the enclosing scope. Otherwise, every |
| 3486 |
// instantiation of a class has its own local instantiation scope. |
| 3487 |
bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
| 3488 |
LocalInstantiationScope Scope(*this, MergeWithParentScope); |
| 3489 |
|
| 3490 |
// Some class state isn't processed immediately but delayed till class |
| 3491 |
// instantiation completes. We may not be ready to handle any delayed state |
| 3492 |
// already on the stack as it might correspond to a different class, so save |
| 3493 |
// it now and put it back later. |
| 3494 |
SavePendingParsedClassStateRAII SavedPendingParsedClassState(*this); |
| 3495 |
|
| 3496 |
// Pull attributes from the pattern onto the instantiation. |
| 3497 |
InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 3498 |
|
| 3499 |
// Start the definition of this instantiation. |
| 3500 |
Instantiation->startDefinition(); |
| 3501 |
|
| 3502 |
// The instantiation is visible here, even if it was first declared in an |
| 3503 |
// unimported module. |
| 3504 |
Instantiation->setVisibleDespiteOwningModule(); |
| 3505 |
|
| 3506 |
// FIXME: This loses the as-written tag kind for an explicit instantiation. |
| 3507 |
Instantiation->setTagKind(Pattern->getTagKind()); |
| 3508 |
|
| 3509 |
// Do substitution on the base class specifiers. |
| 3510 |
if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
| 3511 |
Instantiation->setInvalidDecl(); |
| 3512 |
|
| 3513 |
TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
| 3514 |
Instantiator.setEvaluateConstraints(false); |
| 3515 |
SmallVector<Decl*, 4> Fields; |
| 3516 |
// Delay instantiation of late parsed attributes. |
| 3517 |
LateInstantiatedAttrVec LateAttrs; |
| 3518 |
Instantiator.enableLateAttributeInstantiation(&LateAttrs); |
| 3519 |
|
| 3520 |
bool MightHaveConstexprVirtualFunctions = false; |
| 3521 |
for (auto *Member : Pattern->decls()) { |
| 3522 |
// Don't instantiate members not belonging in this semantic context. |
| 3523 |
// e.g. for: |
| 3524 |
// @code |
| 3525 |
// template <int i> class A { |
| 3526 |
// class B *g; |
| 3527 |
// }; |
| 3528 |
// @endcode |
| 3529 |
// 'class B' has the template as lexical context but semantically it is |
| 3530 |
// introduced in namespace scope. |
| 3531 |
if (Member->getDeclContext() != Pattern) |
| 3532 |
continue; |
| 3533 |
|
| 3534 |
// BlockDecls can appear in a default-member-initializer. They must be the |
| 3535 |
// child of a BlockExpr, so we only know how to instantiate them from there. |
| 3536 |
// Similarly, lambda closure types are recreated when instantiating the |
| 3537 |
// corresponding LambdaExpr. |
| 3538 |
if (isa<BlockDecl>(Member) || |
| 3539 |
(isa<CXXRecordDecl>(Member) && cast<CXXRecordDecl>(Member)->isLambda())) |
| 3540 |
continue; |
| 3541 |
|
| 3542 |
if (Member->isInvalidDecl()) { |
| 3543 |
Instantiation->setInvalidDecl(); |
| 3544 |
continue; |
| 3545 |
} |
| 3546 |
|
| 3547 |
Decl *NewMember = Instantiator.Visit(Member); |
| 3548 |
if (NewMember) { |
| 3549 |
if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) { |
| 3550 |
Fields.push_back(Field); |
| 3551 |
} else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) { |
| 3552 |
// C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 3553 |
// specialization causes the implicit instantiation of the definitions |
| 3554 |
// of unscoped member enumerations. |
| 3555 |
// Record a point of instantiation for this implicit instantiation. |
| 3556 |
if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() && |
| 3557 |
Enum->isCompleteDefinition()) { |
| 3558 |
MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo(); |
| 3559 |
assert(MSInfo && "no spec info for member enum specialization"); |
| 3560 |
MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation); |
| 3561 |
MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3562 |
} |
| 3563 |
} else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) { |
| 3564 |
if (SA->isFailed()) { |
| 3565 |
// A static_assert failed. Bail out; instantiating this |
| 3566 |
// class is probably not meaningful. |
| 3567 |
Instantiation->setInvalidDecl(); |
| 3568 |
break; |
| 3569 |
} |
| 3570 |
} else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewMember)) { |
| 3571 |
if (MD->isConstexpr() && !MD->getFriendObjectKind() && |
| 3572 |
(MD->isVirtualAsWritten() || Instantiation->getNumBases())) |
| 3573 |
MightHaveConstexprVirtualFunctions = true; |
| 3574 |
} |
| 3575 |
|
| 3576 |
if (NewMember->isInvalidDecl()) |
| 3577 |
Instantiation->setInvalidDecl(); |
| 3578 |
} else { |
| 3579 |
// FIXME: Eventually, a NULL return will mean that one of the |
| 3580 |
// instantiations was a semantic disaster, and we'll want to mark the |
| 3581 |
// declaration invalid. |
| 3582 |
// For now, we expect to skip some members that we can't yet handle. |
| 3583 |
} |
| 3584 |
} |
| 3585 |
|
| 3586 |
// Finish checking fields. |
| 3587 |
ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields, |
| 3588 |
SourceLocation(), SourceLocation(), ParsedAttributesView()); |
| 3589 |
CheckCompletedCXXClass(nullptr, Instantiation); |
| 3590 |
|
| 3591 |
// Default arguments are parsed, if not instantiated. We can go instantiate |
| 3592 |
// default arg exprs for default constructors if necessary now. Unless we're |
| 3593 |
// parsing a class, in which case wait until that's finished. |
| 3594 |
if (ParsingClassDepth == 0) |
| 3595 |
ActOnFinishCXXNonNestedClass(); |
| 3596 |
|
| 3597 |
// Instantiate late parsed attributes, and attach them to their decls. |
| 3598 |
// See Sema::InstantiateAttrs |
| 3599 |
for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(), |
| 3600 |
E = LateAttrs.end(); I != E; ++I) { |
| 3601 |
assert(CurrentInstantiationScope == Instantiator.getStartingScope()); |
| 3602 |
CurrentInstantiationScope = I->Scope; |
| 3603 |
|
| 3604 |
// Allow 'this' within late-parsed attributes. |
| 3605 |
auto *ND = cast<NamedDecl>(I->NewDecl); |
| 3606 |
auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
| 3607 |
CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
| 3608 |
ND->isCXXInstanceMember()); |
| 3609 |
|
| 3610 |
Attr *NewAttr = |
| 3611 |
instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs); |
| 3612 |
if (NewAttr) |
| 3613 |
I->NewDecl->addAttr(NewAttr); |
| 3614 |
LocalInstantiationScope::deleteScopes(I->Scope, |
| 3615 |
Instantiator.getStartingScope()); |
| 3616 |
} |
| 3617 |
Instantiator.disableLateAttributeInstantiation(); |
| 3618 |
LateAttrs.clear(); |
| 3619 |
|
| 3620 |
ActOnFinishDelayedMemberInitializers(Instantiation); |
| 3621 |
|
| 3622 |
// FIXME: We should do something similar for explicit instantiations so they |
| 3623 |
// end up in the right module. |
| 3624 |
if (TSK == TSK_ImplicitInstantiation) { |
| 3625 |
Instantiation->setLocation(Pattern->getLocation()); |
| 3626 |
Instantiation->setLocStart(Pattern->getInnerLocStart()); |
| 3627 |
Instantiation->setBraceRange(Pattern->getBraceRange()); |
| 3628 |
} |
| 3629 |
|
| 3630 |
if (!Instantiation->isInvalidDecl()) { |
| 3631 |
// Perform any dependent diagnostics from the pattern. |
| 3632 |
if (Pattern->isDependentContext()) |
| 3633 |
PerformDependentDiagnostics(Pattern, TemplateArgs); |
| 3634 |
|
| 3635 |
// Instantiate any out-of-line class template partial |
| 3636 |
// specializations now. |
| 3637 |
for (TemplateDeclInstantiator::delayed_partial_spec_iterator |
| 3638 |
P = Instantiator.delayed_partial_spec_begin(), |
| 3639 |
PEnd = Instantiator.delayed_partial_spec_end(); |
| 3640 |
P != PEnd; ++P) { |
| 3641 |
if (!Instantiator.InstantiateClassTemplatePartialSpecialization( |
| 3642 |
P->first, P->second)) { |
| 3643 |
Instantiation->setInvalidDecl(); |
| 3644 |
break; |
| 3645 |
} |
| 3646 |
} |
| 3647 |
|
| 3648 |
// Instantiate any out-of-line variable template partial |
| 3649 |
// specializations now. |
| 3650 |
for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator |
| 3651 |
P = Instantiator.delayed_var_partial_spec_begin(), |
| 3652 |
PEnd = Instantiator.delayed_var_partial_spec_end(); |
| 3653 |
P != PEnd; ++P) { |
| 3654 |
if (!Instantiator.InstantiateVarTemplatePartialSpecialization( |
| 3655 |
P->first, P->second)) { |
| 3656 |
Instantiation->setInvalidDecl(); |
| 3657 |
break; |
| 3658 |
} |
| 3659 |
} |
| 3660 |
} |
| 3661 |
|
| 3662 |
// Exit the scope of this instantiation. |
| 3663 |
SavedContext.pop(); |
| 3664 |
|
| 3665 |
if (!Instantiation->isInvalidDecl()) { |
| 3666 |
// Always emit the vtable for an explicit instantiation definition |
| 3667 |
// of a polymorphic class template specialization. Otherwise, eagerly |
| 3668 |
// instantiate only constexpr virtual functions in preparation for their use |
| 3669 |
// in constant evaluation. |
| 3670 |
if (TSK == TSK_ExplicitInstantiationDefinition) |
| 3671 |
MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
| 3672 |
else if (MightHaveConstexprVirtualFunctions) |
| 3673 |
MarkVirtualMembersReferenced(PointOfInstantiation, Instantiation, |
| 3674 |
/*ConstexprOnly*/ true); |
| 3675 |
} |
| 3676 |
|
| 3677 |
Consumer.HandleTagDeclDefinition(Instantiation); |
| 3678 |
|
| 3679 |
return Instantiation->isInvalidDecl(); |
| 3680 |
} |
| 3681 |
|
| 3682 |
bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation, |
| 3683 |
EnumDecl *Instantiation, EnumDecl *Pattern, |
| 3684 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 3685 |
TemplateSpecializationKind TSK) { |
| 3686 |
#ifndef NDEBUG |
| 3687 |
RecursiveInstGuard AlreadyInstantiating(*this, Instantiation, |
| 3688 |
RecursiveInstGuard::Kind::Template); |
| 3689 |
assert(!AlreadyInstantiating && "should have been caught by caller"); |
| 3690 |
#endif |
| 3691 |
|
| 3692 |
EnumDecl *PatternDef = Pattern->getDefinition(); |
| 3693 |
if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
| 3694 |
Instantiation->getInstantiatedFromMemberEnum(), |
| 3695 |
Pattern, PatternDef, TSK,/*Complain*/true)) |
| 3696 |
return true; |
| 3697 |
Pattern = PatternDef; |
| 3698 |
|
| 3699 |
// Record the point of instantiation. |
| 3700 |
if (MemberSpecializationInfo *MSInfo |
| 3701 |
= Instantiation->getMemberSpecializationInfo()) { |
| 3702 |
MSInfo->setTemplateSpecializationKind(TSK); |
| 3703 |
MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3704 |
} |
| 3705 |
|
| 3706 |
NonSFINAEContext _(*this); |
| 3707 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| 3708 |
if (Inst.isInvalid()) |
| 3709 |
return true; |
| 3710 |
PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
| 3711 |
"instantiating enum definition"); |
| 3712 |
|
| 3713 |
// The instantiation is visible here, even if it was first declared in an |
| 3714 |
// unimported module. |
| 3715 |
Instantiation->setVisibleDespiteOwningModule(); |
| 3716 |
|
| 3717 |
// Enter the scope of this instantiation. We don't use |
| 3718 |
// PushDeclContext because we don't have a scope. |
| 3719 |
ContextRAII SavedContext(*this, Instantiation); |
| 3720 |
EnterExpressionEvaluationContext EvalContext( |
| 3721 |
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 3722 |
|
| 3723 |
LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true); |
| 3724 |
|
| 3725 |
// Pull attributes from the pattern onto the instantiation. |
| 3726 |
InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
| 3727 |
|
| 3728 |
TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
| 3729 |
Instantiator.InstantiateEnumDefinition(Instantiation, Pattern); |
| 3730 |
|
| 3731 |
// Exit the scope of this instantiation. |
| 3732 |
SavedContext.pop(); |
| 3733 |
|
| 3734 |
return Instantiation->isInvalidDecl(); |
| 3735 |
} |
| 3736 |
|
| 3737 |
bool Sema::InstantiateInClassInitializer( |
| 3738 |
SourceLocation PointOfInstantiation, FieldDecl *Instantiation, |
| 3739 |
FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3740 |
// If there is no initializer, we don't need to do anything. |
| 3741 |
if (!Pattern->hasInClassInitializer()) |
| 3742 |
return false; |
| 3743 |
|
| 3744 |
assert(Instantiation->getInClassInitStyle() == |
| 3745 |
Pattern->getInClassInitStyle() && |
| 3746 |
"pattern and instantiation disagree about init style"); |
| 3747 |
|
| 3748 |
RecursiveInstGuard AlreadyInstantiating(*this, Instantiation, |
| 3749 |
RecursiveInstGuard::Kind::Template); |
| 3750 |
if (AlreadyInstantiating) |
| 3751 |
// Error out if we hit an instantiation cycle for this initializer. |
| 3752 |
return Diag(PointOfInstantiation, |
| 3753 |
diag::err_default_member_initializer_cycle) |
| 3754 |
<< Instantiation; |
| 3755 |
|
| 3756 |
// Error out if we haven't parsed the initializer of the pattern yet because |
| 3757 |
// we are waiting for the closing brace of the outer class. |
| 3758 |
Expr *OldInit = Pattern->getInClassInitializer(); |
| 3759 |
if (!OldInit) { |
| 3760 |
RecordDecl *PatternRD = Pattern->getParent(); |
| 3761 |
RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext(); |
| 3762 |
Diag(PointOfInstantiation, |
| 3763 |
diag::err_default_member_initializer_not_yet_parsed) |
| 3764 |
<< OutermostClass << Pattern; |
| 3765 |
Diag(Pattern->getEndLoc(), |
| 3766 |
diag::note_default_member_initializer_not_yet_parsed); |
| 3767 |
Instantiation->setInvalidDecl(); |
| 3768 |
return true; |
| 3769 |
} |
| 3770 |
|
| 3771 |
NonSFINAEContext _(*this); |
| 3772 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| 3773 |
if (Inst.isInvalid()) |
| 3774 |
return true; |
| 3775 |
PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
| 3776 |
"instantiating default member init"); |
| 3777 |
|
| 3778 |
// Enter the scope of this instantiation. We don't use PushDeclContext because |
| 3779 |
// we don't have a scope. |
| 3780 |
ContextRAII SavedContext(*this, Instantiation->getParent()); |
| 3781 |
EnterExpressionEvaluationContext EvalContext( |
| 3782 |
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 3783 |
ExprEvalContexts.back().DelayedDefaultInitializationContext = { |
| 3784 |
PointOfInstantiation, Instantiation, CurContext}; |
| 3785 |
|
| 3786 |
LocalInstantiationScope Scope(*this, true); |
| 3787 |
|
| 3788 |
// Instantiate the initializer. |
| 3789 |
ActOnStartCXXInClassMemberInitializer(); |
| 3790 |
CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), Qualifiers()); |
| 3791 |
|
| 3792 |
ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs, |
| 3793 |
/*CXXDirectInit=*/false); |
| 3794 |
Expr *Init = NewInit.get(); |
| 3795 |
assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class"); |
| 3796 |
ActOnFinishCXXInClassMemberInitializer( |
| 3797 |
Instantiation, Init ? Init->getBeginLoc() : SourceLocation(), Init); |
| 3798 |
|
| 3799 |
if (auto *L = getASTMutationListener()) |
| 3800 |
L->DefaultMemberInitializerInstantiated(Instantiation); |
| 3801 |
|
| 3802 |
// Return true if the in-class initializer is still missing. |
| 3803 |
return !Instantiation->getInClassInitializer(); |
| 3804 |
} |
| 3805 |
|
| 3806 |
namespace { |
| 3807 |
/// A partial specialization whose template arguments have matched |
| 3808 |
/// a given template-id. |
| 3809 |
struct PartialSpecMatchResult { |
| 3810 |
ClassTemplatePartialSpecializationDecl *Partial; |
| 3811 |
TemplateArgumentList *Args; |
| 3812 |
}; |
| 3813 |
} |
| 3814 |
|
| 3815 |
bool Sema::usesPartialOrExplicitSpecialization( |
| 3816 |
SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
| 3817 |
if (ClassTemplateSpec->getTemplateSpecializationKind() == |
| 3818 |
TSK_ExplicitSpecialization) |
| 3819 |
return true; |
| 3820 |
|
| 3821 |
SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3822 |
ClassTemplateDecl *CTD = ClassTemplateSpec->getSpecializedTemplate(); |
| 3823 |
CTD->getPartialSpecializations(PartialSpecs); |
| 3824 |
for (ClassTemplatePartialSpecializationDecl *CTPSD : PartialSpecs) { |
| 3825 |
// C++ [temp.spec.partial.member]p2: |
| 3826 |
// If the primary member template is explicitly specialized for a given |
| 3827 |
// (implicit) specialization of the enclosing class template, the partial |
| 3828 |
// specializations of the member template are ignored for this |
| 3829 |
// specialization of the enclosing class template. If a partial |
| 3830 |
// specialization of the member template is explicitly specialized for a |
| 3831 |
// given (implicit) specialization of the enclosing class template, the |
| 3832 |
// primary member template and its other partial specializations are still |
| 3833 |
// considered for this specialization of the enclosing class template. |
| 3834 |
if (CTD->getMostRecentDecl()->isMemberSpecialization() && |
| 3835 |
!CTPSD->getMostRecentDecl()->isMemberSpecialization()) |
| 3836 |
continue; |
| 3837 |
|
| 3838 |
TemplateDeductionInfo Info(Loc); |
| 3839 |
if (DeduceTemplateArguments(CTPSD, |
| 3840 |
ClassTemplateSpec->getTemplateArgs().asArray(), |
| 3841 |
Info) == TemplateDeductionResult::Success) |
| 3842 |
return true; |
| 3843 |
} |
| 3844 |
|
| 3845 |
return false; |
| 3846 |
} |
| 3847 |
|
| 3848 |
/// Get the instantiation pattern to use to instantiate the definition of a |
| 3849 |
/// given ClassTemplateSpecializationDecl (either the pattern of the primary |
| 3850 |
/// template or of a partial specialization). |
| 3851 |
static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization( |
| 3852 |
Sema &S, SourceLocation PointOfInstantiation, |
| 3853 |
ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 3854 |
TemplateSpecializationKind TSK, bool PrimaryStrictPackMatch) { |
| 3855 |
std::optional<Sema::NonSFINAEContext> NSC(S); |
| 3856 |
Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec); |
| 3857 |
if (Inst.isInvalid()) |
| 3858 |
return {/*Invalid=*/true}; |
| 3859 |
|
| 3860 |
llvm::PointerUnion<ClassTemplateDecl *, |
| 3861 |
ClassTemplatePartialSpecializationDecl *> |
| 3862 |
Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
| 3863 |
if (!isa<ClassTemplatePartialSpecializationDecl *>(Specialized)) { |
| 3864 |
// Find best matching specialization. |
| 3865 |
ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| 3866 |
|
| 3867 |
// C++ [temp.class.spec.match]p1: |
| 3868 |
// When a class template is used in a context that requires an |
| 3869 |
// instantiation of the class, it is necessary to determine |
| 3870 |
// whether the instantiation is to be generated using the primary |
| 3871 |
// template or one of the partial specializations. This is done by |
| 3872 |
// matching the template arguments of the class template |
| 3873 |
// specialization with the template argument lists of the partial |
| 3874 |
// specializations. |
| 3875 |
typedef PartialSpecMatchResult MatchResult; |
| 3876 |
SmallVector<MatchResult, 4> Matched, ExtraMatched; |
| 3877 |
SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3878 |
Template->getPartialSpecializations(PartialSpecs); |
| 3879 |
TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 3880 |
for (ClassTemplatePartialSpecializationDecl *Partial : PartialSpecs) { |
| 3881 |
// C++ [temp.spec.partial.member]p2: |
| 3882 |
// If the primary member template is explicitly specialized for a given |
| 3883 |
// (implicit) specialization of the enclosing class template, the |
| 3884 |
// partial specializations of the member template are ignored for this |
| 3885 |
// specialization of the enclosing class template. If a partial |
| 3886 |
// specialization of the member template is explicitly specialized for a |
| 3887 |
// given (implicit) specialization of the enclosing class template, the |
| 3888 |
// primary member template and its other partial specializations are |
| 3889 |
// still considered for this specialization of the enclosing class |
| 3890 |
// template. |
| 3891 |
if (Template->getMostRecentDecl()->isMemberSpecialization() && |
| 3892 |
!Partial->getMostRecentDecl()->isMemberSpecialization()) |
| 3893 |
continue; |
| 3894 |
|
| 3895 |
TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3896 |
if (TemplateDeductionResult Result = S.DeduceTemplateArguments( |
| 3897 |
Partial, ClassTemplateSpec->getTemplateArgs().asArray(), Info); |
| 3898 |
Result != TemplateDeductionResult::Success) { |
| 3899 |
// Store the failed-deduction information for use in diagnostics, later. |
| 3900 |
// TODO: Actually use the failed-deduction info? |
| 3901 |
FailedCandidates.addCandidate().set( |
| 3902 |
DeclAccessPair::make(Template, AS_public), Partial, |
| 3903 |
MakeDeductionFailureInfo(S.Context, Result, Info)); |
| 3904 |
(void)Result; |
| 3905 |
} else { |
| 3906 |
auto &List = Info.hasStrictPackMatch() ? ExtraMatched : Matched; |
| 3907 |
List.push_back(MatchResult{Partial, Info.takeCanonical()}); |
| 3908 |
} |
| 3909 |
} |
| 3910 |
if (Matched.empty() && PrimaryStrictPackMatch) |
| 3911 |
Matched = std::move(ExtraMatched); |
| 3912 |
|
| 3913 |
// If we're dealing with a member template where the template parameters |
| 3914 |
// have been instantiated, this provides the original template parameters |
| 3915 |
// from which the member template's parameters were instantiated. |
| 3916 |
|
| 3917 |
if (Matched.size() >= 1) { |
| 3918 |
SmallVectorImpl<MatchResult>::iterator Best = Matched.begin(); |
| 3919 |
if (Matched.size() == 1) { |
| 3920 |
// -- If exactly one matching specialization is found, the |
| 3921 |
// instantiation is generated from that specialization. |
| 3922 |
// We don't need to do anything for this. |
| 3923 |
} else { |
| 3924 |
// -- If more than one matching specialization is found, the |
| 3925 |
// partial order rules (14.5.4.2) are used to determine |
| 3926 |
// whether one of the specializations is more specialized |
| 3927 |
// than the others. If none of the specializations is more |
| 3928 |
// specialized than all of the other matching |
| 3929 |
// specializations, then the use of the class template is |
| 3930 |
// ambiguous and the program is ill-formed. |
| 3931 |
for (SmallVectorImpl<MatchResult>::iterator P = Best + 1, |
| 3932 |
PEnd = Matched.end(); |
| 3933 |
P != PEnd; ++P) { |
| 3934 |
if (S.getMoreSpecializedPartialSpecialization( |
| 3935 |
P->Partial, Best->Partial, PointOfInstantiation) == |
| 3936 |
P->Partial) |
| 3937 |
Best = P; |
| 3938 |
} |
| 3939 |
|
| 3940 |
// Determine if the best partial specialization is more specialized than |
| 3941 |
// the others. |
| 3942 |
bool Ambiguous = false; |
| 3943 |
for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
| 3944 |
PEnd = Matched.end(); |
| 3945 |
P != PEnd; ++P) { |
| 3946 |
if (P != Best && S.getMoreSpecializedPartialSpecialization( |
| 3947 |
P->Partial, Best->Partial, |
| 3948 |
PointOfInstantiation) != Best->Partial) { |
| 3949 |
Ambiguous = true; |
| 3950 |
break; |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
if (Ambiguous) { |
| 3955 |
// Partial ordering did not produce a clear winner. Complain. |
| 3956 |
Inst.Clear(); |
| 3957 |
NSC.reset(); |
| 3958 |
S.Diag(PointOfInstantiation, |
| 3959 |
diag::err_partial_spec_ordering_ambiguous) |
| 3960 |
<< ClassTemplateSpec; |
| 3961 |
|
| 3962 |
// Print the matching partial specializations. |
| 3963 |
for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
| 3964 |
PEnd = Matched.end(); |
| 3965 |
P != PEnd; ++P) |
| 3966 |
S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 3967 |
<< S.getTemplateArgumentBindingsText( |
| 3968 |
P->Partial->getTemplateParameters(), *P->Args); |
| 3969 |
|
| 3970 |
return {/*Invalid=*/true}; |
| 3971 |
} |
| 3972 |
} |
| 3973 |
|
| 3974 |
ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); |
| 3975 |
} else { |
| 3976 |
// -- If no matches are found, the instantiation is generated |
| 3977 |
// from the primary template. |
| 3978 |
} |
| 3979 |
} |
| 3980 |
|
| 3981 |
CXXRecordDecl *Pattern = nullptr; |
| 3982 |
Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
| 3983 |
if (auto *PartialSpec = |
| 3984 |
Specialized.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 3985 |
// Instantiate using the best class template partial specialization. |
| 3986 |
while (PartialSpec->getInstantiatedFromMember()) { |
| 3987 |
// If we've found an explicit specialization of this class template, |
| 3988 |
// stop here and use that as the pattern. |
| 3989 |
if (PartialSpec->isMemberSpecialization()) |
| 3990 |
break; |
| 3991 |
|
| 3992 |
PartialSpec = PartialSpec->getInstantiatedFromMember(); |
| 3993 |
} |
| 3994 |
Pattern = PartialSpec; |
| 3995 |
} else { |
| 3996 |
ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| 3997 |
while (Template->getInstantiatedFromMemberTemplate()) { |
| 3998 |
// If we've found an explicit specialization of this class template, |
| 3999 |
// stop here and use that as the pattern. |
| 4000 |
if (Template->isMemberSpecialization()) |
| 4001 |
break; |
| 4002 |
|
| 4003 |
Template = Template->getInstantiatedFromMemberTemplate(); |
| 4004 |
} |
| 4005 |
Pattern = Template->getTemplatedDecl(); |
| 4006 |
} |
| 4007 |
|
| 4008 |
return Pattern; |
| 4009 |
} |
| 4010 |
|
| 4011 |
bool Sema::InstantiateClassTemplateSpecialization( |
| 4012 |
SourceLocation PointOfInstantiation, |
| 4013 |
ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 4014 |
TemplateSpecializationKind TSK, bool Complain, |
| 4015 |
bool PrimaryStrictPackMatch) { |
| 4016 |
// Perform the actual instantiation on the canonical declaration. |
| 4017 |
ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 4018 |
ClassTemplateSpec->getCanonicalDecl()); |
| 4019 |
if (ClassTemplateSpec->isInvalidDecl()) |
| 4020 |
return true; |
| 4021 |
|
| 4022 |
Sema::RecursiveInstGuard AlreadyInstantiating( |
| 4023 |
*this, ClassTemplateSpec, Sema::RecursiveInstGuard::Kind::Template); |
| 4024 |
if (AlreadyInstantiating) |
| 4025 |
return false; |
| 4026 |
|
| 4027 |
bool HadAvaibilityWarning = |
| 4028 |
ShouldDiagnoseAvailabilityOfDecl(ClassTemplateSpec, nullptr, nullptr) |
| 4029 |
.first != AR_Available; |
| 4030 |
|
| 4031 |
ActionResult<CXXRecordDecl *> Pattern = |
| 4032 |
getPatternForClassTemplateSpecialization(*this, PointOfInstantiation, |
| 4033 |
ClassTemplateSpec, TSK, |
| 4034 |
PrimaryStrictPackMatch); |
| 4035 |
|
| 4036 |
if (!Pattern.isUsable()) |
| 4037 |
return Pattern.isInvalid(); |
| 4038 |
|
| 4039 |
bool Err = InstantiateClassImpl( |
| 4040 |
PointOfInstantiation, ClassTemplateSpec, Pattern.get(), |
| 4041 |
getTemplateInstantiationArgs(ClassTemplateSpec), TSK, Complain); |
| 4042 |
|
| 4043 |
// If we haven't already warn on avaibility, consider the avaibility |
| 4044 |
// attributes of the partial specialization. |
| 4045 |
// Note that - because we need to have deduced the partial specialization - |
| 4046 |
// We can only emit these warnings when the specialization is instantiated. |
| 4047 |
if (!Err && !HadAvaibilityWarning) { |
| 4048 |
assert(ClassTemplateSpec->getTemplateSpecializationKind() != |
| 4049 |
TSK_Undeclared); |
| 4050 |
DiagnoseAvailabilityOfDecl(ClassTemplateSpec, PointOfInstantiation); |
| 4051 |
} |
| 4052 |
return Err; |
| 4053 |
} |
| 4054 |
|
| 4055 |
void |
| 4056 |
Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
| 4057 |
CXXRecordDecl *Instantiation, |
| 4058 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4059 |
TemplateSpecializationKind TSK) { |
| 4060 |
// FIXME: We need to notify the ASTMutationListener that we did all of these |
| 4061 |
// things, in case we have an explicit instantiation definition in a PCM, a |
| 4062 |
// module, or preamble, and the declaration is in an imported AST. |
| 4063 |
assert( |
| 4064 |
(TSK == TSK_ExplicitInstantiationDefinition || |
| 4065 |
TSK == TSK_ExplicitInstantiationDeclaration || |
| 4066 |
(TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) && |
| 4067 |
"Unexpected template specialization kind!"); |
| 4068 |
for (auto *D : Instantiation->decls()) { |
| 4069 |
bool SuppressNew = false; |
| 4070 |
if (auto *Function = dyn_cast<FunctionDecl>(D)) { |
| 4071 |
if (FunctionDecl *Pattern = |
| 4072 |
Function->getInstantiatedFromMemberFunction()) { |
| 4073 |
|
| 4074 |
if (Function->getTrailingRequiresClause()) { |
| 4075 |
ConstraintSatisfaction Satisfaction; |
| 4076 |
if (CheckFunctionConstraints(Function, Satisfaction) || |
| 4077 |
!Satisfaction.IsSatisfied) { |
| 4078 |
continue; |
| 4079 |
} |
| 4080 |
} |
| 4081 |
|
| 4082 |
if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
| 4083 |
continue; |
| 4084 |
|
| 4085 |
TemplateSpecializationKind PrevTSK = |
| 4086 |
Function->getTemplateSpecializationKind(); |
| 4087 |
if (PrevTSK == TSK_ExplicitSpecialization) |
| 4088 |
continue; |
| 4089 |
|
| 4090 |
if (CheckSpecializationInstantiationRedecl( |
| 4091 |
PointOfInstantiation, TSK, Function, PrevTSK, |
| 4092 |
Function->getPointOfInstantiation(), SuppressNew) || |
| 4093 |
SuppressNew) |
| 4094 |
continue; |
| 4095 |
|
| 4096 |
// C++11 [temp.explicit]p8: |
| 4097 |
// An explicit instantiation definition that names a class template |
| 4098 |
// specialization explicitly instantiates the class template |
| 4099 |
// specialization and is only an explicit instantiation definition |
| 4100 |
// of members whose definition is visible at the point of |
| 4101 |
// instantiation. |
| 4102 |
if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined()) |
| 4103 |
continue; |
| 4104 |
|
| 4105 |
Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 4106 |
|
| 4107 |
if (Function->isDefined()) { |
| 4108 |
// Let the ASTConsumer know that this function has been explicitly |
| 4109 |
// instantiated now, and its linkage might have changed. |
| 4110 |
Consumer.HandleTopLevelDecl(DeclGroupRef(Function)); |
| 4111 |
} else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 4112 |
InstantiateFunctionDefinition(PointOfInstantiation, Function); |
| 4113 |
} else if (TSK == TSK_ImplicitInstantiation) { |
| 4114 |
PendingLocalImplicitInstantiations.push_back( |
| 4115 |
std::make_pair(Function, PointOfInstantiation)); |
| 4116 |
} |
| 4117 |
} |
| 4118 |
} else if (auto *Var = dyn_cast<VarDecl>(D)) { |
| 4119 |
if (isa<VarTemplateSpecializationDecl>(Var)) |
| 4120 |
continue; |
| 4121 |
|
| 4122 |
if (Var->isStaticDataMember()) { |
| 4123 |
if (Var->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
| 4124 |
continue; |
| 4125 |
|
| 4126 |
MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 4127 |
assert(MSInfo && "No member specialization information?"); |
| 4128 |
if (MSInfo->getTemplateSpecializationKind() |
| 4129 |
== TSK_ExplicitSpecialization) |
| 4130 |
continue; |
| 4131 |
|
| 4132 |
if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 4133 |
Var, |
| 4134 |
MSInfo->getTemplateSpecializationKind(), |
| 4135 |
MSInfo->getPointOfInstantiation(), |
| 4136 |
SuppressNew) || |
| 4137 |
SuppressNew) |
| 4138 |
continue; |
| 4139 |
|
| 4140 |
if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 4141 |
// C++0x [temp.explicit]p8: |
| 4142 |
// An explicit instantiation definition that names a class template |
| 4143 |
// specialization explicitly instantiates the class template |
| 4144 |
// specialization and is only an explicit instantiation definition |
| 4145 |
// of members whose definition is visible at the point of |
| 4146 |
// instantiation. |
| 4147 |
if (!Var->getInstantiatedFromStaticDataMember()->getDefinition()) |
| 4148 |
continue; |
| 4149 |
|
| 4150 |
Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 4151 |
InstantiateVariableDefinition(PointOfInstantiation, Var); |
| 4152 |
} else { |
| 4153 |
Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
| 4154 |
} |
| 4155 |
} |
| 4156 |
} else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 4157 |
if (Record->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
| 4158 |
continue; |
| 4159 |
|
| 4160 |
// Always skip the injected-class-name, along with any |
| 4161 |
// redeclarations of nested classes, since both would cause us |
| 4162 |
// to try to instantiate the members of a class twice. |
| 4163 |
// Skip closure types; they'll get instantiated when we instantiate |
| 4164 |
// the corresponding lambda-expression. |
| 4165 |
if (Record->isInjectedClassName() || Record->getPreviousDecl() || |
| 4166 |
Record->isLambda()) |
| 4167 |
continue; |
| 4168 |
|
| 4169 |
MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
| 4170 |
assert(MSInfo && "No member specialization information?"); |
| 4171 |
|
| 4172 |
if (MSInfo->getTemplateSpecializationKind() |
| 4173 |
== TSK_ExplicitSpecialization) |
| 4174 |
continue; |
| 4175 |
|
| 4176 |
if (Context.getTargetInfo().getTriple().isOSWindows() && |
| 4177 |
TSK == TSK_ExplicitInstantiationDeclaration) { |
| 4178 |
// On Windows, explicit instantiation decl of the outer class doesn't |
| 4179 |
// affect the inner class. Typically extern template declarations are |
| 4180 |
// used in combination with dll import/export annotations, but those |
| 4181 |
// are not propagated from the outer class templates to inner classes. |
| 4182 |
// Therefore, do not instantiate inner classes on this platform, so |
| 4183 |
// that users don't end up with undefined symbols during linking. |
| 4184 |
continue; |
| 4185 |
} |
| 4186 |
|
| 4187 |
if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
| 4188 |
Record, |
| 4189 |
MSInfo->getTemplateSpecializationKind(), |
| 4190 |
MSInfo->getPointOfInstantiation(), |
| 4191 |
SuppressNew) || |
| 4192 |
SuppressNew) |
| 4193 |
continue; |
| 4194 |
|
| 4195 |
CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4196 |
assert(Pattern && "Missing instantiated-from-template information"); |
| 4197 |
|
| 4198 |
if (!Record->getDefinition()) { |
| 4199 |
if (!Pattern->getDefinition()) { |
| 4200 |
// C++0x [temp.explicit]p8: |
| 4201 |
// An explicit instantiation definition that names a class template |
| 4202 |
// specialization explicitly instantiates the class template |
| 4203 |
// specialization and is only an explicit instantiation definition |
| 4204 |
// of members whose definition is visible at the point of |
| 4205 |
// instantiation. |
| 4206 |
if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 4207 |
MSInfo->setTemplateSpecializationKind(TSK); |
| 4208 |
MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 4209 |
} |
| 4210 |
|
| 4211 |
continue; |
| 4212 |
} |
| 4213 |
|
| 4214 |
InstantiateClass(PointOfInstantiation, Record, Pattern, |
| 4215 |
TemplateArgs, |
| 4216 |
TSK); |
| 4217 |
} else { |
| 4218 |
if (TSK == TSK_ExplicitInstantiationDefinition && |
| 4219 |
Record->getTemplateSpecializationKind() == |
| 4220 |
TSK_ExplicitInstantiationDeclaration) { |
| 4221 |
Record->setTemplateSpecializationKind(TSK); |
| 4222 |
MarkVTableUsed(PointOfInstantiation, Record, true); |
| 4223 |
} |
| 4224 |
} |
| 4225 |
|
| 4226 |
Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
| 4227 |
if (Pattern) |
| 4228 |
InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
| 4229 |
TSK); |
| 4230 |
} else if (auto *Enum = dyn_cast<EnumDecl>(D)) { |
| 4231 |
MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo(); |
| 4232 |
assert(MSInfo && "No member specialization information?"); |
| 4233 |
|
| 4234 |
if (MSInfo->getTemplateSpecializationKind() |
| 4235 |
== TSK_ExplicitSpecialization) |
| 4236 |
continue; |
| 4237 |
|
| 4238 |
if (CheckSpecializationInstantiationRedecl( |
| 4239 |
PointOfInstantiation, TSK, Enum, |
| 4240 |
MSInfo->getTemplateSpecializationKind(), |
| 4241 |
MSInfo->getPointOfInstantiation(), SuppressNew) || |
| 4242 |
SuppressNew) |
| 4243 |
continue; |
| 4244 |
|
| 4245 |
if (Enum->getDefinition()) |
| 4246 |
continue; |
| 4247 |
|
| 4248 |
EnumDecl *Pattern = Enum->getTemplateInstantiationPattern(); |
| 4249 |
assert(Pattern && "Missing instantiated-from-template information"); |
| 4250 |
|
| 4251 |
if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 4252 |
if (!Pattern->getDefinition()) |
| 4253 |
continue; |
| 4254 |
|
| 4255 |
InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK); |
| 4256 |
} else { |
| 4257 |
MSInfo->setTemplateSpecializationKind(TSK); |
| 4258 |
MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 4259 |
} |
| 4260 |
} else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
| 4261 |
// No need to instantiate in-class initializers during explicit |
| 4262 |
// instantiation. |
| 4263 |
if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) { |
| 4264 |
// Handle local classes which could have substituted template params. |
| 4265 |
CXXRecordDecl *ClassPattern = |
| 4266 |
Instantiation->isLocalClass() |
| 4267 |
? Instantiation->getInstantiatedFromMemberClass() |
| 4268 |
: Instantiation->getTemplateInstantiationPattern(); |
| 4269 |
|
| 4270 |
DeclContext::lookup_result Lookup = |
| 4271 |
ClassPattern->lookup(Field->getDeclName()); |
| 4272 |
FieldDecl *Pattern = Lookup.find_first<FieldDecl>(); |
| 4273 |
assert(Pattern); |
| 4274 |
InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern, |
| 4275 |
TemplateArgs); |
| 4276 |
} |
| 4277 |
} |
| 4278 |
} |
| 4279 |
} |
| 4280 |
|
| 4281 |
void |
| 4282 |
Sema::InstantiateClassTemplateSpecializationMembers( |
| 4283 |
SourceLocation PointOfInstantiation, |
| 4284 |
ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 4285 |
TemplateSpecializationKind TSK) { |
| 4286 |
// C++0x [temp.explicit]p7: |
| 4287 |
// An explicit instantiation that names a class template |
| 4288 |
// specialization is an explicit instantion of the same kind |
| 4289 |
// (declaration or definition) of each of its members (not |
| 4290 |
// including members inherited from base classes) that has not |
| 4291 |
// been previously explicitly specialized in the translation unit |
| 4292 |
// containing the explicit instantiation, except as described |
| 4293 |
// below. |
| 4294 |
InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
| 4295 |
getTemplateInstantiationArgs(ClassTemplateSpec), |
| 4296 |
TSK); |
| 4297 |
} |
| 4298 |
|
| 4299 |
StmtResult |
| 4300 |
Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4301 |
if (!S) |
| 4302 |
return S; |
| 4303 |
|
| 4304 |
TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 4305 |
SourceLocation(), |
| 4306 |
DeclarationName()); |
| 4307 |
return Instantiator.TransformStmt(S); |
| 4308 |
} |
| 4309 |
|
| 4310 |
bool Sema::SubstTemplateArgument( |
| 4311 |
const TemplateArgumentLoc &Input, |
| 4312 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4313 |
TemplateArgumentLoc &Output, SourceLocation Loc, |
| 4314 |
const DeclarationName &Entity) { |
| 4315 |
TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
| 4316 |
return Instantiator.TransformTemplateArgument(Input, Output); |
| 4317 |
} |
| 4318 |
|
| 4319 |
bool Sema::SubstTemplateArguments( |
| 4320 |
ArrayRef<TemplateArgumentLoc> Args, |
| 4321 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4322 |
TemplateArgumentListInfo &Out) { |
| 4323 |
TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 4324 |
DeclarationName()); |
| 4325 |
return Instantiator.TransformTemplateArguments(Args.begin(), Args.end(), Out); |
| 4326 |
} |
| 4327 |
|
| 4328 |
bool Sema::SubstTemplateArgumentsInParameterMapping( |
| 4329 |
ArrayRef<TemplateArgumentLoc> Args, SourceLocation BaseLoc, |
| 4330 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4331 |
TemplateArgumentListInfo &Out) { |
| 4332 |
TemplateInstantiator Instantiator( |
| 4333 |
TemplateInstantiator::ForParameterMappingSubstitution, *this, BaseLoc, |
| 4334 |
TemplateArgs); |
| 4335 |
return Instantiator.TransformTemplateArguments(Args.begin(), Args.end(), Out); |
| 4336 |
} |
| 4337 |
|
| 4338 |
ExprResult |
| 4339 |
Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4340 |
if (!E) |
| 4341 |
return E; |
| 4342 |
|
| 4343 |
TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 4344 |
SourceLocation(), |
| 4345 |
DeclarationName()); |
| 4346 |
return Instantiator.TransformExpr(E); |
| 4347 |
} |
| 4348 |
|
| 4349 |
ExprResult |
| 4350 |
Sema::SubstCXXIdExpr(Expr *E, |
| 4351 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4352 |
if (!E) |
| 4353 |
return E; |
| 4354 |
|
| 4355 |
TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 4356 |
DeclarationName()); |
| 4357 |
return Instantiator.TransformAddressOfOperand(E); |
| 4358 |
} |
| 4359 |
|
| 4360 |
ExprResult |
| 4361 |
Sema::SubstConstraintExpr(Expr *E, |
| 4362 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4363 |
// FIXME: should call SubstExpr directly if this function is equivalent or |
| 4364 |
// should it be different? |
| 4365 |
return SubstExpr(E, TemplateArgs); |
| 4366 |
} |
| 4367 |
|
| 4368 |
ExprResult Sema::SubstConstraintExprWithoutSatisfaction( |
| 4369 |
Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4370 |
if (!E) |
| 4371 |
return E; |
| 4372 |
|
| 4373 |
TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 4374 |
DeclarationName()); |
| 4375 |
Instantiator.setEvaluateConstraints(false); |
| 4376 |
return Instantiator.TransformExpr(E); |
| 4377 |
} |
| 4378 |
|
| 4379 |
ExprResult Sema::SubstConceptTemplateArguments( |
| 4380 |
const ConceptSpecializationExpr *CSE, const Expr *ConstraintExpr, |
| 4381 |
const MultiLevelTemplateArgumentList &MLTAL) { |
| 4382 |
TemplateInstantiator Instantiator(*this, MLTAL, SourceLocation(), |
| 4383 |
DeclarationName()); |
| 4384 |
const ASTTemplateArgumentListInfo *ArgsAsWritten = |
| 4385 |
CSE->getTemplateArgsAsWritten(); |
| 4386 |
TemplateArgumentListInfo SubstArgs(ArgsAsWritten->getLAngleLoc(), |
| 4387 |
ArgsAsWritten->getRAngleLoc()); |
| 4388 |
|
| 4389 |
NonSFINAEContext _(*this); |
| 4390 |
Sema::InstantiatingTemplate Inst( |
| 4391 |
*this, ArgsAsWritten->arguments().front().getSourceRange().getBegin(), |
| 4392 |
Sema::InstantiatingTemplate::ConstraintNormalization{}, |
| 4393 |
CSE->getNamedConcept(), |
| 4394 |
ArgsAsWritten->arguments().front().getSourceRange()); |
| 4395 |
|
| 4396 |
if (Inst.isInvalid()) |
| 4397 |
return ExprError(); |
| 4398 |
|
| 4399 |
if (Instantiator.TransformConceptTemplateArguments( |
| 4400 |
ArgsAsWritten->getTemplateArgs(), |
| 4401 |
ArgsAsWritten->getTemplateArgs() + |
| 4402 |
ArgsAsWritten->getNumTemplateArgs(), |
| 4403 |
SubstArgs)) |
| 4404 |
return true; |
| 4405 |
|
| 4406 |
llvm::SmallVector<TemplateArgument, 4> NewArgList = llvm::map_to_vector( |
| 4407 |
SubstArgs.arguments(), |
| 4408 |
[](const TemplateArgumentLoc &Loc) { return Loc.getArgument(); }); |
| 4409 |
|
| 4410 |
MultiLevelTemplateArgumentList MLTALForConstraint = |
| 4411 |
getTemplateInstantiationArgs( |
| 4412 |
CSE->getNamedConcept(), |
| 4413 |
CSE->getNamedConcept()->getLexicalDeclContext(), |
| 4414 |
/*Final=*/false, |
| 4415 |
/*Innermost=*/NewArgList, |
| 4416 |
/*RelativeToPrimary=*/true, |
| 4417 |
/*Pattern=*/nullptr, |
| 4418 |
/*ForConstraintInstantiation=*/true); |
| 4419 |
|
| 4420 |
// Rebuild a constraint, only substituting non-dependent concept names |
| 4421 |
// and nothing else. |
| 4422 |
// Given C<SomeType, SomeValue, SomeConceptName, SomeDependentConceptName>. |
| 4423 |
// only SomeConceptName is substituted, in the constraint expression of C. |
| 4424 |
struct ConstraintExprTransformer : TreeTransform<ConstraintExprTransformer> { |
| 4425 |
using Base = TreeTransform<ConstraintExprTransformer>; |
| 4426 |
MultiLevelTemplateArgumentList &MLTAL; |
| 4427 |
|
| 4428 |
ConstraintExprTransformer(Sema &SemaRef, |
| 4429 |
MultiLevelTemplateArgumentList &MLTAL) |
| 4430 |
: TreeTransform(SemaRef), MLTAL(MLTAL) {} |
| 4431 |
|
| 4432 |
ExprResult TransformExpr(Expr *E) { |
| 4433 |
if (!E) |
| 4434 |
return E; |
| 4435 |
switch (E->getStmtClass()) { |
| 4436 |
case Stmt::BinaryOperatorClass: |
| 4437 |
case Stmt::ConceptSpecializationExprClass: |
| 4438 |
case Stmt::ParenExprClass: |
| 4439 |
case Stmt::UnresolvedLookupExprClass: |
| 4440 |
return Base::TransformExpr(E); |
| 4441 |
default: |
| 4442 |
break; |
| 4443 |
} |
| 4444 |
return E; |
| 4445 |
} |
| 4446 |
|
| 4447 |
// Rebuild both branches of a conjunction / disjunction |
| 4448 |
// even if there is a substitution failure in one of |
| 4449 |
// the branch. |
| 4450 |
ExprResult TransformBinaryOperator(BinaryOperator *E) { |
| 4451 |
if (!(E->getOpcode() == BinaryOperatorKind::BO_LAnd || |
| 4452 |
E->getOpcode() == BinaryOperatorKind::BO_LOr)) |
| 4453 |
return E; |
| 4454 |
|
| 4455 |
ExprResult LHS = TransformExpr(E->getLHS()); |
| 4456 |
ExprResult RHS = TransformExpr(E->getRHS()); |
| 4457 |
|
| 4458 |
if (LHS.get() == E->getLHS() && RHS.get() == E->getRHS()) |
| 4459 |
return E; |
| 4460 |
|
| 4461 |
return BinaryOperator::Create(SemaRef.Context, LHS.get(), RHS.get(), |
| 4462 |
E->getOpcode(), SemaRef.Context.BoolTy, |
| 4463 |
VK_PRValue, OK_Ordinary, |
| 4464 |
E->getOperatorLoc(), FPOptionsOverride{}); |
| 4465 |
} |
| 4466 |
|
| 4467 |
bool TransformTemplateArgument(const TemplateArgumentLoc &Input, |
| 4468 |
TemplateArgumentLoc &Output, |
| 4469 |
bool Uneval = false) { |
| 4470 |
if (Input.getArgument().isConceptOrConceptTemplateParameter()) |
| 4471 |
return Base::TransformTemplateArgument(Input, Output, Uneval); |
| 4472 |
|
| 4473 |
Output = Input; |
| 4474 |
return false; |
| 4475 |
} |
| 4476 |
|
| 4477 |
ExprResult TransformUnresolvedLookupExpr(UnresolvedLookupExpr *E, |
| 4478 |
bool IsAddressOfOperand = false) { |
| 4479 |
if (E->isConceptReference()) { |
| 4480 |
ExprResult Res = SemaRef.SubstExpr(E, MLTAL); |
| 4481 |
return Res; |
| 4482 |
} |
| 4483 |
return E; |
| 4484 |
} |
| 4485 |
}; |
| 4486 |
|
| 4487 |
ConstraintExprTransformer Transformer(*this, MLTALForConstraint); |
| 4488 |
ExprResult Res = |
| 4489 |
Transformer.TransformExpr(const_cast<Expr *>(ConstraintExpr)); |
| 4490 |
return Res; |
| 4491 |
} |
| 4492 |
|
| 4493 |
ExprResult Sema::SubstInitializer(Expr *Init, |
| 4494 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4495 |
bool CXXDirectInit) { |
| 4496 |
TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
| 4497 |
DeclarationName()); |
| 4498 |
return Instantiator.TransformInitializer(Init, CXXDirectInit); |
| 4499 |
} |
| 4500 |
|
| 4501 |
bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall, |
| 4502 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4503 |
SmallVectorImpl<Expr *> &Outputs) { |
| 4504 |
if (Exprs.empty()) |
| 4505 |
return false; |
| 4506 |
|
| 4507 |
TemplateInstantiator Instantiator(*this, TemplateArgs, |
| 4508 |
SourceLocation(), |
| 4509 |
DeclarationName()); |
| 4510 |
return Instantiator.TransformExprs(Exprs.data(), Exprs.size(), |
| 4511 |
IsCall, Outputs); |
| 4512 |
} |
| 4513 |
|
| 4514 |
NestedNameSpecifierLoc |
| 4515 |
Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| 4516 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4517 |
if (!NNS) |
| 4518 |
return NestedNameSpecifierLoc(); |
| 4519 |
|
| 4520 |
TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(), |
| 4521 |
DeclarationName()); |
| 4522 |
return Instantiator.TransformNestedNameSpecifierLoc(NNS); |
| 4523 |
} |
| 4524 |
|
| 4525 |
DeclarationNameInfo |
| 4526 |
Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
| 4527 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4528 |
TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
| 4529 |
NameInfo.getName()); |
| 4530 |
return Instantiator.TransformDeclarationNameInfo(NameInfo); |
| 4531 |
} |
| 4532 |
|
| 4533 |
TemplateName |
| 4534 |
Sema::SubstTemplateName(SourceLocation TemplateKWLoc, |
| 4535 |
NestedNameSpecifierLoc &QualifierLoc, TemplateName Name, |
| 4536 |
SourceLocation NameLoc, |
| 4537 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4538 |
TemplateInstantiator Instantiator(*this, TemplateArgs, NameLoc, |
| 4539 |
DeclarationName()); |
| 4540 |
return Instantiator.TransformTemplateName(QualifierLoc, TemplateKWLoc, Name, |
| 4541 |
NameLoc); |
| 4542 |
} |
| 4543 |
|
| 4544 |
static const Decl *getCanonicalParmVarDecl(const Decl *D) { |
| 4545 |
// When storing ParmVarDecls in the local instantiation scope, we always |
| 4546 |
// want to use the ParmVarDecl from the canonical function declaration, |
| 4547 |
// since the map is then valid for any redeclaration or definition of that |
| 4548 |
// function. |
| 4549 |
if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) { |
| 4550 |
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
| 4551 |
unsigned i = PV->getFunctionScopeIndex(); |
| 4552 |
// This parameter might be from a freestanding function type within the |
| 4553 |
// function and isn't necessarily referring to one of FD's parameters. |
| 4554 |
if (i < FD->getNumParams() && FD->getParamDecl(i) == PV) |
| 4555 |
return FD->getCanonicalDecl()->getParamDecl(i); |
| 4556 |
} |
| 4557 |
} |
| 4558 |
return D; |
| 4559 |
} |
| 4560 |
|
| 4561 |
llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * |
| 4562 |
LocalInstantiationScope::getInstantiationOfIfExists(const Decl *D) { |
| 4563 |
D = getCanonicalParmVarDecl(D); |
| 4564 |
for (LocalInstantiationScope *Current = this; Current; |
| 4565 |
Current = Current->Outer) { |
| 4566 |
|
| 4567 |
// Check if we found something within this scope. |
| 4568 |
const Decl *CheckD = D; |
| 4569 |
do { |
| 4570 |
LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD); |
| 4571 |
if (Found != Current->LocalDecls.end()) |
| 4572 |
return &Found->second; |
| 4573 |
|
| 4574 |
// If this is a tag declaration, it's possible that we need to look for |
| 4575 |
// a previous declaration. |
| 4576 |
if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) |
| 4577 |
CheckD = Tag->getPreviousDecl(); |
| 4578 |
else |
| 4579 |
CheckD = nullptr; |
| 4580 |
} while (CheckD); |
| 4581 |
|
| 4582 |
// If we aren't combined with our outer scope, we're done. |
| 4583 |
if (!Current->CombineWithOuterScope) |
| 4584 |
break; |
| 4585 |
} |
| 4586 |
|
| 4587 |
return nullptr; |
| 4588 |
} |
| 4589 |
|
| 4590 |
llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * |
| 4591 |
LocalInstantiationScope::findInstantiationOf(const Decl *D) { |
| 4592 |
auto *Result = getInstantiationOfIfExists(D); |
| 4593 |
if (Result) |
| 4594 |
return Result; |
| 4595 |
// If we're performing a partial substitution during template argument |
| 4596 |
// deduction, we may not have values for template parameters yet. |
| 4597 |
if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 4598 |
isa<TemplateTemplateParmDecl>(D)) |
| 4599 |
return nullptr; |
| 4600 |
|
| 4601 |
// Local types referenced prior to definition may require instantiation. |
| 4602 |
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 4603 |
if (RD->isLocalClass()) |
| 4604 |
return nullptr; |
| 4605 |
|
| 4606 |
// Enumeration types referenced prior to definition may appear as a result of |
| 4607 |
// error recovery. |
| 4608 |
if (isa<EnumDecl>(D)) |
| 4609 |
return nullptr; |
| 4610 |
|
| 4611 |
// Materialized typedefs/type alias for implicit deduction guides may require |
| 4612 |
// instantiation. |
| 4613 |
if (isa<TypedefNameDecl>(D) && |
| 4614 |
isa<CXXDeductionGuideDecl>(D->getDeclContext())) |
| 4615 |
return nullptr; |
| 4616 |
|
| 4617 |
// If we didn't find the decl, then we either have a sema bug, or we have a |
| 4618 |
// forward reference to a label declaration. Return null to indicate that |
| 4619 |
// we have an uninstantiated label. |
| 4620 |
assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope"); |
| 4621 |
return nullptr; |
| 4622 |
} |
| 4623 |
|
| 4624 |
void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
| 4625 |
D = getCanonicalParmVarDecl(D); |
| 4626 |
llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
| 4627 |
if (Stored.isNull()) { |
| 4628 |
#ifndef NDEBUG |
| 4629 |
// It should not be present in any surrounding scope either. |
| 4630 |
LocalInstantiationScope *Current = this; |
| 4631 |
while (Current->CombineWithOuterScope && Current->Outer) { |
| 4632 |
Current = Current->Outer; |
| 4633 |
assert(!Current->LocalDecls.contains(D) && |
| 4634 |
"Instantiated local in inner and outer scopes"); |
| 4635 |
} |
| 4636 |
#endif |
| 4637 |
Stored = Inst; |
| 4638 |
} else if (DeclArgumentPack *Pack = dyn_cast<DeclArgumentPack *>(Stored)) { |
| 4639 |
Pack->push_back(cast<ValueDecl>(Inst)); |
| 4640 |
} else { |
| 4641 |
assert(cast<Decl *>(Stored) == Inst && "Already instantiated this local"); |
| 4642 |
} |
| 4643 |
} |
| 4644 |
|
| 4645 |
void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, |
| 4646 |
VarDecl *Inst) { |
| 4647 |
D = getCanonicalParmVarDecl(D); |
| 4648 |
DeclArgumentPack *Pack = cast<DeclArgumentPack *>(LocalDecls[D]); |
| 4649 |
Pack->push_back(Inst); |
| 4650 |
} |
| 4651 |
|
| 4652 |
void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { |
| 4653 |
#ifndef NDEBUG |
| 4654 |
// This should be the first time we've been told about this decl. |
| 4655 |
for (LocalInstantiationScope *Current = this; |
| 4656 |
Current && Current->CombineWithOuterScope; Current = Current->Outer) |
| 4657 |
assert(!Current->LocalDecls.contains(D) && |
| 4658 |
"Creating local pack after instantiation of local"); |
| 4659 |
#endif |
| 4660 |
|
| 4661 |
D = getCanonicalParmVarDecl(D); |
| 4662 |
llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
| 4663 |
DeclArgumentPack *Pack = new DeclArgumentPack; |
| 4664 |
Stored = Pack; |
| 4665 |
ArgumentPacks.push_back(Pack); |
| 4666 |
} |
| 4667 |
|
| 4668 |
bool LocalInstantiationScope::isLocalPackExpansion(const Decl *D) { |
| 4669 |
for (DeclArgumentPack *Pack : ArgumentPacks) |
| 4670 |
if (llvm::is_contained(*Pack, D)) |
| 4671 |
return true; |
| 4672 |
return false; |
| 4673 |
} |
| 4674 |
|
| 4675 |
void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack, |
| 4676 |
const TemplateArgument *ExplicitArgs, |
| 4677 |
unsigned NumExplicitArgs) { |
| 4678 |
assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) && |
| 4679 |
"Already have a partially-substituted pack"); |
| 4680 |
assert((!PartiallySubstitutedPack |
| 4681 |
|| NumArgsInPartiallySubstitutedPack == NumExplicitArgs) && |
| 4682 |
"Wrong number of arguments in partially-substituted pack"); |
| 4683 |
PartiallySubstitutedPack = Pack; |
| 4684 |
ArgsInPartiallySubstitutedPack = ExplicitArgs; |
| 4685 |
NumArgsInPartiallySubstitutedPack = NumExplicitArgs; |
| 4686 |
} |
| 4687 |
|
| 4688 |
NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack( |
| 4689 |
const TemplateArgument **ExplicitArgs, |
| 4690 |
unsigned *NumExplicitArgs) const { |
| 4691 |
if (ExplicitArgs) |
| 4692 |
*ExplicitArgs = nullptr; |
| 4693 |
if (NumExplicitArgs) |
| 4694 |
*NumExplicitArgs = 0; |
| 4695 |
|
| 4696 |
for (const LocalInstantiationScope *Current = this; Current; |
| 4697 |
Current = Current->Outer) { |
| 4698 |
if (Current->PartiallySubstitutedPack) { |
| 4699 |
if (ExplicitArgs) |
| 4700 |
*ExplicitArgs = Current->ArgsInPartiallySubstitutedPack; |
| 4701 |
if (NumExplicitArgs) |
| 4702 |
*NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack; |
| 4703 |
|
| 4704 |
return Current->PartiallySubstitutedPack; |
| 4705 |
} |
| 4706 |
|
| 4707 |
if (!Current->CombineWithOuterScope) |
| 4708 |
break; |
| 4709 |
} |
| 4710 |
|
| 4711 |
return nullptr; |
| 4712 |
} |