File: | build/source/clang/lib/Sema/TreeTransform.h |
Warning: | line 6323, column 21 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/ | ||||||||||||
2 | // | ||||||||||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||
6 | //===----------------------------------------------------------------------===/ | ||||||||||||
7 | // | ||||||||||||
8 | // This file implements C++ template instantiation for declarations. | ||||||||||||
9 | // | ||||||||||||
10 | //===----------------------------------------------------------------------===/ | ||||||||||||
11 | |||||||||||||
12 | #include "TreeTransform.h" | ||||||||||||
13 | #include "clang/AST/ASTConsumer.h" | ||||||||||||
14 | #include "clang/AST/ASTContext.h" | ||||||||||||
15 | #include "clang/AST/ASTMutationListener.h" | ||||||||||||
16 | #include "clang/AST/DeclTemplate.h" | ||||||||||||
17 | #include "clang/AST/DeclVisitor.h" | ||||||||||||
18 | #include "clang/AST/DependentDiagnostic.h" | ||||||||||||
19 | #include "clang/AST/Expr.h" | ||||||||||||
20 | #include "clang/AST/ExprCXX.h" | ||||||||||||
21 | #include "clang/AST/PrettyDeclStackTrace.h" | ||||||||||||
22 | #include "clang/AST/TypeLoc.h" | ||||||||||||
23 | #include "clang/Basic/SourceManager.h" | ||||||||||||
24 | #include "clang/Basic/TargetInfo.h" | ||||||||||||
25 | #include "clang/Sema/Initialization.h" | ||||||||||||
26 | #include "clang/Sema/Lookup.h" | ||||||||||||
27 | #include "clang/Sema/ScopeInfo.h" | ||||||||||||
28 | #include "clang/Sema/SemaInternal.h" | ||||||||||||
29 | #include "clang/Sema/Template.h" | ||||||||||||
30 | #include "clang/Sema/TemplateInstCallback.h" | ||||||||||||
31 | #include "llvm/Support/TimeProfiler.h" | ||||||||||||
32 | #include <optional> | ||||||||||||
33 | |||||||||||||
34 | using namespace clang; | ||||||||||||
35 | |||||||||||||
36 | static bool isDeclWithinFunction(const Decl *D) { | ||||||||||||
37 | const DeclContext *DC = D->getDeclContext(); | ||||||||||||
38 | if (DC->isFunctionOrMethod()) | ||||||||||||
39 | return true; | ||||||||||||
40 | |||||||||||||
41 | if (DC->isRecord()) | ||||||||||||
42 | return cast<CXXRecordDecl>(DC)->isLocalClass(); | ||||||||||||
43 | |||||||||||||
44 | return false; | ||||||||||||
45 | } | ||||||||||||
46 | |||||||||||||
47 | template<typename DeclT> | ||||||||||||
48 | static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl, | ||||||||||||
49 | const MultiLevelTemplateArgumentList &TemplateArgs) { | ||||||||||||
50 | if (!OldDecl->getQualifierLoc()) | ||||||||||||
51 | return false; | ||||||||||||
52 | |||||||||||||
53 | assert((NewDecl->getFriendObjectKind() ||(static_cast <bool> ((NewDecl->getFriendObjectKind() || !OldDecl->getLexicalDeclContext()->isDependentContext ()) && "non-friend with qualified name defined in dependent context" ) ? void (0) : __assert_fail ("(NewDecl->getFriendObjectKind() || !OldDecl->getLexicalDeclContext()->isDependentContext()) && \"non-friend with qualified name defined in dependent context\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 55, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
54 | !OldDecl->getLexicalDeclContext()->isDependentContext()) &&(static_cast <bool> ((NewDecl->getFriendObjectKind() || !OldDecl->getLexicalDeclContext()->isDependentContext ()) && "non-friend with qualified name defined in dependent context" ) ? void (0) : __assert_fail ("(NewDecl->getFriendObjectKind() || !OldDecl->getLexicalDeclContext()->isDependentContext()) && \"non-friend with qualified name defined in dependent context\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 55, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
55 | "non-friend with qualified name defined in dependent context")(static_cast <bool> ((NewDecl->getFriendObjectKind() || !OldDecl->getLexicalDeclContext()->isDependentContext ()) && "non-friend with qualified name defined in dependent context" ) ? void (0) : __assert_fail ("(NewDecl->getFriendObjectKind() || !OldDecl->getLexicalDeclContext()->isDependentContext()) && \"non-friend with qualified name defined in dependent context\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 55, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
56 | Sema::ContextRAII SavedContext( | ||||||||||||
57 | SemaRef, | ||||||||||||
58 | const_cast<DeclContext *>(NewDecl->getFriendObjectKind() | ||||||||||||
59 | ? NewDecl->getLexicalDeclContext() | ||||||||||||
60 | : OldDecl->getLexicalDeclContext())); | ||||||||||||
61 | |||||||||||||
62 | NestedNameSpecifierLoc NewQualifierLoc | ||||||||||||
63 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), | ||||||||||||
64 | TemplateArgs); | ||||||||||||
65 | |||||||||||||
66 | if (!NewQualifierLoc) | ||||||||||||
67 | return true; | ||||||||||||
68 | |||||||||||||
69 | NewDecl->setQualifierInfo(NewQualifierLoc); | ||||||||||||
70 | return false; | ||||||||||||
71 | } | ||||||||||||
72 | |||||||||||||
73 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, | ||||||||||||
74 | DeclaratorDecl *NewDecl) { | ||||||||||||
75 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); | ||||||||||||
76 | } | ||||||||||||
77 | |||||||||||||
78 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, | ||||||||||||
79 | TagDecl *NewDecl) { | ||||||||||||
80 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); | ||||||||||||
81 | } | ||||||||||||
82 | |||||||||||||
83 | // Include attribute instantiation code. | ||||||||||||
84 | #include "clang/Sema/AttrTemplateInstantiate.inc" | ||||||||||||
85 | |||||||||||||
86 | static void instantiateDependentAlignedAttr( | ||||||||||||
87 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
88 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { | ||||||||||||
89 | if (Aligned->isAlignmentExpr()) { | ||||||||||||
90 | // The alignment expression is a constant expression. | ||||||||||||
91 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
92 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
93 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); | ||||||||||||
94 | if (!Result.isInvalid()) | ||||||||||||
95 | S.AddAlignedAttr(New, *Aligned, Result.getAs<Expr>(), IsPackExpansion); | ||||||||||||
96 | } else { | ||||||||||||
97 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), | ||||||||||||
98 | TemplateArgs, Aligned->getLocation(), | ||||||||||||
99 | DeclarationName()); | ||||||||||||
100 | if (Result) | ||||||||||||
101 | S.AddAlignedAttr(New, *Aligned, Result, IsPackExpansion); | ||||||||||||
102 | } | ||||||||||||
103 | } | ||||||||||||
104 | |||||||||||||
105 | static void instantiateDependentAlignedAttr( | ||||||||||||
106 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
107 | const AlignedAttr *Aligned, Decl *New) { | ||||||||||||
108 | if (!Aligned->isPackExpansion()) { | ||||||||||||
109 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); | ||||||||||||
110 | return; | ||||||||||||
111 | } | ||||||||||||
112 | |||||||||||||
113 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||||||
114 | if (Aligned->isAlignmentExpr()) | ||||||||||||
115 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), | ||||||||||||
116 | Unexpanded); | ||||||||||||
117 | else | ||||||||||||
118 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), | ||||||||||||
119 | Unexpanded); | ||||||||||||
120 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?")(static_cast <bool> (!Unexpanded.empty() && "Pack expansion without parameter packs?" ) ? void (0) : __assert_fail ("!Unexpanded.empty() && \"Pack expansion without parameter packs?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 120, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
121 | |||||||||||||
122 | // Determine whether we can expand this attribute pack yet. | ||||||||||||
123 | bool Expand = true, RetainExpansion = false; | ||||||||||||
124 | std::optional<unsigned> NumExpansions; | ||||||||||||
125 | // FIXME: Use the actual location of the ellipsis. | ||||||||||||
126 | SourceLocation EllipsisLoc = Aligned->getLocation(); | ||||||||||||
127 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), | ||||||||||||
128 | Unexpanded, TemplateArgs, Expand, | ||||||||||||
129 | RetainExpansion, NumExpansions)) | ||||||||||||
130 | return; | ||||||||||||
131 | |||||||||||||
132 | if (!Expand) { | ||||||||||||
133 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); | ||||||||||||
134 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); | ||||||||||||
135 | } else { | ||||||||||||
136 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||||||
137 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); | ||||||||||||
138 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); | ||||||||||||
139 | } | ||||||||||||
140 | } | ||||||||||||
141 | } | ||||||||||||
142 | |||||||||||||
143 | static void instantiateDependentAssumeAlignedAttr( | ||||||||||||
144 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
145 | const AssumeAlignedAttr *Aligned, Decl *New) { | ||||||||||||
146 | // The alignment expression is a constant expression. | ||||||||||||
147 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
148 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
149 | |||||||||||||
150 | Expr *E, *OE = nullptr; | ||||||||||||
151 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); | ||||||||||||
152 | if (Result.isInvalid()) | ||||||||||||
153 | return; | ||||||||||||
154 | E = Result.getAs<Expr>(); | ||||||||||||
155 | |||||||||||||
156 | if (Aligned->getOffset()) { | ||||||||||||
157 | Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs); | ||||||||||||
158 | if (Result.isInvalid()) | ||||||||||||
159 | return; | ||||||||||||
160 | OE = Result.getAs<Expr>(); | ||||||||||||
161 | } | ||||||||||||
162 | |||||||||||||
163 | S.AddAssumeAlignedAttr(New, *Aligned, E, OE); | ||||||||||||
164 | } | ||||||||||||
165 | |||||||||||||
166 | static void instantiateDependentAlignValueAttr( | ||||||||||||
167 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
168 | const AlignValueAttr *Aligned, Decl *New) { | ||||||||||||
169 | // The alignment expression is a constant expression. | ||||||||||||
170 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
171 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
172 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); | ||||||||||||
173 | if (!Result.isInvalid()) | ||||||||||||
174 | S.AddAlignValueAttr(New, *Aligned, Result.getAs<Expr>()); | ||||||||||||
175 | } | ||||||||||||
176 | |||||||||||||
177 | static void instantiateDependentAllocAlignAttr( | ||||||||||||
178 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
179 | const AllocAlignAttr *Align, Decl *New) { | ||||||||||||
180 | Expr *Param = IntegerLiteral::Create( | ||||||||||||
181 | S.getASTContext(), | ||||||||||||
182 | llvm::APInt(64, Align->getParamIndex().getSourceIndex()), | ||||||||||||
183 | S.getASTContext().UnsignedLongLongTy, Align->getLocation()); | ||||||||||||
184 | S.AddAllocAlignAttr(New, *Align, Param); | ||||||||||||
185 | } | ||||||||||||
186 | |||||||||||||
187 | static void instantiateDependentAnnotationAttr( | ||||||||||||
188 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
189 | const AnnotateAttr *Attr, Decl *New) { | ||||||||||||
190 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
191 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
192 | |||||||||||||
193 | // If the attribute has delayed arguments it will have to instantiate those | ||||||||||||
194 | // and handle them as new arguments for the attribute. | ||||||||||||
195 | bool HasDelayedArgs = Attr->delayedArgs_size(); | ||||||||||||
196 | |||||||||||||
197 | ArrayRef<Expr *> ArgsToInstantiate = | ||||||||||||
198 | HasDelayedArgs | ||||||||||||
199 | ? ArrayRef<Expr *>{Attr->delayedArgs_begin(), Attr->delayedArgs_end()} | ||||||||||||
200 | : ArrayRef<Expr *>{Attr->args_begin(), Attr->args_end()}; | ||||||||||||
201 | |||||||||||||
202 | SmallVector<Expr *, 4> Args; | ||||||||||||
203 | if (S.SubstExprs(ArgsToInstantiate, | ||||||||||||
204 | /*IsCall=*/false, TemplateArgs, Args)) | ||||||||||||
205 | return; | ||||||||||||
206 | |||||||||||||
207 | StringRef Str = Attr->getAnnotation(); | ||||||||||||
208 | if (HasDelayedArgs) { | ||||||||||||
209 | if (Args.size() < 1) { | ||||||||||||
210 | S.Diag(Attr->getLoc(), diag::err_attribute_too_few_arguments) | ||||||||||||
211 | << Attr << 1; | ||||||||||||
212 | return; | ||||||||||||
213 | } | ||||||||||||
214 | |||||||||||||
215 | if (!S.checkStringLiteralArgumentAttr(*Attr, Args[0], Str)) | ||||||||||||
216 | return; | ||||||||||||
217 | |||||||||||||
218 | llvm::SmallVector<Expr *, 4> ActualArgs; | ||||||||||||
219 | ActualArgs.insert(ActualArgs.begin(), Args.begin() + 1, Args.end()); | ||||||||||||
220 | std::swap(Args, ActualArgs); | ||||||||||||
221 | } | ||||||||||||
222 | S.AddAnnotationAttr(New, *Attr, Str, Args); | ||||||||||||
223 | } | ||||||||||||
224 | |||||||||||||
225 | static Expr *instantiateDependentFunctionAttrCondition( | ||||||||||||
226 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
227 | const Attr *A, Expr *OldCond, const Decl *Tmpl, FunctionDecl *New) { | ||||||||||||
228 | Expr *Cond = nullptr; | ||||||||||||
229 | { | ||||||||||||
230 | Sema::ContextRAII SwitchContext(S, New); | ||||||||||||
231 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
232 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
233 | ExprResult Result = S.SubstExpr(OldCond, TemplateArgs); | ||||||||||||
234 | if (Result.isInvalid()) | ||||||||||||
235 | return nullptr; | ||||||||||||
236 | Cond = Result.getAs<Expr>(); | ||||||||||||
237 | } | ||||||||||||
238 | if (!Cond->isTypeDependent()) { | ||||||||||||
239 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); | ||||||||||||
240 | if (Converted.isInvalid()) | ||||||||||||
241 | return nullptr; | ||||||||||||
242 | Cond = Converted.get(); | ||||||||||||
243 | } | ||||||||||||
244 | |||||||||||||
245 | SmallVector<PartialDiagnosticAt, 8> Diags; | ||||||||||||
246 | if (OldCond->isValueDependent() && !Cond->isValueDependent() && | ||||||||||||
247 | !Expr::isPotentialConstantExprUnevaluated(Cond, New, Diags)) { | ||||||||||||
248 | S.Diag(A->getLocation(), diag::err_attr_cond_never_constant_expr) << A; | ||||||||||||
249 | for (const auto &P : Diags) | ||||||||||||
250 | S.Diag(P.first, P.second); | ||||||||||||
251 | return nullptr; | ||||||||||||
252 | } | ||||||||||||
253 | return Cond; | ||||||||||||
254 | } | ||||||||||||
255 | |||||||||||||
256 | static void instantiateDependentEnableIfAttr( | ||||||||||||
257 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
258 | const EnableIfAttr *EIA, const Decl *Tmpl, FunctionDecl *New) { | ||||||||||||
259 | Expr *Cond = instantiateDependentFunctionAttrCondition( | ||||||||||||
260 | S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New); | ||||||||||||
261 | |||||||||||||
262 | if (Cond) | ||||||||||||
263 | New->addAttr(new (S.getASTContext()) EnableIfAttr(S.getASTContext(), *EIA, | ||||||||||||
264 | Cond, EIA->getMessage())); | ||||||||||||
265 | } | ||||||||||||
266 | |||||||||||||
267 | static void instantiateDependentDiagnoseIfAttr( | ||||||||||||
268 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
269 | const DiagnoseIfAttr *DIA, const Decl *Tmpl, FunctionDecl *New) { | ||||||||||||
270 | Expr *Cond = instantiateDependentFunctionAttrCondition( | ||||||||||||
271 | S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New); | ||||||||||||
272 | |||||||||||||
273 | if (Cond) | ||||||||||||
274 | New->addAttr(new (S.getASTContext()) DiagnoseIfAttr( | ||||||||||||
275 | S.getASTContext(), *DIA, Cond, DIA->getMessage(), | ||||||||||||
276 | DIA->getDiagnosticType(), DIA->getArgDependent(), New)); | ||||||||||||
277 | } | ||||||||||||
278 | |||||||||||||
279 | // Constructs and adds to New a new instance of CUDALaunchBoundsAttr using | ||||||||||||
280 | // template A as the base and arguments from TemplateArgs. | ||||||||||||
281 | static void instantiateDependentCUDALaunchBoundsAttr( | ||||||||||||
282 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
283 | const CUDALaunchBoundsAttr &Attr, Decl *New) { | ||||||||||||
284 | // The alignment expression is a constant expression. | ||||||||||||
285 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
286 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
287 | |||||||||||||
288 | ExprResult Result = S.SubstExpr(Attr.getMaxThreads(), TemplateArgs); | ||||||||||||
289 | if (Result.isInvalid()) | ||||||||||||
290 | return; | ||||||||||||
291 | Expr *MaxThreads = Result.getAs<Expr>(); | ||||||||||||
292 | |||||||||||||
293 | Expr *MinBlocks = nullptr; | ||||||||||||
294 | if (Attr.getMinBlocks()) { | ||||||||||||
295 | Result = S.SubstExpr(Attr.getMinBlocks(), TemplateArgs); | ||||||||||||
296 | if (Result.isInvalid()) | ||||||||||||
297 | return; | ||||||||||||
298 | MinBlocks = Result.getAs<Expr>(); | ||||||||||||
299 | } | ||||||||||||
300 | |||||||||||||
301 | S.AddLaunchBoundsAttr(New, Attr, MaxThreads, MinBlocks); | ||||||||||||
302 | } | ||||||||||||
303 | |||||||||||||
304 | static void | ||||||||||||
305 | instantiateDependentModeAttr(Sema &S, | ||||||||||||
306 | const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
307 | const ModeAttr &Attr, Decl *New) { | ||||||||||||
308 | S.AddModeAttr(New, Attr, Attr.getMode(), | ||||||||||||
309 | /*InInstantiation=*/true); | ||||||||||||
310 | } | ||||||||||||
311 | |||||||||||||
312 | /// Instantiation of 'declare simd' attribute and its arguments. | ||||||||||||
313 | static void instantiateOMPDeclareSimdDeclAttr( | ||||||||||||
314 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
315 | const OMPDeclareSimdDeclAttr &Attr, Decl *New) { | ||||||||||||
316 | // Allow 'this' in clauses with varlists. | ||||||||||||
317 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) | ||||||||||||
318 | New = FTD->getTemplatedDecl(); | ||||||||||||
319 | auto *FD = cast<FunctionDecl>(New); | ||||||||||||
320 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); | ||||||||||||
321 | SmallVector<Expr *, 4> Uniforms, Aligneds, Alignments, Linears, Steps; | ||||||||||||
322 | SmallVector<unsigned, 4> LinModifiers; | ||||||||||||
323 | |||||||||||||
324 | auto SubstExpr = [&](Expr *E) -> ExprResult { | ||||||||||||
325 | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) | ||||||||||||
326 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { | ||||||||||||
327 | Sema::ContextRAII SavedContext(S, FD); | ||||||||||||
328 | LocalInstantiationScope Local(S); | ||||||||||||
329 | if (FD->getNumParams() > PVD->getFunctionScopeIndex()) | ||||||||||||
330 | Local.InstantiatedLocal( | ||||||||||||
331 | PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); | ||||||||||||
332 | return S.SubstExpr(E, TemplateArgs); | ||||||||||||
333 | } | ||||||||||||
334 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), | ||||||||||||
335 | FD->isCXXInstanceMember()); | ||||||||||||
336 | return S.SubstExpr(E, TemplateArgs); | ||||||||||||
337 | }; | ||||||||||||
338 | |||||||||||||
339 | // Substitute a single OpenMP clause, which is a potentially-evaluated | ||||||||||||
340 | // full-expression. | ||||||||||||
341 | auto Subst = [&](Expr *E) -> ExprResult { | ||||||||||||
342 | EnterExpressionEvaluationContext Evaluated( | ||||||||||||
343 | S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); | ||||||||||||
344 | ExprResult Res = SubstExpr(E); | ||||||||||||
345 | if (Res.isInvalid()) | ||||||||||||
346 | return Res; | ||||||||||||
347 | return S.ActOnFinishFullExpr(Res.get(), false); | ||||||||||||
348 | }; | ||||||||||||
349 | |||||||||||||
350 | ExprResult Simdlen; | ||||||||||||
351 | if (auto *E = Attr.getSimdlen()) | ||||||||||||
352 | Simdlen = Subst(E); | ||||||||||||
353 | |||||||||||||
354 | if (Attr.uniforms_size() > 0) { | ||||||||||||
355 | for(auto *E : Attr.uniforms()) { | ||||||||||||
356 | ExprResult Inst = Subst(E); | ||||||||||||
357 | if (Inst.isInvalid()) | ||||||||||||
358 | continue; | ||||||||||||
359 | Uniforms.push_back(Inst.get()); | ||||||||||||
360 | } | ||||||||||||
361 | } | ||||||||||||
362 | |||||||||||||
363 | auto AI = Attr.alignments_begin(); | ||||||||||||
364 | for (auto *E : Attr.aligneds()) { | ||||||||||||
365 | ExprResult Inst = Subst(E); | ||||||||||||
366 | if (Inst.isInvalid()) | ||||||||||||
367 | continue; | ||||||||||||
368 | Aligneds.push_back(Inst.get()); | ||||||||||||
369 | Inst = ExprEmpty(); | ||||||||||||
370 | if (*AI) | ||||||||||||
371 | Inst = S.SubstExpr(*AI, TemplateArgs); | ||||||||||||
372 | Alignments.push_back(Inst.get()); | ||||||||||||
373 | ++AI; | ||||||||||||
374 | } | ||||||||||||
375 | |||||||||||||
376 | auto SI = Attr.steps_begin(); | ||||||||||||
377 | for (auto *E : Attr.linears()) { | ||||||||||||
378 | ExprResult Inst = Subst(E); | ||||||||||||
379 | if (Inst.isInvalid()) | ||||||||||||
380 | continue; | ||||||||||||
381 | Linears.push_back(Inst.get()); | ||||||||||||
382 | Inst = ExprEmpty(); | ||||||||||||
383 | if (*SI) | ||||||||||||
384 | Inst = S.SubstExpr(*SI, TemplateArgs); | ||||||||||||
385 | Steps.push_back(Inst.get()); | ||||||||||||
386 | ++SI; | ||||||||||||
387 | } | ||||||||||||
388 | LinModifiers.append(Attr.modifiers_begin(), Attr.modifiers_end()); | ||||||||||||
389 | (void)S.ActOnOpenMPDeclareSimdDirective( | ||||||||||||
390 | S.ConvertDeclToDeclGroup(New), Attr.getBranchState(), Simdlen.get(), | ||||||||||||
391 | Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps, | ||||||||||||
392 | Attr.getRange()); | ||||||||||||
393 | } | ||||||||||||
394 | |||||||||||||
395 | /// Instantiation of 'declare variant' attribute and its arguments. | ||||||||||||
396 | static void instantiateOMPDeclareVariantAttr( | ||||||||||||
397 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
398 | const OMPDeclareVariantAttr &Attr, Decl *New) { | ||||||||||||
399 | // Allow 'this' in clauses with varlists. | ||||||||||||
400 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) | ||||||||||||
401 | New = FTD->getTemplatedDecl(); | ||||||||||||
402 | auto *FD = cast<FunctionDecl>(New); | ||||||||||||
403 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); | ||||||||||||
404 | |||||||||||||
405 | auto &&SubstExpr = [FD, ThisContext, &S, &TemplateArgs](Expr *E) { | ||||||||||||
406 | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) | ||||||||||||
407 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { | ||||||||||||
408 | Sema::ContextRAII SavedContext(S, FD); | ||||||||||||
409 | LocalInstantiationScope Local(S); | ||||||||||||
410 | if (FD->getNumParams() > PVD->getFunctionScopeIndex()) | ||||||||||||
411 | Local.InstantiatedLocal( | ||||||||||||
412 | PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); | ||||||||||||
413 | return S.SubstExpr(E, TemplateArgs); | ||||||||||||
414 | } | ||||||||||||
415 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), | ||||||||||||
416 | FD->isCXXInstanceMember()); | ||||||||||||
417 | return S.SubstExpr(E, TemplateArgs); | ||||||||||||
418 | }; | ||||||||||||
419 | |||||||||||||
420 | // Substitute a single OpenMP clause, which is a potentially-evaluated | ||||||||||||
421 | // full-expression. | ||||||||||||
422 | auto &&Subst = [&SubstExpr, &S](Expr *E) { | ||||||||||||
423 | EnterExpressionEvaluationContext Evaluated( | ||||||||||||
424 | S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); | ||||||||||||
425 | ExprResult Res = SubstExpr(E); | ||||||||||||
426 | if (Res.isInvalid()) | ||||||||||||
427 | return Res; | ||||||||||||
428 | return S.ActOnFinishFullExpr(Res.get(), false); | ||||||||||||
429 | }; | ||||||||||||
430 | |||||||||||||
431 | ExprResult VariantFuncRef; | ||||||||||||
432 | if (Expr *E = Attr.getVariantFuncRef()) { | ||||||||||||
433 | // Do not mark function as is used to prevent its emission if this is the | ||||||||||||
434 | // only place where it is used. | ||||||||||||
435 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
436 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
437 | VariantFuncRef = Subst(E); | ||||||||||||
438 | } | ||||||||||||
439 | |||||||||||||
440 | // Copy the template version of the OMPTraitInfo and run substitute on all | ||||||||||||
441 | // score and condition expressiosn. | ||||||||||||
442 | OMPTraitInfo &TI = S.getASTContext().getNewOMPTraitInfo(); | ||||||||||||
443 | TI = *Attr.getTraitInfos(); | ||||||||||||
444 | |||||||||||||
445 | // Try to substitute template parameters in score and condition expressions. | ||||||||||||
446 | auto SubstScoreOrConditionExpr = [&S, Subst](Expr *&E, bool) { | ||||||||||||
447 | if (E) { | ||||||||||||
448 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
449 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
450 | ExprResult ER = Subst(E); | ||||||||||||
451 | if (ER.isUsable()) | ||||||||||||
452 | E = ER.get(); | ||||||||||||
453 | else | ||||||||||||
454 | return true; | ||||||||||||
455 | } | ||||||||||||
456 | return false; | ||||||||||||
457 | }; | ||||||||||||
458 | if (TI.anyScoreOrCondition(SubstScoreOrConditionExpr)) | ||||||||||||
459 | return; | ||||||||||||
460 | |||||||||||||
461 | Expr *E = VariantFuncRef.get(); | ||||||||||||
462 | |||||||||||||
463 | // Check function/variant ref for `omp declare variant` but not for `omp | ||||||||||||
464 | // begin declare variant` (which use implicit attributes). | ||||||||||||
465 | std::optional<std::pair<FunctionDecl *, Expr *>> DeclVarData = | ||||||||||||
466 | S.checkOpenMPDeclareVariantFunction(S.ConvertDeclToDeclGroup(New), E, TI, | ||||||||||||
467 | Attr.appendArgs_size(), | ||||||||||||
468 | Attr.getRange()); | ||||||||||||
469 | |||||||||||||
470 | if (!DeclVarData) | ||||||||||||
471 | return; | ||||||||||||
472 | |||||||||||||
473 | E = DeclVarData->second; | ||||||||||||
474 | FD = DeclVarData->first; | ||||||||||||
475 | |||||||||||||
476 | if (auto *VariantDRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) { | ||||||||||||
477 | if (auto *VariantFD = dyn_cast<FunctionDecl>(VariantDRE->getDecl())) { | ||||||||||||
478 | if (auto *VariantFTD = VariantFD->getDescribedFunctionTemplate()) { | ||||||||||||
479 | if (!VariantFTD->isThisDeclarationADefinition()) | ||||||||||||
480 | return; | ||||||||||||
481 | Sema::TentativeAnalysisScope Trap(S); | ||||||||||||
482 | const TemplateArgumentList *TAL = TemplateArgumentList::CreateCopy( | ||||||||||||
483 | S.Context, TemplateArgs.getInnermost()); | ||||||||||||
484 | |||||||||||||
485 | auto *SubstFD = S.InstantiateFunctionDeclaration(VariantFTD, TAL, | ||||||||||||
486 | New->getLocation()); | ||||||||||||
487 | if (!SubstFD) | ||||||||||||
488 | return; | ||||||||||||
489 | QualType NewType = S.Context.mergeFunctionTypes( | ||||||||||||
490 | SubstFD->getType(), FD->getType(), | ||||||||||||
491 | /* OfBlockPointer */ false, | ||||||||||||
492 | /* Unqualified */ false, /* AllowCXX */ true); | ||||||||||||
493 | if (NewType.isNull()) | ||||||||||||
494 | return; | ||||||||||||
495 | S.InstantiateFunctionDefinition( | ||||||||||||
496 | New->getLocation(), SubstFD, /* Recursive */ true, | ||||||||||||
497 | /* DefinitionRequired */ false, /* AtEndOfTU */ false); | ||||||||||||
498 | SubstFD->setInstantiationIsPending(!SubstFD->isDefined()); | ||||||||||||
499 | E = DeclRefExpr::Create(S.Context, NestedNameSpecifierLoc(), | ||||||||||||
500 | SourceLocation(), SubstFD, | ||||||||||||
501 | /* RefersToEnclosingVariableOrCapture */ false, | ||||||||||||
502 | /* NameLoc */ SubstFD->getLocation(), | ||||||||||||
503 | SubstFD->getType(), ExprValueKind::VK_PRValue); | ||||||||||||
504 | } | ||||||||||||
505 | } | ||||||||||||
506 | } | ||||||||||||
507 | |||||||||||||
508 | SmallVector<Expr *, 8> NothingExprs; | ||||||||||||
509 | SmallVector<Expr *, 8> NeedDevicePtrExprs; | ||||||||||||
510 | SmallVector<OMPInteropInfo, 4> AppendArgs; | ||||||||||||
511 | |||||||||||||
512 | for (Expr *E : Attr.adjustArgsNothing()) { | ||||||||||||
513 | ExprResult ER = Subst(E); | ||||||||||||
514 | if (ER.isInvalid()) | ||||||||||||
515 | continue; | ||||||||||||
516 | NothingExprs.push_back(ER.get()); | ||||||||||||
517 | } | ||||||||||||
518 | for (Expr *E : Attr.adjustArgsNeedDevicePtr()) { | ||||||||||||
519 | ExprResult ER = Subst(E); | ||||||||||||
520 | if (ER.isInvalid()) | ||||||||||||
521 | continue; | ||||||||||||
522 | NeedDevicePtrExprs.push_back(ER.get()); | ||||||||||||
523 | } | ||||||||||||
524 | for (OMPInteropInfo &II : Attr.appendArgs()) { | ||||||||||||
525 | // When prefer_type is implemented for append_args handle them here too. | ||||||||||||
526 | AppendArgs.emplace_back(II.IsTarget, II.IsTargetSync); | ||||||||||||
527 | } | ||||||||||||
528 | |||||||||||||
529 | S.ActOnOpenMPDeclareVariantDirective( | ||||||||||||
530 | FD, E, TI, NothingExprs, NeedDevicePtrExprs, AppendArgs, SourceLocation(), | ||||||||||||
531 | SourceLocation(), Attr.getRange()); | ||||||||||||
532 | } | ||||||||||||
533 | |||||||||||||
534 | static void instantiateDependentAMDGPUFlatWorkGroupSizeAttr( | ||||||||||||
535 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
536 | const AMDGPUFlatWorkGroupSizeAttr &Attr, Decl *New) { | ||||||||||||
537 | // Both min and max expression are constant expressions. | ||||||||||||
538 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
539 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
540 | |||||||||||||
541 | ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); | ||||||||||||
542 | if (Result.isInvalid()) | ||||||||||||
543 | return; | ||||||||||||
544 | Expr *MinExpr = Result.getAs<Expr>(); | ||||||||||||
545 | |||||||||||||
546 | Result = S.SubstExpr(Attr.getMax(), TemplateArgs); | ||||||||||||
547 | if (Result.isInvalid()) | ||||||||||||
548 | return; | ||||||||||||
549 | Expr *MaxExpr = Result.getAs<Expr>(); | ||||||||||||
550 | |||||||||||||
551 | S.addAMDGPUFlatWorkGroupSizeAttr(New, Attr, MinExpr, MaxExpr); | ||||||||||||
552 | } | ||||||||||||
553 | |||||||||||||
554 | static ExplicitSpecifier | ||||||||||||
555 | instantiateExplicitSpecifier(Sema &S, | ||||||||||||
556 | const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
557 | ExplicitSpecifier ES, FunctionDecl *New) { | ||||||||||||
558 | if (!ES.getExpr()) | ||||||||||||
559 | return ES; | ||||||||||||
560 | Expr *OldCond = ES.getExpr(); | ||||||||||||
561 | Expr *Cond = nullptr; | ||||||||||||
562 | { | ||||||||||||
563 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
564 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
565 | ExprResult SubstResult = S.SubstExpr(OldCond, TemplateArgs); | ||||||||||||
566 | if (SubstResult.isInvalid()) { | ||||||||||||
567 | return ExplicitSpecifier::Invalid(); | ||||||||||||
568 | } | ||||||||||||
569 | Cond = SubstResult.get(); | ||||||||||||
570 | } | ||||||||||||
571 | ExplicitSpecifier Result(Cond, ES.getKind()); | ||||||||||||
572 | if (!Cond->isTypeDependent()) | ||||||||||||
573 | S.tryResolveExplicitSpecifier(Result); | ||||||||||||
574 | return Result; | ||||||||||||
575 | } | ||||||||||||
576 | |||||||||||||
577 | static void instantiateDependentAMDGPUWavesPerEUAttr( | ||||||||||||
578 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
579 | const AMDGPUWavesPerEUAttr &Attr, Decl *New) { | ||||||||||||
580 | // Both min and max expression are constant expressions. | ||||||||||||
581 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
582 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
583 | |||||||||||||
584 | ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); | ||||||||||||
585 | if (Result.isInvalid()) | ||||||||||||
586 | return; | ||||||||||||
587 | Expr *MinExpr = Result.getAs<Expr>(); | ||||||||||||
588 | |||||||||||||
589 | Expr *MaxExpr = nullptr; | ||||||||||||
590 | if (auto Max = Attr.getMax()) { | ||||||||||||
591 | Result = S.SubstExpr(Max, TemplateArgs); | ||||||||||||
592 | if (Result.isInvalid()) | ||||||||||||
593 | return; | ||||||||||||
594 | MaxExpr = Result.getAs<Expr>(); | ||||||||||||
595 | } | ||||||||||||
596 | |||||||||||||
597 | S.addAMDGPUWavesPerEUAttr(New, Attr, MinExpr, MaxExpr); | ||||||||||||
598 | } | ||||||||||||
599 | |||||||||||||
600 | // This doesn't take any template parameters, but we have a custom action that | ||||||||||||
601 | // needs to happen when the kernel itself is instantiated. We need to run the | ||||||||||||
602 | // ItaniumMangler to mark the names required to name this kernel. | ||||||||||||
603 | static void instantiateDependentSYCLKernelAttr( | ||||||||||||
604 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
605 | const SYCLKernelAttr &Attr, Decl *New) { | ||||||||||||
606 | New->addAttr(Attr.clone(S.getASTContext())); | ||||||||||||
607 | } | ||||||||||||
608 | |||||||||||||
609 | /// Determine whether the attribute A might be relevant to the declaration D. | ||||||||||||
610 | /// If not, we can skip instantiating it. The attribute may or may not have | ||||||||||||
611 | /// been instantiated yet. | ||||||||||||
612 | static bool isRelevantAttr(Sema &S, const Decl *D, const Attr *A) { | ||||||||||||
613 | // 'preferred_name' is only relevant to the matching specialization of the | ||||||||||||
614 | // template. | ||||||||||||
615 | if (const auto *PNA = dyn_cast<PreferredNameAttr>(A)) { | ||||||||||||
616 | QualType T = PNA->getTypedefType(); | ||||||||||||
617 | const auto *RD = cast<CXXRecordDecl>(D); | ||||||||||||
618 | if (!T->isDependentType() && !RD->isDependentContext() && | ||||||||||||
619 | !declaresSameEntity(T->getAsCXXRecordDecl(), RD)) | ||||||||||||
620 | return false; | ||||||||||||
621 | for (const auto *ExistingPNA : D->specific_attrs<PreferredNameAttr>()) | ||||||||||||
622 | if (S.Context.hasSameType(ExistingPNA->getTypedefType(), | ||||||||||||
623 | PNA->getTypedefType())) | ||||||||||||
624 | return false; | ||||||||||||
625 | return true; | ||||||||||||
626 | } | ||||||||||||
627 | |||||||||||||
628 | if (const auto *BA = dyn_cast<BuiltinAttr>(A)) { | ||||||||||||
629 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); | ||||||||||||
630 | switch (BA->getID()) { | ||||||||||||
631 | case Builtin::BIforward: | ||||||||||||
632 | // Do not treat 'std::forward' as a builtin if it takes an rvalue reference | ||||||||||||
633 | // type and returns an lvalue reference type. The library implementation | ||||||||||||
634 | // will produce an error in this case; don't get in its way. | ||||||||||||
635 | if (FD && FD->getNumParams() >= 1 && | ||||||||||||
636 | FD->getParamDecl(0)->getType()->isRValueReferenceType() && | ||||||||||||
637 | FD->getReturnType()->isLValueReferenceType()) { | ||||||||||||
638 | return false; | ||||||||||||
639 | } | ||||||||||||
640 | [[fallthrough]]; | ||||||||||||
641 | case Builtin::BImove: | ||||||||||||
642 | case Builtin::BImove_if_noexcept: | ||||||||||||
643 | // HACK: Super-old versions of libc++ (3.1 and earlier) provide | ||||||||||||
644 | // std::forward and std::move overloads that sometimes return by value | ||||||||||||
645 | // instead of by reference when building in C++98 mode. Don't treat such | ||||||||||||
646 | // cases as builtins. | ||||||||||||
647 | if (FD && !FD->getReturnType()->isReferenceType()) | ||||||||||||
648 | return false; | ||||||||||||
649 | break; | ||||||||||||
650 | } | ||||||||||||
651 | } | ||||||||||||
652 | |||||||||||||
653 | return true; | ||||||||||||
654 | } | ||||||||||||
655 | |||||||||||||
656 | void Sema::InstantiateAttrsForDecl( | ||||||||||||
657 | const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl, | ||||||||||||
658 | Decl *New, LateInstantiatedAttrVec *LateAttrs, | ||||||||||||
659 | LocalInstantiationScope *OuterMostScope) { | ||||||||||||
660 | if (NamedDecl *ND = dyn_cast<NamedDecl>(New)) { | ||||||||||||
661 | // FIXME: This function is called multiple times for the same template | ||||||||||||
662 | // specialization. We should only instantiate attributes that were added | ||||||||||||
663 | // since the previous instantiation. | ||||||||||||
664 | for (const auto *TmplAttr : Tmpl->attrs()) { | ||||||||||||
665 | if (!isRelevantAttr(*this, New, TmplAttr)) | ||||||||||||
666 | continue; | ||||||||||||
667 | |||||||||||||
668 | // FIXME: If any of the special case versions from InstantiateAttrs become | ||||||||||||
669 | // applicable to template declaration, we'll need to add them here. | ||||||||||||
670 | CXXThisScopeRAII ThisScope( | ||||||||||||
671 | *this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()), | ||||||||||||
672 | Qualifiers(), ND->isCXXInstanceMember()); | ||||||||||||
673 | |||||||||||||
674 | Attr *NewAttr = sema::instantiateTemplateAttributeForDecl( | ||||||||||||
675 | TmplAttr, Context, *this, TemplateArgs); | ||||||||||||
676 | if (NewAttr && isRelevantAttr(*this, New, NewAttr)) | ||||||||||||
677 | New->addAttr(NewAttr); | ||||||||||||
678 | } | ||||||||||||
679 | } | ||||||||||||
680 | } | ||||||||||||
681 | |||||||||||||
682 | static Sema::RetainOwnershipKind | ||||||||||||
683 | attrToRetainOwnershipKind(const Attr *A) { | ||||||||||||
684 | switch (A->getKind()) { | ||||||||||||
685 | case clang::attr::CFConsumed: | ||||||||||||
686 | return Sema::RetainOwnershipKind::CF; | ||||||||||||
687 | case clang::attr::OSConsumed: | ||||||||||||
688 | return Sema::RetainOwnershipKind::OS; | ||||||||||||
689 | case clang::attr::NSConsumed: | ||||||||||||
690 | return Sema::RetainOwnershipKind::NS; | ||||||||||||
691 | default: | ||||||||||||
692 | llvm_unreachable("Wrong argument supplied")::llvm::llvm_unreachable_internal("Wrong argument supplied", "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp" , 692); | ||||||||||||
693 | } | ||||||||||||
694 | } | ||||||||||||
695 | |||||||||||||
696 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
697 | const Decl *Tmpl, Decl *New, | ||||||||||||
698 | LateInstantiatedAttrVec *LateAttrs, | ||||||||||||
699 | LocalInstantiationScope *OuterMostScope) { | ||||||||||||
700 | for (const auto *TmplAttr : Tmpl->attrs()) { | ||||||||||||
701 | if (!isRelevantAttr(*this, New, TmplAttr)) | ||||||||||||
702 | continue; | ||||||||||||
703 | |||||||||||||
704 | // FIXME: This should be generalized to more than just the AlignedAttr. | ||||||||||||
705 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); | ||||||||||||
706 | if (Aligned && Aligned->isAlignmentDependent()) { | ||||||||||||
707 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); | ||||||||||||
708 | continue; | ||||||||||||
709 | } | ||||||||||||
710 | |||||||||||||
711 | if (const auto *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr)) { | ||||||||||||
712 | instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); | ||||||||||||
713 | continue; | ||||||||||||
714 | } | ||||||||||||
715 | |||||||||||||
716 | if (const auto *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr)) { | ||||||||||||
717 | instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); | ||||||||||||
718 | continue; | ||||||||||||
719 | } | ||||||||||||
720 | |||||||||||||
721 | if (const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) { | ||||||||||||
722 | instantiateDependentAllocAlignAttr(*this, TemplateArgs, AllocAlign, New); | ||||||||||||
723 | continue; | ||||||||||||
724 | } | ||||||||||||
725 | |||||||||||||
726 | if (const auto *Annotate = dyn_cast<AnnotateAttr>(TmplAttr)) { | ||||||||||||
727 | instantiateDependentAnnotationAttr(*this, TemplateArgs, Annotate, New); | ||||||||||||
728 | continue; | ||||||||||||
729 | } | ||||||||||||
730 | |||||||||||||
731 | if (const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) { | ||||||||||||
732 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, | ||||||||||||
733 | cast<FunctionDecl>(New)); | ||||||||||||
734 | continue; | ||||||||||||
735 | } | ||||||||||||
736 | |||||||||||||
737 | if (const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) { | ||||||||||||
738 | instantiateDependentDiagnoseIfAttr(*this, TemplateArgs, DiagnoseIf, Tmpl, | ||||||||||||
739 | cast<FunctionDecl>(New)); | ||||||||||||
740 | continue; | ||||||||||||
741 | } | ||||||||||||
742 | |||||||||||||
743 | if (const auto *CUDALaunchBounds = | ||||||||||||
744 | dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { | ||||||||||||
745 | instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, | ||||||||||||
746 | *CUDALaunchBounds, New); | ||||||||||||
747 | continue; | ||||||||||||
748 | } | ||||||||||||
749 | |||||||||||||
750 | if (const auto *Mode = dyn_cast<ModeAttr>(TmplAttr)) { | ||||||||||||
751 | instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); | ||||||||||||
752 | continue; | ||||||||||||
753 | } | ||||||||||||
754 | |||||||||||||
755 | if (const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) { | ||||||||||||
756 | instantiateOMPDeclareSimdDeclAttr(*this, TemplateArgs, *OMPAttr, New); | ||||||||||||
757 | continue; | ||||||||||||
758 | } | ||||||||||||
759 | |||||||||||||
760 | if (const auto *OMPAttr = dyn_cast<OMPDeclareVariantAttr>(TmplAttr)) { | ||||||||||||
761 | instantiateOMPDeclareVariantAttr(*this, TemplateArgs, *OMPAttr, New); | ||||||||||||
762 | continue; | ||||||||||||
763 | } | ||||||||||||
764 | |||||||||||||
765 | if (const auto *AMDGPUFlatWorkGroupSize = | ||||||||||||
766 | dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) { | ||||||||||||
767 | instantiateDependentAMDGPUFlatWorkGroupSizeAttr( | ||||||||||||
768 | *this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New); | ||||||||||||
769 | } | ||||||||||||
770 | |||||||||||||
771 | if (const auto *AMDGPUFlatWorkGroupSize = | ||||||||||||
772 | dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) { | ||||||||||||
773 | instantiateDependentAMDGPUWavesPerEUAttr(*this, TemplateArgs, | ||||||||||||
774 | *AMDGPUFlatWorkGroupSize, New); | ||||||||||||
775 | } | ||||||||||||
776 | |||||||||||||
777 | // Existing DLL attribute on the instantiation takes precedence. | ||||||||||||
778 | if (TmplAttr->getKind() == attr::DLLExport || | ||||||||||||
779 | TmplAttr->getKind() == attr::DLLImport) { | ||||||||||||
780 | if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { | ||||||||||||
781 | continue; | ||||||||||||
782 | } | ||||||||||||
783 | } | ||||||||||||
784 | |||||||||||||
785 | if (const auto *ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { | ||||||||||||
786 | AddParameterABIAttr(New, *ABIAttr, ABIAttr->getABI()); | ||||||||||||
787 | continue; | ||||||||||||
788 | } | ||||||||||||
789 | |||||||||||||
790 | if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || | ||||||||||||
791 | isa<CFConsumedAttr>(TmplAttr)) { | ||||||||||||
792 | AddXConsumedAttr(New, *TmplAttr, attrToRetainOwnershipKind(TmplAttr), | ||||||||||||
793 | /*template instantiation=*/true); | ||||||||||||
794 | continue; | ||||||||||||
795 | } | ||||||||||||
796 | |||||||||||||
797 | if (auto *A = dyn_cast<PointerAttr>(TmplAttr)) { | ||||||||||||
798 | if (!New->hasAttr<PointerAttr>()) | ||||||||||||
799 | New->addAttr(A->clone(Context)); | ||||||||||||
800 | continue; | ||||||||||||
801 | } | ||||||||||||
802 | |||||||||||||
803 | if (auto *A = dyn_cast<OwnerAttr>(TmplAttr)) { | ||||||||||||
804 | if (!New->hasAttr<OwnerAttr>()) | ||||||||||||
805 | New->addAttr(A->clone(Context)); | ||||||||||||
806 | continue; | ||||||||||||
807 | } | ||||||||||||
808 | |||||||||||||
809 | if (auto *A = dyn_cast<SYCLKernelAttr>(TmplAttr)) { | ||||||||||||
810 | instantiateDependentSYCLKernelAttr(*this, TemplateArgs, *A, New); | ||||||||||||
811 | continue; | ||||||||||||
812 | } | ||||||||||||
813 | |||||||||||||
814 | assert(!TmplAttr->isPackExpansion())(static_cast <bool> (!TmplAttr->isPackExpansion()) ? void (0) : __assert_fail ("!TmplAttr->isPackExpansion()", "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 814, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
815 | if (TmplAttr->isLateParsed() && LateAttrs) { | ||||||||||||
816 | // Late parsed attributes must be instantiated and attached after the | ||||||||||||
817 | // enclosing class has been instantiated. See Sema::InstantiateClass. | ||||||||||||
818 | LocalInstantiationScope *Saved = nullptr; | ||||||||||||
819 | if (CurrentInstantiationScope) | ||||||||||||
820 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); | ||||||||||||
821 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); | ||||||||||||
822 | } else { | ||||||||||||
823 | // Allow 'this' within late-parsed attributes. | ||||||||||||
824 | auto *ND = cast<NamedDecl>(New); | ||||||||||||
825 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); | ||||||||||||
826 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), | ||||||||||||
827 | ND->isCXXInstanceMember()); | ||||||||||||
828 | |||||||||||||
829 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, | ||||||||||||
830 | *this, TemplateArgs); | ||||||||||||
831 | if (NewAttr && isRelevantAttr(*this, New, TmplAttr)) | ||||||||||||
832 | New->addAttr(NewAttr); | ||||||||||||
833 | } | ||||||||||||
834 | } | ||||||||||||
835 | } | ||||||||||||
836 | |||||||||||||
837 | /// Update instantiation attributes after template was late parsed. | ||||||||||||
838 | /// | ||||||||||||
839 | /// Some attributes are evaluated based on the body of template. If it is | ||||||||||||
840 | /// late parsed, such attributes cannot be evaluated when declaration is | ||||||||||||
841 | /// instantiated. This function is used to update instantiation attributes when | ||||||||||||
842 | /// template definition is ready. | ||||||||||||
843 | void Sema::updateAttrsForLateParsedTemplate(const Decl *Pattern, Decl *Inst) { | ||||||||||||
844 | for (const auto *Attr : Pattern->attrs()) { | ||||||||||||
845 | if (auto *A = dyn_cast<StrictFPAttr>(Attr)) { | ||||||||||||
846 | if (!Inst->hasAttr<StrictFPAttr>()) | ||||||||||||
847 | Inst->addAttr(A->clone(getASTContext())); | ||||||||||||
848 | continue; | ||||||||||||
849 | } | ||||||||||||
850 | } | ||||||||||||
851 | } | ||||||||||||
852 | |||||||||||||
853 | /// In the MS ABI, we need to instantiate default arguments of dllexported | ||||||||||||
854 | /// default constructors along with the constructor definition. This allows IR | ||||||||||||
855 | /// gen to emit a constructor closure which calls the default constructor with | ||||||||||||
856 | /// its default arguments. | ||||||||||||
857 | void Sema::InstantiateDefaultCtorDefaultArgs(CXXConstructorDecl *Ctor) { | ||||||||||||
858 | assert(Context.getTargetInfo().getCXXABI().isMicrosoft() &&(static_cast <bool> (Context.getTargetInfo().getCXXABI( ).isMicrosoft() && Ctor->isDefaultConstructor()) ? void (0) : __assert_fail ("Context.getTargetInfo().getCXXABI().isMicrosoft() && Ctor->isDefaultConstructor()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 859, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
859 | Ctor->isDefaultConstructor())(static_cast <bool> (Context.getTargetInfo().getCXXABI( ).isMicrosoft() && Ctor->isDefaultConstructor()) ? void (0) : __assert_fail ("Context.getTargetInfo().getCXXABI().isMicrosoft() && Ctor->isDefaultConstructor()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 859, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
860 | unsigned NumParams = Ctor->getNumParams(); | ||||||||||||
861 | if (NumParams == 0) | ||||||||||||
862 | return; | ||||||||||||
863 | DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>(); | ||||||||||||
864 | if (!Attr) | ||||||||||||
865 | return; | ||||||||||||
866 | for (unsigned I = 0; I != NumParams; ++I) { | ||||||||||||
867 | (void)CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor, | ||||||||||||
868 | Ctor->getParamDecl(I)); | ||||||||||||
869 | CleanupVarDeclMarking(); | ||||||||||||
870 | } | ||||||||||||
871 | } | ||||||||||||
872 | |||||||||||||
873 | /// Get the previous declaration of a declaration for the purposes of template | ||||||||||||
874 | /// instantiation. If this finds a previous declaration, then the previous | ||||||||||||
875 | /// declaration of the instantiation of D should be an instantiation of the | ||||||||||||
876 | /// result of this function. | ||||||||||||
877 | template<typename DeclT> | ||||||||||||
878 | static DeclT *getPreviousDeclForInstantiation(DeclT *D) { | ||||||||||||
879 | DeclT *Result = D->getPreviousDecl(); | ||||||||||||
880 | |||||||||||||
881 | // If the declaration is within a class, and the previous declaration was | ||||||||||||
882 | // merged from a different definition of that class, then we don't have a | ||||||||||||
883 | // previous declaration for the purpose of template instantiation. | ||||||||||||
884 | if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && | ||||||||||||
885 | D->getLexicalDeclContext() != Result->getLexicalDeclContext()) | ||||||||||||
886 | return nullptr; | ||||||||||||
887 | |||||||||||||
888 | return Result; | ||||||||||||
889 | } | ||||||||||||
890 | |||||||||||||
891 | Decl * | ||||||||||||
892 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { | ||||||||||||
893 | llvm_unreachable("Translation units cannot be instantiated")::llvm::llvm_unreachable_internal("Translation units cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 893); | ||||||||||||
894 | } | ||||||||||||
895 | |||||||||||||
896 | Decl *TemplateDeclInstantiator::VisitHLSLBufferDecl(HLSLBufferDecl *Decl) { | ||||||||||||
897 | llvm_unreachable("HLSL buffer declarations cannot be instantiated")::llvm::llvm_unreachable_internal("HLSL buffer declarations cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 897); | ||||||||||||
898 | } | ||||||||||||
899 | |||||||||||||
900 | Decl * | ||||||||||||
901 | TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { | ||||||||||||
902 | llvm_unreachable("pragma comment cannot be instantiated")::llvm::llvm_unreachable_internal("pragma comment cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 902); | ||||||||||||
903 | } | ||||||||||||
904 | |||||||||||||
905 | Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( | ||||||||||||
906 | PragmaDetectMismatchDecl *D) { | ||||||||||||
907 | llvm_unreachable("pragma comment cannot be instantiated")::llvm::llvm_unreachable_internal("pragma comment cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 907); | ||||||||||||
908 | } | ||||||||||||
909 | |||||||||||||
910 | Decl * | ||||||||||||
911 | TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { | ||||||||||||
912 | llvm_unreachable("extern \"C\" context cannot be instantiated")::llvm::llvm_unreachable_internal("extern \"C\" context cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 912); | ||||||||||||
913 | } | ||||||||||||
914 | |||||||||||||
915 | Decl *TemplateDeclInstantiator::VisitMSGuidDecl(MSGuidDecl *D) { | ||||||||||||
916 | llvm_unreachable("GUID declaration cannot be instantiated")::llvm::llvm_unreachable_internal("GUID declaration cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 916); | ||||||||||||
917 | } | ||||||||||||
918 | |||||||||||||
919 | Decl *TemplateDeclInstantiator::VisitUnnamedGlobalConstantDecl( | ||||||||||||
920 | UnnamedGlobalConstantDecl *D) { | ||||||||||||
921 | llvm_unreachable("UnnamedGlobalConstantDecl cannot be instantiated")::llvm::llvm_unreachable_internal("UnnamedGlobalConstantDecl cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 921); | ||||||||||||
922 | } | ||||||||||||
923 | |||||||||||||
924 | Decl *TemplateDeclInstantiator::VisitTemplateParamObjectDecl( | ||||||||||||
925 | TemplateParamObjectDecl *D) { | ||||||||||||
926 | llvm_unreachable("template parameter objects cannot be instantiated")::llvm::llvm_unreachable_internal("template parameter objects cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 926); | ||||||||||||
927 | } | ||||||||||||
928 | |||||||||||||
929 | Decl * | ||||||||||||
930 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { | ||||||||||||
931 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
932 | D->getIdentifier()); | ||||||||||||
933 | Owner->addDecl(Inst); | ||||||||||||
934 | return Inst; | ||||||||||||
935 | } | ||||||||||||
936 | |||||||||||||
937 | Decl * | ||||||||||||
938 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { | ||||||||||||
939 | llvm_unreachable("Namespaces cannot be instantiated")::llvm::llvm_unreachable_internal("Namespaces cannot be instantiated" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 939); | ||||||||||||
940 | } | ||||||||||||
941 | |||||||||||||
942 | Decl * | ||||||||||||
943 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { | ||||||||||||
944 | NamespaceAliasDecl *Inst | ||||||||||||
945 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, | ||||||||||||
946 | D->getNamespaceLoc(), | ||||||||||||
947 | D->getAliasLoc(), | ||||||||||||
948 | D->getIdentifier(), | ||||||||||||
949 | D->getQualifierLoc(), | ||||||||||||
950 | D->getTargetNameLoc(), | ||||||||||||
951 | D->getNamespace()); | ||||||||||||
952 | Owner->addDecl(Inst); | ||||||||||||
953 | return Inst; | ||||||||||||
954 | } | ||||||||||||
955 | |||||||||||||
956 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, | ||||||||||||
957 | bool IsTypeAlias) { | ||||||||||||
958 | bool Invalid = false; | ||||||||||||
959 | TypeSourceInfo *DI = D->getTypeSourceInfo(); | ||||||||||||
960 | if (DI->getType()->isInstantiationDependentType() || | ||||||||||||
961 | DI->getType()->isVariablyModifiedType()) { | ||||||||||||
962 | DI = SemaRef.SubstType(DI, TemplateArgs, | ||||||||||||
963 | D->getLocation(), D->getDeclName()); | ||||||||||||
964 | if (!DI) { | ||||||||||||
965 | Invalid = true; | ||||||||||||
966 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); | ||||||||||||
967 | } | ||||||||||||
968 | } else { | ||||||||||||
969 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); | ||||||||||||
970 | } | ||||||||||||
971 | |||||||||||||
972 | // HACK: 2012-10-23 g++ has a bug where it gets the value kind of ?: wrong. | ||||||||||||
973 | // libstdc++ relies upon this bug in its implementation of common_type. If we | ||||||||||||
974 | // happen to be processing that implementation, fake up the g++ ?: | ||||||||||||
975 | // semantics. See LWG issue 2141 for more information on the bug. The bugs | ||||||||||||
976 | // are fixed in g++ and libstdc++ 4.9.0 (2014-04-22). | ||||||||||||
977 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); | ||||||||||||
978 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); | ||||||||||||
979 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && | ||||||||||||
980 | DT->isReferenceType() && | ||||||||||||
981 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && | ||||||||||||
982 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && | ||||||||||||
983 | D->getIdentifier() && D->getIdentifier()->isStr("type") && | ||||||||||||
984 | SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc())) | ||||||||||||
985 | // Fold it to the (non-reference) type which g++ would have produced. | ||||||||||||
986 | DI = SemaRef.Context.getTrivialTypeSourceInfo( | ||||||||||||
987 | DI->getType().getNonReferenceType()); | ||||||||||||
988 | |||||||||||||
989 | // Create the new typedef | ||||||||||||
990 | TypedefNameDecl *Typedef; | ||||||||||||
991 | if (IsTypeAlias) | ||||||||||||
992 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), | ||||||||||||
993 | D->getLocation(), D->getIdentifier(), DI); | ||||||||||||
994 | else | ||||||||||||
995 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), | ||||||||||||
996 | D->getLocation(), D->getIdentifier(), DI); | ||||||||||||
997 | if (Invalid) | ||||||||||||
998 | Typedef->setInvalidDecl(); | ||||||||||||
999 | |||||||||||||
1000 | // If the old typedef was the name for linkage purposes of an anonymous | ||||||||||||
1001 | // tag decl, re-establish that relationship for the new typedef. | ||||||||||||
1002 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { | ||||||||||||
1003 | TagDecl *oldTag = oldTagType->getDecl(); | ||||||||||||
1004 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { | ||||||||||||
1005 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); | ||||||||||||
1006 | assert(!newTag->hasNameForLinkage())(static_cast <bool> (!newTag->hasNameForLinkage()) ? void (0) : __assert_fail ("!newTag->hasNameForLinkage()", "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1006, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1007 | newTag->setTypedefNameForAnonDecl(Typedef); | ||||||||||||
1008 | } | ||||||||||||
1009 | } | ||||||||||||
1010 | |||||||||||||
1011 | if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { | ||||||||||||
1012 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, | ||||||||||||
1013 | TemplateArgs); | ||||||||||||
1014 | if (!InstPrev) | ||||||||||||
1015 | return nullptr; | ||||||||||||
1016 | |||||||||||||
1017 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); | ||||||||||||
1018 | |||||||||||||
1019 | // If the typedef types are not identical, reject them. | ||||||||||||
1020 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); | ||||||||||||
1021 | |||||||||||||
1022 | Typedef->setPreviousDecl(InstPrevTypedef); | ||||||||||||
1023 | } | ||||||||||||
1024 | |||||||||||||
1025 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); | ||||||||||||
1026 | |||||||||||||
1027 | if (D->getUnderlyingType()->getAs<DependentNameType>()) | ||||||||||||
1028 | SemaRef.inferGslPointerAttribute(Typedef); | ||||||||||||
1029 | |||||||||||||
1030 | Typedef->setAccess(D->getAccess()); | ||||||||||||
1031 | Typedef->setReferenced(D->isReferenced()); | ||||||||||||
1032 | |||||||||||||
1033 | return Typedef; | ||||||||||||
1034 | } | ||||||||||||
1035 | |||||||||||||
1036 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { | ||||||||||||
1037 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); | ||||||||||||
1038 | if (Typedef) | ||||||||||||
1039 | Owner->addDecl(Typedef); | ||||||||||||
1040 | return Typedef; | ||||||||||||
1041 | } | ||||||||||||
1042 | |||||||||||||
1043 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { | ||||||||||||
1044 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); | ||||||||||||
1045 | if (Typedef) | ||||||||||||
1046 | Owner->addDecl(Typedef); | ||||||||||||
1047 | return Typedef; | ||||||||||||
1048 | } | ||||||||||||
1049 | |||||||||||||
1050 | Decl * | ||||||||||||
1051 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { | ||||||||||||
1052 | // Create a local instantiation scope for this type alias template, which | ||||||||||||
1053 | // will contain the instantiations of the template parameters. | ||||||||||||
1054 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
1055 | |||||||||||||
1056 | TemplateParameterList *TempParams = D->getTemplateParameters(); | ||||||||||||
1057 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
1058 | if (!InstParams) | ||||||||||||
1059 | return nullptr; | ||||||||||||
1060 | |||||||||||||
1061 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); | ||||||||||||
1062 | |||||||||||||
1063 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; | ||||||||||||
1064 | if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { | ||||||||||||
1065 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); | ||||||||||||
1066 | if (!Found.empty()) { | ||||||||||||
1067 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); | ||||||||||||
1068 | } | ||||||||||||
1069 | } | ||||||||||||
1070 | |||||||||||||
1071 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( | ||||||||||||
1072 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); | ||||||||||||
1073 | if (!AliasInst) | ||||||||||||
1074 | return nullptr; | ||||||||||||
1075 | |||||||||||||
1076 | TypeAliasTemplateDecl *Inst | ||||||||||||
1077 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
1078 | D->getDeclName(), InstParams, AliasInst); | ||||||||||||
1079 | AliasInst->setDescribedAliasTemplate(Inst); | ||||||||||||
1080 | if (PrevAliasTemplate) | ||||||||||||
1081 | Inst->setPreviousDecl(PrevAliasTemplate); | ||||||||||||
1082 | |||||||||||||
1083 | Inst->setAccess(D->getAccess()); | ||||||||||||
1084 | |||||||||||||
1085 | if (!PrevAliasTemplate) | ||||||||||||
1086 | Inst->setInstantiatedFromMemberTemplate(D); | ||||||||||||
1087 | |||||||||||||
1088 | Owner->addDecl(Inst); | ||||||||||||
1089 | |||||||||||||
1090 | return Inst; | ||||||||||||
1091 | } | ||||||||||||
1092 | |||||||||||||
1093 | Decl *TemplateDeclInstantiator::VisitBindingDecl(BindingDecl *D) { | ||||||||||||
1094 | auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
1095 | D->getIdentifier()); | ||||||||||||
1096 | NewBD->setReferenced(D->isReferenced()); | ||||||||||||
1097 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD); | ||||||||||||
1098 | return NewBD; | ||||||||||||
1099 | } | ||||||||||||
1100 | |||||||||||||
1101 | Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) { | ||||||||||||
1102 | // Transform the bindings first. | ||||||||||||
1103 | SmallVector<BindingDecl*, 16> NewBindings; | ||||||||||||
1104 | for (auto *OldBD : D->bindings()) | ||||||||||||
1105 | NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD))); | ||||||||||||
1106 | ArrayRef<BindingDecl*> NewBindingArray = NewBindings; | ||||||||||||
1107 | |||||||||||||
1108 | auto *NewDD = cast_or_null<DecompositionDecl>( | ||||||||||||
1109 | VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray)); | ||||||||||||
1110 | |||||||||||||
1111 | if (!NewDD || NewDD->isInvalidDecl()) | ||||||||||||
1112 | for (auto *NewBD : NewBindings) | ||||||||||||
1113 | NewBD->setInvalidDecl(); | ||||||||||||
1114 | |||||||||||||
1115 | return NewDD; | ||||||||||||
1116 | } | ||||||||||||
1117 | |||||||||||||
1118 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { | ||||||||||||
1119 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); | ||||||||||||
1120 | } | ||||||||||||
1121 | |||||||||||||
1122 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, | ||||||||||||
1123 | bool InstantiatingVarTemplate, | ||||||||||||
1124 | ArrayRef<BindingDecl*> *Bindings) { | ||||||||||||
1125 | |||||||||||||
1126 | // Do substitution on the type of the declaration | ||||||||||||
1127 | TypeSourceInfo *DI = SemaRef.SubstType( | ||||||||||||
1128 | D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(), | ||||||||||||
1129 | D->getDeclName(), /*AllowDeducedTST*/true); | ||||||||||||
1130 | if (!DI) | ||||||||||||
1131 | return nullptr; | ||||||||||||
1132 | |||||||||||||
1133 | if (DI->getType()->isFunctionType()) { | ||||||||||||
1134 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) | ||||||||||||
1135 | << D->isStaticDataMember() << DI->getType(); | ||||||||||||
1136 | return nullptr; | ||||||||||||
1137 | } | ||||||||||||
1138 | |||||||||||||
1139 | DeclContext *DC = Owner; | ||||||||||||
1140 | if (D->isLocalExternDecl()) | ||||||||||||
1141 | SemaRef.adjustContextForLocalExternDecl(DC); | ||||||||||||
1142 | |||||||||||||
1143 | // Build the instantiated declaration. | ||||||||||||
1144 | VarDecl *Var; | ||||||||||||
1145 | if (Bindings) | ||||||||||||
1146 | Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), | ||||||||||||
1147 | D->getLocation(), DI->getType(), DI, | ||||||||||||
1148 | D->getStorageClass(), *Bindings); | ||||||||||||
1149 | else | ||||||||||||
1150 | Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), | ||||||||||||
1151 | D->getLocation(), D->getIdentifier(), DI->getType(), | ||||||||||||
1152 | DI, D->getStorageClass()); | ||||||||||||
1153 | |||||||||||||
1154 | // In ARC, infer 'retaining' for variables of retainable type. | ||||||||||||
1155 | if (SemaRef.getLangOpts().ObjCAutoRefCount && | ||||||||||||
1156 | SemaRef.inferObjCARCLifetime(Var)) | ||||||||||||
1157 | Var->setInvalidDecl(); | ||||||||||||
1158 | |||||||||||||
1159 | if (SemaRef.getLangOpts().OpenCL) | ||||||||||||
1160 | SemaRef.deduceOpenCLAddressSpace(Var); | ||||||||||||
1161 | |||||||||||||
1162 | // Substitute the nested name specifier, if any. | ||||||||||||
1163 | if (SubstQualifier(D, Var)) | ||||||||||||
1164 | return nullptr; | ||||||||||||
1165 | |||||||||||||
1166 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, | ||||||||||||
1167 | StartingScope, InstantiatingVarTemplate); | ||||||||||||
1168 | if (D->isNRVOVariable() && !Var->isInvalidDecl()) { | ||||||||||||
1169 | QualType RT; | ||||||||||||
1170 | if (auto *F = dyn_cast<FunctionDecl>(DC)) | ||||||||||||
1171 | RT = F->getReturnType(); | ||||||||||||
1172 | else if (isa<BlockDecl>(DC)) | ||||||||||||
1173 | RT = cast<FunctionType>(SemaRef.getCurBlock()->FunctionType) | ||||||||||||
1174 | ->getReturnType(); | ||||||||||||
1175 | else | ||||||||||||
1176 | llvm_unreachable("Unknown context type")::llvm::llvm_unreachable_internal("Unknown context type", "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp" , 1176); | ||||||||||||
1177 | |||||||||||||
1178 | // This is the last chance we have of checking copy elision eligibility | ||||||||||||
1179 | // for functions in dependent contexts. The sema actions for building | ||||||||||||
1180 | // the return statement during template instantiation will have no effect | ||||||||||||
1181 | // regarding copy elision, since NRVO propagation runs on the scope exit | ||||||||||||
1182 | // actions, and these are not run on instantiation. | ||||||||||||
1183 | // This might run through some VarDecls which were returned from non-taken | ||||||||||||
1184 | // 'if constexpr' branches, and these will end up being constructed on the | ||||||||||||
1185 | // return slot even if they will never be returned, as a sort of accidental | ||||||||||||
1186 | // 'optimization'. Notably, functions with 'auto' return types won't have it | ||||||||||||
1187 | // deduced by this point. Coupled with the limitation described | ||||||||||||
1188 | // previously, this makes it very hard to support copy elision for these. | ||||||||||||
1189 | Sema::NamedReturnInfo Info = SemaRef.getNamedReturnInfo(Var); | ||||||||||||
1190 | bool NRVO = SemaRef.getCopyElisionCandidate(Info, RT) != nullptr; | ||||||||||||
1191 | Var->setNRVOVariable(NRVO); | ||||||||||||
1192 | } | ||||||||||||
1193 | |||||||||||||
1194 | Var->setImplicit(D->isImplicit()); | ||||||||||||
1195 | |||||||||||||
1196 | if (Var->isStaticLocal()) | ||||||||||||
1197 | SemaRef.CheckStaticLocalForDllExport(Var); | ||||||||||||
1198 | |||||||||||||
1199 | if (Var->getTLSKind()) | ||||||||||||
1200 | SemaRef.CheckThreadLocalForLargeAlignment(Var); | ||||||||||||
1201 | |||||||||||||
1202 | return Var; | ||||||||||||
1203 | } | ||||||||||||
1204 | |||||||||||||
1205 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { | ||||||||||||
1206 | AccessSpecDecl* AD | ||||||||||||
1207 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, | ||||||||||||
1208 | D->getAccessSpecifierLoc(), D->getColonLoc()); | ||||||||||||
1209 | Owner->addHiddenDecl(AD); | ||||||||||||
1210 | return AD; | ||||||||||||
1211 | } | ||||||||||||
1212 | |||||||||||||
1213 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { | ||||||||||||
1214 | bool Invalid = false; | ||||||||||||
1215 | TypeSourceInfo *DI = D->getTypeSourceInfo(); | ||||||||||||
1216 | if (DI->getType()->isInstantiationDependentType() || | ||||||||||||
1217 | DI->getType()->isVariablyModifiedType()) { | ||||||||||||
1218 | DI = SemaRef.SubstType(DI, TemplateArgs, | ||||||||||||
1219 | D->getLocation(), D->getDeclName()); | ||||||||||||
1220 | if (!DI) { | ||||||||||||
1221 | DI = D->getTypeSourceInfo(); | ||||||||||||
1222 | Invalid = true; | ||||||||||||
1223 | } else if (DI->getType()->isFunctionType()) { | ||||||||||||
1224 | // C++ [temp.arg.type]p3: | ||||||||||||
1225 | // If a declaration acquires a function type through a type | ||||||||||||
1226 | // dependent on a template-parameter and this causes a | ||||||||||||
1227 | // declaration that does not use the syntactic form of a | ||||||||||||
1228 | // function declarator to have function type, the program is | ||||||||||||
1229 | // ill-formed. | ||||||||||||
1230 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) | ||||||||||||
1231 | << DI->getType(); | ||||||||||||
1232 | Invalid = true; | ||||||||||||
1233 | } | ||||||||||||
1234 | } else { | ||||||||||||
1235 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); | ||||||||||||
1236 | } | ||||||||||||
1237 | |||||||||||||
1238 | Expr *BitWidth = D->getBitWidth(); | ||||||||||||
1239 | if (Invalid) | ||||||||||||
1240 | BitWidth = nullptr; | ||||||||||||
1241 | else if (BitWidth) { | ||||||||||||
1242 | // The bit-width expression is a constant expression. | ||||||||||||
1243 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
1244 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
1245 | |||||||||||||
1246 | ExprResult InstantiatedBitWidth | ||||||||||||
1247 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); | ||||||||||||
1248 | if (InstantiatedBitWidth.isInvalid()) { | ||||||||||||
1249 | Invalid = true; | ||||||||||||
1250 | BitWidth = nullptr; | ||||||||||||
1251 | } else | ||||||||||||
1252 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); | ||||||||||||
1253 | } | ||||||||||||
1254 | |||||||||||||
1255 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), | ||||||||||||
1256 | DI->getType(), DI, | ||||||||||||
1257 | cast<RecordDecl>(Owner), | ||||||||||||
1258 | D->getLocation(), | ||||||||||||
1259 | D->isMutable(), | ||||||||||||
1260 | BitWidth, | ||||||||||||
1261 | D->getInClassInitStyle(), | ||||||||||||
1262 | D->getInnerLocStart(), | ||||||||||||
1263 | D->getAccess(), | ||||||||||||
1264 | nullptr); | ||||||||||||
1265 | if (!Field) { | ||||||||||||
1266 | cast<Decl>(Owner)->setInvalidDecl(); | ||||||||||||
1267 | return nullptr; | ||||||||||||
1268 | } | ||||||||||||
1269 | |||||||||||||
1270 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); | ||||||||||||
1271 | |||||||||||||
1272 | if (Field->hasAttrs()) | ||||||||||||
1273 | SemaRef.CheckAlignasUnderalignment(Field); | ||||||||||||
1274 | |||||||||||||
1275 | if (Invalid) | ||||||||||||
1276 | Field->setInvalidDecl(); | ||||||||||||
1277 | |||||||||||||
1278 | if (!Field->getDeclName()) { | ||||||||||||
1279 | // Keep track of where this decl came from. | ||||||||||||
1280 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); | ||||||||||||
1281 | } | ||||||||||||
1282 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { | ||||||||||||
1283 | if (Parent->isAnonymousStructOrUnion() && | ||||||||||||
1284 | Parent->getRedeclContext()->isFunctionOrMethod()) | ||||||||||||
1285 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); | ||||||||||||
1286 | } | ||||||||||||
1287 | |||||||||||||
1288 | Field->setImplicit(D->isImplicit()); | ||||||||||||
1289 | Field->setAccess(D->getAccess()); | ||||||||||||
1290 | Owner->addDecl(Field); | ||||||||||||
1291 | |||||||||||||
1292 | return Field; | ||||||||||||
1293 | } | ||||||||||||
1294 | |||||||||||||
1295 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { | ||||||||||||
1296 | bool Invalid = false; | ||||||||||||
1297 | TypeSourceInfo *DI = D->getTypeSourceInfo(); | ||||||||||||
1298 | |||||||||||||
1299 | if (DI->getType()->isVariablyModifiedType()) { | ||||||||||||
1300 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) | ||||||||||||
1301 | << D; | ||||||||||||
1302 | Invalid = true; | ||||||||||||
1303 | } else if (DI->getType()->isInstantiationDependentType()) { | ||||||||||||
1304 | DI = SemaRef.SubstType(DI, TemplateArgs, | ||||||||||||
1305 | D->getLocation(), D->getDeclName()); | ||||||||||||
1306 | if (!DI) { | ||||||||||||
1307 | DI = D->getTypeSourceInfo(); | ||||||||||||
1308 | Invalid = true; | ||||||||||||
1309 | } else if (DI->getType()->isFunctionType()) { | ||||||||||||
1310 | // C++ [temp.arg.type]p3: | ||||||||||||
1311 | // If a declaration acquires a function type through a type | ||||||||||||
1312 | // dependent on a template-parameter and this causes a | ||||||||||||
1313 | // declaration that does not use the syntactic form of a | ||||||||||||
1314 | // function declarator to have function type, the program is | ||||||||||||
1315 | // ill-formed. | ||||||||||||
1316 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) | ||||||||||||
1317 | << DI->getType(); | ||||||||||||
1318 | Invalid = true; | ||||||||||||
1319 | } | ||||||||||||
1320 | } else { | ||||||||||||
1321 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); | ||||||||||||
1322 | } | ||||||||||||
1323 | |||||||||||||
1324 | MSPropertyDecl *Property = MSPropertyDecl::Create( | ||||||||||||
1325 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), | ||||||||||||
1326 | DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId()); | ||||||||||||
1327 | |||||||||||||
1328 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, | ||||||||||||
1329 | StartingScope); | ||||||||||||
1330 | |||||||||||||
1331 | if (Invalid) | ||||||||||||
1332 | Property->setInvalidDecl(); | ||||||||||||
1333 | |||||||||||||
1334 | Property->setAccess(D->getAccess()); | ||||||||||||
1335 | Owner->addDecl(Property); | ||||||||||||
1336 | |||||||||||||
1337 | return Property; | ||||||||||||
1338 | } | ||||||||||||
1339 | |||||||||||||
1340 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { | ||||||||||||
1341 | NamedDecl **NamedChain = | ||||||||||||
1342 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; | ||||||||||||
1343 | |||||||||||||
1344 | int i = 0; | ||||||||||||
1345 | for (auto *PI : D->chain()) { | ||||||||||||
1346 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, | ||||||||||||
1347 | TemplateArgs); | ||||||||||||
1348 | if (!Next) | ||||||||||||
1349 | return nullptr; | ||||||||||||
1350 | |||||||||||||
1351 | NamedChain[i++] = Next; | ||||||||||||
1352 | } | ||||||||||||
1353 | |||||||||||||
1354 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); | ||||||||||||
1355 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( | ||||||||||||
1356 | SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, | ||||||||||||
1357 | {NamedChain, D->getChainingSize()}); | ||||||||||||
1358 | |||||||||||||
1359 | for (const auto *Attr : D->attrs()) | ||||||||||||
1360 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); | ||||||||||||
1361 | |||||||||||||
1362 | IndirectField->setImplicit(D->isImplicit()); | ||||||||||||
1363 | IndirectField->setAccess(D->getAccess()); | ||||||||||||
1364 | Owner->addDecl(IndirectField); | ||||||||||||
1365 | return IndirectField; | ||||||||||||
1366 | } | ||||||||||||
1367 | |||||||||||||
1368 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { | ||||||||||||
1369 | // Handle friend type expressions by simply substituting template | ||||||||||||
1370 | // parameters into the pattern type and checking the result. | ||||||||||||
1371 | if (TypeSourceInfo *Ty = D->getFriendType()) { | ||||||||||||
1372 | TypeSourceInfo *InstTy; | ||||||||||||
1373 | // If this is an unsupported friend, don't bother substituting template | ||||||||||||
1374 | // arguments into it. The actual type referred to won't be used by any | ||||||||||||
1375 | // parts of Clang, and may not be valid for instantiating. Just use the | ||||||||||||
1376 | // same info for the instantiated friend. | ||||||||||||
1377 | if (D->isUnsupportedFriend()) { | ||||||||||||
1378 | InstTy = Ty; | ||||||||||||
1379 | } else { | ||||||||||||
1380 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, | ||||||||||||
1381 | D->getLocation(), DeclarationName()); | ||||||||||||
1382 | } | ||||||||||||
1383 | if (!InstTy) | ||||||||||||
1384 | return nullptr; | ||||||||||||
1385 | |||||||||||||
1386 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getBeginLoc(), | ||||||||||||
1387 | D->getFriendLoc(), InstTy); | ||||||||||||
1388 | if (!FD) | ||||||||||||
1389 | return nullptr; | ||||||||||||
1390 | |||||||||||||
1391 | FD->setAccess(AS_public); | ||||||||||||
1392 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); | ||||||||||||
1393 | Owner->addDecl(FD); | ||||||||||||
1394 | return FD; | ||||||||||||
1395 | } | ||||||||||||
1396 | |||||||||||||
1397 | NamedDecl *ND = D->getFriendDecl(); | ||||||||||||
1398 | assert(ND && "friend decl must be a decl or a type!")(static_cast <bool> (ND && "friend decl must be a decl or a type!" ) ? void (0) : __assert_fail ("ND && \"friend decl must be a decl or a type!\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1398, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1399 | |||||||||||||
1400 | // All of the Visit implementations for the various potential friend | ||||||||||||
1401 | // declarations have to be carefully written to work for friend | ||||||||||||
1402 | // objects, with the most important detail being that the target | ||||||||||||
1403 | // decl should almost certainly not be placed in Owner. | ||||||||||||
1404 | Decl *NewND = Visit(ND); | ||||||||||||
1405 | if (!NewND) return nullptr; | ||||||||||||
1406 | |||||||||||||
1407 | FriendDecl *FD = | ||||||||||||
1408 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
1409 | cast<NamedDecl>(NewND), D->getFriendLoc()); | ||||||||||||
1410 | FD->setAccess(AS_public); | ||||||||||||
1411 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); | ||||||||||||
1412 | Owner->addDecl(FD); | ||||||||||||
1413 | return FD; | ||||||||||||
1414 | } | ||||||||||||
1415 | |||||||||||||
1416 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { | ||||||||||||
1417 | Expr *AssertExpr = D->getAssertExpr(); | ||||||||||||
1418 | |||||||||||||
1419 | // The expression in a static assertion is a constant expression. | ||||||||||||
1420 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
1421 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
1422 | |||||||||||||
1423 | ExprResult InstantiatedAssertExpr | ||||||||||||
1424 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); | ||||||||||||
1425 | if (InstantiatedAssertExpr.isInvalid()) | ||||||||||||
1426 | return nullptr; | ||||||||||||
1427 | |||||||||||||
1428 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), | ||||||||||||
1429 | InstantiatedAssertExpr.get(), | ||||||||||||
1430 | D->getMessage(), | ||||||||||||
1431 | D->getRParenLoc(), | ||||||||||||
1432 | D->isFailed()); | ||||||||||||
1433 | } | ||||||||||||
1434 | |||||||||||||
1435 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { | ||||||||||||
1436 | EnumDecl *PrevDecl = nullptr; | ||||||||||||
1437 | if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { | ||||||||||||
1438 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), | ||||||||||||
1439 | PatternPrev, | ||||||||||||
1440 | TemplateArgs); | ||||||||||||
1441 | if (!Prev) return nullptr; | ||||||||||||
1442 | PrevDecl = cast<EnumDecl>(Prev); | ||||||||||||
1443 | } | ||||||||||||
1444 | |||||||||||||
1445 | EnumDecl *Enum = | ||||||||||||
1446 | EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), | ||||||||||||
1447 | D->getLocation(), D->getIdentifier(), PrevDecl, | ||||||||||||
1448 | D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); | ||||||||||||
1449 | if (D->isFixed()) { | ||||||||||||
1450 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { | ||||||||||||
1451 | // If we have type source information for the underlying type, it means it | ||||||||||||
1452 | // has been explicitly set by the user. Perform substitution on it before | ||||||||||||
1453 | // moving on. | ||||||||||||
1454 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); | ||||||||||||
1455 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, | ||||||||||||
1456 | DeclarationName()); | ||||||||||||
1457 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) | ||||||||||||
1458 | Enum->setIntegerType(SemaRef.Context.IntTy); | ||||||||||||
1459 | else | ||||||||||||
1460 | Enum->setIntegerTypeSourceInfo(NewTI); | ||||||||||||
1461 | } else { | ||||||||||||
1462 | assert(!D->getIntegerType()->isDependentType()(static_cast <bool> (!D->getIntegerType()->isDependentType () && "Dependent type without type source info") ? void (0) : __assert_fail ("!D->getIntegerType()->isDependentType() && \"Dependent type without type source info\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1463, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
1463 | && "Dependent type without type source info")(static_cast <bool> (!D->getIntegerType()->isDependentType () && "Dependent type without type source info") ? void (0) : __assert_fail ("!D->getIntegerType()->isDependentType() && \"Dependent type without type source info\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1463, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1464 | Enum->setIntegerType(D->getIntegerType()); | ||||||||||||
1465 | } | ||||||||||||
1466 | } | ||||||||||||
1467 | |||||||||||||
1468 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); | ||||||||||||
1469 | |||||||||||||
1470 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); | ||||||||||||
1471 | Enum->setAccess(D->getAccess()); | ||||||||||||
1472 | // Forward the mangling number from the template to the instantiated decl. | ||||||||||||
1473 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); | ||||||||||||
1474 | // See if the old tag was defined along with a declarator. | ||||||||||||
1475 | // If it did, mark the new tag as being associated with that declarator. | ||||||||||||
1476 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) | ||||||||||||
1477 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Enum, DD); | ||||||||||||
1478 | // See if the old tag was defined along with a typedef. | ||||||||||||
1479 | // If it did, mark the new tag as being associated with that typedef. | ||||||||||||
1480 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) | ||||||||||||
1481 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Enum, TND); | ||||||||||||
1482 | if (SubstQualifier(D, Enum)) return nullptr; | ||||||||||||
1483 | Owner->addDecl(Enum); | ||||||||||||
1484 | |||||||||||||
1485 | EnumDecl *Def = D->getDefinition(); | ||||||||||||
1486 | if (Def && Def != D) { | ||||||||||||
1487 | // If this is an out-of-line definition of an enum member template, check | ||||||||||||
1488 | // that the underlying types match in the instantiation of both | ||||||||||||
1489 | // declarations. | ||||||||||||
1490 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { | ||||||||||||
1491 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); | ||||||||||||
1492 | QualType DefnUnderlying = | ||||||||||||
1493 | SemaRef.SubstType(TI->getType(), TemplateArgs, | ||||||||||||
1494 | UnderlyingLoc, DeclarationName()); | ||||||||||||
1495 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), | ||||||||||||
1496 | DefnUnderlying, /*IsFixed=*/true, Enum); | ||||||||||||
1497 | } | ||||||||||||
1498 | } | ||||||||||||
1499 | |||||||||||||
1500 | // C++11 [temp.inst]p1: The implicit instantiation of a class template | ||||||||||||
1501 | // specialization causes the implicit instantiation of the declarations, but | ||||||||||||
1502 | // not the definitions of scoped member enumerations. | ||||||||||||
1503 | // | ||||||||||||
1504 | // DR1484 clarifies that enumeration definitions inside of a template | ||||||||||||
1505 | // declaration aren't considered entities that can be separately instantiated | ||||||||||||
1506 | // from the rest of the entity they are declared inside of. | ||||||||||||
1507 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { | ||||||||||||
1508 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); | ||||||||||||
1509 | InstantiateEnumDefinition(Enum, Def); | ||||||||||||
1510 | } | ||||||||||||
1511 | |||||||||||||
1512 | return Enum; | ||||||||||||
1513 | } | ||||||||||||
1514 | |||||||||||||
1515 | void TemplateDeclInstantiator::InstantiateEnumDefinition( | ||||||||||||
1516 | EnumDecl *Enum, EnumDecl *Pattern) { | ||||||||||||
1517 | Enum->startDefinition(); | ||||||||||||
1518 | |||||||||||||
1519 | // Update the location to refer to the definition. | ||||||||||||
1520 | Enum->setLocation(Pattern->getLocation()); | ||||||||||||
1521 | |||||||||||||
1522 | SmallVector<Decl*, 4> Enumerators; | ||||||||||||
1523 | |||||||||||||
1524 | EnumConstantDecl *LastEnumConst = nullptr; | ||||||||||||
1525 | for (auto *EC : Pattern->enumerators()) { | ||||||||||||
1526 | // The specified value for the enumerator. | ||||||||||||
1527 | ExprResult Value((Expr *)nullptr); | ||||||||||||
1528 | if (Expr *UninstValue = EC->getInitExpr()) { | ||||||||||||
1529 | // The enumerator's value expression is a constant expression. | ||||||||||||
1530 | EnterExpressionEvaluationContext Unevaluated( | ||||||||||||
1531 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
1532 | |||||||||||||
1533 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); | ||||||||||||
1534 | } | ||||||||||||
1535 | |||||||||||||
1536 | // Drop the initial value and continue. | ||||||||||||
1537 | bool isInvalid = false; | ||||||||||||
1538 | if (Value.isInvalid()) { | ||||||||||||
1539 | Value = nullptr; | ||||||||||||
1540 | isInvalid = true; | ||||||||||||
1541 | } | ||||||||||||
1542 | |||||||||||||
1543 | EnumConstantDecl *EnumConst | ||||||||||||
1544 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, | ||||||||||||
1545 | EC->getLocation(), EC->getIdentifier(), | ||||||||||||
1546 | Value.get()); | ||||||||||||
1547 | |||||||||||||
1548 | if (isInvalid) { | ||||||||||||
1549 | if (EnumConst) | ||||||||||||
1550 | EnumConst->setInvalidDecl(); | ||||||||||||
1551 | Enum->setInvalidDecl(); | ||||||||||||
1552 | } | ||||||||||||
1553 | |||||||||||||
1554 | if (EnumConst) { | ||||||||||||
1555 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); | ||||||||||||
1556 | |||||||||||||
1557 | EnumConst->setAccess(Enum->getAccess()); | ||||||||||||
1558 | Enum->addDecl(EnumConst); | ||||||||||||
1559 | Enumerators.push_back(EnumConst); | ||||||||||||
1560 | LastEnumConst = EnumConst; | ||||||||||||
1561 | |||||||||||||
1562 | if (Pattern->getDeclContext()->isFunctionOrMethod() && | ||||||||||||
1563 | !Enum->isScoped()) { | ||||||||||||
1564 | // If the enumeration is within a function or method, record the enum | ||||||||||||
1565 | // constant as a local. | ||||||||||||
1566 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); | ||||||||||||
1567 | } | ||||||||||||
1568 | } | ||||||||||||
1569 | } | ||||||||||||
1570 | |||||||||||||
1571 | SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, | ||||||||||||
1572 | Enumerators, nullptr, ParsedAttributesView()); | ||||||||||||
1573 | } | ||||||||||||
1574 | |||||||||||||
1575 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { | ||||||||||||
1576 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.")::llvm::llvm_unreachable_internal("EnumConstantDecls can only occur within EnumDecls." , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1576); | ||||||||||||
1577 | } | ||||||||||||
1578 | |||||||||||||
1579 | Decl * | ||||||||||||
1580 | TemplateDeclInstantiator::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { | ||||||||||||
1581 | llvm_unreachable("BuiltinTemplateDecls cannot be instantiated.")::llvm::llvm_unreachable_internal("BuiltinTemplateDecls cannot be instantiated." , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1581); | ||||||||||||
1582 | } | ||||||||||||
1583 | |||||||||||||
1584 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { | ||||||||||||
1585 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); | ||||||||||||
1586 | |||||||||||||
1587 | // Create a local instantiation scope for this class template, which | ||||||||||||
1588 | // will contain the instantiations of the template parameters. | ||||||||||||
1589 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
1590 | TemplateParameterList *TempParams = D->getTemplateParameters(); | ||||||||||||
1591 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
1592 | if (!InstParams) | ||||||||||||
1593 | return nullptr; | ||||||||||||
1594 | |||||||||||||
1595 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); | ||||||||||||
1596 | |||||||||||||
1597 | // Instantiate the qualifier. We have to do this first in case | ||||||||||||
1598 | // we're a friend declaration, because if we are then we need to put | ||||||||||||
1599 | // the new declaration in the appropriate context. | ||||||||||||
1600 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); | ||||||||||||
1601 | if (QualifierLoc) { | ||||||||||||
1602 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | ||||||||||||
1603 | TemplateArgs); | ||||||||||||
1604 | if (!QualifierLoc) | ||||||||||||
1605 | return nullptr; | ||||||||||||
1606 | } | ||||||||||||
1607 | |||||||||||||
1608 | CXXRecordDecl *PrevDecl = nullptr; | ||||||||||||
1609 | ClassTemplateDecl *PrevClassTemplate = nullptr; | ||||||||||||
1610 | |||||||||||||
1611 | if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { | ||||||||||||
1612 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); | ||||||||||||
1613 | if (!Found.empty()) { | ||||||||||||
1614 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); | ||||||||||||
1615 | if (PrevClassTemplate) | ||||||||||||
1616 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); | ||||||||||||
1617 | } | ||||||||||||
1618 | } | ||||||||||||
1619 | |||||||||||||
1620 | // If this isn't a friend, then it's a member template, in which | ||||||||||||
1621 | // case we just want to build the instantiation in the | ||||||||||||
1622 | // specialization. If it is a friend, we want to build it in | ||||||||||||
1623 | // the appropriate context. | ||||||||||||
1624 | DeclContext *DC = Owner; | ||||||||||||
1625 | if (isFriend) { | ||||||||||||
1626 | if (QualifierLoc) { | ||||||||||||
1627 | CXXScopeSpec SS; | ||||||||||||
1628 | SS.Adopt(QualifierLoc); | ||||||||||||
1629 | DC = SemaRef.computeDeclContext(SS); | ||||||||||||
1630 | if (!DC) return nullptr; | ||||||||||||
1631 | } else { | ||||||||||||
1632 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), | ||||||||||||
1633 | Pattern->getDeclContext(), | ||||||||||||
1634 | TemplateArgs); | ||||||||||||
1635 | } | ||||||||||||
1636 | |||||||||||||
1637 | // Look for a previous declaration of the template in the owning | ||||||||||||
1638 | // context. | ||||||||||||
1639 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), | ||||||||||||
1640 | Sema::LookupOrdinaryName, | ||||||||||||
1641 | SemaRef.forRedeclarationInCurContext()); | ||||||||||||
1642 | SemaRef.LookupQualifiedName(R, DC); | ||||||||||||
1643 | |||||||||||||
1644 | if (R.isSingleResult()) { | ||||||||||||
1645 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); | ||||||||||||
1646 | if (PrevClassTemplate) | ||||||||||||
1647 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); | ||||||||||||
1648 | } | ||||||||||||
1649 | |||||||||||||
1650 | if (!PrevClassTemplate && QualifierLoc) { | ||||||||||||
1651 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) | ||||||||||||
1652 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC | ||||||||||||
1653 | << QualifierLoc.getSourceRange(); | ||||||||||||
1654 | return nullptr; | ||||||||||||
1655 | } | ||||||||||||
1656 | |||||||||||||
1657 | if (PrevClassTemplate) { | ||||||||||||
1658 | const ClassTemplateDecl *MostRecentPrevCT = | ||||||||||||
1659 | PrevClassTemplate->getMostRecentDecl(); | ||||||||||||
1660 | TemplateParameterList *PrevParams = | ||||||||||||
1661 | MostRecentPrevCT->getTemplateParameters(); | ||||||||||||
1662 | |||||||||||||
1663 | // Make sure the parameter lists match. | ||||||||||||
1664 | if (!SemaRef.TemplateParameterListsAreEqual( | ||||||||||||
1665 | D->getTemplatedDecl(), InstParams, | ||||||||||||
1666 | MostRecentPrevCT->getTemplatedDecl(), PrevParams, true, | ||||||||||||
1667 | Sema::TPL_TemplateMatch)) | ||||||||||||
1668 | return nullptr; | ||||||||||||
1669 | |||||||||||||
1670 | // Do some additional validation, then merge default arguments | ||||||||||||
1671 | // from the existing declarations. | ||||||||||||
1672 | if (SemaRef.CheckTemplateParameterList(InstParams, PrevParams, | ||||||||||||
1673 | Sema::TPC_ClassTemplate)) | ||||||||||||
1674 | return nullptr; | ||||||||||||
1675 | } | ||||||||||||
1676 | } | ||||||||||||
1677 | |||||||||||||
1678 | CXXRecordDecl *RecordInst = CXXRecordDecl::Create( | ||||||||||||
1679 | SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(), | ||||||||||||
1680 | Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl, | ||||||||||||
1681 | /*DelayTypeCreation=*/true); | ||||||||||||
1682 | |||||||||||||
1683 | if (QualifierLoc) | ||||||||||||
1684 | RecordInst->setQualifierInfo(QualifierLoc); | ||||||||||||
1685 | |||||||||||||
1686 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, Pattern, RecordInst, LateAttrs, | ||||||||||||
1687 | StartingScope); | ||||||||||||
1688 | |||||||||||||
1689 | ClassTemplateDecl *Inst | ||||||||||||
1690 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), | ||||||||||||
1691 | D->getIdentifier(), InstParams, RecordInst); | ||||||||||||
1692 | assert(!(isFriend && Owner->isDependentContext()))(static_cast <bool> (!(isFriend && Owner->isDependentContext ())) ? void (0) : __assert_fail ("!(isFriend && Owner->isDependentContext())" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1692, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1693 | Inst->setPreviousDecl(PrevClassTemplate); | ||||||||||||
1694 | |||||||||||||
1695 | RecordInst->setDescribedClassTemplate(Inst); | ||||||||||||
1696 | |||||||||||||
1697 | if (isFriend) { | ||||||||||||
1698 | if (PrevClassTemplate) | ||||||||||||
1699 | Inst->setAccess(PrevClassTemplate->getAccess()); | ||||||||||||
1700 | else | ||||||||||||
1701 | Inst->setAccess(D->getAccess()); | ||||||||||||
1702 | |||||||||||||
1703 | Inst->setObjectOfFriendDecl(); | ||||||||||||
1704 | // TODO: do we want to track the instantiation progeny of this | ||||||||||||
1705 | // friend target decl? | ||||||||||||
1706 | } else { | ||||||||||||
1707 | Inst->setAccess(D->getAccess()); | ||||||||||||
1708 | if (!PrevClassTemplate) | ||||||||||||
1709 | Inst->setInstantiatedFromMemberTemplate(D); | ||||||||||||
1710 | } | ||||||||||||
1711 | |||||||||||||
1712 | // Trigger creation of the type for the instantiation. | ||||||||||||
1713 | SemaRef.Context.getInjectedClassNameType(RecordInst, | ||||||||||||
1714 | Inst->getInjectedClassNameSpecialization()); | ||||||||||||
1715 | |||||||||||||
1716 | // Finish handling of friends. | ||||||||||||
1717 | if (isFriend) { | ||||||||||||
1718 | DC->makeDeclVisibleInContext(Inst); | ||||||||||||
1719 | Inst->setLexicalDeclContext(Owner); | ||||||||||||
1720 | RecordInst->setLexicalDeclContext(Owner); | ||||||||||||
1721 | return Inst; | ||||||||||||
1722 | } | ||||||||||||
1723 | |||||||||||||
1724 | if (D->isOutOfLine()) { | ||||||||||||
1725 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); | ||||||||||||
1726 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); | ||||||||||||
1727 | } | ||||||||||||
1728 | |||||||||||||
1729 | Owner->addDecl(Inst); | ||||||||||||
1730 | |||||||||||||
1731 | if (!PrevClassTemplate) { | ||||||||||||
1732 | // Queue up any out-of-line partial specializations of this member | ||||||||||||
1733 | // class template; the client will force their instantiation once | ||||||||||||
1734 | // the enclosing class has been instantiated. | ||||||||||||
1735 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; | ||||||||||||
1736 | D->getPartialSpecializations(PartialSpecs); | ||||||||||||
1737 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) | ||||||||||||
1738 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) | ||||||||||||
1739 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); | ||||||||||||
1740 | } | ||||||||||||
1741 | |||||||||||||
1742 | return Inst; | ||||||||||||
1743 | } | ||||||||||||
1744 | |||||||||||||
1745 | Decl * | ||||||||||||
1746 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( | ||||||||||||
1747 | ClassTemplatePartialSpecializationDecl *D) { | ||||||||||||
1748 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); | ||||||||||||
1749 | |||||||||||||
1750 | // Lookup the already-instantiated declaration in the instantiation | ||||||||||||
1751 | // of the class template and return that. | ||||||||||||
1752 | DeclContext::lookup_result Found | ||||||||||||
1753 | = Owner->lookup(ClassTemplate->getDeclName()); | ||||||||||||
1754 | if (Found.empty()) | ||||||||||||
1755 | return nullptr; | ||||||||||||
1756 | |||||||||||||
1757 | ClassTemplateDecl *InstClassTemplate | ||||||||||||
1758 | = dyn_cast<ClassTemplateDecl>(Found.front()); | ||||||||||||
1759 | if (!InstClassTemplate) | ||||||||||||
1760 | return nullptr; | ||||||||||||
1761 | |||||||||||||
1762 | if (ClassTemplatePartialSpecializationDecl *Result | ||||||||||||
1763 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) | ||||||||||||
1764 | return Result; | ||||||||||||
1765 | |||||||||||||
1766 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); | ||||||||||||
1767 | } | ||||||||||||
1768 | |||||||||||||
1769 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { | ||||||||||||
1770 | assert(D->getTemplatedDecl()->isStaticDataMember() &&(static_cast <bool> (D->getTemplatedDecl()->isStaticDataMember () && "Only static data member templates are allowed." ) ? void (0) : __assert_fail ("D->getTemplatedDecl()->isStaticDataMember() && \"Only static data member templates are allowed.\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1771, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
1771 | "Only static data member templates are allowed.")(static_cast <bool> (D->getTemplatedDecl()->isStaticDataMember () && "Only static data member templates are allowed." ) ? void (0) : __assert_fail ("D->getTemplatedDecl()->isStaticDataMember() && \"Only static data member templates are allowed.\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1771, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1772 | |||||||||||||
1773 | // Create a local instantiation scope for this variable template, which | ||||||||||||
1774 | // will contain the instantiations of the template parameters. | ||||||||||||
1775 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
1776 | TemplateParameterList *TempParams = D->getTemplateParameters(); | ||||||||||||
1777 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
1778 | if (!InstParams) | ||||||||||||
1779 | return nullptr; | ||||||||||||
1780 | |||||||||||||
1781 | VarDecl *Pattern = D->getTemplatedDecl(); | ||||||||||||
1782 | VarTemplateDecl *PrevVarTemplate = nullptr; | ||||||||||||
1783 | |||||||||||||
1784 | if (getPreviousDeclForInstantiation(Pattern)) { | ||||||||||||
1785 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); | ||||||||||||
1786 | if (!Found.empty()) | ||||||||||||
1787 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); | ||||||||||||
1788 | } | ||||||||||||
1789 | |||||||||||||
1790 | VarDecl *VarInst = | ||||||||||||
1791 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, | ||||||||||||
1792 | /*InstantiatingVarTemplate=*/true)); | ||||||||||||
1793 | if (!VarInst) return nullptr; | ||||||||||||
1794 | |||||||||||||
1795 | DeclContext *DC = Owner; | ||||||||||||
1796 | |||||||||||||
1797 | VarTemplateDecl *Inst = VarTemplateDecl::Create( | ||||||||||||
1798 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, | ||||||||||||
1799 | VarInst); | ||||||||||||
1800 | VarInst->setDescribedVarTemplate(Inst); | ||||||||||||
1801 | Inst->setPreviousDecl(PrevVarTemplate); | ||||||||||||
1802 | |||||||||||||
1803 | Inst->setAccess(D->getAccess()); | ||||||||||||
1804 | if (!PrevVarTemplate) | ||||||||||||
1805 | Inst->setInstantiatedFromMemberTemplate(D); | ||||||||||||
1806 | |||||||||||||
1807 | if (D->isOutOfLine()) { | ||||||||||||
1808 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); | ||||||||||||
1809 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); | ||||||||||||
1810 | } | ||||||||||||
1811 | |||||||||||||
1812 | Owner->addDecl(Inst); | ||||||||||||
1813 | |||||||||||||
1814 | if (!PrevVarTemplate) { | ||||||||||||
1815 | // Queue up any out-of-line partial specializations of this member | ||||||||||||
1816 | // variable template; the client will force their instantiation once | ||||||||||||
1817 | // the enclosing class has been instantiated. | ||||||||||||
1818 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; | ||||||||||||
1819 | D->getPartialSpecializations(PartialSpecs); | ||||||||||||
1820 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) | ||||||||||||
1821 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) | ||||||||||||
1822 | OutOfLineVarPartialSpecs.push_back( | ||||||||||||
1823 | std::make_pair(Inst, PartialSpecs[I])); | ||||||||||||
1824 | } | ||||||||||||
1825 | |||||||||||||
1826 | return Inst; | ||||||||||||
1827 | } | ||||||||||||
1828 | |||||||||||||
1829 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( | ||||||||||||
1830 | VarTemplatePartialSpecializationDecl *D) { | ||||||||||||
1831 | assert(D->isStaticDataMember() &&(static_cast <bool> (D->isStaticDataMember() && "Only static data member templates are allowed.") ? void (0) : __assert_fail ("D->isStaticDataMember() && \"Only static data member templates are allowed.\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1832, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
1832 | "Only static data member templates are allowed.")(static_cast <bool> (D->isStaticDataMember() && "Only static data member templates are allowed.") ? void (0) : __assert_fail ("D->isStaticDataMember() && \"Only static data member templates are allowed.\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1832, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1833 | |||||||||||||
1834 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); | ||||||||||||
1835 | |||||||||||||
1836 | // Lookup the already-instantiated declaration and return that. | ||||||||||||
1837 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); | ||||||||||||
1838 | assert(!Found.empty() && "Instantiation found nothing?")(static_cast <bool> (!Found.empty() && "Instantiation found nothing?" ) ? void (0) : __assert_fail ("!Found.empty() && \"Instantiation found nothing?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1838, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1839 | |||||||||||||
1840 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); | ||||||||||||
1841 | assert(InstVarTemplate && "Instantiation did not find a variable template?")(static_cast <bool> (InstVarTemplate && "Instantiation did not find a variable template?" ) ? void (0) : __assert_fail ("InstVarTemplate && \"Instantiation did not find a variable template?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1841, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1842 | |||||||||||||
1843 | if (VarTemplatePartialSpecializationDecl *Result = | ||||||||||||
1844 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) | ||||||||||||
1845 | return Result; | ||||||||||||
1846 | |||||||||||||
1847 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); | ||||||||||||
1848 | } | ||||||||||||
1849 | |||||||||||||
1850 | Decl * | ||||||||||||
1851 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { | ||||||||||||
1852 | // Create a local instantiation scope for this function template, which | ||||||||||||
1853 | // will contain the instantiations of the template parameters and then get | ||||||||||||
1854 | // merged with the local instantiation scope for the function template | ||||||||||||
1855 | // itself. | ||||||||||||
1856 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
1857 | Sema::ConstraintEvalRAII<TemplateDeclInstantiator> RAII(*this); | ||||||||||||
1858 | |||||||||||||
1859 | TemplateParameterList *TempParams = D->getTemplateParameters(); | ||||||||||||
1860 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
1861 | if (!InstParams) | ||||||||||||
1862 | return nullptr; | ||||||||||||
1863 | |||||||||||||
1864 | FunctionDecl *Instantiated = nullptr; | ||||||||||||
1865 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) | ||||||||||||
1866 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, | ||||||||||||
1867 | InstParams)); | ||||||||||||
1868 | else | ||||||||||||
1869 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( | ||||||||||||
1870 | D->getTemplatedDecl(), | ||||||||||||
1871 | InstParams)); | ||||||||||||
1872 | |||||||||||||
1873 | if (!Instantiated) | ||||||||||||
1874 | return nullptr; | ||||||||||||
1875 | |||||||||||||
1876 | // Link the instantiated function template declaration to the function | ||||||||||||
1877 | // template from which it was instantiated. | ||||||||||||
1878 | FunctionTemplateDecl *InstTemplate | ||||||||||||
1879 | = Instantiated->getDescribedFunctionTemplate(); | ||||||||||||
1880 | InstTemplate->setAccess(D->getAccess()); | ||||||||||||
1881 | assert(InstTemplate &&(static_cast <bool> (InstTemplate && "VisitFunctionDecl/CXXMethodDecl didn't create a template!" ) ? void (0) : __assert_fail ("InstTemplate && \"VisitFunctionDecl/CXXMethodDecl didn't create a template!\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1882, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
1882 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!")(static_cast <bool> (InstTemplate && "VisitFunctionDecl/CXXMethodDecl didn't create a template!" ) ? void (0) : __assert_fail ("InstTemplate && \"VisitFunctionDecl/CXXMethodDecl didn't create a template!\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1882, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1883 | |||||||||||||
1884 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); | ||||||||||||
1885 | |||||||||||||
1886 | // Link the instantiation back to the pattern *unless* this is a | ||||||||||||
1887 | // non-definition friend declaration. | ||||||||||||
1888 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && | ||||||||||||
1889 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) | ||||||||||||
1890 | InstTemplate->setInstantiatedFromMemberTemplate(D); | ||||||||||||
1891 | |||||||||||||
1892 | // Make declarations visible in the appropriate context. | ||||||||||||
1893 | if (!isFriend) { | ||||||||||||
1894 | Owner->addDecl(InstTemplate); | ||||||||||||
1895 | } else if (InstTemplate->getDeclContext()->isRecord() && | ||||||||||||
1896 | !getPreviousDeclForInstantiation(D)) { | ||||||||||||
1897 | SemaRef.CheckFriendAccess(InstTemplate); | ||||||||||||
1898 | } | ||||||||||||
1899 | |||||||||||||
1900 | return InstTemplate; | ||||||||||||
1901 | } | ||||||||||||
1902 | |||||||||||||
1903 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { | ||||||||||||
1904 | CXXRecordDecl *PrevDecl = nullptr; | ||||||||||||
1905 | if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { | ||||||||||||
1906 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), | ||||||||||||
1907 | PatternPrev, | ||||||||||||
1908 | TemplateArgs); | ||||||||||||
1909 | if (!Prev) return nullptr; | ||||||||||||
1910 | PrevDecl = cast<CXXRecordDecl>(Prev); | ||||||||||||
1911 | } | ||||||||||||
1912 | |||||||||||||
1913 | CXXRecordDecl *Record = nullptr; | ||||||||||||
1914 | bool IsInjectedClassName = D->isInjectedClassName(); | ||||||||||||
1915 | if (D->isLambda()) | ||||||||||||
1916 | Record = CXXRecordDecl::CreateLambda( | ||||||||||||
1917 | SemaRef.Context, Owner, D->getLambdaTypeInfo(), D->getLocation(), | ||||||||||||
1918 | D->getLambdaDependencyKind(), D->isGenericLambda(), | ||||||||||||
1919 | D->getLambdaCaptureDefault()); | ||||||||||||
1920 | else | ||||||||||||
1921 | Record = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, | ||||||||||||
1922 | D->getBeginLoc(), D->getLocation(), | ||||||||||||
1923 | D->getIdentifier(), PrevDecl, | ||||||||||||
1924 | /*DelayTypeCreation=*/IsInjectedClassName); | ||||||||||||
1925 | // Link the type of the injected-class-name to that of the outer class. | ||||||||||||
1926 | if (IsInjectedClassName) | ||||||||||||
1927 | (void)SemaRef.Context.getTypeDeclType(Record, cast<CXXRecordDecl>(Owner)); | ||||||||||||
1928 | |||||||||||||
1929 | // Substitute the nested name specifier, if any. | ||||||||||||
1930 | if (SubstQualifier(D, Record)) | ||||||||||||
1931 | return nullptr; | ||||||||||||
1932 | |||||||||||||
1933 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, D, Record, LateAttrs, | ||||||||||||
1934 | StartingScope); | ||||||||||||
1935 | |||||||||||||
1936 | Record->setImplicit(D->isImplicit()); | ||||||||||||
1937 | // FIXME: Check against AS_none is an ugly hack to work around the issue that | ||||||||||||
1938 | // the tag decls introduced by friend class declarations don't have an access | ||||||||||||
1939 | // specifier. Remove once this area of the code gets sorted out. | ||||||||||||
1940 | if (D->getAccess() != AS_none) | ||||||||||||
1941 | Record->setAccess(D->getAccess()); | ||||||||||||
1942 | if (!IsInjectedClassName) | ||||||||||||
1943 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); | ||||||||||||
1944 | |||||||||||||
1945 | // If the original function was part of a friend declaration, | ||||||||||||
1946 | // inherit its namespace state. | ||||||||||||
1947 | if (D->getFriendObjectKind()) | ||||||||||||
1948 | Record->setObjectOfFriendDecl(); | ||||||||||||
1949 | |||||||||||||
1950 | // Make sure that anonymous structs and unions are recorded. | ||||||||||||
1951 | if (D->isAnonymousStructOrUnion()) | ||||||||||||
1952 | Record->setAnonymousStructOrUnion(true); | ||||||||||||
1953 | |||||||||||||
1954 | if (D->isLocalClass()) | ||||||||||||
1955 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); | ||||||||||||
1956 | |||||||||||||
1957 | // Forward the mangling number from the template to the instantiated decl. | ||||||||||||
1958 | SemaRef.Context.setManglingNumber(Record, | ||||||||||||
1959 | SemaRef.Context.getManglingNumber(D)); | ||||||||||||
1960 | |||||||||||||
1961 | // See if the old tag was defined along with a declarator. | ||||||||||||
1962 | // If it did, mark the new tag as being associated with that declarator. | ||||||||||||
1963 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) | ||||||||||||
1964 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Record, DD); | ||||||||||||
1965 | |||||||||||||
1966 | // See if the old tag was defined along with a typedef. | ||||||||||||
1967 | // If it did, mark the new tag as being associated with that typedef. | ||||||||||||
1968 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) | ||||||||||||
1969 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Record, TND); | ||||||||||||
1970 | |||||||||||||
1971 | Owner->addDecl(Record); | ||||||||||||
1972 | |||||||||||||
1973 | // DR1484 clarifies that the members of a local class are instantiated as part | ||||||||||||
1974 | // of the instantiation of their enclosing entity. | ||||||||||||
1975 | if (D->isCompleteDefinition() && D->isLocalClass()) { | ||||||||||||
1976 | Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef); | ||||||||||||
1977 | |||||||||||||
1978 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, | ||||||||||||
1979 | TSK_ImplicitInstantiation, | ||||||||||||
1980 | /*Complain=*/true); | ||||||||||||
1981 | |||||||||||||
1982 | // For nested local classes, we will instantiate the members when we | ||||||||||||
1983 | // reach the end of the outermost (non-nested) local class. | ||||||||||||
1984 | if (!D->isCXXClassMember()) | ||||||||||||
1985 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, | ||||||||||||
1986 | TSK_ImplicitInstantiation); | ||||||||||||
1987 | |||||||||||||
1988 | // This class may have local implicit instantiations that need to be | ||||||||||||
1989 | // performed within this scope. | ||||||||||||
1990 | LocalInstantiations.perform(); | ||||||||||||
1991 | } | ||||||||||||
1992 | |||||||||||||
1993 | SemaRef.DiagnoseUnusedNestedTypedefs(Record); | ||||||||||||
1994 | |||||||||||||
1995 | if (IsInjectedClassName) | ||||||||||||
1996 | assert(Record->isInjectedClassName() && "Broken injected-class-name")(static_cast <bool> (Record->isInjectedClassName() && "Broken injected-class-name") ? void (0) : __assert_fail ("Record->isInjectedClassName() && \"Broken injected-class-name\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 1996, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
1997 | |||||||||||||
1998 | return Record; | ||||||||||||
1999 | } | ||||||||||||
2000 | |||||||||||||
2001 | /// Adjust the given function type for an instantiation of the | ||||||||||||
2002 | /// given declaration, to cope with modifications to the function's type that | ||||||||||||
2003 | /// aren't reflected in the type-source information. | ||||||||||||
2004 | /// | ||||||||||||
2005 | /// \param D The declaration we're instantiating. | ||||||||||||
2006 | /// \param TInfo The already-instantiated type. | ||||||||||||
2007 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, | ||||||||||||
2008 | FunctionDecl *D, | ||||||||||||
2009 | TypeSourceInfo *TInfo) { | ||||||||||||
2010 | const FunctionProtoType *OrigFunc | ||||||||||||
2011 | = D->getType()->castAs<FunctionProtoType>(); | ||||||||||||
2012 | const FunctionProtoType *NewFunc | ||||||||||||
2013 | = TInfo->getType()->castAs<FunctionProtoType>(); | ||||||||||||
2014 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) | ||||||||||||
2015 | return TInfo->getType(); | ||||||||||||
2016 | |||||||||||||
2017 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); | ||||||||||||
2018 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); | ||||||||||||
2019 | return Context.getFunctionType(NewFunc->getReturnType(), | ||||||||||||
2020 | NewFunc->getParamTypes(), NewEPI); | ||||||||||||
2021 | } | ||||||||||||
2022 | |||||||||||||
2023 | /// Normal class members are of more specific types and therefore | ||||||||||||
2024 | /// don't make it here. This function serves three purposes: | ||||||||||||
2025 | /// 1) instantiating function templates | ||||||||||||
2026 | /// 2) substituting friend and local function declarations | ||||||||||||
2027 | /// 3) substituting deduction guide declarations for nested class templates | ||||||||||||
2028 | Decl *TemplateDeclInstantiator::VisitFunctionDecl( | ||||||||||||
2029 | FunctionDecl *D, TemplateParameterList *TemplateParams, | ||||||||||||
2030 | RewriteKind FunctionRewriteKind) { | ||||||||||||
2031 | // Check whether there is already a function template specialization for | ||||||||||||
2032 | // this declaration. | ||||||||||||
2033 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); | ||||||||||||
2034 | if (FunctionTemplate && !TemplateParams) { | ||||||||||||
2035 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | ||||||||||||
2036 | |||||||||||||
2037 | void *InsertPos = nullptr; | ||||||||||||
2038 | FunctionDecl *SpecFunc | ||||||||||||
2039 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); | ||||||||||||
2040 | |||||||||||||
2041 | // If we already have a function template specialization, return it. | ||||||||||||
2042 | if (SpecFunc) | ||||||||||||
2043 | return SpecFunc; | ||||||||||||
2044 | } | ||||||||||||
2045 | |||||||||||||
2046 | bool isFriend; | ||||||||||||
2047 | if (FunctionTemplate) | ||||||||||||
2048 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); | ||||||||||||
2049 | else | ||||||||||||
2050 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); | ||||||||||||
2051 | |||||||||||||
2052 | bool MergeWithParentScope = (TemplateParams != nullptr) || | ||||||||||||
2053 | Owner->isFunctionOrMethod() || | ||||||||||||
2054 | !(isa<Decl>(Owner) && | ||||||||||||
2055 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); | ||||||||||||
2056 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); | ||||||||||||
2057 | |||||||||||||
2058 | ExplicitSpecifier InstantiatedExplicitSpecifier; | ||||||||||||
2059 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { | ||||||||||||
2060 | InstantiatedExplicitSpecifier = instantiateExplicitSpecifier( | ||||||||||||
2061 | SemaRef, TemplateArgs, DGuide->getExplicitSpecifier(), DGuide); | ||||||||||||
2062 | if (InstantiatedExplicitSpecifier.isInvalid()) | ||||||||||||
2063 | return nullptr; | ||||||||||||
2064 | } | ||||||||||||
2065 | |||||||||||||
2066 | SmallVector<ParmVarDecl *, 4> Params; | ||||||||||||
2067 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); | ||||||||||||
2068 | if (!TInfo) | ||||||||||||
2069 | return nullptr; | ||||||||||||
2070 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); | ||||||||||||
2071 | |||||||||||||
2072 | if (TemplateParams && TemplateParams->size()) { | ||||||||||||
2073 | auto *LastParam = | ||||||||||||
2074 | dyn_cast<TemplateTypeParmDecl>(TemplateParams->asArray().back()); | ||||||||||||
2075 | if (LastParam && LastParam->isImplicit() && | ||||||||||||
2076 | LastParam->hasTypeConstraint()) { | ||||||||||||
2077 | // In abbreviated templates, the type-constraints of invented template | ||||||||||||
2078 | // type parameters are instantiated with the function type, invalidating | ||||||||||||
2079 | // the TemplateParameterList which relied on the template type parameter | ||||||||||||
2080 | // not having a type constraint. Recreate the TemplateParameterList with | ||||||||||||
2081 | // the updated parameter list. | ||||||||||||
2082 | TemplateParams = TemplateParameterList::Create( | ||||||||||||
2083 | SemaRef.Context, TemplateParams->getTemplateLoc(), | ||||||||||||
2084 | TemplateParams->getLAngleLoc(), TemplateParams->asArray(), | ||||||||||||
2085 | TemplateParams->getRAngleLoc(), TemplateParams->getRequiresClause()); | ||||||||||||
2086 | } | ||||||||||||
2087 | } | ||||||||||||
2088 | |||||||||||||
2089 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); | ||||||||||||
2090 | if (QualifierLoc) { | ||||||||||||
2091 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | ||||||||||||
2092 | TemplateArgs); | ||||||||||||
2093 | if (!QualifierLoc) | ||||||||||||
2094 | return nullptr; | ||||||||||||
2095 | } | ||||||||||||
2096 | |||||||||||||
2097 | Expr *TrailingRequiresClause = D->getTrailingRequiresClause(); | ||||||||||||
2098 | |||||||||||||
2099 | // If we're instantiating a local function declaration, put the result | ||||||||||||
2100 | // in the enclosing namespace; otherwise we need to find the instantiated | ||||||||||||
2101 | // context. | ||||||||||||
2102 | DeclContext *DC; | ||||||||||||
2103 | if (D->isLocalExternDecl()) { | ||||||||||||
2104 | DC = Owner; | ||||||||||||
2105 | SemaRef.adjustContextForLocalExternDecl(DC); | ||||||||||||
2106 | } else if (isFriend && QualifierLoc) { | ||||||||||||
2107 | CXXScopeSpec SS; | ||||||||||||
2108 | SS.Adopt(QualifierLoc); | ||||||||||||
2109 | DC = SemaRef.computeDeclContext(SS); | ||||||||||||
2110 | if (!DC) return nullptr; | ||||||||||||
2111 | } else { | ||||||||||||
2112 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), | ||||||||||||
2113 | TemplateArgs); | ||||||||||||
2114 | } | ||||||||||||
2115 | |||||||||||||
2116 | DeclarationNameInfo NameInfo | ||||||||||||
2117 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); | ||||||||||||
2118 | |||||||||||||
2119 | if (FunctionRewriteKind != RewriteKind::None) | ||||||||||||
2120 | adjustForRewrite(FunctionRewriteKind, D, T, TInfo, NameInfo); | ||||||||||||
2121 | |||||||||||||
2122 | FunctionDecl *Function; | ||||||||||||
2123 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { | ||||||||||||
2124 | Function = CXXDeductionGuideDecl::Create( | ||||||||||||
2125 | SemaRef.Context, DC, D->getInnerLocStart(), | ||||||||||||
2126 | InstantiatedExplicitSpecifier, NameInfo, T, TInfo, | ||||||||||||
2127 | D->getSourceRange().getEnd()); | ||||||||||||
2128 | if (DGuide->isCopyDeductionCandidate()) | ||||||||||||
2129 | cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate(); | ||||||||||||
2130 | Function->setAccess(D->getAccess()); | ||||||||||||
2131 | } else { | ||||||||||||
2132 | Function = FunctionDecl::Create( | ||||||||||||
2133 | SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo, | ||||||||||||
2134 | D->getCanonicalDecl()->getStorageClass(), D->UsesFPIntrin(), | ||||||||||||
2135 | D->isInlineSpecified(), D->hasWrittenPrototype(), D->getConstexprKind(), | ||||||||||||
2136 | TrailingRequiresClause); | ||||||||||||
2137 | Function->setFriendConstraintRefersToEnclosingTemplate( | ||||||||||||
2138 | D->FriendConstraintRefersToEnclosingTemplate()); | ||||||||||||
2139 | Function->setRangeEnd(D->getSourceRange().getEnd()); | ||||||||||||
2140 | } | ||||||||||||
2141 | |||||||||||||
2142 | if (D->isInlined()) | ||||||||||||
2143 | Function->setImplicitlyInline(); | ||||||||||||
2144 | |||||||||||||
2145 | if (QualifierLoc) | ||||||||||||
2146 | Function->setQualifierInfo(QualifierLoc); | ||||||||||||
2147 | |||||||||||||
2148 | if (D->isLocalExternDecl()) | ||||||||||||
2149 | Function->setLocalExternDecl(); | ||||||||||||
2150 | |||||||||||||
2151 | DeclContext *LexicalDC = Owner; | ||||||||||||
2152 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { | ||||||||||||
2153 | assert(D->getDeclContext()->isFileContext())(static_cast <bool> (D->getDeclContext()->isFileContext ()) ? void (0) : __assert_fail ("D->getDeclContext()->isFileContext()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2153, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
2154 | LexicalDC = D->getDeclContext(); | ||||||||||||
2155 | } | ||||||||||||
2156 | else if (D->isLocalExternDecl()) { | ||||||||||||
2157 | LexicalDC = SemaRef.CurContext; | ||||||||||||
2158 | } | ||||||||||||
2159 | |||||||||||||
2160 | Function->setLexicalDeclContext(LexicalDC); | ||||||||||||
2161 | |||||||||||||
2162 | // Attach the parameters | ||||||||||||
2163 | for (unsigned P = 0; P < Params.size(); ++P) | ||||||||||||
2164 | if (Params[P]) | ||||||||||||
2165 | Params[P]->setOwningFunction(Function); | ||||||||||||
2166 | Function->setParams(Params); | ||||||||||||
2167 | |||||||||||||
2168 | if (TrailingRequiresClause) | ||||||||||||
2169 | Function->setTrailingRequiresClause(TrailingRequiresClause); | ||||||||||||
2170 | |||||||||||||
2171 | if (TemplateParams) { | ||||||||||||
2172 | // Our resulting instantiation is actually a function template, since we | ||||||||||||
2173 | // are substituting only the outer template parameters. For example, given | ||||||||||||
2174 | // | ||||||||||||
2175 | // template<typename T> | ||||||||||||
2176 | // struct X { | ||||||||||||
2177 | // template<typename U> friend void f(T, U); | ||||||||||||
2178 | // }; | ||||||||||||
2179 | // | ||||||||||||
2180 | // X<int> x; | ||||||||||||
2181 | // | ||||||||||||
2182 | // We are instantiating the friend function template "f" within X<int>, | ||||||||||||
2183 | // which means substituting int for T, but leaving "f" as a friend function | ||||||||||||
2184 | // template. | ||||||||||||
2185 | // Build the function template itself. | ||||||||||||
2186 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, | ||||||||||||
2187 | Function->getLocation(), | ||||||||||||
2188 | Function->getDeclName(), | ||||||||||||
2189 | TemplateParams, Function); | ||||||||||||
2190 | Function->setDescribedFunctionTemplate(FunctionTemplate); | ||||||||||||
2191 | |||||||||||||
2192 | FunctionTemplate->setLexicalDeclContext(LexicalDC); | ||||||||||||
2193 | |||||||||||||
2194 | if (isFriend && D->isThisDeclarationADefinition()) { | ||||||||||||
2195 | FunctionTemplate->setInstantiatedFromMemberTemplate( | ||||||||||||
2196 | D->getDescribedFunctionTemplate()); | ||||||||||||
2197 | } | ||||||||||||
2198 | } else if (FunctionTemplate) { | ||||||||||||
2199 | // Record this function template specialization. | ||||||||||||
2200 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | ||||||||||||
2201 | Function->setFunctionTemplateSpecialization(FunctionTemplate, | ||||||||||||
2202 | TemplateArgumentList::CreateCopy(SemaRef.Context, | ||||||||||||
2203 | Innermost), | ||||||||||||
2204 | /*InsertPos=*/nullptr); | ||||||||||||
2205 | } else if (isFriend && D->isThisDeclarationADefinition()) { | ||||||||||||
2206 | // Do not connect the friend to the template unless it's actually a | ||||||||||||
2207 | // definition. We don't want non-template functions to be marked as being | ||||||||||||
2208 | // template instantiations. | ||||||||||||
2209 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); | ||||||||||||
2210 | } else if (!isFriend) { | ||||||||||||
2211 | // If this is not a function template, and this is not a friend (that is, | ||||||||||||
2212 | // this is a locally declared function), save the instantiation relationship | ||||||||||||
2213 | // for the purposes of constraint instantiation. | ||||||||||||
2214 | Function->setInstantiatedFromDecl(D); | ||||||||||||
2215 | } | ||||||||||||
2216 | |||||||||||||
2217 | if (isFriend) { | ||||||||||||
2218 | Function->setObjectOfFriendDecl(); | ||||||||||||
2219 | if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate()) | ||||||||||||
2220 | FT->setObjectOfFriendDecl(); | ||||||||||||
2221 | } | ||||||||||||
2222 | |||||||||||||
2223 | if (InitFunctionInstantiation(Function, D)) | ||||||||||||
2224 | Function->setInvalidDecl(); | ||||||||||||
2225 | |||||||||||||
2226 | bool IsExplicitSpecialization = false; | ||||||||||||
2227 | |||||||||||||
2228 | LookupResult Previous( | ||||||||||||
2229 | SemaRef, Function->getDeclName(), SourceLocation(), | ||||||||||||
2230 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage | ||||||||||||
2231 | : Sema::LookupOrdinaryName, | ||||||||||||
2232 | D->isLocalExternDecl() ? Sema::ForExternalRedeclaration | ||||||||||||
2233 | : SemaRef.forRedeclarationInCurContext()); | ||||||||||||
2234 | |||||||||||||
2235 | if (DependentFunctionTemplateSpecializationInfo *Info | ||||||||||||
2236 | = D->getDependentSpecializationInfo()) { | ||||||||||||
2237 | assert(isFriend && "non-friend has dependent specialization info?")(static_cast <bool> (isFriend && "non-friend has dependent specialization info?" ) ? void (0) : __assert_fail ("isFriend && \"non-friend has dependent specialization info?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2237, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
2238 | |||||||||||||
2239 | // Instantiate the explicit template arguments. | ||||||||||||
2240 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), | ||||||||||||
2241 | Info->getRAngleLoc()); | ||||||||||||
2242 | if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, | ||||||||||||
2243 | ExplicitArgs)) | ||||||||||||
2244 | return nullptr; | ||||||||||||
2245 | |||||||||||||
2246 | // Map the candidate templates to their instantiations. | ||||||||||||
2247 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { | ||||||||||||
2248 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), | ||||||||||||
2249 | Info->getTemplate(I), | ||||||||||||
2250 | TemplateArgs); | ||||||||||||
2251 | if (!Temp) return nullptr; | ||||||||||||
2252 | |||||||||||||
2253 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); | ||||||||||||
2254 | } | ||||||||||||
2255 | |||||||||||||
2256 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, | ||||||||||||
2257 | &ExplicitArgs, | ||||||||||||
2258 | Previous)) | ||||||||||||
2259 | Function->setInvalidDecl(); | ||||||||||||
2260 | |||||||||||||
2261 | IsExplicitSpecialization = true; | ||||||||||||
2262 | } else if (const ASTTemplateArgumentListInfo *Info = | ||||||||||||
2263 | D->getTemplateSpecializationArgsAsWritten()) { | ||||||||||||
2264 | // The name of this function was written as a template-id. | ||||||||||||
2265 | SemaRef.LookupQualifiedName(Previous, DC); | ||||||||||||
2266 | |||||||||||||
2267 | // Instantiate the explicit template arguments. | ||||||||||||
2268 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), | ||||||||||||
2269 | Info->getRAngleLoc()); | ||||||||||||
2270 | if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, | ||||||||||||
2271 | ExplicitArgs)) | ||||||||||||
2272 | return nullptr; | ||||||||||||
2273 | |||||||||||||
2274 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, | ||||||||||||
2275 | &ExplicitArgs, | ||||||||||||
2276 | Previous)) | ||||||||||||
2277 | Function->setInvalidDecl(); | ||||||||||||
2278 | |||||||||||||
2279 | IsExplicitSpecialization = true; | ||||||||||||
2280 | } else if (TemplateParams || !FunctionTemplate) { | ||||||||||||
2281 | // Look only into the namespace where the friend would be declared to | ||||||||||||
2282 | // find a previous declaration. This is the innermost enclosing namespace, | ||||||||||||
2283 | // as described in ActOnFriendFunctionDecl. | ||||||||||||
2284 | SemaRef.LookupQualifiedName(Previous, DC->getRedeclContext()); | ||||||||||||
2285 | |||||||||||||
2286 | // In C++, the previous declaration we find might be a tag type | ||||||||||||
2287 | // (class or enum). In this case, the new declaration will hide the | ||||||||||||
2288 | // tag type. Note that this does not apply if we're declaring a | ||||||||||||
2289 | // typedef (C++ [dcl.typedef]p4). | ||||||||||||
2290 | if (Previous.isSingleTagDecl()) | ||||||||||||
2291 | Previous.clear(); | ||||||||||||
2292 | |||||||||||||
2293 | // Filter out previous declarations that don't match the scope. The only | ||||||||||||
2294 | // effect this has is to remove declarations found in inline namespaces | ||||||||||||
2295 | // for friend declarations with unqualified names. | ||||||||||||
2296 | if (isFriend && !QualifierLoc && !FunctionTemplate) { | ||||||||||||
2297 | SemaRef.FilterLookupForScope(Previous, DC, /*Scope=*/ nullptr, | ||||||||||||
2298 | /*ConsiderLinkage=*/ true, | ||||||||||||
2299 | QualifierLoc.hasQualifier()); | ||||||||||||
2300 | } | ||||||||||||
2301 | } | ||||||||||||
2302 | |||||||||||||
2303 | // Per [temp.inst], default arguments in function declarations at local scope | ||||||||||||
2304 | // are instantiated along with the enclosing declaration. For example: | ||||||||||||
2305 | // | ||||||||||||
2306 | // template<typename T> | ||||||||||||
2307 | // void ft() { | ||||||||||||
2308 | // void f(int = []{ return T::value; }()); | ||||||||||||
2309 | // } | ||||||||||||
2310 | // template void ft<int>(); // error: type 'int' cannot be used prior | ||||||||||||
2311 | // to '::' because it has no members | ||||||||||||
2312 | // | ||||||||||||
2313 | // The error is issued during instantiation of ft<int>() because substitution | ||||||||||||
2314 | // into the default argument fails; the default argument is instantiated even | ||||||||||||
2315 | // though it is never used. | ||||||||||||
2316 | if (Function->isLocalExternDecl()) { | ||||||||||||
2317 | for (ParmVarDecl *PVD : Function->parameters()) { | ||||||||||||
2318 | if (!PVD->hasDefaultArg()) | ||||||||||||
2319 | continue; | ||||||||||||
2320 | if (SemaRef.SubstDefaultArgument(D->getInnerLocStart(), PVD, TemplateArgs)) { | ||||||||||||
2321 | // If substitution fails, the default argument is set to a | ||||||||||||
2322 | // RecoveryExpr that wraps the uninstantiated default argument so | ||||||||||||
2323 | // that downstream diagnostics are omitted. | ||||||||||||
2324 | Expr *UninstExpr = PVD->getUninstantiatedDefaultArg(); | ||||||||||||
2325 | ExprResult ErrorResult = SemaRef.CreateRecoveryExpr( | ||||||||||||
2326 | UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(), | ||||||||||||
2327 | { UninstExpr }, UninstExpr->getType()); | ||||||||||||
2328 | if (ErrorResult.isUsable()) | ||||||||||||
2329 | PVD->setDefaultArg(ErrorResult.get()); | ||||||||||||
2330 | } | ||||||||||||
2331 | } | ||||||||||||
2332 | } | ||||||||||||
2333 | |||||||||||||
2334 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, | ||||||||||||
2335 | IsExplicitSpecialization, | ||||||||||||
2336 | Function->isThisDeclarationADefinition()); | ||||||||||||
2337 | |||||||||||||
2338 | // Check the template parameter list against the previous declaration. The | ||||||||||||
2339 | // goal here is to pick up default arguments added since the friend was | ||||||||||||
2340 | // declared; we know the template parameter lists match, since otherwise | ||||||||||||
2341 | // we would not have picked this template as the previous declaration. | ||||||||||||
2342 | if (isFriend && TemplateParams && FunctionTemplate->getPreviousDecl()) { | ||||||||||||
2343 | SemaRef.CheckTemplateParameterList( | ||||||||||||
2344 | TemplateParams, | ||||||||||||
2345 | FunctionTemplate->getPreviousDecl()->getTemplateParameters(), | ||||||||||||
2346 | Function->isThisDeclarationADefinition() | ||||||||||||
2347 | ? Sema::TPC_FriendFunctionTemplateDefinition | ||||||||||||
2348 | : Sema::TPC_FriendFunctionTemplate); | ||||||||||||
2349 | } | ||||||||||||
2350 | |||||||||||||
2351 | // If we're introducing a friend definition after the first use, trigger | ||||||||||||
2352 | // instantiation. | ||||||||||||
2353 | // FIXME: If this is a friend function template definition, we should check | ||||||||||||
2354 | // to see if any specializations have been used. | ||||||||||||
2355 | if (isFriend && D->isThisDeclarationADefinition() && Function->isUsed(false)) { | ||||||||||||
2356 | if (MemberSpecializationInfo *MSInfo = | ||||||||||||
2357 | Function->getMemberSpecializationInfo()) { | ||||||||||||
2358 | if (MSInfo->getPointOfInstantiation().isInvalid()) { | ||||||||||||
2359 | SourceLocation Loc = D->getLocation(); // FIXME | ||||||||||||
2360 | MSInfo->setPointOfInstantiation(Loc); | ||||||||||||
2361 | SemaRef.PendingLocalImplicitInstantiations.push_back( | ||||||||||||
2362 | std::make_pair(Function, Loc)); | ||||||||||||
2363 | } | ||||||||||||
2364 | } | ||||||||||||
2365 | } | ||||||||||||
2366 | |||||||||||||
2367 | if (D->isExplicitlyDefaulted()) { | ||||||||||||
2368 | if (SubstDefaultedFunction(Function, D)) | ||||||||||||
2369 | return nullptr; | ||||||||||||
2370 | } | ||||||||||||
2371 | if (D->isDeleted()) | ||||||||||||
2372 | SemaRef.SetDeclDeleted(Function, D->getLocation()); | ||||||||||||
2373 | |||||||||||||
2374 | NamedDecl *PrincipalDecl = | ||||||||||||
2375 | (TemplateParams ? cast<NamedDecl>(FunctionTemplate) : Function); | ||||||||||||
2376 | |||||||||||||
2377 | // If this declaration lives in a different context from its lexical context, | ||||||||||||
2378 | // add it to the corresponding lookup table. | ||||||||||||
2379 | if (isFriend || | ||||||||||||
2380 | (Function->isLocalExternDecl() && !Function->getPreviousDecl())) | ||||||||||||
2381 | DC->makeDeclVisibleInContext(PrincipalDecl); | ||||||||||||
2382 | |||||||||||||
2383 | if (Function->isOverloadedOperator() && !DC->isRecord() && | ||||||||||||
2384 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | ||||||||||||
2385 | PrincipalDecl->setNonMemberOperator(); | ||||||||||||
2386 | |||||||||||||
2387 | return Function; | ||||||||||||
2388 | } | ||||||||||||
2389 | |||||||||||||
2390 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( | ||||||||||||
2391 | CXXMethodDecl *D, TemplateParameterList *TemplateParams, | ||||||||||||
2392 | std::optional<const ASTTemplateArgumentListInfo *> | ||||||||||||
2393 | ClassScopeSpecializationArgs, | ||||||||||||
2394 | RewriteKind FunctionRewriteKind) { | ||||||||||||
2395 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); | ||||||||||||
2396 | if (FunctionTemplate && !TemplateParams) { | ||||||||||||
2397 | // We are creating a function template specialization from a function | ||||||||||||
2398 | // template. Check whether there is already a function template | ||||||||||||
2399 | // specialization for this particular set of template arguments. | ||||||||||||
2400 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | ||||||||||||
2401 | |||||||||||||
2402 | void *InsertPos = nullptr; | ||||||||||||
2403 | FunctionDecl *SpecFunc | ||||||||||||
2404 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); | ||||||||||||
2405 | |||||||||||||
2406 | // If we already have a function template specialization, return it. | ||||||||||||
2407 | if (SpecFunc) | ||||||||||||
2408 | return SpecFunc; | ||||||||||||
2409 | } | ||||||||||||
2410 | |||||||||||||
2411 | bool isFriend; | ||||||||||||
2412 | if (FunctionTemplate) | ||||||||||||
2413 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); | ||||||||||||
2414 | else | ||||||||||||
2415 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); | ||||||||||||
2416 | |||||||||||||
2417 | bool MergeWithParentScope = (TemplateParams != nullptr) || | ||||||||||||
2418 | !(isa<Decl>(Owner) && | ||||||||||||
2419 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); | ||||||||||||
2420 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); | ||||||||||||
2421 | |||||||||||||
2422 | // Instantiate enclosing template arguments for friends. | ||||||||||||
2423 | SmallVector<TemplateParameterList *, 4> TempParamLists; | ||||||||||||
2424 | unsigned NumTempParamLists = 0; | ||||||||||||
2425 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { | ||||||||||||
2426 | TempParamLists.resize(NumTempParamLists); | ||||||||||||
2427 | for (unsigned I = 0; I != NumTempParamLists; ++I) { | ||||||||||||
2428 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); | ||||||||||||
2429 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
2430 | if (!InstParams) | ||||||||||||
2431 | return nullptr; | ||||||||||||
2432 | TempParamLists[I] = InstParams; | ||||||||||||
2433 | } | ||||||||||||
2434 | } | ||||||||||||
2435 | |||||||||||||
2436 | ExplicitSpecifier InstantiatedExplicitSpecifier = | ||||||||||||
2437 | instantiateExplicitSpecifier(SemaRef, TemplateArgs, | ||||||||||||
2438 | ExplicitSpecifier::getFromDecl(D), D); | ||||||||||||
2439 | if (InstantiatedExplicitSpecifier.isInvalid()) | ||||||||||||
2440 | return nullptr; | ||||||||||||
2441 | |||||||||||||
2442 | // Implicit destructors/constructors created for local classes in | ||||||||||||
2443 | // DeclareImplicit* (see SemaDeclCXX.cpp) might not have an associated TSI. | ||||||||||||
2444 | // Unfortunately there isn't enough context in those functions to | ||||||||||||
2445 | // conditionally populate the TSI without breaking non-template related use | ||||||||||||
2446 | // cases. Populate TSIs prior to calling SubstFunctionType to make sure we get | ||||||||||||
2447 | // a proper transformation. | ||||||||||||
2448 | if (cast<CXXRecordDecl>(D->getParent())->isLambda() && | ||||||||||||
2449 | !D->getTypeSourceInfo() && | ||||||||||||
2450 | isa<CXXConstructorDecl, CXXDestructorDecl>(D)) { | ||||||||||||
2451 | TypeSourceInfo *TSI = | ||||||||||||
2452 | SemaRef.Context.getTrivialTypeSourceInfo(D->getType()); | ||||||||||||
2453 | D->setTypeSourceInfo(TSI); | ||||||||||||
2454 | } | ||||||||||||
2455 | |||||||||||||
2456 | SmallVector<ParmVarDecl *, 4> Params; | ||||||||||||
2457 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); | ||||||||||||
2458 | if (!TInfo) | ||||||||||||
2459 | return nullptr; | ||||||||||||
2460 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); | ||||||||||||
2461 | |||||||||||||
2462 | if (TemplateParams && TemplateParams->size()) { | ||||||||||||
2463 | auto *LastParam = | ||||||||||||
2464 | dyn_cast<TemplateTypeParmDecl>(TemplateParams->asArray().back()); | ||||||||||||
2465 | if (LastParam && LastParam->isImplicit() && | ||||||||||||
2466 | LastParam->hasTypeConstraint()) { | ||||||||||||
2467 | // In abbreviated templates, the type-constraints of invented template | ||||||||||||
2468 | // type parameters are instantiated with the function type, invalidating | ||||||||||||
2469 | // the TemplateParameterList which relied on the template type parameter | ||||||||||||
2470 | // not having a type constraint. Recreate the TemplateParameterList with | ||||||||||||
2471 | // the updated parameter list. | ||||||||||||
2472 | TemplateParams = TemplateParameterList::Create( | ||||||||||||
2473 | SemaRef.Context, TemplateParams->getTemplateLoc(), | ||||||||||||
2474 | TemplateParams->getLAngleLoc(), TemplateParams->asArray(), | ||||||||||||
2475 | TemplateParams->getRAngleLoc(), TemplateParams->getRequiresClause()); | ||||||||||||
2476 | } | ||||||||||||
2477 | } | ||||||||||||
2478 | |||||||||||||
2479 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); | ||||||||||||
2480 | if (QualifierLoc) { | ||||||||||||
2481 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | ||||||||||||
2482 | TemplateArgs); | ||||||||||||
2483 | if (!QualifierLoc) | ||||||||||||
2484 | return nullptr; | ||||||||||||
2485 | } | ||||||||||||
2486 | |||||||||||||
2487 | DeclContext *DC = Owner; | ||||||||||||
2488 | if (isFriend) { | ||||||||||||
2489 | if (QualifierLoc) { | ||||||||||||
2490 | CXXScopeSpec SS; | ||||||||||||
2491 | SS.Adopt(QualifierLoc); | ||||||||||||
2492 | DC = SemaRef.computeDeclContext(SS); | ||||||||||||
2493 | |||||||||||||
2494 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) | ||||||||||||
2495 | return nullptr; | ||||||||||||
2496 | } else { | ||||||||||||
2497 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), | ||||||||||||
2498 | D->getDeclContext(), | ||||||||||||
2499 | TemplateArgs); | ||||||||||||
2500 | } | ||||||||||||
2501 | if (!DC) return nullptr; | ||||||||||||
2502 | } | ||||||||||||
2503 | |||||||||||||
2504 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); | ||||||||||||
2505 | Expr *TrailingRequiresClause = D->getTrailingRequiresClause(); | ||||||||||||
2506 | |||||||||||||
2507 | DeclarationNameInfo NameInfo | ||||||||||||
2508 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); | ||||||||||||
2509 | |||||||||||||
2510 | if (FunctionRewriteKind != RewriteKind::None) | ||||||||||||
2511 | adjustForRewrite(FunctionRewriteKind, D, T, TInfo, NameInfo); | ||||||||||||
2512 | |||||||||||||
2513 | // Build the instantiated method declaration. | ||||||||||||
2514 | CXXMethodDecl *Method = nullptr; | ||||||||||||
2515 | |||||||||||||
2516 | SourceLocation StartLoc = D->getInnerLocStart(); | ||||||||||||
2517 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { | ||||||||||||
2518 | Method = CXXConstructorDecl::Create( | ||||||||||||
2519 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, | ||||||||||||
2520 | InstantiatedExplicitSpecifier, Constructor->UsesFPIntrin(), | ||||||||||||
2521 | Constructor->isInlineSpecified(), false, | ||||||||||||
2522 | Constructor->getConstexprKind(), InheritedConstructor(), | ||||||||||||
2523 | TrailingRequiresClause); | ||||||||||||
2524 | Method->setRangeEnd(Constructor->getEndLoc()); | ||||||||||||
2525 | if (Constructor->isDefaultConstructor() || | ||||||||||||
2526 | Constructor->isCopyOrMoveConstructor()) | ||||||||||||
2527 | Method->setIneligibleOrNotSelected(true); | ||||||||||||
2528 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { | ||||||||||||
2529 | Method = CXXDestructorDecl::Create( | ||||||||||||
2530 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, | ||||||||||||
2531 | Destructor->UsesFPIntrin(), Destructor->isInlineSpecified(), false, | ||||||||||||
2532 | Destructor->getConstexprKind(), TrailingRequiresClause); | ||||||||||||
2533 | Method->setIneligibleOrNotSelected(true); | ||||||||||||
2534 | Method->setRangeEnd(Destructor->getEndLoc()); | ||||||||||||
2535 | Method->setDeclName(SemaRef.Context.DeclarationNames.getCXXDestructorName( | ||||||||||||
2536 | SemaRef.Context.getCanonicalType( | ||||||||||||
2537 | SemaRef.Context.getTypeDeclType(Record)))); | ||||||||||||
2538 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { | ||||||||||||
2539 | Method = CXXConversionDecl::Create( | ||||||||||||
2540 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, | ||||||||||||
2541 | Conversion->UsesFPIntrin(), Conversion->isInlineSpecified(), | ||||||||||||
2542 | InstantiatedExplicitSpecifier, Conversion->getConstexprKind(), | ||||||||||||
2543 | Conversion->getEndLoc(), TrailingRequiresClause); | ||||||||||||
2544 | } else { | ||||||||||||
2545 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; | ||||||||||||
2546 | Method = CXXMethodDecl::Create( | ||||||||||||
2547 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, SC, | ||||||||||||
2548 | D->UsesFPIntrin(), D->isInlineSpecified(), D->getConstexprKind(), | ||||||||||||
2549 | D->getEndLoc(), TrailingRequiresClause); | ||||||||||||
2550 | if (D->isMoveAssignmentOperator() || D->isCopyAssignmentOperator()) | ||||||||||||
2551 | Method->setIneligibleOrNotSelected(true); | ||||||||||||
2552 | } | ||||||||||||
2553 | |||||||||||||
2554 | if (D->isInlined()) | ||||||||||||
2555 | Method->setImplicitlyInline(); | ||||||||||||
2556 | |||||||||||||
2557 | if (QualifierLoc) | ||||||||||||
2558 | Method->setQualifierInfo(QualifierLoc); | ||||||||||||
2559 | |||||||||||||
2560 | if (TemplateParams) { | ||||||||||||
2561 | // Our resulting instantiation is actually a function template, since we | ||||||||||||
2562 | // are substituting only the outer template parameters. For example, given | ||||||||||||
2563 | // | ||||||||||||
2564 | // template<typename T> | ||||||||||||
2565 | // struct X { | ||||||||||||
2566 | // template<typename U> void f(T, U); | ||||||||||||
2567 | // }; | ||||||||||||
2568 | // | ||||||||||||
2569 | // X<int> x; | ||||||||||||
2570 | // | ||||||||||||
2571 | // We are instantiating the member template "f" within X<int>, which means | ||||||||||||
2572 | // substituting int for T, but leaving "f" as a member function template. | ||||||||||||
2573 | // Build the function template itself. | ||||||||||||
2574 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, | ||||||||||||
2575 | Method->getLocation(), | ||||||||||||
2576 | Method->getDeclName(), | ||||||||||||
2577 | TemplateParams, Method); | ||||||||||||
2578 | if (isFriend) { | ||||||||||||
2579 | FunctionTemplate->setLexicalDeclContext(Owner); | ||||||||||||
2580 | FunctionTemplate->setObjectOfFriendDecl(); | ||||||||||||
2581 | } else if (D->isOutOfLine()) | ||||||||||||
2582 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); | ||||||||||||
2583 | Method->setDescribedFunctionTemplate(FunctionTemplate); | ||||||||||||
2584 | } else if (FunctionTemplate) { | ||||||||||||
2585 | // Record this function template specialization. | ||||||||||||
2586 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | ||||||||||||
2587 | Method->setFunctionTemplateSpecialization(FunctionTemplate, | ||||||||||||
2588 | TemplateArgumentList::CreateCopy(SemaRef.Context, | ||||||||||||
2589 | Innermost), | ||||||||||||
2590 | /*InsertPos=*/nullptr); | ||||||||||||
2591 | } else if (!isFriend) { | ||||||||||||
2592 | // Record that this is an instantiation of a member function. | ||||||||||||
2593 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); | ||||||||||||
2594 | } | ||||||||||||
2595 | |||||||||||||
2596 | // If we are instantiating a member function defined | ||||||||||||
2597 | // out-of-line, the instantiation will have the same lexical | ||||||||||||
2598 | // context (which will be a namespace scope) as the template. | ||||||||||||
2599 | if (isFriend) { | ||||||||||||
2600 | if (NumTempParamLists) | ||||||||||||
2601 | Method->setTemplateParameterListsInfo( | ||||||||||||
2602 | SemaRef.Context, | ||||||||||||
2603 | llvm::ArrayRef(TempParamLists.data(), NumTempParamLists)); | ||||||||||||
2604 | |||||||||||||
2605 | Method->setLexicalDeclContext(Owner); | ||||||||||||
2606 | Method->setObjectOfFriendDecl(); | ||||||||||||
2607 | } else if (D->isOutOfLine()) | ||||||||||||
2608 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); | ||||||||||||
2609 | |||||||||||||
2610 | // Attach the parameters | ||||||||||||
2611 | for (unsigned P = 0; P < Params.size(); ++P) | ||||||||||||
2612 | Params[P]->setOwningFunction(Method); | ||||||||||||
2613 | Method->setParams(Params); | ||||||||||||
2614 | |||||||||||||
2615 | if (InitMethodInstantiation(Method, D)) | ||||||||||||
2616 | Method->setInvalidDecl(); | ||||||||||||
2617 | |||||||||||||
2618 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, | ||||||||||||
2619 | Sema::ForExternalRedeclaration); | ||||||||||||
2620 | |||||||||||||
2621 | bool IsExplicitSpecialization = false; | ||||||||||||
2622 | |||||||||||||
2623 | // If the name of this function was written as a template-id, instantiate | ||||||||||||
2624 | // the explicit template arguments. | ||||||||||||
2625 | if (DependentFunctionTemplateSpecializationInfo *Info | ||||||||||||
2626 | = D->getDependentSpecializationInfo()) { | ||||||||||||
2627 | assert(isFriend && "non-friend has dependent specialization info?")(static_cast <bool> (isFriend && "non-friend has dependent specialization info?" ) ? void (0) : __assert_fail ("isFriend && \"non-friend has dependent specialization info?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2627, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
2628 | |||||||||||||
2629 | // Instantiate the explicit template arguments. | ||||||||||||
2630 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), | ||||||||||||
2631 | Info->getRAngleLoc()); | ||||||||||||
2632 | if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, | ||||||||||||
2633 | ExplicitArgs)) | ||||||||||||
2634 | return nullptr; | ||||||||||||
2635 | |||||||||||||
2636 | // Map the candidate templates to their instantiations. | ||||||||||||
2637 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { | ||||||||||||
2638 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), | ||||||||||||
2639 | Info->getTemplate(I), | ||||||||||||
2640 | TemplateArgs); | ||||||||||||
2641 | if (!Temp) return nullptr; | ||||||||||||
2642 | |||||||||||||
2643 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); | ||||||||||||
2644 | } | ||||||||||||
2645 | |||||||||||||
2646 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, | ||||||||||||
2647 | &ExplicitArgs, | ||||||||||||
2648 | Previous)) | ||||||||||||
2649 | Method->setInvalidDecl(); | ||||||||||||
2650 | |||||||||||||
2651 | IsExplicitSpecialization = true; | ||||||||||||
2652 | } else if (const ASTTemplateArgumentListInfo *Info = | ||||||||||||
2653 | ClassScopeSpecializationArgs.value_or( | ||||||||||||
2654 | D->getTemplateSpecializationArgsAsWritten())) { | ||||||||||||
2655 | SemaRef.LookupQualifiedName(Previous, DC); | ||||||||||||
2656 | |||||||||||||
2657 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), | ||||||||||||
2658 | Info->getRAngleLoc()); | ||||||||||||
2659 | if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, | ||||||||||||
2660 | ExplicitArgs)) | ||||||||||||
2661 | return nullptr; | ||||||||||||
2662 | |||||||||||||
2663 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, | ||||||||||||
2664 | &ExplicitArgs, | ||||||||||||
2665 | Previous)) | ||||||||||||
2666 | Method->setInvalidDecl(); | ||||||||||||
2667 | |||||||||||||
2668 | IsExplicitSpecialization = true; | ||||||||||||
2669 | } else if (ClassScopeSpecializationArgs) { | ||||||||||||
2670 | // Class-scope explicit specialization written without explicit template | ||||||||||||
2671 | // arguments. | ||||||||||||
2672 | SemaRef.LookupQualifiedName(Previous, DC); | ||||||||||||
2673 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, nullptr, Previous)) | ||||||||||||
2674 | Method->setInvalidDecl(); | ||||||||||||
2675 | |||||||||||||
2676 | IsExplicitSpecialization = true; | ||||||||||||
2677 | } else if (!FunctionTemplate || TemplateParams || isFriend) { | ||||||||||||
2678 | SemaRef.LookupQualifiedName(Previous, Record); | ||||||||||||
2679 | |||||||||||||
2680 | // In C++, the previous declaration we find might be a tag type | ||||||||||||
2681 | // (class or enum). In this case, the new declaration will hide the | ||||||||||||
2682 | // tag type. Note that this does not apply if we're declaring a | ||||||||||||
2683 | // typedef (C++ [dcl.typedef]p4). | ||||||||||||
2684 | if (Previous.isSingleTagDecl()) | ||||||||||||
2685 | Previous.clear(); | ||||||||||||
2686 | } | ||||||||||||
2687 | |||||||||||||
2688 | // Per [temp.inst], default arguments in member functions of local classes | ||||||||||||
2689 | // are instantiated along with the member function declaration. For example: | ||||||||||||
2690 | // | ||||||||||||
2691 | // template<typename T> | ||||||||||||
2692 | // void ft() { | ||||||||||||
2693 | // struct lc { | ||||||||||||
2694 | // int operator()(int p = []{ return T::value; }()); | ||||||||||||
2695 | // }; | ||||||||||||
2696 | // } | ||||||||||||
2697 | // template void ft<int>(); // error: type 'int' cannot be used prior | ||||||||||||
2698 | // to '::'because it has no members | ||||||||||||
2699 | // | ||||||||||||
2700 | // The error is issued during instantiation of ft<int>()::lc::operator() | ||||||||||||
2701 | // because substitution into the default argument fails; the default argument | ||||||||||||
2702 | // is instantiated even though it is never used. | ||||||||||||
2703 | if (D->isInLocalScopeForInstantiation()) { | ||||||||||||
2704 | for (unsigned P = 0; P < Params.size(); ++P) { | ||||||||||||
2705 | if (!Params[P]->hasDefaultArg()) | ||||||||||||
2706 | continue; | ||||||||||||
2707 | if (SemaRef.SubstDefaultArgument(StartLoc, Params[P], TemplateArgs)) { | ||||||||||||
2708 | // If substitution fails, the default argument is set to a | ||||||||||||
2709 | // RecoveryExpr that wraps the uninstantiated default argument so | ||||||||||||
2710 | // that downstream diagnostics are omitted. | ||||||||||||
2711 | Expr *UninstExpr = Params[P]->getUninstantiatedDefaultArg(); | ||||||||||||
2712 | ExprResult ErrorResult = SemaRef.CreateRecoveryExpr( | ||||||||||||
2713 | UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(), | ||||||||||||
2714 | { UninstExpr }, UninstExpr->getType()); | ||||||||||||
2715 | if (ErrorResult.isUsable()) | ||||||||||||
2716 | Params[P]->setDefaultArg(ErrorResult.get()); | ||||||||||||
2717 | } | ||||||||||||
2718 | } | ||||||||||||
2719 | } | ||||||||||||
2720 | |||||||||||||
2721 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, | ||||||||||||
2722 | IsExplicitSpecialization, | ||||||||||||
2723 | Method->isThisDeclarationADefinition()); | ||||||||||||
2724 | |||||||||||||
2725 | if (D->isPure()) | ||||||||||||
2726 | SemaRef.CheckPureMethod(Method, SourceRange()); | ||||||||||||
2727 | |||||||||||||
2728 | // Propagate access. For a non-friend declaration, the access is | ||||||||||||
2729 | // whatever we're propagating from. For a friend, it should be the | ||||||||||||
2730 | // previous declaration we just found. | ||||||||||||
2731 | if (isFriend && Method->getPreviousDecl()) | ||||||||||||
2732 | Method->setAccess(Method->getPreviousDecl()->getAccess()); | ||||||||||||
2733 | else | ||||||||||||
2734 | Method->setAccess(D->getAccess()); | ||||||||||||
2735 | if (FunctionTemplate) | ||||||||||||
2736 | FunctionTemplate->setAccess(Method->getAccess()); | ||||||||||||
2737 | |||||||||||||
2738 | SemaRef.CheckOverrideControl(Method); | ||||||||||||
2739 | |||||||||||||
2740 | // If a function is defined as defaulted or deleted, mark it as such now. | ||||||||||||
2741 | if (D->isExplicitlyDefaulted()) { | ||||||||||||
2742 | if (SubstDefaultedFunction(Method, D)) | ||||||||||||
2743 | return nullptr; | ||||||||||||
2744 | } | ||||||||||||
2745 | if (D->isDeletedAsWritten()) | ||||||||||||
2746 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); | ||||||||||||
2747 | |||||||||||||
2748 | // If this is an explicit specialization, mark the implicitly-instantiated | ||||||||||||
2749 | // template specialization as being an explicit specialization too. | ||||||||||||
2750 | // FIXME: Is this necessary? | ||||||||||||
2751 | if (IsExplicitSpecialization && !isFriend) | ||||||||||||
2752 | SemaRef.CompleteMemberSpecialization(Method, Previous); | ||||||||||||
2753 | |||||||||||||
2754 | // If there's a function template, let our caller handle it. | ||||||||||||
2755 | if (FunctionTemplate) { | ||||||||||||
2756 | // do nothing | ||||||||||||
2757 | |||||||||||||
2758 | // Don't hide a (potentially) valid declaration with an invalid one. | ||||||||||||
2759 | } else if (Method->isInvalidDecl() && !Previous.empty()) { | ||||||||||||
2760 | // do nothing | ||||||||||||
2761 | |||||||||||||
2762 | // Otherwise, check access to friends and make them visible. | ||||||||||||
2763 | } else if (isFriend) { | ||||||||||||
2764 | // We only need to re-check access for methods which we didn't | ||||||||||||
2765 | // manage to match during parsing. | ||||||||||||
2766 | if (!D->getPreviousDecl()) | ||||||||||||
2767 | SemaRef.CheckFriendAccess(Method); | ||||||||||||
2768 | |||||||||||||
2769 | Record->makeDeclVisibleInContext(Method); | ||||||||||||
2770 | |||||||||||||
2771 | // Otherwise, add the declaration. We don't need to do this for | ||||||||||||
2772 | // class-scope specializations because we'll have matched them with | ||||||||||||
2773 | // the appropriate template. | ||||||||||||
2774 | } else { | ||||||||||||
2775 | Owner->addDecl(Method); | ||||||||||||
2776 | } | ||||||||||||
2777 | |||||||||||||
2778 | // PR17480: Honor the used attribute to instantiate member function | ||||||||||||
2779 | // definitions | ||||||||||||
2780 | if (Method->hasAttr<UsedAttr>()) { | ||||||||||||
2781 | if (const auto *A = dyn_cast<CXXRecordDecl>(Owner)) { | ||||||||||||
2782 | SourceLocation Loc; | ||||||||||||
2783 | if (const MemberSpecializationInfo *MSInfo = | ||||||||||||
2784 | A->getMemberSpecializationInfo()) | ||||||||||||
2785 | Loc = MSInfo->getPointOfInstantiation(); | ||||||||||||
2786 | else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A)) | ||||||||||||
2787 | Loc = Spec->getPointOfInstantiation(); | ||||||||||||
2788 | SemaRef.MarkFunctionReferenced(Loc, Method); | ||||||||||||
2789 | } | ||||||||||||
2790 | } | ||||||||||||
2791 | |||||||||||||
2792 | return Method; | ||||||||||||
2793 | } | ||||||||||||
2794 | |||||||||||||
2795 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { | ||||||||||||
2796 | return VisitCXXMethodDecl(D); | ||||||||||||
2797 | } | ||||||||||||
2798 | |||||||||||||
2799 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { | ||||||||||||
2800 | return VisitCXXMethodDecl(D); | ||||||||||||
2801 | } | ||||||||||||
2802 | |||||||||||||
2803 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { | ||||||||||||
2804 | return VisitCXXMethodDecl(D); | ||||||||||||
2805 | } | ||||||||||||
2806 | |||||||||||||
2807 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { | ||||||||||||
2808 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, | ||||||||||||
2809 | std::nullopt, | ||||||||||||
2810 | /*ExpectParameterPack=*/false); | ||||||||||||
2811 | } | ||||||||||||
2812 | |||||||||||||
2813 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( | ||||||||||||
2814 | TemplateTypeParmDecl *D) { | ||||||||||||
2815 | assert(D->getTypeForDecl()->isTemplateTypeParmType())(static_cast <bool> (D->getTypeForDecl()->isTemplateTypeParmType ()) ? void (0) : __assert_fail ("D->getTypeForDecl()->isTemplateTypeParmType()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2815, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
2816 | |||||||||||||
2817 | std::optional<unsigned> NumExpanded; | ||||||||||||
2818 | |||||||||||||
2819 | if (const TypeConstraint *TC = D->getTypeConstraint()) { | ||||||||||||
2820 | if (D->isPackExpansion() && !D->isExpandedParameterPack()) { | ||||||||||||
2821 | assert(TC->getTemplateArgsAsWritten() &&(static_cast <bool> (TC->getTemplateArgsAsWritten() && "type parameter can only be an expansion when explicit arguments " "are specified") ? void (0) : __assert_fail ("TC->getTemplateArgsAsWritten() && \"type parameter can only be an expansion when explicit arguments \" \"are specified\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2823, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
2822 | "type parameter can only be an expansion when explicit arguments "(static_cast <bool> (TC->getTemplateArgsAsWritten() && "type parameter can only be an expansion when explicit arguments " "are specified") ? void (0) : __assert_fail ("TC->getTemplateArgsAsWritten() && \"type parameter can only be an expansion when explicit arguments \" \"are specified\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2823, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
2823 | "are specified")(static_cast <bool> (TC->getTemplateArgsAsWritten() && "type parameter can only be an expansion when explicit arguments " "are specified") ? void (0) : __assert_fail ("TC->getTemplateArgsAsWritten() && \"type parameter can only be an expansion when explicit arguments \" \"are specified\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 2823, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
2824 | // The template type parameter pack's type is a pack expansion of types. | ||||||||||||
2825 | // Determine whether we need to expand this parameter pack into separate | ||||||||||||
2826 | // types. | ||||||||||||
2827 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||||||
2828 | for (auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments()) | ||||||||||||
2829 | SemaRef.collectUnexpandedParameterPacks(ArgLoc, Unexpanded); | ||||||||||||
2830 | |||||||||||||
2831 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||||||
2832 | // be expanded. | ||||||||||||
2833 | bool Expand = true; | ||||||||||||
2834 | bool RetainExpansion = false; | ||||||||||||
2835 | if (SemaRef.CheckParameterPacksForExpansion( | ||||||||||||
2836 | cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint()) | ||||||||||||
2837 | ->getEllipsisLoc(), | ||||||||||||
2838 | SourceRange(TC->getConceptNameLoc(), | ||||||||||||
2839 | TC->hasExplicitTemplateArgs() ? | ||||||||||||
2840 | TC->getTemplateArgsAsWritten()->getRAngleLoc() : | ||||||||||||
2841 | TC->getConceptNameInfo().getEndLoc()), | ||||||||||||
2842 | Unexpanded, TemplateArgs, Expand, RetainExpansion, NumExpanded)) | ||||||||||||
2843 | return nullptr; | ||||||||||||
2844 | } | ||||||||||||
2845 | } | ||||||||||||
2846 | |||||||||||||
2847 | TemplateTypeParmDecl *Inst = TemplateTypeParmDecl::Create( | ||||||||||||
2848 | SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(), | ||||||||||||
2849 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), | ||||||||||||
2850 | D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack(), | ||||||||||||
2851 | D->hasTypeConstraint(), NumExpanded); | ||||||||||||
2852 | |||||||||||||
2853 | Inst->setAccess(AS_public); | ||||||||||||
2854 | Inst->setImplicit(D->isImplicit()); | ||||||||||||
2855 | if (auto *TC = D->getTypeConstraint()) { | ||||||||||||
2856 | if (!D->isImplicit()) { | ||||||||||||
2857 | // Invented template parameter type constraints will be instantiated | ||||||||||||
2858 | // with the corresponding auto-typed parameter as it might reference | ||||||||||||
2859 | // other parameters. | ||||||||||||
2860 | if (SemaRef.SubstTypeConstraint(Inst, TC, TemplateArgs, | ||||||||||||
2861 | EvaluateConstraints)) | ||||||||||||
2862 | return nullptr; | ||||||||||||
2863 | } | ||||||||||||
2864 | } | ||||||||||||
2865 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { | ||||||||||||
2866 | TypeSourceInfo *InstantiatedDefaultArg = | ||||||||||||
2867 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, | ||||||||||||
2868 | D->getDefaultArgumentLoc(), D->getDeclName()); | ||||||||||||
2869 | if (InstantiatedDefaultArg) | ||||||||||||
2870 | Inst->setDefaultArgument(InstantiatedDefaultArg); | ||||||||||||
2871 | } | ||||||||||||
2872 | |||||||||||||
2873 | // Introduce this template parameter's instantiation into the instantiation | ||||||||||||
2874 | // scope. | ||||||||||||
2875 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); | ||||||||||||
2876 | |||||||||||||
2877 | return Inst; | ||||||||||||
2878 | } | ||||||||||||
2879 | |||||||||||||
2880 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( | ||||||||||||
2881 | NonTypeTemplateParmDecl *D) { | ||||||||||||
2882 | // Substitute into the type of the non-type template parameter. | ||||||||||||
2883 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); | ||||||||||||
2884 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; | ||||||||||||
2885 | SmallVector<QualType, 4> ExpandedParameterPackTypes; | ||||||||||||
2886 | bool IsExpandedParameterPack = false; | ||||||||||||
2887 | TypeSourceInfo *DI; | ||||||||||||
2888 | QualType T; | ||||||||||||
2889 | bool Invalid = false; | ||||||||||||
2890 | |||||||||||||
2891 | if (D->isExpandedParameterPack()) { | ||||||||||||
2892 | // The non-type template parameter pack is an already-expanded pack | ||||||||||||
2893 | // expansion of types. Substitute into each of the expanded types. | ||||||||||||
2894 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); | ||||||||||||
2895 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); | ||||||||||||
2896 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { | ||||||||||||
2897 | TypeSourceInfo *NewDI = | ||||||||||||
2898 | SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs, | ||||||||||||
2899 | D->getLocation(), D->getDeclName()); | ||||||||||||
2900 | if (!NewDI) | ||||||||||||
2901 | return nullptr; | ||||||||||||
2902 | |||||||||||||
2903 | QualType NewT = | ||||||||||||
2904 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); | ||||||||||||
2905 | if (NewT.isNull()) | ||||||||||||
2906 | return nullptr; | ||||||||||||
2907 | |||||||||||||
2908 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); | ||||||||||||
2909 | ExpandedParameterPackTypes.push_back(NewT); | ||||||||||||
2910 | } | ||||||||||||
2911 | |||||||||||||
2912 | IsExpandedParameterPack = true; | ||||||||||||
2913 | DI = D->getTypeSourceInfo(); | ||||||||||||
2914 | T = DI->getType(); | ||||||||||||
2915 | } else if (D->isPackExpansion()) { | ||||||||||||
2916 | // The non-type template parameter pack's type is a pack expansion of types. | ||||||||||||
2917 | // Determine whether we need to expand this parameter pack into separate | ||||||||||||
2918 | // types. | ||||||||||||
2919 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); | ||||||||||||
2920 | TypeLoc Pattern = Expansion.getPatternLoc(); | ||||||||||||
2921 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||||||
2922 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); | ||||||||||||
2923 | |||||||||||||
2924 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||||||
2925 | // be expanded. | ||||||||||||
2926 | bool Expand = true; | ||||||||||||
2927 | bool RetainExpansion = false; | ||||||||||||
2928 | std::optional<unsigned> OrigNumExpansions = | ||||||||||||
2929 | Expansion.getTypePtr()->getNumExpansions(); | ||||||||||||
2930 | std::optional<unsigned> NumExpansions = OrigNumExpansions; | ||||||||||||
2931 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), | ||||||||||||
2932 | Pattern.getSourceRange(), | ||||||||||||
2933 | Unexpanded, | ||||||||||||
2934 | TemplateArgs, | ||||||||||||
2935 | Expand, RetainExpansion, | ||||||||||||
2936 | NumExpansions)) | ||||||||||||
2937 | return nullptr; | ||||||||||||
2938 | |||||||||||||
2939 | if (Expand) { | ||||||||||||
2940 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||||||
2941 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); | ||||||||||||
2942 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, | ||||||||||||
2943 | D->getLocation(), | ||||||||||||
2944 | D->getDeclName()); | ||||||||||||
2945 | if (!NewDI) | ||||||||||||
2946 | return nullptr; | ||||||||||||
2947 | |||||||||||||
2948 | QualType NewT = | ||||||||||||
2949 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); | ||||||||||||
2950 | if (NewT.isNull()) | ||||||||||||
2951 | return nullptr; | ||||||||||||
2952 | |||||||||||||
2953 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); | ||||||||||||
2954 | ExpandedParameterPackTypes.push_back(NewT); | ||||||||||||
2955 | } | ||||||||||||
2956 | |||||||||||||
2957 | // Note that we have an expanded parameter pack. The "type" of this | ||||||||||||
2958 | // expanded parameter pack is the original expansion type, but callers | ||||||||||||
2959 | // will end up using the expanded parameter pack types for type-checking. | ||||||||||||
2960 | IsExpandedParameterPack = true; | ||||||||||||
2961 | DI = D->getTypeSourceInfo(); | ||||||||||||
2962 | T = DI->getType(); | ||||||||||||
2963 | } else { | ||||||||||||
2964 | // We cannot fully expand the pack expansion now, so substitute into the | ||||||||||||
2965 | // pattern and create a new pack expansion type. | ||||||||||||
2966 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); | ||||||||||||
2967 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, | ||||||||||||
2968 | D->getLocation(), | ||||||||||||
2969 | D->getDeclName()); | ||||||||||||
2970 | if (!NewPattern) | ||||||||||||
2971 | return nullptr; | ||||||||||||
2972 | |||||||||||||
2973 | SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation()); | ||||||||||||
2974 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), | ||||||||||||
2975 | NumExpansions); | ||||||||||||
2976 | if (!DI) | ||||||||||||
2977 | return nullptr; | ||||||||||||
2978 | |||||||||||||
2979 | T = DI->getType(); | ||||||||||||
2980 | } | ||||||||||||
2981 | } else { | ||||||||||||
2982 | // Simple case: substitution into a parameter that is not a parameter pack. | ||||||||||||
2983 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, | ||||||||||||
2984 | D->getLocation(), D->getDeclName()); | ||||||||||||
2985 | if (!DI) | ||||||||||||
2986 | return nullptr; | ||||||||||||
2987 | |||||||||||||
2988 | // Check that this type is acceptable for a non-type template parameter. | ||||||||||||
2989 | T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation()); | ||||||||||||
2990 | if (T.isNull()) { | ||||||||||||
2991 | T = SemaRef.Context.IntTy; | ||||||||||||
2992 | Invalid = true; | ||||||||||||
2993 | } | ||||||||||||
2994 | } | ||||||||||||
2995 | |||||||||||||
2996 | NonTypeTemplateParmDecl *Param; | ||||||||||||
2997 | if (IsExpandedParameterPack) | ||||||||||||
2998 | Param = NonTypeTemplateParmDecl::Create( | ||||||||||||
2999 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), | ||||||||||||
3000 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), | ||||||||||||
3001 | D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes, | ||||||||||||
3002 | ExpandedParameterPackTypesAsWritten); | ||||||||||||
3003 | else | ||||||||||||
3004 | Param = NonTypeTemplateParmDecl::Create( | ||||||||||||
3005 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), | ||||||||||||
3006 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), | ||||||||||||
3007 | D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); | ||||||||||||
3008 | |||||||||||||
3009 | if (AutoTypeLoc AutoLoc = DI->getTypeLoc().getContainedAutoTypeLoc()) | ||||||||||||
3010 | if (AutoLoc.isConstrained()) | ||||||||||||
3011 | if (SemaRef.AttachTypeConstraint( | ||||||||||||
3012 | AutoLoc, Param, | ||||||||||||
3013 | IsExpandedParameterPack | ||||||||||||
3014 | ? DI->getTypeLoc().getAs<PackExpansionTypeLoc>() | ||||||||||||
3015 | .getEllipsisLoc() | ||||||||||||
3016 | : SourceLocation())) | ||||||||||||
3017 | Invalid = true; | ||||||||||||
3018 | |||||||||||||
3019 | Param->setAccess(AS_public); | ||||||||||||
3020 | Param->setImplicit(D->isImplicit()); | ||||||||||||
3021 | if (Invalid) | ||||||||||||
3022 | Param->setInvalidDecl(); | ||||||||||||
3023 | |||||||||||||
3024 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { | ||||||||||||
3025 | EnterExpressionEvaluationContext ConstantEvaluated( | ||||||||||||
3026 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); | ||||||||||||
3027 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); | ||||||||||||
3028 | if (!Value.isInvalid()) | ||||||||||||
3029 | Param->setDefaultArgument(Value.get()); | ||||||||||||
3030 | } | ||||||||||||
3031 | |||||||||||||
3032 | // Introduce this template parameter's instantiation into the instantiation | ||||||||||||
3033 | // scope. | ||||||||||||
3034 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); | ||||||||||||
3035 | return Param; | ||||||||||||
3036 | } | ||||||||||||
3037 | |||||||||||||
3038 | static void collectUnexpandedParameterPacks( | ||||||||||||
3039 | Sema &S, | ||||||||||||
3040 | TemplateParameterList *Params, | ||||||||||||
3041 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { | ||||||||||||
3042 | for (const auto &P : *Params) { | ||||||||||||
3043 | if (P->isTemplateParameterPack()) | ||||||||||||
3044 | continue; | ||||||||||||
3045 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) | ||||||||||||
3046 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), | ||||||||||||
3047 | Unexpanded); | ||||||||||||
3048 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) | ||||||||||||
3049 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), | ||||||||||||
3050 | Unexpanded); | ||||||||||||
3051 | } | ||||||||||||
3052 | } | ||||||||||||
3053 | |||||||||||||
3054 | Decl * | ||||||||||||
3055 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( | ||||||||||||
3056 | TemplateTemplateParmDecl *D) { | ||||||||||||
3057 | // Instantiate the template parameter list of the template template parameter. | ||||||||||||
3058 | TemplateParameterList *TempParams = D->getTemplateParameters(); | ||||||||||||
3059 | TemplateParameterList *InstParams; | ||||||||||||
3060 | SmallVector<TemplateParameterList*, 8> ExpandedParams; | ||||||||||||
3061 | |||||||||||||
3062 | bool IsExpandedParameterPack = false; | ||||||||||||
3063 | |||||||||||||
3064 | if (D->isExpandedParameterPack()) { | ||||||||||||
3065 | // The template template parameter pack is an already-expanded pack | ||||||||||||
3066 | // expansion of template parameters. Substitute into each of the expanded | ||||||||||||
3067 | // parameters. | ||||||||||||
3068 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); | ||||||||||||
3069 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); | ||||||||||||
3070 | I != N; ++I) { | ||||||||||||
3071 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
3072 | TemplateParameterList *Expansion = | ||||||||||||
3073 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); | ||||||||||||
3074 | if (!Expansion) | ||||||||||||
3075 | return nullptr; | ||||||||||||
3076 | ExpandedParams.push_back(Expansion); | ||||||||||||
3077 | } | ||||||||||||
3078 | |||||||||||||
3079 | IsExpandedParameterPack = true; | ||||||||||||
3080 | InstParams = TempParams; | ||||||||||||
3081 | } else if (D->isPackExpansion()) { | ||||||||||||
3082 | // The template template parameter pack expands to a pack of template | ||||||||||||
3083 | // template parameters. Determine whether we need to expand this parameter | ||||||||||||
3084 | // pack into separate parameters. | ||||||||||||
3085 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||||||
3086 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), | ||||||||||||
3087 | Unexpanded); | ||||||||||||
3088 | |||||||||||||
3089 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||||||
3090 | // be expanded. | ||||||||||||
3091 | bool Expand = true; | ||||||||||||
3092 | bool RetainExpansion = false; | ||||||||||||
3093 | std::optional<unsigned> NumExpansions; | ||||||||||||
3094 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), | ||||||||||||
3095 | TempParams->getSourceRange(), | ||||||||||||
3096 | Unexpanded, | ||||||||||||
3097 | TemplateArgs, | ||||||||||||
3098 | Expand, RetainExpansion, | ||||||||||||
3099 | NumExpansions)) | ||||||||||||
3100 | return nullptr; | ||||||||||||
3101 | |||||||||||||
3102 | if (Expand) { | ||||||||||||
3103 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||||||
3104 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); | ||||||||||||
3105 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
3106 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); | ||||||||||||
3107 | if (!Expansion) | ||||||||||||
3108 | return nullptr; | ||||||||||||
3109 | ExpandedParams.push_back(Expansion); | ||||||||||||
3110 | } | ||||||||||||
3111 | |||||||||||||
3112 | // Note that we have an expanded parameter pack. The "type" of this | ||||||||||||
3113 | // expanded parameter pack is the original expansion type, but callers | ||||||||||||
3114 | // will end up using the expanded parameter pack types for type-checking. | ||||||||||||
3115 | IsExpandedParameterPack = true; | ||||||||||||
3116 | InstParams = TempParams; | ||||||||||||
3117 | } else { | ||||||||||||
3118 | // We cannot fully expand the pack expansion now, so just substitute | ||||||||||||
3119 | // into the pattern. | ||||||||||||
3120 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); | ||||||||||||
3121 | |||||||||||||
3122 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
3123 | InstParams = SubstTemplateParams(TempParams); | ||||||||||||
3124 | if (!InstParams) | ||||||||||||
3125 | return nullptr; | ||||||||||||
3126 | } | ||||||||||||
3127 | } else { | ||||||||||||
3128 | // Perform the actual substitution of template parameters within a new, | ||||||||||||
3129 | // local instantiation scope. | ||||||||||||
3130 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
3131 | InstParams = SubstTemplateParams(TempParams); | ||||||||||||
3132 | if (!InstParams) | ||||||||||||
3133 | return nullptr; | ||||||||||||
3134 | } | ||||||||||||
3135 | |||||||||||||
3136 | // Build the template template parameter. | ||||||||||||
3137 | TemplateTemplateParmDecl *Param; | ||||||||||||
3138 | if (IsExpandedParameterPack) | ||||||||||||
3139 | Param = TemplateTemplateParmDecl::Create( | ||||||||||||
3140 | SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
3141 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), | ||||||||||||
3142 | D->getPosition(), D->getIdentifier(), InstParams, ExpandedParams); | ||||||||||||
3143 | else | ||||||||||||
3144 | Param = TemplateTemplateParmDecl::Create( | ||||||||||||
3145 | SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
3146 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), | ||||||||||||
3147 | D->getPosition(), D->isParameterPack(), D->getIdentifier(), InstParams); | ||||||||||||
3148 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { | ||||||||||||
3149 | NestedNameSpecifierLoc QualifierLoc = | ||||||||||||
3150 | D->getDefaultArgument().getTemplateQualifierLoc(); | ||||||||||||
3151 | QualifierLoc = | ||||||||||||
3152 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); | ||||||||||||
3153 | TemplateName TName = SemaRef.SubstTemplateName( | ||||||||||||
3154 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), | ||||||||||||
3155 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); | ||||||||||||
3156 | if (!TName.isNull()) | ||||||||||||
3157 | Param->setDefaultArgument( | ||||||||||||
3158 | SemaRef.Context, | ||||||||||||
3159 | TemplateArgumentLoc(SemaRef.Context, TemplateArgument(TName), | ||||||||||||
3160 | D->getDefaultArgument().getTemplateQualifierLoc(), | ||||||||||||
3161 | D->getDefaultArgument().getTemplateNameLoc())); | ||||||||||||
3162 | } | ||||||||||||
3163 | Param->setAccess(AS_public); | ||||||||||||
3164 | Param->setImplicit(D->isImplicit()); | ||||||||||||
3165 | |||||||||||||
3166 | // Introduce this template parameter's instantiation into the instantiation | ||||||||||||
3167 | // scope. | ||||||||||||
3168 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); | ||||||||||||
3169 | |||||||||||||
3170 | return Param; | ||||||||||||
3171 | } | ||||||||||||
3172 | |||||||||||||
3173 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { | ||||||||||||
3174 | // Using directives are never dependent (and never contain any types or | ||||||||||||
3175 | // expressions), so they require no explicit instantiation work. | ||||||||||||
3176 | |||||||||||||
3177 | UsingDirectiveDecl *Inst | ||||||||||||
3178 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), | ||||||||||||
3179 | D->getNamespaceKeyLocation(), | ||||||||||||
3180 | D->getQualifierLoc(), | ||||||||||||
3181 | D->getIdentLocation(), | ||||||||||||
3182 | D->getNominatedNamespace(), | ||||||||||||
3183 | D->getCommonAncestor()); | ||||||||||||
3184 | |||||||||||||
3185 | // Add the using directive to its declaration context | ||||||||||||
3186 | // only if this is not a function or method. | ||||||||||||
3187 | if (!Owner->isFunctionOrMethod()) | ||||||||||||
3188 | Owner->addDecl(Inst); | ||||||||||||
3189 | |||||||||||||
3190 | return Inst; | ||||||||||||
3191 | } | ||||||||||||
3192 | |||||||||||||
3193 | Decl *TemplateDeclInstantiator::VisitBaseUsingDecls(BaseUsingDecl *D, | ||||||||||||
3194 | BaseUsingDecl *Inst, | ||||||||||||
3195 | LookupResult *Lookup) { | ||||||||||||
3196 | |||||||||||||
3197 | bool isFunctionScope = Owner->isFunctionOrMethod(); | ||||||||||||
3198 | |||||||||||||
3199 | for (auto *Shadow : D->shadows()) { | ||||||||||||
3200 | // FIXME: UsingShadowDecl doesn't preserve its immediate target, so | ||||||||||||
3201 | // reconstruct it in the case where it matters. Hm, can we extract it from | ||||||||||||
3202 | // the DeclSpec when parsing and save it in the UsingDecl itself? | ||||||||||||
3203 | NamedDecl *OldTarget = Shadow->getTargetDecl(); | ||||||||||||
3204 | if (auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow)) | ||||||||||||
3205 | if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl()) | ||||||||||||
3206 | OldTarget = BaseShadow; | ||||||||||||
3207 | |||||||||||||
3208 | NamedDecl *InstTarget = nullptr; | ||||||||||||
3209 | if (auto *EmptyD = | ||||||||||||
3210 | dyn_cast<UnresolvedUsingIfExistsDecl>(Shadow->getTargetDecl())) { | ||||||||||||
3211 | InstTarget = UnresolvedUsingIfExistsDecl::Create( | ||||||||||||
3212 | SemaRef.Context, Owner, EmptyD->getLocation(), EmptyD->getDeclName()); | ||||||||||||
3213 | } else { | ||||||||||||
3214 | InstTarget = cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( | ||||||||||||
3215 | Shadow->getLocation(), OldTarget, TemplateArgs)); | ||||||||||||
3216 | } | ||||||||||||
3217 | if (!InstTarget) | ||||||||||||
3218 | return nullptr; | ||||||||||||
3219 | |||||||||||||
3220 | UsingShadowDecl *PrevDecl = nullptr; | ||||||||||||
3221 | if (Lookup && | ||||||||||||
3222 | SemaRef.CheckUsingShadowDecl(Inst, InstTarget, *Lookup, PrevDecl)) | ||||||||||||
3223 | continue; | ||||||||||||
3224 | |||||||||||||
3225 | if (UsingShadowDecl *OldPrev = getPreviousDeclForInstantiation(Shadow)) | ||||||||||||
3226 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( | ||||||||||||
3227 | Shadow->getLocation(), OldPrev, TemplateArgs)); | ||||||||||||
3228 | |||||||||||||
3229 | UsingShadowDecl *InstShadow = SemaRef.BuildUsingShadowDecl( | ||||||||||||
3230 | /*Scope*/ nullptr, Inst, InstTarget, PrevDecl); | ||||||||||||
3231 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); | ||||||||||||
3232 | |||||||||||||
3233 | if (isFunctionScope) | ||||||||||||
3234 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); | ||||||||||||
3235 | } | ||||||||||||
3236 | |||||||||||||
3237 | return Inst; | ||||||||||||
3238 | } | ||||||||||||
3239 | |||||||||||||
3240 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { | ||||||||||||
3241 | |||||||||||||
3242 | // The nested name specifier may be dependent, for example | ||||||||||||
3243 | // template <typename T> struct t { | ||||||||||||
3244 | // struct s1 { T f1(); }; | ||||||||||||
3245 | // struct s2 : s1 { using s1::f1; }; | ||||||||||||
3246 | // }; | ||||||||||||
3247 | // template struct t<int>; | ||||||||||||
3248 | // Here, in using s1::f1, s1 refers to t<T>::s1; | ||||||||||||
3249 | // we need to substitute for t<int>::s1. | ||||||||||||
3250 | NestedNameSpecifierLoc QualifierLoc | ||||||||||||
3251 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), | ||||||||||||
3252 | TemplateArgs); | ||||||||||||
3253 | if (!QualifierLoc) | ||||||||||||
3254 | return nullptr; | ||||||||||||
3255 | |||||||||||||
3256 | // For an inheriting constructor declaration, the name of the using | ||||||||||||
3257 | // declaration is the name of a constructor in this class, not in the | ||||||||||||
3258 | // base class. | ||||||||||||
3259 | DeclarationNameInfo NameInfo = D->getNameInfo(); | ||||||||||||
3260 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) | ||||||||||||
3261 | if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.CurContext)) | ||||||||||||
3262 | NameInfo.setName(SemaRef.Context.DeclarationNames.getCXXConstructorName( | ||||||||||||
3263 | SemaRef.Context.getCanonicalType(SemaRef.Context.getRecordType(RD)))); | ||||||||||||
3264 | |||||||||||||
3265 | // We only need to do redeclaration lookups if we're in a class scope (in | ||||||||||||
3266 | // fact, it's not really even possible in non-class scopes). | ||||||||||||
3267 | bool CheckRedeclaration = Owner->isRecord(); | ||||||||||||
3268 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, | ||||||||||||
3269 | Sema::ForVisibleRedeclaration); | ||||||||||||
3270 | |||||||||||||
3271 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, | ||||||||||||
3272 | D->getUsingLoc(), | ||||||||||||
3273 | QualifierLoc, | ||||||||||||
3274 | NameInfo, | ||||||||||||
3275 | D->hasTypename()); | ||||||||||||
3276 | |||||||||||||
3277 | CXXScopeSpec SS; | ||||||||||||
3278 | SS.Adopt(QualifierLoc); | ||||||||||||
3279 | if (CheckRedeclaration) { | ||||||||||||
3280 | Prev.setHideTags(false); | ||||||||||||
3281 | SemaRef.LookupQualifiedName(Prev, Owner); | ||||||||||||
3282 | |||||||||||||
3283 | // Check for invalid redeclarations. | ||||||||||||
3284 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), | ||||||||||||
3285 | D->hasTypename(), SS, | ||||||||||||
3286 | D->getLocation(), Prev)) | ||||||||||||
3287 | NewUD->setInvalidDecl(); | ||||||||||||
3288 | } | ||||||||||||
3289 | |||||||||||||
3290 | if (!NewUD->isInvalidDecl() && | ||||||||||||
3291 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), SS, | ||||||||||||
3292 | NameInfo, D->getLocation(), nullptr, D)) | ||||||||||||
3293 | NewUD->setInvalidDecl(); | ||||||||||||
3294 | |||||||||||||
3295 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); | ||||||||||||
3296 | NewUD->setAccess(D->getAccess()); | ||||||||||||
3297 | Owner->addDecl(NewUD); | ||||||||||||
3298 | |||||||||||||
3299 | // Don't process the shadow decls for an invalid decl. | ||||||||||||
3300 | if (NewUD->isInvalidDecl()) | ||||||||||||
3301 | return NewUD; | ||||||||||||
3302 | |||||||||||||
3303 | // If the using scope was dependent, or we had dependent bases, we need to | ||||||||||||
3304 | // recheck the inheritance | ||||||||||||
3305 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) | ||||||||||||
3306 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); | ||||||||||||
3307 | |||||||||||||
3308 | return VisitBaseUsingDecls(D, NewUD, CheckRedeclaration ? &Prev : nullptr); | ||||||||||||
3309 | } | ||||||||||||
3310 | |||||||||||||
3311 | Decl *TemplateDeclInstantiator::VisitUsingEnumDecl(UsingEnumDecl *D) { | ||||||||||||
3312 | // Cannot be a dependent type, but still could be an instantiation | ||||||||||||
3313 | EnumDecl *EnumD = cast_or_null<EnumDecl>(SemaRef.FindInstantiatedDecl( | ||||||||||||
3314 | D->getLocation(), D->getEnumDecl(), TemplateArgs)); | ||||||||||||
3315 | |||||||||||||
3316 | if (SemaRef.RequireCompleteEnumDecl(EnumD, EnumD->getLocation())) | ||||||||||||
3317 | return nullptr; | ||||||||||||
3318 | |||||||||||||
3319 | TypeSourceInfo *TSI = SemaRef.SubstType(D->getEnumType(), TemplateArgs, | ||||||||||||
3320 | D->getLocation(), D->getDeclName()); | ||||||||||||
3321 | UsingEnumDecl *NewUD = | ||||||||||||
3322 | UsingEnumDecl::Create(SemaRef.Context, Owner, D->getUsingLoc(), | ||||||||||||
3323 | D->getEnumLoc(), D->getLocation(), TSI); | ||||||||||||
3324 | |||||||||||||
3325 | SemaRef.Context.setInstantiatedFromUsingEnumDecl(NewUD, D); | ||||||||||||
3326 | NewUD->setAccess(D->getAccess()); | ||||||||||||
3327 | Owner->addDecl(NewUD); | ||||||||||||
3328 | |||||||||||||
3329 | // Don't process the shadow decls for an invalid decl. | ||||||||||||
3330 | if (NewUD->isInvalidDecl()) | ||||||||||||
3331 | return NewUD; | ||||||||||||
3332 | |||||||||||||
3333 | // We don't have to recheck for duplication of the UsingEnumDecl itself, as it | ||||||||||||
3334 | // cannot be dependent, and will therefore have been checked during template | ||||||||||||
3335 | // definition. | ||||||||||||
3336 | |||||||||||||
3337 | return VisitBaseUsingDecls(D, NewUD, nullptr); | ||||||||||||
3338 | } | ||||||||||||
3339 | |||||||||||||
3340 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { | ||||||||||||
3341 | // Ignore these; we handle them in bulk when processing the UsingDecl. | ||||||||||||
3342 | return nullptr; | ||||||||||||
3343 | } | ||||||||||||
3344 | |||||||||||||
3345 | Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl( | ||||||||||||
3346 | ConstructorUsingShadowDecl *D) { | ||||||||||||
3347 | // Ignore these; we handle them in bulk when processing the UsingDecl. | ||||||||||||
3348 | return nullptr; | ||||||||||||
3349 | } | ||||||||||||
3350 | |||||||||||||
3351 | template <typename T> | ||||||||||||
3352 | Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl( | ||||||||||||
3353 | T *D, bool InstantiatingPackElement) { | ||||||||||||
3354 | // If this is a pack expansion, expand it now. | ||||||||||||
3355 | if (D->isPackExpansion() && !InstantiatingPackElement) { | ||||||||||||
3356 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | ||||||||||||
3357 | SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded); | ||||||||||||
3358 | SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded); | ||||||||||||
3359 | |||||||||||||
3360 | // Determine whether the set of unexpanded parameter packs can and should | ||||||||||||
3361 | // be expanded. | ||||||||||||
3362 | bool Expand = true; | ||||||||||||
3363 | bool RetainExpansion = false; | ||||||||||||
3364 | std::optional<unsigned> NumExpansions; | ||||||||||||
3365 | if (SemaRef.CheckParameterPacksForExpansion( | ||||||||||||
3366 | D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs, | ||||||||||||
3367 | Expand, RetainExpansion, NumExpansions)) | ||||||||||||
3368 | return nullptr; | ||||||||||||
3369 | |||||||||||||
3370 | // This declaration cannot appear within a function template signature, | ||||||||||||
3371 | // so we can't have a partial argument list for a parameter pack. | ||||||||||||
3372 | assert(!RetainExpansion &&(static_cast <bool> (!RetainExpansion && "should never need to retain an expansion for UsingPackDecl" ) ? void (0) : __assert_fail ("!RetainExpansion && \"should never need to retain an expansion for UsingPackDecl\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3373, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
3373 | "should never need to retain an expansion for UsingPackDecl")(static_cast <bool> (!RetainExpansion && "should never need to retain an expansion for UsingPackDecl" ) ? void (0) : __assert_fail ("!RetainExpansion && \"should never need to retain an expansion for UsingPackDecl\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3373, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
3374 | |||||||||||||
3375 | if (!Expand) { | ||||||||||||
3376 | // We cannot fully expand the pack expansion now, so substitute into the | ||||||||||||
3377 | // pattern and create a new pack expansion. | ||||||||||||
3378 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); | ||||||||||||
3379 | return instantiateUnresolvedUsingDecl(D, true); | ||||||||||||
3380 | } | ||||||||||||
3381 | |||||||||||||
3382 | // Within a function, we don't have any normal way to check for conflicts | ||||||||||||
3383 | // between shadow declarations from different using declarations in the | ||||||||||||
3384 | // same pack expansion, but this is always ill-formed because all expansions | ||||||||||||
3385 | // must produce (conflicting) enumerators. | ||||||||||||
3386 | // | ||||||||||||
3387 | // Sadly we can't just reject this in the template definition because it | ||||||||||||
3388 | // could be valid if the pack is empty or has exactly one expansion. | ||||||||||||
3389 | if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) { | ||||||||||||
3390 | SemaRef.Diag(D->getEllipsisLoc(), | ||||||||||||
3391 | diag::err_using_decl_redeclaration_expansion); | ||||||||||||
3392 | return nullptr; | ||||||||||||
3393 | } | ||||||||||||
3394 | |||||||||||||
3395 | // Instantiate the slices of this pack and build a UsingPackDecl. | ||||||||||||
3396 | SmallVector<NamedDecl*, 8> Expansions; | ||||||||||||
3397 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||||||
3398 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); | ||||||||||||
3399 | Decl *Slice = instantiateUnresolvedUsingDecl(D, true); | ||||||||||||
3400 | if (!Slice) | ||||||||||||
3401 | return nullptr; | ||||||||||||
3402 | // Note that we can still get unresolved using declarations here, if we | ||||||||||||
3403 | // had arguments for all packs but the pattern also contained other | ||||||||||||
3404 | // template arguments (this only happens during partial substitution, eg | ||||||||||||
3405 | // into the body of a generic lambda in a function template). | ||||||||||||
3406 | Expansions.push_back(cast<NamedDecl>(Slice)); | ||||||||||||
3407 | } | ||||||||||||
3408 | |||||||||||||
3409 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); | ||||||||||||
3410 | if (isDeclWithinFunction(D)) | ||||||||||||
3411 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); | ||||||||||||
3412 | return NewD; | ||||||||||||
3413 | } | ||||||||||||
3414 | |||||||||||||
3415 | UnresolvedUsingTypenameDecl *TD = dyn_cast<UnresolvedUsingTypenameDecl>(D); | ||||||||||||
3416 | SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation(); | ||||||||||||
3417 | |||||||||||||
3418 | NestedNameSpecifierLoc QualifierLoc | ||||||||||||
3419 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), | ||||||||||||
3420 | TemplateArgs); | ||||||||||||
3421 | if (!QualifierLoc) | ||||||||||||
3422 | return nullptr; | ||||||||||||
3423 | |||||||||||||
3424 | CXXScopeSpec SS; | ||||||||||||
3425 | SS.Adopt(QualifierLoc); | ||||||||||||
3426 | |||||||||||||
3427 | DeclarationNameInfo NameInfo | ||||||||||||
3428 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); | ||||||||||||
3429 | |||||||||||||
3430 | // Produce a pack expansion only if we're not instantiating a particular | ||||||||||||
3431 | // slice of a pack expansion. | ||||||||||||
3432 | bool InstantiatingSlice = D->getEllipsisLoc().isValid() && | ||||||||||||
3433 | SemaRef.ArgumentPackSubstitutionIndex != -1; | ||||||||||||
3434 | SourceLocation EllipsisLoc = | ||||||||||||
3435 | InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc(); | ||||||||||||
3436 | |||||||||||||
3437 | bool IsUsingIfExists = D->template hasAttr<UsingIfExistsAttr>(); | ||||||||||||
3438 | NamedDecl *UD = SemaRef.BuildUsingDeclaration( | ||||||||||||
3439 | /*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(), | ||||||||||||
3440 | /*HasTypename*/ TD, TypenameLoc, SS, NameInfo, EllipsisLoc, | ||||||||||||
3441 | ParsedAttributesView(), | ||||||||||||
3442 | /*IsInstantiation*/ true, IsUsingIfExists); | ||||||||||||
3443 | if (UD) { | ||||||||||||
3444 | SemaRef.InstantiateAttrs(TemplateArgs, D, UD); | ||||||||||||
3445 | SemaRef.Context.setInstantiatedFromUsingDecl(UD, D); | ||||||||||||
3446 | } | ||||||||||||
3447 | |||||||||||||
3448 | return UD; | ||||||||||||
3449 | } | ||||||||||||
3450 | |||||||||||||
3451 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl( | ||||||||||||
3452 | UnresolvedUsingTypenameDecl *D) { | ||||||||||||
3453 | return instantiateUnresolvedUsingDecl(D); | ||||||||||||
3454 | } | ||||||||||||
3455 | |||||||||||||
3456 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl( | ||||||||||||
3457 | UnresolvedUsingValueDecl *D) { | ||||||||||||
3458 | return instantiateUnresolvedUsingDecl(D); | ||||||||||||
3459 | } | ||||||||||||
3460 | |||||||||||||
3461 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingIfExistsDecl( | ||||||||||||
3462 | UnresolvedUsingIfExistsDecl *D) { | ||||||||||||
3463 | llvm_unreachable("referring to unresolved decl out of UsingShadowDecl")::llvm::llvm_unreachable_internal("referring to unresolved decl out of UsingShadowDecl" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3463); | ||||||||||||
3464 | } | ||||||||||||
3465 | |||||||||||||
3466 | Decl *TemplateDeclInstantiator::VisitUsingPackDecl(UsingPackDecl *D) { | ||||||||||||
3467 | SmallVector<NamedDecl*, 8> Expansions; | ||||||||||||
3468 | for (auto *UD : D->expansions()) { | ||||||||||||
3469 | if (NamedDecl *NewUD = | ||||||||||||
3470 | SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs)) | ||||||||||||
3471 | Expansions.push_back(NewUD); | ||||||||||||
3472 | else | ||||||||||||
3473 | return nullptr; | ||||||||||||
3474 | } | ||||||||||||
3475 | |||||||||||||
3476 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); | ||||||||||||
3477 | if (isDeclWithinFunction(D)) | ||||||||||||
3478 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); | ||||||||||||
3479 | return NewD; | ||||||||||||
3480 | } | ||||||||||||
3481 | |||||||||||||
3482 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( | ||||||||||||
3483 | ClassScopeFunctionSpecializationDecl *Decl) { | ||||||||||||
3484 | CXXMethodDecl *OldFD = Decl->getSpecialization(); | ||||||||||||
3485 | return cast_or_null<CXXMethodDecl>( | ||||||||||||
3486 | VisitCXXMethodDecl(OldFD, nullptr, Decl->getTemplateArgsAsWritten())); | ||||||||||||
3487 | } | ||||||||||||
3488 | |||||||||||||
3489 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( | ||||||||||||
3490 | OMPThreadPrivateDecl *D) { | ||||||||||||
3491 | SmallVector<Expr *, 5> Vars; | ||||||||||||
3492 | for (auto *I : D->varlists()) { | ||||||||||||
3493 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); | ||||||||||||
3494 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr")(static_cast <bool> (isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr") ? void (0) : __assert_fail ("isa<DeclRefExpr>(Var) && \"threadprivate arg is not a DeclRefExpr\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3494, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
3495 | Vars.push_back(Var); | ||||||||||||
3496 | } | ||||||||||||
3497 | |||||||||||||
3498 | OMPThreadPrivateDecl *TD = | ||||||||||||
3499 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); | ||||||||||||
3500 | |||||||||||||
3501 | TD->setAccess(AS_public); | ||||||||||||
3502 | Owner->addDecl(TD); | ||||||||||||
3503 | |||||||||||||
3504 | return TD; | ||||||||||||
3505 | } | ||||||||||||
3506 | |||||||||||||
3507 | Decl *TemplateDeclInstantiator::VisitOMPAllocateDecl(OMPAllocateDecl *D) { | ||||||||||||
3508 | SmallVector<Expr *, 5> Vars; | ||||||||||||
3509 | for (auto *I : D->varlists()) { | ||||||||||||
3510 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); | ||||||||||||
3511 | assert(isa<DeclRefExpr>(Var) && "allocate arg is not a DeclRefExpr")(static_cast <bool> (isa<DeclRefExpr>(Var) && "allocate arg is not a DeclRefExpr") ? void (0) : __assert_fail ("isa<DeclRefExpr>(Var) && \"allocate arg is not a DeclRefExpr\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3511, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
3512 | Vars.push_back(Var); | ||||||||||||
3513 | } | ||||||||||||
3514 | SmallVector<OMPClause *, 4> Clauses; | ||||||||||||
3515 | // Copy map clauses from the original mapper. | ||||||||||||
3516 | for (OMPClause *C : D->clauselists()) { | ||||||||||||
3517 | OMPClause *IC = nullptr; | ||||||||||||
3518 | if (auto *AC = dyn_cast<OMPAllocatorClause>(C)) { | ||||||||||||
3519 | ExprResult NewE = SemaRef.SubstExpr(AC->getAllocator(), TemplateArgs); | ||||||||||||
3520 | if (!NewE.isUsable()) | ||||||||||||
3521 | continue; | ||||||||||||
3522 | IC = SemaRef.ActOnOpenMPAllocatorClause( | ||||||||||||
3523 | NewE.get(), AC->getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc()); | ||||||||||||
3524 | } else if (auto *AC = dyn_cast<OMPAlignClause>(C)) { | ||||||||||||
3525 | ExprResult NewE = SemaRef.SubstExpr(AC->getAlignment(), TemplateArgs); | ||||||||||||
3526 | if (!NewE.isUsable()) | ||||||||||||
3527 | continue; | ||||||||||||
3528 | IC = SemaRef.ActOnOpenMPAlignClause(NewE.get(), AC->getBeginLoc(), | ||||||||||||
3529 | AC->getLParenLoc(), AC->getEndLoc()); | ||||||||||||
3530 | // If align clause value ends up being invalid, this can end up null. | ||||||||||||
3531 | if (!IC) | ||||||||||||
3532 | continue; | ||||||||||||
3533 | } | ||||||||||||
3534 | Clauses.push_back(IC); | ||||||||||||
3535 | } | ||||||||||||
3536 | |||||||||||||
3537 | Sema::DeclGroupPtrTy Res = SemaRef.ActOnOpenMPAllocateDirective( | ||||||||||||
3538 | D->getLocation(), Vars, Clauses, Owner); | ||||||||||||
3539 | if (Res.get().isNull()) | ||||||||||||
3540 | return nullptr; | ||||||||||||
3541 | return Res.get().getSingleDecl(); | ||||||||||||
3542 | } | ||||||||||||
3543 | |||||||||||||
3544 | Decl *TemplateDeclInstantiator::VisitOMPRequiresDecl(OMPRequiresDecl *D) { | ||||||||||||
3545 | llvm_unreachable(::llvm::llvm_unreachable_internal("Requires directive cannot be instantiated within a dependent context" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3546) | ||||||||||||
3546 | "Requires directive cannot be instantiated within a dependent context")::llvm::llvm_unreachable_internal("Requires directive cannot be instantiated within a dependent context" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3546); | ||||||||||||
3547 | } | ||||||||||||
3548 | |||||||||||||
3549 | Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( | ||||||||||||
3550 | OMPDeclareReductionDecl *D) { | ||||||||||||
3551 | // Instantiate type and check if it is allowed. | ||||||||||||
3552 | const bool RequiresInstantiation = | ||||||||||||
3553 | D->getType()->isDependentType() || | ||||||||||||
3554 | D->getType()->isInstantiationDependentType() || | ||||||||||||
3555 | D->getType()->containsUnexpandedParameterPack(); | ||||||||||||
3556 | QualType SubstReductionType; | ||||||||||||
3557 | if (RequiresInstantiation) { | ||||||||||||
3558 | SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( | ||||||||||||
3559 | D->getLocation(), | ||||||||||||
3560 | ParsedType::make(SemaRef.SubstType( | ||||||||||||
3561 | D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); | ||||||||||||
3562 | } else { | ||||||||||||
3563 | SubstReductionType = D->getType(); | ||||||||||||
3564 | } | ||||||||||||
3565 | if (SubstReductionType.isNull()) | ||||||||||||
3566 | return nullptr; | ||||||||||||
3567 | Expr *Combiner = D->getCombiner(); | ||||||||||||
3568 | Expr *Init = D->getInitializer(); | ||||||||||||
3569 | bool IsCorrect = true; | ||||||||||||
3570 | // Create instantiated copy. | ||||||||||||
3571 | std::pair<QualType, SourceLocation> ReductionTypes[] = { | ||||||||||||
3572 | std::make_pair(SubstReductionType, D->getLocation())}; | ||||||||||||
3573 | auto *PrevDeclInScope = D->getPrevDeclInScope(); | ||||||||||||
3574 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { | ||||||||||||
3575 | PrevDeclInScope = cast<OMPDeclareReductionDecl>( | ||||||||||||
3576 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) | ||||||||||||
3577 | ->get<Decl *>()); | ||||||||||||
3578 | } | ||||||||||||
3579 | auto DRD = SemaRef.ActOnOpenMPDeclareReductionDirectiveStart( | ||||||||||||
3580 | /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), | ||||||||||||
3581 | PrevDeclInScope); | ||||||||||||
3582 | auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl()); | ||||||||||||
3583 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); | ||||||||||||
3584 | Expr *SubstCombiner = nullptr; | ||||||||||||
3585 | Expr *SubstInitializer = nullptr; | ||||||||||||
3586 | // Combiners instantiation sequence. | ||||||||||||
3587 | if (Combiner) { | ||||||||||||
3588 | SemaRef.ActOnOpenMPDeclareReductionCombinerStart( | ||||||||||||
3589 | /*S=*/nullptr, NewDRD); | ||||||||||||
3590 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( | ||||||||||||
3591 | cast<DeclRefExpr>(D->getCombinerIn())->getDecl(), | ||||||||||||
3592 | cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl()); | ||||||||||||
3593 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( | ||||||||||||
3594 | cast<DeclRefExpr>(D->getCombinerOut())->getDecl(), | ||||||||||||
3595 | cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl()); | ||||||||||||
3596 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); | ||||||||||||
3597 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), | ||||||||||||
3598 | ThisContext); | ||||||||||||
3599 | SubstCombiner = SemaRef.SubstExpr(Combiner, TemplateArgs).get(); | ||||||||||||
3600 | SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); | ||||||||||||
3601 | } | ||||||||||||
3602 | // Initializers instantiation sequence. | ||||||||||||
3603 | if (Init) { | ||||||||||||
3604 | VarDecl *OmpPrivParm = SemaRef.ActOnOpenMPDeclareReductionInitializerStart( | ||||||||||||
3605 | /*S=*/nullptr, NewDRD); | ||||||||||||
3606 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( | ||||||||||||
3607 | cast<DeclRefExpr>(D->getInitOrig())->getDecl(), | ||||||||||||
3608 | cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl()); | ||||||||||||
3609 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( | ||||||||||||
3610 | cast<DeclRefExpr>(D->getInitPriv())->getDecl(), | ||||||||||||
3611 | cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl()); | ||||||||||||
3612 | if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { | ||||||||||||
3613 | SubstInitializer = SemaRef.SubstExpr(Init, TemplateArgs).get(); | ||||||||||||
3614 | } else { | ||||||||||||
3615 | auto *OldPrivParm = | ||||||||||||
3616 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()); | ||||||||||||
3617 | IsCorrect = IsCorrect && OldPrivParm->hasInit(); | ||||||||||||
3618 | if (IsCorrect) | ||||||||||||
3619 | SemaRef.InstantiateVariableInitializer(OmpPrivParm, OldPrivParm, | ||||||||||||
3620 | TemplateArgs); | ||||||||||||
3621 | } | ||||||||||||
3622 | SemaRef.ActOnOpenMPDeclareReductionInitializerEnd(NewDRD, SubstInitializer, | ||||||||||||
3623 | OmpPrivParm); | ||||||||||||
3624 | } | ||||||||||||
3625 | IsCorrect = IsCorrect && SubstCombiner && | ||||||||||||
3626 | (!Init || | ||||||||||||
3627 | (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit && | ||||||||||||
3628 | SubstInitializer) || | ||||||||||||
3629 | (D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && | ||||||||||||
3630 | !SubstInitializer)); | ||||||||||||
3631 | |||||||||||||
3632 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( | ||||||||||||
3633 | /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); | ||||||||||||
3634 | |||||||||||||
3635 | return NewDRD; | ||||||||||||
3636 | } | ||||||||||||
3637 | |||||||||||||
3638 | Decl * | ||||||||||||
3639 | TemplateDeclInstantiator::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { | ||||||||||||
3640 | // Instantiate type and check if it is allowed. | ||||||||||||
3641 | const bool RequiresInstantiation = | ||||||||||||
3642 | D->getType()->isDependentType() || | ||||||||||||
3643 | D->getType()->isInstantiationDependentType() || | ||||||||||||
3644 | D->getType()->containsUnexpandedParameterPack(); | ||||||||||||
3645 | QualType SubstMapperTy; | ||||||||||||
3646 | DeclarationName VN = D->getVarName(); | ||||||||||||
3647 | if (RequiresInstantiation) { | ||||||||||||
3648 | SubstMapperTy = SemaRef.ActOnOpenMPDeclareMapperType( | ||||||||||||
3649 | D->getLocation(), | ||||||||||||
3650 | ParsedType::make(SemaRef.SubstType(D->getType(), TemplateArgs, | ||||||||||||
3651 | D->getLocation(), VN))); | ||||||||||||
3652 | } else { | ||||||||||||
3653 | SubstMapperTy = D->getType(); | ||||||||||||
3654 | } | ||||||||||||
3655 | if (SubstMapperTy.isNull()) | ||||||||||||
3656 | return nullptr; | ||||||||||||
3657 | // Create an instantiated copy of mapper. | ||||||||||||
3658 | auto *PrevDeclInScope = D->getPrevDeclInScope(); | ||||||||||||
3659 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { | ||||||||||||
3660 | PrevDeclInScope = cast<OMPDeclareMapperDecl>( | ||||||||||||
3661 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) | ||||||||||||
3662 | ->get<Decl *>()); | ||||||||||||
3663 | } | ||||||||||||
3664 | bool IsCorrect = true; | ||||||||||||
3665 | SmallVector<OMPClause *, 6> Clauses; | ||||||||||||
3666 | // Instantiate the mapper variable. | ||||||||||||
3667 | DeclarationNameInfo DirName; | ||||||||||||
3668 | SemaRef.StartOpenMPDSABlock(llvm::omp::OMPD_declare_mapper, DirName, | ||||||||||||
3669 | /*S=*/nullptr, | ||||||||||||
3670 | (*D->clauselist_begin())->getBeginLoc()); | ||||||||||||
3671 | ExprResult MapperVarRef = SemaRef.ActOnOpenMPDeclareMapperDirectiveVarDecl( | ||||||||||||
3672 | /*S=*/nullptr, SubstMapperTy, D->getLocation(), VN); | ||||||||||||
3673 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( | ||||||||||||
3674 | cast<DeclRefExpr>(D->getMapperVarRef())->getDecl(), | ||||||||||||
3675 | cast<DeclRefExpr>(MapperVarRef.get())->getDecl()); | ||||||||||||
3676 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); | ||||||||||||
3677 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), | ||||||||||||
3678 | ThisContext); | ||||||||||||
3679 | // Instantiate map clauses. | ||||||||||||
3680 | for (OMPClause *C : D->clauselists()) { | ||||||||||||
3681 | auto *OldC = cast<OMPMapClause>(C); | ||||||||||||
3682 | SmallVector<Expr *, 4> NewVars; | ||||||||||||
3683 | for (Expr *OE : OldC->varlists()) { | ||||||||||||
3684 | Expr *NE = SemaRef.SubstExpr(OE, TemplateArgs).get(); | ||||||||||||
3685 | if (!NE) { | ||||||||||||
3686 | IsCorrect = false; | ||||||||||||
3687 | break; | ||||||||||||
3688 | } | ||||||||||||
3689 | NewVars.push_back(NE); | ||||||||||||
3690 | } | ||||||||||||
3691 | if (!IsCorrect) | ||||||||||||
3692 | break; | ||||||||||||
3693 | NestedNameSpecifierLoc NewQualifierLoc = | ||||||||||||
3694 | SemaRef.SubstNestedNameSpecifierLoc(OldC->getMapperQualifierLoc(), | ||||||||||||
3695 | TemplateArgs); | ||||||||||||
3696 | CXXScopeSpec SS; | ||||||||||||
3697 | SS.Adopt(NewQualifierLoc); | ||||||||||||
3698 | DeclarationNameInfo NewNameInfo = | ||||||||||||
3699 | SemaRef.SubstDeclarationNameInfo(OldC->getMapperIdInfo(), TemplateArgs); | ||||||||||||
3700 | OMPVarListLocTy Locs(OldC->getBeginLoc(), OldC->getLParenLoc(), | ||||||||||||
3701 | OldC->getEndLoc()); | ||||||||||||
3702 | OMPClause *NewC = SemaRef.ActOnOpenMPMapClause( | ||||||||||||
3703 | OldC->getIteratorModifier(), OldC->getMapTypeModifiers(), | ||||||||||||
3704 | OldC->getMapTypeModifiersLoc(), SS, NewNameInfo, OldC->getMapType(), | ||||||||||||
3705 | OldC->isImplicitMapType(), OldC->getMapLoc(), OldC->getColonLoc(), | ||||||||||||
3706 | NewVars, Locs); | ||||||||||||
3707 | Clauses.push_back(NewC); | ||||||||||||
3708 | } | ||||||||||||
3709 | SemaRef.EndOpenMPDSABlock(nullptr); | ||||||||||||
3710 | if (!IsCorrect) | ||||||||||||
3711 | return nullptr; | ||||||||||||
3712 | Sema::DeclGroupPtrTy DG = SemaRef.ActOnOpenMPDeclareMapperDirective( | ||||||||||||
3713 | /*S=*/nullptr, Owner, D->getDeclName(), SubstMapperTy, D->getLocation(), | ||||||||||||
3714 | VN, D->getAccess(), MapperVarRef.get(), Clauses, PrevDeclInScope); | ||||||||||||
3715 | Decl *NewDMD = DG.get().getSingleDecl(); | ||||||||||||
3716 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDMD); | ||||||||||||
3717 | return NewDMD; | ||||||||||||
3718 | } | ||||||||||||
3719 | |||||||||||||
3720 | Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl( | ||||||||||||
3721 | OMPCapturedExprDecl * /*D*/) { | ||||||||||||
3722 | llvm_unreachable("Should not be met in templates")::llvm::llvm_unreachable_internal("Should not be met in templates" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3722); | ||||||||||||
3723 | } | ||||||||||||
3724 | |||||||||||||
3725 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { | ||||||||||||
3726 | return VisitFunctionDecl(D, nullptr); | ||||||||||||
3727 | } | ||||||||||||
3728 | |||||||||||||
3729 | Decl * | ||||||||||||
3730 | TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { | ||||||||||||
3731 | Decl *Inst = VisitFunctionDecl(D, nullptr); | ||||||||||||
3732 | if (Inst && !D->getDescribedFunctionTemplate()) | ||||||||||||
3733 | Owner->addDecl(Inst); | ||||||||||||
3734 | return Inst; | ||||||||||||
3735 | } | ||||||||||||
3736 | |||||||||||||
3737 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { | ||||||||||||
3738 | return VisitCXXMethodDecl(D, nullptr); | ||||||||||||
3739 | } | ||||||||||||
3740 | |||||||||||||
3741 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { | ||||||||||||
3742 | llvm_unreachable("There are only CXXRecordDecls in C++")::llvm::llvm_unreachable_internal("There are only CXXRecordDecls in C++" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3742); | ||||||||||||
3743 | } | ||||||||||||
3744 | |||||||||||||
3745 | Decl * | ||||||||||||
3746 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( | ||||||||||||
3747 | ClassTemplateSpecializationDecl *D) { | ||||||||||||
3748 | // As a MS extension, we permit class-scope explicit specialization | ||||||||||||
3749 | // of member class templates. | ||||||||||||
3750 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); | ||||||||||||
3751 | assert(ClassTemplate->getDeclContext()->isRecord() &&(static_cast <bool> (ClassTemplate->getDeclContext() ->isRecord() && D->getTemplateSpecializationKind () == TSK_ExplicitSpecialization && "can only instantiate an explicit specialization " "for a member class template") ? void (0) : __assert_fail ("ClassTemplate->getDeclContext()->isRecord() && D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && \"can only instantiate an explicit specialization \" \"for a member class template\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3754, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
3752 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&(static_cast <bool> (ClassTemplate->getDeclContext() ->isRecord() && D->getTemplateSpecializationKind () == TSK_ExplicitSpecialization && "can only instantiate an explicit specialization " "for a member class template") ? void (0) : __assert_fail ("ClassTemplate->getDeclContext()->isRecord() && D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && \"can only instantiate an explicit specialization \" \"for a member class template\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3754, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
3753 | "can only instantiate an explicit specialization "(static_cast <bool> (ClassTemplate->getDeclContext() ->isRecord() && D->getTemplateSpecializationKind () == TSK_ExplicitSpecialization && "can only instantiate an explicit specialization " "for a member class template") ? void (0) : __assert_fail ("ClassTemplate->getDeclContext()->isRecord() && D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && \"can only instantiate an explicit specialization \" \"for a member class template\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3754, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
3754 | "for a member class template")(static_cast <bool> (ClassTemplate->getDeclContext() ->isRecord() && D->getTemplateSpecializationKind () == TSK_ExplicitSpecialization && "can only instantiate an explicit specialization " "for a member class template") ? void (0) : __assert_fail ("ClassTemplate->getDeclContext()->isRecord() && D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && \"can only instantiate an explicit specialization \" \"for a member class template\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3754, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
3755 | |||||||||||||
3756 | // Lookup the already-instantiated declaration in the instantiation | ||||||||||||
3757 | // of the class template. | ||||||||||||
3758 | ClassTemplateDecl *InstClassTemplate = | ||||||||||||
3759 | cast_or_null<ClassTemplateDecl>(SemaRef.FindInstantiatedDecl( | ||||||||||||
3760 | D->getLocation(), ClassTemplate, TemplateArgs)); | ||||||||||||
3761 | if (!InstClassTemplate) | ||||||||||||
3762 | return nullptr; | ||||||||||||
3763 | |||||||||||||
3764 | // Substitute into the template arguments of the class template explicit | ||||||||||||
3765 | // specialization. | ||||||||||||
3766 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). | ||||||||||||
3767 | castAs<TemplateSpecializationTypeLoc>(); | ||||||||||||
3768 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), | ||||||||||||
3769 | Loc.getRAngleLoc()); | ||||||||||||
3770 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; | ||||||||||||
3771 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) | ||||||||||||
3772 | ArgLocs.push_back(Loc.getArgLoc(I)); | ||||||||||||
3773 | if (SemaRef.SubstTemplateArguments(ArgLocs, TemplateArgs, InstTemplateArgs)) | ||||||||||||
3774 | return nullptr; | ||||||||||||
3775 | |||||||||||||
3776 | // Check that the template argument list is well-formed for this | ||||||||||||
3777 | // class template. | ||||||||||||
3778 | SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; | ||||||||||||
3779 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, D->getLocation(), | ||||||||||||
3780 | InstTemplateArgs, false, | ||||||||||||
3781 | SugaredConverted, CanonicalConverted, | ||||||||||||
3782 | /*UpdateArgsWithConversions=*/true)) | ||||||||||||
3783 | return nullptr; | ||||||||||||
3784 | |||||||||||||
3785 | // Figure out where to insert this class template explicit specialization | ||||||||||||
3786 | // in the member template's set of class template explicit specializations. | ||||||||||||
3787 | void *InsertPos = nullptr; | ||||||||||||
3788 | ClassTemplateSpecializationDecl *PrevDecl = | ||||||||||||
3789 | InstClassTemplate->findSpecialization(CanonicalConverted, InsertPos); | ||||||||||||
3790 | |||||||||||||
3791 | // Check whether we've already seen a conflicting instantiation of this | ||||||||||||
3792 | // declaration (for instance, if there was a prior implicit instantiation). | ||||||||||||
3793 | bool Ignored; | ||||||||||||
3794 | if (PrevDecl && | ||||||||||||
3795 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), | ||||||||||||
3796 | D->getSpecializationKind(), | ||||||||||||
3797 | PrevDecl, | ||||||||||||
3798 | PrevDecl->getSpecializationKind(), | ||||||||||||
3799 | PrevDecl->getPointOfInstantiation(), | ||||||||||||
3800 | Ignored)) | ||||||||||||
3801 | return nullptr; | ||||||||||||
3802 | |||||||||||||
3803 | // If PrevDecl was a definition and D is also a definition, diagnose. | ||||||||||||
3804 | // This happens in cases like: | ||||||||||||
3805 | // | ||||||||||||
3806 | // template<typename T, typename U> | ||||||||||||
3807 | // struct Outer { | ||||||||||||
3808 | // template<typename X> struct Inner; | ||||||||||||
3809 | // template<> struct Inner<T> {}; | ||||||||||||
3810 | // template<> struct Inner<U> {}; | ||||||||||||
3811 | // }; | ||||||||||||
3812 | // | ||||||||||||
3813 | // Outer<int, int> outer; // error: the explicit specializations of Inner | ||||||||||||
3814 | // // have the same signature. | ||||||||||||
3815 | if (PrevDecl && PrevDecl->getDefinition() && | ||||||||||||
3816 | D->isThisDeclarationADefinition()) { | ||||||||||||
3817 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; | ||||||||||||
3818 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), | ||||||||||||
3819 | diag::note_previous_definition); | ||||||||||||
3820 | return nullptr; | ||||||||||||
3821 | } | ||||||||||||
3822 | |||||||||||||
3823 | // Create the class template partial specialization declaration. | ||||||||||||
3824 | ClassTemplateSpecializationDecl *InstD = | ||||||||||||
3825 | ClassTemplateSpecializationDecl::Create( | ||||||||||||
3826 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), | ||||||||||||
3827 | D->getLocation(), InstClassTemplate, CanonicalConverted, PrevDecl); | ||||||||||||
3828 | |||||||||||||
3829 | // Add this partial specialization to the set of class template partial | ||||||||||||
3830 | // specializations. | ||||||||||||
3831 | if (!PrevDecl) | ||||||||||||
3832 | InstClassTemplate->AddSpecialization(InstD, InsertPos); | ||||||||||||
3833 | |||||||||||||
3834 | // Substitute the nested name specifier, if any. | ||||||||||||
3835 | if (SubstQualifier(D, InstD)) | ||||||||||||
3836 | return nullptr; | ||||||||||||
3837 | |||||||||||||
3838 | // Build the canonical type that describes the converted template | ||||||||||||
3839 | // arguments of the class template explicit specialization. | ||||||||||||
3840 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( | ||||||||||||
3841 | TemplateName(InstClassTemplate), CanonicalConverted, | ||||||||||||
3842 | SemaRef.Context.getRecordType(InstD)); | ||||||||||||
3843 | |||||||||||||
3844 | // Build the fully-sugared type for this class template | ||||||||||||
3845 | // specialization as the user wrote in the specialization | ||||||||||||
3846 | // itself. This means that we'll pretty-print the type retrieved | ||||||||||||
3847 | // from the specialization's declaration the way that the user | ||||||||||||
3848 | // actually wrote the specialization, rather than formatting the | ||||||||||||
3849 | // name based on the "canonical" representation used to store the | ||||||||||||
3850 | // template arguments in the specialization. | ||||||||||||
3851 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( | ||||||||||||
3852 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, | ||||||||||||
3853 | CanonType); | ||||||||||||
3854 | |||||||||||||
3855 | InstD->setAccess(D->getAccess()); | ||||||||||||
3856 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); | ||||||||||||
3857 | InstD->setSpecializationKind(D->getSpecializationKind()); | ||||||||||||
3858 | InstD->setTypeAsWritten(WrittenTy); | ||||||||||||
3859 | InstD->setExternLoc(D->getExternLoc()); | ||||||||||||
3860 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); | ||||||||||||
3861 | |||||||||||||
3862 | Owner->addDecl(InstD); | ||||||||||||
3863 | |||||||||||||
3864 | // Instantiate the members of the class-scope explicit specialization eagerly. | ||||||||||||
3865 | // We don't have support for lazy instantiation of an explicit specialization | ||||||||||||
3866 | // yet, and MSVC eagerly instantiates in this case. | ||||||||||||
3867 | // FIXME: This is wrong in standard C++. | ||||||||||||
3868 | if (D->isThisDeclarationADefinition() && | ||||||||||||
3869 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, | ||||||||||||
3870 | TSK_ImplicitInstantiation, | ||||||||||||
3871 | /*Complain=*/true)) | ||||||||||||
3872 | return nullptr; | ||||||||||||
3873 | |||||||||||||
3874 | return InstD; | ||||||||||||
3875 | } | ||||||||||||
3876 | |||||||||||||
3877 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( | ||||||||||||
3878 | VarTemplateSpecializationDecl *D) { | ||||||||||||
3879 | |||||||||||||
3880 | TemplateArgumentListInfo VarTemplateArgsInfo; | ||||||||||||
3881 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); | ||||||||||||
3882 | assert(VarTemplate &&(static_cast <bool> (VarTemplate && "A template specialization without specialized template?" ) ? void (0) : __assert_fail ("VarTemplate && \"A template specialization without specialized template?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3883, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
3883 | "A template specialization without specialized template?")(static_cast <bool> (VarTemplate && "A template specialization without specialized template?" ) ? void (0) : __assert_fail ("VarTemplate && \"A template specialization without specialized template?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3883, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
3884 | |||||||||||||
3885 | VarTemplateDecl *InstVarTemplate = | ||||||||||||
3886 | cast_or_null<VarTemplateDecl>(SemaRef.FindInstantiatedDecl( | ||||||||||||
3887 | D->getLocation(), VarTemplate, TemplateArgs)); | ||||||||||||
3888 | if (!InstVarTemplate) | ||||||||||||
3889 | return nullptr; | ||||||||||||
3890 | |||||||||||||
3891 | // Substitute the current template arguments. | ||||||||||||
3892 | if (const ASTTemplateArgumentListInfo *TemplateArgsInfo = | ||||||||||||
3893 | D->getTemplateArgsInfo()) { | ||||||||||||
3894 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo->getLAngleLoc()); | ||||||||||||
3895 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo->getRAngleLoc()); | ||||||||||||
3896 | |||||||||||||
3897 | if (SemaRef.SubstTemplateArguments(TemplateArgsInfo->arguments(), | ||||||||||||
3898 | TemplateArgs, VarTemplateArgsInfo)) | ||||||||||||
3899 | return nullptr; | ||||||||||||
3900 | } | ||||||||||||
3901 | |||||||||||||
3902 | // Check that the template argument list is well-formed for this template. | ||||||||||||
3903 | SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; | ||||||||||||
3904 | if (SemaRef.CheckTemplateArgumentList(InstVarTemplate, D->getLocation(), | ||||||||||||
3905 | VarTemplateArgsInfo, false, | ||||||||||||
3906 | SugaredConverted, CanonicalConverted, | ||||||||||||
3907 | /*UpdateArgsWithConversions=*/true)) | ||||||||||||
3908 | return nullptr; | ||||||||||||
3909 | |||||||||||||
3910 | // Check whether we've already seen a declaration of this specialization. | ||||||||||||
3911 | void *InsertPos = nullptr; | ||||||||||||
3912 | VarTemplateSpecializationDecl *PrevDecl = | ||||||||||||
3913 | InstVarTemplate->findSpecialization(CanonicalConverted, InsertPos); | ||||||||||||
3914 | |||||||||||||
3915 | // Check whether we've already seen a conflicting instantiation of this | ||||||||||||
3916 | // declaration (for instance, if there was a prior implicit instantiation). | ||||||||||||
3917 | bool Ignored; | ||||||||||||
3918 | if (PrevDecl && SemaRef.CheckSpecializationInstantiationRedecl( | ||||||||||||
3919 | D->getLocation(), D->getSpecializationKind(), PrevDecl, | ||||||||||||
3920 | PrevDecl->getSpecializationKind(), | ||||||||||||
3921 | PrevDecl->getPointOfInstantiation(), Ignored)) | ||||||||||||
3922 | return nullptr; | ||||||||||||
3923 | |||||||||||||
3924 | return VisitVarTemplateSpecializationDecl( | ||||||||||||
3925 | InstVarTemplate, D, VarTemplateArgsInfo, CanonicalConverted, PrevDecl); | ||||||||||||
3926 | } | ||||||||||||
3927 | |||||||||||||
3928 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( | ||||||||||||
3929 | VarTemplateDecl *VarTemplate, VarDecl *D, | ||||||||||||
3930 | const TemplateArgumentListInfo &TemplateArgsInfo, | ||||||||||||
3931 | ArrayRef<TemplateArgument> Converted, | ||||||||||||
3932 | VarTemplateSpecializationDecl *PrevDecl) { | ||||||||||||
3933 | |||||||||||||
3934 | // Do substitution on the type of the declaration | ||||||||||||
3935 | TypeSourceInfo *DI = | ||||||||||||
3936 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, | ||||||||||||
3937 | D->getTypeSpecStartLoc(), D->getDeclName()); | ||||||||||||
3938 | if (!DI) | ||||||||||||
3939 | return nullptr; | ||||||||||||
3940 | |||||||||||||
3941 | if (DI->getType()->isFunctionType()) { | ||||||||||||
3942 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) | ||||||||||||
3943 | << D->isStaticDataMember() << DI->getType(); | ||||||||||||
3944 | return nullptr; | ||||||||||||
3945 | } | ||||||||||||
3946 | |||||||||||||
3947 | // Build the instantiated declaration | ||||||||||||
3948 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( | ||||||||||||
3949 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), | ||||||||||||
3950 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); | ||||||||||||
3951 | Var->setTemplateArgsInfo(TemplateArgsInfo); | ||||||||||||
3952 | if (!PrevDecl) { | ||||||||||||
3953 | void *InsertPos = nullptr; | ||||||||||||
3954 | VarTemplate->findSpecialization(Converted, InsertPos); | ||||||||||||
3955 | VarTemplate->AddSpecialization(Var, InsertPos); | ||||||||||||
3956 | } | ||||||||||||
3957 | |||||||||||||
3958 | if (SemaRef.getLangOpts().OpenCL) | ||||||||||||
3959 | SemaRef.deduceOpenCLAddressSpace(Var); | ||||||||||||
3960 | |||||||||||||
3961 | // Substitute the nested name specifier, if any. | ||||||||||||
3962 | if (SubstQualifier(D, Var)) | ||||||||||||
3963 | return nullptr; | ||||||||||||
3964 | |||||||||||||
3965 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, | ||||||||||||
3966 | StartingScope, false, PrevDecl); | ||||||||||||
3967 | |||||||||||||
3968 | return Var; | ||||||||||||
3969 | } | ||||||||||||
3970 | |||||||||||||
3971 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { | ||||||||||||
3972 | llvm_unreachable("@defs is not supported in Objective-C++")::llvm::llvm_unreachable_internal("@defs is not supported in Objective-C++" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3972); | ||||||||||||
3973 | } | ||||||||||||
3974 | |||||||||||||
3975 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { | ||||||||||||
3976 | // FIXME: We need to be able to instantiate FriendTemplateDecls. | ||||||||||||
3977 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( | ||||||||||||
3978 | DiagnosticsEngine::Error, | ||||||||||||
3979 | "cannot instantiate %0 yet"); | ||||||||||||
3980 | SemaRef.Diag(D->getLocation(), DiagID) | ||||||||||||
3981 | << D->getDeclKindName(); | ||||||||||||
3982 | |||||||||||||
3983 | return nullptr; | ||||||||||||
3984 | } | ||||||||||||
3985 | |||||||||||||
3986 | Decl *TemplateDeclInstantiator::VisitConceptDecl(ConceptDecl *D) { | ||||||||||||
3987 | llvm_unreachable("Concept definitions cannot reside inside a template")::llvm::llvm_unreachable_internal("Concept definitions cannot reside inside a template" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3987); | ||||||||||||
3988 | } | ||||||||||||
3989 | |||||||||||||
3990 | Decl *TemplateDeclInstantiator::VisitImplicitConceptSpecializationDecl( | ||||||||||||
3991 | ImplicitConceptSpecializationDecl *D) { | ||||||||||||
3992 | llvm_unreachable("Concept specializations cannot reside inside a template")::llvm::llvm_unreachable_internal("Concept specializations cannot reside inside a template" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 3992); | ||||||||||||
3993 | } | ||||||||||||
3994 | |||||||||||||
3995 | Decl * | ||||||||||||
3996 | TemplateDeclInstantiator::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) { | ||||||||||||
3997 | return RequiresExprBodyDecl::Create(SemaRef.Context, D->getDeclContext(), | ||||||||||||
3998 | D->getBeginLoc()); | ||||||||||||
3999 | } | ||||||||||||
4000 | |||||||||||||
4001 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { | ||||||||||||
4002 | llvm_unreachable("Unexpected decl")::llvm::llvm_unreachable_internal("Unexpected decl", "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp" , 4002); | ||||||||||||
4003 | } | ||||||||||||
4004 | |||||||||||||
4005 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, | ||||||||||||
4006 | const MultiLevelTemplateArgumentList &TemplateArgs) { | ||||||||||||
4007 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); | ||||||||||||
4008 | if (D->isInvalidDecl()) | ||||||||||||
4009 | return nullptr; | ||||||||||||
4010 | |||||||||||||
4011 | Decl *SubstD; | ||||||||||||
4012 | runWithSufficientStackSpace(D->getLocation(), [&] { | ||||||||||||
4013 | SubstD = Instantiator.Visit(D); | ||||||||||||
4014 | }); | ||||||||||||
4015 | return SubstD; | ||||||||||||
4016 | } | ||||||||||||
4017 | |||||||||||||
4018 | void TemplateDeclInstantiator::adjustForRewrite(RewriteKind RK, | ||||||||||||
4019 | FunctionDecl *Orig, QualType &T, | ||||||||||||
4020 | TypeSourceInfo *&TInfo, | ||||||||||||
4021 | DeclarationNameInfo &NameInfo) { | ||||||||||||
4022 | assert(RK == RewriteKind::RewriteSpaceshipAsEqualEqual)(static_cast <bool> (RK == RewriteKind::RewriteSpaceshipAsEqualEqual ) ? void (0) : __assert_fail ("RK == RewriteKind::RewriteSpaceshipAsEqualEqual" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4022, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4023 | |||||||||||||
4024 | // C++2a [class.compare.default]p3: | ||||||||||||
4025 | // the return type is replaced with bool | ||||||||||||
4026 | auto *FPT = T->castAs<FunctionProtoType>(); | ||||||||||||
4027 | T = SemaRef.Context.getFunctionType( | ||||||||||||
4028 | SemaRef.Context.BoolTy, FPT->getParamTypes(), FPT->getExtProtoInfo()); | ||||||||||||
4029 | |||||||||||||
4030 | // Update the return type in the source info too. The most straightforward | ||||||||||||
4031 | // way is to create new TypeSourceInfo for the new type. Use the location of | ||||||||||||
4032 | // the '= default' as the location of the new type. | ||||||||||||
4033 | // | ||||||||||||
4034 | // FIXME: Set the correct return type when we initially transform the type, | ||||||||||||
4035 | // rather than delaying it to now. | ||||||||||||
4036 | TypeSourceInfo *NewTInfo = | ||||||||||||
4037 | SemaRef.Context.getTrivialTypeSourceInfo(T, Orig->getEndLoc()); | ||||||||||||
4038 | auto OldLoc = TInfo->getTypeLoc().getAsAdjusted<FunctionProtoTypeLoc>(); | ||||||||||||
4039 | assert(OldLoc && "type of function is not a function type?")(static_cast <bool> (OldLoc && "type of function is not a function type?" ) ? void (0) : __assert_fail ("OldLoc && \"type of function is not a function type?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4039, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4040 | auto NewLoc = NewTInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>(); | ||||||||||||
4041 | for (unsigned I = 0, N = OldLoc.getNumParams(); I != N; ++I) | ||||||||||||
4042 | NewLoc.setParam(I, OldLoc.getParam(I)); | ||||||||||||
4043 | TInfo = NewTInfo; | ||||||||||||
4044 | |||||||||||||
4045 | // and the declarator-id is replaced with operator== | ||||||||||||
4046 | NameInfo.setName( | ||||||||||||
4047 | SemaRef.Context.DeclarationNames.getCXXOperatorName(OO_EqualEqual)); | ||||||||||||
4048 | } | ||||||||||||
4049 | |||||||||||||
4050 | FunctionDecl *Sema::SubstSpaceshipAsEqualEqual(CXXRecordDecl *RD, | ||||||||||||
4051 | FunctionDecl *Spaceship) { | ||||||||||||
4052 | if (Spaceship->isInvalidDecl()) | ||||||||||||
4053 | return nullptr; | ||||||||||||
4054 | |||||||||||||
4055 | // C++2a [class.compare.default]p3: | ||||||||||||
4056 | // an == operator function is declared implicitly [...] with the same | ||||||||||||
4057 | // access and function-definition and in the same class scope as the | ||||||||||||
4058 | // three-way comparison operator function | ||||||||||||
4059 | MultiLevelTemplateArgumentList NoTemplateArgs; | ||||||||||||
4060 | NoTemplateArgs.setKind(TemplateSubstitutionKind::Rewrite); | ||||||||||||
4061 | NoTemplateArgs.addOuterRetainedLevels(RD->getTemplateDepth()); | ||||||||||||
4062 | TemplateDeclInstantiator Instantiator(*this, RD, NoTemplateArgs); | ||||||||||||
4063 | Decl *R; | ||||||||||||
4064 | if (auto *MD = dyn_cast<CXXMethodDecl>(Spaceship)) { | ||||||||||||
4065 | R = Instantiator.VisitCXXMethodDecl( | ||||||||||||
4066 | MD, nullptr, std::nullopt, | ||||||||||||
4067 | TemplateDeclInstantiator::RewriteKind::RewriteSpaceshipAsEqualEqual); | ||||||||||||
4068 | } else { | ||||||||||||
4069 | assert(Spaceship->getFriendObjectKind() &&(static_cast <bool> (Spaceship->getFriendObjectKind( ) && "defaulted spaceship is neither a member nor a friend" ) ? void (0) : __assert_fail ("Spaceship->getFriendObjectKind() && \"defaulted spaceship is neither a member nor a friend\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4070, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
4070 | "defaulted spaceship is neither a member nor a friend")(static_cast <bool> (Spaceship->getFriendObjectKind( ) && "defaulted spaceship is neither a member nor a friend" ) ? void (0) : __assert_fail ("Spaceship->getFriendObjectKind() && \"defaulted spaceship is neither a member nor a friend\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4070, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4071 | |||||||||||||
4072 | R = Instantiator.VisitFunctionDecl( | ||||||||||||
4073 | Spaceship, nullptr, | ||||||||||||
4074 | TemplateDeclInstantiator::RewriteKind::RewriteSpaceshipAsEqualEqual); | ||||||||||||
4075 | if (!R) | ||||||||||||
4076 | return nullptr; | ||||||||||||
4077 | |||||||||||||
4078 | FriendDecl *FD = | ||||||||||||
4079 | FriendDecl::Create(Context, RD, Spaceship->getLocation(), | ||||||||||||
4080 | cast<NamedDecl>(R), Spaceship->getBeginLoc()); | ||||||||||||
4081 | FD->setAccess(AS_public); | ||||||||||||
4082 | RD->addDecl(FD); | ||||||||||||
4083 | } | ||||||||||||
4084 | return cast_or_null<FunctionDecl>(R); | ||||||||||||
4085 | } | ||||||||||||
4086 | |||||||||||||
4087 | /// Instantiates a nested template parameter list in the current | ||||||||||||
4088 | /// instantiation context. | ||||||||||||
4089 | /// | ||||||||||||
4090 | /// \param L The parameter list to instantiate | ||||||||||||
4091 | /// | ||||||||||||
4092 | /// \returns NULL if there was an error | ||||||||||||
4093 | TemplateParameterList * | ||||||||||||
4094 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { | ||||||||||||
4095 | // Get errors for all the parameters before bailing out. | ||||||||||||
4096 | bool Invalid = false; | ||||||||||||
4097 | |||||||||||||
4098 | unsigned N = L->size(); | ||||||||||||
4099 | typedef SmallVector<NamedDecl *, 8> ParamVector; | ||||||||||||
4100 | ParamVector Params; | ||||||||||||
4101 | Params.reserve(N); | ||||||||||||
4102 | for (auto &P : *L) { | ||||||||||||
4103 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(P)); | ||||||||||||
4104 | Params.push_back(D); | ||||||||||||
4105 | Invalid = Invalid || !D || D->isInvalidDecl(); | ||||||||||||
4106 | } | ||||||||||||
4107 | |||||||||||||
4108 | // Clean up if we had an error. | ||||||||||||
4109 | if (Invalid) | ||||||||||||
4110 | return nullptr; | ||||||||||||
4111 | |||||||||||||
4112 | Expr *InstRequiresClause = L->getRequiresClause(); | ||||||||||||
4113 | |||||||||||||
4114 | TemplateParameterList *InstL | ||||||||||||
4115 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), | ||||||||||||
4116 | L->getLAngleLoc(), Params, | ||||||||||||
4117 | L->getRAngleLoc(), InstRequiresClause); | ||||||||||||
4118 | return InstL; | ||||||||||||
4119 | } | ||||||||||||
4120 | |||||||||||||
4121 | TemplateParameterList * | ||||||||||||
4122 | Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, | ||||||||||||
4123 | const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
4124 | bool EvaluateConstraints) { | ||||||||||||
4125 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); | ||||||||||||
4126 | Instantiator.setEvaluateConstraints(EvaluateConstraints); | ||||||||||||
4127 | return Instantiator.SubstTemplateParams(Params); | ||||||||||||
4128 | } | ||||||||||||
4129 | |||||||||||||
4130 | /// Instantiate the declaration of a class template partial | ||||||||||||
4131 | /// specialization. | ||||||||||||
4132 | /// | ||||||||||||
4133 | /// \param ClassTemplate the (instantiated) class template that is partially | ||||||||||||
4134 | // specialized by the instantiation of \p PartialSpec. | ||||||||||||
4135 | /// | ||||||||||||
4136 | /// \param PartialSpec the (uninstantiated) class template partial | ||||||||||||
4137 | /// specialization that we are instantiating. | ||||||||||||
4138 | /// | ||||||||||||
4139 | /// \returns The instantiated partial specialization, if successful; otherwise, | ||||||||||||
4140 | /// NULL to indicate an error. | ||||||||||||
4141 | ClassTemplatePartialSpecializationDecl * | ||||||||||||
4142 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( | ||||||||||||
4143 | ClassTemplateDecl *ClassTemplate, | ||||||||||||
4144 | ClassTemplatePartialSpecializationDecl *PartialSpec) { | ||||||||||||
4145 | // Create a local instantiation scope for this class template partial | ||||||||||||
4146 | // specialization, which will contain the instantiations of the template | ||||||||||||
4147 | // parameters. | ||||||||||||
4148 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
4149 | |||||||||||||
4150 | // Substitute into the template parameters of the class template partial | ||||||||||||
4151 | // specialization. | ||||||||||||
4152 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); | ||||||||||||
4153 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
4154 | if (!InstParams) | ||||||||||||
4155 | return nullptr; | ||||||||||||
4156 | |||||||||||||
4157 | // Substitute into the template arguments of the class template partial | ||||||||||||
4158 | // specialization. | ||||||||||||
4159 | const ASTTemplateArgumentListInfo *TemplArgInfo | ||||||||||||
4160 | = PartialSpec->getTemplateArgsAsWritten(); | ||||||||||||
4161 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, | ||||||||||||
4162 | TemplArgInfo->RAngleLoc); | ||||||||||||
4163 | if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, | ||||||||||||
4164 | InstTemplateArgs)) | ||||||||||||
4165 | return nullptr; | ||||||||||||
4166 | |||||||||||||
4167 | // Check that the template argument list is well-formed for this | ||||||||||||
4168 | // class template. | ||||||||||||
4169 | SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; | ||||||||||||
4170 | if (SemaRef.CheckTemplateArgumentList( | ||||||||||||
4171 | ClassTemplate, PartialSpec->getLocation(), InstTemplateArgs, | ||||||||||||
4172 | /*PartialTemplateArgs=*/false, SugaredConverted, CanonicalConverted)) | ||||||||||||
4173 | return nullptr; | ||||||||||||
4174 | |||||||||||||
4175 | // Check these arguments are valid for a template partial specialization. | ||||||||||||
4176 | if (SemaRef.CheckTemplatePartialSpecializationArgs( | ||||||||||||
4177 | PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(), | ||||||||||||
4178 | CanonicalConverted)) | ||||||||||||
4179 | return nullptr; | ||||||||||||
4180 | |||||||||||||
4181 | // Figure out where to insert this class template partial specialization | ||||||||||||
4182 | // in the member template's set of class template partial specializations. | ||||||||||||
4183 | void *InsertPos = nullptr; | ||||||||||||
4184 | ClassTemplateSpecializationDecl *PrevDecl = | ||||||||||||
4185 | ClassTemplate->findPartialSpecialization(CanonicalConverted, InstParams, | ||||||||||||
4186 | InsertPos); | ||||||||||||
4187 | |||||||||||||
4188 | // Build the canonical type that describes the converted template | ||||||||||||
4189 | // arguments of the class template partial specialization. | ||||||||||||
4190 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( | ||||||||||||
4191 | TemplateName(ClassTemplate), CanonicalConverted); | ||||||||||||
4192 | |||||||||||||
4193 | // Build the fully-sugared type for this class template | ||||||||||||
4194 | // specialization as the user wrote in the specialization | ||||||||||||
4195 | // itself. This means that we'll pretty-print the type retrieved | ||||||||||||
4196 | // from the specialization's declaration the way that the user | ||||||||||||
4197 | // actually wrote the specialization, rather than formatting the | ||||||||||||
4198 | // name based on the "canonical" representation used to store the | ||||||||||||
4199 | // template arguments in the specialization. | ||||||||||||
4200 | TypeSourceInfo *WrittenTy | ||||||||||||
4201 | = SemaRef.Context.getTemplateSpecializationTypeInfo( | ||||||||||||
4202 | TemplateName(ClassTemplate), | ||||||||||||
4203 | PartialSpec->getLocation(), | ||||||||||||
4204 | InstTemplateArgs, | ||||||||||||
4205 | CanonType); | ||||||||||||
4206 | |||||||||||||
4207 | if (PrevDecl) { | ||||||||||||
4208 | // We've already seen a partial specialization with the same template | ||||||||||||
4209 | // parameters and template arguments. This can happen, for example, when | ||||||||||||
4210 | // substituting the outer template arguments ends up causing two | ||||||||||||
4211 | // class template partial specializations of a member class template | ||||||||||||
4212 | // to have identical forms, e.g., | ||||||||||||
4213 | // | ||||||||||||
4214 | // template<typename T, typename U> | ||||||||||||
4215 | // struct Outer { | ||||||||||||
4216 | // template<typename X, typename Y> struct Inner; | ||||||||||||
4217 | // template<typename Y> struct Inner<T, Y>; | ||||||||||||
4218 | // template<typename Y> struct Inner<U, Y>; | ||||||||||||
4219 | // }; | ||||||||||||
4220 | // | ||||||||||||
4221 | // Outer<int, int> outer; // error: the partial specializations of Inner | ||||||||||||
4222 | // // have the same signature. | ||||||||||||
4223 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) | ||||||||||||
4224 | << WrittenTy->getType(); | ||||||||||||
4225 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) | ||||||||||||
4226 | << SemaRef.Context.getTypeDeclType(PrevDecl); | ||||||||||||
4227 | return nullptr; | ||||||||||||
4228 | } | ||||||||||||
4229 | |||||||||||||
4230 | |||||||||||||
4231 | // Create the class template partial specialization declaration. | ||||||||||||
4232 | ClassTemplatePartialSpecializationDecl *InstPartialSpec = | ||||||||||||
4233 | ClassTemplatePartialSpecializationDecl::Create( | ||||||||||||
4234 | SemaRef.Context, PartialSpec->getTagKind(), Owner, | ||||||||||||
4235 | PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, | ||||||||||||
4236 | ClassTemplate, CanonicalConverted, InstTemplateArgs, CanonType, | ||||||||||||
4237 | nullptr); | ||||||||||||
4238 | // Substitute the nested name specifier, if any. | ||||||||||||
4239 | if (SubstQualifier(PartialSpec, InstPartialSpec)) | ||||||||||||
4240 | return nullptr; | ||||||||||||
4241 | |||||||||||||
4242 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); | ||||||||||||
4243 | InstPartialSpec->setTypeAsWritten(WrittenTy); | ||||||||||||
4244 | |||||||||||||
4245 | // Check the completed partial specialization. | ||||||||||||
4246 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); | ||||||||||||
4247 | |||||||||||||
4248 | // Add this partial specialization to the set of class template partial | ||||||||||||
4249 | // specializations. | ||||||||||||
4250 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, | ||||||||||||
4251 | /*InsertPos=*/nullptr); | ||||||||||||
4252 | return InstPartialSpec; | ||||||||||||
4253 | } | ||||||||||||
4254 | |||||||||||||
4255 | /// Instantiate the declaration of a variable template partial | ||||||||||||
4256 | /// specialization. | ||||||||||||
4257 | /// | ||||||||||||
4258 | /// \param VarTemplate the (instantiated) variable template that is partially | ||||||||||||
4259 | /// specialized by the instantiation of \p PartialSpec. | ||||||||||||
4260 | /// | ||||||||||||
4261 | /// \param PartialSpec the (uninstantiated) variable template partial | ||||||||||||
4262 | /// specialization that we are instantiating. | ||||||||||||
4263 | /// | ||||||||||||
4264 | /// \returns The instantiated partial specialization, if successful; otherwise, | ||||||||||||
4265 | /// NULL to indicate an error. | ||||||||||||
4266 | VarTemplatePartialSpecializationDecl * | ||||||||||||
4267 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( | ||||||||||||
4268 | VarTemplateDecl *VarTemplate, | ||||||||||||
4269 | VarTemplatePartialSpecializationDecl *PartialSpec) { | ||||||||||||
4270 | // Create a local instantiation scope for this variable template partial | ||||||||||||
4271 | // specialization, which will contain the instantiations of the template | ||||||||||||
4272 | // parameters. | ||||||||||||
4273 | LocalInstantiationScope Scope(SemaRef); | ||||||||||||
4274 | |||||||||||||
4275 | // Substitute into the template parameters of the variable template partial | ||||||||||||
4276 | // specialization. | ||||||||||||
4277 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); | ||||||||||||
4278 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | ||||||||||||
4279 | if (!InstParams) | ||||||||||||
4280 | return nullptr; | ||||||||||||
4281 | |||||||||||||
4282 | // Substitute into the template arguments of the variable template partial | ||||||||||||
4283 | // specialization. | ||||||||||||
4284 | const ASTTemplateArgumentListInfo *TemplArgInfo | ||||||||||||
4285 | = PartialSpec->getTemplateArgsAsWritten(); | ||||||||||||
4286 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, | ||||||||||||
4287 | TemplArgInfo->RAngleLoc); | ||||||||||||
4288 | if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, | ||||||||||||
4289 | InstTemplateArgs)) | ||||||||||||
4290 | return nullptr; | ||||||||||||
4291 | |||||||||||||
4292 | // Check that the template argument list is well-formed for this | ||||||||||||
4293 | // class template. | ||||||||||||
4294 | SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; | ||||||||||||
4295 | if (SemaRef.CheckTemplateArgumentList( | ||||||||||||
4296 | VarTemplate, PartialSpec->getLocation(), InstTemplateArgs, | ||||||||||||
4297 | /*PartialTemplateArgs=*/false, SugaredConverted, CanonicalConverted)) | ||||||||||||
4298 | return nullptr; | ||||||||||||
4299 | |||||||||||||
4300 | // Check these arguments are valid for a template partial specialization. | ||||||||||||
4301 | if (SemaRef.CheckTemplatePartialSpecializationArgs( | ||||||||||||
4302 | PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(), | ||||||||||||
4303 | CanonicalConverted)) | ||||||||||||
4304 | return nullptr; | ||||||||||||
4305 | |||||||||||||
4306 | // Figure out where to insert this variable template partial specialization | ||||||||||||
4307 | // in the member template's set of variable template partial specializations. | ||||||||||||
4308 | void *InsertPos = nullptr; | ||||||||||||
4309 | VarTemplateSpecializationDecl *PrevDecl = | ||||||||||||
4310 | VarTemplate->findPartialSpecialization(CanonicalConverted, InstParams, | ||||||||||||
4311 | InsertPos); | ||||||||||||
4312 | |||||||||||||
4313 | // Build the canonical type that describes the converted template | ||||||||||||
4314 | // arguments of the variable template partial specialization. | ||||||||||||
4315 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( | ||||||||||||
4316 | TemplateName(VarTemplate), CanonicalConverted); | ||||||||||||
4317 | |||||||||||||
4318 | // Build the fully-sugared type for this variable template | ||||||||||||
4319 | // specialization as the user wrote in the specialization | ||||||||||||
4320 | // itself. This means that we'll pretty-print the type retrieved | ||||||||||||
4321 | // from the specialization's declaration the way that the user | ||||||||||||
4322 | // actually wrote the specialization, rather than formatting the | ||||||||||||
4323 | // name based on the "canonical" representation used to store the | ||||||||||||
4324 | // template arguments in the specialization. | ||||||||||||
4325 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( | ||||||||||||
4326 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, | ||||||||||||
4327 | CanonType); | ||||||||||||
4328 | |||||||||||||
4329 | if (PrevDecl) { | ||||||||||||
4330 | // We've already seen a partial specialization with the same template | ||||||||||||
4331 | // parameters and template arguments. This can happen, for example, when | ||||||||||||
4332 | // substituting the outer template arguments ends up causing two | ||||||||||||
4333 | // variable template partial specializations of a member variable template | ||||||||||||
4334 | // to have identical forms, e.g., | ||||||||||||
4335 | // | ||||||||||||
4336 | // template<typename T, typename U> | ||||||||||||
4337 | // struct Outer { | ||||||||||||
4338 | // template<typename X, typename Y> pair<X,Y> p; | ||||||||||||
4339 | // template<typename Y> pair<T, Y> p; | ||||||||||||
4340 | // template<typename Y> pair<U, Y> p; | ||||||||||||
4341 | // }; | ||||||||||||
4342 | // | ||||||||||||
4343 | // Outer<int, int> outer; // error: the partial specializations of Inner | ||||||||||||
4344 | // // have the same signature. | ||||||||||||
4345 | SemaRef.Diag(PartialSpec->getLocation(), | ||||||||||||
4346 | diag::err_var_partial_spec_redeclared) | ||||||||||||
4347 | << WrittenTy->getType(); | ||||||||||||
4348 | SemaRef.Diag(PrevDecl->getLocation(), | ||||||||||||
4349 | diag::note_var_prev_partial_spec_here); | ||||||||||||
4350 | return nullptr; | ||||||||||||
4351 | } | ||||||||||||
4352 | |||||||||||||
4353 | // Do substitution on the type of the declaration | ||||||||||||
4354 | TypeSourceInfo *DI = SemaRef.SubstType( | ||||||||||||
4355 | PartialSpec->getTypeSourceInfo(), TemplateArgs, | ||||||||||||
4356 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); | ||||||||||||
4357 | if (!DI) | ||||||||||||
4358 | return nullptr; | ||||||||||||
4359 | |||||||||||||
4360 | if (DI->getType()->isFunctionType()) { | ||||||||||||
4361 | SemaRef.Diag(PartialSpec->getLocation(), | ||||||||||||
4362 | diag::err_variable_instantiates_to_function) | ||||||||||||
4363 | << PartialSpec->isStaticDataMember() << DI->getType(); | ||||||||||||
4364 | return nullptr; | ||||||||||||
4365 | } | ||||||||||||
4366 | |||||||||||||
4367 | // Create the variable template partial specialization declaration. | ||||||||||||
4368 | VarTemplatePartialSpecializationDecl *InstPartialSpec = | ||||||||||||
4369 | VarTemplatePartialSpecializationDecl::Create( | ||||||||||||
4370 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), | ||||||||||||
4371 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), | ||||||||||||
4372 | DI, PartialSpec->getStorageClass(), CanonicalConverted, | ||||||||||||
4373 | InstTemplateArgs); | ||||||||||||
4374 | |||||||||||||
4375 | // Substitute the nested name specifier, if any. | ||||||||||||
4376 | if (SubstQualifier(PartialSpec, InstPartialSpec)) | ||||||||||||
4377 | return nullptr; | ||||||||||||
4378 | |||||||||||||
4379 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); | ||||||||||||
4380 | InstPartialSpec->setTypeAsWritten(WrittenTy); | ||||||||||||
4381 | |||||||||||||
4382 | // Check the completed partial specialization. | ||||||||||||
4383 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); | ||||||||||||
4384 | |||||||||||||
4385 | // Add this partial specialization to the set of variable template partial | ||||||||||||
4386 | // specializations. The instantiation of the initializer is not necessary. | ||||||||||||
4387 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); | ||||||||||||
4388 | |||||||||||||
4389 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, | ||||||||||||
4390 | LateAttrs, Owner, StartingScope); | ||||||||||||
4391 | |||||||||||||
4392 | return InstPartialSpec; | ||||||||||||
4393 | } | ||||||||||||
4394 | |||||||||||||
4395 | TypeSourceInfo* | ||||||||||||
4396 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, | ||||||||||||
4397 | SmallVectorImpl<ParmVarDecl *> &Params) { | ||||||||||||
4398 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); | ||||||||||||
4399 | assert(OldTInfo && "substituting function without type source info")(static_cast <bool> (OldTInfo && "substituting function without type source info" ) ? void (0) : __assert_fail ("OldTInfo && \"substituting function without type source info\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4399, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4400 | assert(Params.empty() && "parameter vector is non-empty at start")(static_cast <bool> (Params.empty() && "parameter vector is non-empty at start" ) ? void (0) : __assert_fail ("Params.empty() && \"parameter vector is non-empty at start\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4400, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4401 | |||||||||||||
4402 | CXXRecordDecl *ThisContext = nullptr; | ||||||||||||
4403 | Qualifiers ThisTypeQuals; | ||||||||||||
4404 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { | ||||||||||||
4405 | ThisContext = cast<CXXRecordDecl>(Owner); | ||||||||||||
4406 | ThisTypeQuals = Method->getMethodQualifiers(); | ||||||||||||
4407 | } | ||||||||||||
4408 | |||||||||||||
4409 | TypeSourceInfo *NewTInfo = SemaRef.SubstFunctionDeclType( | ||||||||||||
4410 | OldTInfo, TemplateArgs, D->getTypeSpecStartLoc(), D->getDeclName(), | ||||||||||||
4411 | ThisContext, ThisTypeQuals, EvaluateConstraints); | ||||||||||||
4412 | if (!NewTInfo) | ||||||||||||
4413 | return nullptr; | ||||||||||||
4414 | |||||||||||||
4415 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); | ||||||||||||
4416 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { | ||||||||||||
4417 | if (NewTInfo != OldTInfo) { | ||||||||||||
4418 | // Get parameters from the new type info. | ||||||||||||
4419 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); | ||||||||||||
4420 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); | ||||||||||||
4421 | unsigned NewIdx = 0; | ||||||||||||
4422 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); | ||||||||||||
4423 | OldIdx != NumOldParams; ++OldIdx) { | ||||||||||||
4424 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); | ||||||||||||
4425 | if (!OldParam) | ||||||||||||
4426 | return nullptr; | ||||||||||||
4427 | |||||||||||||
4428 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; | ||||||||||||
4429 | |||||||||||||
4430 | std::optional<unsigned> NumArgumentsInExpansion; | ||||||||||||
4431 | if (OldParam->isParameterPack()) | ||||||||||||
4432 | NumArgumentsInExpansion = | ||||||||||||
4433 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), | ||||||||||||
4434 | TemplateArgs); | ||||||||||||
4435 | if (!NumArgumentsInExpansion) { | ||||||||||||
4436 | // Simple case: normal parameter, or a parameter pack that's | ||||||||||||
4437 | // instantiated to a (still-dependent) parameter pack. | ||||||||||||
4438 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); | ||||||||||||
4439 | Params.push_back(NewParam); | ||||||||||||
4440 | Scope->InstantiatedLocal(OldParam, NewParam); | ||||||||||||
4441 | } else { | ||||||||||||
4442 | // Parameter pack expansion: make the instantiation an argument pack. | ||||||||||||
4443 | Scope->MakeInstantiatedLocalArgPack(OldParam); | ||||||||||||
4444 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { | ||||||||||||
4445 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); | ||||||||||||
4446 | Params.push_back(NewParam); | ||||||||||||
4447 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); | ||||||||||||
4448 | } | ||||||||||||
4449 | } | ||||||||||||
4450 | } | ||||||||||||
4451 | } else { | ||||||||||||
4452 | // The function type itself was not dependent and therefore no | ||||||||||||
4453 | // substitution occurred. However, we still need to instantiate | ||||||||||||
4454 | // the function parameters themselves. | ||||||||||||
4455 | const FunctionProtoType *OldProto = | ||||||||||||
4456 | cast<FunctionProtoType>(OldProtoLoc.getType()); | ||||||||||||
4457 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; | ||||||||||||
4458 | ++i) { | ||||||||||||
4459 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); | ||||||||||||
4460 | if (!OldParam) { | ||||||||||||
4461 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( | ||||||||||||
4462 | D, D->getLocation(), OldProto->getParamType(i))); | ||||||||||||
4463 | continue; | ||||||||||||
4464 | } | ||||||||||||
4465 | |||||||||||||
4466 | ParmVarDecl *Parm = | ||||||||||||
4467 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); | ||||||||||||
4468 | if (!Parm) | ||||||||||||
4469 | return nullptr; | ||||||||||||
4470 | Params.push_back(Parm); | ||||||||||||
4471 | } | ||||||||||||
4472 | } | ||||||||||||
4473 | } else { | ||||||||||||
4474 | // If the type of this function, after ignoring parentheses, is not | ||||||||||||
4475 | // *directly* a function type, then we're instantiating a function that | ||||||||||||
4476 | // was declared via a typedef or with attributes, e.g., | ||||||||||||
4477 | // | ||||||||||||
4478 | // typedef int functype(int, int); | ||||||||||||
4479 | // functype func; | ||||||||||||
4480 | // int __cdecl meth(int, int); | ||||||||||||
4481 | // | ||||||||||||
4482 | // In this case, we'll just go instantiate the ParmVarDecls that we | ||||||||||||
4483 | // synthesized in the method declaration. | ||||||||||||
4484 | SmallVector<QualType, 4> ParamTypes; | ||||||||||||
4485 | Sema::ExtParameterInfoBuilder ExtParamInfos; | ||||||||||||
4486 | if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr, | ||||||||||||
4487 | TemplateArgs, ParamTypes, &Params, | ||||||||||||
4488 | ExtParamInfos)) | ||||||||||||
4489 | return nullptr; | ||||||||||||
4490 | } | ||||||||||||
4491 | |||||||||||||
4492 | return NewTInfo; | ||||||||||||
4493 | } | ||||||||||||
4494 | |||||||||||||
4495 | /// Introduce the instantiated function parameters into the local | ||||||||||||
4496 | /// instantiation scope, and set the parameter names to those used | ||||||||||||
4497 | /// in the template. | ||||||||||||
4498 | bool Sema::addInstantiatedParametersToScope( | ||||||||||||
4499 | FunctionDecl *Function, const FunctionDecl *PatternDecl, | ||||||||||||
4500 | LocalInstantiationScope &Scope, | ||||||||||||
4501 | const MultiLevelTemplateArgumentList &TemplateArgs) { | ||||||||||||
4502 | unsigned FParamIdx = 0; | ||||||||||||
4503 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { | ||||||||||||
4504 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); | ||||||||||||
4505 | if (!PatternParam->isParameterPack()) { | ||||||||||||
4506 | // Simple case: not a parameter pack. | ||||||||||||
4507 | assert(FParamIdx < Function->getNumParams())(static_cast <bool> (FParamIdx < Function->getNumParams ()) ? void (0) : __assert_fail ("FParamIdx < Function->getNumParams()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4507, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4508 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); | ||||||||||||
4509 | FunctionParam->setDeclName(PatternParam->getDeclName()); | ||||||||||||
4510 | // If the parameter's type is not dependent, update it to match the type | ||||||||||||
4511 | // in the pattern. They can differ in top-level cv-qualifiers, and we want | ||||||||||||
4512 | // the pattern's type here. If the type is dependent, they can't differ, | ||||||||||||
4513 | // per core issue 1668. Substitute into the type from the pattern, in case | ||||||||||||
4514 | // it's instantiation-dependent. | ||||||||||||
4515 | // FIXME: Updating the type to work around this is at best fragile. | ||||||||||||
4516 | if (!PatternDecl->getType()->isDependentType()) { | ||||||||||||
4517 | QualType T = SubstType(PatternParam->getType(), TemplateArgs, | ||||||||||||
4518 | FunctionParam->getLocation(), | ||||||||||||
4519 | FunctionParam->getDeclName()); | ||||||||||||
4520 | if (T.isNull()) | ||||||||||||
4521 | return true; | ||||||||||||
4522 | FunctionParam->setType(T); | ||||||||||||
4523 | } | ||||||||||||
4524 | |||||||||||||
4525 | Scope.InstantiatedLocal(PatternParam, FunctionParam); | ||||||||||||
4526 | ++FParamIdx; | ||||||||||||
4527 | continue; | ||||||||||||
4528 | } | ||||||||||||
4529 | |||||||||||||
4530 | // Expand the parameter pack. | ||||||||||||
4531 | Scope.MakeInstantiatedLocalArgPack(PatternParam); | ||||||||||||
4532 | std::optional<unsigned> NumArgumentsInExpansion = | ||||||||||||
4533 | getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); | ||||||||||||
4534 | if (NumArgumentsInExpansion) { | ||||||||||||
4535 | QualType PatternType = | ||||||||||||
4536 | PatternParam->getType()->castAs<PackExpansionType>()->getPattern(); | ||||||||||||
4537 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { | ||||||||||||
4538 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); | ||||||||||||
4539 | FunctionParam->setDeclName(PatternParam->getDeclName()); | ||||||||||||
4540 | if (!PatternDecl->getType()->isDependentType()) { | ||||||||||||
4541 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, Arg); | ||||||||||||
4542 | QualType T = | ||||||||||||
4543 | SubstType(PatternType, TemplateArgs, FunctionParam->getLocation(), | ||||||||||||
4544 | FunctionParam->getDeclName()); | ||||||||||||
4545 | if (T.isNull()) | ||||||||||||
4546 | return true; | ||||||||||||
4547 | FunctionParam->setType(T); | ||||||||||||
4548 | } | ||||||||||||
4549 | |||||||||||||
4550 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); | ||||||||||||
4551 | ++FParamIdx; | ||||||||||||
4552 | } | ||||||||||||
4553 | } | ||||||||||||
4554 | } | ||||||||||||
4555 | |||||||||||||
4556 | return false; | ||||||||||||
4557 | } | ||||||||||||
4558 | |||||||||||||
4559 | bool Sema::InstantiateDefaultArgument(SourceLocation CallLoc, FunctionDecl *FD, | ||||||||||||
4560 | ParmVarDecl *Param) { | ||||||||||||
4561 | assert(Param->hasUninstantiatedDefaultArg())(static_cast <bool> (Param->hasUninstantiatedDefaultArg ()) ? void (0) : __assert_fail ("Param->hasUninstantiatedDefaultArg()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4561, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4562 | |||||||||||||
4563 | // Instantiate the expression. | ||||||||||||
4564 | // | ||||||||||||
4565 | // FIXME: Pass in a correct Pattern argument, otherwise | ||||||||||||
4566 | // getTemplateInstantiationArgs uses the lexical context of FD, e.g. | ||||||||||||
4567 | // | ||||||||||||
4568 | // template<typename T> | ||||||||||||
4569 | // struct A { | ||||||||||||
4570 | // static int FooImpl(); | ||||||||||||
4571 | // | ||||||||||||
4572 | // template<typename Tp> | ||||||||||||
4573 | // // bug: default argument A<T>::FooImpl() is evaluated with 2-level | ||||||||||||
4574 | // // template argument list [[T], [Tp]], should be [[Tp]]. | ||||||||||||
4575 | // friend A<Tp> Foo(int a); | ||||||||||||
4576 | // }; | ||||||||||||
4577 | // | ||||||||||||
4578 | // template<typename T> | ||||||||||||
4579 | // A<T> Foo(int a = A<T>::FooImpl()); | ||||||||||||
4580 | MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs( | ||||||||||||
4581 | FD, /*Final=*/false, nullptr, /*RelativeToPrimary=*/true); | ||||||||||||
4582 | |||||||||||||
4583 | if (SubstDefaultArgument(CallLoc, Param, TemplateArgs, /*ForCallExpr*/ true)) | ||||||||||||
4584 | return true; | ||||||||||||
4585 | |||||||||||||
4586 | if (ASTMutationListener *L = getASTMutationListener()) | ||||||||||||
4587 | L->DefaultArgumentInstantiated(Param); | ||||||||||||
4588 | |||||||||||||
4589 | return false; | ||||||||||||
4590 | } | ||||||||||||
4591 | |||||||||||||
4592 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, | ||||||||||||
4593 | FunctionDecl *Decl) { | ||||||||||||
4594 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); | ||||||||||||
4595 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) | ||||||||||||
4596 | return; | ||||||||||||
4597 | |||||||||||||
4598 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, | ||||||||||||
4599 | InstantiatingTemplate::ExceptionSpecification()); | ||||||||||||
4600 | if (Inst.isInvalid()) { | ||||||||||||
4601 | // We hit the instantiation depth limit. Clear the exception specification | ||||||||||||
4602 | // so that our callers don't have to cope with EST_Uninstantiated. | ||||||||||||
4603 | UpdateExceptionSpec(Decl, EST_None); | ||||||||||||
4604 | return; | ||||||||||||
4605 | } | ||||||||||||
4606 | if (Inst.isAlreadyInstantiating()) { | ||||||||||||
4607 | // This exception specification indirectly depends on itself. Reject. | ||||||||||||
4608 | // FIXME: Corresponding rule in the standard? | ||||||||||||
4609 | Diag(PointOfInstantiation, diag::err_exception_spec_cycle) << Decl; | ||||||||||||
4610 | UpdateExceptionSpec(Decl, EST_None); | ||||||||||||
4611 | return; | ||||||||||||
4612 | } | ||||||||||||
4613 | |||||||||||||
4614 | // Enter the scope of this instantiation. We don't use | ||||||||||||
4615 | // PushDeclContext because we don't have a scope. | ||||||||||||
4616 | Sema::ContextRAII savedContext(*this, Decl); | ||||||||||||
4617 | LocalInstantiationScope Scope(*this); | ||||||||||||
4618 | |||||||||||||
4619 | MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs( | ||||||||||||
4620 | Decl, /*Final=*/false, nullptr, /*RelativeToPrimary*/ true); | ||||||||||||
4621 | |||||||||||||
4622 | // FIXME: We can't use getTemplateInstantiationPattern(false) in general | ||||||||||||
4623 | // here, because for a non-defining friend declaration in a class template, | ||||||||||||
4624 | // we don't store enough information to map back to the friend declaration in | ||||||||||||
4625 | // the template. | ||||||||||||
4626 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); | ||||||||||||
4627 | if (addInstantiatedParametersToScope(Decl, Template, Scope, TemplateArgs)) { | ||||||||||||
4628 | UpdateExceptionSpec(Decl, EST_None); | ||||||||||||
4629 | return; | ||||||||||||
4630 | } | ||||||||||||
4631 | |||||||||||||
4632 | SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(), | ||||||||||||
4633 | TemplateArgs); | ||||||||||||
4634 | } | ||||||||||||
4635 | |||||||||||||
4636 | /// Initializes the common fields of an instantiation function | ||||||||||||
4637 | /// declaration (New) from the corresponding fields of its template (Tmpl). | ||||||||||||
4638 | /// | ||||||||||||
4639 | /// \returns true if there was an error | ||||||||||||
4640 | bool | ||||||||||||
4641 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, | ||||||||||||
4642 | FunctionDecl *Tmpl) { | ||||||||||||
4643 | New->setImplicit(Tmpl->isImplicit()); | ||||||||||||
4644 | |||||||||||||
4645 | // Forward the mangling number from the template to the instantiated decl. | ||||||||||||
4646 | SemaRef.Context.setManglingNumber(New, | ||||||||||||
4647 | SemaRef.Context.getManglingNumber(Tmpl)); | ||||||||||||
4648 | |||||||||||||
4649 | // If we are performing substituting explicitly-specified template arguments | ||||||||||||
4650 | // or deduced template arguments into a function template and we reach this | ||||||||||||
4651 | // point, we are now past the point where SFINAE applies and have committed | ||||||||||||
4652 | // to keeping the new function template specialization. We therefore | ||||||||||||
4653 | // convert the active template instantiation for the function template | ||||||||||||
4654 | // into a template instantiation for this specific function template | ||||||||||||
4655 | // specialization, which is not a SFINAE context, so that we diagnose any | ||||||||||||
4656 | // further errors in the declaration itself. | ||||||||||||
4657 | // | ||||||||||||
4658 | // FIXME: This is a hack. | ||||||||||||
4659 | typedef Sema::CodeSynthesisContext ActiveInstType; | ||||||||||||
4660 | ActiveInstType &ActiveInst = SemaRef.CodeSynthesisContexts.back(); | ||||||||||||
4661 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || | ||||||||||||
4662 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { | ||||||||||||
4663 | if (FunctionTemplateDecl *FunTmpl | ||||||||||||
4664 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { | ||||||||||||
4665 | assert(FunTmpl->getTemplatedDecl() == Tmpl &&(static_cast <bool> (FunTmpl->getTemplatedDecl() == Tmpl && "Deduction from the wrong function template?") ? void (0) : __assert_fail ("FunTmpl->getTemplatedDecl() == Tmpl && \"Deduction from the wrong function template?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4666, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
4666 | "Deduction from the wrong function template?")(static_cast <bool> (FunTmpl->getTemplatedDecl() == Tmpl && "Deduction from the wrong function template?") ? void (0) : __assert_fail ("FunTmpl->getTemplatedDecl() == Tmpl && \"Deduction from the wrong function template?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4666, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4667 | (void) FunTmpl; | ||||||||||||
4668 | SemaRef.InstantiatingSpecializations.erase( | ||||||||||||
4669 | {ActiveInst.Entity->getCanonicalDecl(), ActiveInst.Kind}); | ||||||||||||
4670 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); | ||||||||||||
4671 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; | ||||||||||||
4672 | ActiveInst.Entity = New; | ||||||||||||
4673 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); | ||||||||||||
4674 | } | ||||||||||||
4675 | } | ||||||||||||
4676 | |||||||||||||
4677 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); | ||||||||||||
4678 | assert(Proto && "Function template without prototype?")(static_cast <bool> (Proto && "Function template without prototype?" ) ? void (0) : __assert_fail ("Proto && \"Function template without prototype?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4678, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4679 | |||||||||||||
4680 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { | ||||||||||||
4681 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); | ||||||||||||
4682 | |||||||||||||
4683 | // DR1330: In C++11, defer instantiation of a non-trivial | ||||||||||||
4684 | // exception specification. | ||||||||||||
4685 | // DR1484: Local classes and their members are instantiated along with the | ||||||||||||
4686 | // containing function. | ||||||||||||
4687 | if (SemaRef.getLangOpts().CPlusPlus11 && | ||||||||||||
4688 | EPI.ExceptionSpec.Type != EST_None && | ||||||||||||
4689 | EPI.ExceptionSpec.Type != EST_DynamicNone && | ||||||||||||
4690 | EPI.ExceptionSpec.Type != EST_BasicNoexcept && | ||||||||||||
4691 | !Tmpl->isInLocalScopeForInstantiation()) { | ||||||||||||
4692 | FunctionDecl *ExceptionSpecTemplate = Tmpl; | ||||||||||||
4693 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) | ||||||||||||
4694 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; | ||||||||||||
4695 | ExceptionSpecificationType NewEST = EST_Uninstantiated; | ||||||||||||
4696 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) | ||||||||||||
4697 | NewEST = EST_Unevaluated; | ||||||||||||
4698 | |||||||||||||
4699 | // Mark the function has having an uninstantiated exception specification. | ||||||||||||
4700 | const FunctionProtoType *NewProto | ||||||||||||
4701 | = New->getType()->getAs<FunctionProtoType>(); | ||||||||||||
4702 | assert(NewProto && "Template instantiation without function prototype?")(static_cast <bool> (NewProto && "Template instantiation without function prototype?" ) ? void (0) : __assert_fail ("NewProto && \"Template instantiation without function prototype?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4702, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4703 | EPI = NewProto->getExtProtoInfo(); | ||||||||||||
4704 | EPI.ExceptionSpec.Type = NewEST; | ||||||||||||
4705 | EPI.ExceptionSpec.SourceDecl = New; | ||||||||||||
4706 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; | ||||||||||||
4707 | New->setType(SemaRef.Context.getFunctionType( | ||||||||||||
4708 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); | ||||||||||||
4709 | } else { | ||||||||||||
4710 | Sema::ContextRAII SwitchContext(SemaRef, New); | ||||||||||||
4711 | SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs); | ||||||||||||
4712 | } | ||||||||||||
4713 | } | ||||||||||||
4714 | |||||||||||||
4715 | // Get the definition. Leaves the variable unchanged if undefined. | ||||||||||||
4716 | const FunctionDecl *Definition = Tmpl; | ||||||||||||
4717 | Tmpl->isDefined(Definition); | ||||||||||||
4718 | |||||||||||||
4719 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, | ||||||||||||
4720 | LateAttrs, StartingScope); | ||||||||||||
4721 | |||||||||||||
4722 | return false; | ||||||||||||
4723 | } | ||||||||||||
4724 | |||||||||||||
4725 | /// Initializes common fields of an instantiated method | ||||||||||||
4726 | /// declaration (New) from the corresponding fields of its template | ||||||||||||
4727 | /// (Tmpl). | ||||||||||||
4728 | /// | ||||||||||||
4729 | /// \returns true if there was an error | ||||||||||||
4730 | bool | ||||||||||||
4731 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, | ||||||||||||
4732 | CXXMethodDecl *Tmpl) { | ||||||||||||
4733 | if (InitFunctionInstantiation(New, Tmpl)) | ||||||||||||
4734 | return true; | ||||||||||||
4735 | |||||||||||||
4736 | if (isa<CXXDestructorDecl>(New) && SemaRef.getLangOpts().CPlusPlus11) | ||||||||||||
4737 | SemaRef.AdjustDestructorExceptionSpec(cast<CXXDestructorDecl>(New)); | ||||||||||||
4738 | |||||||||||||
4739 | New->setAccess(Tmpl->getAccess()); | ||||||||||||
4740 | if (Tmpl->isVirtualAsWritten()) | ||||||||||||
4741 | New->setVirtualAsWritten(true); | ||||||||||||
4742 | |||||||||||||
4743 | // FIXME: New needs a pointer to Tmpl | ||||||||||||
4744 | return false; | ||||||||||||
4745 | } | ||||||||||||
4746 | |||||||||||||
4747 | bool TemplateDeclInstantiator::SubstDefaultedFunction(FunctionDecl *New, | ||||||||||||
4748 | FunctionDecl *Tmpl) { | ||||||||||||
4749 | // Transfer across any unqualified lookups. | ||||||||||||
4750 | if (auto *DFI = Tmpl->getDefaultedFunctionInfo()) { | ||||||||||||
4751 | SmallVector<DeclAccessPair, 32> Lookups; | ||||||||||||
4752 | Lookups.reserve(DFI->getUnqualifiedLookups().size()); | ||||||||||||
4753 | bool AnyChanged = false; | ||||||||||||
4754 | for (DeclAccessPair DA : DFI->getUnqualifiedLookups()) { | ||||||||||||
4755 | NamedDecl *D = SemaRef.FindInstantiatedDecl(New->getLocation(), | ||||||||||||
4756 | DA.getDecl(), TemplateArgs); | ||||||||||||
4757 | if (!D) | ||||||||||||
4758 | return true; | ||||||||||||
4759 | AnyChanged |= (D != DA.getDecl()); | ||||||||||||
4760 | Lookups.push_back(DeclAccessPair::make(D, DA.getAccess())); | ||||||||||||
4761 | } | ||||||||||||
4762 | |||||||||||||
4763 | // It's unlikely that substitution will change any declarations. Don't | ||||||||||||
4764 | // store an unnecessary copy in that case. | ||||||||||||
4765 | New->setDefaultedFunctionInfo( | ||||||||||||
4766 | AnyChanged ? FunctionDecl::DefaultedFunctionInfo::Create( | ||||||||||||
4767 | SemaRef.Context, Lookups) | ||||||||||||
4768 | : DFI); | ||||||||||||
4769 | } | ||||||||||||
4770 | |||||||||||||
4771 | SemaRef.SetDeclDefaulted(New, Tmpl->getLocation()); | ||||||||||||
4772 | return false; | ||||||||||||
4773 | } | ||||||||||||
4774 | |||||||||||||
4775 | /// Instantiate (or find existing instantiation of) a function template with a | ||||||||||||
4776 | /// given set of template arguments. | ||||||||||||
4777 | /// | ||||||||||||
4778 | /// Usually this should not be used, and template argument deduction should be | ||||||||||||
4779 | /// used in its place. | ||||||||||||
4780 | FunctionDecl * | ||||||||||||
4781 | Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD, | ||||||||||||
4782 | const TemplateArgumentList *Args, | ||||||||||||
4783 | SourceLocation Loc) { | ||||||||||||
4784 | FunctionDecl *FD = FTD->getTemplatedDecl(); | ||||||||||||
4785 | |||||||||||||
4786 | sema::TemplateDeductionInfo Info(Loc); | ||||||||||||
4787 | InstantiatingTemplate Inst( | ||||||||||||
4788 | *this, Loc, FTD, Args->asArray(), | ||||||||||||
4789 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); | ||||||||||||
4790 | if (Inst.isInvalid()) | ||||||||||||
4791 | return nullptr; | ||||||||||||
4792 | |||||||||||||
4793 | ContextRAII SavedContext(*this, FD); | ||||||||||||
4794 | MultiLevelTemplateArgumentList MArgs(FTD, Args->asArray(), | ||||||||||||
4795 | /*Final=*/false); | ||||||||||||
4796 | |||||||||||||
4797 | return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs)); | ||||||||||||
4798 | } | ||||||||||||
4799 | |||||||||||||
4800 | /// Instantiate the definition of the given function from its | ||||||||||||
4801 | /// template. | ||||||||||||
4802 | /// | ||||||||||||
4803 | /// \param PointOfInstantiation the point at which the instantiation was | ||||||||||||
4804 | /// required. Note that this is not precisely a "point of instantiation" | ||||||||||||
4805 | /// for the function, but it's close. | ||||||||||||
4806 | /// | ||||||||||||
4807 | /// \param Function the already-instantiated declaration of a | ||||||||||||
4808 | /// function template specialization or member function of a class template | ||||||||||||
4809 | /// specialization. | ||||||||||||
4810 | /// | ||||||||||||
4811 | /// \param Recursive if true, recursively instantiates any functions that | ||||||||||||
4812 | /// are required by this instantiation. | ||||||||||||
4813 | /// | ||||||||||||
4814 | /// \param DefinitionRequired if true, then we are performing an explicit | ||||||||||||
4815 | /// instantiation where the body of the function is required. Complain if | ||||||||||||
4816 | /// there is no such body. | ||||||||||||
4817 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, | ||||||||||||
4818 | FunctionDecl *Function, | ||||||||||||
4819 | bool Recursive, | ||||||||||||
4820 | bool DefinitionRequired, | ||||||||||||
4821 | bool AtEndOfTU) { | ||||||||||||
4822 | if (Function->isInvalidDecl() || isa<CXXDeductionGuideDecl>(Function)) | ||||||||||||
4823 | return; | ||||||||||||
4824 | |||||||||||||
4825 | // Never instantiate an explicit specialization except if it is a class scope | ||||||||||||
4826 | // explicit specialization. | ||||||||||||
4827 | TemplateSpecializationKind TSK = | ||||||||||||
4828 | Function->getTemplateSpecializationKindForInstantiation(); | ||||||||||||
4829 | if (TSK == TSK_ExplicitSpecialization) | ||||||||||||
4830 | return; | ||||||||||||
4831 | |||||||||||||
4832 | // Never implicitly instantiate a builtin; we don't actually need a function | ||||||||||||
4833 | // body. | ||||||||||||
4834 | if (Function->getBuiltinID() && TSK == TSK_ImplicitInstantiation && | ||||||||||||
4835 | !DefinitionRequired) | ||||||||||||
4836 | return; | ||||||||||||
4837 | |||||||||||||
4838 | // Don't instantiate a definition if we already have one. | ||||||||||||
4839 | const FunctionDecl *ExistingDefn = nullptr; | ||||||||||||
4840 | if (Function->isDefined(ExistingDefn, | ||||||||||||
4841 | /*CheckForPendingFriendDefinition=*/true)) { | ||||||||||||
4842 | if (ExistingDefn->isThisDeclarationADefinition()) | ||||||||||||
4843 | return; | ||||||||||||
4844 | |||||||||||||
4845 | // If we're asked to instantiate a function whose body comes from an | ||||||||||||
4846 | // instantiated friend declaration, attach the instantiated body to the | ||||||||||||
4847 | // corresponding declaration of the function. | ||||||||||||
4848 | assert(ExistingDefn->isThisDeclarationInstantiatedFromAFriendDefinition())(static_cast <bool> (ExistingDefn->isThisDeclarationInstantiatedFromAFriendDefinition ()) ? void (0) : __assert_fail ("ExistingDefn->isThisDeclarationInstantiatedFromAFriendDefinition()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4848, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4849 | Function = const_cast<FunctionDecl*>(ExistingDefn); | ||||||||||||
4850 | } | ||||||||||||
4851 | |||||||||||||
4852 | // Find the function body that we'll be substituting. | ||||||||||||
4853 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); | ||||||||||||
4854 | assert(PatternDecl && "instantiating a non-template")(static_cast <bool> (PatternDecl && "instantiating a non-template" ) ? void (0) : __assert_fail ("PatternDecl && \"instantiating a non-template\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4854, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4855 | |||||||||||||
4856 | const FunctionDecl *PatternDef = PatternDecl->getDefinition(); | ||||||||||||
4857 | Stmt *Pattern = nullptr; | ||||||||||||
4858 | if (PatternDef) { | ||||||||||||
4859 | Pattern = PatternDef->getBody(PatternDef); | ||||||||||||
4860 | PatternDecl = PatternDef; | ||||||||||||
4861 | if (PatternDef->willHaveBody()) | ||||||||||||
4862 | PatternDef = nullptr; | ||||||||||||
4863 | } | ||||||||||||
4864 | |||||||||||||
4865 | // FIXME: We need to track the instantiation stack in order to know which | ||||||||||||
4866 | // definitions should be visible within this instantiation. | ||||||||||||
4867 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, | ||||||||||||
4868 | Function->getInstantiatedFromMemberFunction(), | ||||||||||||
4869 | PatternDecl, PatternDef, TSK, | ||||||||||||
4870 | /*Complain*/DefinitionRequired)) { | ||||||||||||
4871 | if (DefinitionRequired) | ||||||||||||
4872 | Function->setInvalidDecl(); | ||||||||||||
4873 | else if (TSK == TSK_ExplicitInstantiationDefinition || | ||||||||||||
4874 | (Function->isConstexpr() && !Recursive)) { | ||||||||||||
4875 | // Try again at the end of the translation unit (at which point a | ||||||||||||
4876 | // definition will be required). | ||||||||||||
4877 | assert(!Recursive)(static_cast <bool> (!Recursive) ? void (0) : __assert_fail ("!Recursive", "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp" , 4877, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4878 | Function->setInstantiationIsPending(true); | ||||||||||||
4879 | PendingInstantiations.push_back( | ||||||||||||
4880 | std::make_pair(Function, PointOfInstantiation)); | ||||||||||||
4881 | } else if (TSK == TSK_ImplicitInstantiation) { | ||||||||||||
4882 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && | ||||||||||||
4883 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { | ||||||||||||
4884 | Diag(PointOfInstantiation, diag::warn_func_template_missing) | ||||||||||||
4885 | << Function; | ||||||||||||
4886 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); | ||||||||||||
4887 | if (getLangOpts().CPlusPlus11) | ||||||||||||
4888 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) | ||||||||||||
4889 | << Function; | ||||||||||||
4890 | } | ||||||||||||
4891 | } | ||||||||||||
4892 | |||||||||||||
4893 | return; | ||||||||||||
4894 | } | ||||||||||||
4895 | |||||||||||||
4896 | // Postpone late parsed template instantiations. | ||||||||||||
4897 | if (PatternDecl->isLateTemplateParsed() && | ||||||||||||
4898 | !LateTemplateParser) { | ||||||||||||
4899 | Function->setInstantiationIsPending(true); | ||||||||||||
4900 | LateParsedInstantiations.push_back( | ||||||||||||
4901 | std::make_pair(Function, PointOfInstantiation)); | ||||||||||||
4902 | return; | ||||||||||||
4903 | } | ||||||||||||
4904 | |||||||||||||
4905 | llvm::TimeTraceScope TimeScope("InstantiateFunction", [&]() { | ||||||||||||
4906 | std::string Name; | ||||||||||||
4907 | llvm::raw_string_ostream OS(Name); | ||||||||||||
4908 | Function->getNameForDiagnostic(OS, getPrintingPolicy(), | ||||||||||||
4909 | /*Qualified=*/true); | ||||||||||||
4910 | return Name; | ||||||||||||
4911 | }); | ||||||||||||
4912 | |||||||||||||
4913 | // If we're performing recursive template instantiation, create our own | ||||||||||||
4914 | // queue of pending implicit instantiations that we will instantiate later, | ||||||||||||
4915 | // while we're still within our own instantiation context. | ||||||||||||
4916 | // This has to happen before LateTemplateParser below is called, so that | ||||||||||||
4917 | // it marks vtables used in late parsed templates as used. | ||||||||||||
4918 | GlobalEagerInstantiationScope GlobalInstantiations(*this, | ||||||||||||
4919 | /*Enabled=*/Recursive); | ||||||||||||
4920 | LocalEagerInstantiationScope LocalInstantiations(*this); | ||||||||||||
4921 | |||||||||||||
4922 | // Call the LateTemplateParser callback if there is a need to late parse | ||||||||||||
4923 | // a templated function definition. | ||||||||||||
4924 | if (!Pattern && PatternDecl->isLateTemplateParsed() && | ||||||||||||
4925 | LateTemplateParser) { | ||||||||||||
4926 | // FIXME: Optimize to allow individual templates to be deserialized. | ||||||||||||
4927 | if (PatternDecl->isFromASTFile()) | ||||||||||||
4928 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); | ||||||||||||
4929 | |||||||||||||
4930 | auto LPTIter = LateParsedTemplateMap.find(PatternDecl); | ||||||||||||
4931 | assert(LPTIter != LateParsedTemplateMap.end() &&(static_cast <bool> (LPTIter != LateParsedTemplateMap.end () && "missing LateParsedTemplate") ? void (0) : __assert_fail ("LPTIter != LateParsedTemplateMap.end() && \"missing LateParsedTemplate\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4932, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
4932 | "missing LateParsedTemplate")(static_cast <bool> (LPTIter != LateParsedTemplateMap.end () && "missing LateParsedTemplate") ? void (0) : __assert_fail ("LPTIter != LateParsedTemplateMap.end() && \"missing LateParsedTemplate\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4932, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4933 | LateTemplateParser(OpaqueParser, *LPTIter->second); | ||||||||||||
4934 | Pattern = PatternDecl->getBody(PatternDecl); | ||||||||||||
4935 | updateAttrsForLateParsedTemplate(PatternDecl, Function); | ||||||||||||
4936 | } | ||||||||||||
4937 | |||||||||||||
4938 | // Note, we should never try to instantiate a deleted function template. | ||||||||||||
4939 | assert((Pattern || PatternDecl->isDefaulted() ||(static_cast <bool> ((Pattern || PatternDecl->isDefaulted () || PatternDecl->hasSkippedBody()) && "unexpected kind of function template definition" ) ? void (0) : __assert_fail ("(Pattern || PatternDecl->isDefaulted() || PatternDecl->hasSkippedBody()) && \"unexpected kind of function template definition\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4941, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
4940 | PatternDecl->hasSkippedBody()) &&(static_cast <bool> ((Pattern || PatternDecl->isDefaulted () || PatternDecl->hasSkippedBody()) && "unexpected kind of function template definition" ) ? void (0) : __assert_fail ("(Pattern || PatternDecl->isDefaulted() || PatternDecl->hasSkippedBody()) && \"unexpected kind of function template definition\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4941, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
4941 | "unexpected kind of function template definition")(static_cast <bool> ((Pattern || PatternDecl->isDefaulted () || PatternDecl->hasSkippedBody()) && "unexpected kind of function template definition" ) ? void (0) : __assert_fail ("(Pattern || PatternDecl->isDefaulted() || PatternDecl->hasSkippedBody()) && \"unexpected kind of function template definition\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4941, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4942 | |||||||||||||
4943 | // C++1y [temp.explicit]p10: | ||||||||||||
4944 | // Except for inline functions, declarations with types deduced from their | ||||||||||||
4945 | // initializer or return value, and class template specializations, other | ||||||||||||
4946 | // explicit instantiation declarations have the effect of suppressing the | ||||||||||||
4947 | // implicit instantiation of the entity to which they refer. | ||||||||||||
4948 | if (TSK == TSK_ExplicitInstantiationDeclaration && | ||||||||||||
4949 | !PatternDecl->isInlined() && | ||||||||||||
4950 | !PatternDecl->getReturnType()->getContainedAutoType()) | ||||||||||||
4951 | return; | ||||||||||||
4952 | |||||||||||||
4953 | if (PatternDecl->isInlined()) { | ||||||||||||
4954 | // Function, and all later redeclarations of it (from imported modules, | ||||||||||||
4955 | // for instance), are now implicitly inline. | ||||||||||||
4956 | for (auto *D = Function->getMostRecentDecl(); /**/; | ||||||||||||
4957 | D = D->getPreviousDecl()) { | ||||||||||||
4958 | D->setImplicitlyInline(); | ||||||||||||
4959 | if (D == Function) | ||||||||||||
4960 | break; | ||||||||||||
4961 | } | ||||||||||||
4962 | } | ||||||||||||
4963 | |||||||||||||
4964 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); | ||||||||||||
4965 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) | ||||||||||||
4966 | return; | ||||||||||||
4967 | PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(), | ||||||||||||
4968 | "instantiating function definition"); | ||||||||||||
4969 | |||||||||||||
4970 | // The instantiation is visible here, even if it was first declared in an | ||||||||||||
4971 | // unimported module. | ||||||||||||
4972 | Function->setVisibleDespiteOwningModule(); | ||||||||||||
4973 | |||||||||||||
4974 | // Copy the inner loc start from the pattern. | ||||||||||||
4975 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); | ||||||||||||
4976 | |||||||||||||
4977 | EnterExpressionEvaluationContext EvalContext( | ||||||||||||
4978 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); | ||||||||||||
4979 | |||||||||||||
4980 | // Introduce a new scope where local variable instantiations will be | ||||||||||||
4981 | // recorded, unless we're actually a member function within a local | ||||||||||||
4982 | // class, in which case we need to merge our results with the parent | ||||||||||||
4983 | // scope (of the enclosing function). The exception is instantiating | ||||||||||||
4984 | // a function template specialization, since the template to be | ||||||||||||
4985 | // instantiated already has references to locals properly substituted. | ||||||||||||
4986 | bool MergeWithParentScope = false; | ||||||||||||
4987 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) | ||||||||||||
4988 | MergeWithParentScope = | ||||||||||||
4989 | Rec->isLocalClass() && !Function->isFunctionTemplateSpecialization(); | ||||||||||||
4990 | |||||||||||||
4991 | LocalInstantiationScope Scope(*this, MergeWithParentScope); | ||||||||||||
4992 | auto RebuildTypeSourceInfoForDefaultSpecialMembers = [&]() { | ||||||||||||
4993 | // Special members might get their TypeSourceInfo set up w.r.t the | ||||||||||||
4994 | // PatternDecl context, in which case parameters could still be pointing | ||||||||||||
4995 | // back to the original class, make sure arguments are bound to the | ||||||||||||
4996 | // instantiated record instead. | ||||||||||||
4997 | assert(PatternDecl->isDefaulted() &&(static_cast <bool> (PatternDecl->isDefaulted() && "Special member needs to be defaulted") ? void (0) : __assert_fail ("PatternDecl->isDefaulted() && \"Special member needs to be defaulted\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4998, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
| |||||||||||||
4998 | "Special member needs to be defaulted")(static_cast <bool> (PatternDecl->isDefaulted() && "Special member needs to be defaulted") ? void (0) : __assert_fail ("PatternDecl->isDefaulted() && \"Special member needs to be defaulted\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 4998, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
4999 | auto PatternSM = getDefaultedFunctionKind(PatternDecl).asSpecialMember(); | ||||||||||||
5000 | if (!(PatternSM == Sema::CXXCopyConstructor || | ||||||||||||
5001 | PatternSM == Sema::CXXCopyAssignment || | ||||||||||||
5002 | PatternSM == Sema::CXXMoveConstructor || | ||||||||||||
5003 | PatternSM == Sema::CXXMoveAssignment)) | ||||||||||||
5004 | return; | ||||||||||||
5005 | |||||||||||||
5006 | auto *NewRec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()); | ||||||||||||
5007 | const auto *PatternRec = | ||||||||||||
5008 | dyn_cast<CXXRecordDecl>(PatternDecl->getDeclContext()); | ||||||||||||
5009 | if (!NewRec
| ||||||||||||
5010 | return; | ||||||||||||
5011 | if (!PatternRec->isLambda()) | ||||||||||||
5012 | return; | ||||||||||||
5013 | |||||||||||||
5014 | struct SpecialMemberTypeInfoRebuilder | ||||||||||||
5015 | : TreeTransform<SpecialMemberTypeInfoRebuilder> { | ||||||||||||
5016 | using Base = TreeTransform<SpecialMemberTypeInfoRebuilder>; | ||||||||||||
5017 | const CXXRecordDecl *OldDecl; | ||||||||||||
5018 | CXXRecordDecl *NewDecl; | ||||||||||||
5019 | |||||||||||||
5020 | SpecialMemberTypeInfoRebuilder(Sema &SemaRef, const CXXRecordDecl *O, | ||||||||||||
5021 | CXXRecordDecl *N) | ||||||||||||
5022 | : TreeTransform(SemaRef), OldDecl(O), NewDecl(N) {} | ||||||||||||
5023 | |||||||||||||
5024 | bool TransformExceptionSpec(SourceLocation Loc, | ||||||||||||
5025 | FunctionProtoType::ExceptionSpecInfo &ESI, | ||||||||||||
5026 | SmallVectorImpl<QualType> &Exceptions, | ||||||||||||
5027 | bool &Changed) { | ||||||||||||
5028 | return false; | ||||||||||||
5029 | } | ||||||||||||
5030 | |||||||||||||
5031 | QualType TransformRecordType(TypeLocBuilder &TLB, RecordTypeLoc TL) { | ||||||||||||
5032 | const RecordType *T = TL.getTypePtr(); | ||||||||||||
5033 | RecordDecl *Record = cast_or_null<RecordDecl>( | ||||||||||||
5034 | getDerived().TransformDecl(TL.getNameLoc(), T->getDecl())); | ||||||||||||
5035 | if (Record != OldDecl) | ||||||||||||
5036 | return Base::TransformRecordType(TLB, TL); | ||||||||||||
5037 | |||||||||||||
5038 | QualType Result = getDerived().RebuildRecordType(NewDecl); | ||||||||||||
5039 | if (Result.isNull()) | ||||||||||||
5040 | return QualType(); | ||||||||||||
5041 | |||||||||||||
5042 | RecordTypeLoc NewTL = TLB.push<RecordTypeLoc>(Result); | ||||||||||||
5043 | NewTL.setNameLoc(TL.getNameLoc()); | ||||||||||||
5044 | return Result; | ||||||||||||
5045 | } | ||||||||||||
5046 | } IR{*this, PatternRec, NewRec}; | ||||||||||||
5047 | |||||||||||||
5048 | TypeSourceInfo *NewSI = IR.TransformType(Function->getTypeSourceInfo()); | ||||||||||||
5049 | Function->setType(NewSI->getType()); | ||||||||||||
5050 | Function->setTypeSourceInfo(NewSI); | ||||||||||||
5051 | |||||||||||||
5052 | ParmVarDecl *Parm = Function->getParamDecl(0); | ||||||||||||
5053 | TypeSourceInfo *NewParmSI = IR.TransformType(Parm->getTypeSourceInfo()); | ||||||||||||
5054 | Parm->setType(NewParmSI->getType()); | ||||||||||||
5055 | Parm->setTypeSourceInfo(NewParmSI); | ||||||||||||
5056 | }; | ||||||||||||
5057 | |||||||||||||
5058 | if (PatternDecl->isDefaulted()) { | ||||||||||||
5059 | RebuildTypeSourceInfoForDefaultSpecialMembers(); | ||||||||||||
5060 | SetDeclDefaulted(Function, PatternDecl->getLocation()); | ||||||||||||
5061 | } else { | ||||||||||||
5062 | MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs( | ||||||||||||
5063 | Function, /*Final=*/false, nullptr, false, PatternDecl); | ||||||||||||
5064 | |||||||||||||
5065 | // Substitute into the qualifier; we can get a substitution failure here | ||||||||||||
5066 | // through evil use of alias templates. | ||||||||||||
5067 | // FIXME: Is CurContext correct for this? Should we go to the (instantiation | ||||||||||||
5068 | // of the) lexical context of the pattern? | ||||||||||||
5069 | SubstQualifier(*this, PatternDecl, Function, TemplateArgs); | ||||||||||||
5070 | |||||||||||||
5071 | ActOnStartOfFunctionDef(nullptr, Function); | ||||||||||||
5072 | |||||||||||||
5073 | // Enter the scope of this instantiation. We don't use | ||||||||||||
5074 | // PushDeclContext because we don't have a scope. | ||||||||||||
5075 | Sema::ContextRAII savedContext(*this, Function); | ||||||||||||
5076 | |||||||||||||
5077 | if (addInstantiatedParametersToScope(Function, PatternDecl, Scope, | ||||||||||||
5078 | TemplateArgs)) | ||||||||||||
5079 | return; | ||||||||||||
5080 | |||||||||||||
5081 | StmtResult Body; | ||||||||||||
5082 | if (PatternDecl->hasSkippedBody()) { | ||||||||||||
5083 | ActOnSkippedFunctionBody(Function); | ||||||||||||
5084 | Body = nullptr; | ||||||||||||
5085 | } else { | ||||||||||||
5086 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Function)) { | ||||||||||||
5087 | // If this is a constructor, instantiate the member initializers. | ||||||||||||
5088 | InstantiateMemInitializers(Ctor, cast<CXXConstructorDecl>(PatternDecl), | ||||||||||||
5089 | TemplateArgs); | ||||||||||||
5090 | |||||||||||||
5091 | // If this is an MS ABI dllexport default constructor, instantiate any | ||||||||||||
5092 | // default arguments. | ||||||||||||
5093 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && | ||||||||||||
5094 | Ctor->isDefaultConstructor()) { | ||||||||||||
5095 | InstantiateDefaultCtorDefaultArgs(Ctor); | ||||||||||||
5096 | } | ||||||||||||
5097 | } | ||||||||||||
5098 | |||||||||||||
5099 | // Instantiate the function body. | ||||||||||||
5100 | Body = SubstStmt(Pattern, TemplateArgs); | ||||||||||||
5101 | |||||||||||||
5102 | if (Body.isInvalid()) | ||||||||||||
5103 | Function->setInvalidDecl(); | ||||||||||||
5104 | } | ||||||||||||
5105 | // FIXME: finishing the function body while in an expression evaluation | ||||||||||||
5106 | // context seems wrong. Investigate more. | ||||||||||||
5107 | ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true); | ||||||||||||
5108 | |||||||||||||
5109 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); | ||||||||||||
5110 | |||||||||||||
5111 | if (auto *Listener = getASTMutationListener()) | ||||||||||||
5112 | Listener->FunctionDefinitionInstantiated(Function); | ||||||||||||
5113 | |||||||||||||
5114 | savedContext.pop(); | ||||||||||||
5115 | } | ||||||||||||
5116 | |||||||||||||
5117 | DeclGroupRef DG(Function); | ||||||||||||
5118 | Consumer.HandleTopLevelDecl(DG); | ||||||||||||
5119 | |||||||||||||
5120 | // This class may have local implicit instantiations that need to be | ||||||||||||
5121 | // instantiation within this scope. | ||||||||||||
5122 | LocalInstantiations.perform(); | ||||||||||||
5123 | Scope.Exit(); | ||||||||||||
5124 | GlobalInstantiations.perform(); | ||||||||||||
5125 | } | ||||||||||||
5126 | |||||||||||||
5127 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( | ||||||||||||
5128 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, | ||||||||||||
5129 | const TemplateArgumentList &TemplateArgList, | ||||||||||||
5130 | const TemplateArgumentListInfo &TemplateArgsInfo, | ||||||||||||
5131 | SmallVectorImpl<TemplateArgument> &Converted, | ||||||||||||
5132 | SourceLocation PointOfInstantiation, LateInstantiatedAttrVec *LateAttrs, | ||||||||||||
5133 | LocalInstantiationScope *StartingScope) { | ||||||||||||
5134 | if (FromVar->isInvalidDecl()) | ||||||||||||
5135 | return nullptr; | ||||||||||||
5136 | |||||||||||||
5137 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); | ||||||||||||
5138 | if (Inst.isInvalid()) | ||||||||||||
5139 | return nullptr; | ||||||||||||
5140 | |||||||||||||
5141 | // Instantiate the first declaration of the variable template: for a partial | ||||||||||||
5142 | // specialization of a static data member template, the first declaration may | ||||||||||||
5143 | // or may not be the declaration in the class; if it's in the class, we want | ||||||||||||
5144 | // to instantiate a member in the class (a declaration), and if it's outside, | ||||||||||||
5145 | // we want to instantiate a definition. | ||||||||||||
5146 | // | ||||||||||||
5147 | // If we're instantiating an explicitly-specialized member template or member | ||||||||||||
5148 | // partial specialization, don't do this. The member specialization completely | ||||||||||||
5149 | // replaces the original declaration in this case. | ||||||||||||
5150 | bool IsMemberSpec = false; | ||||||||||||
5151 | MultiLevelTemplateArgumentList MultiLevelList; | ||||||||||||
5152 | if (auto *PartialSpec = | ||||||||||||
5153 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) { | ||||||||||||
5154 | IsMemberSpec = PartialSpec->isMemberSpecialization(); | ||||||||||||
5155 | MultiLevelList.addOuterTemplateArguments( | ||||||||||||
5156 | PartialSpec, TemplateArgList.asArray(), /*Final=*/false); | ||||||||||||
5157 | } else { | ||||||||||||
5158 | assert(VarTemplate == FromVar->getDescribedVarTemplate())(static_cast <bool> (VarTemplate == FromVar->getDescribedVarTemplate ()) ? void (0) : __assert_fail ("VarTemplate == FromVar->getDescribedVarTemplate()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5158, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
5159 | IsMemberSpec = VarTemplate->isMemberSpecialization(); | ||||||||||||
5160 | MultiLevelList.addOuterTemplateArguments( | ||||||||||||
5161 | VarTemplate, TemplateArgList.asArray(), /*Final=*/false); | ||||||||||||
5162 | } | ||||||||||||
5163 | if (!IsMemberSpec) | ||||||||||||
5164 | FromVar = FromVar->getFirstDecl(); | ||||||||||||
5165 | |||||||||||||
5166 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), | ||||||||||||
5167 | MultiLevelList); | ||||||||||||
5168 | |||||||||||||
5169 | // TODO: Set LateAttrs and StartingScope ... | ||||||||||||
5170 | |||||||||||||
5171 | return cast_or_null<VarTemplateSpecializationDecl>( | ||||||||||||
5172 | Instantiator.VisitVarTemplateSpecializationDecl( | ||||||||||||
5173 | VarTemplate, FromVar, TemplateArgsInfo, Converted)); | ||||||||||||
5174 | } | ||||||||||||
5175 | |||||||||||||
5176 | /// Instantiates a variable template specialization by completing it | ||||||||||||
5177 | /// with appropriate type information and initializer. | ||||||||||||
5178 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( | ||||||||||||
5179 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, | ||||||||||||
5180 | const MultiLevelTemplateArgumentList &TemplateArgs) { | ||||||||||||
5181 | assert(PatternDecl->isThisDeclarationADefinition() &&(static_cast <bool> (PatternDecl->isThisDeclarationADefinition () && "don't have a definition to instantiate from") ? void (0) : __assert_fail ("PatternDecl->isThisDeclarationADefinition() && \"don't have a definition to instantiate from\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5182, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
5182 | "don't have a definition to instantiate from")(static_cast <bool> (PatternDecl->isThisDeclarationADefinition () && "don't have a definition to instantiate from") ? void (0) : __assert_fail ("PatternDecl->isThisDeclarationADefinition() && \"don't have a definition to instantiate from\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5182, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
5183 | |||||||||||||
5184 | // Do substitution on the type of the declaration | ||||||||||||
5185 | TypeSourceInfo *DI = | ||||||||||||
5186 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, | ||||||||||||
5187 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); | ||||||||||||
5188 | if (!DI) | ||||||||||||
5189 | return nullptr; | ||||||||||||
5190 | |||||||||||||
5191 | // Update the type of this variable template specialization. | ||||||||||||
5192 | VarSpec->setType(DI->getType()); | ||||||||||||
5193 | |||||||||||||
5194 | // Convert the declaration into a definition now. | ||||||||||||
5195 | VarSpec->setCompleteDefinition(); | ||||||||||||
5196 | |||||||||||||
5197 | // Instantiate the initializer. | ||||||||||||
5198 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); | ||||||||||||
5199 | |||||||||||||
5200 | if (getLangOpts().OpenCL) | ||||||||||||
5201 | deduceOpenCLAddressSpace(VarSpec); | ||||||||||||
5202 | |||||||||||||
5203 | return VarSpec; | ||||||||||||
5204 | } | ||||||||||||
5205 | |||||||||||||
5206 | /// BuildVariableInstantiation - Used after a new variable has been created. | ||||||||||||
5207 | /// Sets basic variable data and decides whether to postpone the | ||||||||||||
5208 | /// variable instantiation. | ||||||||||||
5209 | void Sema::BuildVariableInstantiation( | ||||||||||||
5210 | VarDecl *NewVar, VarDecl *OldVar, | ||||||||||||
5211 | const MultiLevelTemplateArgumentList &TemplateArgs, | ||||||||||||
5212 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, | ||||||||||||
5213 | LocalInstantiationScope *StartingScope, | ||||||||||||
5214 | bool InstantiatingVarTemplate, | ||||||||||||
5215 | VarTemplateSpecializationDecl *PrevDeclForVarTemplateSpecialization) { | ||||||||||||
5216 | // Instantiating a partial specialization to produce a partial | ||||||||||||
5217 | // specialization. | ||||||||||||
5218 | bool InstantiatingVarTemplatePartialSpec = | ||||||||||||
5219 | isa<VarTemplatePartialSpecializationDecl>(OldVar) && | ||||||||||||
5220 | isa<VarTemplatePartialSpecializationDecl>(NewVar); | ||||||||||||
5221 | // Instantiating from a variable template (or partial specialization) to | ||||||||||||
5222 | // produce a variable template specialization. | ||||||||||||
5223 | bool InstantiatingSpecFromTemplate = | ||||||||||||
5224 | isa<VarTemplateSpecializationDecl>(NewVar) && | ||||||||||||
5225 | (OldVar->getDescribedVarTemplate() || | ||||||||||||
5226 | isa<VarTemplatePartialSpecializationDecl>(OldVar)); | ||||||||||||
5227 | |||||||||||||
5228 | // If we are instantiating a local extern declaration, the | ||||||||||||
5229 | // instantiation belongs lexically to the containing function. | ||||||||||||
5230 | // If we are instantiating a static data member defined | ||||||||||||
5231 | // out-of-line, the instantiation will have the same lexical | ||||||||||||
5232 | // context (which will be a namespace scope) as the template. | ||||||||||||
5233 | if (OldVar->isLocalExternDecl()) { | ||||||||||||
5234 | NewVar->setLocalExternDecl(); | ||||||||||||
5235 | NewVar->setLexicalDeclContext(Owner); | ||||||||||||
5236 | } else if (OldVar->isOutOfLine()) | ||||||||||||
5237 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); | ||||||||||||
5238 | NewVar->setTSCSpec(OldVar->getTSCSpec()); | ||||||||||||
5239 | NewVar->setInitStyle(OldVar->getInitStyle()); | ||||||||||||
5240 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); | ||||||||||||
5241 | NewVar->setObjCForDecl(OldVar->isObjCForDecl()); | ||||||||||||
5242 | NewVar->setConstexpr(OldVar->isConstexpr()); | ||||||||||||
5243 | NewVar->setInitCapture(OldVar->isInitCapture()); | ||||||||||||
5244 | NewVar->setPreviousDeclInSameBlockScope( | ||||||||||||
5245 | OldVar->isPreviousDeclInSameBlockScope()); | ||||||||||||
5246 | NewVar->setAccess(OldVar->getAccess()); | ||||||||||||
5247 | |||||||||||||
5248 | if (!OldVar->isStaticDataMember()) { | ||||||||||||
5249 | if (OldVar->isUsed(false)) | ||||||||||||
5250 | NewVar->setIsUsed(); | ||||||||||||
5251 | NewVar->setReferenced(OldVar->isReferenced()); | ||||||||||||
5252 | } | ||||||||||||
5253 | |||||||||||||
5254 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); | ||||||||||||
5255 | |||||||||||||
5256 | LookupResult Previous( | ||||||||||||
5257 | *this, NewVar->getDeclName(), NewVar->getLocation(), | ||||||||||||
5258 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage | ||||||||||||
5259 | : Sema::LookupOrdinaryName, | ||||||||||||
5260 | NewVar->isLocalExternDecl() ? Sema::ForExternalRedeclaration | ||||||||||||
5261 | : forRedeclarationInCurContext()); | ||||||||||||
5262 | |||||||||||||
5263 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && | ||||||||||||
5264 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || | ||||||||||||
5265 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { | ||||||||||||
5266 | // We have a previous declaration. Use that one, so we merge with the | ||||||||||||
5267 | // right type. | ||||||||||||
5268 | if (NamedDecl *NewPrev = FindInstantiatedDecl( | ||||||||||||
5269 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) | ||||||||||||
5270 | Previous.addDecl(NewPrev); | ||||||||||||
5271 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && | ||||||||||||
5272 | OldVar->hasLinkage()) { | ||||||||||||
5273 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); | ||||||||||||
5274 | } else if (PrevDeclForVarTemplateSpecialization) { | ||||||||||||
5275 | Previous.addDecl(PrevDeclForVarTemplateSpecialization); | ||||||||||||
5276 | } | ||||||||||||
5277 | CheckVariableDeclaration(NewVar, Previous); | ||||||||||||
5278 | |||||||||||||
5279 | if (!InstantiatingVarTemplate) { | ||||||||||||
5280 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); | ||||||||||||
5281 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) | ||||||||||||
5282 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); | ||||||||||||
5283 | } | ||||||||||||
5284 | |||||||||||||
5285 | if (!OldVar->isOutOfLine()) { | ||||||||||||
5286 | if (NewVar->getDeclContext()->isFunctionOrMethod()) | ||||||||||||
5287 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); | ||||||||||||
5288 | } | ||||||||||||
5289 | |||||||||||||
5290 | // Link instantiations of static data members back to the template from | ||||||||||||
5291 | // which they were instantiated. | ||||||||||||
5292 | // | ||||||||||||
5293 | // Don't do this when instantiating a template (we link the template itself | ||||||||||||
5294 | // back in that case) nor when instantiating a static data member template | ||||||||||||
5295 | // (that's not a member specialization). | ||||||||||||
5296 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate && | ||||||||||||
5297 | !InstantiatingSpecFromTemplate) | ||||||||||||
5298 | NewVar->setInstantiationOfStaticDataMember(OldVar, | ||||||||||||
5299 | TSK_ImplicitInstantiation); | ||||||||||||
5300 | |||||||||||||
5301 | // If the pattern is an (in-class) explicit specialization, then the result | ||||||||||||
5302 | // is also an explicit specialization. | ||||||||||||
5303 | if (VarTemplateSpecializationDecl *OldVTSD = | ||||||||||||
5304 | dyn_cast<VarTemplateSpecializationDecl>(OldVar)) { | ||||||||||||
5305 | if (OldVTSD->getSpecializationKind() == TSK_ExplicitSpecialization && | ||||||||||||
5306 | !isa<VarTemplatePartialSpecializationDecl>(OldVTSD)) | ||||||||||||
5307 | cast<VarTemplateSpecializationDecl>(NewVar)->setSpecializationKind( | ||||||||||||
5308 | TSK_ExplicitSpecialization); | ||||||||||||
5309 | } | ||||||||||||
5310 | |||||||||||||
5311 | // Forward the mangling number from the template to the instantiated decl. | ||||||||||||
5312 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); | ||||||||||||
5313 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); | ||||||||||||
5314 | |||||||||||||
5315 | // Figure out whether to eagerly instantiate the initializer. | ||||||||||||
5316 | if (InstantiatingVarTemplate || InstantiatingVarTemplatePartialSpec) { | ||||||||||||
5317 | // We're producing a template. Don't instantiate the initializer yet. | ||||||||||||
5318 | } else if (NewVar->getType()->isUndeducedType()) { | ||||||||||||
5319 | // We need the type to complete the declaration of the variable. | ||||||||||||
5320 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); | ||||||||||||
5321 | } else if (InstantiatingSpecFromTemplate || | ||||||||||||
5322 | (OldVar->isInline() && OldVar->isThisDeclarationADefinition() && | ||||||||||||
5323 | !NewVar->isThisDeclarationADefinition())) { | ||||||||||||
5324 | // Delay instantiation of the initializer for variable template | ||||||||||||
5325 | // specializations or inline static data members until a definition of the | ||||||||||||
5326 | // variable is needed. | ||||||||||||
5327 | } else { | ||||||||||||
5328 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); | ||||||||||||
5329 | } | ||||||||||||
5330 | |||||||||||||
5331 | // Diagnose unused local variables with dependent types, where the diagnostic | ||||||||||||
5332 | // will have been deferred. | ||||||||||||
5333 | if (!NewVar->isInvalidDecl() && | ||||||||||||
5334 | NewVar->getDeclContext()->isFunctionOrMethod() && | ||||||||||||
5335 | OldVar->getType()->isDependentType()) | ||||||||||||
5336 | DiagnoseUnusedDecl(NewVar); | ||||||||||||
5337 | } | ||||||||||||
5338 | |||||||||||||
5339 | /// Instantiate the initializer of a variable. | ||||||||||||
5340 | void Sema::InstantiateVariableInitializer( | ||||||||||||
5341 | VarDecl *Var, VarDecl *OldVar, | ||||||||||||
5342 | const MultiLevelTemplateArgumentList &TemplateArgs) { | ||||||||||||
5343 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) | ||||||||||||
5344 | L->VariableDefinitionInstantiated(Var); | ||||||||||||
5345 | |||||||||||||
5346 | // We propagate the 'inline' flag with the initializer, because it | ||||||||||||
5347 | // would otherwise imply that the variable is a definition for a | ||||||||||||
5348 | // non-static data member. | ||||||||||||
5349 | if (OldVar->isInlineSpecified()) | ||||||||||||
5350 | Var->setInlineSpecified(); | ||||||||||||
5351 | else if (OldVar->isInline()) | ||||||||||||
5352 | Var->setImplicitlyInline(); | ||||||||||||
5353 | |||||||||||||
5354 | if (OldVar->getInit()) { | ||||||||||||
5355 | EnterExpressionEvaluationContext Evaluated( | ||||||||||||
5356 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var); | ||||||||||||
5357 | |||||||||||||
5358 | // Instantiate the initializer. | ||||||||||||
5359 | ExprResult Init; | ||||||||||||
5360 | |||||||||||||
5361 | { | ||||||||||||
5362 | ContextRAII SwitchContext(*this, Var->getDeclContext()); | ||||||||||||
5363 | Init = SubstInitializer(OldVar->getInit(), TemplateArgs, | ||||||||||||
5364 | OldVar->getInitStyle() == VarDecl::CallInit); | ||||||||||||
5365 | } | ||||||||||||
5366 | |||||||||||||
5367 | if (!Init.isInvalid()) { | ||||||||||||
5368 | Expr *InitExpr = Init.get(); | ||||||||||||
5369 | |||||||||||||
5370 | if (Var->hasAttr<DLLImportAttr>() && | ||||||||||||
5371 | (!InitExpr || | ||||||||||||
5372 | !InitExpr->isConstantInitializer(getASTContext(), false))) { | ||||||||||||
5373 | // Do not dynamically initialize dllimport variables. | ||||||||||||
5374 | } else if (InitExpr) { | ||||||||||||
5375 | bool DirectInit = OldVar->isDirectInit(); | ||||||||||||
5376 | AddInitializerToDecl(Var, InitExpr, DirectInit); | ||||||||||||
5377 | } else | ||||||||||||
5378 | ActOnUninitializedDecl(Var); | ||||||||||||
5379 | } else { | ||||||||||||
5380 | // FIXME: Not too happy about invalidating the declaration | ||||||||||||
5381 | // because of a bogus initializer. | ||||||||||||
5382 | Var->setInvalidDecl(); | ||||||||||||
5383 | } | ||||||||||||
5384 | } else { | ||||||||||||
5385 | // `inline` variables are a definition and declaration all in one; we won't | ||||||||||||
5386 | // pick up an initializer from anywhere else. | ||||||||||||
5387 | if (Var->isStaticDataMember() && !Var->isInline()) { | ||||||||||||
5388 | if (!Var->isOutOfLine()) | ||||||||||||
5389 | return; | ||||||||||||
5390 | |||||||||||||
5391 | // If the declaration inside the class had an initializer, don't add | ||||||||||||
5392 | // another one to the out-of-line definition. | ||||||||||||
5393 | if (OldVar->getFirstDecl()->hasInit()) | ||||||||||||
5394 | return; | ||||||||||||
5395 | } | ||||||||||||
5396 | |||||||||||||
5397 | // We'll add an initializer to a for-range declaration later. | ||||||||||||
5398 | if (Var->isCXXForRangeDecl() || Var->isObjCForDecl()) | ||||||||||||
5399 | return; | ||||||||||||
5400 | |||||||||||||
5401 | ActOnUninitializedDecl(Var); | ||||||||||||
5402 | } | ||||||||||||
5403 | |||||||||||||
5404 | if (getLangOpts().CUDA) | ||||||||||||
5405 | checkAllowedCUDAInitializer(Var); | ||||||||||||
5406 | } | ||||||||||||
5407 | |||||||||||||
5408 | /// Instantiate the definition of the given variable from its | ||||||||||||
5409 | /// template. | ||||||||||||
5410 | /// | ||||||||||||
5411 | /// \param PointOfInstantiation the point at which the instantiation was | ||||||||||||
5412 | /// required. Note that this is not precisely a "point of instantiation" | ||||||||||||
5413 | /// for the variable, but it's close. | ||||||||||||
5414 | /// | ||||||||||||
5415 | /// \param Var the already-instantiated declaration of a templated variable. | ||||||||||||
5416 | /// | ||||||||||||
5417 | /// \param Recursive if true, recursively instantiates any functions that | ||||||||||||
5418 | /// are required by this instantiation. | ||||||||||||
5419 | /// | ||||||||||||
5420 | /// \param DefinitionRequired if true, then we are performing an explicit | ||||||||||||
5421 | /// instantiation where a definition of the variable is required. Complain | ||||||||||||
5422 | /// if there is no such definition. | ||||||||||||
5423 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, | ||||||||||||
5424 | VarDecl *Var, bool Recursive, | ||||||||||||
5425 | bool DefinitionRequired, bool AtEndOfTU) { | ||||||||||||
5426 | if (Var->isInvalidDecl()) | ||||||||||||
5427 | return; | ||||||||||||
5428 | |||||||||||||
5429 | // Never instantiate an explicitly-specialized entity. | ||||||||||||
5430 | TemplateSpecializationKind TSK = | ||||||||||||
5431 | Var->getTemplateSpecializationKindForInstantiation(); | ||||||||||||
5432 | if (TSK == TSK_ExplicitSpecialization) | ||||||||||||
5433 | return; | ||||||||||||
5434 | |||||||||||||
5435 | // Find the pattern and the arguments to substitute into it. | ||||||||||||
5436 | VarDecl *PatternDecl = Var->getTemplateInstantiationPattern(); | ||||||||||||
5437 | assert(PatternDecl && "no pattern for templated variable")(static_cast <bool> (PatternDecl && "no pattern for templated variable" ) ? void (0) : __assert_fail ("PatternDecl && \"no pattern for templated variable\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5437, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
5438 | MultiLevelTemplateArgumentList TemplateArgs = | ||||||||||||
5439 | getTemplateInstantiationArgs(Var); | ||||||||||||
5440 | |||||||||||||
5441 | VarTemplateSpecializationDecl *VarSpec = | ||||||||||||
5442 | dyn_cast<VarTemplateSpecializationDecl>(Var); | ||||||||||||
5443 | if (VarSpec) { | ||||||||||||
5444 | // If this is a static data member template, there might be an | ||||||||||||
5445 | // uninstantiated initializer on the declaration. If so, instantiate | ||||||||||||
5446 | // it now. | ||||||||||||
5447 | // | ||||||||||||
5448 | // FIXME: This largely duplicates what we would do below. The difference | ||||||||||||
5449 | // is that along this path we may instantiate an initializer from an | ||||||||||||
5450 | // in-class declaration of the template and instantiate the definition | ||||||||||||
5451 | // from a separate out-of-class definition. | ||||||||||||
5452 | if (PatternDecl->isStaticDataMember() && | ||||||||||||
5453 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && | ||||||||||||
5454 | !Var->hasInit()) { | ||||||||||||
5455 | // FIXME: Factor out the duplicated instantiation context setup/tear down | ||||||||||||
5456 | // code here. | ||||||||||||
5457 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); | ||||||||||||
5458 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) | ||||||||||||
5459 | return; | ||||||||||||
5460 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), | ||||||||||||
5461 | "instantiating variable initializer"); | ||||||||||||
5462 | |||||||||||||
5463 | // The instantiation is visible here, even if it was first declared in an | ||||||||||||
5464 | // unimported module. | ||||||||||||
5465 | Var->setVisibleDespiteOwningModule(); | ||||||||||||
5466 | |||||||||||||
5467 | // If we're performing recursive template instantiation, create our own | ||||||||||||
5468 | // queue of pending implicit instantiations that we will instantiate | ||||||||||||
5469 | // later, while we're still within our own instantiation context. | ||||||||||||
5470 | GlobalEagerInstantiationScope GlobalInstantiations(*this, | ||||||||||||
5471 | /*Enabled=*/Recursive); | ||||||||||||
5472 | LocalInstantiationScope Local(*this); | ||||||||||||
5473 | LocalEagerInstantiationScope LocalInstantiations(*this); | ||||||||||||
5474 | |||||||||||||
5475 | // Enter the scope of this instantiation. We don't use | ||||||||||||
5476 | // PushDeclContext because we don't have a scope. | ||||||||||||
5477 | ContextRAII PreviousContext(*this, Var->getDeclContext()); | ||||||||||||
5478 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); | ||||||||||||
5479 | PreviousContext.pop(); | ||||||||||||
5480 | |||||||||||||
5481 | // This variable may have local implicit instantiations that need to be | ||||||||||||
5482 | // instantiated within this scope. | ||||||||||||
5483 | LocalInstantiations.perform(); | ||||||||||||
5484 | Local.Exit(); | ||||||||||||
5485 | GlobalInstantiations.perform(); | ||||||||||||
5486 | } | ||||||||||||
5487 | } else { | ||||||||||||
5488 | assert(Var->isStaticDataMember() && PatternDecl->isStaticDataMember() &&(static_cast <bool> (Var->isStaticDataMember() && PatternDecl->isStaticDataMember() && "not a static data member?" ) ? void (0) : __assert_fail ("Var->isStaticDataMember() && PatternDecl->isStaticDataMember() && \"not a static data member?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5489, __extension__ __PRETTY_FUNCTION__)) | ||||||||||||
5489 | "not a static data member?")(static_cast <bool> (Var->isStaticDataMember() && PatternDecl->isStaticDataMember() && "not a static data member?" ) ? void (0) : __assert_fail ("Var->isStaticDataMember() && PatternDecl->isStaticDataMember() && \"not a static data member?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5489, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
5490 | } | ||||||||||||
5491 | |||||||||||||
5492 | VarDecl *Def = PatternDecl->getDefinition(getASTContext()); | ||||||||||||
5493 | |||||||||||||
5494 | // If we don't have a definition of the variable template, we won't perform | ||||||||||||
5495 | // any instantiation. Rather, we rely on the user to instantiate this | ||||||||||||
5496 | // definition (or provide a specialization for it) in another translation | ||||||||||||
5497 | // unit. | ||||||||||||
5498 | if (!Def && !DefinitionRequired) { | ||||||||||||
5499 | if (TSK == TSK_ExplicitInstantiationDefinition) { | ||||||||||||
5500 | PendingInstantiations.push_back( | ||||||||||||
5501 | std::make_pair(Var, PointOfInstantiation)); | ||||||||||||
5502 | } else if (TSK == TSK_ImplicitInstantiation) { | ||||||||||||
5503 | // Warn about missing definition at the end of translation unit. | ||||||||||||
5504 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && | ||||||||||||
5505 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { | ||||||||||||
5506 | Diag(PointOfInstantiation, diag::warn_var_template_missing) | ||||||||||||
5507 | << Var; | ||||||||||||
5508 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); | ||||||||||||
5509 | if (getLangOpts().CPlusPlus11) | ||||||||||||
5510 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) << Var; | ||||||||||||
5511 | } | ||||||||||||
5512 | return; | ||||||||||||
5513 | } | ||||||||||||
5514 | } | ||||||||||||
5515 | |||||||||||||
5516 | // FIXME: We need to track the instantiation stack in order to know which | ||||||||||||
5517 | // definitions should be visible within this instantiation. | ||||||||||||
5518 | // FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember(). | ||||||||||||
5519 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Var, | ||||||||||||
5520 | /*InstantiatedFromMember*/false, | ||||||||||||
5521 | PatternDecl, Def, TSK, | ||||||||||||
5522 | /*Complain*/DefinitionRequired)) | ||||||||||||
5523 | return; | ||||||||||||
5524 | |||||||||||||
5525 | // C++11 [temp.explicit]p10: | ||||||||||||
5526 | // Except for inline functions, const variables of literal types, variables | ||||||||||||
5527 | // of reference types, [...] explicit instantiation declarations | ||||||||||||
5528 | // have the effect of suppressing the implicit instantiation of the entity | ||||||||||||
5529 | // to which they refer. | ||||||||||||
5530 | // | ||||||||||||
5531 | // FIXME: That's not exactly the same as "might be usable in constant | ||||||||||||
5532 | // expressions", which only allows constexpr variables and const integral | ||||||||||||
5533 | // types, not arbitrary const literal types. | ||||||||||||
5534 | if (TSK == TSK_ExplicitInstantiationDeclaration && | ||||||||||||
5535 | !Var->mightBeUsableInConstantExpressions(getASTContext())) | ||||||||||||
5536 | return; | ||||||||||||
5537 | |||||||||||||
5538 | // Make sure to pass the instantiated variable to the consumer at the end. | ||||||||||||
5539 | struct PassToConsumerRAII { | ||||||||||||
5540 | ASTConsumer &Consumer; | ||||||||||||
5541 | VarDecl *Var; | ||||||||||||
5542 | |||||||||||||
5543 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) | ||||||||||||
5544 | : Consumer(Consumer), Var(Var) { } | ||||||||||||
5545 | |||||||||||||
5546 | ~PassToConsumerRAII() { | ||||||||||||
5547 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); | ||||||||||||
5548 | } | ||||||||||||
5549 | } PassToConsumerRAII(Consumer, Var); | ||||||||||||
5550 | |||||||||||||
5551 | // If we already have a definition, we're done. | ||||||||||||
5552 | if (VarDecl *Def = Var->getDefinition()) { | ||||||||||||
5553 | // We may be explicitly instantiating something we've already implicitly | ||||||||||||
5554 | // instantiated. | ||||||||||||
5555 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), | ||||||||||||
5556 | PointOfInstantiation); | ||||||||||||
5557 | return; | ||||||||||||
5558 | } | ||||||||||||
5559 | |||||||||||||
5560 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); | ||||||||||||
5561 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) | ||||||||||||
5562 | return; | ||||||||||||
5563 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), | ||||||||||||
5564 | "instantiating variable definition"); | ||||||||||||
5565 | |||||||||||||
5566 | // If we're performing recursive template instantiation, create our own | ||||||||||||
5567 | // queue of pending implicit instantiations that we will instantiate later, | ||||||||||||
5568 | // while we're still within our own instantiation context. | ||||||||||||
5569 | GlobalEagerInstantiationScope GlobalInstantiations(*this, | ||||||||||||
5570 | /*Enabled=*/Recursive); | ||||||||||||
5571 | |||||||||||||
5572 | // Enter the scope of this instantiation. We don't use | ||||||||||||
5573 | // PushDeclContext because we don't have a scope. | ||||||||||||
5574 | ContextRAII PreviousContext(*this, Var->getDeclContext()); | ||||||||||||
5575 | LocalInstantiationScope Local(*this); | ||||||||||||
5576 | |||||||||||||
5577 | LocalEagerInstantiationScope LocalInstantiations(*this); | ||||||||||||
5578 | |||||||||||||
5579 | VarDecl *OldVar = Var; | ||||||||||||
5580 | if (Def->isStaticDataMember() && !Def->isOutOfLine()) { | ||||||||||||
5581 | // We're instantiating an inline static data member whose definition was | ||||||||||||
5582 | // provided inside the class. | ||||||||||||
5583 | InstantiateVariableInitializer(Var, Def, TemplateArgs); | ||||||||||||
5584 | } else if (!VarSpec) { | ||||||||||||
5585 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), | ||||||||||||
5586 | TemplateArgs)); | ||||||||||||
5587 | } else if (Var->isStaticDataMember() && | ||||||||||||
5588 | Var->getLexicalDeclContext()->isRecord()) { | ||||||||||||
5589 | // We need to instantiate the definition of a static data member template, | ||||||||||||
5590 | // and all we have is the in-class declaration of it. Instantiate a separate | ||||||||||||
5591 | // declaration of the definition. | ||||||||||||
5592 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), | ||||||||||||
5593 | TemplateArgs); | ||||||||||||
5594 | |||||||||||||
5595 | TemplateArgumentListInfo TemplateArgInfo; | ||||||||||||
5596 | if (const ASTTemplateArgumentListInfo *ArgInfo = | ||||||||||||
5597 | VarSpec->getTemplateArgsInfo()) { | ||||||||||||
5598 | TemplateArgInfo.setLAngleLoc(ArgInfo->getLAngleLoc()); | ||||||||||||
5599 | TemplateArgInfo.setRAngleLoc(ArgInfo->getRAngleLoc()); | ||||||||||||
5600 | for (const TemplateArgumentLoc &Arg : ArgInfo->arguments()) | ||||||||||||
5601 | TemplateArgInfo.addArgument(Arg); | ||||||||||||
5602 | } | ||||||||||||
5603 | |||||||||||||
5604 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( | ||||||||||||
5605 | VarSpec->getSpecializedTemplate(), Def, TemplateArgInfo, | ||||||||||||
5606 | VarSpec->getTemplateArgs().asArray(), VarSpec)); | ||||||||||||
5607 | if (Var) { | ||||||||||||
5608 | llvm::PointerUnion<VarTemplateDecl *, | ||||||||||||
5609 | VarTemplatePartialSpecializationDecl *> PatternPtr = | ||||||||||||
5610 | VarSpec->getSpecializedTemplateOrPartial(); | ||||||||||||
5611 | if (VarTemplatePartialSpecializationDecl *Partial = | ||||||||||||
5612 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) | ||||||||||||
5613 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( | ||||||||||||
5614 | Partial, &VarSpec->getTemplateInstantiationArgs()); | ||||||||||||
5615 | |||||||||||||
5616 | // Attach the initializer. | ||||||||||||
5617 | InstantiateVariableInitializer(Var, Def, TemplateArgs); | ||||||||||||
5618 | } | ||||||||||||
5619 | } else | ||||||||||||
5620 | // Complete the existing variable's definition with an appropriately | ||||||||||||
5621 | // substituted type and initializer. | ||||||||||||
5622 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); | ||||||||||||
5623 | |||||||||||||
5624 | PreviousContext.pop(); | ||||||||||||
5625 | |||||||||||||
5626 | if (Var) { | ||||||||||||
5627 | PassToConsumerRAII.Var = Var; | ||||||||||||
5628 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), | ||||||||||||
5629 | OldVar->getPointOfInstantiation()); | ||||||||||||
5630 | } | ||||||||||||
5631 | |||||||||||||
5632 | // This variable may have local implicit instantiations that need to be | ||||||||||||
5633 | // instantiated within this scope. | ||||||||||||
5634 | LocalInstantiations.perform(); | ||||||||||||
5635 | Local.Exit(); | ||||||||||||
5636 | GlobalInstantiations.perform(); | ||||||||||||
5637 | } | ||||||||||||
5638 | |||||||||||||
5639 | void | ||||||||||||
5640 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, | ||||||||||||
5641 | const CXXConstructorDecl *Tmpl, | ||||||||||||
5642 | const MultiLevelTemplateArgumentList &TemplateArgs) { | ||||||||||||
5643 | |||||||||||||
5644 | SmallVector<CXXCtorInitializer*, 4> NewInits; | ||||||||||||
5645 | bool AnyErrors = Tmpl->isInvalidDecl(); | ||||||||||||
5646 | |||||||||||||
5647 | // Instantiate all the initializers. | ||||||||||||
5648 | for (const auto *Init : Tmpl->inits()) { | ||||||||||||
5649 | // Only instantiate written initializers, let Sema re-construct implicit | ||||||||||||
5650 | // ones. | ||||||||||||
5651 | if (!Init->isWritten()) | ||||||||||||
5652 | continue; | ||||||||||||
5653 | |||||||||||||
5654 | SourceLocation EllipsisLoc; | ||||||||||||
5655 | |||||||||||||
5656 | if (Init->isPackExpansion()) { | ||||||||||||
5657 | // This is a pack expansion. We should expand it now. | ||||||||||||
5658 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); | ||||||||||||
5659 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; | ||||||||||||
5660 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); | ||||||||||||
5661 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); | ||||||||||||
5662 | bool ShouldExpand = false; | ||||||||||||
5663 | bool RetainExpansion = false; | ||||||||||||
5664 | std::optional<unsigned> NumExpansions; | ||||||||||||
5665 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), | ||||||||||||
5666 | BaseTL.getSourceRange(), | ||||||||||||
5667 | Unexpanded, | ||||||||||||
5668 | TemplateArgs, ShouldExpand, | ||||||||||||
5669 | RetainExpansion, | ||||||||||||
5670 | NumExpansions)) { | ||||||||||||
5671 | AnyErrors = true; | ||||||||||||
5672 | New->setInvalidDecl(); | ||||||||||||
5673 | continue; | ||||||||||||
5674 | } | ||||||||||||
5675 | assert(ShouldExpand && "Partial instantiation of base initializer?")(static_cast <bool> (ShouldExpand && "Partial instantiation of base initializer?" ) ? void (0) : __assert_fail ("ShouldExpand && \"Partial instantiation of base initializer?\"" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5675, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
5676 | |||||||||||||
5677 | // Loop over all of the arguments in the argument pack(s), | ||||||||||||
5678 | for (unsigned I = 0; I != *NumExpansions; ++I) { | ||||||||||||
5679 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); | ||||||||||||
5680 | |||||||||||||
5681 | // Instantiate the initializer. | ||||||||||||
5682 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, | ||||||||||||
5683 | /*CXXDirectInit=*/true); | ||||||||||||
5684 | if (TempInit.isInvalid()) { | ||||||||||||
5685 | AnyErrors = true; | ||||||||||||
5686 | break; | ||||||||||||
5687 | } | ||||||||||||
5688 | |||||||||||||
5689 | // Instantiate the base type. | ||||||||||||
5690 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), | ||||||||||||
5691 | TemplateArgs, | ||||||||||||
5692 | Init->getSourceLocation(), | ||||||||||||
5693 | New->getDeclName()); | ||||||||||||
5694 | if (!BaseTInfo) { | ||||||||||||
5695 | AnyErrors = true; | ||||||||||||
5696 | break; | ||||||||||||
5697 | } | ||||||||||||
5698 | |||||||||||||
5699 | // Build the initializer. | ||||||||||||
5700 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), | ||||||||||||
5701 | BaseTInfo, TempInit.get(), | ||||||||||||
5702 | New->getParent(), | ||||||||||||
5703 | SourceLocation()); | ||||||||||||
5704 | if (NewInit.isInvalid()) { | ||||||||||||
5705 | AnyErrors = true; | ||||||||||||
5706 | break; | ||||||||||||
5707 | } | ||||||||||||
5708 | |||||||||||||
5709 | NewInits.push_back(NewInit.get()); | ||||||||||||
5710 | } | ||||||||||||
5711 | |||||||||||||
5712 | continue; | ||||||||||||
5713 | } | ||||||||||||
5714 | |||||||||||||
5715 | // Instantiate the initializer. | ||||||||||||
5716 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, | ||||||||||||
5717 | /*CXXDirectInit=*/true); | ||||||||||||
5718 | if (TempInit.isInvalid()) { | ||||||||||||
5719 | AnyErrors = true; | ||||||||||||
5720 | continue; | ||||||||||||
5721 | } | ||||||||||||
5722 | |||||||||||||
5723 | MemInitResult NewInit; | ||||||||||||
5724 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { | ||||||||||||
5725 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), | ||||||||||||
5726 | TemplateArgs, | ||||||||||||
5727 | Init->getSourceLocation(), | ||||||||||||
5728 | New->getDeclName()); | ||||||||||||
5729 | if (!TInfo) { | ||||||||||||
5730 | AnyErrors = true; | ||||||||||||
5731 | New->setInvalidDecl(); | ||||||||||||
5732 | continue; | ||||||||||||
5733 | } | ||||||||||||
5734 | |||||||||||||
5735 | if (Init->isBaseInitializer()) | ||||||||||||
5736 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), | ||||||||||||
5737 | New->getParent(), EllipsisLoc); | ||||||||||||
5738 | else | ||||||||||||
5739 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), | ||||||||||||
5740 | cast<CXXRecordDecl>(CurContext->getParent())); | ||||||||||||
5741 | } else if (Init->isMemberInitializer()) { | ||||||||||||
5742 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( | ||||||||||||
5743 | Init->getMemberLocation(), | ||||||||||||
5744 | Init->getMember(), | ||||||||||||
5745 | TemplateArgs)); | ||||||||||||
5746 | if (!Member) { | ||||||||||||
5747 | AnyErrors = true; | ||||||||||||
5748 | New->setInvalidDecl(); | ||||||||||||
5749 | continue; | ||||||||||||
5750 | } | ||||||||||||
5751 | |||||||||||||
5752 | NewInit = BuildMemberInitializer(Member, TempInit.get(), | ||||||||||||
5753 | Init->getSourceLocation()); | ||||||||||||
5754 | } else if (Init->isIndirectMemberInitializer()) { | ||||||||||||
5755 | IndirectFieldDecl *IndirectMember = | ||||||||||||
5756 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( | ||||||||||||
5757 | Init->getMemberLocation(), | ||||||||||||
5758 | Init->getIndirectMember(), TemplateArgs)); | ||||||||||||
5759 | |||||||||||||
5760 | if (!IndirectMember) { | ||||||||||||
5761 | AnyErrors = true; | ||||||||||||
5762 | New->setInvalidDecl(); | ||||||||||||
5763 | continue; | ||||||||||||
5764 | } | ||||||||||||
5765 | |||||||||||||
5766 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), | ||||||||||||
5767 | Init->getSourceLocation()); | ||||||||||||
5768 | } | ||||||||||||
5769 | |||||||||||||
5770 | if (NewInit.isInvalid()) { | ||||||||||||
5771 | AnyErrors = true; | ||||||||||||
5772 | New->setInvalidDecl(); | ||||||||||||
5773 | } else { | ||||||||||||
5774 | NewInits.push_back(NewInit.get()); | ||||||||||||
5775 | } | ||||||||||||
5776 | } | ||||||||||||
5777 | |||||||||||||
5778 | // Assign all the initializers to the new constructor. | ||||||||||||
5779 | ActOnMemInitializers(New, | ||||||||||||
5780 | /*FIXME: ColonLoc */ | ||||||||||||
5781 | SourceLocation(), | ||||||||||||
5782 | NewInits, | ||||||||||||
5783 | AnyErrors); | ||||||||||||
5784 | } | ||||||||||||
5785 | |||||||||||||
5786 | // TODO: this could be templated if the various decl types used the | ||||||||||||
5787 | // same method name. | ||||||||||||
5788 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, | ||||||||||||
5789 | ClassTemplateDecl *Instance) { | ||||||||||||
5790 | Pattern = Pattern->getCanonicalDecl(); | ||||||||||||
5791 | |||||||||||||
5792 | do { | ||||||||||||
5793 | Instance = Instance->getCanonicalDecl(); | ||||||||||||
5794 | if (Pattern == Instance) return true; | ||||||||||||
5795 | Instance = Instance->getInstantiatedFromMemberTemplate(); | ||||||||||||
5796 | } while (Instance); | ||||||||||||
5797 | |||||||||||||
5798 | return false; | ||||||||||||
5799 | } | ||||||||||||
5800 | |||||||||||||
5801 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, | ||||||||||||
5802 | FunctionTemplateDecl *Instance) { | ||||||||||||
5803 | Pattern = Pattern->getCanonicalDecl(); | ||||||||||||
5804 | |||||||||||||
5805 | do { | ||||||||||||
5806 | Instance = Instance->getCanonicalDecl(); | ||||||||||||
5807 | if (Pattern == Instance) return true; | ||||||||||||
5808 | Instance = Instance->getInstantiatedFromMemberTemplate(); | ||||||||||||
5809 | } while (Instance); | ||||||||||||
5810 | |||||||||||||
5811 | return false; | ||||||||||||
5812 | } | ||||||||||||
5813 | |||||||||||||
5814 | static bool | ||||||||||||
5815 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, | ||||||||||||
5816 | ClassTemplatePartialSpecializationDecl *Instance) { | ||||||||||||
5817 | Pattern | ||||||||||||
5818 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); | ||||||||||||
5819 | do { | ||||||||||||
5820 | Instance = cast<ClassTemplatePartialSpecializationDecl>( | ||||||||||||
5821 | Instance->getCanonicalDecl()); | ||||||||||||
5822 | if (Pattern == Instance) | ||||||||||||
5823 | return true; | ||||||||||||
5824 | Instance = Instance->getInstantiatedFromMember(); | ||||||||||||
5825 | } while (Instance); | ||||||||||||
5826 | |||||||||||||
5827 | return false; | ||||||||||||
5828 | } | ||||||||||||
5829 | |||||||||||||
5830 | static bool isInstantiationOf(CXXRecordDecl *Pattern, | ||||||||||||
5831 | CXXRecordDecl *Instance) { | ||||||||||||
5832 | Pattern = Pattern->getCanonicalDecl(); | ||||||||||||
5833 | |||||||||||||
5834 | do { | ||||||||||||
5835 | Instance = Instance->getCanonicalDecl(); | ||||||||||||
5836 | if (Pattern == Instance) return true; | ||||||||||||
5837 | Instance = Instance->getInstantiatedFromMemberClass(); | ||||||||||||
5838 | } while (Instance); | ||||||||||||
5839 | |||||||||||||
5840 | return false; | ||||||||||||
5841 | } | ||||||||||||
5842 | |||||||||||||
5843 | static bool isInstantiationOf(FunctionDecl *Pattern, | ||||||||||||
5844 | FunctionDecl *Instance) { | ||||||||||||
5845 | Pattern = Pattern->getCanonicalDecl(); | ||||||||||||
5846 | |||||||||||||
5847 | do { | ||||||||||||
5848 | Instance = Instance->getCanonicalDecl(); | ||||||||||||
5849 | if (Pattern == Instance) return true; | ||||||||||||
5850 | Instance = Instance->getInstantiatedFromMemberFunction(); | ||||||||||||
5851 | } while (Instance); | ||||||||||||
5852 | |||||||||||||
5853 | return false; | ||||||||||||
5854 | } | ||||||||||||
5855 | |||||||||||||
5856 | static bool isInstantiationOf(EnumDecl *Pattern, | ||||||||||||
5857 | EnumDecl *Instance) { | ||||||||||||
5858 | Pattern = Pattern->getCanonicalDecl(); | ||||||||||||
5859 | |||||||||||||
5860 | do { | ||||||||||||
5861 | Instance = Instance->getCanonicalDecl(); | ||||||||||||
5862 | if (Pattern == Instance) return true; | ||||||||||||
5863 | Instance = Instance->getInstantiatedFromMemberEnum(); | ||||||||||||
5864 | } while (Instance); | ||||||||||||
5865 | |||||||||||||
5866 | return false; | ||||||||||||
5867 | } | ||||||||||||
5868 | |||||||||||||
5869 | static bool isInstantiationOf(UsingShadowDecl *Pattern, | ||||||||||||
5870 | UsingShadowDecl *Instance, | ||||||||||||
5871 | ASTContext &C) { | ||||||||||||
5872 | return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), | ||||||||||||
5873 | Pattern); | ||||||||||||
5874 | } | ||||||||||||
5875 | |||||||||||||
5876 | static bool isInstantiationOf(UsingDecl *Pattern, UsingDecl *Instance, | ||||||||||||
5877 | ASTContext &C) { | ||||||||||||
5878 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); | ||||||||||||
5879 | } | ||||||||||||
5880 | |||||||||||||
5881 | template<typename T> | ||||||||||||
5882 | static bool isInstantiationOfUnresolvedUsingDecl(T *Pattern, Decl *Other, | ||||||||||||
5883 | ASTContext &Ctx) { | ||||||||||||
5884 | // An unresolved using declaration can instantiate to an unresolved using | ||||||||||||
5885 | // declaration, or to a using declaration or a using declaration pack. | ||||||||||||
5886 | // | ||||||||||||
5887 | // Multiple declarations can claim to be instantiated from an unresolved | ||||||||||||
5888 | // using declaration if it's a pack expansion. We want the UsingPackDecl | ||||||||||||
5889 | // in that case, not the individual UsingDecls within the pack. | ||||||||||||
5890 | bool OtherIsPackExpansion; | ||||||||||||
5891 | NamedDecl *OtherFrom; | ||||||||||||
5892 | if (auto *OtherUUD = dyn_cast<T>(Other)) { | ||||||||||||
5893 | OtherIsPackExpansion = OtherUUD->isPackExpansion(); | ||||||||||||
5894 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUUD); | ||||||||||||
5895 | } else if (auto *OtherUPD = dyn_cast<UsingPackDecl>(Other)) { | ||||||||||||
5896 | OtherIsPackExpansion = true; | ||||||||||||
5897 | OtherFrom = OtherUPD->getInstantiatedFromUsingDecl(); | ||||||||||||
5898 | } else if (auto *OtherUD = dyn_cast<UsingDecl>(Other)) { | ||||||||||||
5899 | OtherIsPackExpansion = false; | ||||||||||||
5900 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUD); | ||||||||||||
5901 | } else { | ||||||||||||
5902 | return false; | ||||||||||||
5903 | } | ||||||||||||
5904 | return Pattern->isPackExpansion() == OtherIsPackExpansion && | ||||||||||||
5905 | declaresSameEntity(OtherFrom, Pattern); | ||||||||||||
5906 | } | ||||||||||||
5907 | |||||||||||||
5908 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, | ||||||||||||
5909 | VarDecl *Instance) { | ||||||||||||
5910 | assert(Instance->isStaticDataMember())(static_cast <bool> (Instance->isStaticDataMember()) ? void (0) : __assert_fail ("Instance->isStaticDataMember()" , "clang/lib/Sema/SemaTemplateInstantiateDecl.cpp", 5910, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||
5911 | |||||||||||||
5912 | Pattern = Pattern->getCanonicalDecl(); | ||||||||||||
5913 | |||||||||||||
5914 | do { | ||||||||||||
5915 | Instance = Instance->getCanonicalDecl(); | ||||||||||||
5916 | if (Pattern == Instance) return true; | ||||||||||||
5917 | Instance = Instance->getInstantiatedFromStaticDataMember(); | ||||||||||||
5918 | } while (Instance); | ||||||||||||
5919 | |||||||||||||
5920 | return false; | ||||||||||||
5921 | } | ||||||||||||
5922 | |||||||||||||
5923 | // Other is the prospective instantiation | ||||||||||||
5924 | // D is the prospective pattern | ||||||||||||
5925 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { | ||||||||||||
5926 | if (auto *UUD = dyn_cast<UnresolvedUsingTypenameDecl>(D)) | ||||||||||||
5927 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); | ||||||||||||
5928 | |||||||||||||
5929 | if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(D)) | ||||||||||||
5930 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); | ||||||||||||
5931 | |||||||||||||
5932 | if (D->getKind() != Other->getKind()) | ||||||||||||
5933 | return false; | ||||||||||||
5934 | |||||||||||||
5935 | if (auto *Record = dyn_cast<CXXRecordDecl>(Other)) | ||||||||||||
5936 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); | ||||||||||||
5937 | |||||||||||||
5938 | if (auto *Function = dyn_cast<FunctionDecl>(Other)) | ||||||||||||
5939 | return isInstantiationOf(cast<FunctionDecl>(D), Function); | ||||||||||||
5940 | |||||||||||||
5941 | if (auto *Enum = dyn_cast<EnumDecl>(Other)) | ||||||||||||
5942 | return isInstantiationOf(cast<EnumDecl>(D), Enum); | ||||||||||||
5943 | |||||||||||||
5944 | if (auto *Var = dyn_cast<VarDecl>(Other)) | ||||||||||||
5945 | if (Var->isStaticDataMember()) | ||||||||||||
5946 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); | ||||||||||||
5947 | |||||||||||||
5948 | if (auto *Temp = dyn_cast<ClassTemplateDecl>(Other)) | ||||||||||||
5949 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); | ||||||||||||
5950 | |||||||||||||
5951 | if (auto *Temp = dyn_cast<FunctionTemplateDecl>(Other)) | ||||||||||||
5952 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); | ||||||||||||
5953 | |||||||||||||
5954 | if (auto *PartialSpec = | ||||||||||||
5955 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) | ||||||||||||
5956 | return isInstantiationOf(cast&l |