| 1 |
//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl 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 for declarations. |
| 9 |
// |
| 10 |
//===----------------------------------------------------------------------===/ |
| 11 |
|
| 12 |
#include "TreeTransform.h" |
| 13 |
#include "clang/AST/ASTConsumer.h" |
| 14 |
#include "clang/AST/ASTContext.h" |
| 15 |
#include "clang/AST/ASTMutationListener.h" |
| 16 |
#include "clang/AST/DeclTemplate.h" |
| 17 |
#include "clang/AST/DeclVisitor.h" |
| 18 |
#include "clang/AST/DependentDiagnostic.h" |
| 19 |
#include "clang/AST/Expr.h" |
| 20 |
#include "clang/AST/ExprCXX.h" |
| 21 |
#include "clang/AST/PrettyDeclStackTrace.h" |
| 22 |
#include "clang/AST/TypeLoc.h" |
| 23 |
#include "clang/Basic/SourceManager.h" |
| 24 |
#include "clang/Basic/TargetInfo.h" |
| 25 |
#include "clang/Sema/Initialization.h" |
| 26 |
#include "clang/Sema/Lookup.h" |
| 27 |
#include "clang/Sema/ScopeInfo.h" |
| 28 |
#include "clang/Sema/SemaInternal.h" |
| 29 |
#include "clang/Sema/Template.h" |
| 30 |
#include "clang/Sema/TemplateInstCallback.h" |
| 31 |
#include "llvm/Support/TimeProfiler.h" |
| 32 |
#include <optional> |
| 33 |
|
| 34 |
using namespace clang; |
| 35 |
|
| 36 |
static bool isDeclWithinFunction(const Decl *D) { |
| 37 |
const DeclContext *DC = D->getDeclContext(); |
| 38 |
if (DC->isFunctionOrMethod()) |
| 39 |
return true; |
| 40 |
|
| 41 |
if (DC->isRecord()) |
| 42 |
return cast<CXXRecordDecl>(DC)->isLocalClass(); |
| 43 |
|
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
template<typename DeclT> |
| 48 |
static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl, |
| 49 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 50 |
if (!OldDecl->getQualifierLoc()) |
| 51 |
return false; |
| 52 |
|
| 53 |
assert((NewDecl->getFriendObjectKind() || |
| 54 |
!OldDecl->getLexicalDeclContext()->isDependentContext()) && |
| 55 |
"non-friend with qualified name defined in dependent context"); |
| 56 |
Sema::ContextRAII SavedContext( |
| 57 |
SemaRef, |
| 58 |
const_cast<DeclContext *>(NewDecl->getFriendObjectKind() |
| 59 |
? NewDecl->getLexicalDeclContext() |
| 60 |
: OldDecl->getLexicalDeclContext())); |
| 61 |
|
| 62 |
NestedNameSpecifierLoc NewQualifierLoc |
| 63 |
= SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 64 |
TemplateArgs); |
| 65 |
|
| 66 |
if (!NewQualifierLoc) |
| 67 |
return true; |
| 68 |
|
| 69 |
NewDecl->setQualifierInfo(NewQualifierLoc); |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 74 |
DeclaratorDecl *NewDecl) { |
| 75 |
return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| 76 |
} |
| 77 |
|
| 78 |
bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 79 |
TagDecl *NewDecl) { |
| 80 |
return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| 81 |
} |
| 82 |
|
| 83 |
// Include attribute instantiation code. |
| 84 |
#include "clang/Sema/AttrTemplateInstantiate.inc" |
| 85 |
|
| 86 |
static void instantiateDependentAlignedAttr( |
| 87 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 88 |
const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { |
| 89 |
if (Aligned->isAlignmentExpr()) { |
| 90 |
// The alignment expression is a constant expression. |
| 91 |
EnterExpressionEvaluationContext Unevaluated( |
| 92 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 93 |
ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); |
| 94 |
if (!Result.isInvalid()) |
| 95 |
S.AddAlignedAttr(New, *Aligned, Result.getAs<Expr>(), IsPackExpansion); |
| 96 |
} else { |
| 97 |
TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), |
| 98 |
TemplateArgs, Aligned->getLocation(), |
| 99 |
DeclarationName()); |
| 100 |
if (Result) |
| 101 |
S.AddAlignedAttr(New, *Aligned, Result, IsPackExpansion); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static void instantiateDependentAlignedAttr( |
| 106 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 107 |
const AlignedAttr *Aligned, Decl *New) { |
| 108 |
if (!Aligned->isPackExpansion()) { |
| 109 |
instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 114 |
if (Aligned->isAlignmentExpr()) |
| 115 |
S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), |
| 116 |
Unexpanded); |
| 117 |
else |
| 118 |
S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), |
| 119 |
Unexpanded); |
| 120 |
assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 121 |
|
| 122 |
// Determine whether we can expand this attribute pack yet. |
| 123 |
bool Expand = true, RetainExpansion = false; |
| 124 |
std::optional<unsigned> NumExpansions; |
| 125 |
// FIXME: Use the actual location of the ellipsis. |
| 126 |
SourceLocation EllipsisLoc = Aligned->getLocation(); |
| 127 |
if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), |
| 128 |
Unexpanded, TemplateArgs, Expand, |
| 129 |
RetainExpansion, NumExpansions)) |
| 130 |
return; |
| 131 |
|
| 132 |
if (!Expand) { |
| 133 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); |
| 134 |
instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); |
| 135 |
} else { |
| 136 |
for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 137 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); |
| 138 |
instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static void instantiateDependentAssumeAlignedAttr( |
| 144 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 145 |
const AssumeAlignedAttr *Aligned, Decl *New) { |
| 146 |
// The alignment expression is a constant expression. |
| 147 |
EnterExpressionEvaluationContext Unevaluated( |
| 148 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 149 |
|
| 150 |
Expr *E, *OE = nullptr; |
| 151 |
ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 152 |
if (Result.isInvalid()) |
| 153 |
return; |
| 154 |
E = Result.getAs<Expr>(); |
| 155 |
|
| 156 |
if (Aligned->getOffset()) { |
| 157 |
Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs); |
| 158 |
if (Result.isInvalid()) |
| 159 |
return; |
| 160 |
OE = Result.getAs<Expr>(); |
| 161 |
} |
| 162 |
|
| 163 |
S.AddAssumeAlignedAttr(New, *Aligned, E, OE); |
| 164 |
} |
| 165 |
|
| 166 |
static void instantiateDependentAlignValueAttr( |
| 167 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 168 |
const AlignValueAttr *Aligned, Decl *New) { |
| 169 |
// The alignment expression is a constant expression. |
| 170 |
EnterExpressionEvaluationContext Unevaluated( |
| 171 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 172 |
ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 173 |
if (!Result.isInvalid()) |
| 174 |
S.AddAlignValueAttr(New, *Aligned, Result.getAs<Expr>()); |
| 175 |
} |
| 176 |
|
| 177 |
static void instantiateDependentAllocAlignAttr( |
| 178 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 179 |
const AllocAlignAttr *Align, Decl *New) { |
| 180 |
Expr *Param = IntegerLiteral::Create( |
| 181 |
S.getASTContext(), |
| 182 |
llvm::APInt(64, Align->getParamIndex().getSourceIndex()), |
| 183 |
S.getASTContext().UnsignedLongLongTy, Align->getLocation()); |
| 184 |
S.AddAllocAlignAttr(New, *Align, Param); |
| 185 |
} |
| 186 |
|
| 187 |
static void instantiateDependentAnnotationAttr( |
| 188 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 189 |
const AnnotateAttr *Attr, Decl *New) { |
| 190 |
EnterExpressionEvaluationContext Unevaluated( |
| 191 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 192 |
|
| 193 |
// If the attribute has delayed arguments it will have to instantiate those |
| 194 |
// and handle them as new arguments for the attribute. |
| 195 |
bool HasDelayedArgs = Attr->delayedArgs_size(); |
| 196 |
|
| 197 |
ArrayRef<Expr *> ArgsToInstantiate = |
| 198 |
HasDelayedArgs |
| 199 |
? ArrayRef<Expr *>{Attr->delayedArgs_begin(), Attr->delayedArgs_end()} |
| 200 |
: ArrayRef<Expr *>{Attr->args_begin(), Attr->args_end()}; |
| 201 |
|
| 202 |
SmallVector<Expr *, 4> Args; |
| 203 |
if (S.SubstExprs(ArgsToInstantiate, |
| 204 |
/*IsCall=*/false, TemplateArgs, Args)) |
| 205 |
return; |
| 206 |
|
| 207 |
StringRef Str = Attr->getAnnotation(); |
| 208 |
if (HasDelayedArgs) { |
| 209 |
if (Args.size() < 1) { |
| 210 |
S.Diag(Attr->getLoc(), diag::err_attribute_too_few_arguments) |
| 211 |
<< Attr << 1; |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
if (!S.checkStringLiteralArgumentAttr(*Attr, Args[0], Str)) |
| 216 |
return; |
| 217 |
|
| 218 |
llvm::SmallVector<Expr *, 4> ActualArgs; |
| 219 |
ActualArgs.insert(ActualArgs.begin(), Args.begin() + 1, Args.end()); |
| 220 |
std::swap(Args, ActualArgs); |
| 221 |
} |
| 222 |
S.AddAnnotationAttr(New, *Attr, Str, Args); |
| 223 |
} |
| 224 |
|
| 225 |
static Expr *instantiateDependentFunctionAttrCondition( |
| 226 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 227 |
const Attr *A, Expr *OldCond, const Decl *Tmpl, FunctionDecl *New) { |
| 228 |
Expr *Cond = nullptr; |
| 229 |
{ |
| 230 |
Sema::ContextRAII SwitchContext(S, New); |
| 231 |
EnterExpressionEvaluationContext Unevaluated( |
| 232 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 233 |
ExprResult Result = S.SubstExpr(OldCond, TemplateArgs); |
| 234 |
if (Result.isInvalid()) |
| 235 |
return nullptr; |
| 236 |
Cond = Result.getAs<Expr>(); |
| 237 |
} |
| 238 |
if (!Cond->isTypeDependent()) { |
| 239 |
ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 240 |
if (Converted.isInvalid()) |
| 241 |
return nullptr; |
| 242 |
Cond = Converted.get(); |
| 243 |
} |
| 244 |
|
| 245 |
SmallVector<PartialDiagnosticAt, 8> Diags; |
| 246 |
if (OldCond->isValueDependent() && !Cond->isValueDependent() && |
| 247 |
!Expr::isPotentialConstantExprUnevaluated(Cond, New, Diags)) { |
| 248 |
S.Diag(A->getLocation(), diag::err_attr_cond_never_constant_expr) << A; |
| 249 |
for (const auto &P : Diags) |
| 250 |
S.Diag(P.first, P.second); |
| 251 |
return nullptr; |
| 252 |
} |
| 253 |
return Cond; |
| 254 |
} |
| 255 |
|
| 256 |
static void instantiateDependentEnableIfAttr( |
| 257 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 258 |
const EnableIfAttr *EIA, const Decl *Tmpl, FunctionDecl *New) { |
| 259 |
Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 260 |
S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New); |
| 261 |
|
| 262 |
if (Cond) |
| 263 |
New->addAttr(new (S.getASTContext()) EnableIfAttr(S.getASTContext(), *EIA, |
| 264 |
Cond, EIA->getMessage())); |
| 265 |
} |
| 266 |
|
| 267 |
static void instantiateDependentDiagnoseIfAttr( |
| 268 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 269 |
const DiagnoseIfAttr *DIA, const Decl *Tmpl, FunctionDecl *New) { |
| 270 |
Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 271 |
S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New); |
| 272 |
|
| 273 |
if (Cond) |
| 274 |
New->addAttr(new (S.getASTContext()) DiagnoseIfAttr( |
| 275 |
S.getASTContext(), *DIA, Cond, DIA->getMessage(), |
| 276 |
DIA->getDiagnosticType(), DIA->getArgDependent(), New)); |
| 277 |
} |
| 278 |
|
| 279 |
// Constructs and adds to New a new instance of CUDALaunchBoundsAttr using |
| 280 |
// template A as the base and arguments from TemplateArgs. |
| 281 |
static void instantiateDependentCUDALaunchBoundsAttr( |
| 282 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 283 |
const CUDALaunchBoundsAttr &Attr, Decl *New) { |
| 284 |
// The alignment expression is a constant expression. |
| 285 |
EnterExpressionEvaluationContext Unevaluated( |
| 286 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 287 |
|
| 288 |
ExprResult Result = S.SubstExpr(Attr.getMaxThreads(), TemplateArgs); |
| 289 |
if (Result.isInvalid()) |
| 290 |
return; |
| 291 |
Expr *MaxThreads = Result.getAs<Expr>(); |
| 292 |
|
| 293 |
Expr *MinBlocks = nullptr; |
| 294 |
if (Attr.getMinBlocks()) { |
| 295 |
Result = S.SubstExpr(Attr.getMinBlocks(), TemplateArgs); |
| 296 |
if (Result.isInvalid()) |
| 297 |
return; |
| 298 |
MinBlocks = Result.getAs<Expr>(); |
| 299 |
} |
| 300 |
|
| 301 |
S.AddLaunchBoundsAttr(New, Attr, MaxThreads, MinBlocks); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
instantiateDependentModeAttr(Sema &S, |
| 306 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 307 |
const ModeAttr &Attr, Decl *New) { |
| 308 |
S.AddModeAttr(New, Attr, Attr.getMode(), |
| 309 |
/*InInstantiation=*/true); |
| 310 |
} |
| 311 |
|
| 312 |
/// Instantiation of 'declare simd' attribute and its arguments. |
| 313 |
static void instantiateOMPDeclareSimdDeclAttr( |
| 314 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 315 |
const OMPDeclareSimdDeclAttr &Attr, Decl *New) { |
| 316 |
// Allow 'this' in clauses with varlists. |
| 317 |
if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) |
| 318 |
New = FTD->getTemplatedDecl(); |
| 319 |
auto *FD = cast<FunctionDecl>(New); |
| 320 |
auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); |
| 321 |
SmallVector<Expr *, 4> Uniforms, Aligneds, Alignments, Linears, Steps; |
| 322 |
SmallVector<unsigned, 4> LinModifiers; |
| 323 |
|
| 324 |
auto SubstExpr = [&](Expr *E) -> ExprResult { |
| 325 |
if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 326 |
if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 327 |
Sema::ContextRAII SavedContext(S, FD); |
| 328 |
LocalInstantiationScope Local(S); |
| 329 |
if (FD->getNumParams() > PVD->getFunctionScopeIndex()) |
| 330 |
Local.InstantiatedLocal( |
| 331 |
PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); |
| 332 |
return S.SubstExpr(E, TemplateArgs); |
| 333 |
} |
| 334 |
Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), |
| 335 |
FD->isCXXInstanceMember()); |
| 336 |
return S.SubstExpr(E, TemplateArgs); |
| 337 |
}; |
| 338 |
|
| 339 |
// Substitute a single OpenMP clause, which is a potentially-evaluated |
| 340 |
// full-expression. |
| 341 |
auto Subst = [&](Expr *E) -> ExprResult { |
| 342 |
EnterExpressionEvaluationContext Evaluated( |
| 343 |
S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 344 |
ExprResult Res = SubstExpr(E); |
| 345 |
if (Res.isInvalid()) |
| 346 |
return Res; |
| 347 |
return S.ActOnFinishFullExpr(Res.get(), false); |
| 348 |
}; |
| 349 |
|
| 350 |
ExprResult Simdlen; |
| 351 |
if (auto *E = Attr.getSimdlen()) |
| 352 |
Simdlen = Subst(E); |
| 353 |
|
| 354 |
if (Attr.uniforms_size() > 0) { |
| 355 |
for(auto *E : Attr.uniforms()) { |
| 356 |
ExprResult Inst = Subst(E); |
| 357 |
if (Inst.isInvalid()) |
| 358 |
continue; |
| 359 |
Uniforms.push_back(Inst.get()); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
auto AI = Attr.alignments_begin(); |
| 364 |
for (auto *E : Attr.aligneds()) { |
| 365 |
ExprResult Inst = Subst(E); |
| 366 |
if (Inst.isInvalid()) |
| 367 |
continue; |
| 368 |
Aligneds.push_back(Inst.get()); |
| 369 |
Inst = ExprEmpty(); |
| 370 |
if (*AI) |
| 371 |
Inst = S.SubstExpr(*AI, TemplateArgs); |
| 372 |
Alignments.push_back(Inst.get()); |
| 373 |
++AI; |
| 374 |
} |
| 375 |
|
| 376 |
auto SI = Attr.steps_begin(); |
| 377 |
for (auto *E : Attr.linears()) { |
| 378 |
ExprResult Inst = Subst(E); |
| 379 |
if (Inst.isInvalid()) |
| 380 |
continue; |
| 381 |
Linears.push_back(Inst.get()); |
| 382 |
Inst = ExprEmpty(); |
| 383 |
if (*SI) |
| 384 |
Inst = S.SubstExpr(*SI, TemplateArgs); |
| 385 |
Steps.push_back(Inst.get()); |
| 386 |
++SI; |
| 387 |
} |
| 388 |
LinModifiers.append(Attr.modifiers_begin(), Attr.modifiers_end()); |
| 389 |
(void)S.ActOnOpenMPDeclareSimdDirective( |
| 390 |
S.ConvertDeclToDeclGroup(New), Attr.getBranchState(), Simdlen.get(), |
| 391 |
Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps, |
| 392 |
Attr.getRange()); |
| 393 |
} |
| 394 |
|
| 395 |
/// Instantiation of 'declare variant' attribute and its arguments. |
| 396 |
static void instantiateOMPDeclareVariantAttr( |
| 397 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 398 |
const OMPDeclareVariantAttr &Attr, Decl *New) { |
| 399 |
// Allow 'this' in clauses with varlists. |
| 400 |
if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) |
| 401 |
New = FTD->getTemplatedDecl(); |
| 402 |
auto *FD = cast<FunctionDecl>(New); |
| 403 |
auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); |
| 404 |
|
| 405 |
auto &&SubstExpr = [FD, ThisContext, &S, &TemplateArgs](Expr *E) { |
| 406 |
if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 407 |
if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 408 |
Sema::ContextRAII SavedContext(S, FD); |
| 409 |
LocalInstantiationScope Local(S); |
| 410 |
if (FD->getNumParams() > PVD->getFunctionScopeIndex()) |
| 411 |
Local.InstantiatedLocal( |
| 412 |
PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); |
| 413 |
return S.SubstExpr(E, TemplateArgs); |
| 414 |
} |
| 415 |
Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), |
| 416 |
FD->isCXXInstanceMember()); |
| 417 |
return S.SubstExpr(E, TemplateArgs); |
| 418 |
}; |
| 419 |
|
| 420 |
// Substitute a single OpenMP clause, which is a potentially-evaluated |
| 421 |
// full-expression. |
| 422 |
auto &&Subst = [&SubstExpr, &S](Expr *E) { |
| 423 |
EnterExpressionEvaluationContext Evaluated( |
| 424 |
S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 425 |
ExprResult Res = SubstExpr(E); |
| 426 |
if (Res.isInvalid()) |
| 427 |
return Res; |
| 428 |
return S.ActOnFinishFullExpr(Res.get(), false); |
| 429 |
}; |
| 430 |
|
| 431 |
ExprResult VariantFuncRef; |
| 432 |
if (Expr *E = Attr.getVariantFuncRef()) { |
| 433 |
// Do not mark function as is used to prevent its emission if this is the |
| 434 |
// only place where it is used. |
| 435 |
EnterExpressionEvaluationContext Unevaluated( |
| 436 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 437 |
VariantFuncRef = Subst(E); |
| 438 |
} |
| 439 |
|
| 440 |
// Copy the template version of the OMPTraitInfo and run substitute on all |
| 441 |
// score and condition expressiosn. |
| 442 |
OMPTraitInfo &TI = S.getASTContext().getNewOMPTraitInfo(); |
| 443 |
TI = *Attr.getTraitInfos(); |
| 444 |
|
| 445 |
// Try to substitute template parameters in score and condition expressions. |
| 446 |
auto SubstScoreOrConditionExpr = [&S, Subst](Expr *&E, bool) { |
| 447 |
if (E) { |
| 448 |
EnterExpressionEvaluationContext Unevaluated( |
| 449 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 450 |
ExprResult ER = Subst(E); |
| 451 |
if (ER.isUsable()) |
| 452 |
E = ER.get(); |
| 453 |
else |
| 454 |
return true; |
| 455 |
} |
| 456 |
return false; |
| 457 |
}; |
| 458 |
if (TI.anyScoreOrCondition(SubstScoreOrConditionExpr)) |
| 459 |
return; |
| 460 |
|
| 461 |
Expr *E = VariantFuncRef.get(); |
| 462 |
|
| 463 |
// Check function/variant ref for `omp declare variant` but not for `omp |
| 464 |
// begin declare variant` (which use implicit attributes). |
| 465 |
std::optional<std::pair<FunctionDecl *, Expr *>> DeclVarData = |
| 466 |
S.checkOpenMPDeclareVariantFunction(S.ConvertDeclToDeclGroup(New), E, TI, |
| 467 |
Attr.appendArgs_size(), |
| 468 |
Attr.getRange()); |
| 469 |
|
| 470 |
if (!DeclVarData) |
| 471 |
return; |
| 472 |
|
| 473 |
E = DeclVarData->second; |
| 474 |
FD = DeclVarData->first; |
| 475 |
|
| 476 |
if (auto *VariantDRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) { |
| 477 |
if (auto *VariantFD = dyn_cast<FunctionDecl>(VariantDRE->getDecl())) { |
| 478 |
if (auto *VariantFTD = VariantFD->getDescribedFunctionTemplate()) { |
| 479 |
if (!VariantFTD->isThisDeclarationADefinition()) |
| 480 |
return; |
| 481 |
Sema::TentativeAnalysisScope Trap(S); |
| 482 |
const TemplateArgumentList *TAL = TemplateArgumentList::CreateCopy( |
| 483 |
S.Context, TemplateArgs.getInnermost()); |
| 484 |
|
| 485 |
auto *SubstFD = S.InstantiateFunctionDeclaration(VariantFTD, TAL, |
| 486 |
New->getLocation()); |
| 487 |
if (!SubstFD) |
| 488 |
return; |
| 489 |
QualType NewType = S.Context.mergeFunctionTypes( |
| 490 |
SubstFD->getType(), FD->getType(), |
| 491 |
/* OfBlockPointer */ false, |
| 492 |
/* Unqualified */ false, /* AllowCXX */ true); |
| 493 |
if (NewType.isNull()) |
| 494 |
return; |
| 495 |
S.InstantiateFunctionDefinition( |
| 496 |
New->getLocation(), SubstFD, /* Recursive */ true, |
| 497 |
/* DefinitionRequired */ false, /* AtEndOfTU */ false); |
| 498 |
SubstFD->setInstantiationIsPending(!SubstFD->isDefined()); |
| 499 |
E = DeclRefExpr::Create(S.Context, NestedNameSpecifierLoc(), |
| 500 |
SourceLocation(), SubstFD, |
| 501 |
/* RefersToEnclosingVariableOrCapture */ false, |
| 502 |
/* NameLoc */ SubstFD->getLocation(), |
| 503 |
SubstFD->getType(), ExprValueKind::VK_PRValue); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
SmallVector<Expr *, 8> NothingExprs; |
| 509 |
SmallVector<Expr *, 8> NeedDevicePtrExprs; |
| 510 |
SmallVector<OMPInteropInfo, 4> AppendArgs; |
| 511 |
|
| 512 |
for (Expr *E : Attr.adjustArgsNothing()) { |
| 513 |
ExprResult ER = Subst(E); |
| 514 |
if (ER.isInvalid()) |
| 515 |
continue; |
| 516 |
NothingExprs.push_back(ER.get()); |
| 517 |
} |
| 518 |
for (Expr *E : Attr.adjustArgsNeedDevicePtr()) { |
| 519 |
ExprResult ER = Subst(E); |
| 520 |
if (ER.isInvalid()) |
| 521 |
continue; |
| 522 |
NeedDevicePtrExprs.push_back(ER.get()); |
| 523 |
} |
| 524 |
for (OMPInteropInfo &II : Attr.appendArgs()) { |
| 525 |
// When prefer_type is implemented for append_args handle them here too. |
| 526 |
AppendArgs.emplace_back(II.IsTarget, II.IsTargetSync); |
| 527 |
} |
| 528 |
|
| 529 |
S.ActOnOpenMPDeclareVariantDirective( |
| 530 |
FD, E, TI, NothingExprs, NeedDevicePtrExprs, AppendArgs, SourceLocation(), |
| 531 |
SourceLocation(), Attr.getRange()); |
| 532 |
} |
| 533 |
|
| 534 |
static void instantiateDependentAMDGPUFlatWorkGroupSizeAttr( |
| 535 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 536 |
const AMDGPUFlatWorkGroupSizeAttr &Attr, Decl *New) { |
| 537 |
// Both min and max expression are constant expressions. |
| 538 |
EnterExpressionEvaluationContext Unevaluated( |
| 539 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 540 |
|
| 541 |
ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); |
| 542 |
if (Result.isInvalid()) |
| 543 |
return; |
| 544 |
Expr *MinExpr = Result.getAs<Expr>(); |
| 545 |
|
| 546 |
Result = S.SubstExpr(Attr.getMax(), TemplateArgs); |
| 547 |
if (Result.isInvalid()) |
| 548 |
return; |
| 549 |
Expr *MaxExpr = Result.getAs<Expr>(); |
| 550 |
|
| 551 |
S.addAMDGPUFlatWorkGroupSizeAttr(New, Attr, MinExpr, MaxExpr); |
| 552 |
} |
| 553 |
|
| 554 |
static ExplicitSpecifier |
| 555 |
instantiateExplicitSpecifier(Sema &S, |
| 556 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 557 |
ExplicitSpecifier ES, FunctionDecl *New) { |
| 558 |
if (!ES.getExpr()) |
| 559 |
return ES; |
| 560 |
Expr *OldCond = ES.getExpr(); |
| 561 |
Expr *Cond = nullptr; |
| 562 |
{ |
| 563 |
EnterExpressionEvaluationContext Unevaluated( |
| 564 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 565 |
ExprResult SubstResult = S.SubstExpr(OldCond, TemplateArgs); |
| 566 |
if (SubstResult.isInvalid()) { |
| 567 |
return ExplicitSpecifier::Invalid(); |
| 568 |
} |
| 569 |
Cond = SubstResult.get(); |
| 570 |
} |
| 571 |
ExplicitSpecifier Result(Cond, ES.getKind()); |
| 572 |
if (!Cond->isTypeDependent()) |
| 573 |
S.tryResolveExplicitSpecifier(Result); |
| 574 |
return Result; |
| 575 |
} |
| 576 |
|
| 577 |
static void instantiateDependentAMDGPUWavesPerEUAttr( |
| 578 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 579 |
const AMDGPUWavesPerEUAttr &Attr, Decl *New) { |
| 580 |
// Both min and max expression are constant expressions. |
| 581 |
EnterExpressionEvaluationContext Unevaluated( |
| 582 |
S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 583 |
|
| 584 |
ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); |
| 585 |
if (Result.isInvalid()) |
| 586 |
return; |
| 587 |
Expr *MinExpr = Result.getAs<Expr>(); |
| 588 |
|
| 589 |
Expr *MaxExpr = nullptr; |
| 590 |
if (auto Max = Attr.getMax()) { |
| 591 |
Result = S.SubstExpr(Max, TemplateArgs); |
| 592 |
if (Result.isInvalid()) |
| 593 |
return; |
| 594 |
MaxExpr = Result.getAs<Expr>(); |
| 595 |
} |
| 596 |
|
| 597 |
S.addAMDGPUWavesPerEUAttr(New, Attr, MinExpr, MaxExpr); |
| 598 |
} |
| 599 |
|
| 600 |
// This doesn't take any template parameters, but we have a custom action that |
| 601 |
// needs to happen when the kernel itself is instantiated. We need to run the |
| 602 |
// ItaniumMangler to mark the names required to name this kernel. |
| 603 |
static void instantiateDependentSYCLKernelAttr( |
| 604 |
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 605 |
const SYCLKernelAttr &Attr, Decl *New) { |
| 606 |
New->addAttr(Attr.clone(S.getASTContext())); |
| 607 |
} |
| 608 |
|
| 609 |
/// Determine whether the attribute A might be relevant to the declaration D. |
| 610 |
/// If not, we can skip instantiating it. The attribute may or may not have |
| 611 |
/// been instantiated yet. |
| 612 |
static bool isRelevantAttr(Sema &S, const Decl *D, const Attr *A) { |
| 613 |
// 'preferred_name' is only relevant to the matching specialization of the |
| 614 |
// template. |
| 615 |
if (const auto *PNA = dyn_cast<PreferredNameAttr>(A)) { |
| 616 |
QualType T = PNA->getTypedefType(); |
| 617 |
const auto *RD = cast<CXXRecordDecl>(D); |
| 618 |
if (!T->isDependentType() && !RD->isDependentContext() && |
| 619 |
!declaresSameEntity(T->getAsCXXRecordDecl(), RD)) |
| 620 |
return false; |
| 621 |
for (const auto *ExistingPNA : D->specific_attrs<PreferredNameAttr>()) |
| 622 |
if (S.Context.hasSameType(ExistingPNA->getTypedefType(), |
| 623 |
PNA->getTypedefType())) |
| 624 |
return false; |
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
if (const auto *BA = dyn_cast<BuiltinAttr>(A)) { |
| 629 |
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 630 |
switch (BA->getID()) { |
| 631 |
case Builtin::BIforward: |
| 632 |
// Do not treat 'std::forward' as a builtin if it takes an rvalue reference |
| 633 |
// type and returns an lvalue reference type. The library implementation |
| 634 |
// will produce an error in this case; don't get in its way. |
| 635 |
if (FD && FD->getNumParams() >= 1 && |
| 636 |
FD->getParamDecl(0)->getType()->isRValueReferenceType() && |
| 637 |
FD->getReturnType()->isLValueReferenceType()) { |
| 638 |
return false; |
| 639 |
} |
| 640 |
[[fallthrough]]; |
| 641 |
case Builtin::BImove: |
| 642 |
case Builtin::BImove_if_noexcept: |
| 643 |
// HACK: Super-old versions of libc++ (3.1 and earlier) provide |
| 644 |
// std::forward and std::move overloads that sometimes return by value |
| 645 |
// instead of by reference when building in C++98 mode. Don't treat such |
| 646 |
// cases as builtins. |
| 647 |
if (FD && !FD->getReturnType()->isReferenceType()) |
| 648 |
return false; |
| 649 |
break; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
return true; |
| 654 |
} |
| 655 |
|
| 656 |
void Sema::InstantiateAttrsForDecl( |
| 657 |
const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl, |
| 658 |
Decl *New, LateInstantiatedAttrVec *LateAttrs, |
| 659 |
LocalInstantiationScope *OuterMostScope) { |
| 660 |
if (NamedDecl *ND = dyn_cast<NamedDecl>(New)) { |
| 661 |
// FIXME: This function is called multiple times for the same template |
| 662 |
// specialization. We should only instantiate attributes that were added |
| 663 |
// since the previous instantiation. |
| 664 |
for (const auto *TmplAttr : Tmpl->attrs()) { |
| 665 |
if (!isRelevantAttr(*this, New, TmplAttr)) |
| 666 |
continue; |
| 667 |
|
| 668 |
// FIXME: If any of the special case versions from InstantiateAttrs become |
| 669 |
// applicable to template declaration, we'll need to add them here. |
| 670 |
CXXThisScopeRAII ThisScope( |
| 671 |
*this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()), |
| 672 |
Qualifiers(), ND->isCXXInstanceMember()); |
| 673 |
|
| 674 |
Attr *NewAttr = sema::instantiateTemplateAttributeForDecl( |
| 675 |
TmplAttr, Context, *this, TemplateArgs); |
| 676 |
if (NewAttr && isRelevantAttr(*this, New, NewAttr)) |
| 677 |
New->addAttr(NewAttr); |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
static Sema::RetainOwnershipKind |
| 683 |
attrToRetainOwnershipKind(const Attr *A) { |
| 684 |
switch (A->getKind()) { |
| 685 |
case clang::attr::CFConsumed: |
| 686 |
return Sema::RetainOwnershipKind::CF; |
| 687 |
case clang::attr::OSConsumed: |
| 688 |
return Sema::RetainOwnershipKind::OS; |
| 689 |
case clang::attr::NSConsumed: |
| 690 |
return Sema::RetainOwnershipKind::NS; |
| 691 |
default: |
| 692 |
llvm_unreachable("Wrong argument supplied"); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
| 697 |
const Decl *Tmpl, Decl *New, |
| 698 |
LateInstantiatedAttrVec *LateAttrs, |
| 699 |
LocalInstantiationScope *OuterMostScope) { |
| 700 |
for (const auto *TmplAttr : Tmpl->attrs()) { |
| 701 |
if (!isRelevantAttr(*this, New, TmplAttr)) |
| 702 |
continue; |
| 703 |
|
| 704 |
// FIXME: This should be generalized to more than just the AlignedAttr. |
| 705 |
const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 706 |
if (Aligned && Aligned->isAlignmentDependent()) { |
| 707 |
instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 708 |
continue; |
| 709 |
} |
| 710 |
|
| 711 |
if (const auto *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr)) { |
| 712 |
instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); |
| 713 |
continue; |
| 714 |
} |
| 715 |
|
| 716 |
if (const auto *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr)) { |
| 717 |
instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); |
| 718 |
continue; |
| 719 |
} |
| 720 |
|
| 721 |
if (const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) { |
| 722 |
instantiateDependentAllocAlignAttr(*this, TemplateArgs, AllocAlign, New); |
| 723 |
continue; |
| 724 |
} |
| 725 |
|
| 726 |
if (const auto *Annotate = dyn_cast<AnnotateAttr>(TmplAttr)) { |
| 727 |
instantiateDependentAnnotationAttr(*this, TemplateArgs, Annotate, New); |
| 728 |
continue; |
| 729 |
} |
| 730 |
|
| 731 |
if (const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) { |
| 732 |
instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
| 733 |
cast<FunctionDecl>(New)); |
| 734 |
continue; |
| 735 |
} |
| 736 |
|
| 737 |
if (const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) { |
| 738 |
instantiateDependentDiagnoseIfAttr(*this, TemplateArgs, DiagnoseIf, Tmpl, |
| 739 |
cast<FunctionDecl>(New)); |
| 740 |
continue; |
| 741 |
} |
| 742 |
|
| 743 |
if (const auto *CUDALaunchBounds = |
| 744 |
dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { |
| 745 |
instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, |
| 746 |
*CUDALaunchBounds, New); |
| 747 |
continue; |
| 748 |
} |
| 749 |
|
| 750 |
if (const auto *Mode = dyn_cast<ModeAttr>(TmplAttr)) { |
| 751 |
instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); |
| 752 |
continue; |
| 753 |
} |
| 754 |
|
| 755 |
if (const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) { |
| 756 |
instantiateOMPDeclareSimdDeclAttr(*this, TemplateArgs, *OMPAttr, New); |
| 757 |
continue; |
| 758 |
} |
| 759 |
|
| 760 |
if (const auto *OMPAttr = dyn_cast<OMPDeclareVariantAttr>(TmplAttr)) { |
| 761 |
instantiateOMPDeclareVariantAttr(*this, TemplateArgs, *OMPAttr, New); |
| 762 |
continue; |
| 763 |
} |
| 764 |
|
| 765 |
if (const auto *AMDGPUFlatWorkGroupSize = |
| 766 |
dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) { |
| 767 |
instantiateDependentAMDGPUFlatWorkGroupSizeAttr( |
| 768 |
*this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New); |
| 769 |
} |
| 770 |
|
| 771 |
if (const auto *AMDGPUFlatWorkGroupSize = |
| 772 |
dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) { |
| 773 |
instantiateDependentAMDGPUWavesPerEUAttr(*this, TemplateArgs, |
| 774 |
*AMDGPUFlatWorkGroupSize, New); |
| 775 |
} |
| 776 |
|
| 777 |
// Existing DLL attribute on the instantiation takes precedence. |
| 778 |
if (TmplAttr->getKind() == attr::DLLExport || |
| 779 |
TmplAttr->getKind() == attr::DLLImport) { |
| 780 |
if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { |
| 781 |
continue; |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
if (const auto *ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { |
| 786 |
AddParameterABIAttr(New, *ABIAttr, ABIAttr->getABI()); |
| 787 |
continue; |
| 788 |
} |
| 789 |
|
| 790 |
if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || |
| 791 |
isa<CFConsumedAttr>(TmplAttr)) { |
| 792 |
AddXConsumedAttr(New, *TmplAttr, attrToRetainOwnershipKind(TmplAttr), |
| 793 |
/*template instantiation=*/true); |
| 794 |
continue; |
| 795 |
} |
| 796 |
|
| 797 |
if (auto *A = dyn_cast<PointerAttr>(TmplAttr)) { |
| 798 |
if (!New->hasAttr<PointerAttr>()) |
| 799 |
New->addAttr(A->clone(Context)); |
| 800 |
continue; |
| 801 |
} |
| 802 |
|
| 803 |
if (auto *A = dyn_cast<OwnerAttr>(TmplAttr)) { |
| 804 |
if (!New->hasAttr<OwnerAttr>()) |
| 805 |
New->addAttr(A->clone(Context)); |
| 806 |
continue; |
| 807 |
} |
| 808 |
|
| 809 |
if (auto *A = dyn_cast<SYCLKernelAttr>(TmplAttr)) { |
| 810 |
instantiateDependentSYCLKernelAttr(*this, TemplateArgs, *A, New); |
| 811 |
continue; |
| 812 |
} |
| 813 |
|
| 814 |
assert(!TmplAttr->isPackExpansion()); |
| 815 |
if (TmplAttr->isLateParsed() && LateAttrs) { |
| 816 |
// Late parsed attributes must be instantiated and attached after the |
| 817 |
// enclosing class has been instantiated. See Sema::InstantiateClass. |
| 818 |
LocalInstantiationScope *Saved = nullptr; |
| 819 |
if (CurrentInstantiationScope) |
| 820 |
Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 821 |
LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 822 |
} else { |
| 823 |
// Allow 'this' within late-parsed attributes. |
| 824 |
auto *ND = cast<NamedDecl>(New); |
| 825 |
auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
| 826 |
CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
| 827 |
ND->isCXXInstanceMember()); |
| 828 |
|
| 829 |
Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 830 |
*this, TemplateArgs); |
| 831 |
if (NewAttr && isRelevantAttr(*this, New, TmplAttr)) |
| 832 |
New->addAttr(NewAttr); |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
/// In the MS ABI, we need to instantiate default arguments of dllexported |
| 838 |
/// default constructors along with the constructor definition. This allows IR |
| 839 |
/// gen to emit a constructor closure which calls the default constructor with |
| 840 |
/// its default arguments. |
| 841 |
void Sema::InstantiateDefaultCtorDefaultArgs(CXXConstructorDecl *Ctor) { |
| 842 |
assert(Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 843 |
Ctor->isDefaultConstructor()); |
| 844 |
unsigned NumParams = Ctor->getNumParams(); |
| 845 |
if (NumParams == 0) |
| 846 |
return; |
| 847 |
DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>(); |
| 848 |
if (!Attr) |
| 849 |
return; |
| 850 |
for (unsigned I = 0; I != NumParams; ++I) { |
| 851 |
(void)CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor, |
| 852 |
Ctor->getParamDecl(I)); |
| 853 |
CleanupVarDeclMarking(); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
/// Get the previous declaration of a declaration for the purposes of template |
| 858 |
/// instantiation. If this finds a previous declaration, then the previous |
| 859 |
/// declaration of the instantiation of D should be an instantiation of the |
| 860 |
/// result of this function. |
| 861 |
template<typename DeclT> |
| 862 |
static DeclT *getPreviousDeclForInstantiation(DeclT *D) { |
| 863 |
DeclT *Result = D->getPreviousDecl(); |
| 864 |
|
| 865 |
// If the declaration is within a class, and the previous declaration was |
| 866 |
// merged from a different definition of that class, then we don't have a |
| 867 |
// previous declaration for the purpose of template instantiation. |
| 868 |
if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && |
| 869 |
D->getLexicalDeclContext() != Result->getLexicalDeclContext()) |
| 870 |
return nullptr; |
| 871 |
|
| 872 |
return Result; |
| 873 |
} |
| 874 |
|
| 875 |
Decl * |
| 876 |
TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 877 |
llvm_unreachable("Translation units cannot be instantiated"); |
| 878 |
} |
| 879 |
|
| 880 |
Decl *TemplateDeclInstantiator::VisitHLSLBufferDecl(HLSLBufferDecl *Decl) { |
| 881 |
llvm_unreachable("HLSL buffer declarations cannot be instantiated"); |
| 882 |
} |
| 883 |
|
| 884 |
Decl * |
| 885 |
TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { |
| 886 |
llvm_unreachable("pragma comment cannot be instantiated"); |
| 887 |
} |
| 888 |
|
| 889 |
Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( |
| 890 |
PragmaDetectMismatchDecl *D) { |
| 891 |
llvm_unreachable("pragma comment cannot be instantiated"); |
| 892 |
} |
| 893 |
|
| 894 |
Decl * |
| 895 |
TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { |
| 896 |
llvm_unreachable("extern \"C\" context cannot be instantiated"); |
| 897 |
} |
| 898 |
|
| 899 |
Decl *TemplateDeclInstantiator::VisitMSGuidDecl(MSGuidDecl *D) { |
| 900 |
llvm_unreachable("GUID declaration cannot be instantiated"); |
| 901 |
} |
| 902 |
|
| 903 |
Decl *TemplateDeclInstantiator::VisitUnnamedGlobalConstantDecl( |
| 904 |
UnnamedGlobalConstantDecl *D) { |
| 905 |
llvm_unreachable("UnnamedGlobalConstantDecl cannot be instantiated"); |
| 906 |
} |
| 907 |
|
| 908 |
Decl *TemplateDeclInstantiator::VisitTemplateParamObjectDecl( |
| 909 |
TemplateParamObjectDecl *D) { |
| 910 |
llvm_unreachable("template parameter objects cannot be instantiated"); |
| 911 |
} |
| 912 |
|
| 913 |
Decl * |
| 914 |
TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 915 |
LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 916 |
D->getIdentifier()); |
| 917 |
Owner->addDecl(Inst); |
| 918 |
return Inst; |
| 919 |
} |
| 920 |
|
| 921 |
Decl * |
| 922 |
TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 923 |
llvm_unreachable("Namespaces cannot be instantiated"); |
| 924 |
} |
| 925 |
|
| 926 |
Decl * |
| 927 |
TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 928 |
NamespaceAliasDecl *Inst |
| 929 |
= NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 930 |
D->getNamespaceLoc(), |
| 931 |
D->getAliasLoc(), |
| 932 |
D->getIdentifier(), |
| 933 |
D->getQualifierLoc(), |
| 934 |
D->getTargetNameLoc(), |
| 935 |
D->getNamespace()); |
| 936 |
Owner->addDecl(Inst); |
| 937 |
return Inst; |
| 938 |
} |
| 939 |
|
| 940 |
Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 941 |
bool IsTypeAlias) { |
| 942 |
bool Invalid = false; |
| 943 |
TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 944 |
if (DI->getType()->isInstantiationDependentType() || |
| 945 |
DI->getType()->isVariablyModifiedType()) { |
| 946 |
DI = SemaRef.SubstType(DI, TemplateArgs, |
| 947 |
D->getLocation(), D->getDeclName()); |
| 948 |
if (!DI) { |
| 949 |
Invalid = true; |
| 950 |
DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
| 951 |
} |
| 952 |
} else { |
| 953 |
SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 954 |
} |
| 955 |
|
| 956 |
// HACK: 2012-10-23 g++ has a bug where it gets the value kind of ?: wrong. |
| 957 |
// libstdc++ relies upon this bug in its implementation of common_type. If we |
| 958 |
// happen to be processing that implementation, fake up the g++ ?: |
| 959 |
// semantics. See LWG issue 2141 for more information on the bug. The bugs |
| 960 |
// are fixed in g++ and libstdc++ 4.9.0 (2014-04-22). |
| 961 |
const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 962 |
CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 963 |
if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 964 |
DT->isReferenceType() && |
| 965 |
RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 966 |
RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 967 |
D->getIdentifier() && D->getIdentifier()->isStr("type") && |
| 968 |
SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc())) |
| 969 |
// Fold it to the (non-reference) type which g++ would have produced. |
| 970 |
DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 971 |
DI->getType().getNonReferenceType()); |
| 972 |
|
| 973 |
// Create the new typedef |
| 974 |
TypedefNameDecl *Typedef; |
| 975 |
if (IsTypeAlias) |
| 976 |
Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 977 |
D->getLocation(), D->getIdentifier(), DI); |
| 978 |
else |
| 979 |
Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 980 |
D->getLocation(), D->getIdentifier(), DI); |
| 981 |
if (Invalid) |
| 982 |
Typedef->setInvalidDecl(); |
| 983 |
|
| 984 |
// If the old typedef was the name for linkage purposes of an anonymous |
| 985 |
// tag decl, re-establish that relationship for the new typedef. |
| 986 |
if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 987 |
TagDecl *oldTag = oldTagType->getDecl(); |
| 988 |
if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
| 989 |
TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
| 990 |
assert(!newTag->hasNameForLinkage()); |
| 991 |
newTag->setTypedefNameForAnonDecl(Typedef); |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { |
| 996 |
NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 997 |
TemplateArgs); |
| 998 |
if (!InstPrev) |
| 999 |
return nullptr; |
| 1000 |
|
| 1001 |
TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 1002 |
|
| 1003 |
// If the typedef types are not identical, reject them. |
| 1004 |
SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 1005 |
|
| 1006 |
Typedef->setPreviousDecl(InstPrevTypedef); |
| 1007 |
} |
| 1008 |
|
| 1009 |
SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
| 1010 |
|
| 1011 |
if (D->getUnderlyingType()->getAs<DependentNameType>()) |
| 1012 |
SemaRef.inferGslPointerAttribute(Typedef); |
| 1013 |
|
| 1014 |
Typedef->setAccess(D->getAccess()); |
| 1015 |
Typedef->setReferenced(D->isReferenced()); |
| 1016 |
|
| 1017 |
return Typedef; |
| 1018 |
} |
| 1019 |
|
| 1020 |
Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 1021 |
Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
| 1022 |
if (Typedef) |
| 1023 |
Owner->addDecl(Typedef); |
| 1024 |
return Typedef; |
| 1025 |
} |
| 1026 |
|
| 1027 |
Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 1028 |
Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
| 1029 |
if (Typedef) |
| 1030 |
Owner->addDecl(Typedef); |
| 1031 |
return Typedef; |
| 1032 |
} |
| 1033 |
|
| 1034 |
Decl * |
| 1035 |
TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 1036 |
// Create a local instantiation scope for this type alias template, which |
| 1037 |
// will contain the instantiations of the template parameters. |
| 1038 |
LocalInstantiationScope Scope(SemaRef); |
| 1039 |
|
| 1040 |
TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1041 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1042 |
if (!InstParams) |
| 1043 |
return nullptr; |
| 1044 |
|
| 1045 |
TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 1046 |
|
| 1047 |
TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
| 1048 |
if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { |
| 1049 |
DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1050 |
if (!Found.empty()) { |
| 1051 |
PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 1056 |
InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 1057 |
if (!AliasInst) |
| 1058 |
return nullptr; |
| 1059 |
|
| 1060 |
TypeAliasTemplateDecl *Inst |
| 1061 |
= TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1062 |
D->getDeclName(), InstParams, AliasInst); |
| 1063 |
AliasInst->setDescribedAliasTemplate(Inst); |
| 1064 |
if (PrevAliasTemplate) |
| 1065 |
Inst->setPreviousDecl(PrevAliasTemplate); |
| 1066 |
|
| 1067 |
Inst->setAccess(D->getAccess()); |
| 1068 |
|
| 1069 |
if (!PrevAliasTemplate) |
| 1070 |
Inst->setInstantiatedFromMemberTemplate(D); |
| 1071 |
|
| 1072 |
Owner->addDecl(Inst); |
| 1073 |
|
| 1074 |
return Inst; |
| 1075 |
} |
| 1076 |
|
| 1077 |
Decl *TemplateDeclInstantiator::VisitBindingDecl(BindingDecl *D) { |
| 1078 |
auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1079 |
D->getIdentifier()); |
| 1080 |
NewBD->setReferenced(D->isReferenced()); |
| 1081 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD); |
| 1082 |
return NewBD; |
| 1083 |
} |
| 1084 |
|
| 1085 |
Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) { |
| 1086 |
// Transform the bindings first. |
| 1087 |
SmallVector<BindingDecl*, 16> NewBindings; |
| 1088 |
for (auto *OldBD : D->bindings()) |
| 1089 |
NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD))); |
| 1090 |
ArrayRef<BindingDecl*> NewBindingArray = NewBindings; |
| 1091 |
|
| 1092 |
auto *NewDD = cast_or_null<DecompositionDecl>( |
| 1093 |
VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray)); |
| 1094 |
|
| 1095 |
if (!NewDD || NewDD->isInvalidDecl()) |
| 1096 |
for (auto *NewBD : NewBindings) |
| 1097 |
NewBD->setInvalidDecl(); |
| 1098 |
|
| 1099 |
return NewDD; |
| 1100 |
} |
| 1101 |
|
| 1102 |
Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
| 1103 |
return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
| 1104 |
} |
| 1105 |
|
| 1106 |
Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
| 1107 |
bool InstantiatingVarTemplate, |
| 1108 |
ArrayRef<BindingDecl*> *Bindings) { |
| 1109 |
|
| 1110 |
// Do substitution on the type of the declaration |
| 1111 |
TypeSourceInfo *DI = SemaRef.SubstType( |
| 1112 |
D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(), |
| 1113 |
D->getDeclName(), /*AllowDeducedTST*/true); |
| 1114 |
if (!DI) |
| 1115 |
return nullptr; |
| 1116 |
|
| 1117 |
if (DI->getType()->isFunctionType()) { |
| 1118 |
SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 1119 |
<< D->isStaticDataMember() << DI->getType(); |
| 1120 |
return nullptr; |
| 1121 |
} |
| 1122 |
|
| 1123 |
DeclContext *DC = Owner; |
| 1124 |
if (D->isLocalExternDecl()) |
| 1125 |
SemaRef.adjustContextForLocalExternDecl(DC); |
| 1126 |
|
| 1127 |
// Build the instantiated declaration. |
| 1128 |
VarDecl *Var; |
| 1129 |
if (Bindings) |
| 1130 |
Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 1131 |
D->getLocation(), DI->getType(), DI, |
| 1132 |
D->getStorageClass(), *Bindings); |
| 1133 |
else |
| 1134 |
Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 1135 |
D->getLocation(), D->getIdentifier(), DI->getType(), |
| 1136 |
DI, D->getStorageClass()); |
| 1137 |
|
| 1138 |
// In ARC, infer 'retaining' for variables of retainable type. |
| 1139 |
if (SemaRef.getLangOpts().ObjCAutoRefCount && |
| 1140 |
SemaRef.inferObjCARCLifetime(Var)) |
| 1141 |
Var->setInvalidDecl(); |
| 1142 |
|
| 1143 |
if (SemaRef.getLangOpts().OpenCL) |
| 1144 |
SemaRef.deduceOpenCLAddressSpace(Var); |
| 1145 |
|
| 1146 |
// Substitute the nested name specifier, if any. |
| 1147 |
if (SubstQualifier(D, Var)) |
| 1148 |
return nullptr; |
| 1149 |
|
| 1150 |
SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
| 1151 |
StartingScope, InstantiatingVarTemplate); |
| 1152 |
if (D->isNRVOVariable() && !Var->isInvalidDecl()) { |
| 1153 |
QualType RT; |
| 1154 |
if (auto *F = dyn_cast<FunctionDecl>(DC)) |
| 1155 |
RT = F->getReturnType(); |
| 1156 |
else if (isa<BlockDecl>(DC)) |
| 1157 |
RT = cast<FunctionType>(SemaRef.getCurBlock()->FunctionType) |
| 1158 |
->getReturnType(); |
| 1159 |
else |
| 1160 |
llvm_unreachable("Unknown context type"); |
| 1161 |
|
| 1162 |
// This is the last chance we have of checking copy elision eligibility |
| 1163 |
// for functions in dependent contexts. The sema actions for building |
| 1164 |
// the return statement during template instantiation will have no effect |
| 1165 |
// regarding copy elision, since NRVO propagation runs on the scope exit |
| 1166 |
// actions, and these are not run on instantiation. |
| 1167 |
// This might run through some VarDecls which were returned from non-taken |
| 1168 |
// 'if constexpr' branches, and these will end up being constructed on the |
| 1169 |
// return slot even if they will never be returned, as a sort of accidental |
| 1170 |
// 'optimization'. Notably, functions with 'auto' return types won't have it |
| 1171 |
// deduced by this point. Coupled with the limitation described |
| 1172 |
// previously, this makes it very hard to support copy elision for these. |
| 1173 |
Sema::NamedReturnInfo Info = SemaRef.getNamedReturnInfo(Var); |
| 1174 |
bool NRVO = SemaRef.getCopyElisionCandidate(Info, RT) != nullptr; |
| 1175 |
Var->setNRVOVariable(NRVO); |
| 1176 |
} |
| 1177 |
|
| 1178 |
Var->setImplicit(D->isImplicit()); |
| 1179 |
|
| 1180 |
if (Var->isStaticLocal()) |
| 1181 |
SemaRef.CheckStaticLocalForDllExport(Var); |
| 1182 |
|
| 1183 |
if (Var->getTLSKind()) |
| 1184 |
SemaRef.CheckThreadLocalForLargeAlignment(Var); |
| 1185 |
|
| 1186 |
return Var; |
| 1187 |
} |
| 1188 |
|
| 1189 |
Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 1190 |
AccessSpecDecl* AD |
| 1191 |
= AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 1192 |
D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 1193 |
Owner->addHiddenDecl(AD); |
| 1194 |
return AD; |
| 1195 |
} |
| 1196 |
|
| 1197 |
Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 1198 |
bool Invalid = false; |
| 1199 |
TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 1200 |
if (DI->getType()->isInstantiationDependentType() || |
| 1201 |
DI->getType()->isVariablyModifiedType()) { |
| 1202 |
DI = SemaRef.SubstType(DI, TemplateArgs, |
| 1203 |
D->getLocation(), D->getDeclName()); |
| 1204 |
if (!DI) { |
| 1205 |
DI = D->getTypeSourceInfo(); |
| 1206 |
Invalid = true; |
| 1207 |
} else if (DI->getType()->isFunctionType()) { |
| 1208 |
// C++ [temp.arg.type]p3: |
| 1209 |
// If a declaration acquires a function type through a type |
| 1210 |
// dependent on a template-parameter and this causes a |
| 1211 |
// declaration that does not use the syntactic form of a |
| 1212 |
// function declarator to have function type, the program is |
| 1213 |
// ill-formed. |
| 1214 |
SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 1215 |
<< DI->getType(); |
| 1216 |
Invalid = true; |
| 1217 |
} |
| 1218 |
} else { |
| 1219 |
SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 1220 |
} |
| 1221 |
|
| 1222 |
Expr *BitWidth = D->getBitWidth(); |
| 1223 |
if (Invalid) |
| 1224 |
BitWidth = nullptr; |
| 1225 |
else if (BitWidth) { |
| 1226 |
// The bit-width expression is a constant expression. |
| 1227 |
EnterExpressionEvaluationContext Unevaluated( |
| 1228 |
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 1229 |
|
| 1230 |
ExprResult InstantiatedBitWidth |
| 1231 |
= SemaRef.SubstExpr(BitWidth, TemplateArgs); |
| 1232 |
if (InstantiatedBitWidth.isInvalid()) { |
| 1233 |
Invalid = true; |
| 1234 |
BitWidth = nullptr; |
| 1235 |
} else |
| 1236 |
BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
| 1237 |
} |
| 1238 |
|
| 1239 |
FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 1240 |
DI->getType(), DI, |
| 1241 |
cast<RecordDecl>(Owner), |
| 1242 |
D->getLocation(), |
| 1243 |
D->isMutable(), |
| 1244 |
BitWidth, |
| 1245 |
D->getInClassInitStyle(), |
| 1246 |
D->getInnerLocStart(), |
| 1247 |
D->getAccess(), |
| 1248 |
nullptr); |
| 1249 |
if (!Field) { |
| 1250 |
cast<Decl>(Owner)->setInvalidDecl(); |
| 1251 |
return nullptr; |
| 1252 |
} |
| 1253 |
|
| 1254 |
SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
| 1255 |
|
| 1256 |
if (Field->hasAttrs()) |
| 1257 |
SemaRef.CheckAlignasUnderalignment(Field); |
| 1258 |
|
| 1259 |
if (Invalid) |
| 1260 |
Field->setInvalidDecl(); |
| 1261 |
|
| 1262 |
if (!Field->getDeclName()) { |
| 1263 |
// Keep track of where this decl came from. |
| 1264 |
SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
| 1265 |
} |
| 1266 |
if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 1267 |
if (Parent->isAnonymousStructOrUnion() && |
| 1268 |
Parent->getRedeclContext()->isFunctionOrMethod()) |
| 1269 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
| 1270 |
} |
| 1271 |
|
| 1272 |
Field->setImplicit(D->isImplicit()); |
| 1273 |
Field->setAccess(D->getAccess()); |
| 1274 |
Owner->addDecl(Field); |
| 1275 |
|
| 1276 |
return Field; |
| 1277 |
} |
| 1278 |
|
| 1279 |
Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 1280 |
bool Invalid = false; |
| 1281 |
TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 1282 |
|
| 1283 |
if (DI->getType()->isVariablyModifiedType()) { |
| 1284 |
SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
| 1285 |
<< D; |
| 1286 |
Invalid = true; |
| 1287 |
} else if (DI->getType()->isInstantiationDependentType()) { |
| 1288 |
DI = SemaRef.SubstType(DI, TemplateArgs, |
| 1289 |
D->getLocation(), D->getDeclName()); |
| 1290 |
if (!DI) { |
| 1291 |
DI = D->getTypeSourceInfo(); |
| 1292 |
Invalid = true; |
| 1293 |
} else if (DI->getType()->isFunctionType()) { |
| 1294 |
// C++ [temp.arg.type]p3: |
| 1295 |
// If a declaration acquires a function type through a type |
| 1296 |
// dependent on a template-parameter and this causes a |
| 1297 |
// declaration that does not use the syntactic form of a |
| 1298 |
// function declarator to have function type, the program is |
| 1299 |
// ill-formed. |
| 1300 |
SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 1301 |
<< DI->getType(); |
| 1302 |
Invalid = true; |
| 1303 |
} |
| 1304 |
} else { |
| 1305 |
SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 1306 |
} |
| 1307 |
|
| 1308 |
MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 1309 |
SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
| 1310 |
DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId()); |
| 1311 |
|
| 1312 |
SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 1313 |
StartingScope); |
| 1314 |
|
| 1315 |
if (Invalid) |
| 1316 |
Property->setInvalidDecl(); |
| 1317 |
|
| 1318 |
Property->setAccess(D->getAccess()); |
| 1319 |
Owner->addDecl(Property); |
| 1320 |
|
| 1321 |
return Property; |
| 1322 |
} |
| 1323 |
|
| 1324 |
Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 1325 |
NamedDecl **NamedChain = |
| 1326 |
new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 1327 |
|
| 1328 |
int i = 0; |
| 1329 |
for (auto *PI : D->chain()) { |
| 1330 |
NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
| 1331 |
TemplateArgs); |
| 1332 |
if (!Next) |
| 1333 |
return nullptr; |
| 1334 |
|
| 1335 |
NamedChain[i++] = Next; |
| 1336 |
} |
| 1337 |
|
| 1338 |
QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
| 1339 |
IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 1340 |
SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, |
| 1341 |
{NamedChain, D->getChainingSize()}); |
| 1342 |
|
| 1343 |
for (const auto *Attr : D->attrs()) |
| 1344 |
IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
| 1345 |
|
| 1346 |
IndirectField->setImplicit(D->isImplicit()); |
| 1347 |
IndirectField->setAccess(D->getAccess()); |
| 1348 |
Owner->addDecl(IndirectField); |
| 1349 |
return IndirectField; |
| 1350 |
} |
| 1351 |
|
| 1352 |
Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
| 1353 |
// Handle friend type expressions by simply substituting template |
| 1354 |
// parameters into the pattern type and checking the result. |
| 1355 |
if (TypeSourceInfo *Ty = D->getFriendType()) { |
| 1356 |
TypeSourceInfo *InstTy; |
| 1357 |
// If this is an unsupported friend, don't bother substituting template |
| 1358 |
// arguments into it. The actual type referred to won't be used by any |
| 1359 |
// parts of Clang, and may not be valid for instantiating. Just use the |
| 1360 |
// same info for the instantiated friend. |
| 1361 |
if (D->isUnsupportedFriend()) { |
| 1362 |
InstTy = Ty; |
| 1363 |
} else { |
| 1364 |
InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 1365 |
D->getLocation(), DeclarationName()); |
| 1366 |
} |
| 1367 |
if (!InstTy) |
| 1368 |
return nullptr; |
| 1369 |
|
| 1370 |
FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getBeginLoc(), |
| 1371 |
D->getFriendLoc(), InstTy); |
| 1372 |
if (!FD) |
| 1373 |
return nullptr; |
| 1374 |
|
| 1375 |
FD->setAccess(AS_public); |
| 1376 |
FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
| 1377 |
Owner->addDecl(FD); |
| 1378 |
return FD; |
| 1379 |
} |
| 1380 |
|
| 1381 |
NamedDecl *ND = D->getFriendDecl(); |
| 1382 |
assert(ND && "friend decl must be a decl or a type!"); |
| 1383 |
|
| 1384 |
// All of the Visit implementations for the various potential friend |
| 1385 |
// declarations have to be carefully written to work for friend |
| 1386 |
// objects, with the most important detail being that the target |
| 1387 |
// decl should almost certainly not be placed in Owner. |
| 1388 |
Decl *NewND = Visit(ND); |
| 1389 |
if (!NewND) return nullptr; |
| 1390 |
|
| 1391 |
FriendDecl *FD = |
| 1392 |
FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1393 |
cast<NamedDecl>(NewND), D->getFriendLoc()); |
| 1394 |
FD->setAccess(AS_public); |
| 1395 |
FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
| 1396 |
Owner->addDecl(FD); |
| 1397 |
return FD; |
| 1398 |
} |
| 1399 |
|
| 1400 |
Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1401 |
Expr *AssertExpr = D->getAssertExpr(); |
| 1402 |
|
| 1403 |
// The expression in a static assertion is a constant expression. |
| 1404 |
EnterExpressionEvaluationContext Unevaluated( |
| 1405 |
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 1406 |
|
| 1407 |
ExprResult InstantiatedAssertExpr |
| 1408 |
= SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
| 1409 |
if (InstantiatedAssertExpr.isInvalid()) |
| 1410 |
return nullptr; |
| 1411 |
|
| 1412 |
return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
| 1413 |
InstantiatedAssertExpr.get(), |
| 1414 |
D->getMessage(), |
| 1415 |
D->getRParenLoc(), |
| 1416 |
D->isFailed()); |
| 1417 |
} |
| 1418 |
|
| 1419 |
Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| 1420 |
EnumDecl *PrevDecl = nullptr; |
| 1421 |
if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
| 1422 |
NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1423 |
PatternPrev, |
| 1424 |
TemplateArgs); |
| 1425 |
if (!Prev) return nullptr; |
| 1426 |
PrevDecl = cast<EnumDecl>(Prev); |
| 1427 |
} |
| 1428 |
|
| 1429 |
EnumDecl *Enum = |
| 1430 |
EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 1431 |
D->getLocation(), D->getIdentifier(), PrevDecl, |
| 1432 |
D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); |
| 1433 |
if (D->isFixed()) { |
| 1434 |
if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
| 1435 |
// If we have type source information for the underlying type, it means it |
| 1436 |
// has been explicitly set by the user. Perform substitution on it before |
| 1437 |
// moving on. |
| 1438 |
SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 1439 |
TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 1440 |
DeclarationName()); |
| 1441 |
if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
| 1442 |
Enum->setIntegerType(SemaRef.Context.IntTy); |
| 1443 |
else |
| 1444 |
Enum->setIntegerTypeSourceInfo(NewTI); |
| 1445 |
} else { |
| 1446 |
assert(!D->getIntegerType()->isDependentType() |
| 1447 |
&& "Dependent type without type source info"); |
| 1448 |
Enum->setIntegerType(D->getIntegerType()); |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 1453 |
|
| 1454 |
Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
| 1455 |
Enum->setAccess(D->getAccess()); |
| 1456 |
// Forward the mangling number from the template to the instantiated decl. |
| 1457 |
SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
| 1458 |
// See if the old tag was defined along with a declarator. |
| 1459 |
// If it did, mark the new tag as being associated with that declarator. |
| 1460 |
if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1461 |
SemaRef.Context.addDeclaratorForUnnamedTagDecl(Enum, DD); |
| 1462 |
// See if the old tag was defined along with a typedef. |
| 1463 |
// If it did, mark the new tag as being associated with that typedef. |
| 1464 |
if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1465 |
SemaRef.Context.addTypedefNameForUnnamedTagDecl(Enum, TND); |
| 1466 |
if (SubstQualifier(D, Enum)) return nullptr; |
| 1467 |
Owner->addDecl(Enum); |
| 1468 |
|
| 1469 |
EnumDecl *Def = D->getDefinition(); |
| 1470 |
if (Def && Def != D) { |
| 1471 |
// If this is an out-of-line definition of an enum member template, check |
| 1472 |
// that the underlying types match in the instantiation of both |
| 1473 |
// declarations. |
| 1474 |
if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 1475 |
SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 1476 |
QualType DefnUnderlying = |
| 1477 |
SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 1478 |
UnderlyingLoc, DeclarationName()); |
| 1479 |
SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
| 1480 |
DefnUnderlying, /*IsFixed=*/true, Enum); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
// C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 1485 |
// specialization causes the implicit instantiation of the declarations, but |
| 1486 |
// not the definitions of scoped member enumerations. |
| 1487 |
// |
| 1488 |
// DR1484 clarifies that enumeration definitions inside of a template |
| 1489 |
// declaration aren't considered entities that can be separately instantiated |
| 1490 |
// from the rest of the entity they are declared inside of. |
| 1491 |
if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 1492 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
| 1493 |
InstantiateEnumDefinition(Enum, Def); |
| 1494 |
} |
| 1495 |
|
| 1496 |
return Enum; |
| 1497 |
} |
| 1498 |
|
| 1499 |
void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 1500 |
EnumDecl *Enum, EnumDecl *Pattern) { |
| 1501 |
Enum->startDefinition(); |
| 1502 |
|
| 1503 |
// Update the location to refer to the definition. |
| 1504 |
Enum->setLocation(Pattern->getLocation()); |
| 1505 |
|
| 1506 |
SmallVector<Decl*, 4> Enumerators; |
| 1507 |
|
| 1508 |
EnumConstantDecl *LastEnumConst = nullptr; |
| 1509 |
for (auto *EC : Pattern->enumerators()) { |
| 1510 |
// The specified value for the enumerator. |
| 1511 |
ExprResult Value((Expr *)nullptr); |
| 1512 |
if (Expr *UninstValue = EC->getInitExpr()) { |
| 1513 |
// The enumerator's value expression is a constant expression. |
| 1514 |
EnterExpressionEvaluationContext Unevaluated( |
| 1515 |
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 1516 |
|
| 1517 |
Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
| 1518 |
} |
| 1519 |
|
| 1520 |
// Drop the initial value and continue. |
| 1521 |
bool isInvalid = false; |
| 1522 |
if (Value.isInvalid()) { |
| 1523 |
Value = nullptr; |
| 1524 |
isInvalid = true; |
| 1525 |
} |
| 1526 |
|
| 1527 |
EnumConstantDecl *EnumConst |
| 1528 |
= SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 1529 |
EC->getLocation(), EC->getIdentifier(), |
| 1530 |
Value.get()); |
| 1531 |
|
| 1532 |
if (isInvalid) { |
| 1533 |
if (EnumConst) |
| 1534 |
EnumConst->setInvalidDecl(); |
| 1535 |
Enum->setInvalidDecl(); |
| 1536 |
} |
| 1537 |
|
| 1538 |
if (EnumConst) { |
| 1539 |
SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
| 1540 |
|
| 1541 |
EnumConst->setAccess(Enum->getAccess()); |
| 1542 |
Enum->addDecl(EnumConst); |
| 1543 |
Enumerators.push_back(EnumConst); |
| 1544 |
LastEnumConst = EnumConst; |
| 1545 |
|
| 1546 |
if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 1547 |
!Enum->isScoped()) { |
| 1548 |
// If the enumeration is within a function or method, record the enum |
| 1549 |
// constant as a local. |
| 1550 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, |
| 1556 |
Enumerators, nullptr, ParsedAttributesView()); |
| 1557 |
} |
| 1558 |
|
| 1559 |
Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 1560 |
llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
| 1561 |
} |
| 1562 |
|
| 1563 |
Decl * |
| 1564 |
TemplateDeclInstantiator::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 1565 |
llvm_unreachable("BuiltinTemplateDecls cannot be instantiated."); |
| 1566 |
} |
| 1567 |
|
| 1568 |
Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 1569 |
bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1570 |
|
| 1571 |
// Create a local instantiation scope for this class template, which |
| 1572 |
// will contain the instantiations of the template parameters. |
| 1573 |
LocalInstantiationScope Scope(SemaRef); |
| 1574 |
TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1575 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1576 |
if (!InstParams) |
| 1577 |
return nullptr; |
| 1578 |
|
| 1579 |
CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
| 1580 |
|
| 1581 |
// Instantiate the qualifier. We have to do this first in case |
| 1582 |
// we're a friend declaration, because if we are then we need to put |
| 1583 |
// the new declaration in the appropriate context. |
| 1584 |
NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 1585 |
if (QualifierLoc) { |
| 1586 |
QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1587 |
TemplateArgs); |
| 1588 |
if (!QualifierLoc) |
| 1589 |
return nullptr; |
| 1590 |
} |
| 1591 |
|
| 1592 |
CXXRecordDecl *PrevDecl = nullptr; |
| 1593 |
ClassTemplateDecl *PrevClassTemplate = nullptr; |
| 1594 |
|
| 1595 |
if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { |
| 1596 |
DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1597 |
if (!Found.empty()) { |
| 1598 |
PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
| 1599 |
if (PrevClassTemplate) |
| 1600 |
PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1601 |
} |
| 1602 |
} |
| 1603 |
|
| 1604 |
// If this isn't a friend, then it's a member template, in which |
| 1605 |
// case we just want to build the instantiation in the |
| 1606 |
// specialization. If it is a friend, we want to build it in |
| 1607 |
// the appropriate context. |
| 1608 |
DeclContext *DC = Owner; |
| 1609 |
if (isFriend) { |
| 1610 |
if (QualifierLoc) { |
| 1611 |
CXXScopeSpec SS; |
| 1612 |
SS.Adopt(QualifierLoc); |
| 1613 |
DC = SemaRef.computeDeclContext(SS); |
| 1614 |
if (!DC) return nullptr; |
| 1615 |
} else { |
| 1616 |
DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 1617 |
Pattern->getDeclContext(), |
| 1618 |
TemplateArgs); |
| 1619 |
} |
| 1620 |
|
| 1621 |
// Look for a previous declaration of the template in the owning |
| 1622 |
// context. |
| 1623 |
LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
| 1624 |
Sema::LookupOrdinaryName, |
| 1625 |
SemaRef.forRedeclarationInCurContext()); |
| 1626 |
SemaRef.LookupQualifiedName(R, DC); |
| 1627 |
|
| 1628 |
if (R.isSingleResult()) { |
| 1629 |
PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 1630 |
if (PrevClassTemplate) |
| 1631 |
PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1632 |
} |
| 1633 |
|
| 1634 |
if (!PrevClassTemplate && QualifierLoc) { |
| 1635 |
SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
| 1636 |
<< D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
| 1637 |
<< QualifierLoc.getSourceRange(); |
| 1638 |
return nullptr; |
| 1639 |
} |
| 1640 |
|
| 1641 |
if (PrevClassTemplate) { |
| 1642 |
const ClassTemplateDecl *MostRecentPrevCT = |
| 1643 |
PrevClassTemplate->getMostRecentDecl(); |
| 1644 |
TemplateParameterList *PrevParams = |
| 1645 |
MostRecentPrevCT->getTemplateParameters(); |
| 1646 |
|
| 1647 |
// Make sure the parameter lists match. |
| 1648 |
if (!SemaRef.TemplateParameterListsAreEqual( |
| 1649 |
D->getTemplatedDecl(), InstParams, |
| 1650 |
MostRecentPrevCT->getTemplatedDecl(), PrevParams, true, |
| 1651 |
Sema::TPL_TemplateMatch)) |
| 1652 |
return nullptr; |
| 1653 |
|
| 1654 |
// Do some additional validation, then merge default arguments |
| 1655 |
// from the existing declarations. |
| 1656 |
if (SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
| 1657 |
Sema::TPC_ClassTemplate)) |
| 1658 |
return nullptr; |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
CXXRecordDecl *RecordInst = CXXRecordDecl::Create( |
| 1663 |
SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(), |
| 1664 |
Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl, |
| 1665 |
/*DelayTypeCreation=*/true); |
| 1666 |
|
| 1667 |
if (QualifierLoc) |
| 1668 |
RecordInst->setQualifierInfo(QualifierLoc); |
| 1669 |
|
| 1670 |
SemaRef.InstantiateAttrsForDecl(TemplateArgs, Pattern, RecordInst, LateAttrs, |
| 1671 |
StartingScope); |
| 1672 |
|
| 1673 |
ClassTemplateDecl *Inst |
| 1674 |
= ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
| 1675 |
D->getIdentifier(), InstParams, RecordInst); |
| 1676 |
assert(!(isFriend && Owner->isDependentContext())); |
| 1677 |
Inst->setPreviousDecl(PrevClassTemplate); |
| 1678 |
|
| 1679 |
RecordInst->setDescribedClassTemplate(Inst); |
| 1680 |
|
| 1681 |
if (isFriend) { |
| 1682 |
if (PrevClassTemplate) |
| 1683 |
Inst->setAccess(PrevClassTemplate->getAccess()); |
| 1684 |
else |
| 1685 |
Inst->setAccess(D->getAccess()); |
| 1686 |
|
| 1687 |
Inst->setObjectOfFriendDecl(); |
| 1688 |
// TODO: do we want to track the instantiation progeny of this |
| 1689 |
// friend target decl? |
| 1690 |
} else { |
| 1691 |
Inst->setAccess(D->getAccess()); |
| 1692 |
if (!PrevClassTemplate) |
| 1693 |
Inst->setInstantiatedFromMemberTemplate(D); |
| 1694 |
} |
| 1695 |
|
| 1696 |
// Trigger creation of the type for the instantiation. |
| 1697 |
SemaRef.Context.getInjectedClassNameType(RecordInst, |
| 1698 |
Inst->getInjectedClassNameSpecialization()); |
| 1699 |
|
| 1700 |
// Finish handling of friends. |
| 1701 |
if (isFriend) { |
| 1702 |
DC->makeDeclVisibleInContext(Inst); |
| 1703 |
Inst->setLexicalDeclContext(Owner); |
| 1704 |
RecordInst->setLexicalDeclContext(Owner); |
| 1705 |
return Inst; |
| 1706 |
} |
| 1707 |
|
| 1708 |
if (D->isOutOfLine()) { |
| 1709 |
Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1710 |
RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1711 |
} |
| 1712 |
|
| 1713 |
Owner->addDecl(Inst); |
| 1714 |
|
| 1715 |
if (!PrevClassTemplate) { |
| 1716 |
// Queue up any out-of-line partial specializations of this member |
| 1717 |
// class template; the client will force their instantiation once |
| 1718 |
// the enclosing class has been instantiated. |
| 1719 |
SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1720 |
D->getPartialSpecializations(PartialSpecs); |
| 1721 |
for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| 1722 |
if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
| 1723 |
OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 1724 |
} |
| 1725 |
|
| 1726 |
return Inst; |
| 1727 |
} |
| 1728 |
|
| 1729 |
Decl * |
| 1730 |
TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 1731 |
ClassTemplatePartialSpecializationDecl *D) { |
| 1732 |
ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 1733 |
|
| 1734 |
// Lookup the already-instantiated declaration in the instantiation |
| 1735 |
// of the class template and return that. |
| 1736 |
DeclContext::lookup_result Found |
| 1737 |
= Owner->lookup(ClassTemplate->getDeclName()); |
| 1738 |
if (Found.empty()) |
| 1739 |
return nullptr; |
| 1740 |
|
| 1741 |
ClassTemplateDecl *InstClassTemplate |
| 1742 |
= dyn_cast<ClassTemplateDecl>(Found.front()); |
| 1743 |
if (!InstClassTemplate) |
| 1744 |
return nullptr; |
| 1745 |
|
| 1746 |
if (ClassTemplatePartialSpecializationDecl *Result |
| 1747 |
= InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1748 |
return Result; |
| 1749 |
|
| 1750 |
return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
| 1751 |
} |
| 1752 |
|
| 1753 |
Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1754 |
assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1755 |
"Only static data member templates are allowed."); |
| 1756 |
|
| 1757 |
// Create a local instantiation scope for this variable template, which |
| 1758 |
// will contain the instantiations of the template parameters. |
| 1759 |
LocalInstantiationScope Scope(SemaRef); |
| 1760 |
TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1761 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1762 |
if (!InstParams) |
| 1763 |
return nullptr; |
| 1764 |
|
| 1765 |
VarDecl *Pattern = D->getTemplatedDecl(); |
| 1766 |
VarTemplateDecl *PrevVarTemplate = nullptr; |
| 1767 |
|
| 1768 |
if (getPreviousDeclForInstantiation(Pattern)) { |
| 1769 |
DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1770 |
if (!Found.empty()) |
| 1771 |
PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1772 |
} |
| 1773 |
|
| 1774 |
VarDecl *VarInst = |
| 1775 |
cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1776 |
/*InstantiatingVarTemplate=*/true)); |
| 1777 |
if (!VarInst) return nullptr; |
| 1778 |
|
| 1779 |
DeclContext *DC = Owner; |
| 1780 |
|
| 1781 |
VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1782 |
SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
| 1783 |
VarInst); |
| 1784 |
VarInst->setDescribedVarTemplate(Inst); |
| 1785 |
Inst->setPreviousDecl(PrevVarTemplate); |
| 1786 |
|
| 1787 |
Inst->setAccess(D->getAccess()); |
| 1788 |
if (!PrevVarTemplate) |
| 1789 |
Inst->setInstantiatedFromMemberTemplate(D); |
| 1790 |
|
| 1791 |
if (D->isOutOfLine()) { |
| 1792 |
Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1793 |
VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1794 |
} |
| 1795 |
|
| 1796 |
Owner->addDecl(Inst); |
| 1797 |
|
| 1798 |
if (!PrevVarTemplate) { |
| 1799 |
// Queue up any out-of-line partial specializations of this member |
| 1800 |
// variable template; the client will force their instantiation once |
| 1801 |
// the enclosing class has been instantiated. |
| 1802 |
SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1803 |
D->getPartialSpecializations(PartialSpecs); |
| 1804 |
for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| 1805 |
if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
| 1806 |
OutOfLineVarPartialSpecs.push_back( |
| 1807 |
std::make_pair(Inst, PartialSpecs[I])); |
| 1808 |
} |
| 1809 |
|
| 1810 |
return Inst; |
| 1811 |
} |
| 1812 |
|
| 1813 |
Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1814 |
VarTemplatePartialSpecializationDecl *D) { |
| 1815 |
assert(D->isStaticDataMember() && |
| 1816 |
"Only static data member templates are allowed."); |
| 1817 |
|
| 1818 |
VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1819 |
|
| 1820 |
// Lookup the already-instantiated declaration and return that. |
| 1821 |
DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1822 |
assert(!Found.empty() && "Instantiation found nothing?"); |
| 1823 |
|
| 1824 |
VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1825 |
assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1826 |
|
| 1827 |
if (VarTemplatePartialSpecializationDecl *Result = |
| 1828 |
InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1829 |
return Result; |
| 1830 |
|
| 1831 |
return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1832 |
} |
| 1833 |
|
| 1834 |
Decl * |
| 1835 |
TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 1836 |
// Create a local instantiation scope for this function template, which |
| 1837 |
// will contain the instantiations of the template parameters and then get |
| 1838 |
// merged with the local instantiation scope for the function template |
| 1839 |
// itself. |
| 1840 |
LocalInstantiationScope Scope(SemaRef); |
| 1841 |
Sema::ConstraintEvalRAII<TemplateDeclInstantiator> RAII(*this); |
| 1842 |
|
| 1843 |
TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1844 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1845 |
if (!InstParams) |
| 1846 |
return nullptr; |
| 1847 |
|
| 1848 |
FunctionDecl *Instantiated = nullptr; |
| 1849 |
if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
| 1850 |
Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
| 1851 |
InstParams)); |
| 1852 |
else |
| 1853 |
Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
| 1854 |
D->getTemplatedDecl(), |
| 1855 |
InstParams)); |
| 1856 |
|
| 1857 |
if (!Instantiated) |
| 1858 |
return nullptr; |
| 1859 |
|
| 1860 |
// Link the instantiated function template declaration to the function |
| 1861 |
// template from which it was instantiated. |
| 1862 |
FunctionTemplateDecl *InstTemplate |
| 1863 |
= Instantiated->getDescribedFunctionTemplate(); |
| 1864 |
InstTemplate->setAccess(D->getAccess()); |
| 1865 |
assert(InstTemplate && |
| 1866 |
"VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
| 1867 |
|
| 1868 |
bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1869 |
|
| 1870 |
// Link the instantiation back to the pattern *unless* this is a |
| 1871 |
// non-definition friend declaration. |
| 1872 |
if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
| 1873 |
!(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
| 1874 |
InstTemplate->setInstantiatedFromMemberTemplate(D); |
| 1875 |
|
| 1876 |
// Make declarations visible in the appropriate context. |
| 1877 |
if (!isFriend) { |
| 1878 |
Owner->addDecl(InstTemplate); |
| 1879 |
} else if (InstTemplate->getDeclContext()->isRecord() && |
| 1880 |
!getPreviousDeclForInstantiation(D)) { |
| 1881 |
SemaRef.CheckFriendAccess(InstTemplate); |
| 1882 |
} |
| 1883 |
|
| 1884 |
return InstTemplate; |
| 1885 |
} |
| 1886 |
|
| 1887 |
Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1888 |
CXXRecordDecl *PrevDecl = nullptr; |
| 1889 |
if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
| 1890 |
NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1891 |
PatternPrev, |
| 1892 |
TemplateArgs); |
| 1893 |
if (!Prev) return nullptr; |
| 1894 |
PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1895 |
} |
| 1896 |
|
| 1897 |
CXXRecordDecl *Record = nullptr; |
| 1898 |
bool IsInjectedClassName = D->isInjectedClassName(); |
| 1899 |
if (D->isLambda()) |
| 1900 |
Record = CXXRecordDecl::CreateLambda( |
| 1901 |
SemaRef.Context, Owner, D->getLambdaTypeInfo(), D->getLocation(), |
| 1902 |
D->getLambdaDependencyKind(), D->isGenericLambda(), |
| 1903 |
D->getLambdaCaptureDefault()); |
| 1904 |
else |
| 1905 |
Record = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
| 1906 |
D->getBeginLoc(), D->getLocation(), |
| 1907 |
D->getIdentifier(), PrevDecl, |
| 1908 |
/*DelayTypeCreation=*/IsInjectedClassName); |
| 1909 |
// Link the type of the injected-class-name to that of the outer class. |
| 1910 |
if (IsInjectedClassName) |
| 1911 |
(void)SemaRef.Context.getTypeDeclType(Record, cast<CXXRecordDecl>(Owner)); |
| 1912 |
|
| 1913 |
// Substitute the nested name specifier, if any. |
| 1914 |
if (SubstQualifier(D, Record)) |
| 1915 |
return nullptr; |
| 1916 |
|
| 1917 |
SemaRef.InstantiateAttrsForDecl(TemplateArgs, D, Record, LateAttrs, |
| 1918 |
StartingScope); |
| 1919 |
|
| 1920 |
Record->setImplicit(D->isImplicit()); |
| 1921 |
// FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1922 |
// the tag decls introduced by friend class declarations don't have an access |
| 1923 |
// specifier. Remove once this area of the code gets sorted out. |
| 1924 |
if (D->getAccess() != AS_none) |
| 1925 |
Record->setAccess(D->getAccess()); |
| 1926 |
if (!IsInjectedClassName) |
| 1927 |
Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 1928 |
|
| 1929 |
// If the original function was part of a friend declaration, |
| 1930 |
// inherit its namespace state. |
| 1931 |
if (D->getFriendObjectKind()) |
| 1932 |
Record->setObjectOfFriendDecl(); |
| 1933 |
|
| 1934 |
// Make sure that anonymous structs and unions are recorded. |
| 1935 |
if (D->isAnonymousStructOrUnion()) |
| 1936 |
Record->setAnonymousStructOrUnion(true); |
| 1937 |
|
| 1938 |
if (D->isLocalClass()) |
| 1939 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
| 1940 |
|
| 1941 |
// Forward the mangling number from the template to the instantiated decl. |
| 1942 |
SemaRef.Context.setManglingNumber(Record, |
| 1943 |
SemaRef.Context.getManglingNumber(D)); |
| 1944 |
|
| 1945 |
// See if the old tag was defined along with a declarator. |
| 1946 |
// If it did, mark the new tag as being associated with that declarator. |
| 1947 |
if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1948 |
SemaRef.Context.addDeclaratorForUnnamedTagDecl(Record, DD); |
| 1949 |
|
| 1950 |
// See if the old tag was defined along with a typedef. |
| 1951 |
// If it did, mark the new tag as being associated with that typedef. |
| 1952 |
if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1953 |
SemaRef.Context.addTypedefNameForUnnamedTagDecl(Record, TND); |
| 1954 |
|
| 1955 |
Owner->addDecl(Record); |
| 1956 |
|
| 1957 |
// DR1484 clarifies that the members of a local class are instantiated as part |
| 1958 |
// of the instantiation of their enclosing entity. |
| 1959 |
if (D->isCompleteDefinition() && D->isLocalClass()) { |
| 1960 |
Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef); |
| 1961 |
|
| 1962 |
SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1963 |
TSK_ImplicitInstantiation, |
| 1964 |
/*Complain=*/true); |
| 1965 |
|
| 1966 |
// For nested local classes, we will instantiate the members when we |
| 1967 |
// reach the end of the outermost (non-nested) local class. |
| 1968 |
if (!D->isCXXClassMember()) |
| 1969 |
SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1970 |
TSK_ImplicitInstantiation); |
| 1971 |
|
| 1972 |
// This class may have local implicit instantiations that need to be |
| 1973 |
// performed within this scope. |
| 1974 |
LocalInstantiations.perform(); |
| 1975 |
} |
| 1976 |
|
| 1977 |
SemaRef.DiagnoseUnusedNestedTypedefs(Record); |
| 1978 |
|
| 1979 |
if (IsInjectedClassName) |
| 1980 |
assert(Record->isInjectedClassName() && "Broken injected-class-name"); |
| 1981 |
|
| 1982 |
return Record; |
| 1983 |
} |
| 1984 |
|
| 1985 |
/// Adjust the given function type for an instantiation of the |
| 1986 |
/// given declaration, to cope with modifications to the function's type that |
| 1987 |
/// aren't reflected in the type-source information. |
| 1988 |
/// |
| 1989 |
/// \param D The declaration we're instantiating. |
| 1990 |
/// \param TInfo The already-instantiated type. |
| 1991 |
static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1992 |
FunctionDecl *D, |
| 1993 |
TypeSourceInfo *TInfo) { |
| 1994 |
const FunctionProtoType *OrigFunc |
| 1995 |
= D->getType()->castAs<FunctionProtoType>(); |
| 1996 |
const FunctionProtoType *NewFunc |
| 1997 |
= TInfo->getType()->castAs<FunctionProtoType>(); |
| 1998 |
if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1999 |
return TInfo->getType(); |
| 2000 |
|
| 2001 |
FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 2002 |
NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
| 2003 |
return Context.getFunctionType(NewFunc->getReturnType(), |
| 2004 |
NewFunc->getParamTypes(), NewEPI); |
| 2005 |
} |
| 2006 |
|
| 2007 |
/// Normal class members are of more specific types and therefore |
| 2008 |
/// don't make it here. This function serves three purposes: |
| 2009 |
/// 1) instantiating function templates |
| 2010 |
/// 2) substituting friend and local function declarations |
| 2011 |
/// 3) substituting deduction guide declarations for nested class templates |
| 2012 |
Decl *TemplateDeclInstantiator::VisitFunctionDecl( |
| 2013 |
FunctionDecl *D, TemplateParameterList *TemplateParams, |
| 2014 |
RewriteKind FunctionRewriteKind) { |
| 2015 |
// Check whether there is already a function template specialization for |
| 2016 |
// this declaration. |
| 2017 |
FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 2018 |
if (FunctionTemplate && !TemplateParams) { |
| 2019 |
ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| 2020 |
|
| 2021 |
void *InsertPos = nullptr; |
| 2022 |
FunctionDecl *SpecFunc |
| 2023 |
= FunctionTemplate->findSpecialization(Innermost, InsertPos); |
| 2024 |
|
| 2025 |
// If we already have a function template specialization, return it. |
| 2026 |
if (SpecFunc) |
| 2027 |
return SpecFunc; |
| 2028 |
} |
| 2029 |
|
| 2030 |
bool isFriend; |
| 2031 |
if (FunctionTemplate) |
| 2032 |
isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 2033 |
else |
| 2034 |
isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 2035 |
|
| 2036 |
bool MergeWithParentScope = (TemplateParams != nullptr) || |
| 2037 |
Owner->isFunctionOrMethod() || |
| 2038 |
!(isa<Decl>(Owner) && |
| 2039 |
cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
| 2040 |
LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
| 2041 |
|
| 2042 |
ExplicitSpecifier InstantiatedExplicitSpecifier; |
| 2043 |
if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
| 2044 |
InstantiatedExplicitSpecifier = instantiateExplicitSpecifier( |
| 2045 |
SemaRef, TemplateArgs, DGuide->getExplicitSpecifier(), DGuide); |
| 2046 |
if (InstantiatedExplicitSpecifier.isInvalid()) |
| 2047 |
return nullptr; |
| 2048 |
} |
| 2049 |
|
| 2050 |
SmallVector<ParmVarDecl *, 4> Params; |
| 2051 |
TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
| 2052 |
if (!TInfo) |
| 2053 |
return nullptr; |
| 2054 |
QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
| 2055 |
|
| 2056 |
if (TemplateParams && TemplateParams->size()) { |
| 2057 |
auto *LastParam = |
| 2058 |
dyn_cast<TemplateTypeParmDecl>(TemplateParams->asArray().back()); |
| 2059 |
if (LastParam && LastParam->isImplicit() && |
| 2060 |
LastParam->hasTypeConstraint()) { |
| 2061 |
// In abbreviated templates, the type-constraints of invented template |
| 2062 |
// type parameters are instantiated with the function type, invalidating |
| 2063 |
// the TemplateParameterList which relied on the template type parameter |
| 2064 |
// not having a type constraint. Recreate the TemplateParameterList with |
| 2065 |
// the updated parameter list. |
| 2066 |
TemplateParams = TemplateParameterList::Create( |
| 2067 |
SemaRef.Context, TemplateParams->getTemplateLoc(), |
| 2068 |
TemplateParams->getLAngleLoc(), TemplateParams->asArray(), |
| 2069 |
TemplateParams->getRAngleLoc(), TemplateParams->getRequiresClause()); |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 2074 |
if (QualifierLoc) { |
| 2075 |
QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2076 |
TemplateArgs); |
| 2077 |
if (!QualifierLoc) |
| 2078 |
return nullptr; |
| 2079 |
} |
| 2080 |
|
| 2081 |
Expr *TrailingRequiresClause = D->getTrailingRequiresClause(); |
| 2082 |
|
| 2083 |
// If we're instantiating a local function declaration, put the result |
| 2084 |
// in the enclosing namespace; otherwise we need to find the instantiated |
| 2085 |
// context. |
| 2086 |
DeclContext *DC; |
| 2087 |
if (D->isLocalExternDecl()) { |
| 2088 |
DC = Owner; |
| 2089 |
SemaRef.adjustContextForLocalExternDecl(DC); |
| 2090 |
} else if (isFriend && QualifierLoc) { |
| 2091 |
CXXScopeSpec SS; |
| 2092 |
SS.Adopt(QualifierLoc); |
| 2093 |
DC = SemaRef.computeDeclContext(SS); |
| 2094 |
if (!DC) return nullptr; |
| 2095 |
} else { |
| 2096 |
DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
| 2097 |
TemplateArgs); |
| 2098 |
} |
| 2099 |
|
| 2100 |
DeclarationNameInfo NameInfo |
| 2101 |
= SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2102 |
|
| 2103 |
if (FunctionRewriteKind != RewriteKind::None) |
| 2104 |
adjustForRewrite(FunctionRewriteKind, D, T, TInfo, NameInfo); |
| 2105 |
|
| 2106 |
FunctionDecl *Function; |
| 2107 |
if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
| 2108 |
Function = CXXDeductionGuideDecl::Create( |
| 2109 |
SemaRef.Context, DC, D->getInnerLocStart(), |
| 2110 |
InstantiatedExplicitSpecifier, NameInfo, T, TInfo, |
| 2111 |
D->getSourceRange().getEnd()); |
| 2112 |
if (DGuide->isCopyDeductionCandidate()) |
| 2113 |
cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate(); |
| 2114 |
Function->setAccess(D->getAccess()); |
| 2115 |
} else { |
| 2116 |
Function = FunctionDecl::Create( |
| 2117 |
SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo, |
| 2118 |
D->getCanonicalDecl()->getStorageClass(), D->UsesFPIntrin(), |
| 2119 |
D->isInlineSpecified(), D->hasWrittenPrototype(), D->getConstexprKind(), |
| 2120 |
TrailingRequiresClause); |
| 2121 |
Function->setFriendConstraintRefersToEnclosingTemplate( |
| 2122 |
D->FriendConstraintRefersToEnclosingTemplate()); |
| 2123 |
Function->setRangeEnd(D->getSourceRange().getEnd()); |
| 2124 |
} |
| 2125 |
|
| 2126 |
if (D->isInlined()) |
| 2127 |
Function->setImplicitlyInline(); |
| 2128 |
|
| 2129 |
if (QualifierLoc) |
| 2130 |
Function->setQualifierInfo(QualifierLoc); |
| 2131 |
|
| 2132 |
if (D->isLocalExternDecl()) |
| 2133 |
Function->setLocalExternDecl(); |
| 2134 |
|
| 2135 |
DeclContext *LexicalDC = Owner; |
| 2136 |
if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
| 2137 |
assert(D->getDeclContext()->isFileContext()); |
| 2138 |
LexicalDC = D->getDeclContext(); |
| 2139 |
} |
| 2140 |
else if (D->isLocalExternDecl()) { |
| 2141 |
LexicalDC = SemaRef.CurContext; |
| 2142 |
} |
| 2143 |
|
| 2144 |
Function->setLexicalDeclContext(LexicalDC); |
| 2145 |
|
| 2146 |
// Attach the parameters |
| 2147 |
for (unsigned P = 0; P < Params.size(); ++P) |
| 2148 |
if (Params[P]) |
| 2149 |
Params[P]->setOwningFunction(Function); |
| 2150 |
Function->setParams(Params); |
| 2151 |
|
| 2152 |
if (TrailingRequiresClause) |
| 2153 |
Function->setTrailingRequiresClause(TrailingRequiresClause); |
| 2154 |
|
| 2155 |
if (TemplateParams) { |
| 2156 |
// Our resulting instantiation is actually a function template, since we |
| 2157 |
// are substituting only the outer template parameters. For example, given |
| 2158 |
// |
| 2159 |
// template<typename T> |
| 2160 |
// struct X { |
| 2161 |
// template<typename U> friend void f(T, U); |
| 2162 |
// }; |
| 2163 |
// |
| 2164 |
// X<int> x; |
| 2165 |
// |
| 2166 |
// We are instantiating the friend function template "f" within X<int>, |
| 2167 |
// which means substituting int for T, but leaving "f" as a friend function |
| 2168 |
// template. |
| 2169 |
// Build the function template itself. |
| 2170 |
FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
| 2171 |
Function->getLocation(), |
| 2172 |
Function->getDeclName(), |
| 2173 |
TemplateParams, Function); |
| 2174 |
Function->setDescribedFunctionTemplate(FunctionTemplate); |
| 2175 |
|
| 2176 |
FunctionTemplate->setLexicalDeclContext(LexicalDC); |
| 2177 |
|
| 2178 |
if (isFriend && D->isThisDeclarationADefinition()) { |
| 2179 |
FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 2180 |
D->getDescribedFunctionTemplate()); |
| 2181 |
} |
| 2182 |
} else if (FunctionTemplate) { |
| 2183 |
// Record this function template specialization. |
| 2184 |
ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| 2185 |
Function->setFunctionTemplateSpecialization(FunctionTemplate, |
| 2186 |
TemplateArgumentList::CreateCopy(SemaRef.Context, |
| 2187 |
Innermost), |
| 2188 |
/*InsertPos=*/nullptr); |
| 2189 |
} else if (isFriend && D->isThisDeclarationADefinition()) { |
| 2190 |
// Do not connect the friend to the template unless it's actually a |
| 2191 |
// definition. We don't want non-template functions to be marked as being |
| 2192 |
// template instantiations. |
| 2193 |
Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
| 2194 |
} else if (!isFriend) { |
| 2195 |
// If this is not a function template, and this is not a friend (that is, |
| 2196 |
// this is a locally declared function), save the instantiation relationship |
| 2197 |
// for the purposes of constraint instantiation. |
| 2198 |
Function->setInstantiatedFromDecl(D); |
| 2199 |
} |
| 2200 |
|
| 2201 |
if (isFriend) { |
| 2202 |
Function->setObjectOfFriendDecl(); |
| 2203 |
if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate()) |
| 2204 |
FT->setObjectOfFriendDecl(); |
| 2205 |
} |
| 2206 |
|
| 2207 |
if (InitFunctionInstantiation(Function, D)) |
| 2208 |
Function->setInvalidDecl(); |
| 2209 |
|
| 2210 |
bool IsExplicitSpecialization = false; |
| 2211 |
|
| 2212 |
LookupResult Previous( |
| 2213 |
SemaRef, Function->getDeclName(), SourceLocation(), |
| 2214 |
D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 2215 |
: Sema::LookupOrdinaryName, |
| 2216 |
D->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 2217 |
: SemaRef.forRedeclarationInCurContext()); |
| 2218 |
|
| 2219 |
if (DependentFunctionTemplateSpecializationInfo *Info |
| 2220 |
= D->getDependentSpecializationInfo()) { |
| 2221 |
assert(isFriend && "non-friend has dependent specialization info?"); |
| 2222 |
|
| 2223 |
// Instantiate the explicit template arguments. |
| 2224 |
TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2225 |
Info->getRAngleLoc()); |
| 2226 |
if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, |
| 2227 |
ExplicitArgs)) |
| 2228 |
return nullptr; |
| 2229 |
|
| 2230 |
// Map the candidate templates to their instantiations. |
| 2231 |
for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 2232 |
Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 2233 |
Info->getTemplate(I), |
| 2234 |
TemplateArgs); |
| 2235 |
if (!Temp) return nullptr; |
| 2236 |
|
| 2237 |
Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 2238 |
} |
| 2239 |
|
| 2240 |
if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 2241 |
&ExplicitArgs, |
| 2242 |
Previous)) |
| 2243 |
Function->setInvalidDecl(); |
| 2244 |
|
| 2245 |
IsExplicitSpecialization = true; |
| 2246 |
} else if (const ASTTemplateArgumentListInfo *Info = |
| 2247 |
D->getTemplateSpecializationArgsAsWritten()) { |
| 2248 |
// The name of this function was written as a template-id. |
| 2249 |
SemaRef.LookupQualifiedName(Previous, DC); |
| 2250 |
|
| 2251 |
// Instantiate the explicit template arguments. |
| 2252 |
TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2253 |
Info->getRAngleLoc()); |
| 2254 |
if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, |
| 2255 |
ExplicitArgs)) |
| 2256 |
return nullptr; |
| 2257 |
|
| 2258 |
if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 2259 |
&ExplicitArgs, |
| 2260 |
Previous)) |
| 2261 |
Function->setInvalidDecl(); |
| 2262 |
|
| 2263 |
IsExplicitSpecialization = true; |
| 2264 |
} else if (TemplateParams || !FunctionTemplate) { |
| 2265 |
// Look only into the namespace where the friend would be declared to |
| 2266 |
// find a previous declaration. This is the innermost enclosing namespace, |
| 2267 |
// as described in ActOnFriendFunctionDecl. |
| 2268 |
SemaRef.LookupQualifiedName(Previous, DC->getRedeclContext()); |
| 2269 |
|
| 2270 |
// In C++, the previous declaration we find might be a tag type |
| 2271 |
// (class or enum). In this case, the new declaration will hide the |
| 2272 |
// tag type. Note that this does not apply if we're declaring a |
| 2273 |
// typedef (C++ [dcl.typedef]p4). |
| 2274 |
if (Previous.isSingleTagDecl()) |
| 2275 |
Previous.clear(); |
| 2276 |
|
| 2277 |
// Filter out previous declarations that don't match the scope. The only |
| 2278 |
// effect this has is to remove declarations found in inline namespaces |
| 2279 |
// for friend declarations with unqualified names. |
| 2280 |
if (isFriend && !QualifierLoc && !FunctionTemplate) { |
| 2281 |
SemaRef.FilterLookupForScope(Previous, DC, /*Scope=*/ nullptr, |
| 2282 |
/*ConsiderLinkage=*/ true, |
| 2283 |
QualifierLoc.hasQualifier()); |
| 2284 |
} |
| 2285 |
} |
| 2286 |
|
| 2287 |
// Per [temp.inst], default arguments in function declarations at local scope |
| 2288 |
// are instantiated along with the enclosing declaration. For example: |
| 2289 |
// |
| 2290 |
// template<typename T> |
| 2291 |
// void ft() { |
| 2292 |
// void f(int = []{ return T::value; }()); |
| 2293 |
// } |
| 2294 |
// template void ft<int>(); // error: type 'int' cannot be used prior |
| 2295 |
// to '::' because it has no members |
| 2296 |
// |
| 2297 |
// The error is issued during instantiation of ft<int>() because substitution |
| 2298 |
// into the default argument fails; the default argument is instantiated even |
| 2299 |
// though it is never used. |
| 2300 |
if (Function->isLocalExternDecl()) { |
| 2301 |
for (ParmVarDecl *PVD : Function->parameters()) { |
| 2302 |
if (!PVD->hasDefaultArg()) |
| 2303 |
continue; |
| 2304 |
if (SemaRef.SubstDefaultArgument(D->getInnerLocStart(), PVD, TemplateArgs)) { |
| 2305 |
// If substitution fails, the default argument is set to a |
| 2306 |
// RecoveryExpr that wraps the uninstantiated default argument so |
| 2307 |
// that downstream diagnostics are omitted. |
| 2308 |
Expr *UninstExpr = PVD->getUninstantiatedDefaultArg(); |
| 2309 |
ExprResult ErrorResult = SemaRef.CreateRecoveryExpr( |
| 2310 |
UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(), |
| 2311 |
{ UninstExpr }, UninstExpr->getType()); |
| 2312 |
if (ErrorResult.isUsable()) |
| 2313 |
PVD->setDefaultArg(ErrorResult.get()); |
| 2314 |
} |
| 2315 |
} |
| 2316 |
} |
| 2317 |
|
| 2318 |
SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
| 2319 |
IsExplicitSpecialization, |
| 2320 |
Function->isThisDeclarationADefinition()); |
| 2321 |
|
| 2322 |
// Check the template parameter list against the previous declaration. The |
| 2323 |
// goal here is to pick up default arguments added since the friend was |
| 2324 |
// declared; we know the template parameter lists match, since otherwise |
| 2325 |
// we would not have picked this template as the previous declaration. |
| 2326 |
if (isFriend && TemplateParams && FunctionTemplate->getPreviousDecl()) { |
| 2327 |
SemaRef.CheckTemplateParameterList( |
| 2328 |
TemplateParams, |
| 2329 |
FunctionTemplate->getPreviousDecl()->getTemplateParameters(), |
| 2330 |
Function->isThisDeclarationADefinition() |
| 2331 |
? Sema::TPC_FriendFunctionTemplateDefinition |
| 2332 |
: Sema::TPC_FriendFunctionTemplate); |
| 2333 |
} |
| 2334 |
|
| 2335 |
// If we're introducing a friend definition after the first use, trigger |
| 2336 |
// instantiation. |
| 2337 |
// FIXME: If this is a friend function template definition, we should check |
| 2338 |
// to see if any specializations have been used. |
| 2339 |
if (isFriend && D->isThisDeclarationADefinition() && Function->isUsed(false)) { |
| 2340 |
if (MemberSpecializationInfo *MSInfo = |
| 2341 |
Function->getMemberSpecializationInfo()) { |
| 2342 |
if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 2343 |
SourceLocation Loc = D->getLocation(); // FIXME |
| 2344 |
MSInfo->setPointOfInstantiation(Loc); |
| 2345 |
SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 2346 |
std::make_pair(Function, Loc)); |
| 2347 |
} |
| 2348 |
} |
| 2349 |
} |
| 2350 |
|
| 2351 |
if (D->isExplicitlyDefaulted()) { |
| 2352 |
if (SubstDefaultedFunction(Function, D)) |
| 2353 |
return nullptr; |
| 2354 |
} |
| 2355 |
if (D->isDeleted()) |
| 2356 |
SemaRef.SetDeclDeleted(Function, D->getLocation()); |
| 2357 |
|
| 2358 |
NamedDecl *PrincipalDecl = |
| 2359 |
(TemplateParams ? cast<NamedDecl>(FunctionTemplate) : Function); |
| 2360 |
|
| 2361 |
// If this declaration lives in a different context from its lexical context, |
| 2362 |
// add it to the corresponding lookup table. |
| 2363 |
if (isFriend || |
| 2364 |
(Function->isLocalExternDecl() && !Function->getPreviousDecl())) |
| 2365 |
DC->makeDeclVisibleInContext(PrincipalDecl); |
| 2366 |
|
| 2367 |
if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 2368 |
PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 2369 |
PrincipalDecl->setNonMemberOperator(); |
| 2370 |
|
| 2371 |
return Function; |
| 2372 |
} |
| 2373 |
|
| 2374 |
Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( |
| 2375 |
CXXMethodDecl *D, TemplateParameterList *TemplateParams, |
| 2376 |
std::optional<const ASTTemplateArgumentListInfo *> |
| 2377 |
ClassScopeSpecializationArgs, |
| 2378 |
RewriteKind FunctionRewriteKind) { |
| 2379 |
FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 2380 |
if (FunctionTemplate && !TemplateParams) { |
| 2381 |
// We are creating a function template specialization from a function |
| 2382 |
// template. Check whether there is already a function template |
| 2383 |
// specialization for this particular set of template arguments. |
| 2384 |
ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| 2385 |
|
| 2386 |
void *InsertPos = nullptr; |
| 2387 |
FunctionDecl *SpecFunc |
| 2388 |
= FunctionTemplate->findSpecialization(Innermost, InsertPos); |
| 2389 |
|
| 2390 |
// If we already have a function template specialization, return it. |
| 2391 |
if (SpecFunc) |
| 2392 |
return SpecFunc; |
| 2393 |
} |
| 2394 |
|
| 2395 |
bool isFriend; |
| 2396 |
if (FunctionTemplate) |
| 2397 |
isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 2398 |
else |
| 2399 |
isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 2400 |
|
| 2401 |
bool MergeWithParentScope = (TemplateParams != nullptr) || |
| 2402 |
!(isa<Decl>(Owner) && |
| 2403 |
cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
| 2404 |
LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
| 2405 |
|
| 2406 |
// Instantiate enclosing template arguments for friends. |
| 2407 |
SmallVector<TemplateParameterList *, 4> TempParamLists; |
| 2408 |
unsigned NumTempParamLists = 0; |
| 2409 |
if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| 2410 |
TempParamLists.resize(NumTempParamLists); |
| 2411 |
for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 2412 |
TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 2413 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2414 |
if (!InstParams) |
| 2415 |
return nullptr; |
| 2416 |
TempParamLists[I] = InstParams; |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
ExplicitSpecifier InstantiatedExplicitSpecifier = |
| 2421 |
instantiateExplicitSpecifier(SemaRef, TemplateArgs, |
| 2422 |
ExplicitSpecifier::getFromDecl(D), D); |
| 2423 |
if (InstantiatedExplicitSpecifier.isInvalid()) |
| 2424 |
return nullptr; |
| 2425 |
|
| 2426 |
// Implicit destructors/constructors created for local classes in |
| 2427 |
// DeclareImplicit* (see SemaDeclCXX.cpp) might not have an associated TSI. |
| 2428 |
// Unfortunately there isn't enough context in those functions to |
| 2429 |
// conditionally populate the TSI without breaking non-template related use |
| 2430 |
// cases. Populate TSIs prior to calling SubstFunctionType to make sure we get |
| 2431 |
// a proper transformation. |
| 2432 |
if (cast<CXXRecordDecl>(D->getParent())->isLambda() && |
| 2433 |
!D->getTypeSourceInfo() && |
| 2434 |
isa<CXXConstructorDecl, CXXDestructorDecl>(D)) { |
| 2435 |
TypeSourceInfo *TSI = |
| 2436 |
SemaRef.Context.getTrivialTypeSourceInfo(D->getType()); |
| 2437 |
D->setTypeSourceInfo(TSI); |
| 2438 |
} |
| 2439 |
|
| 2440 |
SmallVector<ParmVarDecl *, 4> Params; |
| 2441 |
TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
| 2442 |
if (!TInfo) |
| 2443 |
return nullptr; |
| 2444 |
QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
| 2445 |
|
| 2446 |
if (TemplateParams && TemplateParams->size()) { |
| 2447 |
auto *LastParam = |
| 2448 |
dyn_cast<TemplateTypeParmDecl>(TemplateParams->asArray().back()); |
| 2449 |
if (LastParam && LastParam->isImplicit() && |
| 2450 |
LastParam->hasTypeConstraint()) { |
| 2451 |
// In abbreviated templates, the type-constraints of invented template |
| 2452 |
// type parameters are instantiated with the function type, invalidating |
| 2453 |
// the TemplateParameterList which relied on the template type parameter |
| 2454 |
// not having a type constraint. Recreate the TemplateParameterList with |
| 2455 |
// the updated parameter list. |
| 2456 |
TemplateParams = TemplateParameterList::Create( |
| 2457 |
SemaRef.Context, TemplateParams->getTemplateLoc(), |
| 2458 |
TemplateParams->getLAngleLoc(), TemplateParams->asArray(), |
| 2459 |
TemplateParams->getRAngleLoc(), TemplateParams->getRequiresClause()); |
| 2460 |
} |
| 2461 |
} |
| 2462 |
|
| 2463 |
NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 2464 |
if (QualifierLoc) { |
| 2465 |
QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2466 |
TemplateArgs); |
| 2467 |
if (!QualifierLoc) |
| 2468 |
return nullptr; |
| 2469 |
} |
| 2470 |
|
| 2471 |
DeclContext *DC = Owner; |
| 2472 |
if (isFriend) { |
| 2473 |
if (QualifierLoc) { |
| 2474 |
CXXScopeSpec SS; |
| 2475 |
SS.Adopt(QualifierLoc); |
| 2476 |
DC = SemaRef.computeDeclContext(SS); |
| 2477 |
|
| 2478 |
if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
| 2479 |
return nullptr; |
| 2480 |
} else { |
| 2481 |
DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 2482 |
D->getDeclContext(), |
| 2483 |
TemplateArgs); |
| 2484 |
} |
| 2485 |
if (!DC) return nullptr; |
| 2486 |
} |
| 2487 |
|
| 2488 |
CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 2489 |
Expr *TrailingRequiresClause = D->getTrailingRequiresClause(); |
| 2490 |
|
| 2491 |
DeclarationNameInfo NameInfo |
| 2492 |
= SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2493 |
|
| 2494 |
if (FunctionRewriteKind != RewriteKind::None) |
| 2495 |
adjustForRewrite(FunctionRewriteKind, D, T, TInfo, NameInfo); |
| 2496 |
|
| 2497 |
// Build the instantiated method declaration. |
| 2498 |
CXXMethodDecl *Method = nullptr; |
| 2499 |
|
| 2500 |
SourceLocation StartLoc = D->getInnerLocStart(); |
| 2501 |
if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 2502 |
Method = CXXConstructorDecl::Create( |
| 2503 |
SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 2504 |
InstantiatedExplicitSpecifier, Constructor->UsesFPIntrin(), |
| 2505 |
Constructor->isInlineSpecified(), false, |
| 2506 |
Constructor->getConstexprKind(), InheritedConstructor(), |
| 2507 |
TrailingRequiresClause); |
| 2508 |
Method->setRangeEnd(Constructor->getEndLoc()); |
| 2509 |
if (Constructor->isDefaultConstructor() || |
| 2510 |
Constructor->isCopyOrMoveConstructor()) |
| 2511 |
Method->setIneligibleOrNotSelected(true); |
| 2512 |
} else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 2513 |
Method = CXXDestructorDecl::Create( |
| 2514 |
SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 2515 |
Destructor->UsesFPIntrin(), Destructor->isInlineSpecified(), false, |
| 2516 |
Destructor->getConstexprKind(), TrailingRequiresClause); |
| 2517 |
Method->setIneligibleOrNotSelected(true); |
| 2518 |
Method->setRangeEnd(Destructor->getEndLoc()); |
| 2519 |
Method->setDeclName(SemaRef.Context.DeclarationNames.getCXXDestructorName( |
| 2520 |
SemaRef.Context.getCanonicalType( |
| 2521 |
SemaRef.Context.getTypeDeclType(Record)))); |
| 2522 |
} else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| 2523 |
Method = CXXConversionDecl::Create( |
| 2524 |
SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 2525 |
Conversion->UsesFPIntrin(), Conversion->isInlineSpecified(), |
| 2526 |
InstantiatedExplicitSpecifier, Conversion->getConstexprKind(), |
| 2527 |
Conversion->getEndLoc(), TrailingRequiresClause); |
| 2528 |
} else { |
| 2529 |
StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
| 2530 |
Method = CXXMethodDecl::Create( |
| 2531 |
SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, SC, |
| 2532 |
D->UsesFPIntrin(), D->isInlineSpecified(), D->getConstexprKind(), |
| 2533 |
D->getEndLoc(), TrailingRequiresClause); |
| 2534 |
if (D->isMoveAssignmentOperator() || D->isCopyAssignmentOperator()) |
| 2535 |
Method->setIneligibleOrNotSelected(true); |
| 2536 |
} |
| 2537 |
|
| 2538 |
if (D->isInlined()) |
| 2539 |
Method->setImplicitlyInline(); |
| 2540 |
|
| 2541 |
if (QualifierLoc) |
| 2542 |
Method->setQualifierInfo(QualifierLoc); |
| 2543 |
|
| 2544 |
if (TemplateParams) { |
| 2545 |
// Our resulting instantiation is actually a function template, since we |
| 2546 |
// are substituting only the outer template parameters. For example, given |
| 2547 |
// |
| 2548 |
// template<typename T> |
| 2549 |
// struct X { |
| 2550 |
// template<typename U> void f(T, U); |
| 2551 |
// }; |
| 2552 |
// |
| 2553 |
// X<int> x; |
| 2554 |
// |
| 2555 |
// We are instantiating the member template "f" within X<int>, which means |
| 2556 |
// substituting int for T, but leaving "f" as a member function template. |
| 2557 |
// Build the function template itself. |
| 2558 |
FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 2559 |
Method->getLocation(), |
| 2560 |
Method->getDeclName(), |
| 2561 |
TemplateParams, Method); |
| 2562 |
if (isFriend) { |
| 2563 |
FunctionTemplate->setLexicalDeclContext(Owner); |
| 2564 |
FunctionTemplate->setObjectOfFriendDecl(); |
| 2565 |
} else if (D->isOutOfLine()) |
| 2566 |
FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 2567 |
Method->setDescribedFunctionTemplate(FunctionTemplate); |
| 2568 |
} else if (FunctionTemplate) { |
| 2569 |
// Record this function template specialization. |
| 2570 |
ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| 2571 |
Method->setFunctionTemplateSpecialization(FunctionTemplate, |
| 2572 |
TemplateArgumentList::CreateCopy(SemaRef.Context, |
| 2573 |
Innermost), |
| 2574 |
/*InsertPos=*/nullptr); |
| 2575 |
} else if (!isFriend) { |
| 2576 |
// Record that this is an instantiation of a member function. |
| 2577 |
Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
| 2578 |
} |
| 2579 |
|
| 2580 |
// If we are instantiating a member function defined |
| 2581 |
// out-of-line, the instantiation will have the same lexical |
| 2582 |
// context (which will be a namespace scope) as the template. |
| 2583 |
if (isFriend) { |
| 2584 |
if (NumTempParamLists) |
| 2585 |
Method->setTemplateParameterListsInfo( |
| 2586 |
SemaRef.Context, |
| 2587 |
llvm::ArrayRef(TempParamLists.data(), NumTempParamLists)); |
| 2588 |
|
| 2589 |
Method->setLexicalDeclContext(Owner); |
| 2590 |
Method->setObjectOfFriendDecl(); |
| 2591 |
} else if (D->isOutOfLine()) |
| 2592 |
Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 2593 |
|
| 2594 |
// Attach the parameters |
| 2595 |
for (unsigned P = 0; P < Params.size(); ++P) |
| 2596 |
Params[P]->setOwningFunction(Method); |
| 2597 |
Method->setParams(Params); |
| 2598 |
|
| 2599 |
if (InitMethodInstantiation(Method, D)) |
| 2600 |
Method->setInvalidDecl(); |
| 2601 |
|
| 2602 |
LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| 2603 |
Sema::ForExternalRedeclaration); |
| 2604 |
|
| 2605 |
bool IsExplicitSpecialization = false; |
| 2606 |
|
| 2607 |
// If the name of this function was written as a template-id, instantiate |
| 2608 |
// the explicit template arguments. |
| 2609 |
if (DependentFunctionTemplateSpecializationInfo *Info |
| 2610 |
= D->getDependentSpecializationInfo()) { |
| 2611 |
assert(isFriend && "non-friend has dependent specialization info?"); |
| 2612 |
|
| 2613 |
// Instantiate the explicit template arguments. |
| 2614 |
TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2615 |
Info->getRAngleLoc()); |
| 2616 |
if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, |
| 2617 |
ExplicitArgs)) |
| 2618 |
return nullptr; |
| 2619 |
|
| 2620 |
// Map the candidate templates to their instantiations. |
| 2621 |
for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 2622 |
Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 2623 |
Info->getTemplate(I), |
| 2624 |
TemplateArgs); |
| 2625 |
if (!Temp) return nullptr; |
| 2626 |
|
| 2627 |
Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 2628 |
} |
| 2629 |
|
| 2630 |
if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2631 |
&ExplicitArgs, |
| 2632 |
Previous)) |
| 2633 |
Method->setInvalidDecl(); |
| 2634 |
|
| 2635 |
IsExplicitSpecialization = true; |
| 2636 |
} else if (const ASTTemplateArgumentListInfo *Info = |
| 2637 |
ClassScopeSpecializationArgs.value_or( |
| 2638 |
D->getTemplateSpecializationArgsAsWritten())) { |
| 2639 |
SemaRef.LookupQualifiedName(Previous, DC); |
| 2640 |
|
| 2641 |
TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2642 |
Info->getRAngleLoc()); |
| 2643 |
if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, |
| 2644 |
ExplicitArgs)) |
| 2645 |
return nullptr; |
| 2646 |
|
| 2647 |
if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2648 |
&ExplicitArgs, |
| 2649 |
Previous)) |
| 2650 |
Method->setInvalidDecl(); |
| 2651 |
|
| 2652 |
IsExplicitSpecialization = true; |
| 2653 |
} else if (ClassScopeSpecializationArgs) { |
| 2654 |
// Class-scope explicit specialization written without explicit template |
| 2655 |
// arguments. |
| 2656 |
SemaRef.LookupQualifiedName(Previous, DC); |
| 2657 |
if (SemaRef.CheckFunctionTemplateSpecialization(Method, nullptr, Previous)) |
| 2658 |
Method->setInvalidDecl(); |
| 2659 |
|
| 2660 |
IsExplicitSpecialization = true; |
| 2661 |
} else if (!FunctionTemplate || TemplateParams || isFriend) { |
| 2662 |
SemaRef.LookupQualifiedName(Previous, Record); |
| 2663 |
|
| 2664 |
// In C++, the previous declaration we find might be a tag type |
| 2665 |
// (class or enum). In this case, the new declaration will hide the |
| 2666 |
// tag type. Note that this does not apply if we're declaring a |
| 2667 |
// typedef (C++ [dcl.typedef]p4). |
| 2668 |
if (Previous.isSingleTagDecl()) |
| 2669 |
Previous.clear(); |
| 2670 |
} |
| 2671 |
|
| 2672 |
// Per [temp.inst], default arguments in member functions of local classes |
| 2673 |
// are instantiated along with the member function declaration. For example: |
| 2674 |
// |
| 2675 |
// template<typename T> |
| 2676 |
// void ft() { |
| 2677 |
// struct lc { |
| 2678 |
// int operator()(int p = []{ return T::value; }()); |
| 2679 |
// }; |
| 2680 |
// } |
| 2681 |
// template void ft<int>(); // error: type 'int' cannot be used prior |
| 2682 |
// to '::'because it has no members |
| 2683 |
// |
| 2684 |
// The error is issued during instantiation of ft<int>()::lc::operator() |
| 2685 |
// because substitution into the default argument fails; the default argument |
| 2686 |
// is instantiated even though it is never used. |
| 2687 |
if (D->isInLocalScopeForInstantiation()) { |
| 2688 |
for (unsigned P = 0; P < Params.size(); ++P) { |
| 2689 |
if (!Params[P]->hasDefaultArg()) |
| 2690 |
continue; |
| 2691 |
if (SemaRef.SubstDefaultArgument(StartLoc, Params[P], TemplateArgs)) { |
| 2692 |
// If substitution fails, the default argument is set to a |
| 2693 |
// RecoveryExpr that wraps the uninstantiated default argument so |
| 2694 |
// that downstream diagnostics are omitted. |
| 2695 |
Expr *UninstExpr = Params[P]->getUninstantiatedDefaultArg(); |
| 2696 |
ExprResult ErrorResult = SemaRef.CreateRecoveryExpr( |
| 2697 |
UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(), |
| 2698 |
{ UninstExpr }, UninstExpr->getType()); |
| 2699 |
if (ErrorResult.isUsable()) |
| 2700 |
Params[P]->setDefaultArg(ErrorResult.get()); |
| 2701 |
} |
| 2702 |
} |
| 2703 |
} |
| 2704 |
|
| 2705 |
SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, |
| 2706 |
IsExplicitSpecialization, |
| 2707 |
Method->isThisDeclarationADefinition()); |
| 2708 |
|
| 2709 |
if (D->isPure()) |
| 2710 |
SemaRef.CheckPureMethod(Method, SourceRange()); |
| 2711 |
|
| 2712 |
// Propagate access. For a non-friend declaration, the access is |
| 2713 |
// whatever we're propagating from. For a friend, it should be the |
| 2714 |
// previous declaration we just found. |
| 2715 |
if (isFriend && Method->getPreviousDecl()) |
| 2716 |
Method->setAccess(Method->getPreviousDecl()->getAccess()); |
| 2717 |
else |
| 2718 |
Method->setAccess(D->getAccess()); |
| 2719 |
if (FunctionTemplate) |
| 2720 |
FunctionTemplate->setAccess(Method->getAccess()); |
| 2721 |
|
| 2722 |
SemaRef.CheckOverrideControl(Method); |
| 2723 |
|
| 2724 |
// If a function is defined as defaulted or deleted, mark it as such now. |
| 2725 |
if (D->isExplicitlyDefaulted()) { |
| 2726 |
if (SubstDefaultedFunction(Method, D)) |
| 2727 |
return nullptr; |
| 2728 |
} |
| 2729 |
if (D->isDeletedAsWritten()) |
| 2730 |
SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
| 2731 |
|
| 2732 |
// If this is an explicit specialization, mark the implicitly-instantiated |
| 2733 |
// template specialization as being an explicit specialization too. |
| 2734 |
// FIXME: Is this necessary? |
| 2735 |
if (IsExplicitSpecialization && !isFriend) |
| 2736 |
SemaRef.CompleteMemberSpecialization(Method, Previous); |
| 2737 |
|
| 2738 |
// If there's a function template, let our caller handle it. |
| 2739 |
if (FunctionTemplate) { |
| 2740 |
// do nothing |
| 2741 |
|
| 2742 |
// Don't hide a (potentially) valid declaration with an invalid one. |
| 2743 |
} else if (Method->isInvalidDecl() && !Previous.empty()) { |
| 2744 |
// do nothing |
| 2745 |
|
| 2746 |
// Otherwise, check access to friends and make them visible. |
| 2747 |
} else if (isFriend) { |
| 2748 |
// We only need to re-check access for methods which we didn't |
| 2749 |
// manage to match during parsing. |
| 2750 |
if (!D->getPreviousDecl()) |
| 2751 |
SemaRef.CheckFriendAccess(Method); |
| 2752 |
|
| 2753 |
Record->makeDeclVisibleInContext(Method); |
| 2754 |
|
| 2755 |
// Otherwise, add the declaration. We don't need to do this for |
| 2756 |
// class-scope specializations because we'll have matched them with |
| 2757 |
// the appropriate template. |
| 2758 |
} else { |
| 2759 |
Owner->addDecl(Method); |
| 2760 |
} |
| 2761 |
|
| 2762 |
// PR17480: Honor the used attribute to instantiate member function |
| 2763 |
// definitions |
| 2764 |
if (Method->hasAttr<UsedAttr>()) { |
| 2765 |
if (const auto *A = dyn_cast<CXXRecordDecl>(Owner)) { |
| 2766 |
SourceLocation Loc; |
| 2767 |
if (const MemberSpecializationInfo *MSInfo = |
| 2768 |
A->getMemberSpecializationInfo()) |
| 2769 |
Loc = MSInfo->getPointOfInstantiation(); |
| 2770 |
else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A)) |
| 2771 |
Loc = Spec->getPointOfInstantiation(); |
| 2772 |
SemaRef.MarkFunctionReferenced(Loc, Method); |
| 2773 |
} |
| 2774 |
} |
| 2775 |
|
| 2776 |
return Method; |
| 2777 |
} |
| 2778 |
|
| 2779 |
Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 2780 |
return VisitCXXMethodDecl(D); |
| 2781 |
} |
| 2782 |
|
| 2783 |
Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 2784 |
return VisitCXXMethodDecl(D); |
| 2785 |
} |
| 2786 |
|
| 2787 |
Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 2788 |
return VisitCXXMethodDecl(D); |
| 2789 |
} |
| 2790 |
|
| 2791 |
Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
| 2792 |
return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, |
| 2793 |
std::nullopt, |
| 2794 |
/*ExpectParameterPack=*/false); |
| 2795 |
} |
| 2796 |
|
| 2797 |
Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 2798 |
TemplateTypeParmDecl *D) { |
| 2799 |
assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
| 2800 |
|
| 2801 |
std::optional<unsigned> NumExpanded; |
| 2802 |
|
| 2803 |
if (const TypeConstraint *TC = D->getTypeConstraint()) { |
| 2804 |
if (D->isPackExpansion() && !D->isExpandedParameterPack()) { |
| 2805 |
assert(TC->getTemplateArgsAsWritten() && |
| 2806 |
"type parameter can only be an expansion when explicit arguments " |
| 2807 |
"are specified"); |
| 2808 |
// The template type parameter pack's type is a pack expansion of types. |
| 2809 |
// Determine whether we need to expand this parameter pack into separate |
| 2810 |
// types. |
| 2811 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2812 |
for (auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments()) |
| 2813 |
SemaRef.collectUnexpandedParameterPacks(ArgLoc, Unexpanded); |
| 2814 |
|
| 2815 |
// Determine whether the set of unexpanded parameter packs can and should |
| 2816 |
// be expanded. |
| 2817 |
bool Expand = true; |
| 2818 |
bool RetainExpansion = false; |
| 2819 |
if (SemaRef.CheckParameterPacksForExpansion( |
| 2820 |
cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint()) |
| 2821 |
->getEllipsisLoc(), |
| 2822 |
SourceRange(TC->getConceptNameLoc(), |
| 2823 |
TC->hasExplicitTemplateArgs() ? |
| 2824 |
TC->getTemplateArgsAsWritten()->getRAngleLoc() : |
| 2825 |
TC->getConceptNameInfo().getEndLoc()), |
| 2826 |
Unexpanded, TemplateArgs, Expand, RetainExpansion, NumExpanded)) |
| 2827 |
return nullptr; |
| 2828 |
} |
| 2829 |
} |
| 2830 |
|
| 2831 |
TemplateTypeParmDecl *Inst = TemplateTypeParmDecl::Create( |
| 2832 |
SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(), |
| 2833 |
D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), |
| 2834 |
D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack(), |
| 2835 |
D->hasTypeConstraint(), NumExpanded); |
| 2836 |
|
| 2837 |
Inst->setAccess(AS_public); |
| 2838 |
Inst->setImplicit(D->isImplicit()); |
| 2839 |
if (auto *TC = D->getTypeConstraint()) { |
| 2840 |
if (!D->isImplicit()) { |
| 2841 |
// Invented template parameter type constraints will be instantiated |
| 2842 |
// with the corresponding auto-typed parameter as it might reference |
| 2843 |
// other parameters. |
| 2844 |
if (SemaRef.SubstTypeConstraint(Inst, TC, TemplateArgs, |
| 2845 |
EvaluateConstraints)) |
| 2846 |
return nullptr; |
| 2847 |
} |
| 2848 |
} |
| 2849 |
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
| 2850 |
TypeSourceInfo *InstantiatedDefaultArg = |
| 2851 |
SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 2852 |
D->getDefaultArgumentLoc(), D->getDeclName()); |
| 2853 |
if (InstantiatedDefaultArg) |
| 2854 |
Inst->setDefaultArgument(InstantiatedDefaultArg); |
| 2855 |
} |
| 2856 |
|
| 2857 |
// Introduce this template parameter's instantiation into the instantiation |
| 2858 |
// scope. |
| 2859 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 2860 |
|
| 2861 |
return Inst; |
| 2862 |
} |
| 2863 |
|
| 2864 |
Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 2865 |
NonTypeTemplateParmDecl *D) { |
| 2866 |
// Substitute into the type of the non-type template parameter. |
| 2867 |
TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
| 2868 |
SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 2869 |
SmallVector<QualType, 4> ExpandedParameterPackTypes; |
| 2870 |
bool IsExpandedParameterPack = false; |
| 2871 |
TypeSourceInfo *DI; |
| 2872 |
QualType T; |
| 2873 |
bool Invalid = false; |
| 2874 |
|
| 2875 |
if (D->isExpandedParameterPack()) { |
| 2876 |
// The non-type template parameter pack is an already-expanded pack |
| 2877 |
// expansion of types. Substitute into each of the expanded types. |
| 2878 |
ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 2879 |
ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 2880 |
for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
| 2881 |
TypeSourceInfo *NewDI = |
| 2882 |
SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs, |
| 2883 |
D->getLocation(), D->getDeclName()); |
| 2884 |
if (!NewDI) |
| 2885 |
return nullptr; |
| 2886 |
|
| 2887 |
QualType NewT = |
| 2888 |
SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
| 2889 |
if (NewT.isNull()) |
| 2890 |
return nullptr; |
| 2891 |
|
| 2892 |
ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 2893 |
ExpandedParameterPackTypes.push_back(NewT); |
| 2894 |
} |
| 2895 |
|
| 2896 |
IsExpandedParameterPack = true; |
| 2897 |
DI = D->getTypeSourceInfo(); |
| 2898 |
T = DI->getType(); |
| 2899 |
} else if (D->isPackExpansion()) { |
| 2900 |
// The non-type template parameter pack's type is a pack expansion of types. |
| 2901 |
// Determine whether we need to expand this parameter pack into separate |
| 2902 |
// types. |
| 2903 |
PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
| 2904 |
TypeLoc Pattern = Expansion.getPatternLoc(); |
| 2905 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2906 |
SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 2907 |
|
| 2908 |
// Determine whether the set of unexpanded parameter packs can and should |
| 2909 |
// be expanded. |
| 2910 |
bool Expand = true; |
| 2911 |
bool RetainExpansion = false; |
| 2912 |
std::optional<unsigned> OrigNumExpansions = |
| 2913 |
Expansion.getTypePtr()->getNumExpansions(); |
| 2914 |
std::optional<unsigned> NumExpansions = OrigNumExpansions; |
| 2915 |
if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 2916 |
Pattern.getSourceRange(), |
| 2917 |
Unexpanded, |
| 2918 |
TemplateArgs, |
| 2919 |
Expand, RetainExpansion, |
| 2920 |
NumExpansions)) |
| 2921 |
return nullptr; |
| 2922 |
|
| 2923 |
if (Expand) { |
| 2924 |
for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2925 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2926 |
TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
| 2927 |
D->getLocation(), |
| 2928 |
D->getDeclName()); |
| 2929 |
if (!NewDI) |
| 2930 |
return nullptr; |
| 2931 |
|
| 2932 |
QualType NewT = |
| 2933 |
SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
| 2934 |
if (NewT.isNull()) |
| 2935 |
return nullptr; |
| 2936 |
|
| 2937 |
ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 2938 |
ExpandedParameterPackTypes.push_back(NewT); |
| 2939 |
} |
| 2940 |
|
| 2941 |
// Note that we have an expanded parameter pack. The "type" of this |
| 2942 |
// expanded parameter pack is the original expansion type, but callers |
| 2943 |
// will end up using the expanded parameter pack types for type-checking. |
| 2944 |
IsExpandedParameterPack = true; |
| 2945 |
DI = D->getTypeSourceInfo(); |
| 2946 |
T = DI->getType(); |
| 2947 |
} else { |
| 2948 |
// We cannot fully expand the pack expansion now, so substitute into the |
| 2949 |
// pattern and create a new pack expansion type. |
| 2950 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2951 |
TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
| 2952 |
D->getLocation(), |
| 2953 |
D->getDeclName()); |
| 2954 |
if (!NewPattern) |
| 2955 |
return nullptr; |
| 2956 |
|
| 2957 |
SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation()); |
| 2958 |
DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 2959 |
NumExpansions); |
| 2960 |
if (!DI) |
| 2961 |
return nullptr; |
| 2962 |
|
| 2963 |
T = DI->getType(); |
| 2964 |
} |
| 2965 |
} else { |
| 2966 |
// Simple case: substitution into a parameter that is not a parameter pack. |
| 2967 |
DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 2968 |
D->getLocation(), D->getDeclName()); |
| 2969 |
if (!DI) |
| 2970 |
return nullptr; |
| 2971 |
|
| 2972 |
// Check that this type is acceptable for a non-type template parameter. |
| 2973 |
T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation()); |
| 2974 |
if (T.isNull()) { |
| 2975 |
T = SemaRef.Context.IntTy; |
| 2976 |
Invalid = true; |
| 2977 |
} |
| 2978 |
} |
| 2979 |
|
| 2980 |
NonTypeTemplateParmDecl *Param; |
| 2981 |
if (IsExpandedParameterPack) |
| 2982 |
Param = NonTypeTemplateParmDecl::Create( |
| 2983 |
SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2984 |
D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2985 |
D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes, |
| 2986 |
ExpandedParameterPackTypesAsWritten); |
| 2987 |
else |
| 2988 |
Param = NonTypeTemplateParmDecl::Create( |
| 2989 |
SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2990 |
D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2991 |
D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); |
| 2992 |
|
| 2993 |
if (AutoTypeLoc AutoLoc = DI->getTypeLoc().getContainedAutoTypeLoc()) |
| 2994 |
if (AutoLoc.isConstrained()) |
| 2995 |
if (SemaRef.AttachTypeConstraint( |
| 2996 |
AutoLoc, Param, |
| 2997 |
IsExpandedParameterPack |
| 2998 |
? DI->getTypeLoc().getAs<PackExpansionTypeLoc>() |
| 2999 |
.getEllipsisLoc() |
| 3000 |
: SourceLocation())) |
| 3001 |
Invalid = true; |
| 3002 |
|
| 3003 |
Param->setAccess(AS_public); |
| 3004 |
Param->setImplicit(D->isImplicit()); |
| 3005 |
if (Invalid) |
| 3006 |
Param->setInvalidDecl(); |
| 3007 |
|
| 3008 |
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
| 3009 |
EnterExpressionEvaluationContext ConstantEvaluated( |
| 3010 |
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 3011 |
ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 3012 |
if (!Value.isInvalid()) |
| 3013 |
Param->setDefaultArgument(Value.get()); |
| 3014 |
} |
| 3015 |
|
| 3016 |
// Introduce this template parameter's instantiation into the instantiation |
| 3017 |
// scope. |
| 3018 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| 3019 |
return Param; |
| 3020 |
} |
| 3021 |
|
| 3022 |
static void collectUnexpandedParameterPacks( |
| 3023 |
Sema &S, |
| 3024 |
TemplateParameterList *Params, |
| 3025 |
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 3026 |
for (const auto &P : *Params) { |
| 3027 |
if (P->isTemplateParameterPack()) |
| 3028 |
continue; |
| 3029 |
if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
| 3030 |
S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 3031 |
Unexpanded); |
| 3032 |
if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 3033 |
collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 3034 |
Unexpanded); |
| 3035 |
} |
| 3036 |
} |
| 3037 |
|
| 3038 |
Decl * |
| 3039 |
TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 3040 |
TemplateTemplateParmDecl *D) { |
| 3041 |
// Instantiate the template parameter list of the template template parameter. |
| 3042 |
TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 3043 |
TemplateParameterList *InstParams; |
| 3044 |
SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 3045 |
|
| 3046 |
bool IsExpandedParameterPack = false; |
| 3047 |
|
| 3048 |
if (D->isExpandedParameterPack()) { |
| 3049 |
// The template template parameter pack is an already-expanded pack |
| 3050 |
// expansion of template parameters. Substitute into each of the expanded |
| 3051 |
// parameters. |
| 3052 |
ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 3053 |
for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 3054 |
I != N; ++I) { |
| 3055 |
LocalInstantiationScope Scope(SemaRef); |
| 3056 |
TemplateParameterList *Expansion = |
| 3057 |
SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 3058 |
if (!Expansion) |
| 3059 |
return nullptr; |
| 3060 |
ExpandedParams.push_back(Expansion); |
| 3061 |
} |
| 3062 |
|
| 3063 |
IsExpandedParameterPack = true; |
| 3064 |
InstParams = TempParams; |
| 3065 |
} else if (D->isPackExpansion()) { |
| 3066 |
// The template template parameter pack expands to a pack of template |
| 3067 |
// template parameters. Determine whether we need to expand this parameter |
| 3068 |
// pack into separate parameters. |
| 3069 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 3070 |
collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 3071 |
Unexpanded); |
| 3072 |
|
| 3073 |
// Determine whether the set of unexpanded parameter packs can and should |
| 3074 |
// be expanded. |
| 3075 |
bool Expand = true; |
| 3076 |
bool RetainExpansion = false; |
| 3077 |
std::optional<unsigned> NumExpansions; |
| 3078 |
if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 3079 |
TempParams->getSourceRange(), |
| 3080 |
Unexpanded, |
| 3081 |
TemplateArgs, |
| 3082 |
Expand, RetainExpansion, |
| 3083 |
NumExpansions)) |
| 3084 |
return nullptr; |
| 3085 |
|
| 3086 |
if (Expand) { |
| 3087 |
for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 3088 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 3089 |
LocalInstantiationScope Scope(SemaRef); |
| 3090 |
TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 3091 |
if (!Expansion) |
| 3092 |
return nullptr; |
| 3093 |
ExpandedParams.push_back(Expansion); |
| 3094 |
} |
| 3095 |
|
| 3096 |
// Note that we have an expanded parameter pack. The "type" of this |
| 3097 |
// expanded parameter pack is the original expansion type, but callers |
| 3098 |
// will end up using the expanded parameter pack types for type-checking. |
| 3099 |
IsExpandedParameterPack = true; |
| 3100 |
InstParams = TempParams; |
| 3101 |
} else { |
| 3102 |
// We cannot fully expand the pack expansion now, so just substitute |
| 3103 |
// into the pattern. |
| 3104 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 3105 |
|
| 3106 |
LocalInstantiationScope Scope(SemaRef); |
| 3107 |
InstParams = SubstTemplateParams(TempParams); |
| 3108 |
if (!InstParams) |
| 3109 |
return nullptr; |
| 3110 |
} |
| 3111 |
} else { |
| 3112 |
// Perform the actual substitution of template parameters within a new, |
| 3113 |
// local instantiation scope. |
| 3114 |
LocalInstantiationScope Scope(SemaRef); |
| 3115 |
InstParams = SubstTemplateParams(TempParams); |
| 3116 |
if (!InstParams) |
| 3117 |
return nullptr; |
| 3118 |
} |
| 3119 |
|
| 3120 |
// Build the template template parameter. |
| 3121 |
TemplateTemplateParmDecl *Param; |
| 3122 |
if (IsExpandedParameterPack) |
| 3123 |
Param = TemplateTemplateParmDecl::Create( |
| 3124 |
SemaRef.Context, Owner, D->getLocation(), |
| 3125 |
D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 3126 |
D->getPosition(), D->getIdentifier(), InstParams, ExpandedParams); |
| 3127 |
else |
| 3128 |
Param = TemplateTemplateParmDecl::Create( |
| 3129 |
SemaRef.Context, Owner, D->getLocation(), |
| 3130 |
D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 3131 |
D->getPosition(), D->isParameterPack(), D->getIdentifier(), InstParams); |
| 3132 |
if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
| 3133 |
NestedNameSpecifierLoc QualifierLoc = |
| 3134 |
D->getDefaultArgument().getTemplateQualifierLoc(); |
| 3135 |
QualifierLoc = |
| 3136 |
SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 3137 |
TemplateName TName = SemaRef.SubstTemplateName( |
| 3138 |
QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 3139 |
D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 3140 |
if (!TName.isNull()) |
| 3141 |
Param->setDefaultArgument( |
| 3142 |
SemaRef.Context, |
| 3143 |
TemplateArgumentLoc(SemaRef.Context, TemplateArgument(TName), |
| 3144 |
D->getDefaultArgument().getTemplateQualifierLoc(), |
| 3145 |
D->getDefaultArgument().getTemplateNameLoc())); |
| 3146 |
} |
| 3147 |
Param->setAccess(AS_public); |
| 3148 |
Param->setImplicit(D->isImplicit()); |
| 3149 |
|
| 3150 |
// Introduce this template parameter's instantiation into the instantiation |
| 3151 |
// scope. |
| 3152 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| 3153 |
|
| 3154 |
return Param; |
| 3155 |
} |
| 3156 |
|
| 3157 |
Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 3158 |
// Using directives are never dependent (and never contain any types or |
| 3159 |
// expressions), so they require no explicit instantiation work. |
| 3160 |
|
| 3161 |
UsingDirectiveDecl *Inst |
| 3162 |
= UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 3163 |
D->getNamespaceKeyLocation(), |
| 3164 |
D->getQualifierLoc(), |
| 3165 |
D->getIdentLocation(), |
| 3166 |
D->getNominatedNamespace(), |
| 3167 |
D->getCommonAncestor()); |
| 3168 |
|
| 3169 |
// Add the using directive to its declaration context |
| 3170 |
// only if this is not a function or method. |
| 3171 |
if (!Owner->isFunctionOrMethod()) |
| 3172 |
Owner->addDecl(Inst); |
| 3173 |
|
| 3174 |
return Inst; |
| 3175 |
} |
| 3176 |
|
| 3177 |
Decl *TemplateDeclInstantiator::VisitBaseUsingDecls(BaseUsingDecl *D, |
| 3178 |
BaseUsingDecl *Inst, |
| 3179 |
LookupResult *Lookup) { |
| 3180 |
|
| 3181 |
bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 3182 |
|
| 3183 |
for (auto *Shadow : D->shadows()) { |
| 3184 |
// FIXME: UsingShadowDecl doesn't preserve its immediate target, so |
| 3185 |
// reconstruct it in the case where it matters. Hm, can we extract it from |
| 3186 |
// the DeclSpec when parsing and save it in the UsingDecl itself? |
| 3187 |
NamedDecl *OldTarget = Shadow->getTargetDecl(); |
| 3188 |
if (auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow)) |
| 3189 |
if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl()) |
| 3190 |
OldTarget = BaseShadow; |
| 3191 |
|
| 3192 |
NamedDecl *InstTarget = nullptr; |
| 3193 |
if (auto *EmptyD = |
| 3194 |
dyn_cast<UnresolvedUsingIfExistsDecl>(Shadow->getTargetDecl())) { |
| 3195 |
InstTarget = UnresolvedUsingIfExistsDecl::Create( |
| 3196 |
SemaRef.Context, Owner, EmptyD->getLocation(), EmptyD->getDeclName()); |
| 3197 |
} else { |
| 3198 |
InstTarget = cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
| 3199 |
Shadow->getLocation(), OldTarget, TemplateArgs)); |
| 3200 |
} |
| 3201 |
if (!InstTarget) |
| 3202 |
return nullptr; |
| 3203 |
|
| 3204 |
UsingShadowDecl *PrevDecl = nullptr; |
| 3205 |
if (Lookup && |
| 3206 |
SemaRef.CheckUsingShadowDecl(Inst, InstTarget, *Lookup, PrevDecl)) |
| 3207 |
continue; |
| 3208 |
|
| 3209 |
if (UsingShadowDecl *OldPrev = getPreviousDeclForInstantiation(Shadow)) |
| 3210 |
PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 3211 |
Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 3212 |
|
| 3213 |
UsingShadowDecl *InstShadow = SemaRef.BuildUsingShadowDecl( |
| 3214 |
/*Scope*/ nullptr, Inst, InstTarget, PrevDecl); |
| 3215 |
SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
| 3216 |
|
| 3217 |
if (isFunctionScope) |
| 3218 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
| 3219 |
} |
| 3220 |
|
| 3221 |
return Inst; |
| 3222 |
} |
| 3223 |
|
| 3224 |
Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
| 3225 |
|
| 3226 |
// The nested name specifier may be dependent, for example |
| 3227 |
// template <typename T> struct t { |
| 3228 |
// struct s1 { T f1(); }; |
| 3229 |
// struct s2 : s1 { using s1::f1; }; |
| 3230 |
// }; |
| 3231 |
// template struct t<int>; |
| 3232 |
// Here, in using s1::f1, s1 refers to t<T>::s1; |
| 3233 |
// we need to substitute for t<int>::s1. |
| 3234 |
NestedNameSpecifierLoc QualifierLoc |
| 3235 |
= SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 3236 |
TemplateArgs); |
| 3237 |
if (!QualifierLoc) |
| 3238 |
return nullptr; |
| 3239 |
|
| 3240 |
// For an inheriting constructor declaration, the name of the using |
| 3241 |
// declaration is the name of a constructor in this class, not in the |
| 3242 |
// base class. |
| 3243 |
DeclarationNameInfo NameInfo = D->getNameInfo(); |
| 3244 |
if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 3245 |
if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.CurContext)) |
| 3246 |
NameInfo.setName(SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 3247 |
SemaRef.Context.getCanonicalType(SemaRef.Context.getRecordType(RD)))); |
| 3248 |
|
| 3249 |
// We only need to do redeclaration lookups if we're in a class scope (in |
| 3250 |
// fact, it's not really even possible in non-class scopes). |
| 3251 |
bool CheckRedeclaration = Owner->isRecord(); |
| 3252 |
LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| 3253 |
Sema::ForVisibleRedeclaration); |
| 3254 |
|
| 3255 |
UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
| 3256 |
D->getUsingLoc(), |
| 3257 |
QualifierLoc, |
| 3258 |
NameInfo, |
| 3259 |
D->hasTypename()); |
| 3260 |
|
| 3261 |
CXXScopeSpec SS; |
| 3262 |
SS.Adopt(QualifierLoc); |
| 3263 |
if (CheckRedeclaration) { |
| 3264 |
Prev.setHideTags(false); |
| 3265 |
SemaRef.LookupQualifiedName(Prev, Owner); |
| 3266 |
|
| 3267 |
// Check for invalid redeclarations. |
| 3268 |
if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 3269 |
D->hasTypename(), SS, |
| 3270 |
D->getLocation(), Prev)) |
| 3271 |
NewUD->setInvalidDecl(); |
| 3272 |
} |
| 3273 |
|
| 3274 |
if (!NewUD->isInvalidDecl() && |
| 3275 |
SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), SS, |
| 3276 |
NameInfo, D->getLocation(), nullptr, D)) |
| 3277 |
NewUD->setInvalidDecl(); |
| 3278 |
|
| 3279 |
SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 3280 |
NewUD->setAccess(D->getAccess()); |
| 3281 |
Owner->addDecl(NewUD); |
| 3282 |
|
| 3283 |
// Don't process the shadow decls for an invalid decl. |
| 3284 |
if (NewUD->isInvalidDecl()) |
| 3285 |
return NewUD; |
| 3286 |
|
| 3287 |
// If the using scope was dependent, or we had dependent bases, we need to |
| 3288 |
// recheck the inheritance |
| 3289 |
if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 3290 |
SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
| 3291 |
|
| 3292 |
return VisitBaseUsingDecls(D, NewUD, CheckRedeclaration ? &Prev : nullptr); |
| 3293 |
} |
| 3294 |
|
| 3295 |
Decl *TemplateDeclInstantiator::VisitUsingEnumDecl(UsingEnumDecl *D) { |
| 3296 |
// Cannot be a dependent type, but still could be an instantiation |
| 3297 |
EnumDecl *EnumD = cast_or_null<EnumDecl>(SemaRef.FindInstantiatedDecl( |
| 3298 |
D->getLocation(), D->getEnumDecl(), TemplateArgs)); |
| 3299 |
|
| 3300 |
if (SemaRef.RequireCompleteEnumDecl(EnumD, EnumD->getLocation())) |
| 3301 |
return nullptr; |
| 3302 |
|
| 3303 |
TypeSourceInfo *TSI = SemaRef.SubstType(D->getEnumType(), TemplateArgs, |
| 3304 |
D->getLocation(), D->getDeclName()); |
| 3305 |
UsingEnumDecl *NewUD = |
| 3306 |
UsingEnumDecl::Create(SemaRef.Context, Owner, D->getUsingLoc(), |
| 3307 |
D->getEnumLoc(), D->getLocation(), TSI); |
| 3308 |
|
| 3309 |
SemaRef.Context.setInstantiatedFromUsingEnumDecl(NewUD, D); |
| 3310 |
NewUD->setAccess(D->getAccess()); |
| 3311 |
Owner->addDecl(NewUD); |
| 3312 |
|
| 3313 |
// Don't process the shadow decls for an invalid decl. |
| 3314 |
if (NewUD->isInvalidDecl()) |
| 3315 |
return NewUD; |
| 3316 |
|
| 3317 |
// We don't have to recheck for duplication of the UsingEnumDecl itself, as it |
| 3318 |
// cannot be dependent, and will therefore have been checked during template |
| 3319 |
// definition. |
| 3320 |
|
| 3321 |
return VisitBaseUsingDecls(D, NewUD, nullptr); |
| 3322 |
} |
| 3323 |
|
| 3324 |
Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 3325 |
// Ignore these; we handle them in bulk when processing the UsingDecl. |
| 3326 |
return nullptr; |
| 3327 |
} |
| 3328 |
|
| 3329 |
Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl( |
| 3330 |
ConstructorUsingShadowDecl *D) { |
| 3331 |
// Ignore these; we handle them in bulk when processing the UsingDecl. |
| 3332 |
return nullptr; |
| 3333 |
} |
| 3334 |
|
| 3335 |
template <typename T> |
| 3336 |
Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl( |
| 3337 |
T *D, bool InstantiatingPackElement) { |
| 3338 |
// If this is a pack expansion, expand it now. |
| 3339 |
if (D->isPackExpansion() && !InstantiatingPackElement) { |
| 3340 |
SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 3341 |
SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded); |
| 3342 |
SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded); |
| 3343 |
|
| 3344 |
// Determine whether the set of unexpanded parameter packs can and should |
| 3345 |
// be expanded. |
| 3346 |
bool Expand = true; |
| 3347 |
bool RetainExpansion = false; |
| 3348 |
std::optional<unsigned> NumExpansions; |
| 3349 |
if (SemaRef.CheckParameterPacksForExpansion( |
| 3350 |
D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs, |
| 3351 |
Expand, RetainExpansion, NumExpansions)) |
| 3352 |
return nullptr; |
| 3353 |
|
| 3354 |
// This declaration cannot appear within a function template signature, |
| 3355 |
// so we can't have a partial argument list for a parameter pack. |
| 3356 |
assert(!RetainExpansion && |
| 3357 |
"should never need to retain an expansion for UsingPackDecl"); |
| 3358 |
|
| 3359 |
if (!Expand) { |
| 3360 |
// We cannot fully expand the pack expansion now, so substitute into the |
| 3361 |
// pattern and create a new pack expansion. |
| 3362 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 3363 |
return instantiateUnresolvedUsingDecl(D, true); |
| 3364 |
} |
| 3365 |
|
| 3366 |
// Within a function, we don't have any normal way to check for conflicts |
| 3367 |
// between shadow declarations from different using declarations in the |
| 3368 |
// same pack expansion, but this is always ill-formed because all expansions |
| 3369 |
// must produce (conflicting) enumerators. |
| 3370 |
// |
| 3371 |
// Sadly we can't just reject this in the template definition because it |
| 3372 |
// could be valid if the pack is empty or has exactly one expansion. |
| 3373 |
if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) { |
| 3374 |
SemaRef.Diag(D->getEllipsisLoc(), |
| 3375 |
diag::err_using_decl_redeclaration_expansion); |
| 3376 |
return nullptr; |
| 3377 |
} |
| 3378 |
|
| 3379 |
// Instantiate the slices of this pack and build a UsingPackDecl. |
| 3380 |
SmallVector<NamedDecl*, 8> Expansions; |
| 3381 |
for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 3382 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 3383 |
Decl *Slice = instantiateUnresolvedUsingDecl(D, true); |
| 3384 |
if (!Slice) |
| 3385 |
return nullptr; |
| 3386 |
// Note that we can still get unresolved using declarations here, if we |
| 3387 |
// had arguments for all packs but the pattern also contained other |
| 3388 |
// template arguments (this only happens during partial substitution, eg |
| 3389 |
// into the body of a generic lambda in a function template). |
| 3390 |
Expansions.push_back(cast<NamedDecl>(Slice)); |
| 3391 |
} |
| 3392 |
|
| 3393 |
auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 3394 |
if (isDeclWithinFunction(D)) |
| 3395 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 3396 |
return NewD; |
| 3397 |
} |
| 3398 |
|
| 3399 |
UnresolvedUsingTypenameDecl *TD = dyn_cast<UnresolvedUsingTypenameDecl>(D); |
| 3400 |
SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation(); |
| 3401 |
|
| 3402 |
NestedNameSpecifierLoc QualifierLoc |
| 3403 |
= SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 3404 |
TemplateArgs); |
| 3405 |
if (!QualifierLoc) |
| 3406 |
return nullptr; |
| 3407 |
|
| 3408 |
CXXScopeSpec SS; |
| 3409 |
SS.Adopt(QualifierLoc); |
| 3410 |
|
| 3411 |
DeclarationNameInfo NameInfo |
| 3412 |
= SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 3413 |
|
| 3414 |
// Produce a pack expansion only if we're not instantiating a particular |
| 3415 |
// slice of a pack expansion. |
| 3416 |
bool InstantiatingSlice = D->getEllipsisLoc().isValid() && |
| 3417 |
SemaRef.ArgumentPackSubstitutionIndex != -1; |
| 3418 |
SourceLocation EllipsisLoc = |
| 3419 |
InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc(); |
| 3420 |
|
| 3421 |
bool IsUsingIfExists = D->template hasAttr<UsingIfExistsAttr>(); |
| 3422 |
NamedDecl *UD = SemaRef.BuildUsingDeclaration( |
| 3423 |
/*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(), |
| 3424 |
/*HasTypename*/ TD, TypenameLoc, SS, NameInfo, EllipsisLoc, |
| 3425 |
ParsedAttributesView(), |
| 3426 |
/*IsInstantiation*/ true, IsUsingIfExists); |
| 3427 |
if (UD) { |
| 3428 |
SemaRef.InstantiateAttrs(TemplateArgs, D, UD); |
| 3429 |
SemaRef.Context.setInstantiatedFromUsingDecl(UD, D); |
| 3430 |
} |
| 3431 |
|
| 3432 |
return UD; |
| 3433 |
} |
| 3434 |
|
| 3435 |
Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl( |
| 3436 |
UnresolvedUsingTypenameDecl *D) { |
| 3437 |
return instantiateUnresolvedUsingDecl(D); |
| 3438 |
} |
| 3439 |
|
| 3440 |
Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl( |
| 3441 |
UnresolvedUsingValueDecl *D) { |
| 3442 |
return instantiateUnresolvedUsingDecl(D); |
| 3443 |
} |
| 3444 |
|
| 3445 |
Decl *TemplateDeclInstantiator::VisitUnresolvedUsingIfExistsDecl( |
| 3446 |
UnresolvedUsingIfExistsDecl *D) { |
| 3447 |
llvm_unreachable("referring to unresolved decl out of UsingShadowDecl"); |
| 3448 |
} |
| 3449 |
|
| 3450 |
Decl *TemplateDeclInstantiator::VisitUsingPackDecl(UsingPackDecl *D) { |
| 3451 |
SmallVector<NamedDecl*, 8> Expansions; |
| 3452 |
for (auto *UD : D->expansions()) { |
| 3453 |
if (NamedDecl *NewUD = |
| 3454 |
SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs)) |
| 3455 |
Expansions.push_back(NewUD); |
| 3456 |
else |
| 3457 |
return nullptr; |
| 3458 |
} |
| 3459 |
|
| 3460 |
auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 3461 |
if (isDeclWithinFunction(D)) |
| 3462 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 3463 |
return NewD; |
| 3464 |
} |
| 3465 |
|
| 3466 |
Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
| 3467 |
ClassScopeFunctionSpecializationDecl *Decl) { |
| 3468 |
CXXMethodDecl *OldFD = Decl->getSpecialization(); |
| 3469 |
return cast_or_null<CXXMethodDecl>( |
| 3470 |
VisitCXXMethodDecl(OldFD, nullptr, Decl->getTemplateArgsAsWritten())); |
| 3471 |
} |
| 3472 |
|
| 3473 |
Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 3474 |
OMPThreadPrivateDecl *D) { |
| 3475 |
SmallVector<Expr *, 5> Vars; |
| 3476 |
for (auto *I : D->varlists()) { |
| 3477 |
Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
| 3478 |
assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
| 3479 |
Vars.push_back(Var); |
| 3480 |
} |
| 3481 |
|
| 3482 |
OMPThreadPrivateDecl *TD = |
| 3483 |
SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 3484 |
|
| 3485 |
TD->setAccess(AS_public); |
| 3486 |
Owner->addDecl(TD); |
| 3487 |
|
| 3488 |
return TD; |
| 3489 |
} |
| 3490 |
|
| 3491 |
Decl *TemplateDeclInstantiator::VisitOMPAllocateDecl(OMPAllocateDecl *D) { |
| 3492 |
SmallVector<Expr *, 5> Vars; |
| 3493 |
for (auto *I : D->varlists()) { |
| 3494 |
Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
| 3495 |
assert(isa<DeclRefExpr>(Var) && "allocate arg is not a DeclRefExpr"); |
| 3496 |
Vars.push_back(Var); |
| 3497 |
} |
| 3498 |
SmallVector<OMPClause *, 4> Clauses; |
| 3499 |
// Copy map clauses from the original mapper. |
| 3500 |
for (OMPClause *C : D->clauselists()) { |
| 3501 |
OMPClause *IC = nullptr; |
| 3502 |
if (auto *AC = dyn_cast<OMPAllocatorClause>(C)) { |
| 3503 |
ExprResult NewE = SemaRef.SubstExpr(AC->getAllocator(), TemplateArgs); |
| 3504 |
if (!NewE.isUsable()) |
| 3505 |
continue; |
| 3506 |
IC = SemaRef.ActOnOpenMPAllocatorClause( |
| 3507 |
NewE.get(), AC->getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc()); |
| 3508 |
} else if (auto *AC = dyn_cast<OMPAlignClause>(C)) { |
| 3509 |
ExprResult NewE = SemaRef.SubstExpr(AC->getAlignment(), TemplateArgs); |
| 3510 |
if (!NewE.isUsable()) |
| 3511 |
continue; |
| 3512 |
IC = SemaRef.ActOnOpenMPAlignClause(NewE.get(), AC->getBeginLoc(), |
| 3513 |
AC->getLParenLoc(), AC->getEndLoc()); |
| 3514 |
// If align clause value ends up being invalid, this can end up null. |
| 3515 |
if (!IC) |
| 3516 |
continue; |
| 3517 |
} |
| 3518 |
Clauses.push_back(IC); |
| 3519 |
} |
| 3520 |
|
| 3521 |
Sema::DeclGroupPtrTy Res = SemaRef.ActOnOpenMPAllocateDirective( |
| 3522 |
D->getLocation(), Vars, Clauses, Owner); |
| 3523 |
if (Res.get().isNull()) |
| 3524 |
return nullptr; |
| 3525 |
return Res.get().getSingleDecl(); |
| 3526 |
} |
| 3527 |
|
| 3528 |
Decl *TemplateDeclInstantiator::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 3529 |
llvm_unreachable( |
| 3530 |
"Requires directive cannot be instantiated within a dependent context"); |
| 3531 |
} |
| 3532 |
|
| 3533 |
Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( |
| 3534 |
OMPDeclareReductionDecl *D) { |
| 3535 |
// Instantiate type and check if it is allowed. |
| 3536 |
const bool RequiresInstantiation = |
| 3537 |
D->getType()->isDependentType() || |
| 3538 |
D->getType()->isInstantiationDependentType() || |
| 3539 |
D->getType()->containsUnexpandedParameterPack(); |
| 3540 |
QualType SubstReductionType; |
| 3541 |
if (RequiresInstantiation) { |
| 3542 |
SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( |
| 3543 |
D->getLocation(), |
| 3544 |
ParsedType::make(SemaRef.SubstType( |
| 3545 |
D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); |
| 3546 |
} else { |
| 3547 |
SubstReductionType = D->getType(); |
| 3548 |
} |
| 3549 |
if (SubstReductionType.isNull()) |
| 3550 |
return nullptr; |
| 3551 |
Expr *Combiner = D->getCombiner(); |
| 3552 |
Expr *Init = D->getInitializer(); |
| 3553 |
bool IsCorrect = true; |
| 3554 |
// Create instantiated copy. |
| 3555 |
std::pair<QualType, SourceLocation> ReductionTypes[] = { |
| 3556 |
std::make_pair(SubstReductionType, D->getLocation())}; |
| 3557 |
auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 3558 |
if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 3559 |
PrevDeclInScope = cast<OMPDeclareReductionDecl>( |
| 3560 |
SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 3561 |
->get<Decl *>()); |
| 3562 |
} |
| 3563 |
auto DRD = SemaRef.ActOnOpenMPDeclareReductionDirectiveStart( |
| 3564 |
/*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), |
| 3565 |
PrevDeclInScope); |
| 3566 |
auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl()); |
| 3567 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); |
| 3568 |
Expr *SubstCombiner = nullptr; |
| 3569 |
Expr *SubstInitializer = nullptr; |
| 3570 |
// Combiners instantiation sequence. |
| 3571 |
if (Combiner) { |
| 3572 |
SemaRef.ActOnOpenMPDeclareReductionCombinerStart( |
| 3573 |
/*S=*/nullptr, NewDRD); |
| 3574 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3575 |
cast<DeclRefExpr>(D->getCombinerIn())->getDecl(), |
| 3576 |
cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl()); |
| 3577 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3578 |
cast<DeclRefExpr>(D->getCombinerOut())->getDecl(), |
| 3579 |
cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl()); |
| 3580 |
auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
| 3581 |
Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
| 3582 |
ThisContext); |
| 3583 |
SubstCombiner = SemaRef.SubstExpr(Combiner, TemplateArgs).get(); |
| 3584 |
SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); |
| 3585 |
} |
| 3586 |
// Initializers instantiation sequence. |
| 3587 |
if (Init) { |
| 3588 |
VarDecl *OmpPrivParm = SemaRef.ActOnOpenMPDeclareReductionInitializerStart( |
| 3589 |
/*S=*/nullptr, NewDRD); |
| 3590 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3591 |
cast<DeclRefExpr>(D->getInitOrig())->getDecl(), |
| 3592 |
cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl()); |
| 3593 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3594 |
cast<DeclRefExpr>(D->getInitPriv())->getDecl(), |
| 3595 |
cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl()); |
| 3596 |
if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { |
| 3597 |
SubstInitializer = SemaRef.SubstExpr(Init, TemplateArgs).get(); |
| 3598 |
} else { |
| 3599 |
auto *OldPrivParm = |
| 3600 |
cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()); |
| 3601 |
IsCorrect = IsCorrect && OldPrivParm->hasInit(); |
| 3602 |
if (IsCorrect) |
| 3603 |
SemaRef.InstantiateVariableInitializer(OmpPrivParm, OldPrivParm, |
| 3604 |
TemplateArgs); |
| 3605 |
} |
| 3606 |
SemaRef.ActOnOpenMPDeclareReductionInitializerEnd(NewDRD, SubstInitializer, |
| 3607 |
OmpPrivParm); |
| 3608 |
} |
| 3609 |
IsCorrect = IsCorrect && SubstCombiner && |
| 3610 |
(!Init || |
| 3611 |
(D->getInitializerKind() == OMPDeclareReductionDecl::CallInit && |
| 3612 |
SubstInitializer) || |
| 3613 |
(D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && |
| 3614 |
!SubstInitializer)); |
| 3615 |
|
| 3616 |
(void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( |
| 3617 |
/*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); |
| 3618 |
|
| 3619 |
return NewDRD; |
| 3620 |
} |
| 3621 |
|
| 3622 |
Decl * |
| 3623 |
TemplateDeclInstantiator::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { |
| 3624 |
// Instantiate type and check if it is allowed. |
| 3625 |
const bool RequiresInstantiation = |
| 3626 |
D->getType()->isDependentType() || |
| 3627 |
D->getType()->isInstantiationDependentType() || |
| 3628 |
D->getType()->containsUnexpandedParameterPack(); |
| 3629 |
QualType SubstMapperTy; |
| 3630 |
DeclarationName VN = D->getVarName(); |
| 3631 |
if (RequiresInstantiation) { |
| 3632 |
SubstMapperTy = SemaRef.ActOnOpenMPDeclareMapperType( |
| 3633 |
D->getLocation(), |
| 3634 |
ParsedType::make(SemaRef.SubstType(D->getType(), TemplateArgs, |
| 3635 |
D->getLocation(), VN))); |
| 3636 |
} else { |
| 3637 |
SubstMapperTy = D->getType(); |
| 3638 |
} |
| 3639 |
if (SubstMapperTy.isNull()) |
| 3640 |
return nullptr; |
| 3641 |
// Create an instantiated copy of mapper. |
| 3642 |
auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 3643 |
if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 3644 |
PrevDeclInScope = cast<OMPDeclareMapperDecl>( |
| 3645 |
SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 3646 |
->get<Decl *>()); |
| 3647 |
} |
| 3648 |
bool IsCorrect = true; |
| 3649 |
SmallVector<OMPClause *, 6> Clauses; |
| 3650 |
// Instantiate the mapper variable. |
| 3651 |
DeclarationNameInfo DirName; |
| 3652 |
SemaRef.StartOpenMPDSABlock(llvm::omp::OMPD_declare_mapper, DirName, |
| 3653 |
/*S=*/nullptr, |
| 3654 |
(*D->clauselist_begin())->getBeginLoc()); |
| 3655 |
ExprResult MapperVarRef = SemaRef.ActOnOpenMPDeclareMapperDirectiveVarDecl( |
| 3656 |
/*S=*/nullptr, SubstMapperTy, D->getLocation(), VN); |
| 3657 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3658 |
cast<DeclRefExpr>(D->getMapperVarRef())->getDecl(), |
| 3659 |
cast<DeclRefExpr>(MapperVarRef.get())->getDecl()); |
| 3660 |
auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
| 3661 |
Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
| 3662 |
ThisContext); |
| 3663 |
// Instantiate map clauses. |
| 3664 |
for (OMPClause *C : D->clauselists()) { |
| 3665 |
auto *OldC = cast<OMPMapClause>(C); |
| 3666 |
SmallVector<Expr *, 4> NewVars; |
| 3667 |
for (Expr *OE : OldC->varlists()) { |
| 3668 |
Expr *NE = SemaRef.SubstExpr(OE, TemplateArgs).get(); |
| 3669 |
if (!NE) { |
| 3670 |
IsCorrect = false; |
| 3671 |
break; |
| 3672 |
} |
| 3673 |
NewVars.push_back(NE); |
| 3674 |
} |
| 3675 |
if (!IsCorrect) |
| 3676 |
break; |
| 3677 |
NestedNameSpecifierLoc NewQualifierLoc = |
| 3678 |
SemaRef.SubstNestedNameSpecifierLoc(OldC->getMapperQualifierLoc(), |
| 3679 |
TemplateArgs); |
| 3680 |
CXXScopeSpec SS; |
| 3681 |
SS.Adopt(NewQualifierLoc); |
| 3682 |
DeclarationNameInfo NewNameInfo = |
| 3683 |
SemaRef.SubstDeclarationNameInfo(OldC->getMapperIdInfo(), TemplateArgs); |
| 3684 |
OMPVarListLocTy Locs(OldC->getBeginLoc(), OldC->getLParenLoc(), |
| 3685 |
OldC->getEndLoc()); |
| 3686 |
OMPClause *NewC = SemaRef.ActOnOpenMPMapClause( |
| 3687 |
OldC->getIteratorModifier(), OldC->getMapTypeModifiers(), |
| 3688 |
OldC->getMapTypeModifiersLoc(), SS, NewNameInfo, OldC->getMapType(), |
| 3689 |
OldC->isImplicitMapType(), OldC->getMapLoc(), OldC->getColonLoc(), |
| 3690 |
NewVars, Locs); |
| 3691 |
Clauses.push_back(NewC); |
| 3692 |
} |
| 3693 |
SemaRef.EndOpenMPDSABlock(nullptr); |
| 3694 |
if (!IsCorrect) |
| 3695 |
return nullptr; |
| 3696 |
Sema::DeclGroupPtrTy DG = SemaRef.ActOnOpenMPDeclareMapperDirective( |
| 3697 |
/*S=*/nullptr, Owner, D->getDeclName(), SubstMapperTy, D->getLocation(), |
| 3698 |
VN, D->getAccess(), MapperVarRef.get(), Clauses, PrevDeclInScope); |
| 3699 |
Decl *NewDMD = DG.get().getSingleDecl(); |
| 3700 |
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDMD); |
| 3701 |
return NewDMD; |
| 3702 |
} |
| 3703 |
|
| 3704 |
Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl( |
| 3705 |
OMPCapturedExprDecl * /*D*/) { |
| 3706 |
llvm_unreachable("Should not be met in templates"); |
| 3707 |
} |
| 3708 |
|
| 3709 |
Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
| 3710 |
return VisitFunctionDecl(D, nullptr); |
| 3711 |
} |
| 3712 |
|
| 3713 |
Decl * |
| 3714 |
TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
| 3715 |
Decl *Inst = VisitFunctionDecl(D, nullptr); |
| 3716 |
if (Inst && !D->getDescribedFunctionTemplate()) |
| 3717 |
Owner->addDecl(Inst); |
| 3718 |
return Inst; |
| 3719 |
} |
| 3720 |
|
| 3721 |
Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 3722 |
return VisitCXXMethodDecl(D, nullptr); |
| 3723 |
} |
| 3724 |
|
| 3725 |
Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 3726 |
llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 3727 |
} |
| 3728 |
|
| 3729 |
Decl * |
| 3730 |
TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 3731 |
ClassTemplateSpecializationDecl *D) { |
| 3732 |
// As a MS extension, we permit class-scope explicit specialization |
| 3733 |
// of member class templates. |
| 3734 |
ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 3735 |
assert(ClassTemplate->getDeclContext()->isRecord() && |
| 3736 |
D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 3737 |
"can only instantiate an explicit specialization " |
| 3738 |
"for a member class template"); |
| 3739 |
|
| 3740 |
// Lookup the already-instantiated declaration in the instantiation |
| 3741 |
// of the class template. |
| 3742 |
ClassTemplateDecl *InstClassTemplate = |
| 3743 |
cast_or_null<ClassTemplateDecl>(SemaRef.FindInstantiatedDecl( |
| 3744 |
D->getLocation(), ClassTemplate, TemplateArgs)); |
| 3745 |
if (!InstClassTemplate) |
| 3746 |
return nullptr; |
| 3747 |
|
| 3748 |
// Substitute into the template arguments of the class template explicit |
| 3749 |
// specialization. |
| 3750 |
TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 3751 |
castAs<TemplateSpecializationTypeLoc>(); |
| 3752 |
TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 3753 |
Loc.getRAngleLoc()); |
| 3754 |
SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 3755 |
for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 3756 |
ArgLocs.push_back(Loc.getArgLoc(I)); |
| 3757 |
if (SemaRef.SubstTemplateArguments(ArgLocs, TemplateArgs, InstTemplateArgs)) |
| 3758 |
return nullptr; |
| 3759 |
|
| 3760 |
// Check that the template argument list is well-formed for this |
| 3761 |
// class template. |
| 3762 |
SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; |
| 3763 |
if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, D->getLocation(), |
| 3764 |
InstTemplateArgs, false, |
| 3765 |
SugaredConverted, CanonicalConverted, |
| 3766 |
/*UpdateArgsWithConversions=*/true)) |
| 3767 |
return nullptr; |
| 3768 |
|
| 3769 |
// Figure out where to insert this class template explicit specialization |
| 3770 |
// in the member template's set of class template explicit specializations. |
| 3771 |
void *InsertPos = nullptr; |
| 3772 |
ClassTemplateSpecializationDecl *PrevDecl = |
| 3773 |
InstClassTemplate->findSpecialization(CanonicalConverted, InsertPos); |
| 3774 |
|
| 3775 |
// Check whether we've already seen a conflicting instantiation of this |
| 3776 |
// declaration (for instance, if there was a prior implicit instantiation). |
| 3777 |
bool Ignored; |
| 3778 |
if (PrevDecl && |
| 3779 |
SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 3780 |
D->getSpecializationKind(), |
| 3781 |
PrevDecl, |
| 3782 |
PrevDecl->getSpecializationKind(), |
| 3783 |
PrevDecl->getPointOfInstantiation(), |
| 3784 |
Ignored)) |
| 3785 |
return nullptr; |
| 3786 |
|
| 3787 |
// If PrevDecl was a definition and D is also a definition, diagnose. |
| 3788 |
// This happens in cases like: |
| 3789 |
// |
| 3790 |
// template<typename T, typename U> |
| 3791 |
// struct Outer { |
| 3792 |
// template<typename X> struct Inner; |
| 3793 |
// template<> struct Inner<T> {}; |
| 3794 |
// template<> struct Inner<U> {}; |
| 3795 |
// }; |
| 3796 |
// |
| 3797 |
// Outer<int, int> outer; // error: the explicit specializations of Inner |
| 3798 |
// // have the same signature. |
| 3799 |
if (PrevDecl && PrevDecl->getDefinition() && |
| 3800 |
D->isThisDeclarationADefinition()) { |
| 3801 |
SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 3802 |
SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 3803 |
diag::note_previous_definition); |
| 3804 |
return nullptr; |
| 3805 |
} |
| 3806 |
|
| 3807 |
// Create the class template partial specialization declaration. |
| 3808 |
ClassTemplateSpecializationDecl *InstD = |
| 3809 |
ClassTemplateSpecializationDecl::Create( |
| 3810 |
SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 3811 |
D->getLocation(), InstClassTemplate, CanonicalConverted, PrevDecl); |
| 3812 |
|
| 3813 |
// Add this partial specialization to the set of class template partial |
| 3814 |
// specializations. |
| 3815 |
if (!PrevDecl) |
| 3816 |
InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 3817 |
|
| 3818 |
// Substitute the nested name specifier, if any. |
| 3819 |
if (SubstQualifier(D, InstD)) |
| 3820 |
return nullptr; |
| 3821 |
|
| 3822 |
// Build the canonical type that describes the converted template |
| 3823 |
// arguments of the class template explicit specialization. |
| 3824 |
QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 3825 |
TemplateName(InstClassTemplate), CanonicalConverted, |
| 3826 |
SemaRef.Context.getRecordType(InstD)); |
| 3827 |
|
| 3828 |
// Build the fully-sugared type for this class template |
| 3829 |
// specialization as the user wrote in the specialization |
| 3830 |
// itself. This means that we'll pretty-print the type retrieved |
| 3831 |
// from the specialization's declaration the way that the user |
| 3832 |
// actually wrote the specialization, rather than formatting the |
| 3833 |
// name based on the "canonical" representation used to store the |
| 3834 |
// template arguments in the specialization. |
| 3835 |
TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3836 |
TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 3837 |
CanonType); |
| 3838 |
|
| 3839 |
InstD->setAccess(D->getAccess()); |
| 3840 |
InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 3841 |
InstD->setSpecializationKind(D->getSpecializationKind()); |
| 3842 |
InstD->setTypeAsWritten(WrittenTy); |
| 3843 |
InstD->setExternLoc(D->getExternLoc()); |
| 3844 |
InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 3845 |
|
| 3846 |
Owner->addDecl(InstD); |
| 3847 |
|
| 3848 |
// Instantiate the members of the class-scope explicit specialization eagerly. |
| 3849 |
// We don't have support for lazy instantiation of an explicit specialization |
| 3850 |
// yet, and MSVC eagerly instantiates in this case. |
| 3851 |
// FIXME: This is wrong in standard C++. |
| 3852 |
if (D->isThisDeclarationADefinition() && |
| 3853 |
SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 3854 |
TSK_ImplicitInstantiation, |
| 3855 |
/*Complain=*/true)) |
| 3856 |
return nullptr; |
| 3857 |
|
| 3858 |
return InstD; |
| 3859 |
} |
| 3860 |
|
| 3861 |
Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3862 |
VarTemplateSpecializationDecl *D) { |
| 3863 |
|
| 3864 |
TemplateArgumentListInfo VarTemplateArgsInfo; |
| 3865 |
VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 3866 |
assert(VarTemplate && |
| 3867 |
"A template specialization without specialized template?"); |
| 3868 |
|
| 3869 |
VarTemplateDecl *InstVarTemplate = |
| 3870 |
cast_or_null<VarTemplateDecl>(SemaRef.FindInstantiatedDecl( |
| 3871 |
D->getLocation(), VarTemplate, TemplateArgs)); |
| 3872 |
if (!InstVarTemplate) |
| 3873 |
return nullptr; |
| 3874 |
|
| 3875 |
// Substitute the current template arguments. |
| 3876 |
if (const ASTTemplateArgumentListInfo *TemplateArgsInfo = |
| 3877 |
D->getTemplateArgsInfo()) { |
| 3878 |
VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo->getLAngleLoc()); |
| 3879 |
VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo->getRAngleLoc()); |
| 3880 |
|
| 3881 |
if (SemaRef.SubstTemplateArguments(TemplateArgsInfo->arguments(), |
| 3882 |
TemplateArgs, VarTemplateArgsInfo)) |
| 3883 |
return nullptr; |
| 3884 |
} |
| 3885 |
|
| 3886 |
// Check that the template argument list is well-formed for this template. |
| 3887 |
SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; |
| 3888 |
if (SemaRef.CheckTemplateArgumentList(InstVarTemplate, D->getLocation(), |
| 3889 |
VarTemplateArgsInfo, false, |
| 3890 |
SugaredConverted, CanonicalConverted, |
| 3891 |
/*UpdateArgsWithConversions=*/true)) |
| 3892 |
return nullptr; |
| 3893 |
|
| 3894 |
// Check whether we've already seen a declaration of this specialization. |
| 3895 |
void *InsertPos = nullptr; |
| 3896 |
VarTemplateSpecializationDecl *PrevDecl = |
| 3897 |
InstVarTemplate->findSpecialization(CanonicalConverted, InsertPos); |
| 3898 |
|
| 3899 |
// Check whether we've already seen a conflicting instantiation of this |
| 3900 |
// declaration (for instance, if there was a prior implicit instantiation). |
| 3901 |
bool Ignored; |
| 3902 |
if (PrevDecl && SemaRef.CheckSpecializationInstantiationRedecl( |
| 3903 |
D->getLocation(), D->getSpecializationKind(), PrevDecl, |
| 3904 |
PrevDecl->getSpecializationKind(), |
| 3905 |
PrevDecl->getPointOfInstantiation(), Ignored)) |
| 3906 |
return nullptr; |
| 3907 |
|
| 3908 |
return VisitVarTemplateSpecializationDecl( |
| 3909 |
InstVarTemplate, D, VarTemplateArgsInfo, CanonicalConverted, PrevDecl); |
| 3910 |
} |
| 3911 |
|
| 3912 |
Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3913 |
VarTemplateDecl *VarTemplate, VarDecl *D, |
| 3914 |
const TemplateArgumentListInfo &TemplateArgsInfo, |
| 3915 |
ArrayRef<TemplateArgument> Converted, |
| 3916 |
VarTemplateSpecializationDecl *PrevDecl) { |
| 3917 |
|
| 3918 |
// Do substitution on the type of the declaration |
| 3919 |
TypeSourceInfo *DI = |
| 3920 |
SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 3921 |
D->getTypeSpecStartLoc(), D->getDeclName()); |
| 3922 |
if (!DI) |
| 3923 |
return nullptr; |
| 3924 |
|
| 3925 |
if (DI->getType()->isFunctionType()) { |
| 3926 |
SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 3927 |
<< D->isStaticDataMember() << DI->getType(); |
| 3928 |
return nullptr; |
| 3929 |
} |
| 3930 |
|
| 3931 |
// Build the instantiated declaration |
| 3932 |
VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 3933 |
SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 3934 |
VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); |
| 3935 |
Var->setTemplateArgsInfo(TemplateArgsInfo); |
| 3936 |
if (!PrevDecl) { |
| 3937 |
void *InsertPos = nullptr; |
| 3938 |
VarTemplate->findSpecialization(Converted, InsertPos); |
| 3939 |
VarTemplate->AddSpecialization(Var, InsertPos); |
| 3940 |
} |
| 3941 |
|
| 3942 |
if (SemaRef.getLangOpts().OpenCL) |
| 3943 |
SemaRef.deduceOpenCLAddressSpace(Var); |
| 3944 |
|
| 3945 |
// Substitute the nested name specifier, if any. |
| 3946 |
if (SubstQualifier(D, Var)) |
| 3947 |
return nullptr; |
| 3948 |
|
| 3949 |
SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
| 3950 |
StartingScope, false, PrevDecl); |
| 3951 |
|
| 3952 |
return Var; |
| 3953 |
} |
| 3954 |
|
| 3955 |
Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 3956 |
llvm_unreachable("@defs is not supported in Objective-C++"); |
| 3957 |
} |
| 3958 |
|
| 3959 |
Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 3960 |
// FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 3961 |
unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 3962 |
DiagnosticsEngine::Error, |
| 3963 |
"cannot instantiate %0 yet"); |
| 3964 |
SemaRef.Diag(D->getLocation(), DiagID) |
| 3965 |
<< D->getDeclKindName(); |
| 3966 |
|
| 3967 |
return nullptr; |
| 3968 |
} |
| 3969 |
|
| 3970 |
Decl *TemplateDeclInstantiator::VisitConceptDecl(ConceptDecl *D) { |
| 3971 |
llvm_unreachable("Concept definitions cannot reside inside a template"); |
| 3972 |
} |
| 3973 |
|
| 3974 |
Decl *TemplateDeclInstantiator::VisitImplicitConceptSpecializationDecl( |
| 3975 |
ImplicitConceptSpecializationDecl *D) { |
| 3976 |
llvm_unreachable("Concept specializations cannot reside inside a template"); |
| 3977 |
} |
| 3978 |
|
| 3979 |
Decl * |
| 3980 |
TemplateDeclInstantiator::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) { |
| 3981 |
return RequiresExprBodyDecl::Create(SemaRef.Context, D->getDeclContext(), |
| 3982 |
D->getBeginLoc()); |
| 3983 |
} |
| 3984 |
|
| 3985 |
Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 3986 |
llvm_unreachable("Unexpected decl"); |
| 3987 |
} |
| 3988 |
|
| 3989 |
Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
| 3990 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3991 |
TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| 3992 |
if (D->isInvalidDecl()) |
| 3993 |
return nullptr; |
| 3994 |
|
| 3995 |
Decl *SubstD; |
| 3996 |
runWithSufficientStackSpace(D->getLocation(), [&] { |
| 3997 |
SubstD = Instantiator.Visit(D); |
| 3998 |
}); |
| 3999 |
return SubstD; |
| 4000 |
} |
| 4001 |
|
| 4002 |
void TemplateDeclInstantiator::adjustForRewrite(RewriteKind RK, |
| 4003 |
FunctionDecl *Orig, QualType &T, |
| 4004 |
TypeSourceInfo *&TInfo, |
| 4005 |
DeclarationNameInfo &NameInfo) { |
| 4006 |
assert(RK == RewriteKind::RewriteSpaceshipAsEqualEqual); |
| 4007 |
|
| 4008 |
// C++2a [class.compare.default]p3: |
| 4009 |
// the return type is replaced with bool |
| 4010 |
auto *FPT = T->castAs<FunctionProtoType>(); |
| 4011 |
T = SemaRef.Context.getFunctionType( |
| 4012 |
SemaRef.Context.BoolTy, FPT->getParamTypes(), FPT->getExtProtoInfo()); |
| 4013 |
|
| 4014 |
// Update the return type in the source info too. The most straightforward |
| 4015 |
// way is to create new TypeSourceInfo for the new type. Use the location of |
| 4016 |
// the '= default' as the location of the new type. |
| 4017 |
// |
| 4018 |
// FIXME: Set the correct return type when we initially transform the type, |
| 4019 |
// rather than delaying it to now. |
| 4020 |
TypeSourceInfo *NewTInfo = |
| 4021 |
SemaRef.Context.getTrivialTypeSourceInfo(T, Orig->getEndLoc()); |
| 4022 |
auto OldLoc = TInfo->getTypeLoc().getAsAdjusted<FunctionProtoTypeLoc>(); |
| 4023 |
assert(OldLoc && "type of function is not a function type?"); |
| 4024 |
auto NewLoc = NewTInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 4025 |
for (unsigned I = 0, N = OldLoc.getNumParams(); I != N; ++I) |
| 4026 |
NewLoc.setParam(I, OldLoc.getParam(I)); |
| 4027 |
TInfo = NewTInfo; |
| 4028 |
|
| 4029 |
// and the declarator-id is replaced with operator== |
| 4030 |
NameInfo.setName( |
| 4031 |
SemaRef.Context.DeclarationNames.getCXXOperatorName(OO_EqualEqual)); |
| 4032 |
} |
| 4033 |
|
| 4034 |
FunctionDecl *Sema::SubstSpaceshipAsEqualEqual(CXXRecordDecl *RD, |
| 4035 |
FunctionDecl *Spaceship) { |
| 4036 |
if (Spaceship->isInvalidDecl()) |
| 4037 |
return nullptr; |
| 4038 |
|
| 4039 |
// C++2a [class.compare.default]p3: |
| 4040 |
// an == operator function is declared implicitly [...] with the same |
| 4041 |
// access and function-definition and in the same class scope as the |
| 4042 |
// three-way comparison operator function |
| 4043 |
MultiLevelTemplateArgumentList NoTemplateArgs; |
| 4044 |
NoTemplateArgs.setKind(TemplateSubstitutionKind::Rewrite); |
| 4045 |
NoTemplateArgs.addOuterRetainedLevels(RD->getTemplateDepth()); |
| 4046 |
TemplateDeclInstantiator Instantiator(*this, RD, NoTemplateArgs); |
| 4047 |
Decl *R; |
| 4048 |
if (auto *MD = dyn_cast<CXXMethodDecl>(Spaceship)) { |
| 4049 |
R = Instantiator.VisitCXXMethodDecl( |
| 4050 |
MD, nullptr, std::nullopt, |
| 4051 |
TemplateDeclInstantiator::RewriteKind::RewriteSpaceshipAsEqualEqual); |
| 4052 |
} else { |
| 4053 |
assert(Spaceship->getFriendObjectKind() && |
| 4054 |
"defaulted spaceship is neither a member nor a friend"); |
| 4055 |
|
| 4056 |
R = Instantiator.VisitFunctionDecl( |
| 4057 |
Spaceship, nullptr, |
| 4058 |
TemplateDeclInstantiator::RewriteKind::RewriteSpaceshipAsEqualEqual); |
| 4059 |
if (!R) |
| 4060 |
return nullptr; |
| 4061 |
|
| 4062 |
FriendDecl *FD = |
| 4063 |
FriendDecl::Create(Context, RD, Spaceship->getLocation(), |
| 4064 |
cast<NamedDecl>(R), Spaceship->getBeginLoc()); |
| 4065 |
FD->setAccess(AS_public); |
| 4066 |
RD->addDecl(FD); |
| 4067 |
} |
| 4068 |
return cast_or_null<FunctionDecl>(R); |
| 4069 |
} |
| 4070 |
|
| 4071 |
/// Instantiates a nested template parameter list in the current |
| 4072 |
/// instantiation context. |
| 4073 |
/// |
| 4074 |
/// \param L The parameter list to instantiate |
| 4075 |
/// |
| 4076 |
/// \returns NULL if there was an error |
| 4077 |
TemplateParameterList * |
| 4078 |
TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
| 4079 |
// Get errors for all the parameters before bailing out. |
| 4080 |
bool Invalid = false; |
| 4081 |
|
| 4082 |
unsigned N = L->size(); |
| 4083 |
typedef SmallVector<NamedDecl *, 8> ParamVector; |
| 4084 |
ParamVector Params; |
| 4085 |
Params.reserve(N); |
| 4086 |
for (auto &P : *L) { |
| 4087 |
NamedDecl *D = cast_or_null<NamedDecl>(Visit(P)); |
| 4088 |
Params.push_back(D); |
| 4089 |
Invalid = Invalid || !D || D->isInvalidDecl(); |
| 4090 |
} |
| 4091 |
|
| 4092 |
// Clean up if we had an error. |
| 4093 |
if (Invalid) |
| 4094 |
return nullptr; |
| 4095 |
|
| 4096 |
Expr *InstRequiresClause = L->getRequiresClause(); |
| 4097 |
|
| 4098 |
TemplateParameterList *InstL |
| 4099 |
= TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 4100 |
L->getLAngleLoc(), Params, |
| 4101 |
L->getRAngleLoc(), InstRequiresClause); |
| 4102 |
return InstL; |
| 4103 |
} |
| 4104 |
|
| 4105 |
TemplateParameterList * |
| 4106 |
Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, |
| 4107 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4108 |
bool EvaluateConstraints) { |
| 4109 |
TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| 4110 |
Instantiator.setEvaluateConstraints(EvaluateConstraints); |
| 4111 |
return Instantiator.SubstTemplateParams(Params); |
| 4112 |
} |
| 4113 |
|
| 4114 |
/// Instantiate the declaration of a class template partial |
| 4115 |
/// specialization. |
| 4116 |
/// |
| 4117 |
/// \param ClassTemplate the (instantiated) class template that is partially |
| 4118 |
// specialized by the instantiation of \p PartialSpec. |
| 4119 |
/// |
| 4120 |
/// \param PartialSpec the (uninstantiated) class template partial |
| 4121 |
/// specialization that we are instantiating. |
| 4122 |
/// |
| 4123 |
/// \returns The instantiated partial specialization, if successful; otherwise, |
| 4124 |
/// NULL to indicate an error. |
| 4125 |
ClassTemplatePartialSpecializationDecl * |
| 4126 |
TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 4127 |
ClassTemplateDecl *ClassTemplate, |
| 4128 |
ClassTemplatePartialSpecializationDecl *PartialSpec) { |
| 4129 |
// Create a local instantiation scope for this class template partial |
| 4130 |
// specialization, which will contain the instantiations of the template |
| 4131 |
// parameters. |
| 4132 |
LocalInstantiationScope Scope(SemaRef); |
| 4133 |
|
| 4134 |
// Substitute into the template parameters of the class template partial |
| 4135 |
// specialization. |
| 4136 |
TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 4137 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 4138 |
if (!InstParams) |
| 4139 |
return nullptr; |
| 4140 |
|
| 4141 |
// Substitute into the template arguments of the class template partial |
| 4142 |
// specialization. |
| 4143 |
const ASTTemplateArgumentListInfo *TemplArgInfo |
| 4144 |
= PartialSpec->getTemplateArgsAsWritten(); |
| 4145 |
TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 4146 |
TemplArgInfo->RAngleLoc); |
| 4147 |
if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, |
| 4148 |
InstTemplateArgs)) |
| 4149 |
return nullptr; |
| 4150 |
|
| 4151 |
// Check that the template argument list is well-formed for this |
| 4152 |
// class template. |
| 4153 |
SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; |
| 4154 |
if (SemaRef.CheckTemplateArgumentList( |
| 4155 |
ClassTemplate, PartialSpec->getLocation(), InstTemplateArgs, |
| 4156 |
/*PartialTemplateArgs=*/false, SugaredConverted, CanonicalConverted)) |
| 4157 |
return nullptr; |
| 4158 |
|
| 4159 |
// Check these arguments are valid for a template partial specialization. |
| 4160 |
if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 4161 |
PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(), |
| 4162 |
CanonicalConverted)) |
| 4163 |
return nullptr; |
| 4164 |
|
| 4165 |
// Figure out where to insert this class template partial specialization |
| 4166 |
// in the member template's set of class template partial specializations. |
| 4167 |
void *InsertPos = nullptr; |
| 4168 |
ClassTemplateSpecializationDecl *PrevDecl = |
| 4169 |
ClassTemplate->findPartialSpecialization(CanonicalConverted, InstParams, |
| 4170 |
InsertPos); |
| 4171 |
|
| 4172 |
// Build the canonical type that describes the converted template |
| 4173 |
// arguments of the class template partial specialization. |
| 4174 |
QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 4175 |
TemplateName(ClassTemplate), CanonicalConverted); |
| 4176 |
|
| 4177 |
// Build the fully-sugared type for this class template |
| 4178 |
// specialization as the user wrote in the specialization |
| 4179 |
// itself. This means that we'll pretty-print the type retrieved |
| 4180 |
// from the specialization's declaration the way that the user |
| 4181 |
// actually wrote the specialization, rather than formatting the |
| 4182 |
// name based on the "canonical" representation used to store the |
| 4183 |
// template arguments in the specialization. |
| 4184 |
TypeSourceInfo *WrittenTy |
| 4185 |
= SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 4186 |
TemplateName(ClassTemplate), |
| 4187 |
PartialSpec->getLocation(), |
| 4188 |
InstTemplateArgs, |
| 4189 |
CanonType); |
| 4190 |
|
| 4191 |
if (PrevDecl) { |
| 4192 |
// We've already seen a partial specialization with the same template |
| 4193 |
// parameters and template arguments. This can happen, for example, when |
| 4194 |
// substituting the outer template arguments ends up causing two |
| 4195 |
// class template partial specializations of a member class template |
| 4196 |
// to have identical forms, e.g., |
| 4197 |
// |
| 4198 |
// template<typename T, typename U> |
| 4199 |
// struct Outer { |
| 4200 |
// template<typename X, typename Y> struct Inner; |
| 4201 |
// template<typename Y> struct Inner<T, Y>; |
| 4202 |
// template<typename Y> struct Inner<U, Y>; |
| 4203 |
// }; |
| 4204 |
// |
| 4205 |
// Outer<int, int> outer; // error: the partial specializations of Inner |
| 4206 |
// // have the same signature. |
| 4207 |
SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
| 4208 |
<< WrittenTy->getType(); |
| 4209 |
SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 4210 |
<< SemaRef.Context.getTypeDeclType(PrevDecl); |
| 4211 |
return nullptr; |
| 4212 |
} |
| 4213 |
|
| 4214 |
|
| 4215 |
// Create the class template partial specialization declaration. |
| 4216 |
ClassTemplatePartialSpecializationDecl *InstPartialSpec = |
| 4217 |
ClassTemplatePartialSpecializationDecl::Create( |
| 4218 |
SemaRef.Context, PartialSpec->getTagKind(), Owner, |
| 4219 |
PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, |
| 4220 |
ClassTemplate, CanonicalConverted, InstTemplateArgs, CanonType, |
| 4221 |
nullptr); |
| 4222 |
// Substitute the nested name specifier, if any. |
| 4223 |
if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| 4224 |
return nullptr; |
| 4225 |
|
| 4226 |
InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 4227 |
InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 4228 |
|
| 4229 |
// Check the completed partial specialization. |
| 4230 |
SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 4231 |
|
| 4232 |
// Add this partial specialization to the set of class template partial |
| 4233 |
// specializations. |
| 4234 |
ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 4235 |
/*InsertPos=*/nullptr); |
| 4236 |
return InstPartialSpec; |
| 4237 |
} |
| 4238 |
|
| 4239 |
/// Instantiate the declaration of a variable template partial |
| 4240 |
/// specialization. |
| 4241 |
/// |
| 4242 |
/// \param VarTemplate the (instantiated) variable template that is partially |
| 4243 |
/// specialized by the instantiation of \p PartialSpec. |
| 4244 |
/// |
| 4245 |
/// \param PartialSpec the (uninstantiated) variable template partial |
| 4246 |
/// specialization that we are instantiating. |
| 4247 |
/// |
| 4248 |
/// \returns The instantiated partial specialization, if successful; otherwise, |
| 4249 |
/// NULL to indicate an error. |
| 4250 |
VarTemplatePartialSpecializationDecl * |
| 4251 |
TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 4252 |
VarTemplateDecl *VarTemplate, |
| 4253 |
VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 4254 |
// Create a local instantiation scope for this variable template partial |
| 4255 |
// specialization, which will contain the instantiations of the template |
| 4256 |
// parameters. |
| 4257 |
LocalInstantiationScope Scope(SemaRef); |
| 4258 |
|
| 4259 |
// Substitute into the template parameters of the variable template partial |
| 4260 |
// specialization. |
| 4261 |
TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 4262 |
TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 4263 |
if (!InstParams) |
| 4264 |
return nullptr; |
| 4265 |
|
| 4266 |
// Substitute into the template arguments of the variable template partial |
| 4267 |
// specialization. |
| 4268 |
const ASTTemplateArgumentListInfo *TemplArgInfo |
| 4269 |
= PartialSpec->getTemplateArgsAsWritten(); |
| 4270 |
TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 4271 |
TemplArgInfo->RAngleLoc); |
| 4272 |
if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, |
| 4273 |
InstTemplateArgs)) |
| 4274 |
return nullptr; |
| 4275 |
|
| 4276 |
// Check that the template argument list is well-formed for this |
| 4277 |
// class template. |
| 4278 |
SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; |
| 4279 |
if (SemaRef.CheckTemplateArgumentList( |
| 4280 |
VarTemplate, PartialSpec->getLocation(), InstTemplateArgs, |
| 4281 |
/*PartialTemplateArgs=*/false, SugaredConverted, CanonicalConverted)) |
| 4282 |
return nullptr; |
| 4283 |
|
| 4284 |
// Check these arguments are valid for a template partial specialization. |
| 4285 |
if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 4286 |
PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(), |
| 4287 |
CanonicalConverted)) |
| 4288 |
return nullptr; |
| 4289 |
|
| 4290 |
// Figure out where to insert this variable template partial specialization |
| 4291 |
// in the member template's set of variable template partial specializations. |
| 4292 |
void *InsertPos = nullptr; |
| 4293 |
VarTemplateSpecializationDecl *PrevDecl = |
| 4294 |
VarTemplate->findPartialSpecialization(CanonicalConverted, InstParams, |
| 4295 |
InsertPos); |
| 4296 |
|
| 4297 |
// Build the canonical type that describes the converted template |
| 4298 |
// arguments of the variable template partial specialization. |
| 4299 |
QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 4300 |
TemplateName(VarTemplate), CanonicalConverted); |
| 4301 |
|
| 4302 |
// Build the fully-sugared type for this variable template |
| 4303 |
// specialization as the user wrote in the specialization |
| 4304 |
// itself. This means that we'll pretty-print the type retrieved |
| 4305 |
// from the specialization's declaration the way that the user |
| 4306 |
// actually wrote the specialization, rather than formatting the |
| 4307 |
// name based on the "canonical" representation used to store the |
| 4308 |
// template arguments in the specialization. |
| 4309 |
TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 4310 |
TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 4311 |
CanonType); |
| 4312 |
|
| 4313 |
if (PrevDecl) { |
| 4314 |
// We've already seen a partial specialization with the same template |
| 4315 |
// parameters and template arguments. This can happen, for example, when |
| 4316 |
// substituting the outer template arguments ends up causing two |
| 4317 |
// variable template partial specializations of a member variable template |
| 4318 |
// to have identical forms, e.g., |
| 4319 |
// |
| 4320 |
// template<typename T, typename U> |
| 4321 |
// struct Outer { |
| 4322 |
// template<typename X, typename Y> pair<X,Y> p; |
| 4323 |
// template<typename Y> pair<T, Y> p; |
| 4324 |
// template<typename Y> pair<U, Y> p; |
| 4325 |
// }; |
| 4326 |
// |
| 4327 |
// Outer<int, int> outer; // error: the partial specializations of Inner |
| 4328 |
// // have the same signature. |
| 4329 |
SemaRef.Diag(PartialSpec->getLocation(), |
| 4330 |
diag::err_var_partial_spec_redeclared) |
| 4331 |
<< WrittenTy->getType(); |
| 4332 |
SemaRef.Diag(PrevDecl->getLocation(), |
| 4333 |
diag::note_var_prev_partial_spec_here); |
| 4334 |
return nullptr; |
| 4335 |
} |
| 4336 |
|
| 4337 |
// Do substitution on the type of the declaration |
| 4338 |
TypeSourceInfo *DI = SemaRef.SubstType( |
| 4339 |
PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 4340 |
PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 4341 |
if (!DI) |
| 4342 |
return nullptr; |
| 4343 |
|
| 4344 |
if (DI->getType()->isFunctionType()) { |
| 4345 |
SemaRef.Diag(PartialSpec->getLocation(), |
| 4346 |
diag::err_variable_instantiates_to_function) |
| 4347 |
<< PartialSpec->isStaticDataMember() << DI->getType(); |
| 4348 |
return nullptr; |
| 4349 |
} |
| 4350 |
|
| 4351 |
// Create the variable template partial specialization declaration. |
| 4352 |
VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 4353 |
VarTemplatePartialSpecializationDecl::Create( |
| 4354 |
SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 4355 |
PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
| 4356 |
DI, PartialSpec->getStorageClass(), CanonicalConverted, |
| 4357 |
InstTemplateArgs); |
| 4358 |
|
| 4359 |
// Substitute the nested name specifier, if any. |
| 4360 |
if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| 4361 |
return nullptr; |
| 4362 |
|
| 4363 |
InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 4364 |
InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 4365 |
|
| 4366 |
// Check the completed partial specialization. |
| 4367 |
SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 4368 |
|
| 4369 |
// Add this partial specialization to the set of variable template partial |
| 4370 |
// specializations. The instantiation of the initializer is not necessary. |
| 4371 |
VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
| 4372 |
|
| 4373 |
SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
| 4374 |
LateAttrs, Owner, StartingScope); |
| 4375 |
|
| 4376 |
return InstPartialSpec; |
| 4377 |
} |
| 4378 |
|
| 4379 |
TypeSourceInfo* |
| 4380 |
TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
| 4381 |
SmallVectorImpl<ParmVarDecl *> &Params) { |
| 4382 |
TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 4383 |
assert(OldTInfo && "substituting function without type source info"); |
| 4384 |
assert(Params.empty() && "parameter vector is non-empty at start"); |
| 4385 |
|
| 4386 |
CXXRecordDecl *ThisContext = nullptr; |
| 4387 |
Qualifiers ThisTypeQuals; |
| 4388 |
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 4389 |
ThisContext = cast<CXXRecordDecl>(Owner); |
| 4390 |
ThisTypeQuals = Method->getMethodQualifiers(); |
| 4391 |
} |
| 4392 |
|
| 4393 |
TypeSourceInfo *NewTInfo = SemaRef.SubstFunctionDeclType( |
| 4394 |
OldTInfo, TemplateArgs, D->getTypeSpecStartLoc(), D->getDeclName(), |
| 4395 |
ThisContext, ThisTypeQuals, EvaluateConstraints); |
| 4396 |
if (!NewTInfo) |
| 4397 |
return nullptr; |
| 4398 |
|
| 4399 |
TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 4400 |
if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 4401 |
if (NewTInfo != OldTInfo) { |
| 4402 |
// Get parameters from the new type info. |
| 4403 |
TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
| 4404 |
FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
| 4405 |
unsigned NewIdx = 0; |
| 4406 |
for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
| 4407 |
OldIdx != NumOldParams; ++OldIdx) { |
| 4408 |
ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
| 4409 |
if (!OldParam) |
| 4410 |
return nullptr; |
| 4411 |
|
| 4412 |
LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 4413 |
|
| 4414 |
std::optional<unsigned> NumArgumentsInExpansion; |
| 4415 |
if (OldParam->isParameterPack()) |
| 4416 |
NumArgumentsInExpansion = |
| 4417 |
SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 4418 |
TemplateArgs); |
| 4419 |
if (!NumArgumentsInExpansion) { |
| 4420 |
// Simple case: normal parameter, or a parameter pack that's |
| 4421 |
// instantiated to a (still-dependent) parameter pack. |
| 4422 |
ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
| 4423 |
Params.push_back(NewParam); |
| 4424 |
Scope->InstantiatedLocal(OldParam, NewParam); |
| 4425 |
} else { |
| 4426 |
// Parameter pack expansion: make the instantiation an argument pack. |
| 4427 |
Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 4428 |
for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
| 4429 |
ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
| 4430 |
Params.push_back(NewParam); |
| 4431 |
Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 4432 |
} |
| 4433 |
} |
| 4434 |
} |
| 4435 |
} else { |
| 4436 |
// The function type itself was not dependent and therefore no |
| 4437 |
// substitution occurred. However, we still need to instantiate |
| 4438 |
// the function parameters themselves. |
| 4439 |
const FunctionProtoType *OldProto = |
| 4440 |
cast<FunctionProtoType>(OldProtoLoc.getType()); |
| 4441 |
for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 4442 |
++i) { |
| 4443 |
ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
| 4444 |
if (!OldParam) { |
| 4445 |
Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
| 4446 |
D, D->getLocation(), OldProto->getParamType(i))); |
| 4447 |
continue; |
| 4448 |
} |
| 4449 |
|
| 4450 |
ParmVarDecl *Parm = |
| 4451 |
cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
| 4452 |
if (!Parm) |
| 4453 |
return nullptr; |
| 4454 |
Params.push_back(Parm); |
| 4455 |
} |
| 4456 |
} |
| 4457 |
} else { |
| 4458 |
// If the type of this function, after ignoring parentheses, is not |
| 4459 |
// *directly* a function type, then we're instantiating a function that |
| 4460 |
// was declared via a typedef or with attributes, e.g., |
| 4461 |
// |
| 4462 |
// typedef int functype(int, int); |
| 4463 |
// functype func; |
| 4464 |
// int __cdecl meth(int, int); |
| 4465 |
// |
| 4466 |
// In this case, we'll just go instantiate the ParmVarDecls that we |
| 4467 |
// synthesized in the method declaration. |
| 4468 |
SmallVector<QualType, 4> ParamTypes; |
| 4469 |
Sema::ExtParameterInfoBuilder ExtParamInfos; |
| 4470 |
if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr, |
| 4471 |
TemplateArgs, ParamTypes, &Params, |
| 4472 |
ExtParamInfos)) |
| 4473 |
return nullptr; |
| 4474 |
} |
| 4475 |
|
| 4476 |
return NewTInfo; |
| 4477 |
} |
| 4478 |
|
| 4479 |
/// Introduce the instantiated function parameters into the local |
| 4480 |
/// instantiation scope, and set the parameter names to those used |
| 4481 |
/// in the template. |
| 4482 |
bool Sema::addInstantiatedParametersToScope( |
| 4483 |
FunctionDecl *Function, const FunctionDecl *PatternDecl, |
| 4484 |
LocalInstantiationScope &Scope, |
| 4485 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 4486 |
unsigned FParamIdx = 0; |
| 4487 |
for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 4488 |
const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 4489 |
if (!PatternParam->isParameterPack()) { |
| 4490 |
// Simple case: not a parameter pack. |
| 4491 |
assert(FParamIdx < Function->getNumParams()); |
| 4492 |
ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 4493 |
FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 4494 |
// If the parameter's type is not dependent, update it to match the type |
| 4495 |
// in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 4496 |
// the pattern's type here. If the type is dependent, they can't differ, |
| 4497 |
// per core issue 1668. Substitute into the type from the pattern, in case |
| 4498 |
// it's instantiation-dependent. |
| 4499 |
// FIXME: Updating the type to work around this is at best fragile. |
| 4500 |
if (!PatternDecl->getType()->isDependentType()) { |
| 4501 |
QualType T = SubstType(PatternParam->getType(), TemplateArgs, |
| 4502 |
FunctionParam->getLocation(), |
| 4503 |
FunctionParam->getDeclName()); |
| 4504 |
if (T.isNull()) |
| 4505 |
return true; |
| 4506 |
FunctionParam->setType(T); |
| 4507 |
} |
| 4508 |
|
| 4509 |
Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 4510 |
++FParamIdx; |
| 4511 |
continue; |
| 4512 |
} |
| 4513 |
|
| 4514 |
// Expand the parameter pack. |
| 4515 |
Scope.MakeInstantiatedLocalArgPack(PatternParam); |
| 4516 |
std::optional<unsigned> NumArgumentsInExpansion = |
| 4517 |
getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
| 4518 |
if (NumArgumentsInExpansion) { |
| 4519 |
QualType PatternType = |
| 4520 |
PatternParam->getType()->castAs<PackExpansionType>()->getPattern(); |
| 4521 |
for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
| 4522 |
ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 4523 |
FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 4524 |
if (!PatternDecl->getType()->isDependentType()) { |
| 4525 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, Arg); |
| 4526 |
QualType T = |
| 4527 |
SubstType(PatternType, TemplateArgs, FunctionParam->getLocation(), |
| 4528 |
FunctionParam->getDeclName()); |
| 4529 |
if (T.isNull()) |
| 4530 |
return true; |
| 4531 |
FunctionParam->setType(T); |
| 4532 |
} |
| 4533 |
|
| 4534 |
Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 4535 |
++FParamIdx; |
| 4536 |
} |
| 4537 |
} |
| 4538 |
} |
| 4539 |
|
| 4540 |
return false; |
| 4541 |
} |
| 4542 |
|
| 4543 |
bool Sema::InstantiateDefaultArgument(SourceLocation CallLoc, FunctionDecl *FD, |
| 4544 |
ParmVarDecl *Param) { |
| 4545 |
assert(Param->hasUninstantiatedDefaultArg()); |
| 4546 |
|
| 4547 |
// Instantiate the expression. |
| 4548 |
// |
| 4549 |
// FIXME: Pass in a correct Pattern argument, otherwise |
| 4550 |
// getTemplateInstantiationArgs uses the lexical context of FD, e.g. |
| 4551 |
// |
| 4552 |
// template<typename T> |
| 4553 |
// struct A { |
| 4554 |
// static int FooImpl(); |
| 4555 |
// |
| 4556 |
// template<typename Tp> |
| 4557 |
// // bug: default argument A<T>::FooImpl() is evaluated with 2-level |
| 4558 |
// // template argument list [[T], [Tp]], should be [[Tp]]. |
| 4559 |
// friend A<Tp> Foo(int a); |
| 4560 |
// }; |
| 4561 |
// |
| 4562 |
// template<typename T> |
| 4563 |
// A<T> Foo(int a = A<T>::FooImpl()); |
| 4564 |
MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs( |
| 4565 |
FD, /*Final=*/false, nullptr, /*RelativeToPrimary=*/true); |
| 4566 |
|
| 4567 |
if (SubstDefaultArgument(CallLoc, Param, TemplateArgs, /*ForCallExpr*/ true)) |
| 4568 |
return true; |
| 4569 |
|
| 4570 |
if (ASTMutationListener *L = getASTMutationListener()) |
| 4571 |
L->DefaultArgumentInstantiated(Param); |
| 4572 |
|
| 4573 |
return false; |
| 4574 |
} |
| 4575 |
|
| 4576 |
void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 4577 |
FunctionDecl *Decl) { |
| 4578 |
const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 4579 |
if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
| 4580 |
return; |
| 4581 |
|
| 4582 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 4583 |
InstantiatingTemplate::ExceptionSpecification()); |
| 4584 |
if (Inst.isInvalid()) { |
| 4585 |
// We hit the instantiation depth limit. Clear the exception specification |
| 4586 |
// so that our callers don't have to cope with EST_Uninstantiated. |
| 4587 |
UpdateExceptionSpec(Decl, EST_None); |
| 4588 |
return; |
| 4589 |
} |
| 4590 |
if (Inst.isAlreadyInstantiating()) { |
| 4591 |
// This exception specification indirectly depends on itself. Reject. |
| 4592 |
// FIXME: Corresponding rule in the standard? |
| 4593 |
Diag(PointOfInstantiation, diag::err_exception_spec_cycle) << Decl; |
| 4594 |
UpdateExceptionSpec(Decl, EST_None); |
| 4595 |
return; |
| 4596 |
} |
| 4597 |
|
| 4598 |
// Enter the scope of this instantiation. We don't use |
| 4599 |
// PushDeclContext because we don't have a scope. |
| 4600 |
Sema::ContextRAII savedContext(*this, Decl); |
| 4601 |
LocalInstantiationScope Scope(*this); |
| 4602 |
|
| 4603 |
MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs( |
| 4604 |
Decl, /*Final=*/false, nullptr, /*RelativeToPrimary*/ true); |
| 4605 |
|
| 4606 |
// FIXME: We can't use getTemplateInstantiationPattern(false) in general |
| 4607 |
// here, because for a non-defining friend declaration in a class template, |
| 4608 |
// we don't store enough information to map back to the friend declaration in |
| 4609 |
// the template. |
| 4610 |
FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
| 4611 |
if (addInstantiatedParametersToScope(Decl, Template, Scope, TemplateArgs)) { |
| 4612 |
UpdateExceptionSpec(Decl, EST_None); |
| 4613 |
return; |
| 4614 |
} |
| 4615 |
|
| 4616 |
SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(), |
| 4617 |
TemplateArgs); |
| 4618 |
} |
| 4619 |
|
| 4620 |
/// Initializes the common fields of an instantiation function |
| 4621 |
/// declaration (New) from the corresponding fields of its template (Tmpl). |
| 4622 |
/// |
| 4623 |
/// \returns true if there was an error |
| 4624 |
bool |
| 4625 |
TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
| 4626 |
FunctionDecl *Tmpl) { |
| 4627 |
New->setImplicit(Tmpl->isImplicit()); |
| 4628 |
|
| 4629 |
// Forward the mangling number from the template to the instantiated decl. |
| 4630 |
SemaRef.Context.setManglingNumber(New, |
| 4631 |
SemaRef.Context.getManglingNumber(Tmpl)); |
| 4632 |
|
| 4633 |
// If we are performing substituting explicitly-specified template arguments |
| 4634 |
// or deduced template arguments into a function template and we reach this |
| 4635 |
// point, we are now past the point where SFINAE applies and have committed |
| 4636 |
// to keeping the new function template specialization. We therefore |
| 4637 |
// convert the active template instantiation for the function template |
| 4638 |
// into a template instantiation for this specific function template |
| 4639 |
// specialization, which is not a SFINAE context, so that we diagnose any |
| 4640 |
// further errors in the declaration itself. |
| 4641 |
// |
| 4642 |
// FIXME: This is a hack. |
| 4643 |
typedef Sema::CodeSynthesisContext ActiveInstType; |
| 4644 |
ActiveInstType &ActiveInst = SemaRef.CodeSynthesisContexts.back(); |
| 4645 |
if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 4646 |
ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
| 4647 |
if (FunctionTemplateDecl *FunTmpl |
| 4648 |
= dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
| 4649 |
assert(FunTmpl->getTemplatedDecl() == Tmpl && |
| 4650 |
"Deduction from the wrong function template?"); |
| 4651 |
(void) FunTmpl; |
| 4652 |
SemaRef.InstantiatingSpecializations.erase( |
| 4653 |
{ActiveInst.Entity->getCanonicalDecl(), ActiveInst.Kind}); |
| 4654 |
atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
| 4655 |
ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 4656 |
ActiveInst.Entity = New; |
| 4657 |
atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
| 4658 |
} |
| 4659 |
} |
| 4660 |
|
| 4661 |
const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 4662 |
assert(Proto && "Function template without prototype?"); |
| 4663 |
|
| 4664 |
if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
| 4665 |
FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 4666 |
|
| 4667 |
// DR1330: In C++11, defer instantiation of a non-trivial |
| 4668 |
// exception specification. |
| 4669 |
// DR1484: Local classes and their members are instantiated along with the |
| 4670 |
// containing function. |
| 4671 |
if (SemaRef.getLangOpts().CPlusPlus11 && |
| 4672 |
EPI.ExceptionSpec.Type != EST_None && |
| 4673 |
EPI.ExceptionSpec.Type != EST_DynamicNone && |
| 4674 |
EPI.ExceptionSpec.Type != EST_BasicNoexcept && |
| 4675 |
!Tmpl->isInLocalScopeForInstantiation()) { |
| 4676 |
FunctionDecl *ExceptionSpecTemplate = Tmpl; |
| 4677 |
if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 4678 |
ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
| 4679 |
ExceptionSpecificationType NewEST = EST_Uninstantiated; |
| 4680 |
if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
| 4681 |
NewEST = EST_Unevaluated; |
| 4682 |
|
| 4683 |
// Mark the function has having an uninstantiated exception specification. |
| 4684 |
const FunctionProtoType *NewProto |
| 4685 |
= New->getType()->getAs<FunctionProtoType>(); |
| 4686 |
assert(NewProto && "Template instantiation without function prototype?"); |
| 4687 |
EPI = NewProto->getExtProtoInfo(); |
| 4688 |
EPI.ExceptionSpec.Type = NewEST; |
| 4689 |
EPI.ExceptionSpec.SourceDecl = New; |
| 4690 |
EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
| 4691 |
New->setType(SemaRef.Context.getFunctionType( |
| 4692 |
NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
| 4693 |
} else { |
| 4694 |
Sema::ContextRAII SwitchContext(SemaRef, New); |
| 4695 |
SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs); |
| 4696 |
} |
| 4697 |
} |
| 4698 |
|
| 4699 |
// Get the definition. Leaves the variable unchanged if undefined. |
| 4700 |
const FunctionDecl *Definition = Tmpl; |
| 4701 |
Tmpl->isDefined(Definition); |
| 4702 |
|
| 4703 |
SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 4704 |
LateAttrs, StartingScope); |
| 4705 |
|
| 4706 |
return false; |
| 4707 |
} |
| 4708 |
|
| 4709 |
/// Initializes common fields of an instantiated method |
| 4710 |
/// declaration (New) from the corresponding fields of its template |
| 4711 |
/// (Tmpl). |
| 4712 |
/// |
| 4713 |
/// \returns true if there was an error |
| 4714 |
bool |
| 4715 |
TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| 4716 |
CXXMethodDecl *Tmpl) { |
| 4717 |
if (InitFunctionInstantiation(New, Tmpl)) |
| 4718 |
return true; |
| 4719 |
|
| 4720 |
if (isa<CXXDestructorDecl>(New) && SemaRef.getLangOpts().CPlusPlus11) |
| 4721 |
SemaRef.AdjustDestructorExceptionSpec(cast<CXXDestructorDecl>(New)); |
| 4722 |
|
| 4723 |
New->setAccess(Tmpl->getAccess()); |
| 4724 |
if (Tmpl->isVirtualAsWritten()) |
| 4725 |
New->setVirtualAsWritten(true); |
| 4726 |
|
| 4727 |
// FIXME: New needs a pointer to Tmpl |
| 4728 |
return false; |
| 4729 |
} |
| 4730 |
|
| 4731 |
bool TemplateDeclInstantiator::SubstDefaultedFunction(FunctionDecl *New, |
| 4732 |
FunctionDecl *Tmpl) { |
| 4733 |
// Transfer across any unqualified lookups. |
| 4734 |
if (auto *DFI = Tmpl->getDefaultedFunctionInfo()) { |
| 4735 |
SmallVector<DeclAccessPair, 32> Lookups; |
| 4736 |
Lookups.reserve(DFI->getUnqualifiedLookups().size()); |
| 4737 |
bool AnyChanged = false; |
| 4738 |
for (DeclAccessPair DA : DFI->getUnqualifiedLookups()) { |
| 4739 |
NamedDecl *D = SemaRef.FindInstantiatedDecl(New->getLocation(), |
| 4740 |
DA.getDecl(), TemplateArgs); |
| 4741 |
if (!D) |
| 4742 |
return true; |
| 4743 |
AnyChanged |= (D != DA.getDecl()); |
| 4744 |
Lookups.push_back(DeclAccessPair::make(D, DA.getAccess())); |
| 4745 |
} |
| 4746 |
|
| 4747 |
// It's unlikely that substitution will change any declarations. Don't |
| 4748 |
// store an unnecessary copy in that case. |
| 4749 |
New->setDefaultedFunctionInfo( |
| 4750 |
AnyChanged ? FunctionDecl::DefaultedFunctionInfo::Create( |
| 4751 |
SemaRef.Context, Lookups) |
| 4752 |
: DFI); |
| 4753 |
} |
| 4754 |
|
| 4755 |
SemaRef.SetDeclDefaulted(New, Tmpl->getLocation()); |
| 4756 |
return false; |
| 4757 |
} |
| 4758 |
|
| 4759 |
/// Instantiate (or find existing instantiation of) a function template with a |
| 4760 |
/// given set of template arguments. |
| 4761 |
/// |
| 4762 |
/// Usually this should not be used, and template argument deduction should be |
| 4763 |
/// used in its place. |
| 4764 |
FunctionDecl * |
| 4765 |
Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD, |
| 4766 |
const TemplateArgumentList *Args, |
| 4767 |
SourceLocation Loc) { |
| 4768 |
FunctionDecl *FD = FTD->getTemplatedDecl(); |
| 4769 |
|
| 4770 |
sema::TemplateDeductionInfo Info(Loc); |
| 4771 |
InstantiatingTemplate Inst( |
| 4772 |
*this, Loc, FTD, Args->asArray(), |
| 4773 |
CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| 4774 |
if (Inst.isInvalid()) |
| 4775 |
return nullptr; |
| 4776 |
|
| 4777 |
ContextRAII SavedContext(*this, FD); |
| 4778 |
MultiLevelTemplateArgumentList MArgs(FTD, Args->asArray(), |
| 4779 |
/*Final=*/false); |
| 4780 |
|
| 4781 |
return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs)); |
| 4782 |
} |
| 4783 |
|
| 4784 |
/// Instantiate the definition of the given function from its |
| 4785 |
/// template. |
| 4786 |
/// |
| 4787 |
/// \param PointOfInstantiation the point at which the instantiation was |
| 4788 |
/// required. Note that this is not precisely a "point of instantiation" |
| 4789 |
/// for the function, but it's close. |
| 4790 |
/// |
| 4791 |
/// \param Function the already-instantiated declaration of a |
| 4792 |
/// function template specialization or member function of a class template |
| 4793 |
/// specialization. |
| 4794 |
/// |
| 4795 |
/// \param Recursive if true, recursively instantiates any functions that |
| 4796 |
/// are required by this instantiation. |
| 4797 |
/// |
| 4798 |
/// \param DefinitionRequired if true, then we are performing an explicit |
| 4799 |
/// instantiation where the body of the function is required. Complain if |
| 4800 |
/// there is no such body. |
| 4801 |
void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
| 4802 |
FunctionDecl *Function, |
| 4803 |
bool Recursive, |
| 4804 |
bool DefinitionRequired, |
| 4805 |
bool AtEndOfTU) { |
| 4806 |
if (Function->isInvalidDecl() || isa<CXXDeductionGuideDecl>(Function)) |
| 4807 |
return; |
| 4808 |
|
| 4809 |
// Never instantiate an explicit specialization except if it is a class scope |
| 4810 |
// explicit specialization. |
| 4811 |
TemplateSpecializationKind TSK = |
| 4812 |
Function->getTemplateSpecializationKindForInstantiation(); |
| 4813 |
if (TSK == TSK_ExplicitSpecialization) |
| 4814 |
return; |
| 4815 |
|
| 4816 |
// Never implicitly instantiate a builtin; we don't actually need a function |
| 4817 |
// body. |
| 4818 |
if (Function->getBuiltinID() && TSK == TSK_ImplicitInstantiation && |
| 4819 |
!DefinitionRequired) |
| 4820 |
return; |
| 4821 |
|
| 4822 |
// Don't instantiate a definition if we already have one. |
| 4823 |
const FunctionDecl *ExistingDefn = nullptr; |
| 4824 |
if (Function->isDefined(ExistingDefn, |
| 4825 |
/*CheckForPendingFriendDefinition=*/true)) { |
| 4826 |
if (ExistingDefn->isThisDeclarationADefinition()) |
| 4827 |
return; |
| 4828 |
|
| 4829 |
// If we're asked to instantiate a function whose body comes from an |
| 4830 |
// instantiated friend declaration, attach the instantiated body to the |
| 4831 |
// corresponding declaration of the function. |
| 4832 |
assert(ExistingDefn->isThisDeclarationInstantiatedFromAFriendDefinition()); |
| 4833 |
Function = const_cast<FunctionDecl*>(ExistingDefn); |
| 4834 |
} |
| 4835 |
|
| 4836 |
// Find the function body that we'll be substituting. |
| 4837 |
const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
| 4838 |
assert(PatternDecl && "instantiating a non-template"); |
| 4839 |
|
| 4840 |
const FunctionDecl *PatternDef = PatternDecl->getDefinition(); |
| 4841 |
Stmt *Pattern = nullptr; |
| 4842 |
if (PatternDef) { |
| 4843 |
Pattern = PatternDef->getBody(PatternDef); |
| 4844 |
PatternDecl = PatternDef; |
| 4845 |
if (PatternDef->willHaveBody()) |
| 4846 |
PatternDef = nullptr; |
| 4847 |
} |
| 4848 |
|
| 4849 |
// FIXME: We need to track the instantiation stack in order to know which |
| 4850 |
// definitions should be visible within this instantiation. |
| 4851 |
if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, |
| 4852 |
Function->getInstantiatedFromMemberFunction(), |
| 4853 |
PatternDecl, PatternDef, TSK, |
| 4854 |
/*Complain*/DefinitionRequired)) { |
| 4855 |
if (DefinitionRequired) |
| 4856 |
Function->setInvalidDecl(); |
| 4857 |
else if (TSK == TSK_ExplicitInstantiationDefinition || |
| 4858 |
(Function->isConstexpr() && !Recursive)) { |
| 4859 |
// Try again at the end of the translation unit (at which point a |
| 4860 |
// definition will be required). |
| 4861 |
assert(!Recursive); |
| 4862 |
Function->setInstantiationIsPending(true); |
| 4863 |
PendingInstantiations.push_back( |
| 4864 |
std::make_pair(Function, PointOfInstantiation)); |
| 4865 |
} else if (TSK == TSK_ImplicitInstantiation) { |
| 4866 |
if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
| 4867 |
!getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
| 4868 |
Diag(PointOfInstantiation, diag::warn_func_template_missing) |
| 4869 |
<< Function; |
| 4870 |
Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4871 |
if (getLangOpts().CPlusPlus11) |
| 4872 |
Diag(PointOfInstantiation, diag::note_inst_declaration_hint) |
| 4873 |
<< Function; |
| 4874 |
} |
| 4875 |
} |
| 4876 |
|
| 4877 |
return; |
| 4878 |
} |
| 4879 |
|
| 4880 |
// Postpone late parsed template instantiations. |
| 4881 |
if (PatternDecl->isLateTemplateParsed() && |
| 4882 |
!LateTemplateParser) { |
| 4883 |
Function->setInstantiationIsPending(true); |
| 4884 |
LateParsedInstantiations.push_back( |
| 4885 |
std::make_pair(Function, PointOfInstantiation)); |
| 4886 |
return; |
| 4887 |
} |
| 4888 |
|
| 4889 |
llvm::TimeTraceScope TimeScope("InstantiateFunction", [&]() { |
| 4890 |
std::string Name; |
| 4891 |
llvm::raw_string_ostream OS(Name); |
| 4892 |
Function->getNameForDiagnostic(OS, getPrintingPolicy(), |
| 4893 |
/*Qualified=*/true); |
| 4894 |
return Name; |
| 4895 |
}); |
| 4896 |
|
| 4897 |
// If we're performing recursive template instantiation, create our own |
| 4898 |
// queue of pending implicit instantiations that we will instantiate later, |
| 4899 |
// while we're still within our own instantiation context. |
| 4900 |
// This has to happen before LateTemplateParser below is called, so that |
| 4901 |
// it marks vtables used in late parsed templates as used. |
| 4902 |
GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4903 |
/*Enabled=*/Recursive); |
| 4904 |
LocalEagerInstantiationScope LocalInstantiations(*this); |
| 4905 |
|
| 4906 |
// Call the LateTemplateParser callback if there is a need to late parse |
| 4907 |
// a templated function definition. |
| 4908 |
if (!Pattern && PatternDecl->isLateTemplateParsed() && |
| 4909 |
LateTemplateParser) { |
| 4910 |
// FIXME: Optimize to allow individual templates to be deserialized. |
| 4911 |
if (PatternDecl->isFromASTFile()) |
| 4912 |
ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 4913 |
|
| 4914 |
auto LPTIter = LateParsedTemplateMap.find(PatternDecl); |
| 4915 |
assert(LPTIter != LateParsedTemplateMap.end() && |
| 4916 |
"missing LateParsedTemplate"); |
| 4917 |
LateTemplateParser(OpaqueParser, *LPTIter->second); |
| 4918 |
Pattern = PatternDecl->getBody(PatternDecl); |
| 4919 |
} |
| 4920 |
|
| 4921 |
// Note, we should never try to instantiate a deleted function template. |
| 4922 |
assert((Pattern || PatternDecl->isDefaulted() || |
| 4923 |
PatternDecl->hasSkippedBody()) && |
| 4924 |
"unexpected kind of function template definition"); |
| 4925 |
|
| 4926 |
// C++1y [temp.explicit]p10: |
| 4927 |
// Except for inline functions, declarations with types deduced from their |
| 4928 |
// initializer or return value, and class template specializations, other |
| 4929 |
// explicit instantiation declarations have the effect of suppressing the |
| 4930 |
// implicit instantiation of the entity to which they refer. |
| 4931 |
if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 4932 |
!PatternDecl->isInlined() && |
| 4933 |
!PatternDecl->getReturnType()->getContainedAutoType()) |
| 4934 |
return; |
| 4935 |
|
| 4936 |
if (PatternDecl->isInlined()) { |
| 4937 |
// Function, and all later redeclarations of it (from imported modules, |
| 4938 |
// for instance), are now implicitly inline. |
| 4939 |
for (auto *D = Function->getMostRecentDecl(); /**/; |
| 4940 |
D = D->getPreviousDecl()) { |
| 4941 |
D->setImplicitlyInline(); |
| 4942 |
if (D == Function) |
| 4943 |
break; |
| 4944 |
} |
| 4945 |
} |
| 4946 |
|
| 4947 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 4948 |
if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| 4949 |
return; |
| 4950 |
PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(), |
| 4951 |
"instantiating function definition"); |
| 4952 |
|
| 4953 |
// The instantiation is visible here, even if it was first declared in an |
| 4954 |
// unimported module. |
| 4955 |
Function->setVisibleDespiteOwningModule(); |
| 4956 |
|
| 4957 |
// Copy the inner loc start from the pattern. |
| 4958 |
Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 4959 |
|
| 4960 |
EnterExpressionEvaluationContext EvalContext( |
| 4961 |
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 4962 |
|
| 4963 |
// Introduce a new scope where local variable instantiations will be |
| 4964 |
// recorded, unless we're actually a member function within a local |
| 4965 |
// class, in which case we need to merge our results with the parent |
| 4966 |
// scope (of the enclosing function). The exception is instantiating |
| 4967 |
// a function template specialization, since the template to be |
| 4968 |
// instantiated already has references to locals properly substituted. |
| 4969 |
bool MergeWithParentScope = false; |
| 4970 |
if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 4971 |
MergeWithParentScope = |
| 4972 |
Rec->isLocalClass() && !Function->isFunctionTemplateSpecialization(); |
| 4973 |
|
| 4974 |
LocalInstantiationScope Scope(*this, MergeWithParentScope); |
| 4975 |
auto RebuildTypeSourceInfoForDefaultSpecialMembers = [&]() { |
| 4976 |
// Special members might get their TypeSourceInfo set up w.r.t the |
| 4977 |
// PatternDecl context, in which case parameters could still be pointing |
| 4978 |
// back to the original class, make sure arguments are bound to the |
| 4979 |
// instantiated record instead. |
| 4980 |
assert(PatternDecl->isDefaulted() && |
| 4981 |
"Special member needs to be defaulted"); |
| 4982 |
auto PatternSM = getDefaultedFunctionKind(PatternDecl).asSpecialMember(); |
| 4983 |
if (!(PatternSM == Sema::CXXCopyConstructor || |
| 4984 |
PatternSM == Sema::CXXCopyAssignment || |
| 4985 |
PatternSM == Sema::CXXMoveConstructor || |
| 4986 |
PatternSM == Sema::CXXMoveAssignment)) |
| 4987 |
return; |
| 4988 |
|
| 4989 |
auto *NewRec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()); |
| 4990 |
const auto *PatternRec = |
| 4991 |
dyn_cast<CXXRecordDecl>(PatternDecl->getDeclContext()); |
| 4992 |
if (!NewRec || !PatternRec) |
| 4993 |
return; |
| 4994 |
if (!PatternRec->isLambda()) |
| 4995 |
return; |
| 4996 |
|
| 4997 |
struct SpecialMemberTypeInfoRebuilder |
| 4998 |
: TreeTransform<SpecialMemberTypeInfoRebuilder> { |
| 4999 |
using Base = TreeTransform<SpecialMemberTypeInfoRebuilder>; |
| 5000 |
const CXXRecordDecl *OldDecl; |
| 5001 |
CXXRecordDecl *NewDecl; |
| 5002 |
|
| 5003 |
SpecialMemberTypeInfoRebuilder(Sema &SemaRef, const CXXRecordDecl *O, |
| 5004 |
CXXRecordDecl *N) |
| 5005 |
: TreeTransform(SemaRef), OldDecl(O), NewDecl(N) {} |
| 5006 |
|
| 5007 |
bool TransformExceptionSpec(SourceLocation Loc, |
| 5008 |
FunctionProtoType::ExceptionSpecInfo &ESI, |
| 5009 |
SmallVectorImpl<QualType> &Exceptions, |
| 5010 |
bool &Changed) { |
| 5011 |
return false; |
| 5012 |
} |
| 5013 |
|
| 5014 |
QualType TransformRecordType(TypeLocBuilder &TLB, RecordTypeLoc TL) { |
| 5015 |
const RecordType *T = TL.getTypePtr(); |
| 5016 |
RecordDecl *Record = cast_or_null<RecordDecl>( |
| 5017 |
getDerived().TransformDecl(TL.getNameLoc(), T->getDecl())); |
| 5018 |
if (Record != OldDecl) |
| 5019 |
return Base::TransformRecordType(TLB, TL); |
| 5020 |
|
| 5021 |
QualType Result = getDerived().RebuildRecordType(NewDecl); |
| 5022 |
if (Result.isNull()) |
| 5023 |
return QualType(); |
| 5024 |
|
| 5025 |
RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result); |
| 5026 |
NewTL.setNameLoc(TL.getNameLoc()); |
| 5027 |
return Result; |
| 5028 |
} |
| 5029 |
} IR{*this, PatternRec, NewRec}; |
| 5030 |
|
| 5031 |
TypeSourceInfo *NewSI = IR.TransformType(Function->getTypeSourceInfo()); |
| 5032 |
Function->setType(NewSI->getType()); |
| 5033 |
Function->setTypeSourceInfo(NewSI); |
| 5034 |
|
| 5035 |
ParmVarDecl *Parm = Function->getParamDecl(0); |
| 5036 |
TypeSourceInfo *NewParmSI = IR.TransformType(Parm->getTypeSourceInfo()); |
| 5037 |
Parm->setType(NewParmSI->getType()); |
| 5038 |
Parm->setTypeSourceInfo(NewParmSI); |
| 5039 |
}; |
| 5040 |
|
| 5041 |
if (PatternDecl->isDefaulted()) { |
| 5042 |
RebuildTypeSourceInfoForDefaultSpecialMembers(); |
| 5043 |
SetDeclDefaulted(Function, PatternDecl->getLocation()); |
| 5044 |
} else { |
| 5045 |
MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs( |
| 5046 |
Function, /*Final=*/false, nullptr, false, PatternDecl); |
| 5047 |
|
| 5048 |
// Substitute into the qualifier; we can get a substitution failure here |
| 5049 |
// through evil use of alias templates. |
| 5050 |
// FIXME: Is CurContext correct for this? Should we go to the (instantiation |
| 5051 |
// of the) lexical context of the pattern? |
| 5052 |
SubstQualifier(*this, PatternDecl, Function, TemplateArgs); |
| 5053 |
|
| 5054 |
ActOnStartOfFunctionDef(nullptr, Function); |
| 5055 |
|
| 5056 |
// Enter the scope of this instantiation. We don't use |
| 5057 |
// PushDeclContext because we don't have a scope. |
| 5058 |
Sema::ContextRAII savedContext(*this, Function); |
| 5059 |
|
| 5060 |
if (addInstantiatedParametersToScope(Function, PatternDecl, Scope, |
| 5061 |
TemplateArgs)) |
| 5062 |
return; |
| 5063 |
|
| 5064 |
StmtResult Body; |
| 5065 |
if (PatternDecl->hasSkippedBody()) { |
| 5066 |
ActOnSkippedFunctionBody(Function); |
| 5067 |
Body = nullptr; |
| 5068 |
} else { |
| 5069 |
if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Function)) { |
| 5070 |
// If this is a constructor, instantiate the member initializers. |
| 5071 |
InstantiateMemInitializers(Ctor, cast<CXXConstructorDecl>(PatternDecl), |
| 5072 |
TemplateArgs); |
| 5073 |
|
| 5074 |
// If this is an MS ABI dllexport default constructor, instantiate any |
| 5075 |
// default arguments. |
| 5076 |
if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5077 |
Ctor->isDefaultConstructor()) { |
| 5078 |
InstantiateDefaultCtorDefaultArgs(Ctor); |
| 5079 |
} |
| 5080 |
} |
| 5081 |
|
| 5082 |
// Instantiate the function body. |
| 5083 |
Body = SubstStmt(Pattern, TemplateArgs); |
| 5084 |
|
| 5085 |
if (Body.isInvalid()) |
| 5086 |
Function->setInvalidDecl(); |
| 5087 |
} |
| 5088 |
// FIXME: finishing the function body while in an expression evaluation |
| 5089 |
// context seems wrong. Investigate more. |
| 5090 |
ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true); |
| 5091 |
|
| 5092 |
PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 5093 |
|
| 5094 |
if (auto *Listener = getASTMutationListener()) |
| 5095 |
Listener->FunctionDefinitionInstantiated(Function); |
| 5096 |
|
| 5097 |
savedContext.pop(); |
| 5098 |
} |
| 5099 |
|
| 5100 |
DeclGroupRef DG(Function); |
| 5101 |
Consumer.HandleTopLevelDecl(DG); |
| 5102 |
|
| 5103 |
// This class may have local implicit instantiations that need to be |
| 5104 |
// instantiation within this scope. |
| 5105 |
LocalInstantiations.perform(); |
| 5106 |
Scope.Exit(); |
| 5107 |
GlobalInstantiations.perform(); |
| 5108 |
} |
| 5109 |
|
| 5110 |
VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 5111 |
VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 5112 |
const TemplateArgumentList &TemplateArgList, |
| 5113 |
const TemplateArgumentListInfo &TemplateArgsInfo, |
| 5114 |
SmallVectorImpl<TemplateArgument> &Converted, |
| 5115 |
SourceLocation PointOfInstantiation, LateInstantiatedAttrVec *LateAttrs, |
| 5116 |
LocalInstantiationScope *StartingScope) { |
| 5117 |
if (FromVar->isInvalidDecl()) |
| 5118 |
return nullptr; |
| 5119 |
|
| 5120 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
| 5121 |
if (Inst.isInvalid()) |
| 5122 |
return nullptr; |
| 5123 |
|
| 5124 |
// Instantiate the first declaration of the variable template: for a partial |
| 5125 |
// specialization of a static data member template, the first declaration may |
| 5126 |
// or may not be the declaration in the class; if it's in the class, we want |
| 5127 |
// to instantiate a member in the class (a declaration), and if it's outside, |
| 5128 |
// we want to instantiate a definition. |
| 5129 |
// |
| 5130 |
// If we're instantiating an explicitly-specialized member template or member |
| 5131 |
// partial specialization, don't do this. The member specialization completely |
| 5132 |
// replaces the original declaration in this case. |
| 5133 |
bool IsMemberSpec = false; |
| 5134 |
MultiLevelTemplateArgumentList MultiLevelList; |
| 5135 |
if (auto *PartialSpec = |
| 5136 |
dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) { |
| 5137 |
IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 5138 |
MultiLevelList.addOuterTemplateArguments( |
| 5139 |
PartialSpec, TemplateArgList.asArray(), /*Final=*/false); |
| 5140 |
} else { |
| 5141 |
assert(VarTemplate == FromVar->getDescribedVarTemplate()); |
| 5142 |
IsMemberSpec = VarTemplate->isMemberSpecialization(); |
| 5143 |
MultiLevelList.addOuterTemplateArguments( |
| 5144 |
VarTemplate, TemplateArgList.asArray(), /*Final=*/false); |
| 5145 |
} |
| 5146 |
if (!IsMemberSpec) |
| 5147 |
FromVar = FromVar->getFirstDecl(); |
| 5148 |
|
| 5149 |
TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 5150 |
MultiLevelList); |
| 5151 |
|
| 5152 |
// TODO: Set LateAttrs and StartingScope ... |
| 5153 |
|
| 5154 |
return cast_or_null<VarTemplateSpecializationDecl>( |
| 5155 |
Instantiator.VisitVarTemplateSpecializationDecl( |
| 5156 |
VarTemplate, FromVar, TemplateArgsInfo, Converted)); |
| 5157 |
} |
| 5158 |
|
| 5159 |
/// Instantiates a variable template specialization by completing it |
| 5160 |
/// with appropriate type information and initializer. |
| 5161 |
VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 5162 |
VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 5163 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 5164 |
assert(PatternDecl->isThisDeclarationADefinition() && |
| 5165 |
"don't have a definition to instantiate from"); |
| 5166 |
|
| 5167 |
// Do substitution on the type of the declaration |
| 5168 |
TypeSourceInfo *DI = |
| 5169 |
SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
| 5170 |
PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 5171 |
if (!DI) |
| 5172 |
return nullptr; |
| 5173 |
|
| 5174 |
// Update the type of this variable template specialization. |
| 5175 |
VarSpec->setType(DI->getType()); |
| 5176 |
|
| 5177 |
// Convert the declaration into a definition now. |
| 5178 |
VarSpec->setCompleteDefinition(); |
| 5179 |
|
| 5180 |
// Instantiate the initializer. |
| 5181 |
InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 5182 |
|
| 5183 |
if (getLangOpts().OpenCL) |
| 5184 |
deduceOpenCLAddressSpace(VarSpec); |
| 5185 |
|
| 5186 |
return VarSpec; |
| 5187 |
} |
| 5188 |
|
| 5189 |
/// BuildVariableInstantiation - Used after a new variable has been created. |
| 5190 |
/// Sets basic variable data and decides whether to postpone the |
| 5191 |
/// variable instantiation. |
| 5192 |
void Sema::BuildVariableInstantiation( |
| 5193 |
VarDecl *NewVar, VarDecl *OldVar, |
| 5194 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 5195 |
LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 5196 |
LocalInstantiationScope *StartingScope, |
| 5197 |
bool InstantiatingVarTemplate, |
| 5198 |
VarTemplateSpecializationDecl *PrevDeclForVarTemplateSpecialization) { |
| 5199 |
// Instantiating a partial specialization to produce a partial |
| 5200 |
// specialization. |
| 5201 |
bool InstantiatingVarTemplatePartialSpec = |
| 5202 |
isa<VarTemplatePartialSpecializationDecl>(OldVar) && |
| 5203 |
isa<VarTemplatePartialSpecializationDecl>(NewVar); |
| 5204 |
// Instantiating from a variable template (or partial specialization) to |
| 5205 |
// produce a variable template specialization. |
| 5206 |
bool InstantiatingSpecFromTemplate = |
| 5207 |
isa<VarTemplateSpecializationDecl>(NewVar) && |
| 5208 |
(OldVar->getDescribedVarTemplate() || |
| 5209 |
isa<VarTemplatePartialSpecializationDecl>(OldVar)); |
| 5210 |
|
| 5211 |
// If we are instantiating a local extern declaration, the |
| 5212 |
// instantiation belongs lexically to the containing function. |
| 5213 |
// If we are instantiating a static data member defined |
| 5214 |
// out-of-line, the instantiation will have the same lexical |
| 5215 |
// context (which will be a namespace scope) as the template. |
| 5216 |
if (OldVar->isLocalExternDecl()) { |
| 5217 |
NewVar->setLocalExternDecl(); |
| 5218 |
NewVar->setLexicalDeclContext(Owner); |
| 5219 |
} else if (OldVar->isOutOfLine()) |
| 5220 |
NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 5221 |
NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 5222 |
NewVar->setInitStyle(OldVar->getInitStyle()); |
| 5223 |
NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
| 5224 |
NewVar->setObjCForDecl(OldVar->isObjCForDecl()); |
| 5225 |
NewVar->setConstexpr(OldVar->isConstexpr()); |
| 5226 |
NewVar->setInitCapture(OldVar->isInitCapture()); |
| 5227 |
NewVar->setPreviousDeclInSameBlockScope( |
| 5228 |
OldVar->isPreviousDeclInSameBlockScope()); |
| 5229 |
NewVar->setAccess(OldVar->getAccess()); |
| 5230 |
|
| 5231 |
if (!OldVar->isStaticDataMember()) { |
| 5232 |
if (OldVar->isUsed(false)) |
| 5233 |
NewVar->setIsUsed(); |
| 5234 |
NewVar->setReferenced(OldVar->isReferenced()); |
| 5235 |
} |
| 5236 |
|
| 5237 |
InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 5238 |
|
| 5239 |
LookupResult Previous( |
| 5240 |
*this, NewVar->getDeclName(), NewVar->getLocation(), |
| 5241 |
NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 5242 |
: Sema::LookupOrdinaryName, |
| 5243 |
NewVar->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 5244 |
: forRedeclarationInCurContext()); |
| 5245 |
|
| 5246 |
if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 5247 |
(!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 5248 |
OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
| 5249 |
// We have a previous declaration. Use that one, so we merge with the |
| 5250 |
// right type. |
| 5251 |
if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 5252 |
NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 5253 |
Previous.addDecl(NewPrev); |
| 5254 |
} else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 5255 |
OldVar->hasLinkage()) { |
| 5256 |
LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
| 5257 |
} else if (PrevDeclForVarTemplateSpecialization) { |
| 5258 |
Previous.addDecl(PrevDeclForVarTemplateSpecialization); |
| 5259 |
} |
| 5260 |
CheckVariableDeclaration(NewVar, Previous); |
| 5261 |
|
| 5262 |
if (!InstantiatingVarTemplate) { |
| 5263 |
NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 5264 |
if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
| 5265 |
NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
| 5266 |
} |
| 5267 |
|
| 5268 |
if (!OldVar->isOutOfLine()) { |
| 5269 |
if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 5270 |
CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 5271 |
} |
| 5272 |
|
| 5273 |
// Link instantiations of static data members back to the template from |
| 5274 |
// which they were instantiated. |
| 5275 |
// |
| 5276 |
// Don't do this when instantiating a template (we link the template itself |
| 5277 |
// back in that case) nor when instantiating a static data member template |
| 5278 |
// (that's not a member specialization). |
| 5279 |
if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate && |
| 5280 |
!InstantiatingSpecFromTemplate) |
| 5281 |
NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 5282 |
TSK_ImplicitInstantiation); |
| 5283 |
|
| 5284 |
// If the pattern is an (in-class) explicit specialization, then the result |
| 5285 |
// is also an explicit specialization. |
| 5286 |
if (VarTemplateSpecializationDecl *OldVTSD = |
| 5287 |
dyn_cast<VarTemplateSpecializationDecl>(OldVar)) { |
| 5288 |
if (OldVTSD->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 5289 |
!isa<VarTemplatePartialSpecializationDecl>(OldVTSD)) |
| 5290 |
cast<VarTemplateSpecializationDecl>(NewVar)->setSpecializationKind( |
| 5291 |
TSK_ExplicitSpecialization); |
| 5292 |
} |
| 5293 |
|
| 5294 |
// Forward the mangling number from the template to the instantiated decl. |
| 5295 |
Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
| 5296 |
Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
| 5297 |
|
| 5298 |
// Figure out whether to eagerly instantiate the initializer. |
| 5299 |
if (InstantiatingVarTemplate || InstantiatingVarTemplatePartialSpec) { |
| 5300 |
// We're producing a template. Don't instantiate the initializer yet. |
| 5301 |
} else if (NewVar->getType()->isUndeducedType()) { |
| 5302 |
// We need the type to complete the declaration of the variable. |
| 5303 |
InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 5304 |
} else if (InstantiatingSpecFromTemplate || |
| 5305 |
(OldVar->isInline() && OldVar->isThisDeclarationADefinition() && |
| 5306 |
!NewVar->isThisDeclarationADefinition())) { |
| 5307 |
// Delay instantiation of the initializer for variable template |
| 5308 |
// specializations or inline static data members until a definition of the |
| 5309 |
// variable is needed. |
| 5310 |
} else { |
| 5311 |
InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 5312 |
} |
| 5313 |
|
| 5314 |
// Diagnose unused local variables with dependent types, where the diagnostic |
| 5315 |
// will have been deferred. |
| 5316 |
if (!NewVar->isInvalidDecl() && |
| 5317 |
NewVar->getDeclContext()->isFunctionOrMethod() && |
| 5318 |
OldVar->getType()->isDependentType()) |
| 5319 |
DiagnoseUnusedDecl(NewVar); |
| 5320 |
} |
| 5321 |
|
| 5322 |
/// Instantiate the initializer of a variable. |
| 5323 |
void Sema::InstantiateVariableInitializer( |
| 5324 |
VarDecl *Var, VarDecl *OldVar, |
| 5325 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 5326 |
if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 5327 |
L->VariableDefinitionInstantiated(Var); |
| 5328 |
|
| 5329 |
// We propagate the 'inline' flag with the initializer, because it |
| 5330 |
// would otherwise imply that the variable is a definition for a |
| 5331 |
// non-static data member. |
| 5332 |
if (OldVar->isInlineSpecified()) |
| 5333 |
Var->setInlineSpecified(); |
| 5334 |
else if (OldVar->isInline()) |
| 5335 |
Var->setImplicitlyInline(); |
| 5336 |
|
| 5337 |
if (OldVar->getInit()) { |
| 5338 |
EnterExpressionEvaluationContext Evaluated( |
| 5339 |
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var); |
| 5340 |
|
| 5341 |
// Instantiate the initializer. |
| 5342 |
ExprResult Init; |
| 5343 |
|
| 5344 |
{ |
| 5345 |
ContextRAII SwitchContext(*this, Var->getDeclContext()); |
| 5346 |
Init = SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 5347 |
OldVar->getInitStyle() == VarDecl::CallInit); |
| 5348 |
} |
| 5349 |
|
| 5350 |
if (!Init.isInvalid()) { |
| 5351 |
Expr *InitExpr = Init.get(); |
| 5352 |
|
| 5353 |
if (Var->hasAttr<DLLImportAttr>() && |
| 5354 |
(!InitExpr || |
| 5355 |
!InitExpr->isConstantInitializer(getASTContext(), false))) { |
| 5356 |
// Do not dynamically initialize dllimport variables. |
| 5357 |
} else if (InitExpr) { |
| 5358 |
bool DirectInit = OldVar->isDirectInit(); |
| 5359 |
AddInitializerToDecl(Var, InitExpr, DirectInit); |
| 5360 |
} else |
| 5361 |
ActOnUninitializedDecl(Var); |
| 5362 |
} else { |
| 5363 |
// FIXME: Not too happy about invalidating the declaration |
| 5364 |
// because of a bogus initializer. |
| 5365 |
Var->setInvalidDecl(); |
| 5366 |
} |
| 5367 |
} else { |
| 5368 |
// `inline` variables are a definition and declaration all in one; we won't |
| 5369 |
// pick up an initializer from anywhere else. |
| 5370 |
if (Var->isStaticDataMember() && !Var->isInline()) { |
| 5371 |
if (!Var->isOutOfLine()) |
| 5372 |
return; |
| 5373 |
|
| 5374 |
// If the declaration inside the class had an initializer, don't add |
| 5375 |
// another one to the out-of-line definition. |
| 5376 |
if (OldVar->getFirstDecl()->hasInit()) |
| 5377 |
return; |
| 5378 |
} |
| 5379 |
|
| 5380 |
// We'll add an initializer to a for-range declaration later. |
| 5381 |
if (Var->isCXXForRangeDecl() || Var->isObjCForDecl()) |
| 5382 |
return; |
| 5383 |
|
| 5384 |
ActOnUninitializedDecl(Var); |
| 5385 |
} |
| 5386 |
|
| 5387 |
if (getLangOpts().CUDA) |
| 5388 |
checkAllowedCUDAInitializer(Var); |
| 5389 |
} |
| 5390 |
|
| 5391 |
/// Instantiate the definition of the given variable from its |
| 5392 |
/// template. |
| 5393 |
/// |
| 5394 |
/// \param PointOfInstantiation the point at which the instantiation was |
| 5395 |
/// required. Note that this is not precisely a "point of instantiation" |
| 5396 |
/// for the variable, but it's close. |
| 5397 |
/// |
| 5398 |
/// \param Var the already-instantiated declaration of a templated variable. |
| 5399 |
/// |
| 5400 |
/// \param Recursive if true, recursively instantiates any functions that |
| 5401 |
/// are required by this instantiation. |
| 5402 |
/// |
| 5403 |
/// \param DefinitionRequired if true, then we are performing an explicit |
| 5404 |
/// instantiation where a definition of the variable is required. Complain |
| 5405 |
/// if there is no such definition. |
| 5406 |
void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 5407 |
VarDecl *Var, bool Recursive, |
| 5408 |
bool DefinitionRequired, bool AtEndOfTU) { |
| 5409 |
if (Var->isInvalidDecl()) |
| 5410 |
return; |
| 5411 |
|
| 5412 |
// Never instantiate an explicitly-specialized entity. |
| 5413 |
TemplateSpecializationKind TSK = |
| 5414 |
Var->getTemplateSpecializationKindForInstantiation(); |
| 5415 |
if (TSK == TSK_ExplicitSpecialization) |
| 5416 |
return; |
| 5417 |
|
| 5418 |
// Find the pattern and the arguments to substitute into it. |
| 5419 |
VarDecl *PatternDecl = Var->getTemplateInstantiationPattern(); |
| 5420 |
assert(PatternDecl && "no pattern for templated variable"); |
| 5421 |
MultiLevelTemplateArgumentList TemplateArgs = |
| 5422 |
getTemplateInstantiationArgs(Var); |
| 5423 |
|
| 5424 |
VarTemplateSpecializationDecl *VarSpec = |
| 5425 |
dyn_cast<VarTemplateSpecializationDecl>(Var); |
| 5426 |
if (VarSpec) { |
| 5427 |
// If this is a static data member template, there might be an |
| 5428 |
// uninstantiated initializer on the declaration. If so, instantiate |
| 5429 |
// it now. |
| 5430 |
// |
| 5431 |
// FIXME: This largely duplicates what we would do below. The difference |
| 5432 |
// is that along this path we may instantiate an initializer from an |
| 5433 |
// in-class declaration of the template and instantiate the definition |
| 5434 |
// from a separate out-of-class definition. |
| 5435 |
if (PatternDecl->isStaticDataMember() && |
| 5436 |
(PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
| 5437 |
!Var->hasInit()) { |
| 5438 |
// FIXME: Factor out the duplicated instantiation context setup/tear down |
| 5439 |
// code here. |
| 5440 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 5441 |
if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| 5442 |
return; |
| 5443 |
PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
| 5444 |
"instantiating variable initializer"); |
| 5445 |
|
| 5446 |
// The instantiation is visible here, even if it was first declared in an |
| 5447 |
// unimported module. |
| 5448 |
Var->setVisibleDespiteOwningModule(); |
| 5449 |
|
| 5450 |
// If we're performing recursive template instantiation, create our own |
| 5451 |
// queue of pending implicit instantiations that we will instantiate |
| 5452 |
// later, while we're still within our own instantiation context. |
| 5453 |
GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 5454 |
/*Enabled=*/Recursive); |
| 5455 |
LocalInstantiationScope Local(*this); |
| 5456 |
LocalEagerInstantiationScope LocalInstantiations(*this); |
| 5457 |
|
| 5458 |
// Enter the scope of this instantiation. We don't use |
| 5459 |
// PushDeclContext because we don't have a scope. |
| 5460 |
ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 5461 |
InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 5462 |
PreviousContext.pop(); |
| 5463 |
|
| 5464 |
// This variable may have local implicit instantiations that need to be |
| 5465 |
// instantiated within this scope. |
| 5466 |
LocalInstantiations.perform(); |
| 5467 |
Local.Exit(); |
| 5468 |
GlobalInstantiations.perform(); |
| 5469 |
} |
| 5470 |
} else { |
| 5471 |
assert(Var->isStaticDataMember() && PatternDecl->isStaticDataMember() && |
| 5472 |
"not a static data member?"); |
| 5473 |
} |
| 5474 |
|
| 5475 |
VarDecl *Def = PatternDecl->getDefinition(getASTContext()); |
| 5476 |
|
| 5477 |
// If we don't have a definition of the variable template, we won't perform |
| 5478 |
// any instantiation. Rather, we rely on the user to instantiate this |
| 5479 |
// definition (or provide a specialization for it) in another translation |
| 5480 |
// unit. |
| 5481 |
if (!Def && !DefinitionRequired) { |
| 5482 |
if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 5483 |
PendingInstantiations.push_back( |
| 5484 |
std::make_pair(Var, PointOfInstantiation)); |
| 5485 |
} else if (TSK == TSK_ImplicitInstantiation) { |
| 5486 |
// Warn about missing definition at the end of translation unit. |
| 5487 |
if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
| 5488 |
!getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
| 5489 |
Diag(PointOfInstantiation, diag::warn_var_template_missing) |
| 5490 |
<< Var; |
| 5491 |
Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 5492 |
if (getLangOpts().CPlusPlus11) |
| 5493 |
Diag(PointOfInstantiation, diag::note_inst_declaration_hint) << Var; |
| 5494 |
} |
| 5495 |
return; |
| 5496 |
} |
| 5497 |
} |
| 5498 |
|
| 5499 |
// FIXME: We need to track the instantiation stack in order to know which |
| 5500 |
// definitions should be visible within this instantiation. |
| 5501 |
// FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember(). |
| 5502 |
if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Var, |
| 5503 |
/*InstantiatedFromMember*/false, |
| 5504 |
PatternDecl, Def, TSK, |
| 5505 |
/*Complain*/DefinitionRequired)) |
| 5506 |
return; |
| 5507 |
|
| 5508 |
// C++11 [temp.explicit]p10: |
| 5509 |
// Except for inline functions, const variables of literal types, variables |
| 5510 |
// of reference types, [...] explicit instantiation declarations |
| 5511 |
// have the effect of suppressing the implicit instantiation of the entity |
| 5512 |
// to which they refer. |
| 5513 |
// |
| 5514 |
// FIXME: That's not exactly the same as "might be usable in constant |
| 5515 |
// expressions", which only allows constexpr variables and const integral |
| 5516 |
// types, not arbitrary const literal types. |
| 5517 |
if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 5518 |
!Var->mightBeUsableInConstantExpressions(getASTContext())) |
| 5519 |
return; |
| 5520 |
|
| 5521 |
// Make sure to pass the instantiated variable to the consumer at the end. |
| 5522 |
struct PassToConsumerRAII { |
| 5523 |
ASTConsumer &Consumer; |
| 5524 |
VarDecl *Var; |
| 5525 |
|
| 5526 |
PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 5527 |
: Consumer(Consumer), Var(Var) { } |
| 5528 |
|
| 5529 |
~PassToConsumerRAII() { |
| 5530 |
Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
| 5531 |
} |
| 5532 |
} PassToConsumerRAII(Consumer, Var); |
| 5533 |
|
| 5534 |
// If we already have a definition, we're done. |
| 5535 |
if (VarDecl *Def = Var->getDefinition()) { |
| 5536 |
// We may be explicitly instantiating something we've already implicitly |
| 5537 |
// instantiated. |
| 5538 |
Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 5539 |
PointOfInstantiation); |
| 5540 |
return; |
| 5541 |
} |
| 5542 |
|
| 5543 |
InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 5544 |
if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| 5545 |
return; |
| 5546 |
PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
| 5547 |
"instantiating variable definition"); |
| 5548 |
|
| 5549 |
// If we're performing recursive template instantiation, create our own |
| 5550 |
// queue of pending implicit instantiations that we will instantiate later, |
| 5551 |
// while we're still within our own instantiation context. |
| 5552 |
GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 5553 |
/*Enabled=*/Recursive); |
| 5554 |
|
| 5555 |
// Enter the scope of this instantiation. We don't use |
| 5556 |
// PushDeclContext because we don't have a scope. |
| 5557 |
ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 5558 |
LocalInstantiationScope Local(*this); |
| 5559 |
|
| 5560 |
LocalEagerInstantiationScope LocalInstantiations(*this); |
| 5561 |
|
| 5562 |
VarDecl *OldVar = Var; |
| 5563 |
if (Def->isStaticDataMember() && !Def->isOutOfLine()) { |
| 5564 |
// We're instantiating an inline static data member whose definition was |
| 5565 |
// provided inside the class. |
| 5566 |
InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 5567 |
} else if (!VarSpec) { |
| 5568 |
Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
| 5569 |
TemplateArgs)); |
| 5570 |
} else if (Var->isStaticDataMember() && |
| 5571 |
Var->getLexicalDeclContext()->isRecord()) { |
| 5572 |
// We need to instantiate the definition of a static data member template, |
| 5573 |
// and all we have is the in-class declaration of it. Instantiate a separate |
| 5574 |
// declaration of the definition. |
| 5575 |
TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 5576 |
TemplateArgs); |
| 5577 |
|
| 5578 |
TemplateArgumentListInfo TemplateArgInfo; |
| 5579 |
if (const ASTTemplateArgumentListInfo *ArgInfo = |
| 5580 |
VarSpec->getTemplateArgsInfo()) { |
| 5581 |
TemplateArgInfo.setLAngleLoc(ArgInfo->getLAngleLoc()); |
| 5582 |
TemplateArgInfo.setRAngleLoc(ArgInfo->getRAngleLoc()); |
| 5583 |
for (const TemplateArgumentLoc &Arg : ArgInfo->arguments()) |
| 5584 |
TemplateArgInfo.addArgument(Arg); |
| 5585 |
} |
| 5586 |
|
| 5587 |
Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
| 5588 |
VarSpec->getSpecializedTemplate(), Def, TemplateArgInfo, |
| 5589 |
VarSpec->getTemplateArgs().asArray(), VarSpec)); |
| 5590 |
if (Var) { |
| 5591 |
llvm::PointerUnion<VarTemplateDecl *, |
| 5592 |
VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 5593 |
VarSpec->getSpecializedTemplateOrPartial(); |
| 5594 |
if (VarTemplatePartialSpecializationDecl *Partial = |
| 5595 |
PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 5596 |
cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 5597 |
Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 5598 |
|
| 5599 |
// Attach the initializer. |
| 5600 |
InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 5601 |
} |
| 5602 |
} else |
| 5603 |
// Complete the existing variable's definition with an appropriately |
| 5604 |
// substituted type and initializer. |
| 5605 |
Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
| 5606 |
|
| 5607 |
PreviousContext.pop(); |
| 5608 |
|
| 5609 |
if (Var) { |
| 5610 |
PassToConsumerRAII.Var = Var; |
| 5611 |
Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 5612 |
OldVar->getPointOfInstantiation()); |
| 5613 |
} |
| 5614 |
|
| 5615 |
// This variable may have local implicit instantiations that need to be |
| 5616 |
// instantiated within this scope. |
| 5617 |
LocalInstantiations.perform(); |
| 5618 |
Local.Exit(); |
| 5619 |
GlobalInstantiations.perform(); |
| 5620 |
} |
| 5621 |
|
| 5622 |
void |
| 5623 |
Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 5624 |
const CXXConstructorDecl *Tmpl, |
| 5625 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 5626 |
|
| 5627 |
SmallVector<CXXCtorInitializer*, 4> NewInits; |
| 5628 |
bool AnyErrors = Tmpl->isInvalidDecl(); |
| 5629 |
|
| 5630 |
// Instantiate all the initializers. |
| 5631 |
for (const auto *Init : Tmpl->inits()) { |
| 5632 |
// Only instantiate written initializers, let Sema re-construct implicit |
| 5633 |
// ones. |
| 5634 |
if (!Init->isWritten()) |
| 5635 |
continue; |
| 5636 |
|
| 5637 |
SourceLocation EllipsisLoc; |
| 5638 |
|
| 5639 |
if (Init->isPackExpansion()) { |
| 5640 |
// This is a pack expansion. We should expand it now. |
| 5641 |
TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
| 5642 |
SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
| 5643 |
collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
| 5644 |
collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
| 5645 |
bool ShouldExpand = false; |
| 5646 |
bool RetainExpansion = false; |
| 5647 |
std::optional<unsigned> NumExpansions; |
| 5648 |
if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
| 5649 |
BaseTL.getSourceRange(), |
| 5650 |
Unexpanded, |
| 5651 |
TemplateArgs, ShouldExpand, |
| 5652 |
RetainExpansion, |
| 5653 |
NumExpansions)) { |
| 5654 |
AnyErrors = true; |
| 5655 |
New->setInvalidDecl(); |
| 5656 |
continue; |
| 5657 |
} |
| 5658 |
assert(ShouldExpand && "Partial instantiation of base initializer?"); |
| 5659 |
|
| 5660 |
// Loop over all of the arguments in the argument pack(s), |
| 5661 |
for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 5662 |
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 5663 |
|
| 5664 |
// Instantiate the initializer. |
| 5665 |
ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 5666 |
/*CXXDirectInit=*/true); |
| 5667 |
if (TempInit.isInvalid()) { |
| 5668 |
AnyErrors = true; |
| 5669 |
break; |
| 5670 |
} |
| 5671 |
|
| 5672 |
// Instantiate the base type. |
| 5673 |
TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
| 5674 |
TemplateArgs, |
| 5675 |
Init->getSourceLocation(), |
| 5676 |
New->getDeclName()); |
| 5677 |
if (!BaseTInfo) { |
| 5678 |
AnyErrors = true; |
| 5679 |
break; |
| 5680 |
} |
| 5681 |
|
| 5682 |
// Build the initializer. |
| 5683 |
MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
| 5684 |
BaseTInfo, TempInit.get(), |
| 5685 |
New->getParent(), |
| 5686 |
SourceLocation()); |
| 5687 |
if (NewInit.isInvalid()) { |
| 5688 |
AnyErrors = true; |
| 5689 |
break; |
| 5690 |
} |
| 5691 |
|
| 5692 |
NewInits.push_back(NewInit.get()); |
| 5693 |
} |
| 5694 |
|
| 5695 |
continue; |
| 5696 |
} |
| 5697 |
|
| 5698 |
// Instantiate the initializer. |
| 5699 |
ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 5700 |
/*CXXDirectInit=*/true); |
| 5701 |
if (TempInit.isInvalid()) { |
| 5702 |
AnyErrors = true; |
| 5703 |
continue; |
| 5704 |
} |
| 5705 |
|
| 5706 |
MemInitResult NewInit; |
| 5707 |
if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 5708 |
TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 5709 |
TemplateArgs, |
| 5710 |
Init->getSourceLocation(), |
| 5711 |
New->getDeclName()); |
| 5712 |
if (!TInfo) { |
| 5713 |
AnyErrors = true; |
| 5714 |
New->setInvalidDecl(); |
| 5715 |
continue; |
| 5716 |
} |
| 5717 |
|
| 5718 |
if (Init->isBaseInitializer()) |
| 5719 |
NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
| 5720 |
New->getParent(), EllipsisLoc); |
| 5721 |
else |
| 5722 |
NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
| 5723 |
cast<CXXRecordDecl>(CurContext->getParent())); |
| 5724 |
} else if (Init->isMemberInitializer()) { |
| 5725 |
FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
| 5726 |
Init->getMemberLocation(), |
| 5727 |
Init->getMember(), |
| 5728 |
TemplateArgs)); |
| 5729 |
if (!Member) { |
| 5730 |
AnyErrors = true; |
| 5731 |
New->setInvalidDecl(); |
| 5732 |
continue; |
| 5733 |
} |
| 5734 |
|
| 5735 |
NewInit = BuildMemberInitializer(Member, TempInit.get(), |
| 5736 |
Init->getSourceLocation()); |
| 5737 |
} else if (Init->isIndirectMemberInitializer()) { |
| 5738 |
IndirectFieldDecl *IndirectMember = |
| 5739 |
cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
| 5740 |
Init->getMemberLocation(), |
| 5741 |
Init->getIndirectMember(), TemplateArgs)); |
| 5742 |
|
| 5743 |
if (!IndirectMember) { |
| 5744 |
AnyErrors = true; |
| 5745 |
New->setInvalidDecl(); |
| 5746 |
continue; |
| 5747 |
} |
| 5748 |
|
| 5749 |
NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
| 5750 |
Init->getSourceLocation()); |
| 5751 |
} |
| 5752 |
|
| 5753 |
if (NewInit.isInvalid()) { |
| 5754 |
AnyErrors = true; |
| 5755 |
New->setInvalidDecl(); |
| 5756 |
} else { |
| 5757 |
NewInits.push_back(NewInit.get()); |
| 5758 |
} |
| 5759 |
} |
| 5760 |
|
| 5761 |
// Assign all the initializers to the new constructor. |
| 5762 |
ActOnMemInitializers(New, |
| 5763 |
/*FIXME: ColonLoc */ |
| 5764 |
SourceLocation(), |
| 5765 |
NewInits, |
| 5766 |
AnyErrors); |
| 5767 |
} |
| 5768 |
|
| 5769 |
// TODO: this could be templated if the various decl types used the |
| 5770 |
// same method name. |
| 5771 |
static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 5772 |
ClassTemplateDecl *Instance) { |
| 5773 |
Pattern = Pattern->getCanonicalDecl(); |
| 5774 |
|
| 5775 |
do { |
| 5776 |
Instance = Instance->getCanonicalDecl(); |
| 5777 |
if (Pattern == Instance) return true; |
| 5778 |
Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 5779 |
} while (Instance); |
| 5780 |
|
| 5781 |
return false; |
| 5782 |
} |
| 5783 |
|
| 5784 |
static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 5785 |
FunctionTemplateDecl *Instance) { |
| 5786 |
Pattern = Pattern->getCanonicalDecl(); |
| 5787 |
|
| 5788 |
do { |
| 5789 |
Instance = Instance->getCanonicalDecl(); |
| 5790 |
if (Pattern == Instance) return true; |
| 5791 |
Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 5792 |
} while (Instance); |
| 5793 |
|
| 5794 |
return false; |
| 5795 |
} |
| 5796 |
|
| 5797 |
static bool |
| 5798 |
isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 5799 |
ClassTemplatePartialSpecializationDecl *Instance) { |
| 5800 |
Pattern |
| 5801 |
= cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 5802 |
do { |
| 5803 |
Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 5804 |
Instance->getCanonicalDecl()); |
| 5805 |
if (Pattern == Instance) |
| 5806 |
return true; |
| 5807 |
Instance = Instance->getInstantiatedFromMember(); |
| 5808 |
} while (Instance); |
| 5809 |
|
| 5810 |
return false; |
| 5811 |
} |
| 5812 |
|
| 5813 |
static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 5814 |
CXXRecordDecl *Instance) { |
| 5815 |
Pattern = Pattern->getCanonicalDecl(); |
| 5816 |
|
| 5817 |
do { |
| 5818 |
Instance = Instance->getCanonicalDecl(); |
| 5819 |
if (Pattern == Instance) return true; |
| 5820 |
Instance = Instance->getInstantiatedFromMemberClass(); |
| 5821 |
} while (Instance); |
| 5822 |
|
| 5823 |
return false; |
| 5824 |
} |
| 5825 |
|
| 5826 |
static bool isInstantiationOf(FunctionDecl *Pattern, |
| 5827 |
FunctionDecl *Instance) { |
| 5828 |
Pattern = Pattern->getCanonicalDecl(); |
| 5829 |
|
| 5830 |
do { |
| 5831 |
Instance = Instance->getCanonicalDecl(); |
| 5832 |
if (Pattern == Instance) return true; |
| 5833 |
Instance = Instance->getInstantiatedFromMemberFunction(); |
| 5834 |
} while (Instance); |
| 5835 |
|
| 5836 |
return false; |
| 5837 |
} |
| 5838 |
|
| 5839 |
static bool isInstantiationOf(EnumDecl *Pattern, |
| 5840 |
EnumDecl *Instance) { |
| 5841 |
Pattern = Pattern->getCanonicalDecl(); |
| 5842 |
|
| 5843 |
do { |
| 5844 |
Instance = Instance->getCanonicalDecl(); |
| 5845 |
if (Pattern == Instance) return true; |
| 5846 |
Instance = Instance->getInstantiatedFromMemberEnum(); |
| 5847 |
} while (Instance); |
| 5848 |
|
| 5849 |
return false; |
| 5850 |
} |
| 5851 |
|
| 5852 |
static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 5853 |
UsingShadowDecl *Instance, |
| 5854 |
ASTContext &C) { |
| 5855 |
return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), |
| 5856 |
Pattern); |
| 5857 |
} |
| 5858 |
|
| 5859 |
static bool isInstantiationOf(UsingDecl *Pattern, UsingDecl *Instance, |
| 5860 |
ASTContext &C) { |
| 5861 |
return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
| 5862 |
} |
| 5863 |
|
| 5864 |
template<typename T> |
| 5865 |
static bool isInstantiationOfUnresolvedUsingDecl(T *Pattern, Decl *Other, |
| 5866 |
ASTContext &Ctx) { |
| 5867 |
// An unresolved using declaration can instantiate to an unresolved using |
| 5868 |
// declaration, or to a using declaration or a using declaration pack. |
| 5869 |
// |
| 5870 |
// Multiple declarations can claim to be instantiated from an unresolved |
| 5871 |
// using declaration if it's a pack expansion. We want the UsingPackDecl |
| 5872 |
// in that case, not the individual UsingDecls within the pack. |
| 5873 |
bool OtherIsPackExpansion; |
| 5874 |
NamedDecl *OtherFrom; |
| 5875 |
if (auto *OtherUUD = dyn_cast<T>(Other)) { |
| 5876 |
OtherIsPackExpansion = OtherUUD->isPackExpansion(); |
| 5877 |
OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUUD); |
| 5878 |
} else if (auto *OtherUPD = dyn_cast<UsingPackDecl>(Other)) { |
| 5879 |
OtherIsPackExpansion = true; |
| 5880 |
OtherFrom = OtherUPD->getInstantiatedFromUsingDecl(); |
| 5881 |
} else if (auto *OtherUD = dyn_cast<UsingDecl>(Other)) { |
| 5882 |
OtherIsPackExpansion = false; |
| 5883 |
OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUD); |
| 5884 |
} else { |
| 5885 |
return false; |
| 5886 |
} |
| 5887 |
return Pattern->isPackExpansion() == OtherIsPackExpansion && |
| 5888 |
declaresSameEntity(OtherFrom, Pattern); |
| 5889 |
} |
| 5890 |
|
| 5891 |
static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 5892 |
VarDecl *Instance) { |
| 5893 |
assert(Instance->isStaticDataMember()); |
| 5894 |
|
| 5895 |
Pattern = Pattern->getCanonicalDecl(); |
| 5896 |
|
| 5897 |
do { |
| 5898 |
Instance = Instance->getCanonicalDecl(); |
| 5899 |
if (Pattern == Instance) return true; |
| 5900 |
Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 5901 |
} while (Instance); |
| 5902 |
|
| 5903 |
return false; |
| 5904 |
} |
| 5905 |
|
| 5906 |
// Other is the prospective instantiation |
| 5907 |
// D is the prospective pattern |
| 5908 |
static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
| 5909 |
if (auto *UUD = dyn_cast<UnresolvedUsingTypenameDecl>(D)) |
| 5910 |
return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
| 5911 |
|
| 5912 |
if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(D)) |
| 5913 |
return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
| 5914 |
|
| 5915 |
if (D->getKind() != Other->getKind()) |
| 5916 |
return false; |
| 5917 |
|
| 5918 |
if (auto *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 5919 |
return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
| 5920 |
|
| 5921 |
if (auto *Function = dyn_cast<FunctionDecl>(Other)) |
| 5922 |
return isInstantiationOf(cast<FunctionDecl>(D), Function); |
| 5923 |
|
| 5924 |
if (auto *Enum = dyn_cast<EnumDecl>(Other)) |
| 5925 |
return isInstantiationOf(cast<EnumDecl>(D), Enum); |
| 5926 |
|
| 5927 |
if (auto *Var = dyn_cast<VarDecl>(Other)) |
| 5928 |
if (Var->isStaticDataMember()) |
| 5929 |
return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 5930 |
|
| 5931 |
if (auto *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 5932 |
return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
| 5933 |
|
| 5934 |
if (auto *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 5935 |
return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 5936 |
|
| 5937 |
if (auto *PartialSpec = |
| 5938 |
dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 5939 |
return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 5940 |
PartialSpec); |
| 5941 |
|
| 5942 |
if (auto *Field = dyn_cast<FieldDecl>(Other)) { |
| 5943 |
if (!Field->getDeclName()) { |
| 5944 |
// This is an unnamed field. |
| 5945 |
return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field), |
| 5946 |
cast<FieldDecl>(D)); |
| 5947 |
} |
| 5948 |
} |
| 5949 |
|
| 5950 |
if (auto *Using = dyn_cast<UsingDecl>(Other)) |
| 5951 |
return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 5952 |
|
| 5953 |
if (auto *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 5954 |
return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 5955 |
|
| 5956 |
return D->getDeclName() && |
| 5957 |
D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 5958 |
} |
| 5959 |
|
| 5960 |
template<typename ForwardIterator> |
| 5961 |
static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
| 5962 |
NamedDecl *D, |
| 5963 |
ForwardIterator first, |
| 5964 |
ForwardIterator last) { |
| 5965 |
for (; first != last; ++first) |
| 5966 |
if (isInstantiationOf(Ctx, D, *first)) |
| 5967 |
return cast<NamedDecl>(*first); |
| 5968 |
|
| 5969 |
return nullptr; |
| 5970 |
} |
| 5971 |
|
| 5972 |
/// Finds the instantiation of the given declaration context |
| 5973 |
/// within the current instantiation. |
| 5974 |
/// |
| 5975 |
/// \returns NULL if there was an error |
| 5976 |
DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
| 5977 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 5978 |
if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
| 5979 |
Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs, true); |
| 5980 |
return cast_or_null<DeclContext>(ID); |
| 5981 |
} else return DC; |
| 5982 |
} |
| 5983 |
|
| 5984 |
/// Determine whether the given context is dependent on template parameters at |
| 5985 |
/// level \p Level or below. |
| 5986 |
/// |
| 5987 |
/// Sometimes we only substitute an inner set of template arguments and leave |
| 5988 |
/// the outer templates alone. In such cases, contexts dependent only on the |
| 5989 |
/// outer levels are not effectively dependent. |
| 5990 |
static bool isDependentContextAtLevel(DeclContext *DC, unsigned Level) { |
| 5991 |
if (!DC->isDependentContext()) |
| 5992 |
return false; |
| 5993 |
if (!Level) |
| 5994 |
return true; |
| 5995 |
return cast<Decl>(DC)->getTemplateDepth() > Level; |
| 5996 |
} |
| 5997 |
|
| 5998 |
/// Find the instantiation of the given declaration within the |
| 5999 |
/// current instantiation. |
| 6000 |
/// |
| 6001 |
/// This routine is intended to be used when \p D is a declaration |
| 6002 |
/// referenced from within a template, that needs to mapped into the |
| 6003 |
/// corresponding declaration within an instantiation. For example, |
| 6004 |
/// given: |
| 6005 |
/// |
| 6006 |
/// \code |
| 6007 |
/// template<typename T> |
| 6008 |
/// struct X { |
| 6009 |
/// enum Kind { |
| 6010 |
/// KnownValue = sizeof(T) |
| 6011 |
/// }; |
| 6012 |
/// |
| 6013 |
/// bool getKind() const { return KnownValue; } |
| 6014 |
/// }; |
| 6015 |
/// |
| 6016 |
/// template struct X<int>; |
| 6017 |
/// \endcode |
| 6018 |
/// |
| 6019 |
/// In the instantiation of X<int>::getKind(), we need to map the \p |
| 6020 |
/// EnumConstantDecl for \p KnownValue (which refers to |
| 6021 |
/// X<T>::<Kind>::KnownValue) to its instantiation (X<int>::<Kind>::KnownValue). |
| 6022 |
/// \p FindInstantiatedDecl performs this mapping from within the instantiation |
| 6023 |
/// of X<int>. |
| 6024 |
NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
| 6025 |
const MultiLevelTemplateArgumentList &TemplateArgs, |
| 6026 |
bool FindingInstantiatedContext) { |
| 6027 |
DeclContext *ParentDC = D->getDeclContext(); |
| 6028 |
// Determine whether our parent context depends on any of the template |
| 6029 |
// arguments we're currently substituting. |
| 6030 |
bool ParentDependsOnArgs = isDependentContextAtLevel( |
| 6031 |
ParentDC, TemplateArgs.getNumRetainedOuterLevels()); |
| 6032 |
// FIXME: Parameters of pointer to functions (y below) that are themselves |
| 6033 |
// parameters (p below) can have their ParentDC set to the translation-unit |
| 6034 |
// - thus we can not consistently check if the ParentDC of such a parameter |
| 6035 |
// is Dependent or/and a FunctionOrMethod. |
| 6036 |
// For e.g. this code, during Template argument deduction tries to |
| 6037 |
// find an instantiated decl for (T y) when the ParentDC for y is |
| 6038 |
// the translation unit. |
| 6039 |
// e.g. template <class T> void Foo(auto (*p)(T y) -> decltype(y())) {} |
| 6040 |
// float baz(float(*)()) { return 0.0; } |
| 6041 |
// Foo(baz); |
| 6042 |
// The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 6043 |
// it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 6044 |
// For now: |
| 6045 |
// - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 6046 |
// whose type is not instantiation dependent, do nothing to the decl |
| 6047 |
// - otherwise find its instantiated decl. |
| 6048 |
if (isa<ParmVarDecl>(D) && !ParentDependsOnArgs && |
| 6049 |
!cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 6050 |
return D; |
| 6051 |
if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
| 6052 |
isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
| 6053 |
(ParentDependsOnArgs && (ParentDC->isFunctionOrMethod() || |
| 6054 |
isa<OMPDeclareReductionDecl>(ParentDC) || |
| 6055 |
isa<OMPDeclareMapperDecl>(ParentDC))) || |
| 6056 |
(isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda() && |
| 6057 |
cast<CXXRecordDecl>(D)->getTemplateDepth() > |
| 6058 |
TemplateArgs.getNumRetainedOuterLevels())) { |
| 6059 |
// D is a local of some kind. Look into the map of local |
| 6060 |
// declarations to their instantiations. |
| 6061 |
if (CurrentInstantiationScope) { |
| 6062 |
if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) { |
| 6063 |
if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 6064 |
return cast<NamedDecl>(FD); |
| 6065 |
|
| 6066 |
int PackIdx = ArgumentPackSubstitutionIndex; |
| 6067 |
assert(PackIdx != -1 && |
| 6068 |
"found declaration pack but not pack expanding"); |
| 6069 |
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 6070 |
return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 6071 |
} |
| 6072 |
} |
| 6073 |
|
| 6074 |
// If we're performing a partial substitution during template argument |
| 6075 |
// deduction, we may not have values for template parameters yet. They |
| 6076 |
// just map to themselves. |
| 6077 |
if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 6078 |
isa<TemplateTemplateParmDecl>(D)) |
| 6079 |
return D; |
| 6080 |
|
| 6081 |
if (D->isInvalidDecl()) |
| 6082 |
return nullptr; |
| 6083 |
|
| 6084 |
// Normally this function only searches for already instantiated declaration |
| 6085 |
// however we have to make an exclusion for local types used before |
| 6086 |
// definition as in the code: |
| 6087 |
// |
| 6088 |
// template<typename T> void f1() { |
| 6089 |
// void g1(struct x1); |
| 6090 |
// struct x1 {}; |
| 6091 |
// } |
| 6092 |
// |
| 6093 |
// In this case instantiation of the type of 'g1' requires definition of |
| 6094 |
// 'x1', which is defined later. Error recovery may produce an enum used |
| 6095 |
// before definition. In these cases we need to instantiate relevant |
| 6096 |
// declarations here. |
| 6097 |
bool NeedInstantiate = false; |
| 6098 |
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 6099 |
NeedInstantiate = RD->isLocalClass(); |
| 6100 |
else if (isa<TypedefNameDecl>(D) && |
| 6101 |
isa<CXXDeductionGuideDecl>(D->getDeclContext())) |
| 6102 |
NeedInstantiate = true; |
| 6103 |
else |
| 6104 |
NeedInstantiate = isa<EnumDecl>(D); |
| 6105 |
if (NeedInstantiate) { |
| 6106 |
Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 6107 |
CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 6108 |
return cast<TypeDecl>(Inst); |
| 6109 |
} |
| 6110 |
|
| 6111 |
// If we didn't find the decl, then we must have a label decl that hasn't |
| 6112 |
// been found yet. Lazily instantiate it and return it now. |
| 6113 |
assert(isa<LabelDecl>(D)); |
| 6114 |
|
| 6115 |
Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 6116 |
assert(Inst && "Failed to instantiate label??"); |
| 6117 |
|
| 6118 |
CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 6119 |
return cast<LabelDecl>(Inst); |
| 6120 |
} |
| 6121 |
|
| 6122 |
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 6123 |
if (!Record->isDependentContext()) |
| 6124 |
return D; |
| 6125 |
|
| 6126 |
// Determine whether this record is the "templated" declaration describing |
| 6127 |
// a class template or class template partial specialization. |
| 6128 |
ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| 6129 |
if (ClassTemplate) |
| 6130 |
ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 6131 |
else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 6132 |
= dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 6133 |
ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
| 6134 |
|
| 6135 |
// Walk the current context to find either the record or an instantiation of |
| 6136 |
// it. |
| 6137 |
DeclContext *DC = CurContext; |
| 6138 |
while (!DC->isFileContext()) { |
| 6139 |
// If we're performing substitution while we're inside the template |
| 6140 |
// definition, we'll find our own context. We're done. |
| 6141 |
if (DC->Equals(Record)) |
| 6142 |
return Record; |
| 6143 |
|
| 6144 |
if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 6145 |
// Check whether we're in the process of instantiating a class template |
| 6146 |
// specialization of the template we're mapping. |
| 6147 |
if (ClassTemplateSpecializationDecl *InstSpec |
| 6148 |
= dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 6149 |
ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 6150 |
if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 6151 |
return InstRecord; |
| 6152 |
} |
| 6153 |
|
| 6154 |
// Check whether we're in the process of instantiating a member class. |
| 6155 |
if (isInstantiationOf(Record, InstRecord)) |
| 6156 |
return InstRecord; |
| 6157 |
} |
| 6158 |
|
| 6159 |
// Move to the outer template scope. |
| 6160 |
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 6161 |
if (FD->getFriendObjectKind() && |
| 6162 |
FD->getNonTransparentDeclContext()->isFileContext()) { |
| 6163 |
DC = FD->getLexicalDeclContext(); |
| 6164 |
continue; |
| 6165 |
} |
| 6166 |
// An implicit deduction guide acts as if it's within the class template |
| 6167 |
// specialization described by its name and first N template params. |
| 6168 |
auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FD); |
| 6169 |
if (Guide && Guide->isImplicit()) { |
| 6170 |
TemplateDecl *TD = Guide->getDeducedTemplate(); |
| 6171 |
// Convert the arguments to an "as-written" list. |
| 6172 |
TemplateArgumentListInfo Args(Loc, Loc); |
| 6173 |
for (TemplateArgument Arg : TemplateArgs.getInnermost().take_front( |
| 6174 |
TD->getTemplateParameters()->size())) { |
| 6175 |
ArrayRef<TemplateArgument> Unpacked(Arg); |
| 6176 |
if (Arg.getKind() == TemplateArgument::Pack) |
| 6177 |
Unpacked = Arg.pack_elements(); |
| 6178 |
for (TemplateArgument UnpackedArg : Unpacked) |
| 6179 |
Args.addArgument( |
| 6180 |
getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); |
| 6181 |
} |
| 6182 |
QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); |
| 6183 |
if (T.isNull()) |
| 6184 |
return nullptr; |
| 6185 |
auto *SubstRecord = T->getAsCXXRecordDecl(); |
| 6186 |
assert(SubstRecord && "class template id not a class type?"); |
| 6187 |
// Check that this template-id names the primary template and not a |
| 6188 |
// partial or explicit specialization. (In the latter cases, it's |
| 6189 |
// meaningless to attempt to find an instantiation of D within the |
| 6190 |
// specialization.) |
| 6191 |
// FIXME: The standard doesn't say what should happen here. |
| 6192 |
if (FindingInstantiatedContext && |
| 6193 |
usesPartialOrExplicitSpecialization( |
| 6194 |
Loc, cast<ClassTemplateSpecializationDecl>(SubstRecord))) { |
| 6195 |
Diag(Loc, diag::err_specialization_not_primary_template) |
| 6196 |
<< T << (SubstRecord->getTemplateSpecializationKind() == |
| 6197 |
TSK_ExplicitSpecialization); |
| 6198 |
return nullptr; |
| 6199 |
} |
| 6200 |
DC = SubstRecord; |
| 6201 |
continue; |
| 6202 |
} |
| 6203 |
} |
| 6204 |
|
| 6205 |
DC = DC->getParent(); |
| 6206 |
} |
| 6207 |
|
| 6208 |
// Fall through to deal with other dependent record types (e.g., |
| 6209 |
// anonymous unions in class templates). |
| 6210 |
} |
| 6211 |
|
| 6212 |
if (!ParentDependsOnArgs) |
| 6213 |
return D; |
| 6214 |
|
| 6215 |
ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
| 6216 |
if (!ParentDC) |
| 6217 |
return nullptr; |
| 6218 |
|
| 6219 |
if (ParentDC != D->getDeclContext()) { |
| 6220 |
// We performed some kind of instantiation in the parent context, |
| 6221 |
// so now we need to look into the instantiated parent context to |
| 6222 |
// find the instantiation of the declaration D. |
| 6223 |
|
| 6224 |
// If our context used to be dependent, we may need to instantiate |
| 6225 |
// it before performing lookup into that context. |
| 6226 |
bool IsBeingInstantiated = false; |
| 6227 |
if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
| 6228 |
if (!Spec->isDependentContext()) { |
| 6229 |
QualType T = Context.getTypeDeclType(Spec); |
| 6230 |
const RecordType *Tag = T->getAs<RecordType>(); |
| 6231 |
assert(Tag && "type of non-dependent record is not a RecordType"); |
| 6232 |
if (Tag->isBeingDefined()) |
| 6233 |
IsBeingInstantiated = true; |
| 6234 |
if (!Tag->isBeingDefined() && |
| 6235 |
RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
| 6236 |
return nullptr; |
| 6237 |
|
| 6238 |
ParentDC = Tag->getDecl(); |
| 6239 |
} |
| 6240 |
} |
| 6241 |
|
| 6242 |
NamedDecl *Result = nullptr; |
| 6243 |
// FIXME: If the name is a dependent name, this lookup won't necessarily |
| 6244 |
// find it. Does that ever matter? |
| 6245 |
if (auto Name = D->getDeclName()) { |
| 6246 |
DeclarationNameInfo NameInfo(Name, D->getLocation()); |
| 6247 |
DeclarationNameInfo NewNameInfo = |
| 6248 |
SubstDeclarationNameInfo(NameInfo, TemplateArgs); |
| 6249 |
Name = NewNameInfo.getName(); |
| 6250 |
if (!Name) |
| 6251 |
return nullptr; |
| 6252 |
DeclContext::lookup_result Found = ParentDC->lookup(Name); |
| 6253 |
|
| 6254 |
Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
| 6255 |
} else { |
| 6256 |
// Since we don't have a name for the entity we're looking for, |
| 6257 |
// our only option is to walk through all of the declarations to |
| 6258 |
// find that name. This will occur in a few cases: |
| 6259 |
// |
| 6260 |
// - anonymous struct/union within a template |
| 6261 |
// - unnamed class/struct/union/enum within a template |
| 6262 |
// |
| 6263 |
// FIXME: Find a better way to find these instantiations! |
| 6264 |
Result = findInstantiationOf(Context, D, |
| 6265 |
ParentDC->decls_begin(), |
| 6266 |
ParentDC->decls_end()); |
| 6267 |
} |
| 6268 |
|
| 6269 |
if (!Result) { |
| 6270 |
if (isa<UsingShadowDecl>(D)) { |
| 6271 |
// UsingShadowDecls can instantiate to nothing because of using hiding. |
| 6272 |
} else if (hasUncompilableErrorOccurred()) { |
| 6273 |
// We've already complained about some ill-formed code, so most likely |
| 6274 |
// this declaration failed to instantiate. There's no point in |
| 6275 |
// complaining further, since this is normal in invalid code. |
| 6276 |
// FIXME: Use more fine-grained 'invalid' tracking for this. |
| 6277 |
} else if (IsBeingInstantiated) { |
| 6278 |
// The class in which this member exists is currently being |
| 6279 |
// instantiated, and we haven't gotten around to instantiating this |
| 6280 |
// member yet. This can happen when the code uses forward declarations |
| 6281 |
// of member classes, and introduces ordering dependencies via |
| 6282 |
// template instantiation. |
| 6283 |
Diag(Loc, diag::err_member_not_yet_instantiated) |
| 6284 |
<< D->getDeclName() |
| 6285 |
<< Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 6286 |
Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
| 6287 |
} else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 6288 |
// This enumeration constant was found when the template was defined, |
| 6289 |
// but can't be found in the instantiation. This can happen if an |
| 6290 |
// unscoped enumeration member is explicitly specialized. |
| 6291 |
EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 6292 |
EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 6293 |
TemplateArgs)); |
| 6294 |
assert(Spec->getTemplateSpecializationKind() == |
| 6295 |
TSK_ExplicitSpecialization); |
| 6296 |
Diag(Loc, diag::err_enumerator_does_not_exist) |
| 6297 |
<< D->getDeclName() |
| 6298 |
<< Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 6299 |
Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 6300 |
<< Context.getTypeDeclType(Spec); |
| 6301 |
} else { |
| 6302 |
// We should have found something, but didn't. |
| 6303 |
llvm_unreachable("Unable to find instantiation of declaration!"); |
| 6304 |
} |
| 6305 |
} |
| 6306 |
|
| 6307 |
D = Result; |
| 6308 |
} |
| 6309 |
|
| 6310 |
return D; |
| 6311 |
} |
| 6312 |
|
| 6313 |
/// Performs template instantiation for all implicit template |
| 6314 |
/// instantiations we have seen until this point. |
| 6315 |
void Sema::PerformPendingInstantiations(bool LocalOnly) { |
| 6316 |
std::deque<PendingImplicitInstantiation> delayedPCHInstantiations; |
| 6317 |
while (!PendingLocalImplicitInstantiations.empty() || |
| 6318 |
(!LocalOnly && !PendingInstantiations.empty())) { |
| 6319 |
PendingImplicitInstantiation Inst; |
| 6320 |
|
| 6321 |
if (PendingLocalImplicitInstantiations.empty()) { |
| 6322 |
Inst = PendingInstantiations.front(); |
| 6323 |
PendingInstantiations.pop_front(); |
| 6324 |
} else { |
| 6325 |
Inst = PendingLocalImplicitInstantiations.front(); |
| 6326 |
PendingLocalImplicitInstantiations.pop_front(); |
| 6327 |
} |
| 6328 |
|
| 6329 |
// Instantiate function definitions |
| 6330 |
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
| 6331 |
bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 6332 |
TSK_ExplicitInstantiationDefinition; |
| 6333 |
if (Function->isMultiVersion()) { |
| 6334 |
getASTContext().forEachMultiversionedFunctionVersion( |
| 6335 |
Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) { |
| 6336 |
InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true, |
| 6337 |
DefinitionRequired, true); |
| 6338 |
if (CurFD->isDefined()) |
| 6339 |
CurFD->setInstantiationIsPending(false); |
| 6340 |
}); |
| 6341 |
} else { |
| 6342 |
InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, Function, true, |
| 6343 |
DefinitionRequired, true); |
| 6344 |
if (Function->isDefined()) |
| 6345 |
Function->setInstantiationIsPending(false); |
| 6346 |
} |
| 6347 |
// Definition of a PCH-ed template declaration may be available only in the TU. |
| 6348 |
if (!LocalOnly && LangOpts.PCHInstantiateTemplates && |
| 6349 |
TUKind == TU_Prefix && Function->instantiationIsPending()) |
| 6350 |
delayedPCHInstantiations.push_back(Inst); |
| 6351 |
continue; |
| 6352 |
} |
| 6353 |
|
| 6354 |
// Instantiate variable definitions |
| 6355 |
VarDecl *Var = cast<VarDecl>(Inst.first); |
| 6356 |
|
| 6357 |
assert((Var->isStaticDataMember() || |
| 6358 |
isa<VarTemplateSpecializationDecl>(Var)) && |
| 6359 |
"Not a static data member, nor a variable template" |
| 6360 |
" specialization?"); |
| 6361 |
|
| 6362 |
// Don't try to instantiate declarations if the most recent redeclaration |
| 6363 |
// is invalid. |
| 6364 |
if (Var->getMostRecentDecl()->isInvalidDecl()) |
| 6365 |
continue; |
| 6366 |
|
| 6367 |
// Check if the most recent declaration has changed the specialization kind |
| 6368 |
// and removed the need for implicit instantiation. |
| 6369 |
switch (Var->getMostRecentDecl() |
| 6370 |
->getTemplateSpecializationKindForInstantiation()) { |
| 6371 |
case TSK_Undeclared: |
| 6372 |
llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
| 6373 |
case TSK_ExplicitInstantiationDeclaration: |
| 6374 |
case TSK_ExplicitSpecialization: |
| 6375 |
continue; // No longer need to instantiate this type. |
| 6376 |
case TSK_ExplicitInstantiationDefinition: |
| 6377 |
// We only need an instantiation if the pending instantiation *is* the |
| 6378 |
// explicit instantiation. |
| 6379 |
if (Var != Var->getMostRecentDecl()) |
| 6380 |
continue; |
| 6381 |
break; |
| 6382 |
case TSK_ImplicitInstantiation: |
| 6383 |
break; |
| 6384 |
} |
| 6385 |
|
| 6386 |
PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
| 6387 |
"instantiating variable definition"); |
| 6388 |
bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 6389 |
TSK_ExplicitInstantiationDefinition; |
| 6390 |
|
| 6391 |
// Instantiate static data member definitions or variable template |
| 6392 |
// specializations. |
| 6393 |
InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
| 6394 |
DefinitionRequired, true); |
| 6395 |
} |
| 6396 |
|
| 6397 |
if (!LocalOnly && LangOpts.PCHInstantiateTemplates) |
| 6398 |
PendingInstantiations.swap(delayedPCHInstantiations); |
| 6399 |
} |
| 6400 |
|
| 6401 |
void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 6402 |
const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 6403 |
for (auto *DD : Pattern->ddiags()) { |
| 6404 |
switch (DD->getKind()) { |
| 6405 |
case DependentDiagnostic::Access: |
| 6406 |
HandleDependentAccessCheck(*DD, TemplateArgs); |
| 6407 |
break; |
| 6408 |
} |
| 6409 |
} |
| 6410 |
} |