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