clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaTemplateInstantiate.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaTemplateInstantiate.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | #include "TreeTransform.h" |
13 | #include "clang/AST/ASTConsumer.h" |
14 | #include "clang/AST/ASTContext.h" |
15 | #include "clang/AST/ASTLambda.h" |
16 | #include "clang/AST/ASTMutationListener.h" |
17 | #include "clang/AST/DeclTemplate.h" |
18 | #include "clang/AST/Expr.h" |
19 | #include "clang/AST/PrettyDeclStackTrace.h" |
20 | #include "clang/AST/TypeVisitor.h" |
21 | #include "clang/Basic/LangOptions.h" |
22 | #include "clang/Basic/Stack.h" |
23 | #include "clang/Basic/TargetInfo.h" |
24 | #include "clang/Sema/DeclSpec.h" |
25 | #include "clang/Sema/Initialization.h" |
26 | #include "clang/Sema/Lookup.h" |
27 | #include "clang/Sema/SemaConcept.h" |
28 | #include "clang/Sema/SemaInternal.h" |
29 | #include "clang/Sema/Template.h" |
30 | #include "clang/Sema/TemplateDeduction.h" |
31 | #include "clang/Sema/TemplateInstCallback.h" |
32 | #include "llvm/Support/TimeProfiler.h" |
33 | |
34 | using namespace clang; |
35 | using namespace sema; |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | MultiLevelTemplateArgumentList |
59 | Sema::getTemplateInstantiationArgs(NamedDecl *D, |
60 | const TemplateArgumentList *Innermost, |
61 | bool RelativeToPrimary, |
62 | const FunctionDecl *Pattern) { |
63 | |
64 | MultiLevelTemplateArgumentList Result; |
65 | |
66 | if (Innermost) |
67 | Result.addOuterTemplateArguments(Innermost); |
68 | |
69 | DeclContext *Ctx = dyn_cast<DeclContext>(D); |
70 | if (!Ctx) { |
71 | Ctx = D->getDeclContext(); |
72 | |
73 | |
74 | |
75 | |
76 | VarTemplateSpecializationDecl *Spec = |
77 | dyn_cast<VarTemplateSpecializationDecl>(D); |
78 | if (Spec && !Spec->isClassScopeExplicitSpecialization()) { |
79 | |
80 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && |
81 | !isa<VarTemplatePartialSpecializationDecl>(Spec)) |
82 | return Result; |
83 | |
84 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
85 | |
86 | |
87 | |
88 | assert(Spec->getSpecializedTemplate() && "No variable template?"); |
89 | llvm::PointerUnion<VarTemplateDecl*, |
90 | VarTemplatePartialSpecializationDecl*> Specialized |
91 | = Spec->getSpecializedTemplateOrPartial(); |
92 | if (VarTemplatePartialSpecializationDecl *Partial = |
93 | Specialized.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
94 | if (Partial->isMemberSpecialization()) |
95 | return Result; |
96 | } else { |
97 | VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>(); |
98 | if (Tmpl->isMemberSpecialization()) |
99 | return Result; |
100 | } |
101 | } |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | if (Ctx->isTranslationUnit()) { |
110 | if (TemplateTemplateParmDecl *TTP |
111 | = dyn_cast<TemplateTemplateParmDecl>(D)) { |
112 | for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I) |
113 | Result.addOuterTemplateArguments(None); |
114 | return Result; |
115 | } |
116 | } |
117 | } |
118 | |
119 | while (!Ctx->isFileContext()) { |
120 | |
121 | ClassTemplateSpecializationDecl *Spec |
122 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx); |
123 | if (Spec && !Spec->isClassScopeExplicitSpecialization()) { |
124 | |
125 | if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && |
126 | !isa<ClassTemplatePartialSpecializationDecl>(Spec)) |
127 | break; |
128 | |
129 | Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); |
130 | |
131 | |
132 | |
133 | assert(Spec->getSpecializedTemplate() && "No class template?"); |
134 | if (Spec->getSpecializedTemplate()->isMemberSpecialization()) |
135 | break; |
136 | } |
137 | |
138 | else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { |
139 | if (!RelativeToPrimary && |
140 | Function->getTemplateSpecializationKindForInstantiation() == |
141 | TSK_ExplicitSpecialization) |
142 | break; |
143 | |
144 | if (!RelativeToPrimary && Function->getTemplateSpecializationKind() == |
145 | TSK_ExplicitSpecialization) { |
146 | |
147 | |
148 | |
149 | } else if (const TemplateArgumentList *TemplateArgs |
150 | = Function->getTemplateSpecializationArgs()) { |
151 | |
152 | Result.addOuterTemplateArguments(TemplateArgs); |
153 | |
154 | |
155 | |
156 | assert(Function->getPrimaryTemplate() && "No function template?"); |
157 | if (Function->getPrimaryTemplate()->isMemberSpecialization()) |
158 | break; |
159 | |
160 | |
161 | if (isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) |
162 | break; |
163 | |
164 | } else if (FunctionTemplateDecl *FunTmpl |
165 | = Function->getDescribedFunctionTemplate()) { |
166 | |
167 | Result.addOuterTemplateArguments(FunTmpl->getInjectedTemplateArgs()); |
168 | } |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | if (Function->getFriendObjectKind() && |
175 | Function->getDeclContext()->isFileContext() && |
176 | (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { |
177 | Ctx = Function->getLexicalDeclContext(); |
178 | RelativeToPrimary = false; |
179 | continue; |
180 | } |
181 | } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) { |
182 | if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) { |
183 | QualType T = ClassTemplate->getInjectedClassNameSpecialization(); |
184 | const TemplateSpecializationType *TST = |
185 | cast<TemplateSpecializationType>(Context.getCanonicalType(T)); |
186 | Result.addOuterTemplateArguments( |
187 | llvm::makeArrayRef(TST->getArgs(), TST->getNumArgs())); |
188 | if (ClassTemplate->isMemberSpecialization()) |
189 | break; |
190 | } |
191 | } |
192 | |
193 | Ctx = Ctx->getParent(); |
194 | RelativeToPrimary = false; |
195 | } |
196 | |
197 | return Result; |
198 | } |
199 | |
200 | bool Sema::CodeSynthesisContext::isInstantiationRecord() const { |
201 | switch (Kind) { |
202 | case TemplateInstantiation: |
203 | case ExceptionSpecInstantiation: |
204 | case DefaultTemplateArgumentInstantiation: |
205 | case DefaultFunctionArgumentInstantiation: |
206 | case ExplicitTemplateArgumentSubstitution: |
207 | case DeducedTemplateArgumentSubstitution: |
208 | case PriorTemplateArgumentSubstitution: |
209 | case ConstraintsCheck: |
210 | case NestedRequirementConstraintsCheck: |
211 | return true; |
212 | |
213 | case RequirementInstantiation: |
214 | case DefaultTemplateArgumentChecking: |
215 | case DeclaringSpecialMember: |
216 | case DeclaringImplicitEqualityComparison: |
217 | case DefiningSynthesizedFunction: |
218 | case ExceptionSpecEvaluation: |
219 | case ConstraintSubstitution: |
220 | case ParameterMappingSubstitution: |
221 | case ConstraintNormalization: |
222 | case RewritingOperatorAsSpaceship: |
223 | case InitializingStructuredBinding: |
224 | case MarkingClassDllexported: |
225 | return false; |
226 | |
227 | |
228 | case Memoization: |
229 | break; |
230 | } |
231 | |
232 | llvm_unreachable("Invalid SynthesisKind!"); |
233 | } |
234 | |
235 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
236 | Sema &SemaRef, CodeSynthesisContext::SynthesisKind Kind, |
237 | SourceLocation PointOfInstantiation, SourceRange InstantiationRange, |
238 | Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
239 | sema::TemplateDeductionInfo *DeductionInfo) |
240 | : SemaRef(SemaRef) { |
241 | |
242 | |
243 | |
244 | if (SemaRef.Diags.hasFatalErrorOccurred() && |
| 18 | | Assuming the condition is false | |
|
245 | SemaRef.hasUncompilableErrorOccurred()) { |
246 | Invalid = true; |
247 | return; |
248 | } |
249 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
| 19 | | Calling 'InstantiatingTemplate::CheckInstantiationDepth' | |
|
| 23 | | Returning from 'InstantiatingTemplate::CheckInstantiationDepth' | |
|
250 | if (!Invalid) { |
| |
251 | CodeSynthesisContext Inst; |
252 | Inst.Kind = Kind; |
253 | Inst.PointOfInstantiation = PointOfInstantiation; |
254 | Inst.Entity = Entity; |
255 | Inst.Template = Template; |
256 | Inst.TemplateArgs = TemplateArgs.data(); |
257 | Inst.NumTemplateArgs = TemplateArgs.size(); |
258 | Inst.DeductionInfo = DeductionInfo; |
259 | Inst.InstantiationRange = InstantiationRange; |
260 | SemaRef.pushCodeSynthesisContext(Inst); |
261 | |
262 | AlreadyInstantiating = !Inst.Entity ? false : |
| |
263 | !SemaRef.InstantiatingSpecializations |
| 26 | | Assuming field 'second' is true | |
|
264 | .insert({Inst.Entity->getCanonicalDecl(), Inst.Kind}) |
265 | .second; |
266 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, Inst); |
267 | } |
268 | } |
269 | |
270 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
271 | Sema &SemaRef, SourceLocation PointOfInstantiation, Decl *Entity, |
272 | SourceRange InstantiationRange) |
273 | : InstantiatingTemplate(SemaRef, |
| 17 | | Calling constructor for 'InstantiatingTemplate' | |
|
| 27 | | Returning from constructor for 'InstantiatingTemplate' | |
|
274 | CodeSynthesisContext::TemplateInstantiation, |
275 | PointOfInstantiation, InstantiationRange, Entity) {} |
276 | |
277 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
278 | Sema &SemaRef, SourceLocation PointOfInstantiation, FunctionDecl *Entity, |
279 | ExceptionSpecification, SourceRange InstantiationRange) |
280 | : InstantiatingTemplate( |
281 | SemaRef, CodeSynthesisContext::ExceptionSpecInstantiation, |
282 | PointOfInstantiation, InstantiationRange, Entity) {} |
283 | |
284 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
285 | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateParameter Param, |
286 | TemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
287 | SourceRange InstantiationRange) |
288 | : InstantiatingTemplate( |
289 | SemaRef, |
290 | CodeSynthesisContext::DefaultTemplateArgumentInstantiation, |
291 | PointOfInstantiation, InstantiationRange, getAsNamedDecl(Param), |
292 | Template, TemplateArgs) {} |
293 | |
294 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
295 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
296 | FunctionTemplateDecl *FunctionTemplate, |
297 | ArrayRef<TemplateArgument> TemplateArgs, |
298 | CodeSynthesisContext::SynthesisKind Kind, |
299 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
300 | : InstantiatingTemplate(SemaRef, Kind, PointOfInstantiation, |
301 | InstantiationRange, FunctionTemplate, nullptr, |
302 | TemplateArgs, &DeductionInfo) { |
303 | assert( |
304 | Kind == CodeSynthesisContext::ExplicitTemplateArgumentSubstitution || |
305 | Kind == CodeSynthesisContext::DeducedTemplateArgumentSubstitution); |
306 | } |
307 | |
308 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
309 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
310 | TemplateDecl *Template, |
311 | ArrayRef<TemplateArgument> TemplateArgs, |
312 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
313 | : InstantiatingTemplate( |
314 | SemaRef, |
315 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
316 | PointOfInstantiation, InstantiationRange, Template, nullptr, |
317 | TemplateArgs, &DeductionInfo) {} |
318 | |
319 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
320 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
321 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
322 | ArrayRef<TemplateArgument> TemplateArgs, |
323 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
324 | : InstantiatingTemplate( |
325 | SemaRef, |
326 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
327 | PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
328 | TemplateArgs, &DeductionInfo) {} |
329 | |
330 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
331 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
332 | VarTemplatePartialSpecializationDecl *PartialSpec, |
333 | ArrayRef<TemplateArgument> TemplateArgs, |
334 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
335 | : InstantiatingTemplate( |
336 | SemaRef, |
337 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
338 | PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
339 | TemplateArgs, &DeductionInfo) {} |
340 | |
341 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
342 | Sema &SemaRef, SourceLocation PointOfInstantiation, ParmVarDecl *Param, |
343 | ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
344 | : InstantiatingTemplate( |
345 | SemaRef, |
346 | CodeSynthesisContext::DefaultFunctionArgumentInstantiation, |
347 | PointOfInstantiation, InstantiationRange, Param, nullptr, |
348 | TemplateArgs) {} |
349 | |
350 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
351 | Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
352 | NonTypeTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
353 | SourceRange InstantiationRange) |
354 | : InstantiatingTemplate( |
355 | SemaRef, |
356 | CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
357 | PointOfInstantiation, InstantiationRange, Param, Template, |
358 | TemplateArgs) {} |
359 | |
360 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
361 | Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
362 | TemplateTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
363 | SourceRange InstantiationRange) |
364 | : InstantiatingTemplate( |
365 | SemaRef, |
366 | CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
367 | PointOfInstantiation, InstantiationRange, Param, Template, |
368 | TemplateArgs) {} |
369 | |
370 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
371 | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template, |
372 | NamedDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
373 | SourceRange InstantiationRange) |
374 | : InstantiatingTemplate( |
375 | SemaRef, CodeSynthesisContext::DefaultTemplateArgumentChecking, |
376 | PointOfInstantiation, InstantiationRange, Param, Template, |
377 | TemplateArgs) {} |
378 | |
379 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
380 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
381 | concepts::Requirement *Req, sema::TemplateDeductionInfo &DeductionInfo, |
382 | SourceRange InstantiationRange) |
383 | : InstantiatingTemplate( |
384 | SemaRef, CodeSynthesisContext::RequirementInstantiation, |
385 | PointOfInstantiation, InstantiationRange, nullptr, |
386 | nullptr, None, &DeductionInfo) {} |
387 | |
388 | |
389 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
390 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
391 | concepts::NestedRequirement *Req, ConstraintsCheck, |
392 | SourceRange InstantiationRange) |
393 | : InstantiatingTemplate( |
394 | SemaRef, CodeSynthesisContext::NestedRequirementConstraintsCheck, |
395 | PointOfInstantiation, InstantiationRange, nullptr, |
396 | nullptr, None) {} |
397 | |
398 | |
399 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
400 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
401 | ConstraintsCheck, NamedDecl *Template, |
402 | ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
403 | : InstantiatingTemplate( |
404 | SemaRef, CodeSynthesisContext::ConstraintsCheck, |
405 | PointOfInstantiation, InstantiationRange, Template, nullptr, |
406 | TemplateArgs) {} |
407 | |
408 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
409 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
410 | ConstraintSubstitution, NamedDecl *Template, |
411 | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
412 | : InstantiatingTemplate( |
413 | SemaRef, CodeSynthesisContext::ConstraintSubstitution, |
414 | PointOfInstantiation, InstantiationRange, Template, nullptr, |
415 | {}, &DeductionInfo) {} |
416 | |
417 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
418 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
419 | ConstraintNormalization, NamedDecl *Template, |
420 | SourceRange InstantiationRange) |
421 | : InstantiatingTemplate( |
422 | SemaRef, CodeSynthesisContext::ConstraintNormalization, |
423 | PointOfInstantiation, InstantiationRange, Template) {} |
424 | |
425 | Sema::InstantiatingTemplate::InstantiatingTemplate( |
426 | Sema &SemaRef, SourceLocation PointOfInstantiation, |
427 | ParameterMappingSubstitution, NamedDecl *Template, |
428 | SourceRange InstantiationRange) |
429 | : InstantiatingTemplate( |
430 | SemaRef, CodeSynthesisContext::ParameterMappingSubstitution, |
431 | PointOfInstantiation, InstantiationRange, Template) {} |
432 | |
433 | void Sema::pushCodeSynthesisContext(CodeSynthesisContext Ctx) { |
434 | Ctx.SavedInNonInstantiationSFINAEContext = InNonInstantiationSFINAEContext; |
435 | InNonInstantiationSFINAEContext = false; |
436 | |
437 | CodeSynthesisContexts.push_back(Ctx); |
438 | |
439 | if (!Ctx.isInstantiationRecord()) |
440 | ++NonInstantiationEntries; |
441 | |
442 | |
443 | |
444 | if (isStackNearlyExhausted()) |
445 | warnStackExhausted(Ctx.PointOfInstantiation); |
446 | } |
447 | |
448 | void Sema::popCodeSynthesisContext() { |
449 | auto &Active = CodeSynthesisContexts.back(); |
450 | if (!Active.isInstantiationRecord()) { |
451 | assert(NonInstantiationEntries > 0); |
452 | --NonInstantiationEntries; |
453 | } |
454 | |
455 | InNonInstantiationSFINAEContext = Active.SavedInNonInstantiationSFINAEContext; |
456 | |
457 | |
458 | assert(CodeSynthesisContexts.size() >= |
459 | CodeSynthesisContextLookupModules.size() && |
460 | "forgot to remove a lookup module for a template instantiation"); |
461 | if (CodeSynthesisContexts.size() == |
462 | CodeSynthesisContextLookupModules.size()) { |
463 | if (Module *M = CodeSynthesisContextLookupModules.back()) |
464 | LookupModulesCache.erase(M); |
465 | CodeSynthesisContextLookupModules.pop_back(); |
466 | } |
467 | |
468 | |
469 | |
470 | if (CodeSynthesisContexts.size() == |
471 | LastEmittedCodeSynthesisContextDepth) |
472 | LastEmittedCodeSynthesisContextDepth = 0; |
473 | |
474 | CodeSynthesisContexts.pop_back(); |
475 | } |
476 | |
477 | void Sema::InstantiatingTemplate::Clear() { |
478 | if (!Invalid) { |
479 | if (!AlreadyInstantiating) { |
480 | auto &Active = SemaRef.CodeSynthesisContexts.back(); |
481 | if (Active.Entity) |
482 | SemaRef.InstantiatingSpecializations.erase( |
483 | {Active.Entity->getCanonicalDecl(), Active.Kind}); |
484 | } |
485 | |
486 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, |
487 | SemaRef.CodeSynthesisContexts.back()); |
488 | |
489 | SemaRef.popCodeSynthesisContext(); |
490 | Invalid = true; |
491 | } |
492 | } |
493 | |
494 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
495 | SourceLocation PointOfInstantiation, |
496 | SourceRange InstantiationRange) { |
497 | assert(SemaRef.NonInstantiationEntries <= |
498 | SemaRef.CodeSynthesisContexts.size()); |
499 | if ((SemaRef.CodeSynthesisContexts.size() - |
| 20 | | Assuming the condition is true | |
|
| |
500 | SemaRef.NonInstantiationEntries) |
501 | <= SemaRef.getLangOpts().InstantiationDepth) |
502 | return false; |
| 22 | | Returning zero, which participates in a condition later | |
|
503 | |
504 | SemaRef.Diag(PointOfInstantiation, |
505 | diag::err_template_recursion_depth_exceeded) |
506 | << SemaRef.getLangOpts().InstantiationDepth |
507 | << InstantiationRange; |
508 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
509 | << SemaRef.getLangOpts().InstantiationDepth; |
510 | return true; |
511 | } |
512 | |
513 | |
514 | |
515 | void Sema::PrintInstantiationStack() { |
516 | |
517 | unsigned SkipStart = CodeSynthesisContexts.size(), SkipEnd = SkipStart; |
518 | unsigned Limit = Diags.getTemplateBacktraceLimit(); |
519 | if (Limit && Limit < CodeSynthesisContexts.size()) { |
520 | SkipStart = Limit / 2 + Limit % 2; |
521 | SkipEnd = CodeSynthesisContexts.size() - Limit / 2; |
522 | } |
523 | |
524 | |
525 | unsigned InstantiationIdx = 0; |
526 | for (SmallVectorImpl<CodeSynthesisContext>::reverse_iterator |
527 | Active = CodeSynthesisContexts.rbegin(), |
528 | ActiveEnd = CodeSynthesisContexts.rend(); |
529 | Active != ActiveEnd; |
530 | ++Active, ++InstantiationIdx) { |
531 | |
532 | if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { |
533 | if (InstantiationIdx == SkipStart) { |
534 | |
535 | Diags.Report(Active->PointOfInstantiation, |
536 | diag::note_instantiation_contexts_suppressed) |
537 | << unsigned(CodeSynthesisContexts.size() - Limit); |
538 | } |
539 | continue; |
540 | } |
541 | |
542 | switch (Active->Kind) { |
543 | case CodeSynthesisContext::TemplateInstantiation: { |
544 | Decl *D = Active->Entity; |
545 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
546 | unsigned DiagID = diag::note_template_member_class_here; |
547 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
548 | DiagID = diag::note_template_class_instantiation_here; |
549 | Diags.Report(Active->PointOfInstantiation, DiagID) |
550 | << Record << Active->InstantiationRange; |
551 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
552 | unsigned DiagID; |
553 | if (Function->getPrimaryTemplate()) |
554 | DiagID = diag::note_function_template_spec_here; |
555 | else |
556 | DiagID = diag::note_template_member_function_here; |
557 | Diags.Report(Active->PointOfInstantiation, DiagID) |
558 | << Function |
559 | << Active->InstantiationRange; |
560 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
561 | Diags.Report(Active->PointOfInstantiation, |
562 | VD->isStaticDataMember()? |
563 | diag::note_template_static_data_member_def_here |
564 | : diag::note_template_variable_def_here) |
565 | << VD |
566 | << Active->InstantiationRange; |
567 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
568 | Diags.Report(Active->PointOfInstantiation, |
569 | diag::note_template_enum_def_here) |
570 | << ED |
571 | << Active->InstantiationRange; |
572 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
573 | Diags.Report(Active->PointOfInstantiation, |
574 | diag::note_template_nsdmi_here) |
575 | << FD << Active->InstantiationRange; |
576 | } else { |
577 | Diags.Report(Active->PointOfInstantiation, |
578 | diag::note_template_type_alias_instantiation_here) |
579 | << cast<TypeAliasTemplateDecl>(D) |
580 | << Active->InstantiationRange; |
581 | } |
582 | break; |
583 | } |
584 | |
585 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: { |
586 | TemplateDecl *Template = cast<TemplateDecl>(Active->Template); |
587 | SmallString<128> TemplateArgsStr; |
588 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
589 | Template->printName(OS); |
590 | printTemplateArgumentList(OS, Active->template_arguments(), |
591 | getPrintingPolicy()); |
592 | Diags.Report(Active->PointOfInstantiation, |
593 | diag::note_default_arg_instantiation_here) |
594 | << OS.str() |
595 | << Active->InstantiationRange; |
596 | break; |
597 | } |
598 | |
599 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: { |
600 | FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity); |
601 | Diags.Report(Active->PointOfInstantiation, |
602 | diag::note_explicit_template_arg_substitution_here) |
603 | << FnTmpl |
604 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
605 | Active->TemplateArgs, |
606 | Active->NumTemplateArgs) |
607 | << Active->InstantiationRange; |
608 | break; |
609 | } |
610 | |
611 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: { |
612 | if (FunctionTemplateDecl *FnTmpl = |
613 | dyn_cast<FunctionTemplateDecl>(Active->Entity)) { |
614 | Diags.Report(Active->PointOfInstantiation, |
615 | diag::note_function_template_deduction_instantiation_here) |
616 | << FnTmpl |
617 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
618 | Active->TemplateArgs, |
619 | Active->NumTemplateArgs) |
620 | << Active->InstantiationRange; |
621 | } else { |
622 | bool IsVar = isa<VarTemplateDecl>(Active->Entity) || |
623 | isa<VarTemplateSpecializationDecl>(Active->Entity); |
624 | bool IsTemplate = false; |
625 | TemplateParameterList *Params; |
626 | if (auto *D = dyn_cast<TemplateDecl>(Active->Entity)) { |
627 | IsTemplate = true; |
628 | Params = D->getTemplateParameters(); |
629 | } else if (auto *D = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
630 | Active->Entity)) { |
631 | Params = D->getTemplateParameters(); |
632 | } else if (auto *D = dyn_cast<VarTemplatePartialSpecializationDecl>( |
633 | Active->Entity)) { |
634 | Params = D->getTemplateParameters(); |
635 | } else { |
636 | llvm_unreachable("unexpected template kind"); |
637 | } |
638 | |
639 | Diags.Report(Active->PointOfInstantiation, |
640 | diag::note_deduced_template_arg_substitution_here) |
641 | << IsVar << IsTemplate << cast<NamedDecl>(Active->Entity) |
642 | << getTemplateArgumentBindingsText(Params, Active->TemplateArgs, |
643 | Active->NumTemplateArgs) |
644 | << Active->InstantiationRange; |
645 | } |
646 | break; |
647 | } |
648 | |
649 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: { |
650 | ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity); |
651 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
652 | |
653 | SmallString<128> TemplateArgsStr; |
654 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
655 | FD->printName(OS); |
656 | printTemplateArgumentList(OS, Active->template_arguments(), |
657 | getPrintingPolicy()); |
658 | Diags.Report(Active->PointOfInstantiation, |
659 | diag::note_default_function_arg_instantiation_here) |
660 | << OS.str() |
661 | << Active->InstantiationRange; |
662 | break; |
663 | } |
664 | |
665 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: { |
666 | NamedDecl *Parm = cast<NamedDecl>(Active->Entity); |
667 | std::string Name; |
668 | if (!Parm->getName().empty()) |
669 | Name = std::string(" '") + Parm->getName().str() + "'"; |
670 | |
671 | TemplateParameterList *TemplateParams = nullptr; |
672 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
673 | TemplateParams = Template->getTemplateParameters(); |
674 | else |
675 | TemplateParams = |
676 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
677 | ->getTemplateParameters(); |
678 | Diags.Report(Active->PointOfInstantiation, |
679 | diag::note_prior_template_arg_substitution) |
680 | << isa<TemplateTemplateParmDecl>(Parm) |
681 | << Name |
682 | << getTemplateArgumentBindingsText(TemplateParams, |
683 | Active->TemplateArgs, |
684 | Active->NumTemplateArgs) |
685 | << Active->InstantiationRange; |
686 | break; |
687 | } |
688 | |
689 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: { |
690 | TemplateParameterList *TemplateParams = nullptr; |
691 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
692 | TemplateParams = Template->getTemplateParameters(); |
693 | else |
694 | TemplateParams = |
695 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
696 | ->getTemplateParameters(); |
697 | |
698 | Diags.Report(Active->PointOfInstantiation, |
699 | diag::note_template_default_arg_checking) |
700 | << getTemplateArgumentBindingsText(TemplateParams, |
701 | Active->TemplateArgs, |
702 | Active->NumTemplateArgs) |
703 | << Active->InstantiationRange; |
704 | break; |
705 | } |
706 | |
707 | case CodeSynthesisContext::ExceptionSpecEvaluation: |
708 | Diags.Report(Active->PointOfInstantiation, |
709 | diag::note_evaluating_exception_spec_here) |
710 | << cast<FunctionDecl>(Active->Entity); |
711 | break; |
712 | |
713 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
714 | Diags.Report(Active->PointOfInstantiation, |
715 | diag::note_template_exception_spec_instantiation_here) |
716 | << cast<FunctionDecl>(Active->Entity) |
717 | << Active->InstantiationRange; |
718 | break; |
719 | |
720 | case CodeSynthesisContext::RequirementInstantiation: |
721 | Diags.Report(Active->PointOfInstantiation, |
722 | diag::note_template_requirement_instantiation_here) |
723 | << Active->InstantiationRange; |
724 | break; |
725 | |
726 | case CodeSynthesisContext::NestedRequirementConstraintsCheck: |
727 | Diags.Report(Active->PointOfInstantiation, |
728 | diag::note_nested_requirement_here) |
729 | << Active->InstantiationRange; |
730 | break; |
731 | |
732 | case CodeSynthesisContext::DeclaringSpecialMember: |
733 | Diags.Report(Active->PointOfInstantiation, |
734 | diag::note_in_declaration_of_implicit_special_member) |
735 | << cast<CXXRecordDecl>(Active->Entity) << Active->SpecialMember; |
736 | break; |
737 | |
738 | case CodeSynthesisContext::DeclaringImplicitEqualityComparison: |
739 | Diags.Report(Active->Entity->getLocation(), |
740 | diag::note_in_declaration_of_implicit_equality_comparison); |
741 | break; |
742 | |
743 | case CodeSynthesisContext::DefiningSynthesizedFunction: { |
744 | |
745 | |
746 | auto *FD = dyn_cast<FunctionDecl>(Active->Entity); |
747 | DefaultedFunctionKind DFK = |
748 | FD ? getDefaultedFunctionKind(FD) : DefaultedFunctionKind(); |
749 | if (DFK.isSpecialMember()) { |
750 | auto *MD = cast<CXXMethodDecl>(FD); |
751 | Diags.Report(Active->PointOfInstantiation, |
752 | diag::note_member_synthesized_at) |
753 | << MD->isExplicitlyDefaulted() << DFK.asSpecialMember() |
754 | << Context.getTagDeclType(MD->getParent()); |
755 | } else if (DFK.isComparison()) { |
756 | Diags.Report(Active->PointOfInstantiation, |
757 | diag::note_comparison_synthesized_at) |
758 | << (int)DFK.asComparison() |
759 | << Context.getTagDeclType( |
760 | cast<CXXRecordDecl>(FD->getLexicalDeclContext())); |
761 | } |
762 | break; |
763 | } |
764 | |
765 | case CodeSynthesisContext::RewritingOperatorAsSpaceship: |
766 | Diags.Report(Active->Entity->getLocation(), |
767 | diag::note_rewriting_operator_as_spaceship); |
768 | break; |
769 | |
770 | case CodeSynthesisContext::InitializingStructuredBinding: |
771 | Diags.Report(Active->PointOfInstantiation, |
772 | diag::note_in_binding_decl_init) |
773 | << cast<BindingDecl>(Active->Entity); |
774 | break; |
775 | |
776 | case CodeSynthesisContext::MarkingClassDllexported: |
777 | Diags.Report(Active->PointOfInstantiation, |
778 | diag::note_due_to_dllexported_class) |
779 | << cast<CXXRecordDecl>(Active->Entity) << !getLangOpts().CPlusPlus11; |
780 | break; |
781 | |
782 | case CodeSynthesisContext::Memoization: |
783 | break; |
784 | |
785 | case CodeSynthesisContext::ConstraintsCheck: { |
786 | unsigned DiagID = 0; |
787 | if (!Active->Entity) { |
788 | Diags.Report(Active->PointOfInstantiation, |
789 | diag::note_nested_requirement_here) |
790 | << Active->InstantiationRange; |
791 | break; |
792 | } |
793 | if (isa<ConceptDecl>(Active->Entity)) |
794 | DiagID = diag::note_concept_specialization_here; |
795 | else if (isa<TemplateDecl>(Active->Entity)) |
796 | DiagID = diag::note_checking_constraints_for_template_id_here; |
797 | else if (isa<VarTemplatePartialSpecializationDecl>(Active->Entity)) |
798 | DiagID = diag::note_checking_constraints_for_var_spec_id_here; |
799 | else if (isa<ClassTemplatePartialSpecializationDecl>(Active->Entity)) |
800 | DiagID = diag::note_checking_constraints_for_class_spec_id_here; |
801 | else { |
802 | assert(isa<FunctionDecl>(Active->Entity)); |
803 | DiagID = diag::note_checking_constraints_for_function_here; |
804 | } |
805 | SmallString<128> TemplateArgsStr; |
806 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
807 | cast<NamedDecl>(Active->Entity)->printName(OS); |
808 | if (!isa<FunctionDecl>(Active->Entity)) { |
809 | printTemplateArgumentList(OS, Active->template_arguments(), |
810 | getPrintingPolicy()); |
811 | } |
812 | Diags.Report(Active->PointOfInstantiation, DiagID) << OS.str() |
813 | << Active->InstantiationRange; |
814 | break; |
815 | } |
816 | case CodeSynthesisContext::ConstraintSubstitution: |
817 | Diags.Report(Active->PointOfInstantiation, |
818 | diag::note_constraint_substitution_here) |
819 | << Active->InstantiationRange; |
820 | break; |
821 | case CodeSynthesisContext::ConstraintNormalization: |
822 | Diags.Report(Active->PointOfInstantiation, |
823 | diag::note_constraint_normalization_here) |
824 | << cast<NamedDecl>(Active->Entity)->getName() |
825 | << Active->InstantiationRange; |
826 | break; |
827 | case CodeSynthesisContext::ParameterMappingSubstitution: |
828 | Diags.Report(Active->PointOfInstantiation, |
829 | diag::note_parameter_mapping_substitution_here) |
830 | << Active->InstantiationRange; |
831 | break; |
832 | } |
833 | } |
834 | } |
835 | |
836 | Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const { |
837 | if (InNonInstantiationSFINAEContext) |
838 | return Optional<TemplateDeductionInfo *>(nullptr); |
839 | |
840 | for (SmallVectorImpl<CodeSynthesisContext>::const_reverse_iterator |
841 | Active = CodeSynthesisContexts.rbegin(), |
842 | ActiveEnd = CodeSynthesisContexts.rend(); |
843 | Active != ActiveEnd; |
844 | ++Active) |
845 | { |
846 | switch (Active->Kind) { |
847 | case CodeSynthesisContext::TemplateInstantiation: |
848 | |
849 | |
850 | if (isa<TypeAliasTemplateDecl>(Active->Entity)) |
851 | break; |
852 | LLVM_FALLTHROUGH; |
853 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: |
854 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
855 | case CodeSynthesisContext::ConstraintsCheck: |
856 | case CodeSynthesisContext::ParameterMappingSubstitution: |
857 | case CodeSynthesisContext::ConstraintNormalization: |
858 | case CodeSynthesisContext::NestedRequirementConstraintsCheck: |
859 | |
860 | return None; |
861 | |
862 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: |
863 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: |
864 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: |
865 | case CodeSynthesisContext::RewritingOperatorAsSpaceship: |
866 | |
867 | |
868 | |
869 | break; |
870 | |
871 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: |
872 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: |
873 | case CodeSynthesisContext::ConstraintSubstitution: |
874 | case CodeSynthesisContext::RequirementInstantiation: |
875 | |
876 | |
877 | |
878 | assert(Active->DeductionInfo && "Missing deduction info pointer"); |
879 | return Active->DeductionInfo; |
880 | |
881 | case CodeSynthesisContext::DeclaringSpecialMember: |
882 | case CodeSynthesisContext::DeclaringImplicitEqualityComparison: |
883 | case CodeSynthesisContext::DefiningSynthesizedFunction: |
884 | case CodeSynthesisContext::InitializingStructuredBinding: |
885 | case CodeSynthesisContext::MarkingClassDllexported: |
886 | |
887 | |
888 | return None; |
889 | |
890 | case CodeSynthesisContext::ExceptionSpecEvaluation: |
891 | |
892 | |
893 | |
894 | break; |
895 | |
896 | case CodeSynthesisContext::Memoization: |
897 | break; |
898 | } |
899 | |
900 | |
901 | |
902 | if (Active->SavedInNonInstantiationSFINAEContext) |
903 | return Optional<TemplateDeductionInfo *>(nullptr); |
904 | } |
905 | |
906 | return None; |
907 | } |
908 | |
909 | |
910 | |
911 | |
912 | namespace { |
913 | class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
914 | const MultiLevelTemplateArgumentList &TemplateArgs; |
915 | SourceLocation Loc; |
916 | DeclarationName Entity; |
917 | |
918 | public: |
919 | typedef TreeTransform<TemplateInstantiator> inherited; |
920 | |
921 | TemplateInstantiator(Sema &SemaRef, |
922 | const MultiLevelTemplateArgumentList &TemplateArgs, |
923 | SourceLocation Loc, |
924 | DeclarationName Entity) |
925 | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
926 | Entity(Entity) { } |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | |
933 | bool AlreadyTransformed(QualType T); |
934 | |
935 | |
936 | SourceLocation getBaseLocation() { return Loc; } |
937 | |
938 | |
939 | DeclarationName getBaseEntity() { return Entity; } |
940 | |
941 | |
942 | |
943 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
944 | this->Loc = Loc; |
945 | this->Entity = Entity; |
946 | } |
947 | |
948 | unsigned TransformTemplateDepth(unsigned Depth) { |
949 | return TemplateArgs.getNewDepth(Depth); |
950 | } |
951 | |
952 | bool TryExpandParameterPacks(SourceLocation EllipsisLoc, |
953 | SourceRange PatternRange, |
954 | ArrayRef<UnexpandedParameterPack> Unexpanded, |
955 | bool &ShouldExpand, bool &RetainExpansion, |
956 | Optional<unsigned> &NumExpansions) { |
957 | return getSema().CheckParameterPacksForExpansion(EllipsisLoc, |
958 | PatternRange, Unexpanded, |
959 | TemplateArgs, |
960 | ShouldExpand, |
961 | RetainExpansion, |
962 | NumExpansions); |
963 | } |
964 | |
965 | void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { |
966 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack); |
967 | } |
968 | |
969 | TemplateArgument ForgetPartiallySubstitutedPack() { |
970 | TemplateArgument Result; |
971 | if (NamedDecl *PartialPack |
972 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
973 | MultiLevelTemplateArgumentList &TemplateArgs |
974 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
975 | unsigned Depth, Index; |
976 | std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
977 | if (TemplateArgs.hasTemplateArgument(Depth, Index)) { |
978 | Result = TemplateArgs(Depth, Index); |
979 | TemplateArgs.setArgument(Depth, Index, TemplateArgument()); |
980 | } |
981 | } |
982 | |
983 | return Result; |
984 | } |
985 | |
986 | void RememberPartiallySubstitutedPack(TemplateArgument Arg) { |
987 | if (Arg.isNull()) |
988 | return; |
989 | |
990 | if (NamedDecl *PartialPack |
991 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
992 | MultiLevelTemplateArgumentList &TemplateArgs |
993 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
994 | unsigned Depth, Index; |
995 | std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
996 | TemplateArgs.setArgument(Depth, Index, Arg); |
997 | } |
998 | } |
999 | |
1000 | |
1001 | |
1002 | Decl *TransformDecl(SourceLocation Loc, Decl *D); |
1003 | |
1004 | void transformAttrs(Decl *Old, Decl *New) { |
1005 | SemaRef.InstantiateAttrs(TemplateArgs, Old, New); |
1006 | } |
1007 | |
1008 | void transformedLocalDecl(Decl *Old, ArrayRef<Decl *> NewDecls) { |
1009 | if (Old->isParameterPack()) { |
1010 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Old); |
1011 | for (auto *New : NewDecls) |
1012 | SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg( |
1013 | Old, cast<VarDecl>(New)); |
1014 | return; |
1015 | } |
1016 | |
1017 | assert(NewDecls.size() == 1 && |
1018 | "should only have multiple expansions for a pack"); |
1019 | Decl *New = NewDecls.front(); |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | auto *NewMD = dyn_cast<CXXMethodDecl>(New); |
1025 | if (NewMD && isLambdaCallOperator(NewMD)) { |
1026 | auto *OldMD = dyn_cast<CXXMethodDecl>(Old); |
1027 | if (auto *NewTD = NewMD->getDescribedFunctionTemplate()) |
1028 | NewTD->setInstantiatedFromMemberTemplate( |
1029 | OldMD->getDescribedFunctionTemplate()); |
1030 | else |
1031 | NewMD->setInstantiationOfMemberFunction(OldMD, |
1032 | TSK_ImplicitInstantiation); |
1033 | } |
1034 | |
1035 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New); |
1036 | |
1037 | |
1038 | |
1039 | if (auto *DC = dyn_cast<DeclContext>(Old)) |
1040 | SemaRef.PerformDependentDiagnostics(DC, TemplateArgs); |
1041 | } |
1042 | |
1043 | |
1044 | |
1045 | Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
1046 | |
1047 | |
1048 | |
1049 | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
1050 | |
1051 | |
1052 | |
1053 | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, |
1054 | TypeSourceInfo *Declarator, |
1055 | SourceLocation StartLoc, |
1056 | SourceLocation NameLoc, |
1057 | IdentifierInfo *Name); |
1058 | |
1059 | |
1060 | |
1061 | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
1062 | TypeSourceInfo *TSInfo, QualType T); |
1063 | |
1064 | |
1065 | |
1066 | QualType RebuildElaboratedType(SourceLocation KeywordLoc, |
1067 | ElaboratedTypeKeyword Keyword, |
1068 | NestedNameSpecifierLoc QualifierLoc, |
1069 | QualType T); |
1070 | |
1071 | TemplateName |
1072 | TransformTemplateName(CXXScopeSpec &SS, TemplateName Name, |
1073 | SourceLocation NameLoc, |
1074 | QualType ObjectType = QualType(), |
1075 | NamedDecl *FirstQualifierInScope = nullptr, |
1076 | bool AllowInjectedClassName = false); |
1077 | |
1078 | const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH); |
1079 | |
1080 | ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
1081 | ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
1082 | ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
1083 | |
1084 | ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
1085 | NonTypeTemplateParmDecl *D); |
1086 | ExprResult TransformSubstNonTypeTemplateParmPackExpr( |
1087 | SubstNonTypeTemplateParmPackExpr *E); |
1088 | ExprResult TransformSubstNonTypeTemplateParmExpr( |
1089 | SubstNonTypeTemplateParmExpr *E); |
1090 | |
1091 | |
1092 | ExprResult RebuildVarDeclRefExpr(VarDecl *PD, SourceLocation Loc); |
1093 | |
1094 | |
1095 | ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E, VarDecl *PD); |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E); |
1101 | |
1102 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
1103 | FunctionProtoTypeLoc TL) { |
1104 | |
1105 | return inherited::TransformFunctionProtoType(TLB, TL); |
1106 | } |
1107 | |
1108 | template<typename Fn> |
1109 | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
1110 | FunctionProtoTypeLoc TL, |
1111 | CXXRecordDecl *ThisContext, |
1112 | Qualifiers ThisTypeQuals, |
1113 | Fn TransformExceptionSpec); |
1114 | |
1115 | ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm, |
1116 | int indexAdjustment, |
1117 | Optional<unsigned> NumExpansions, |
1118 | bool ExpectParameterPack); |
1119 | |
1120 | |
1121 | |
1122 | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
1123 | TemplateTypeParmTypeLoc TL); |
1124 | |
1125 | |
1126 | |
1127 | |
1128 | QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB, |
1129 | SubstTemplateTypeParmPackTypeLoc TL); |
1130 | |
1131 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
1132 | LocalInstantiationScope Scope(SemaRef, true); |
1133 | return TreeTransform<TemplateInstantiator>::TransformLambdaExpr(E); |
1134 | } |
1135 | |
1136 | ExprResult TransformRequiresExpr(RequiresExpr *E) { |
1137 | LocalInstantiationScope Scope(SemaRef, true); |
1138 | return TreeTransform<TemplateInstantiator>::TransformRequiresExpr(E); |
1139 | } |
1140 | |
1141 | bool TransformRequiresExprRequirements( |
1142 | ArrayRef<concepts::Requirement *> Reqs, |
1143 | SmallVectorImpl<concepts::Requirement *> &Transformed) { |
1144 | bool SatisfactionDetermined = false; |
1145 | for (concepts::Requirement *Req : Reqs) { |
1146 | concepts::Requirement *TransReq = nullptr; |
1147 | if (!SatisfactionDetermined) { |
1148 | if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) |
1149 | TransReq = TransformTypeRequirement(TypeReq); |
1150 | else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) |
1151 | TransReq = TransformExprRequirement(ExprReq); |
1152 | else |
1153 | TransReq = TransformNestedRequirement( |
1154 | cast<concepts::NestedRequirement>(Req)); |
1155 | if (!TransReq) |
1156 | return true; |
1157 | if (!TransReq->isDependent() && !TransReq->isSatisfied()) |
1158 | |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | SatisfactionDetermined = true; |
1164 | } else |
1165 | TransReq = Req; |
1166 | Transformed.push_back(TransReq); |
1167 | } |
1168 | return false; |
1169 | } |
1170 | |
1171 | TemplateParameterList *TransformTemplateParameterList( |
1172 | TemplateParameterList *OrigTPL) { |
1173 | if (!OrigTPL || !OrigTPL->size()) return OrigTPL; |
1174 | |
1175 | DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext(); |
1176 | TemplateDeclInstantiator DeclInstantiator(getSema(), |
1177 | Owner, TemplateArgs); |
1178 | return DeclInstantiator.SubstTemplateParams(OrigTPL); |
1179 | } |
1180 | |
1181 | concepts::TypeRequirement * |
1182 | TransformTypeRequirement(concepts::TypeRequirement *Req); |
1183 | concepts::ExprRequirement * |
1184 | TransformExprRequirement(concepts::ExprRequirement *Req); |
1185 | concepts::NestedRequirement * |
1186 | TransformNestedRequirement(concepts::NestedRequirement *Req); |
1187 | |
1188 | private: |
1189 | ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm, |
1190 | SourceLocation loc, |
1191 | TemplateArgument arg); |
1192 | }; |
1193 | } |
1194 | |
1195 | bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
1196 | if (T.isNull()) |
1197 | return true; |
1198 | |
1199 | if (T->isInstantiationDependentType() || T->isVariablyModifiedType()) |
1200 | return false; |
1201 | |
1202 | getSema().MarkDeclarationsReferencedInType(Loc, T); |
1203 | return true; |
1204 | } |
1205 | |
1206 | static TemplateArgument |
1207 | getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg) { |
1208 | assert(S.ArgumentPackSubstitutionIndex >= 0); |
1209 | assert(S.ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
1210 | Arg = Arg.pack_begin()[S.ArgumentPackSubstitutionIndex]; |
1211 | if (Arg.isPackExpansion()) |
1212 | Arg = Arg.getPackExpansionPattern(); |
1213 | return Arg; |
1214 | } |
1215 | |
1216 | Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
1217 | if (!D) |
1218 | return nullptr; |
1219 | |
1220 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
1221 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
1227 | TTP->getPosition())) |
1228 | return D; |
1229 | |
1230 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
1231 | |
1232 | if (TTP->isParameterPack()) { |
1233 | assert(Arg.getKind() == TemplateArgument::Pack && |
1234 | "Missing argument pack"); |
1235 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1236 | } |
1237 | |
1238 | TemplateName Template = Arg.getAsTemplate().getNameToSubstitute(); |
1239 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
1240 | "Wrong kind of template template argument"); |
1241 | return Template.getAsTemplateDecl(); |
1242 | } |
1243 | |
1244 | |
1245 | |
1246 | } |
1247 | |
1248 | return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
1249 | } |
1250 | |
1251 | Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
1252 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
1253 | if (!Inst) |
1254 | return nullptr; |
1255 | |
1256 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
1257 | return Inst; |
1258 | } |
1259 | |
1260 | NamedDecl * |
1261 | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
1262 | SourceLocation Loc) { |
1263 | |
1264 | |
1265 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
1266 | const TemplateTypeParmType *TTP |
1267 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
1268 | |
1269 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
1270 | |
1271 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); |
1272 | |
1273 | if (TTP->isParameterPack()) { |
1274 | assert(Arg.getKind() == TemplateArgument::Pack && |
1275 | "Missing argument pack"); |
1276 | |
1277 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
1278 | return nullptr; |
1279 | |
1280 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1281 | } |
1282 | |
1283 | QualType T = Arg.getAsType(); |
1284 | if (T.isNull()) |
1285 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
1286 | |
1287 | if (const TagType *Tag = T->getAs<TagType>()) |
1288 | return Tag->getDecl(); |
1289 | |
1290 | |
1291 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
1292 | return nullptr; |
1293 | } |
1294 | } |
1295 | |
1296 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
1297 | } |
1298 | |
1299 | VarDecl * |
1300 | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
1301 | TypeSourceInfo *Declarator, |
1302 | SourceLocation StartLoc, |
1303 | SourceLocation NameLoc, |
1304 | IdentifierInfo *Name) { |
1305 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, |
1306 | StartLoc, NameLoc, Name); |
1307 | if (Var) |
1308 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
1309 | return Var; |
1310 | } |
1311 | |
1312 | VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
1313 | TypeSourceInfo *TSInfo, |
1314 | QualType T) { |
1315 | VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
1316 | if (Var) |
1317 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
1318 | return Var; |
1319 | } |
1320 | |
1321 | QualType |
1322 | TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc, |
1323 | ElaboratedTypeKeyword Keyword, |
1324 | NestedNameSpecifierLoc QualifierLoc, |
1325 | QualType T) { |
1326 | if (const TagType *TT = T->getAs<TagType>()) { |
1327 | TagDecl* TD = TT->getDecl(); |
1328 | |
1329 | SourceLocation TagLocation = KeywordLoc; |
1330 | |
1331 | IdentifierInfo *Id = TD->getIdentifier(); |
1332 | |
1333 | |
1334 | |
1335 | if (Id && Keyword != ETK_None && Keyword != ETK_Typename) { |
1336 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); |
1337 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, false, |
1338 | TagLocation, Id)) { |
1339 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
1340 | << Id |
1341 | << FixItHint::CreateReplacement(SourceRange(TagLocation), |
1342 | TD->getKindName()); |
1343 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
1344 | } |
1345 | } |
1346 | } |
1347 | |
1348 | return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc, |
1349 | Keyword, |
1350 | QualifierLoc, |
1351 | T); |
1352 | } |
1353 | |
1354 | TemplateName TemplateInstantiator::TransformTemplateName( |
1355 | CXXScopeSpec &SS, TemplateName Name, SourceLocation NameLoc, |
1356 | QualType ObjectType, NamedDecl *FirstQualifierInScope, |
1357 | bool AllowInjectedClassName) { |
1358 | if (TemplateTemplateParmDecl *TTP |
1359 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) { |
1360 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
1361 | |
1362 | |
1363 | |
1364 | |
1365 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
1366 | TTP->getPosition())) |
1367 | return Name; |
1368 | |
1369 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
1370 | |
1371 | if (TemplateArgs.isRewrite()) { |
1372 | |
1373 | |
1374 | if (Arg.getKind() == TemplateArgument::Pack) { |
1375 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
1376 | "unexpected pack arguments in template rewrite"); |
1377 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
1378 | } |
1379 | assert(Arg.getKind() == TemplateArgument::Template && |
1380 | "unexpected nontype template argument kind in template rewrite"); |
1381 | return Arg.getAsTemplate(); |
1382 | } |
1383 | |
1384 | if (TTP->isParameterPack()) { |
1385 | assert(Arg.getKind() == TemplateArgument::Pack && |
1386 | "Missing argument pack"); |
1387 | |
1388 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1389 | |
1390 | |
1391 | |
1392 | return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg); |
1393 | } |
1394 | |
1395 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1396 | } |
1397 | |
1398 | TemplateName Template = Arg.getAsTemplate().getNameToSubstitute(); |
1399 | assert(!Template.isNull() && "Null template template argument"); |
1400 | assert(!Template.getAsQualifiedTemplateName() && |
1401 | "template decl to substitute is qualified?"); |
1402 | |
1403 | Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template); |
1404 | return Template; |
1405 | } |
1406 | } |
1407 | |
1408 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
1409 | = Name.getAsSubstTemplateTemplateParmPack()) { |
1410 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
1411 | return Name; |
1412 | |
1413 | TemplateArgument Arg = SubstPack->getArgumentPack(); |
1414 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1415 | return Arg.getAsTemplate().getNameToSubstitute(); |
1416 | } |
1417 | |
1418 | return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType, |
1419 | FirstQualifierInScope, |
1420 | AllowInjectedClassName); |
1421 | } |
1422 | |
1423 | ExprResult |
1424 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
1425 | if (!E->isTypeDependent()) |
1426 | return E; |
1427 | |
1428 | return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentKind()); |
1429 | } |
1430 | |
1431 | ExprResult |
1432 | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
1433 | NonTypeTemplateParmDecl *NTTP) { |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
1439 | NTTP->getPosition())) |
1440 | return E; |
1441 | |
1442 | TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); |
1443 | |
1444 | if (TemplateArgs.isRewrite()) { |
1445 | |
1446 | |
1447 | if (Arg.getKind() == TemplateArgument::Pack) { |
1448 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
1449 | "unexpected pack arguments in template rewrite"); |
1450 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
1451 | } |
1452 | assert(Arg.getKind() == TemplateArgument::Expression && |
1453 | "unexpected nontype template argument kind in template rewrite"); |
1454 | |
1455 | |
1456 | return Arg.getAsExpr(); |
1457 | } |
1458 | |
1459 | if (NTTP->isParameterPack()) { |
1460 | assert(Arg.getKind() == TemplateArgument::Pack && |
1461 | "Missing argument pack"); |
1462 | |
1463 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1464 | |
1465 | |
1466 | |
1467 | QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
1468 | E->getLocation(), |
1469 | NTTP->getDeclName()); |
1470 | if (TargetType.isNull()) |
1471 | return ExprError(); |
1472 | |
1473 | QualType ExprType = TargetType.getNonLValueExprType(SemaRef.Context); |
1474 | if (TargetType->isRecordType()) |
1475 | ExprType.addConst(); |
1476 | |
1477 | return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr( |
1478 | ExprType, TargetType->isReferenceType() ? VK_LValue : VK_PRValue, |
1479 | NTTP, E->getLocation(), Arg); |
1480 | } |
1481 | |
1482 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1483 | } |
1484 | |
1485 | return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg); |
1486 | } |
1487 | |
1488 | const LoopHintAttr * |
1489 | TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) { |
1490 | Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get(); |
1491 | |
1492 | if (TransformedExpr == LH->getValue()) |
1493 | return LH; |
1494 | |
1495 | |
1496 | if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation())) |
1497 | return LH; |
1498 | |
1499 | |
1500 | |
1501 | return LoopHintAttr::CreateImplicit(getSema().Context, LH->getOption(), |
1502 | LH->getState(), TransformedExpr, *LH); |
1503 | } |
1504 | |
1505 | ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef( |
1506 | NonTypeTemplateParmDecl *parm, |
1507 | SourceLocation loc, |
1508 | TemplateArgument arg) { |
1509 | ExprResult result; |
1510 | |
1511 | |
1512 | |
1513 | auto SubstParamType = [&] { |
1514 | QualType T; |
1515 | if (parm->isExpandedParameterPack()) |
1516 | T = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex); |
1517 | else |
1518 | T = parm->getType(); |
1519 | if (parm->isParameterPack() && isa<PackExpansionType>(T)) |
1520 | T = cast<PackExpansionType>(T)->getPattern(); |
1521 | return SemaRef.SubstType(T, TemplateArgs, loc, parm->getDeclName()); |
1522 | }; |
1523 | |
1524 | bool refParam = false; |
1525 | |
1526 | |
1527 | |
1528 | |
1529 | if (arg.getKind() == TemplateArgument::Expression) { |
1530 | Expr *argExpr = arg.getAsExpr(); |
1531 | result = argExpr; |
1532 | if (argExpr->isLValue()) { |
1533 | if (argExpr->getType()->isRecordType()) { |
1534 | |
1535 | QualType paramType = SubstParamType(); |
1536 | if (paramType.isNull()) |
1537 | return ExprError(); |
1538 | refParam = paramType->isReferenceType(); |
1539 | } else { |
1540 | refParam = true; |
1541 | } |
1542 | } |
1543 | } else if (arg.getKind() == TemplateArgument::Declaration || |
1544 | arg.getKind() == TemplateArgument::NullPtr) { |
1545 | ValueDecl *VD; |
1546 | if (arg.getKind() == TemplateArgument::Declaration) { |
1547 | VD = arg.getAsDecl(); |
1548 | |
1549 | |
1550 | |
1551 | VD = cast_or_null<ValueDecl>( |
1552 | getSema().FindInstantiatedDecl(loc, VD, TemplateArgs)); |
1553 | if (!VD) |
1554 | return ExprError(); |
1555 | } else { |
1556 | |
1557 | VD = nullptr; |
1558 | } |
1559 | |
1560 | QualType paramType = VD ? arg.getParamTypeForDecl() : arg.getNullPtrType(); |
1561 | assert(!paramType.isNull() && "type substitution failed for param type"); |
1562 | assert(!paramType->isDependentType() && "param type still dependent"); |
1563 | result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, paramType, loc); |
1564 | refParam = paramType->isReferenceType(); |
1565 | } else { |
1566 | result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc); |
1567 | assert(result.isInvalid() || |
1568 | SemaRef.Context.hasSameType(result.get()->getType(), |
1569 | arg.getIntegralType())); |
1570 | } |
1571 | |
1572 | if (result.isInvalid()) |
1573 | return ExprError(); |
1574 | |
1575 | Expr *resultExpr = result.get(); |
1576 | return new (SemaRef.Context) SubstNonTypeTemplateParmExpr( |
1577 | resultExpr->getType(), resultExpr->getValueKind(), loc, parm, refParam, |
1578 | resultExpr); |
1579 | } |
1580 | |
1581 | ExprResult |
1582 | TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr( |
1583 | SubstNonTypeTemplateParmPackExpr *E) { |
1584 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1585 | |
1586 | return E; |
1587 | } |
1588 | |
1589 | TemplateArgument Arg = E->getArgumentPack(); |
1590 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1591 | return transformNonTypeTemplateParmRef(E->getParameterPack(), |
1592 | E->getParameterPackLocation(), |
1593 | Arg); |
1594 | } |
1595 | |
1596 | ExprResult |
1597 | TemplateInstantiator::TransformSubstNonTypeTemplateParmExpr( |
1598 | SubstNonTypeTemplateParmExpr *E) { |
1599 | ExprResult SubstReplacement = E->getReplacement(); |
1600 | if (!isa<ConstantExpr>(SubstReplacement.get())) |
1601 | SubstReplacement = TransformExpr(E->getReplacement()); |
1602 | if (SubstReplacement.isInvalid()) |
1603 | return true; |
1604 | QualType SubstType = TransformType(E->getParameterType(getSema().Context)); |
1605 | if (SubstType.isNull()) |
1606 | return true; |
1607 | |
1608 | |
1609 | |
1610 | |
1611 | |
1612 | |
1613 | |
1614 | |
1615 | |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | |
1621 | |
1622 | |
1623 | |
1624 | |
1625 | |
1626 | |
1627 | TemplateArgument Converted; |
1628 | if (SemaRef.CheckTemplateArgument(E->getParameter(), SubstType, |
1629 | SubstReplacement.get(), |
1630 | Converted).isInvalid()) |
1631 | return true; |
1632 | return transformNonTypeTemplateParmRef(E->getParameter(), |
1633 | E->getExprLoc(), Converted); |
1634 | } |
1635 | |
1636 | ExprResult TemplateInstantiator::RebuildVarDeclRefExpr(VarDecl *PD, |
1637 | SourceLocation Loc) { |
1638 | DeclarationNameInfo NameInfo(PD->getDeclName(), Loc); |
1639 | return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD); |
1640 | } |
1641 | |
1642 | ExprResult |
1643 | TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) { |
1644 | if (getSema().ArgumentPackSubstitutionIndex != -1) { |
1645 | |
1646 | VarDecl *D = E->getExpansion(getSema().ArgumentPackSubstitutionIndex); |
1647 | VarDecl *VD = cast_or_null<VarDecl>(TransformDecl(E->getExprLoc(), D)); |
1648 | if (!VD) |
1649 | return ExprError(); |
1650 | return RebuildVarDeclRefExpr(VD, E->getExprLoc()); |
1651 | } |
1652 | |
1653 | QualType T = TransformType(E->getType()); |
1654 | if (T.isNull()) |
1655 | return ExprError(); |
1656 | |
1657 | |
1658 | |
1659 | SmallVector<VarDecl *, 8> Vars; |
1660 | Vars.reserve(E->getNumExpansions()); |
1661 | for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end(); |
1662 | I != End; ++I) { |
1663 | VarDecl *D = cast_or_null<VarDecl>(TransformDecl(E->getExprLoc(), *I)); |
1664 | if (!D) |
1665 | return ExprError(); |
1666 | Vars.push_back(D); |
1667 | } |
1668 | |
1669 | auto *PackExpr = |
1670 | FunctionParmPackExpr::Create(getSema().Context, T, E->getParameterPack(), |
1671 | E->getParameterPackLocation(), Vars); |
1672 | getSema().MarkFunctionParmPackReferenced(PackExpr); |
1673 | return PackExpr; |
1674 | } |
1675 | |
1676 | ExprResult |
1677 | TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E, |
1678 | VarDecl *PD) { |
1679 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
1680 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
1681 | = getSema().CurrentInstantiationScope->findInstantiationOf(PD); |
1682 | assert(Found && "no instantiation for parameter pack"); |
1683 | |
1684 | Decl *TransformedDecl; |
1685 | if (DeclArgumentPack *Pack = Found->dyn_cast<DeclArgumentPack *>()) { |
1686 | |
1687 | |
1688 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1689 | QualType T = TransformType(E->getType()); |
1690 | if (T.isNull()) |
1691 | return ExprError(); |
1692 | auto *PackExpr = FunctionParmPackExpr::Create(getSema().Context, T, PD, |
1693 | E->getExprLoc(), *Pack); |
1694 | getSema().MarkFunctionParmPackReferenced(PackExpr); |
1695 | return PackExpr; |
1696 | } |
1697 | |
1698 | TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex]; |
1699 | } else { |
1700 | TransformedDecl = Found->get<Decl*>(); |
1701 | } |
1702 | |
1703 | |
1704 | return RebuildVarDeclRefExpr(cast<VarDecl>(TransformedDecl), E->getExprLoc()); |
1705 | } |
1706 | |
1707 | ExprResult |
1708 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
1709 | NamedDecl *D = E->getDecl(); |
1710 | |
1711 | |
1712 | |
1713 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
1714 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
1715 | return TransformTemplateParmRefExpr(E, NTTP); |
1716 | |
1717 | |
1718 | |
1719 | } |
1720 | |
1721 | |
1722 | if (VarDecl *PD = dyn_cast<VarDecl>(D)) |
1723 | if (PD->isParameterPack()) |
1724 | return TransformFunctionParmPackRefExpr(E, PD); |
1725 | |
1726 | return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); |
1727 | } |
1728 | |
1729 | ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
1730 | CXXDefaultArgExpr *E) { |
1731 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
1732 | getDescribedFunctionTemplate() && |
1733 | "Default arg expressions are never formed in dependent cases."); |
1734 | return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), |
1735 | cast<FunctionDecl>(E->getParam()->getDeclContext()), |
1736 | E->getParam()); |
1737 | } |
1738 | |
1739 | template<typename Fn> |
1740 | QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
1741 | FunctionProtoTypeLoc TL, |
1742 | CXXRecordDecl *ThisContext, |
1743 | Qualifiers ThisTypeQuals, |
1744 | Fn TransformExceptionSpec) { |
1745 | |
1746 | LocalInstantiationScope Scope(SemaRef, true); |
1747 | return inherited::TransformFunctionProtoType( |
1748 | TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec); |
1749 | } |
1750 | |
1751 | ParmVarDecl * |
1752 | TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm, |
1753 | int indexAdjustment, |
1754 | Optional<unsigned> NumExpansions, |
1755 | bool ExpectParameterPack) { |
1756 | auto NewParm = |
1757 | SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment, |
1758 | NumExpansions, ExpectParameterPack); |
1759 | if (NewParm && SemaRef.getLangOpts().OpenCL) |
1760 | SemaRef.deduceOpenCLAddressSpace(NewParm); |
1761 | return NewParm; |
1762 | } |
1763 | |
1764 | QualType |
1765 | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
1766 | TemplateTypeParmTypeLoc TL) { |
1767 | const TemplateTypeParmType *T = TL.getTypePtr(); |
1768 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
1769 | |
1770 | |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
1777 | TemplateTypeParmTypeLoc NewTL |
1778 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
1779 | NewTL.setNameLoc(TL.getNameLoc()); |
1780 | return TL.getType(); |
1781 | } |
1782 | |
1783 | TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); |
1784 | |
1785 | if (TemplateArgs.isRewrite()) { |
1786 | |
1787 | |
1788 | if (Arg.getKind() == TemplateArgument::Pack) { |
1789 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
1790 | "unexpected pack arguments in template rewrite"); |
1791 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
1792 | } |
1793 | assert(Arg.getKind() == TemplateArgument::Type && |
1794 | "unexpected nontype template argument kind in template rewrite"); |
1795 | QualType NewT = Arg.getAsType(); |
1796 | assert(isa<TemplateTypeParmType>(NewT) && |
1797 | "type parm not rewritten to type parm"); |
1798 | auto NewTL = TLB.push<TemplateTypeParmTypeLoc>(NewT); |
1799 | NewTL.setNameLoc(TL.getNameLoc()); |
1800 | return NewT; |
1801 | } |
1802 | |
1803 | if (T->isParameterPack()) { |
1804 | assert(Arg.getKind() == TemplateArgument::Pack && |
1805 | "Missing argument pack"); |
1806 | |
1807 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1808 | |
1809 | |
1810 | |
1811 | QualType Result |
1812 | = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg); |
1813 | SubstTemplateTypeParmPackTypeLoc NewTL |
1814 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
1815 | NewTL.setNameLoc(TL.getNameLoc()); |
1816 | return Result; |
1817 | } |
1818 | |
1819 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1820 | } |
1821 | |
1822 | assert(Arg.getKind() == TemplateArgument::Type && |
1823 | "Template argument kind mismatch"); |
1824 | |
1825 | QualType Replacement = Arg.getAsType(); |
1826 | |
1827 | |
1828 | QualType Result |
1829 | = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); |
1830 | SubstTemplateTypeParmTypeLoc NewTL |
1831 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
1832 | NewTL.setNameLoc(TL.getNameLoc()); |
1833 | return Result; |
1834 | } |
1835 | |
1836 | |
1837 | |
1838 | |
1839 | |
1840 | TemplateTypeParmDecl *NewTTPDecl = nullptr; |
1841 | if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl()) |
1842 | NewTTPDecl = cast_or_null<TemplateTypeParmDecl>( |
1843 | TransformDecl(TL.getNameLoc(), OldTTPDecl)); |
1844 | |
1845 | QualType Result = getSema().Context.getTemplateTypeParmType( |
1846 | T->getDepth() - TemplateArgs.getNumSubstitutedLevels(), T->getIndex(), |
1847 | T->isParameterPack(), NewTTPDecl); |
1848 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
1849 | NewTL.setNameLoc(TL.getNameLoc()); |
1850 | return Result; |
1851 | } |
1852 | |
1853 | QualType |
1854 | TemplateInstantiator::TransformSubstTemplateTypeParmPackType( |
1855 | TypeLocBuilder &TLB, |
1856 | SubstTemplateTypeParmPackTypeLoc TL) { |
1857 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1858 | |
1859 | SubstTemplateTypeParmPackTypeLoc NewTL |
1860 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType()); |
1861 | NewTL.setNameLoc(TL.getNameLoc()); |
1862 | return TL.getType(); |
1863 | } |
1864 | |
1865 | TemplateArgument Arg = TL.getTypePtr()->getArgumentPack(); |
1866 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1867 | QualType Result = Arg.getAsType(); |
1868 | |
1869 | Result = getSema().Context.getSubstTemplateTypeParmType( |
1870 | TL.getTypePtr()->getReplacedParameter(), |
1871 | Result); |
1872 | SubstTemplateTypeParmTypeLoc NewTL |
1873 | = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
1874 | NewTL.setNameLoc(TL.getNameLoc()); |
1875 | return Result; |
1876 | } |
1877 | |
1878 | template<typename EntityPrinter> |
1879 | static concepts::Requirement::SubstitutionDiagnostic * |
1880 | createSubstDiag(Sema &S, TemplateDeductionInfo &Info, EntityPrinter Printer) { |
1881 | SmallString<128> Message; |
1882 | SourceLocation ErrorLoc; |
1883 | if (Info.hasSFINAEDiagnostic()) { |
1884 | PartialDiagnosticAt PDA(SourceLocation(), |
1885 | PartialDiagnostic::NullDiagnostic{}); |
1886 | Info.takeSFINAEDiagnostic(PDA); |
1887 | PDA.second.EmitToString(S.getDiagnostics(), Message); |
1888 | ErrorLoc = PDA.first; |
1889 | } else { |
1890 | ErrorLoc = Info.getLocation(); |
1891 | } |
1892 | char *MessageBuf = new (S.Context) char[Message.size()]; |
1893 | std::copy(Message.begin(), Message.end(), MessageBuf); |
1894 | SmallString<128> Entity; |
1895 | llvm::raw_svector_ostream OS(Entity); |
1896 | Printer(OS); |
1897 | char *EntityBuf = new (S.Context) char[Entity.size()]; |
1898 | std::copy(Entity.begin(), Entity.end(), EntityBuf); |
1899 | return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{ |
1900 | StringRef(EntityBuf, Entity.size()), ErrorLoc, |
1901 | StringRef(MessageBuf, Message.size())}; |
1902 | } |
1903 | |
1904 | concepts::TypeRequirement * |
1905 | TemplateInstantiator::TransformTypeRequirement(concepts::TypeRequirement *Req) { |
1906 | if (!Req->isDependent() && !AlwaysRebuild()) |
1907 | return Req; |
1908 | if (Req->isSubstitutionFailure()) { |
1909 | if (AlwaysRebuild()) |
1910 | return RebuildTypeRequirement( |
1911 | Req->getSubstitutionDiagnostic()); |
1912 | return Req; |
1913 | } |
1914 | |
1915 | Sema::SFINAETrap Trap(SemaRef); |
1916 | TemplateDeductionInfo Info(Req->getType()->getTypeLoc().getBeginLoc()); |
1917 | Sema::InstantiatingTemplate TypeInst(SemaRef, |
1918 | Req->getType()->getTypeLoc().getBeginLoc(), Req, Info, |
1919 | Req->getType()->getTypeLoc().getSourceRange()); |
1920 | if (TypeInst.isInvalid()) |
1921 | return nullptr; |
1922 | TypeSourceInfo *TransType = TransformType(Req->getType()); |
1923 | if (!TransType || Trap.hasErrorOccurred()) |
1924 | return RebuildTypeRequirement(createSubstDiag(SemaRef, Info, |
1925 | [&] (llvm::raw_ostream& OS) { |
1926 | Req->getType()->getType().print(OS, SemaRef.getPrintingPolicy()); |
1927 | })); |
1928 | return RebuildTypeRequirement(TransType); |
1929 | } |
1930 | |
1931 | concepts::ExprRequirement * |
1932 | TemplateInstantiator::TransformExprRequirement(concepts::ExprRequirement *Req) { |
1933 | if (!Req->isDependent() && !AlwaysRebuild()) |
1934 | return Req; |
1935 | |
1936 | Sema::SFINAETrap Trap(SemaRef); |
1937 | |
1938 | llvm::PointerUnion<Expr *, concepts::Requirement::SubstitutionDiagnostic *> |
1939 | TransExpr; |
1940 | if (Req->isExprSubstitutionFailure()) |
1941 | TransExpr = Req->getExprSubstitutionDiagnostic(); |
1942 | else { |
1943 | Expr *E = Req->getExpr(); |
1944 | TemplateDeductionInfo Info(E->getBeginLoc()); |
1945 | Sema::InstantiatingTemplate ExprInst(SemaRef, E->getBeginLoc(), Req, Info, |
1946 | E->getSourceRange()); |
1947 | if (ExprInst.isInvalid()) |
1948 | return nullptr; |
1949 | ExprResult TransExprRes = TransformExpr(E); |
1950 | if (TransExprRes.isInvalid() || Trap.hasErrorOccurred()) |
1951 | TransExpr = createSubstDiag(SemaRef, Info, [&](llvm::raw_ostream &OS) { |
1952 | E->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
1953 | }); |
1954 | else |
1955 | TransExpr = TransExprRes.get(); |
1956 | } |
1957 | |
1958 | llvm::Optional<concepts::ExprRequirement::ReturnTypeRequirement> TransRetReq; |
1959 | const auto &RetReq = Req->getReturnTypeRequirement(); |
1960 | if (RetReq.isEmpty()) |
1961 | TransRetReq.emplace(); |
1962 | else if (RetReq.isSubstitutionFailure()) |
1963 | TransRetReq.emplace(RetReq.getSubstitutionDiagnostic()); |
1964 | else if (RetReq.isTypeConstraint()) { |
1965 | TemplateParameterList *OrigTPL = |
1966 | RetReq.getTypeConstraintTemplateParameterList(); |
1967 | TemplateDeductionInfo Info(OrigTPL->getTemplateLoc()); |
1968 | Sema::InstantiatingTemplate TPLInst(SemaRef, OrigTPL->getTemplateLoc(), |
1969 | Req, Info, OrigTPL->getSourceRange()); |
1970 | if (TPLInst.isInvalid()) |
1971 | return nullptr; |
1972 | TemplateParameterList *TPL = |
1973 | TransformTemplateParameterList(OrigTPL); |
1974 | if (!TPL) |
1975 | TransRetReq.emplace(createSubstDiag(SemaRef, Info, |
1976 | [&] (llvm::raw_ostream& OS) { |
1977 | RetReq.getTypeConstraint()->getImmediatelyDeclaredConstraint() |
1978 | ->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
1979 | })); |
1980 | else { |
1981 | TPLInst.Clear(); |
1982 | TransRetReq.emplace(TPL); |
1983 | } |
1984 | } |
1985 | assert(TransRetReq.hasValue() && |
1986 | "All code paths leading here must set TransRetReq"); |
1987 | if (Expr *E = TransExpr.dyn_cast<Expr *>()) |
1988 | return RebuildExprRequirement(E, Req->isSimple(), Req->getNoexceptLoc(), |
1989 | std::move(*TransRetReq)); |
1990 | return RebuildExprRequirement( |
1991 | TransExpr.get<concepts::Requirement::SubstitutionDiagnostic *>(), |
1992 | Req->isSimple(), Req->getNoexceptLoc(), std::move(*TransRetReq)); |
1993 | } |
1994 | |
1995 | concepts::NestedRequirement * |
1996 | TemplateInstantiator::TransformNestedRequirement( |
1997 | concepts::NestedRequirement *Req) { |
1998 | if (!Req->isDependent() && !AlwaysRebuild()) |
1999 | return Req; |
2000 | if (Req->isSubstitutionFailure()) { |
2001 | if (AlwaysRebuild()) |
2002 | return RebuildNestedRequirement( |
2003 | Req->getSubstitutionDiagnostic()); |
2004 | return Req; |
2005 | } |
2006 | Sema::InstantiatingTemplate ReqInst(SemaRef, |
2007 | Req->getConstraintExpr()->getBeginLoc(), Req, |
2008 | Sema::InstantiatingTemplate::ConstraintsCheck{}, |
2009 | Req->getConstraintExpr()->getSourceRange()); |
2010 | |
2011 | ExprResult TransConstraint; |
2012 | TemplateDeductionInfo Info(Req->getConstraintExpr()->getBeginLoc()); |
2013 | { |
2014 | EnterExpressionEvaluationContext ContextRAII( |
2015 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
2016 | Sema::SFINAETrap Trap(SemaRef); |
2017 | Sema::InstantiatingTemplate ConstrInst(SemaRef, |
2018 | Req->getConstraintExpr()->getBeginLoc(), Req, Info, |
2019 | Req->getConstraintExpr()->getSourceRange()); |
2020 | if (ConstrInst.isInvalid()) |
2021 | return nullptr; |
2022 | TransConstraint = TransformExpr(Req->getConstraintExpr()); |
2023 | if (TransConstraint.isInvalid() || Trap.hasErrorOccurred()) |
2024 | return RebuildNestedRequirement(createSubstDiag(SemaRef, Info, |
2025 | [&] (llvm::raw_ostream& OS) { |
2026 | Req->getConstraintExpr()->printPretty(OS, nullptr, |
2027 | SemaRef.getPrintingPolicy()); |
2028 | })); |
2029 | } |
2030 | return RebuildNestedRequirement(TransConstraint.get()); |
2031 | } |
2032 | |
2033 | |
2034 | |
2035 | |
2036 | |
2037 | |
2038 | |
2039 | |
2040 | |
2041 | |
2042 | |
2043 | |
2044 | |
2045 | |
2046 | |
2047 | |
2048 | |
2049 | |
2050 | |
2051 | |
2052 | |
2053 | |
2054 | |
2055 | |
2056 | |
2057 | |
2058 | |
2059 | |
2060 | |
2061 | |
2062 | |
2063 | |
2064 | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
2065 | const MultiLevelTemplateArgumentList &Args, |
2066 | SourceLocation Loc, |
2067 | DeclarationName Entity, |
2068 | bool AllowDeducedTST) { |
2069 | assert(!CodeSynthesisContexts.empty() && |
2070 | "Cannot perform an instantiation without some context on the " |
2071 | "instantiation stack"); |
2072 | |
2073 | if (!T->getType()->isInstantiationDependentType() && |
2074 | !T->getType()->isVariablyModifiedType()) |
2075 | return T; |
2076 | |
2077 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
2078 | return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(T) |
2079 | : Instantiator.TransformType(T); |
2080 | } |
2081 | |
2082 | TypeSourceInfo *Sema::SubstType(TypeLoc TL, |
2083 | const MultiLevelTemplateArgumentList &Args, |
2084 | SourceLocation Loc, |
2085 | DeclarationName Entity) { |
2086 | assert(!CodeSynthesisContexts.empty() && |
2087 | "Cannot perform an instantiation without some context on the " |
2088 | "instantiation stack"); |
2089 | |
2090 | if (TL.getType().isNull()) |
2091 | return nullptr; |
2092 | |
2093 | if (!TL.getType()->isInstantiationDependentType() && |
2094 | !TL.getType()->isVariablyModifiedType()) { |
2095 | |
2096 | |
2097 | TypeLocBuilder TLB; |
2098 | TLB.pushFullCopy(TL); |
2099 | return TLB.getTypeSourceInfo(Context, TL.getType()); |
2100 | } |
2101 | |
2102 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
2103 | TypeLocBuilder TLB; |
2104 | TLB.reserve(TL.getFullDataSize()); |
2105 | QualType Result = Instantiator.TransformType(TLB, TL); |
2106 | if (Result.isNull()) |
2107 | return nullptr; |
2108 | |
2109 | return TLB.getTypeSourceInfo(Context, Result); |
2110 | } |
2111 | |
2112 | |
2113 | QualType Sema::SubstType(QualType T, |
2114 | const MultiLevelTemplateArgumentList &TemplateArgs, |
2115 | SourceLocation Loc, DeclarationName Entity) { |
2116 | assert(!CodeSynthesisContexts.empty() && |
2117 | "Cannot perform an instantiation without some context on the " |
2118 | "instantiation stack"); |
2119 | |
2120 | |
2121 | |
2122 | if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType()) |
2123 | return T; |
2124 | |
2125 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
2126 | return Instantiator.TransformType(T); |
2127 | } |
2128 | |
2129 | static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
2130 | if (T->getType()->isInstantiationDependentType() || |
2131 | T->getType()->isVariablyModifiedType()) |
2132 | return true; |
2133 | |
2134 | TypeLoc TL = T->getTypeLoc().IgnoreParens(); |
2135 | if (!TL.getAs<FunctionProtoTypeLoc>()) |
2136 | return false; |
2137 | |
2138 | FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>(); |
2139 | for (ParmVarDecl *P : FP.getParams()) { |
2140 | |
2141 | if (!P) continue; |
2142 | |
2143 | |
2144 | |
2145 | return true; |
2146 | } |
2147 | |
2148 | return false; |
2149 | } |
2150 | |
2151 | |
2152 | |
2153 | |
2154 | |
2155 | TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
2156 | const MultiLevelTemplateArgumentList &Args, |
2157 | SourceLocation Loc, |
2158 | DeclarationName Entity, |
2159 | CXXRecordDecl *ThisContext, |
2160 | Qualifiers ThisTypeQuals) { |
2161 | assert(!CodeSynthesisContexts.empty() && |
2162 | "Cannot perform an instantiation without some context on the " |
2163 | "instantiation stack"); |
2164 | |
2165 | if (!NeedsInstantiationAsFunctionType(T)) |
2166 | return T; |
2167 | |
2168 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
2169 | |
2170 | TypeLocBuilder TLB; |
2171 | |
2172 | TypeLoc TL = T->getTypeLoc(); |
2173 | TLB.reserve(TL.getFullDataSize()); |
2174 | |
2175 | QualType Result; |
2176 | |
2177 | if (FunctionProtoTypeLoc Proto = |
2178 | TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | |
2184 | Result = Instantiator.TransformFunctionProtoType( |
2185 | TLB, Proto, ThisContext, ThisTypeQuals, |
2186 | [](FunctionProtoType::ExceptionSpecInfo &ESI, |
2187 | bool &Changed) { return false; }); |
2188 | } else { |
2189 | Result = Instantiator.TransformType(TLB, TL); |
2190 | } |
2191 | if (Result.isNull()) |
2192 | return nullptr; |
2193 | |
2194 | return TLB.getTypeSourceInfo(Context, Result); |
2195 | } |
2196 | |
2197 | bool Sema::SubstExceptionSpec(SourceLocation Loc, |
2198 | FunctionProtoType::ExceptionSpecInfo &ESI, |
2199 | SmallVectorImpl<QualType> &ExceptionStorage, |
2200 | const MultiLevelTemplateArgumentList &Args) { |
2201 | assert(ESI.Type != EST_Uninstantiated); |
2202 | |
2203 | bool Changed = false; |
2204 | TemplateInstantiator Instantiator(*this, Args, Loc, DeclarationName()); |
2205 | return Instantiator.TransformExceptionSpec(Loc, ESI, ExceptionStorage, |
2206 | Changed); |
2207 | } |
2208 | |
2209 | void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto, |
2210 | const MultiLevelTemplateArgumentList &Args) { |
2211 | FunctionProtoType::ExceptionSpecInfo ESI = |
2212 | Proto->getExtProtoInfo().ExceptionSpec; |
2213 | |
2214 | SmallVector<QualType, 4> ExceptionStorage; |
2215 | if (SubstExceptionSpec(New->getTypeSourceInfo()->getTypeLoc().getEndLoc(), |
2216 | ESI, ExceptionStorage, Args)) |
2217 | |
2218 | ESI.Type = EST_None; |
2219 | |
2220 | UpdateExceptionSpec(New, ESI); |
2221 | } |
2222 | |
2223 | namespace { |
2224 | |
2225 | struct GetContainedInventedTypeParmVisitor : |
2226 | public TypeVisitor<GetContainedInventedTypeParmVisitor, |
2227 | TemplateTypeParmDecl *> { |
2228 | using TypeVisitor<GetContainedInventedTypeParmVisitor, |
2229 | TemplateTypeParmDecl *>::Visit; |
2230 | |
2231 | TemplateTypeParmDecl *Visit(QualType T) { |
2232 | if (T.isNull()) |
2233 | return nullptr; |
2234 | return Visit(T.getTypePtr()); |
2235 | } |
2236 | |
2237 | TemplateTypeParmDecl *VisitTemplateTypeParmType( |
2238 | const TemplateTypeParmType *T) { |
2239 | if (!T->getDecl() || !T->getDecl()->isImplicit()) |
2240 | return nullptr; |
2241 | return T->getDecl(); |
2242 | } |
2243 | |
2244 | |
2245 | |
2246 | |
2247 | TemplateTypeParmDecl *VisitElaboratedType(const ElaboratedType *T) { |
2248 | return Visit(T->getNamedType()); |
2249 | } |
2250 | |
2251 | TemplateTypeParmDecl *VisitPointerType(const PointerType *T) { |
2252 | return Visit(T->getPointeeType()); |
2253 | } |
2254 | |
2255 | TemplateTypeParmDecl *VisitBlockPointerType(const BlockPointerType *T) { |
2256 | return Visit(T->getPointeeType()); |
2257 | } |
2258 | |
2259 | TemplateTypeParmDecl *VisitReferenceType(const ReferenceType *T) { |
2260 | return Visit(T->getPointeeTypeAsWritten()); |
2261 | } |
2262 | |
2263 | TemplateTypeParmDecl *VisitMemberPointerType(const MemberPointerType *T) { |
2264 | return Visit(T->getPointeeType()); |
2265 | } |
2266 | |
2267 | TemplateTypeParmDecl *VisitArrayType(const ArrayType *T) { |
2268 | return Visit(T->getElementType()); |
2269 | } |
2270 | |
2271 | TemplateTypeParmDecl *VisitDependentSizedExtVectorType( |
2272 | const DependentSizedExtVectorType *T) { |
2273 | return Visit(T->getElementType()); |
2274 | } |
2275 | |
2276 | TemplateTypeParmDecl *VisitVectorType(const VectorType *T) { |
2277 | return Visit(T->getElementType()); |
2278 | } |
2279 | |
2280 | TemplateTypeParmDecl *VisitFunctionProtoType(const FunctionProtoType *T) { |
2281 | return VisitFunctionType(T); |
2282 | } |
2283 | |
2284 | TemplateTypeParmDecl *VisitFunctionType(const FunctionType *T) { |
2285 | return Visit(T->getReturnType()); |
2286 | } |
2287 | |
2288 | TemplateTypeParmDecl *VisitParenType(const ParenType *T) { |
2289 | return Visit(T->getInnerType()); |
2290 | } |
2291 | |
2292 | TemplateTypeParmDecl *VisitAttributedType(const AttributedType *T) { |
2293 | return Visit(T->getModifiedType()); |
2294 | } |
2295 | |
2296 | TemplateTypeParmDecl *VisitMacroQualifiedType(const MacroQualifiedType *T) { |
2297 | return Visit(T->getUnderlyingType()); |
2298 | } |
2299 | |
2300 | TemplateTypeParmDecl *VisitAdjustedType(const AdjustedType *T) { |
2301 | return Visit(T->getOriginalType()); |
2302 | } |
2303 | |
2304 | TemplateTypeParmDecl *VisitPackExpansionType(const PackExpansionType *T) { |
2305 | return Visit(T->getPattern()); |
2306 | } |
2307 | }; |
2308 | |
2309 | } |
2310 | |
2311 | ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, |
2312 | const MultiLevelTemplateArgumentList &TemplateArgs, |
2313 | int indexAdjustment, |
2314 | Optional<unsigned> NumExpansions, |
2315 | bool ExpectParameterPack) { |
2316 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); |
2317 | TypeSourceInfo *NewDI = nullptr; |
2318 | |
2319 | TypeLoc OldTL = OldDI->getTypeLoc(); |
2320 | if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) { |
2321 | |
2322 | |
2323 | |
2324 | NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, |
2325 | OldParm->getLocation(), OldParm->getDeclName()); |
2326 | if (!NewDI) |
2327 | return nullptr; |
2328 | |
2329 | if (NewDI->getType()->containsUnexpandedParameterPack()) { |
2330 | |
2331 | |
2332 | |
2333 | NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(), |
2334 | NumExpansions); |
2335 | } else if (ExpectParameterPack) { |
2336 | |
2337 | |
2338 | |
2339 | |
2340 | Diag(OldParm->getLocation(), |
2341 | diag::err_function_parameter_pack_without_parameter_packs) |
2342 | << NewDI->getType(); |
2343 | return nullptr; |
2344 | } |
2345 | } else { |
2346 | NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), |
2347 | OldParm->getDeclName()); |
2348 | } |
2349 | |
2350 | if (!NewDI) |
2351 | return nullptr; |
2352 | |
2353 | if (NewDI->getType()->isVoidType()) { |
2354 | Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
2355 | return nullptr; |
2356 | } |
2357 | |
2358 | |
2359 | |
2360 | |
2361 | |
2362 | |
2363 | |
2364 | if (TemplateTypeParmDecl *TTP = |
2365 | GetContainedInventedTypeParmVisitor().Visit(OldDI->getType())) { |
2366 | if (const TypeConstraint *TC = TTP->getTypeConstraint()) { |
2367 | auto *Inst = cast_or_null<TemplateTypeParmDecl>( |
2368 | FindInstantiatedDecl(TTP->getLocation(), TTP, TemplateArgs)); |
2369 | |
2370 | |
2371 | |
2372 | if (Inst && !Inst->getTypeConstraint()) { |
2373 | |
2374 | |
2375 | const ASTTemplateArgumentListInfo *TemplArgInfo |
2376 | = TC->getTemplateArgsAsWritten(); |
2377 | TemplateArgumentListInfo InstArgs; |
2378 | |
2379 | if (TemplArgInfo) { |
2380 | InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc); |
2381 | InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc); |
2382 | if (Subst(TemplArgInfo->getTemplateArgs(), |
2383 | TemplArgInfo->NumTemplateArgs, InstArgs, TemplateArgs)) |
2384 | return nullptr; |
2385 | } |
2386 | if (AttachTypeConstraint( |
2387 | TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), |
2388 | TC->getNamedConcept(), TemplArgInfo ? &InstArgs : nullptr, Inst, |
2389 | TTP->isParameterPack() |
2390 | ? cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint()) |
2391 | ->getEllipsisLoc() |
2392 | : SourceLocation())) |
2393 | return nullptr; |
2394 | } |
2395 | } |
2396 | } |
2397 | |
2398 | ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), |
2399 | OldParm->getInnerLocStart(), |
2400 | OldParm->getLocation(), |
2401 | OldParm->getIdentifier(), |
2402 | NewDI->getType(), NewDI, |
2403 | OldParm->getStorageClass()); |
2404 | if (!NewParm) |
2405 | return nullptr; |
2406 | |
2407 | |
2408 | if (OldParm->hasUninstantiatedDefaultArg()) { |
2409 | Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
2410 | NewParm->setUninstantiatedDefaultArg(Arg); |
2411 | } else if (OldParm->hasUnparsedDefaultArg()) { |
2412 | NewParm->setUnparsedDefaultArg(); |
2413 | UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); |
2414 | } else if (Expr *Arg = OldParm->getDefaultArg()) { |
2415 | FunctionDecl *OwningFunc = cast<FunctionDecl>(OldParm->getDeclContext()); |
2416 | if (OwningFunc->isInLocalScopeForInstantiation()) { |
2417 | |
2418 | |
2419 | Sema::ContextRAII SavedContext(*this, OwningFunc); |
2420 | LocalInstantiationScope Local(*this, true); |
2421 | ExprResult NewArg = SubstExpr(Arg, TemplateArgs); |
2422 | if (NewArg.isUsable()) { |
2423 | |
2424 | SourceLocation EqualLoc = NewArg.get()->getBeginLoc(); |
2425 | ExprResult Result = |
2426 | ConvertParamDefaultArgument(NewParm, NewArg.get(), EqualLoc); |
2427 | if (Result.isInvalid()) |
2428 | return nullptr; |
2429 | |
2430 | SetParamDefaultArgument(NewParm, Result.getAs<Expr>(), EqualLoc); |
2431 | } |
2432 | } else { |
2433 | |
2434 | |
2435 | |
2436 | NewParm->setUninstantiatedDefaultArg(Arg); |
2437 | } |
2438 | } |
2439 | |
2440 | NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
2441 | |
2442 | if (OldParm->isParameterPack() && !NewParm->isParameterPack()) { |
2443 | |
2444 | CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm); |
2445 | } else { |
2446 | |
2447 | CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
2448 | } |
2449 | |
2450 | |
2451 | |
2452 | NewParm->setDeclContext(CurContext); |
2453 | |
2454 | NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(), |
2455 | OldParm->getFunctionScopeIndex() + indexAdjustment); |
2456 | |
2457 | InstantiateAttrs(TemplateArgs, OldParm, NewParm); |
2458 | |
2459 | return NewParm; |
2460 | } |
2461 | |
2462 | |
2463 | |
2464 | |
2465 | bool Sema::SubstParmTypes( |
2466 | SourceLocation Loc, ArrayRef<ParmVarDecl *> Params, |
2467 | const FunctionProtoType::ExtParameterInfo *ExtParamInfos, |
2468 | const MultiLevelTemplateArgumentList &TemplateArgs, |
2469 | SmallVectorImpl<QualType> &ParamTypes, |
2470 | SmallVectorImpl<ParmVarDecl *> *OutParams, |
2471 | ExtParameterInfoBuilder &ParamInfos) { |
2472 | assert(!CodeSynthesisContexts.empty() && |
2473 | "Cannot perform an instantiation without some context on the " |
2474 | "instantiation stack"); |
2475 | |
2476 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
2477 | DeclarationName()); |
2478 | return Instantiator.TransformFunctionTypeParams( |
2479 | Loc, Params, nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos); |
2480 | } |
2481 | |
2482 | |
2483 | |
2484 | |
2485 | |
2486 | |
2487 | |
2488 | bool |
2489 | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
2490 | CXXRecordDecl *Pattern, |
2491 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
2492 | bool Invalid = false; |
2493 | SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
2494 | for (const auto &Base : Pattern->bases()) { |
2495 | if (!Base.getType()->isDependentType()) { |
2496 | if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) { |
2497 | if (RD->isInvalidDecl()) |
2498 | Instantiation->setInvalidDecl(); |
2499 | } |
2500 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base)); |
2501 | continue; |
2502 | } |
2503 | |
2504 | SourceLocation EllipsisLoc; |
2505 | TypeSourceInfo *BaseTypeLoc; |
2506 | if (Base.isPackExpansion()) { |
2507 | |
2508 | |
2509 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
2510 | collectUnexpandedParameterPacks(Base.getTypeSourceInfo()->getTypeLoc(), |
2511 | Unexpanded); |
2512 | bool ShouldExpand = false; |
2513 | bool RetainExpansion = false; |
2514 | Optional<unsigned> NumExpansions; |
2515 | if (CheckParameterPacksForExpansion(Base.getEllipsisLoc(), |
2516 | Base.getSourceRange(), |
2517 | Unexpanded, |
2518 | TemplateArgs, ShouldExpand, |
2519 | RetainExpansion, |
2520 | NumExpansions)) { |
2521 | Invalid = true; |
2522 | continue; |
2523 | } |
2524 | |
2525 | |
2526 | if (ShouldExpand) { |
2527 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
2528 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
2529 | |
2530 | TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
2531 | TemplateArgs, |
2532 | Base.getSourceRange().getBegin(), |
2533 | DeclarationName()); |
2534 | if (!BaseTypeLoc) { |
2535 | Invalid = true; |
2536 | continue; |
2537 | } |
2538 | |
2539 | if (CXXBaseSpecifier *InstantiatedBase |
2540 | = CheckBaseSpecifier(Instantiation, |
2541 | Base.getSourceRange(), |
2542 | Base.isVirtual(), |
2543 | Base.getAccessSpecifierAsWritten(), |
2544 | BaseTypeLoc, |
2545 | SourceLocation())) |
2546 | InstantiatedBases.push_back(InstantiatedBase); |
2547 | else |
2548 | Invalid = true; |
2549 | } |
2550 | |
2551 | continue; |
2552 | } |
2553 | |
2554 | |
2555 | EllipsisLoc = Base.getEllipsisLoc(); |
2556 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
2557 | BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
2558 | TemplateArgs, |
2559 | Base.getSourceRange().getBegin(), |
2560 | DeclarationName()); |
2561 | } else { |
2562 | BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
2563 | TemplateArgs, |
2564 | Base.getSourceRange().getBegin(), |
2565 | DeclarationName()); |
2566 | } |
2567 | |
2568 | if (!BaseTypeLoc) { |
2569 | Invalid = true; |
2570 | continue; |
2571 | } |
2572 | |
2573 | if (CXXBaseSpecifier *InstantiatedBase |
2574 | = CheckBaseSpecifier(Instantiation, |
2575 | Base.getSourceRange(), |
2576 | Base.isVirtual(), |
2577 | Base.getAccessSpecifierAsWritten(), |
2578 | BaseTypeLoc, |
2579 | EllipsisLoc)) |
2580 | InstantiatedBases.push_back(InstantiatedBase); |
2581 | else |
2582 | Invalid = true; |
2583 | } |
2584 | |
2585 | if (!Invalid && AttachBaseSpecifiers(Instantiation, InstantiatedBases)) |
2586 | Invalid = true; |
2587 | |
2588 | return Invalid; |
2589 | } |
2590 | |
2591 | |
2592 | namespace clang { |
2593 | namespace sema { |
2594 | Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S, |
2595 | const MultiLevelTemplateArgumentList &TemplateArgs); |
2596 | Attr *instantiateTemplateAttributeForDecl( |
2597 | const Attr *At, ASTContext &C, Sema &S, |
2598 | const MultiLevelTemplateArgumentList &TemplateArgs); |
2599 | } |
2600 | } |
2601 | |
2602 | |
2603 | |
2604 | |
2605 | |
2606 | |
2607 | |
2608 | |
2609 | |
2610 | |
2611 | |
2612 | |
2613 | |
2614 | |
2615 | |
2616 | |
2617 | |
2618 | |
2619 | |
2620 | |
2621 | |
2622 | |
2623 | |
2624 | bool |
2625 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
2626 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
2627 | const MultiLevelTemplateArgumentList &TemplateArgs, |
2628 | TemplateSpecializationKind TSK, |
2629 | bool Complain) { |
2630 | CXXRecordDecl *PatternDef |
| 8 | | 'PatternDef' initialized to a null pointer value | |
|
2631 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
| 7 | | Assuming null pointer is passed into cast | |
|
2632 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
| 9 | | Assuming the condition is false | |
|
| |
2633 | Instantiation->getInstantiatedFromMemberClass(), |
2634 | Pattern, PatternDef, TSK, Complain)) |
2635 | return true; |
2636 | |
2637 | llvm::TimeTraceScope TimeScope("InstantiateClass", [&]() { |
2638 | std::string Name; |
2639 | llvm::raw_string_ostream OS(Name); |
2640 | Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), |
2641 | true); |
2642 | return Name; |
2643 | }); |
2644 | |
2645 | Pattern = PatternDef; |
| 11 | | Null pointer value stored to 'Pattern' | |
|
2646 | |
2647 | |
2648 | if (MemberSpecializationInfo *MSInfo |
| 12 | | Assuming 'MSInfo' is null | |
|
| |
2649 | = Instantiation->getMemberSpecializationInfo()) { |
2650 | MSInfo->setTemplateSpecializationKind(TSK); |
2651 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
2652 | } else if (ClassTemplateSpecializationDecl *Spec |
| |
2653 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
| 14 | | Assuming 'Instantiation' is not a 'ClassTemplateSpecializationDecl' | |
|
2654 | Spec->setTemplateSpecializationKind(TSK); |
2655 | Spec->setPointOfInstantiation(PointOfInstantiation); |
2656 | } |
2657 | |
2658 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
| 16 | | Calling constructor for 'InstantiatingTemplate' | |
|
| 28 | | Returning from constructor for 'InstantiatingTemplate' | |
|
2659 | if (Inst.isInvalid()) |
| 29 | | Calling 'InstantiatingTemplate::isInvalid' | |
|
| 31 | | Returning from 'InstantiatingTemplate::isInvalid' | |
|
| |
2660 | return true; |
2661 | assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller"); |
2662 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
2663 | "instantiating class definition"); |
2664 | |
2665 | |
2666 | |
2667 | ContextRAII SavedContext(*this, Instantiation); |
2668 | EnterExpressionEvaluationContext EvalContext( |
2669 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
2670 | |
2671 | |
2672 | |
2673 | |
2674 | bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
2675 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
2676 | |
2677 | |
2678 | |
2679 | |
2680 | |
2681 | SavePendingParsedClassStateRAII SavedPendingParsedClassState(*this); |
2682 | |
2683 | |
2684 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
2685 | |
2686 | |
2687 | Instantiation->startDefinition(); |
2688 | |
2689 | |
2690 | |
2691 | Instantiation->setVisibleDespiteOwningModule(); |
2692 | |
2693 | |
2694 | Instantiation->setTagKind(Pattern->getTagKind()); |
| 33 | | Called C++ object pointer is null |
|
2695 | |
2696 | |
2697 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
2698 | Instantiation->setInvalidDecl(); |
2699 | |
2700 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
2701 | SmallVector<Decl*, 4> Fields; |
2702 | |
2703 | LateInstantiatedAttrVec LateAttrs; |
2704 | Instantiator.enableLateAttributeInstantiation(&LateAttrs); |
2705 | |
2706 | bool MightHaveConstexprVirtualFunctions = false; |
2707 | for (auto *Member : Pattern->decls()) { |
2708 | |
2709 | |
2710 | |
2711 | |
2712 | |
2713 | |
2714 | |
2715 | |
2716 | |
2717 | if (Member->getDeclContext() != Pattern) |
2718 | continue; |
2719 | |
2720 | |
2721 | |
2722 | |
2723 | |
2724 | if (isa<BlockDecl>(Member) || |
2725 | (isa<CXXRecordDecl>(Member) && cast<CXXRecordDecl>(Member)->isLambda())) |
2726 | continue; |
2727 | |
2728 | if (Member->isInvalidDecl()) { |
2729 | Instantiation->setInvalidDecl(); |
2730 | continue; |
2731 | } |
2732 | |
2733 | Decl *NewMember = Instantiator.Visit(Member); |
2734 | if (NewMember) { |
2735 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) { |
2736 | Fields.push_back(Field); |
2737 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) { |
2738 | |
2739 | |
2740 | |
2741 | |
2742 | if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() && |
2743 | Enum->isCompleteDefinition()) { |
2744 | MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo(); |
2745 | assert(MSInfo && "no spec info for member enum specialization"); |
2746 | MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation); |
2747 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
2748 | } |
2749 | } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) { |
2750 | if (SA->isFailed()) { |
2751 | |
2752 | |
2753 | Instantiation->setInvalidDecl(); |
2754 | break; |
2755 | } |
2756 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewMember)) { |
2757 | if (MD->isConstexpr() && !MD->getFriendObjectKind() && |
2758 | (MD->isVirtualAsWritten() || Instantiation->getNumBases())) |
2759 | MightHaveConstexprVirtualFunctions = true; |
2760 | } |
2761 | |
2762 | if (NewMember->isInvalidDecl()) |
2763 | Instantiation->setInvalidDecl(); |
2764 | } else { |
2765 | |
2766 | |
2767 | |
2768 | |
2769 | } |
2770 | } |
2771 | |
2772 | |
2773 | ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields, |
2774 | SourceLocation(), SourceLocation(), ParsedAttributesView()); |
2775 | CheckCompletedCXXClass(nullptr, Instantiation); |
2776 | |
2777 | |
2778 | |
2779 | |
2780 | if (ParsingClassDepth == 0) |
2781 | ActOnFinishCXXNonNestedClass(); |
2782 | |
2783 | |
2784 | |
2785 | for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(), |
2786 | E = LateAttrs.end(); I != E; ++I) { |
2787 | assert(CurrentInstantiationScope == Instantiator.getStartingScope()); |
2788 | CurrentInstantiationScope = I->Scope; |
2789 | |
2790 | |
2791 | NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl); |
2792 | CXXRecordDecl *ThisContext = |
2793 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
2794 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
2795 | ND && ND->isCXXInstanceMember()); |
2796 | |
2797 | Attr *NewAttr = |
2798 | instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs); |
2799 | if (NewAttr) |
2800 | I->NewDecl->addAttr(NewAttr); |
2801 | LocalInstantiationScope::deleteScopes(I->Scope, |
2802 | Instantiator.getStartingScope()); |
2803 | } |
2804 | Instantiator.disableLateAttributeInstantiation(); |
2805 | LateAttrs.clear(); |
2806 | |
2807 | ActOnFinishDelayedMemberInitializers(Instantiation); |
2808 | |
2809 | |
2810 | |
2811 | if (TSK == TSK_ImplicitInstantiation) { |
2812 | Instantiation->setLocation(Pattern->getLocation()); |
2813 | Instantiation->setLocStart(Pattern->getInnerLocStart()); |
2814 | Instantiation->setBraceRange(Pattern->getBraceRange()); |
2815 | } |
2816 | |
2817 | if (!Instantiation->isInvalidDecl()) { |
2818 | |
2819 | if (Pattern->isDependentContext()) |
2820 | PerformDependentDiagnostics(Pattern, TemplateArgs); |
2821 | |
2822 | |
2823 | |
2824 | for (TemplateDeclInstantiator::delayed_partial_spec_iterator |
2825 | P = Instantiator.delayed_partial_spec_begin(), |
2826 | PEnd = Instantiator.delayed_partial_spec_end(); |
2827 | P != PEnd; ++P) { |
2828 | if (!Instantiator.InstantiateClassTemplatePartialSpecialization( |
2829 | P->first, P->second)) { |
2830 | Instantiation->setInvalidDecl(); |
2831 | break; |
2832 | } |
2833 | } |
2834 | |
2835 | |
2836 | |
2837 | for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator |
2838 | P = Instantiator.delayed_var_partial_spec_begin(), |
2839 | PEnd = Instantiator.delayed_var_partial_spec_end(); |
2840 | P != PEnd; ++P) { |
2841 | if (!Instantiator.InstantiateVarTemplatePartialSpecialization( |
2842 | P->first, P->second)) { |
2843 | Instantiation->setInvalidDecl(); |
2844 | break; |
2845 | } |
2846 | } |
2847 | } |
2848 | |
2849 | |
2850 | SavedContext.pop(); |
2851 | |
2852 | if (!Instantiation->isInvalidDecl()) { |
2853 | |
2854 | |
2855 | |
2856 | |
2857 | if (TSK == TSK_ExplicitInstantiationDefinition) |
2858 | MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
2859 | else if (MightHaveConstexprVirtualFunctions) |
2860 | MarkVirtualMembersReferenced(PointOfInstantiation, Instantiation, |
2861 | true); |
2862 | } |
2863 | |
2864 | Consumer.HandleTagDeclDefinition(Instantiation); |
2865 | |
2866 | return Instantiation->isInvalidDecl(); |
2867 | } |
2868 | |
2869 | |
2870 | |
2871 | |
2872 | |
2873 | |
2874 | |
2875 | |
2876 | |
2877 | |
2878 | |
2879 | |
2880 | |
2881 | |
2882 | |
2883 | |
2884 | bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation, |
2885 | EnumDecl *Instantiation, EnumDecl *Pattern, |
2886 | const MultiLevelTemplateArgumentList &TemplateArgs, |
2887 | TemplateSpecializationKind TSK) { |
2888 | EnumDecl *PatternDef = Pattern->getDefinition(); |
2889 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
2890 | Instantiation->getInstantiatedFromMemberEnum(), |
2891 | Pattern, PatternDef, TSK,true)) |
2892 | return true; |
2893 | Pattern = PatternDef; |
2894 | |
2895 | |
2896 | if (MemberSpecializationInfo *MSInfo |
2897 | = Instantiation->getMemberSpecializationInfo()) { |
2898 | MSInfo->setTemplateSpecializationKind(TSK); |
2899 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
2900 | } |
2901 | |
2902 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
2903 | if (Inst.isInvalid()) |
2904 | return true; |
2905 | if (Inst.isAlreadyInstantiating()) |
2906 | return false; |
2907 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
2908 | "instantiating enum definition"); |
2909 | |
2910 | |
2911 | |
2912 | Instantiation->setVisibleDespiteOwningModule(); |
2913 | |
2914 | |
2915 | |
2916 | ContextRAII SavedContext(*this, Instantiation); |
2917 | EnterExpressionEvaluationContext EvalContext( |
2918 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
2919 | |
2920 | LocalInstantiationScope Scope(*this, true); |
2921 | |
2922 | |
2923 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
2924 | |
2925 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
2926 | Instantiator.InstantiateEnumDefinition(Instantiation, Pattern); |
2927 | |
2928 | |
2929 | SavedContext.pop(); |
2930 | |
2931 | return Instantiation->isInvalidDecl(); |
2932 | } |
2933 | |
2934 | |
2935 | |
2936 | |
2937 | |
2938 | |
2939 | |
2940 | |
2941 | |
2942 | |
2943 | |
2944 | |
2945 | |
2946 | |
2947 | |
2948 | |
2949 | bool Sema::InstantiateInClassInitializer( |
2950 | SourceLocation PointOfInstantiation, FieldDecl *Instantiation, |
2951 | FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) { |
2952 | |
2953 | if (!Pattern->hasInClassInitializer()) |
2954 | return false; |
2955 | |
2956 | assert(Instantiation->getInClassInitStyle() == |
2957 | Pattern->getInClassInitStyle() && |
2958 | "pattern and instantiation disagree about init style"); |
2959 | |
2960 | |
2961 | |
2962 | Expr *OldInit = Pattern->getInClassInitializer(); |
2963 | if (!OldInit) { |
2964 | RecordDecl *PatternRD = Pattern->getParent(); |
2965 | RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext(); |
2966 | Diag(PointOfInstantiation, |
2967 | diag::err_default_member_initializer_not_yet_parsed) |
2968 | << OutermostClass << Pattern; |
2969 | Diag(Pattern->getEndLoc(), |
2970 | diag::note_default_member_initializer_not_yet_parsed); |
2971 | Instantiation->setInvalidDecl(); |
2972 | return true; |
2973 | } |
2974 | |
2975 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
2976 | if (Inst.isInvalid()) |
2977 | return true; |
2978 | if (Inst.isAlreadyInstantiating()) { |
2979 | |
2980 | Diag(PointOfInstantiation, diag::err_default_member_initializer_cycle) |
2981 | << Instantiation; |
2982 | return true; |
2983 | } |
2984 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
2985 | "instantiating default member init"); |
2986 | |
2987 | |
2988 | |
2989 | ContextRAII SavedContext(*this, Instantiation->getParent()); |
2990 | EnterExpressionEvaluationContext EvalContext( |
2991 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
2992 | |
2993 | LocalInstantiationScope Scope(*this, true); |
2994 | |
2995 | |
2996 | ActOnStartCXXInClassMemberInitializer(); |
2997 | CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), Qualifiers()); |
2998 | |
2999 | ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs, |
3000 | false); |
3001 | Expr *Init = NewInit.get(); |
3002 | assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class"); |
3003 | ActOnFinishCXXInClassMemberInitializer( |
3004 | Instantiation, Init ? Init->getBeginLoc() : SourceLocation(), Init); |
3005 | |
3006 | if (auto *L = getASTMutationListener()) |
3007 | L->DefaultMemberInitializerInstantiated(Instantiation); |
3008 | |
3009 | |
3010 | return !Instantiation->getInClassInitializer(); |
3011 | } |
3012 | |
3013 | namespace { |
3014 | |
3015 | |
3016 | struct PartialSpecMatchResult { |
3017 | ClassTemplatePartialSpecializationDecl *Partial; |
3018 | TemplateArgumentList *Args; |
3019 | }; |
3020 | } |
3021 | |
3022 | bool Sema::usesPartialOrExplicitSpecialization( |
3023 | SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
3024 | if (ClassTemplateSpec->getTemplateSpecializationKind() == |
3025 | TSK_ExplicitSpecialization) |
3026 | return true; |
3027 | |
3028 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
3029 | ClassTemplateSpec->getSpecializedTemplate() |
3030 | ->getPartialSpecializations(PartialSpecs); |
3031 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
3032 | TemplateDeductionInfo Info(Loc); |
3033 | if (!DeduceTemplateArguments(PartialSpecs[I], |
3034 | ClassTemplateSpec->getTemplateArgs(), Info)) |
3035 | return true; |
3036 | } |
3037 | |
3038 | return false; |
3039 | } |
3040 | |
3041 | |
3042 | |
3043 | |
3044 | static ActionResult<CXXRecordDecl *> |
3045 | getPatternForClassTemplateSpecialization( |
3046 | Sema &S, SourceLocation PointOfInstantiation, |
3047 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
3048 | TemplateSpecializationKind TSK) { |
3049 | Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec); |
3050 | if (Inst.isInvalid()) |
3051 | return {true}; |
3052 | if (Inst.isAlreadyInstantiating()) |
3053 | return {false}; |
3054 | |
3055 | llvm::PointerUnion<ClassTemplateDecl *, |
3056 | ClassTemplatePartialSpecializationDecl *> |
3057 | Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
3058 | if (!Specialized.is<ClassTemplatePartialSpecializationDecl *>()) { |
3059 | |
3060 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
3061 | |
3062 | |
3063 | |
3064 | |
3065 | |
3066 | |
3067 | |
3068 | |
3069 | |
3070 | typedef PartialSpecMatchResult MatchResult; |
3071 | SmallVector<MatchResult, 4> Matched; |
3072 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
3073 | Template->getPartialSpecializations(PartialSpecs); |
3074 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
3075 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
3076 | ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
3077 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
3078 | if (Sema::TemplateDeductionResult Result = S.DeduceTemplateArguments( |
3079 | Partial, ClassTemplateSpec->getTemplateArgs(), Info)) { |
3080 | |
3081 | |
3082 | FailedCandidates.addCandidate().set( |
3083 | DeclAccessPair::make(Template, AS_public), Partial, |
3084 | MakeDeductionFailureInfo(S.Context, Result, Info)); |
3085 | (void)Result; |
3086 | } else { |
3087 | Matched.push_back(PartialSpecMatchResult()); |
3088 | Matched.back().Partial = Partial; |
3089 | Matched.back().Args = Info.take(); |
3090 | } |
3091 | } |
3092 | |
3093 | |
3094 | |
3095 | |
3096 | |
3097 | if (Matched.size() >= 1) { |
3098 | SmallVectorImpl<MatchResult>::iterator Best = Matched.begin(); |
3099 | if (Matched.size() == 1) { |
3100 | |
3101 | |
3102 | |
3103 | } else { |
3104 | |
3105 | |
3106 | |
3107 | |
3108 | |
3109 | |
3110 | |
3111 | for (SmallVectorImpl<MatchResult>::iterator P = Best + 1, |
3112 | PEnd = Matched.end(); |
3113 | P != PEnd; ++P) { |
3114 | if (S.getMoreSpecializedPartialSpecialization( |
3115 | P->Partial, Best->Partial, PointOfInstantiation) == |
3116 | P->Partial) |
3117 | Best = P; |
3118 | } |
3119 | |
3120 | |
3121 | |
3122 | bool Ambiguous = false; |
3123 | for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
3124 | PEnd = Matched.end(); |
3125 | P != PEnd; ++P) { |
3126 | if (P != Best && S.getMoreSpecializedPartialSpecialization( |
3127 | P->Partial, Best->Partial, |
3128 | PointOfInstantiation) != Best->Partial) { |
3129 | Ambiguous = true; |
3130 | break; |
3131 | } |
3132 | } |
3133 | |
3134 | if (Ambiguous) { |
3135 | |
3136 | Inst.Clear(); |
3137 | ClassTemplateSpec->setInvalidDecl(); |
3138 | S.Diag(PointOfInstantiation, |
3139 | diag::err_partial_spec_ordering_ambiguous) |
3140 | << ClassTemplateSpec; |
3141 | |
3142 | |
3143 | for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
3144 | PEnd = Matched.end(); |
3145 | P != PEnd; ++P) |
3146 | S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
3147 | << S.getTemplateArgumentBindingsText( |
3148 | P->Partial->getTemplateParameters(), *P->Args); |
3149 | |
3150 | return {true}; |
3151 | } |
3152 | } |
3153 | |
3154 | ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); |
3155 | } else { |
3156 | |
3157 | |
3158 | } |
3159 | } |
3160 | |
3161 | CXXRecordDecl *Pattern = nullptr; |
3162 | Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
3163 | if (auto *PartialSpec = |
3164 | Specialized.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
3165 | |
3166 | while (PartialSpec->getInstantiatedFromMember()) { |
3167 | |
3168 | |
3169 | if (PartialSpec->isMemberSpecialization()) |
3170 | break; |
3171 | |
3172 | PartialSpec = PartialSpec->getInstantiatedFromMember(); |
3173 | } |
3174 | Pattern = PartialSpec; |
3175 | } else { |
3176 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
3177 | while (Template->getInstantiatedFromMemberTemplate()) { |
3178 | |
3179 | |
3180 | if (Template->isMemberSpecialization()) |
3181 | break; |
3182 | |
3183 | Template = Template->getInstantiatedFromMemberTemplate(); |
3184 | } |
3185 | Pattern = Template->getTemplatedDecl(); |
3186 | } |
3187 | |
3188 | return Pattern; |
3189 | } |
3190 | |
3191 | bool Sema::InstantiateClassTemplateSpecialization( |
3192 | SourceLocation PointOfInstantiation, |
3193 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
3194 | TemplateSpecializationKind TSK, bool Complain) { |
3195 | |
3196 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 1 | The object is a 'ClassTemplateSpecializationDecl' | |
|
3197 | ClassTemplateSpec->getCanonicalDecl()); |
3198 | if (ClassTemplateSpec->isInvalidDecl()) |
| 2 | | Assuming the condition is false | |
|
| |
3199 | return true; |
3200 | |
3201 | ActionResult<CXXRecordDecl *> Pattern = |
3202 | getPatternForClassTemplateSpecialization(*this, PointOfInstantiation, |
3203 | ClassTemplateSpec, TSK); |
3204 | if (!Pattern.isUsable()) |
| 4 | | Assuming the condition is false | |
|
| |
3205 | return Pattern.isInvalid(); |
3206 | |
3207 | return InstantiateClass( |
| 6 | | Calling 'Sema::InstantiateClass' | |
|
3208 | PointOfInstantiation, ClassTemplateSpec, Pattern.get(), |
3209 | getTemplateInstantiationArgs(ClassTemplateSpec), TSK, Complain); |
3210 | } |
3211 | |
3212 | |
3213 | |
3214 | |
3215 | void |
3216 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
3217 | CXXRecordDecl *Instantiation, |
3218 | const MultiLevelTemplateArgumentList &TemplateArgs, |
3219 | TemplateSpecializationKind TSK) { |
3220 | |
3221 | |
3222 | |
3223 | assert( |
3224 | (TSK == TSK_ExplicitInstantiationDefinition || |
3225 | TSK == TSK_ExplicitInstantiationDeclaration || |
3226 | (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) && |
3227 | "Unexpected template specialization kind!"); |
3228 | for (auto *D : Instantiation->decls()) { |
3229 | bool SuppressNew = false; |
3230 | if (auto *Function = dyn_cast<FunctionDecl>(D)) { |
3231 | if (FunctionDecl *Pattern = |
3232 | Function->getInstantiatedFromMemberFunction()) { |
3233 | |
3234 | if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
3235 | continue; |
3236 | |
3237 | MemberSpecializationInfo *MSInfo = |
3238 | Function->getMemberSpecializationInfo(); |
3239 | assert(MSInfo && "No member specialization information?"); |
3240 | if (MSInfo->getTemplateSpecializationKind() |
3241 | == TSK_ExplicitSpecialization) |
3242 | continue; |
3243 | |
3244 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
3245 | Function, |
3246 | MSInfo->getTemplateSpecializationKind(), |
3247 | MSInfo->getPointOfInstantiation(), |
3248 | SuppressNew) || |
3249 | SuppressNew) |
3250 | continue; |
3251 | |
3252 | |
3253 | |
3254 | |
3255 | |
3256 | |
3257 | |
3258 | if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined()) |
3259 | continue; |
3260 | |
3261 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
3262 | |
3263 | if (Function->isDefined()) { |
3264 | |
3265 | |
3266 | Consumer.HandleTopLevelDecl(DeclGroupRef(Function)); |
3267 | } else if (TSK == TSK_ExplicitInstantiationDefinition) { |
3268 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
3269 | } else if (TSK == TSK_ImplicitInstantiation) { |
3270 | PendingLocalImplicitInstantiations.push_back( |
3271 | std::make_pair(Function, PointOfInstantiation)); |
3272 | } |
3273 | } |
3274 | } else if (auto *Var = dyn_cast<VarDecl>(D)) { |
3275 | if (isa<VarTemplateSpecializationDecl>(Var)) |
3276 | continue; |
3277 | |
3278 | if (Var->isStaticDataMember()) { |
3279 | if (Var->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
3280 | continue; |
3281 | |
3282 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
3283 | assert(MSInfo && "No member specialization information?"); |
3284 | if (MSInfo->getTemplateSpecializationKind() |
3285 | == TSK_ExplicitSpecialization) |
3286 | continue; |
3287 | |
3288 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
3289 | Var, |
3290 | MSInfo->getTemplateSpecializationKind(), |
3291 | MSInfo->getPointOfInstantiation(), |
3292 | SuppressNew) || |
3293 | SuppressNew) |
3294 | continue; |
3295 | |
3296 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
3297 | |
3298 | |
3299 | |
3300 | |
3301 | |
3302 | |
3303 | if (!Var->getInstantiatedFromStaticDataMember()->getDefinition()) |
3304 | continue; |
3305 | |
3306 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
3307 | InstantiateVariableDefinition(PointOfInstantiation, Var); |
3308 | } else { |
3309 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
3310 | } |
3311 | } |
3312 | } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) { |
3313 | if (Record->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
3314 | continue; |
3315 | |
3316 | |
3317 | |
3318 | |
3319 | |
3320 | |
3321 | if (Record->isInjectedClassName() || Record->getPreviousDecl() || |
3322 | Record->isLambda()) |
3323 | continue; |
3324 | |
3325 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
3326 | assert(MSInfo && "No member specialization information?"); |
3327 | |
3328 | if (MSInfo->getTemplateSpecializationKind() |
3329 | == TSK_ExplicitSpecialization) |
3330 | continue; |
3331 | |
3332 | if (Context.getTargetInfo().getTriple().isOSWindows() && |
3333 | TSK == TSK_ExplicitInstantiationDeclaration) { |
3334 | |
3335 | |
3336 | |
3337 | |
3338 | |
3339 | |
3340 | continue; |
3341 | } |
3342 | |
3343 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
3344 | Record, |
3345 | MSInfo->getTemplateSpecializationKind(), |
3346 | MSInfo->getPointOfInstantiation(), |
3347 | SuppressNew) || |
3348 | SuppressNew) |
3349 | continue; |
3350 | |
3351 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
3352 | assert(Pattern && "Missing instantiated-from-template information"); |
3353 | |
3354 | if (!Record->getDefinition()) { |
3355 | if (!Pattern->getDefinition()) { |
3356 | |
3357 | |
3358 | |
3359 | |
3360 | |
3361 | |
3362 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
3363 | MSInfo->setTemplateSpecializationKind(TSK); |
3364 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
3365 | } |
3366 | |
3367 | continue; |
3368 | } |
3369 | |
3370 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
3371 | TemplateArgs, |
3372 | TSK); |
3373 | } else { |
3374 | if (TSK == TSK_ExplicitInstantiationDefinition && |
3375 | Record->getTemplateSpecializationKind() == |
3376 | TSK_ExplicitInstantiationDeclaration) { |
3377 | Record->setTemplateSpecializationKind(TSK); |
3378 | MarkVTableUsed(PointOfInstantiation, Record, true); |
3379 | } |
3380 | } |
3381 | |
3382 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
3383 | if (Pattern) |
3384 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
3385 | TSK); |
3386 | } else if (auto *Enum = dyn_cast<EnumDecl>(D)) { |
3387 | MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo(); |
3388 | assert(MSInfo && "No member specialization information?"); |
3389 | |
3390 | if (MSInfo->getTemplateSpecializationKind() |
3391 | == TSK_ExplicitSpecialization) |
3392 | continue; |
3393 | |
3394 | if (CheckSpecializationInstantiationRedecl( |
3395 | PointOfInstantiation, TSK, Enum, |
3396 | MSInfo->getTemplateSpecializationKind(), |
3397 | MSInfo->getPointOfInstantiation(), SuppressNew) || |
3398 | SuppressNew) |
3399 | continue; |
3400 | |
3401 | if (Enum->getDefinition()) |
3402 | continue; |
3403 | |
3404 | EnumDecl *Pattern = Enum->getTemplateInstantiationPattern(); |
3405 | assert(Pattern && "Missing instantiated-from-template information"); |
3406 | |
3407 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
3408 | if (!Pattern->getDefinition()) |
3409 | continue; |
3410 | |
3411 | InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK); |
3412 | } else { |
3413 | MSInfo->setTemplateSpecializationKind(TSK); |
3414 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
3415 | } |
3416 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
3417 | |
3418 | |
3419 | if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) { |
3420 | CXXRecordDecl *ClassPattern = |
3421 | Instantiation->getTemplateInstantiationPattern(); |
3422 | DeclContext::lookup_result Lookup = |
3423 | ClassPattern->lookup(Field->getDeclName()); |
3424 | FieldDecl *Pattern = Lookup.find_first<FieldDecl>(); |
3425 | assert(Pattern); |
3426 | InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern, |
3427 | TemplateArgs); |
3428 | } |
3429 | } |
3430 | } |
3431 | } |
3432 | |
3433 | |
3434 | |
3435 | |
3436 | void |
3437 | Sema::InstantiateClassTemplateSpecializationMembers( |
3438 | SourceLocation PointOfInstantiation, |
3439 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
3440 | TemplateSpecializationKind TSK) { |
3441 | |
3442 | |
3443 | |
3444 | |
3445 | |
3446 | |
3447 | |
3448 | |
3449 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
3450 | getTemplateInstantiationArgs(ClassTemplateSpec), |
3451 | TSK); |
3452 | } |
3453 | |
3454 | StmtResult |
3455 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
3456 | if (!S) |
3457 | return S; |
3458 | |
3459 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
3460 | SourceLocation(), |
3461 | DeclarationName()); |
3462 | return Instantiator.TransformStmt(S); |
3463 | } |
3464 | |
3465 | bool Sema::SubstTemplateArguments( |
3466 | ArrayRef<TemplateArgumentLoc> Args, |
3467 | const MultiLevelTemplateArgumentList &TemplateArgs, |
3468 | TemplateArgumentListInfo &Out) { |
3469 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
3470 | SourceLocation(), |
3471 | DeclarationName()); |
3472 | return Instantiator.TransformTemplateArguments(Args.begin(), Args.end(), |
3473 | Out); |
3474 | } |
3475 | |
3476 | ExprResult |
3477 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
3478 | if (!E) |
3479 | return E; |
3480 | |
3481 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
3482 | SourceLocation(), |
3483 | DeclarationName()); |
3484 | return Instantiator.TransformExpr(E); |
3485 | } |
3486 | |
3487 | ExprResult Sema::SubstInitializer(Expr *Init, |
3488 | const MultiLevelTemplateArgumentList &TemplateArgs, |
3489 | bool CXXDirectInit) { |
3490 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
3491 | SourceLocation(), |
3492 | DeclarationName()); |
3493 | return Instantiator.TransformInitializer(Init, CXXDirectInit); |
3494 | } |
3495 | |
3496 | bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall, |
3497 | const MultiLevelTemplateArgumentList &TemplateArgs, |
3498 | SmallVectorImpl<Expr *> &Outputs) { |
3499 | if (Exprs.empty()) |
3500 | return false; |
3501 | |
3502 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
3503 | SourceLocation(), |
3504 | DeclarationName()); |
3505 | return Instantiator.TransformExprs(Exprs.data(), Exprs.size(), |
3506 | IsCall, Outputs); |
3507 | } |
3508 | |
3509 | NestedNameSpecifierLoc |
3510 | Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
3511 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
3512 | if (!NNS) |
3513 | return NestedNameSpecifierLoc(); |
3514 | |
3515 | TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(), |
3516 | DeclarationName()); |
3517 | return Instantiator.TransformNestedNameSpecifierLoc(NNS); |
3518 | } |
3519 | |
3520 | |
3521 | DeclarationNameInfo |
3522 | Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
3523 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
3524 | TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
3525 | NameInfo.getName()); |
3526 | return Instantiator.TransformDeclarationNameInfo(NameInfo); |
3527 | } |
3528 | |
3529 | TemplateName |
3530 | Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, |
3531 | TemplateName Name, SourceLocation Loc, |
3532 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
3533 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
3534 | DeclarationName()); |
3535 | CXXScopeSpec SS; |
3536 | SS.Adopt(QualifierLoc); |
3537 | return Instantiator.TransformTemplateName(SS, Name, Loc); |
3538 | } |
3539 | |
3540 | bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, |
3541 | TemplateArgumentListInfo &Result, |
3542 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
3543 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
3544 | DeclarationName()); |
3545 | |
3546 | return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); |
3547 | } |
3548 | |
3549 | static const Decl *getCanonicalParmVarDecl(const Decl *D) { |
3550 | |
3551 | |
3552 | |
3553 | |
3554 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) { |
3555 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
3556 | unsigned i = PV->getFunctionScopeIndex(); |
3557 | |
3558 | |
3559 | if (i < FD->getNumParams() && FD->getParamDecl(i) == PV) |
3560 | return FD->getCanonicalDecl()->getParamDecl(i); |
3561 | } |
3562 | } |
3563 | return D; |
3564 | } |
3565 | |
3566 | |
3567 | llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * |
3568 | LocalInstantiationScope::findInstantiationOf(const Decl *D) { |
3569 | D = getCanonicalParmVarDecl(D); |
3570 | for (LocalInstantiationScope *Current = this; Current; |
3571 | Current = Current->Outer) { |
3572 | |
3573 | |
3574 | const Decl *CheckD = D; |
3575 | do { |
3576 | LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD); |
3577 | if (Found != Current->LocalDecls.end()) |
3578 | return &Found->second; |
3579 | |
3580 | |
3581 | |
3582 | if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) |
3583 | CheckD = Tag->getPreviousDecl(); |
3584 | else |
3585 | CheckD = nullptr; |
3586 | } while (CheckD); |
3587 | |
3588 | |
3589 | if (!Current->CombineWithOuterScope) |
3590 | break; |
3591 | } |
3592 | |
3593 | |
3594 | |
3595 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
3596 | isa<TemplateTemplateParmDecl>(D)) |
3597 | return nullptr; |
3598 | |
3599 | |
3600 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
3601 | if (RD->isLocalClass()) |
3602 | return nullptr; |
3603 | |
3604 | |
3605 | |
3606 | if (isa<EnumDecl>(D)) |
3607 | return nullptr; |
3608 | |
3609 | |
3610 | |
3611 | if (isa<TypedefNameDecl>(D) && |
3612 | isa<CXXDeductionGuideDecl>(D->getDeclContext())) |
3613 | return nullptr; |
3614 | |
3615 | |
3616 | |
3617 | |
3618 | assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope"); |
3619 | return nullptr; |
3620 | } |
3621 | |
3622 | void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
3623 | D = getCanonicalParmVarDecl(D); |
3624 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
3625 | if (Stored.isNull()) { |
3626 | #ifndef NDEBUG |
3627 | |
3628 | LocalInstantiationScope *Current = this; |
3629 | while (Current->CombineWithOuterScope && Current->Outer) { |
3630 | Current = Current->Outer; |
3631 | assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() && |
3632 | "Instantiated local in inner and outer scopes"); |
3633 | } |
3634 | #endif |
3635 | Stored = Inst; |
3636 | } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) { |
3637 | Pack->push_back(cast<VarDecl>(Inst)); |
3638 | } else { |
3639 | assert(Stored.get<Decl *>() == Inst && "Already instantiated this local"); |
3640 | } |
3641 | } |
3642 | |
3643 | void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, |
3644 | VarDecl *Inst) { |
3645 | D = getCanonicalParmVarDecl(D); |
3646 | DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>(); |
3647 | Pack->push_back(Inst); |
3648 | } |
3649 | |
3650 | void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { |
3651 | #ifndef NDEBUG |
3652 | |
3653 | for (LocalInstantiationScope *Current = this; |
3654 | Current && Current->CombineWithOuterScope; Current = Current->Outer) |
3655 | assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() && |
3656 | "Creating local pack after instantiation of local"); |
3657 | #endif |
3658 | |
3659 | D = getCanonicalParmVarDecl(D); |
3660 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
3661 | DeclArgumentPack *Pack = new DeclArgumentPack; |
3662 | Stored = Pack; |
3663 | ArgumentPacks.push_back(Pack); |
3664 | } |
3665 | |
3666 | bool LocalInstantiationScope::isLocalPackExpansion(const Decl *D) { |
3667 | for (DeclArgumentPack *Pack : ArgumentPacks) |
3668 | if (std::find(Pack->begin(), Pack->end(), D) != Pack->end()) |
3669 | return true; |
3670 | return false; |
3671 | } |
3672 | |
3673 | void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack, |
3674 | const TemplateArgument *ExplicitArgs, |
3675 | unsigned NumExplicitArgs) { |
3676 | assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) && |
3677 | "Already have a partially-substituted pack"); |
3678 | assert((!PartiallySubstitutedPack |
3679 | || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) && |
3680 | "Wrong number of arguments in partially-substituted pack"); |
3681 | PartiallySubstitutedPack = Pack; |
3682 | ArgsInPartiallySubstitutedPack = ExplicitArgs; |
3683 | NumArgsInPartiallySubstitutedPack = NumExplicitArgs; |
3684 | } |
3685 | |
3686 | NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack( |
3687 | const TemplateArgument **ExplicitArgs, |
3688 | unsigned *NumExplicitArgs) const { |
3689 | if (ExplicitArgs) |
3690 | *ExplicitArgs = nullptr; |
3691 | if (NumExplicitArgs) |
3692 | *NumExplicitArgs = 0; |
3693 | |
3694 | for (const LocalInstantiationScope *Current = this; Current; |
3695 | Current = Current->Outer) { |
3696 | if (Current->PartiallySubstitutedPack) { |
3697 | if (ExplicitArgs) |
3698 | *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack; |
3699 | if (NumExplicitArgs) |
3700 | *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack; |
3701 | |
3702 | return Current->PartiallySubstitutedPack; |
3703 | } |
3704 | |
3705 | if (!Current->CombineWithOuterScope) |
3706 | break; |
3707 | } |
3708 | |
3709 | return nullptr; |
3710 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_CLANG_SEMA_SEMA_H |
15 | #define LLVM_CLANG_SEMA_SEMA_H |
16 | |
17 | #include "clang/AST/ASTConcept.h" |
18 | #include "clang/AST/ASTFwd.h" |
19 | #include "clang/AST/Attr.h" |
20 | #include "clang/AST/Availability.h" |
21 | #include "clang/AST/ComparisonCategories.h" |
22 | #include "clang/AST/DeclTemplate.h" |
23 | #include "clang/AST/DeclarationName.h" |
24 | #include "clang/AST/Expr.h" |
25 | #include "clang/AST/ExprCXX.h" |
26 | #include "clang/AST/ExprConcepts.h" |
27 | #include "clang/AST/ExprObjC.h" |
28 | #include "clang/AST/ExprOpenMP.h" |
29 | #include "clang/AST/ExternalASTSource.h" |
30 | #include "clang/AST/LocInfoType.h" |
31 | #include "clang/AST/MangleNumberingContext.h" |
32 | #include "clang/AST/NSAPI.h" |
33 | #include "clang/AST/PrettyPrinter.h" |
34 | #include "clang/AST/StmtCXX.h" |
35 | #include "clang/AST/StmtOpenMP.h" |
36 | #include "clang/AST/TypeLoc.h" |
37 | #include "clang/AST/TypeOrdering.h" |
38 | #include "clang/Basic/BitmaskEnum.h" |
39 | #include "clang/Basic/Builtins.h" |
40 | #include "clang/Basic/DarwinSDKInfo.h" |
41 | #include "clang/Basic/ExpressionTraits.h" |
42 | #include "clang/Basic/Module.h" |
43 | #include "clang/Basic/OpenCLOptions.h" |
44 | #include "clang/Basic/OpenMPKinds.h" |
45 | #include "clang/Basic/PragmaKinds.h" |
46 | #include "clang/Basic/Specifiers.h" |
47 | #include "clang/Basic/TemplateKinds.h" |
48 | #include "clang/Basic/TypeTraits.h" |
49 | #include "clang/Sema/AnalysisBasedWarnings.h" |
50 | #include "clang/Sema/CleanupInfo.h" |
51 | #include "clang/Sema/DeclSpec.h" |
52 | #include "clang/Sema/ExternalSemaSource.h" |
53 | #include "clang/Sema/IdentifierResolver.h" |
54 | #include "clang/Sema/ObjCMethodList.h" |
55 | #include "clang/Sema/Ownership.h" |
56 | #include "clang/Sema/Scope.h" |
57 | #include "clang/Sema/SemaConcept.h" |
58 | #include "clang/Sema/TypoCorrection.h" |
59 | #include "clang/Sema/Weak.h" |
60 | #include "llvm/ADT/ArrayRef.h" |
61 | #include "llvm/ADT/Optional.h" |
62 | #include "llvm/ADT/SetVector.h" |
63 | #include "llvm/ADT/SmallBitVector.h" |
64 | #include "llvm/ADT/SmallPtrSet.h" |
65 | #include "llvm/ADT/SmallSet.h" |
66 | #include "llvm/ADT/SmallVector.h" |
67 | #include "llvm/ADT/TinyPtrVector.h" |
68 | #include "llvm/Frontend/OpenMP/OMPConstants.h" |
69 | #include <deque> |
70 | #include <memory> |
71 | #include <string> |
72 | #include <tuple> |
73 | #include <vector> |
74 | |
75 | namespace llvm { |
76 | class APSInt; |
77 | template <typename ValueT> struct DenseMapInfo; |
78 | template <typename ValueT, typename ValueInfoT> class DenseSet; |
79 | class SmallBitVector; |
80 | struct InlineAsmIdentifierInfo; |
81 | } |
82 | |
83 | namespace clang { |
84 | class ADLResult; |
85 | class ASTConsumer; |
86 | class ASTContext; |
87 | class ASTMutationListener; |
88 | class ASTReader; |
89 | class ASTWriter; |
90 | class ArrayType; |
91 | class ParsedAttr; |
92 | class BindingDecl; |
93 | class BlockDecl; |
94 | class CapturedDecl; |
95 | class CXXBasePath; |
96 | class CXXBasePaths; |
97 | class CXXBindTemporaryExpr; |
98 | typedef SmallVector<CXXBaseSpecifier*, 4> CXXCastPath; |
99 | class CXXConstructorDecl; |
100 | class CXXConversionDecl; |
101 | class CXXDeleteExpr; |
102 | class CXXDestructorDecl; |
103 | class CXXFieldCollector; |
104 | class CXXMemberCallExpr; |
105 | class CXXMethodDecl; |
106 | class CXXScopeSpec; |
107 | class CXXTemporary; |
108 | class CXXTryStmt; |
109 | class CallExpr; |
110 | class ClassTemplateDecl; |
111 | class ClassTemplatePartialSpecializationDecl; |
112 | class ClassTemplateSpecializationDecl; |
113 | class VarTemplatePartialSpecializationDecl; |
114 | class CodeCompleteConsumer; |
115 | class CodeCompletionAllocator; |
116 | class CodeCompletionTUInfo; |
117 | class CodeCompletionResult; |
118 | class CoroutineBodyStmt; |
119 | class Decl; |
120 | class DeclAccessPair; |
121 | class DeclContext; |
122 | class DeclRefExpr; |
123 | class DeclaratorDecl; |
124 | class DeducedTemplateArgument; |
125 | class DependentDiagnostic; |
126 | class DesignatedInitExpr; |
127 | class Designation; |
128 | class EnableIfAttr; |
129 | class EnumConstantDecl; |
130 | class Expr; |
131 | class ExtVectorType; |
132 | class FormatAttr; |
133 | class FriendDecl; |
134 | class FunctionDecl; |
135 | class FunctionProtoType; |
136 | class FunctionTemplateDecl; |
137 | class ImplicitConversionSequence; |
138 | typedef MutableArrayRef<ImplicitConversionSequence> ConversionSequenceList; |
139 | class InitListExpr; |
140 | class InitializationKind; |
141 | class InitializationSequence; |
142 | class InitializedEntity; |
143 | class IntegerLiteral; |
144 | class LabelStmt; |
145 | class LambdaExpr; |
146 | class LangOptions; |
147 | class LocalInstantiationScope; |
148 | class LookupResult; |
149 | class MacroInfo; |
150 | typedef ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> ModuleIdPath; |
151 | class ModuleLoader; |
152 | class MultiLevelTemplateArgumentList; |
153 | class NamedDecl; |
154 | class ObjCCategoryDecl; |
155 | class ObjCCategoryImplDecl; |
156 | class ObjCCompatibleAliasDecl; |
157 | class ObjCContainerDecl; |
158 | class ObjCImplDecl; |
159 | class ObjCImplementationDecl; |
160 | class ObjCInterfaceDecl; |
161 | class ObjCIvarDecl; |
162 | template <class T> class ObjCList; |
163 | class ObjCMessageExpr; |
164 | class ObjCMethodDecl; |
165 | class ObjCPropertyDecl; |
166 | class ObjCProtocolDecl; |
167 | class OMPThreadPrivateDecl; |
168 | class OMPRequiresDecl; |
169 | class OMPDeclareReductionDecl; |
170 | class OMPDeclareSimdDecl; |
171 | class OMPClause; |
172 | struct OMPVarListLocTy; |
173 | struct OverloadCandidate; |
174 | enum class OverloadCandidateParamOrder : char; |
175 | enum OverloadCandidateRewriteKind : unsigned; |
176 | class OverloadCandidateSet; |
177 | class OverloadExpr; |
178 | class ParenListExpr; |
179 | class ParmVarDecl; |
180 | class Preprocessor; |
181 | class PseudoDestructorTypeStorage; |
182 | class PseudoObjectExpr; |
183 | class QualType; |
184 | class StandardConversionSequence; |
185 | class Stmt; |
186 | class StringLiteral; |
187 | class SwitchStmt; |
188 | class TemplateArgument; |
189 | class TemplateArgumentList; |
190 | class TemplateArgumentLoc; |
191 | class TemplateDecl; |
192 | class TemplateInstantiationCallback; |
193 | class TemplateParameterList; |
194 | class TemplatePartialOrderingContext; |
195 | class TemplateTemplateParmDecl; |
196 | class Token; |
197 | class TypeAliasDecl; |
198 | class TypedefDecl; |
199 | class TypedefNameDecl; |
200 | class TypeLoc; |
201 | class TypoCorrectionConsumer; |
202 | class UnqualifiedId; |
203 | class UnresolvedLookupExpr; |
204 | class UnresolvedMemberExpr; |
205 | class UnresolvedSetImpl; |
206 | class UnresolvedSetIterator; |
207 | class UsingDecl; |
208 | class UsingShadowDecl; |
209 | class ValueDecl; |
210 | class VarDecl; |
211 | class VarTemplateSpecializationDecl; |
212 | class VisibilityAttr; |
213 | class VisibleDeclConsumer; |
214 | class IndirectFieldDecl; |
215 | struct DeductionFailureInfo; |
216 | class TemplateSpecCandidateSet; |
217 | |
218 | namespace sema { |
219 | class AccessedEntity; |
220 | class BlockScopeInfo; |
221 | class Capture; |
222 | class CapturedRegionScopeInfo; |
223 | class CapturingScopeInfo; |
224 | class CompoundScopeInfo; |
225 | class DelayedDiagnostic; |
226 | class DelayedDiagnosticPool; |
227 | class FunctionScopeInfo; |
228 | class LambdaScopeInfo; |
229 | class PossiblyUnreachableDiag; |
230 | class SemaPPCallbacks; |
231 | class TemplateDeductionInfo; |
232 | } |
233 | |
234 | namespace threadSafety { |
235 | class BeforeSet; |
236 | void threadSafetyCleanup(BeforeSet* Cache); |
237 | } |
238 | |
239 | |
240 | |
241 | typedef std::pair<llvm::PointerUnion<const TemplateTypeParmType*, NamedDecl*>, |
242 | SourceLocation> UnexpandedParameterPack; |
243 | |
244 | |
245 | |
246 | struct FileNullability { |
247 | |
248 | |
249 | SourceLocation PointerLoc; |
250 | |
251 | |
252 | |
253 | SourceLocation PointerEndLoc; |
254 | |
255 | |
256 | uint8_t PointerKind; |
257 | |
258 | |
259 | bool SawTypeNullability = false; |
260 | }; |
261 | |
262 | |
263 | |
264 | class FileNullabilityMap { |
265 | |
266 | llvm::DenseMap<FileID, FileNullability> Map; |
267 | |
268 | |
269 | struct { |
270 | FileID File; |
271 | FileNullability Nullability; |
272 | } Cache; |
273 | |
274 | public: |
275 | FileNullability &operator[](FileID file) { |
276 | |
277 | if (file == Cache.File) |
278 | return Cache.Nullability; |
279 | |
280 | |
281 | if (!Cache.File.isInvalid()) { |
282 | Map[Cache.File] = Cache.Nullability; |
283 | } |
284 | |
285 | |
286 | Cache.File = file; |
287 | Cache.Nullability = Map[file]; |
288 | return Cache.Nullability; |
289 | } |
290 | }; |
291 | |
292 | |
293 | |
294 | |
295 | |
296 | class PreferredTypeBuilder { |
297 | public: |
298 | PreferredTypeBuilder(bool Enabled) : Enabled(Enabled) {} |
299 | |
300 | void enterCondition(Sema &S, SourceLocation Tok); |
301 | void enterReturn(Sema &S, SourceLocation Tok); |
302 | void enterVariableInit(SourceLocation Tok, Decl *D); |
303 | |
304 | void enterDesignatedInitializer(SourceLocation Tok, QualType BaseType, |
305 | const Designation &D); |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | void enterFunctionArgument(SourceLocation Tok, |
316 | llvm::function_ref<QualType()> ComputeType); |
317 | |
318 | void enterParenExpr(SourceLocation Tok, SourceLocation LParLoc); |
319 | void enterUnary(Sema &S, SourceLocation Tok, tok::TokenKind OpKind, |
320 | SourceLocation OpLoc); |
321 | void enterBinary(Sema &S, SourceLocation Tok, Expr *LHS, tok::TokenKind Op); |
322 | void enterMemAccess(Sema &S, SourceLocation Tok, Expr *Base); |
323 | void enterSubscript(Sema &S, SourceLocation Tok, Expr *LHS); |
324 | |
325 | void enterTypeCast(SourceLocation Tok, QualType CastType); |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | QualType get(SourceLocation Tok) const { |
334 | if (!Enabled || Tok != ExpectedLoc) |
335 | return QualType(); |
336 | if (!Type.isNull()) |
337 | return Type; |
338 | if (ComputeType) |
339 | return ComputeType(); |
340 | return QualType(); |
341 | } |
342 | |
343 | private: |
344 | bool Enabled; |
345 | |
346 | SourceLocation ExpectedLoc; |
347 | |
348 | QualType Type; |
349 | |
350 | |
351 | llvm::function_ref<QualType()> ComputeType; |
352 | }; |
353 | |
354 | |
355 | class Sema final { |
356 | Sema(const Sema &) = delete; |
357 | void operator=(const Sema &) = delete; |
358 | |
359 | |
360 | ExternalSemaSource *ExternalSource; |
361 | |
362 | |
363 | bool isMultiplexExternalSource; |
364 | |
365 | static bool mightHaveNonExternalLinkage(const DeclaratorDecl *FD); |
366 | |
367 | bool isVisibleSlow(const NamedDecl *D); |
368 | |
369 | |
370 | |
371 | |
372 | bool shouldLinkPossiblyHiddenDecl(const NamedDecl *Old, |
373 | const NamedDecl *New) { |
374 | if (isVisible(Old)) |
375 | return true; |
376 | |
377 | |
378 | if (New->isExternallyDeclarable()) { |
379 | assert(Old->isExternallyDeclarable() && |
380 | "should not have found a non-externally-declarable previous decl"); |
381 | return true; |
382 | } |
383 | return false; |
384 | } |
385 | bool shouldLinkPossiblyHiddenDecl(LookupResult &Old, const NamedDecl *New); |
386 | |
387 | void setupImplicitSpecialMemberType(CXXMethodDecl *SpecialMem, |
388 | QualType ResultTy, |
389 | ArrayRef<QualType> Args); |
390 | |
391 | public: |
392 | |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | static const unsigned MaxAlignmentExponent = 29; |
400 | static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent; |
401 | |
402 | typedef OpaquePtr<DeclGroupRef> DeclGroupPtrTy; |
403 | typedef OpaquePtr<TemplateName> TemplateTy; |
404 | typedef OpaquePtr<QualType> TypeTy; |
405 | |
406 | OpenCLOptions OpenCLFeatures; |
407 | FPOptions CurFPFeatures; |
408 | |
409 | const LangOptions &LangOpts; |
410 | Preprocessor &PP; |
411 | ASTContext &Context; |
412 | ASTConsumer &Consumer; |
413 | DiagnosticsEngine &Diags; |
414 | SourceManager &SourceMgr; |
415 | |
416 | |
417 | bool CollectStats; |
418 | |
419 | |
420 | CodeCompleteConsumer *CodeCompleter; |
421 | |
422 | |
423 | DeclContext *CurContext; |
424 | |
425 | |
426 | |
427 | DeclContext *OriginalLexicalContext; |
428 | |
429 | |
430 | |
431 | DeclarationName VAListTagName; |
432 | |
433 | bool MSStructPragmaOn; |
434 | |
435 | |
436 | LangOptions::PragmaMSPointersToMembersKind |
437 | MSPointerToMemberRepresentationMethod; |
438 | |
439 | |
440 | SmallVector<Scope*, 2> CurrentSEHFinally; |
441 | |
442 | |
443 | SourceLocation ImplicitMSInheritanceAttrLoc; |
444 | |
445 | |
446 | |
447 | |
448 | |
449 | llvm::SmallVector<TypoExpr *, 2> TypoExprs; |
450 | |
451 | |
452 | enum PragmaClangSectionKind { |
453 | PCSK_Invalid = 0, |
454 | PCSK_BSS = 1, |
455 | PCSK_Data = 2, |
456 | PCSK_Rodata = 3, |
457 | PCSK_Text = 4, |
458 | PCSK_Relro = 5 |
459 | }; |
460 | |
461 | enum PragmaClangSectionAction { |
462 | PCSA_Set = 0, |
463 | PCSA_Clear = 1 |
464 | }; |
465 | |
466 | struct PragmaClangSection { |
467 | std::string SectionName; |
468 | bool Valid = false; |
469 | SourceLocation PragmaLocation; |
470 | }; |
471 | |
472 | PragmaClangSection PragmaClangBSSSection; |
473 | PragmaClangSection PragmaClangDataSection; |
474 | PragmaClangSection PragmaClangRodataSection; |
475 | PragmaClangSection PragmaClangRelroSection; |
476 | PragmaClangSection PragmaClangTextSection; |
477 | |
478 | enum PragmaMsStackAction { |
479 | PSK_Reset = 0x0, |
480 | PSK_Set = 0x1, |
481 | PSK_Push = 0x2, |
482 | PSK_Pop = 0x4, |
483 | PSK_Show = 0x8, |
484 | PSK_Push_Set = PSK_Push | PSK_Set, |
485 | PSK_Pop_Set = PSK_Pop | PSK_Set, |
486 | }; |
487 | |
488 | |
489 | class AlignPackInfo { |
490 | public: |
491 | |
492 | |
493 | enum Mode : unsigned char { Native, Natural, Packed, Mac68k }; |
494 | |
495 | |
496 | AlignPackInfo(AlignPackInfo::Mode M, unsigned Num, bool IsXL) |
497 | : PackAttr(true), AlignMode(M), PackNumber(Num), XLStack(IsXL) { |
498 | assert(Num == PackNumber && "The pack number has been truncated."); |
499 | } |
500 | |
501 | |
502 | AlignPackInfo(AlignPackInfo::Mode M, bool IsXL) |
503 | : PackAttr(false), AlignMode(M), |
504 | PackNumber(M == Packed ? 1 : UninitPackVal), XLStack(IsXL) {} |
505 | |
506 | explicit AlignPackInfo(bool IsXL) : AlignPackInfo(Native, IsXL) {} |
507 | |
508 | AlignPackInfo() : AlignPackInfo(Native, false) {} |
509 | |
510 | |
511 | |
512 | |
513 | static uint32_t getRawEncoding(const AlignPackInfo &Info) { |
514 | std::uint32_t Encoding{}; |
515 | if (Info.IsXLStack()) |
516 | Encoding |= IsXLMask; |
517 | |
518 | Encoding |= static_cast<uint32_t>(Info.getAlignMode()) << 1; |
519 | |
520 | if (Info.IsPackAttr()) |
521 | Encoding |= PackAttrMask; |
522 | |
523 | Encoding |= static_cast<uint32_t>(Info.getPackNumber()) << 4; |
524 | |
525 | return Encoding; |
526 | } |
527 | |
528 | static AlignPackInfo getFromRawEncoding(unsigned Encoding) { |
529 | bool IsXL = static_cast<bool>(Encoding & IsXLMask); |
530 | AlignPackInfo::Mode M = |
531 | static_cast<AlignPackInfo::Mode>((Encoding & AlignModeMask) >> 1); |
532 | int PackNumber = (Encoding & PackNumMask) >> 4; |
533 | |
534 | if (Encoding & PackAttrMask) |
535 | return AlignPackInfo(M, PackNumber, IsXL); |
536 | |
537 | return AlignPackInfo(M, IsXL); |
538 | } |
539 | |
540 | bool IsPackAttr() const { return PackAttr; } |
541 | |
542 | bool IsAlignAttr() const { return !PackAttr; } |
543 | |
544 | Mode getAlignMode() const { return AlignMode; } |
545 | |
546 | unsigned getPackNumber() const { return PackNumber; } |
547 | |
548 | bool IsPackSet() const { |
549 | |
550 | |
551 | return PackNumber != UninitPackVal && PackNumber != 0; |
552 | } |
553 | |
554 | bool IsXLStack() const { return XLStack; } |
555 | |
556 | bool operator==(const AlignPackInfo &Info) const { |
557 | return std::tie(AlignMode, PackNumber, PackAttr, XLStack) == |
558 | std::tie(Info.AlignMode, Info.PackNumber, Info.PackAttr, |
559 | Info.XLStack); |
560 | } |
561 | |
562 | bool operator!=(const AlignPackInfo &Info) const { |
563 | return !(*this == Info); |
564 | } |
565 | |
566 | private: |
567 | |
568 | |
569 | bool PackAttr; |
570 | |
571 | |
572 | Mode AlignMode; |
573 | |
574 | |
575 | unsigned char PackNumber; |
576 | |
577 | |
578 | bool XLStack; |
579 | |
580 | |
581 | static constexpr unsigned char UninitPackVal = -1; |
582 | |
583 | |
584 | static constexpr uint32_t IsXLMask{0x0000'0001}; |
585 | static constexpr uint32_t AlignModeMask{0x0000'0006}; |
586 | static constexpr uint32_t PackAttrMask{0x00000'0008}; |
587 | static constexpr uint32_t PackNumMask{0x0000'01F0}; |
588 | }; |
589 | |
590 | template<typename ValueType> |
591 | struct PragmaStack { |
592 | struct Slot { |
593 | llvm::StringRef StackSlotLabel; |
594 | ValueType Value; |
595 | SourceLocation PragmaLocation; |
596 | SourceLocation PragmaPushLocation; |
597 | Slot(llvm::StringRef StackSlotLabel, ValueType Value, |
598 | SourceLocation PragmaLocation, SourceLocation PragmaPushLocation) |
599 | : StackSlotLabel(StackSlotLabel), Value(Value), |
600 | PragmaLocation(PragmaLocation), |
601 | PragmaPushLocation(PragmaPushLocation) {} |
602 | }; |
603 | |
604 | void Act(SourceLocation PragmaLocation, PragmaMsStackAction Action, |
605 | llvm::StringRef StackSlotLabel, ValueType Value) { |
606 | if (Action == PSK_Reset) { |
607 | CurrentValue = DefaultValue; |
608 | CurrentPragmaLocation = PragmaLocation; |
609 | return; |
610 | } |
611 | if (Action & PSK_Push) |
612 | Stack.emplace_back(StackSlotLabel, CurrentValue, CurrentPragmaLocation, |
613 | PragmaLocation); |
614 | else if (Action & PSK_Pop) { |
615 | if (!StackSlotLabel.empty()) { |
616 | |
617 | auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) { |
618 | return x.StackSlotLabel == StackSlotLabel; |
619 | }); |
620 | |
621 | if (I != Stack.rend()) { |
622 | CurrentValue = I->Value; |
623 | CurrentPragmaLocation = I->PragmaLocation; |
624 | Stack.erase(std::prev(I.base()), Stack.end()); |
625 | } |
626 | } else if (!Stack.empty()) { |
627 | |
628 | CurrentValue = Stack.back().Value; |
629 | CurrentPragmaLocation = Stack.back().PragmaLocation; |
630 | Stack.pop_back(); |
631 | } |
632 | } |
633 | if (Action & PSK_Set) { |
634 | CurrentValue = Value; |
635 | CurrentPragmaLocation = PragmaLocation; |
636 | } |
637 | } |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | |
652 | |
653 | void SentinelAction(PragmaMsStackAction Action, StringRef Label) { |
654 | assert((Action == PSK_Push || Action == PSK_Pop) && |
655 | "Can only push / pop #pragma stack sentinels!"); |
656 | Act(CurrentPragmaLocation, Action, Label, CurrentValue); |
657 | } |
658 | |
659 | |
660 | explicit PragmaStack(const ValueType &Default) |
661 | : DefaultValue(Default), CurrentValue(Default) {} |
662 | |
663 | bool hasValue() const { return CurrentValue != DefaultValue; } |
664 | |
665 | SmallVector<Slot, 2> Stack; |
666 | ValueType DefaultValue; |
667 | ValueType CurrentValue; |
668 | SourceLocation CurrentPragmaLocation; |
669 | }; |
670 | |
671 | |
672 | |
673 | |
674 | |
675 | |
676 | |
677 | |
678 | |
679 | |
680 | |
681 | PragmaStack<MSVtorDispMode> VtorDispStack; |
682 | PragmaStack<AlignPackInfo> AlignPackStack; |
683 | |
684 | struct AlignPackIncludeState { |
685 | AlignPackInfo CurrentValue; |
686 | SourceLocation CurrentPragmaLocation; |
687 | bool HasNonDefaultValue, ShouldWarnOnInclude; |
688 | }; |
689 | SmallVector<AlignPackIncludeState, 8> AlignPackIncludeStack; |
690 | |
691 | PragmaStack<StringLiteral *> DataSegStack; |
692 | PragmaStack<StringLiteral *> BSSSegStack; |
693 | PragmaStack<StringLiteral *> ConstSegStack; |
694 | PragmaStack<StringLiteral *> CodeSegStack; |
695 | |
696 | |
697 | PragmaStack<FPOptionsOverride> FpPragmaStack; |
698 | FPOptionsOverride CurFPFeatureOverrides() { |
699 | FPOptionsOverride result; |
700 | if (!FpPragmaStack.hasValue()) { |
701 | result = FPOptionsOverride(); |
702 | } else { |
703 | result = FpPragmaStack.CurrentValue; |
704 | } |
705 | return result; |
706 | } |
707 | |
708 | |
709 | |
710 | class PragmaStackSentinelRAII { |
711 | public: |
712 | PragmaStackSentinelRAII(Sema &S, StringRef SlotLabel, bool ShouldAct); |
713 | ~PragmaStackSentinelRAII(); |
714 | |
715 | private: |
716 | Sema &S; |
717 | StringRef SlotLabel; |
718 | bool ShouldAct; |
719 | }; |
720 | |
721 | |
722 | FileNullabilityMap NullabilityMap; |
723 | |
724 | |
725 | StringLiteral *CurInitSeg; |
726 | SourceLocation CurInitSegLoc; |
727 | |
728 | |
729 | void *VisContext; |
730 | |
731 | |
732 | struct PragmaAttributeEntry { |
733 | SourceLocation Loc; |
734 | ParsedAttr *Attribute; |
735 | SmallVector<attr::SubjectMatchRule, 4> MatchRules; |
736 | bool IsUsed; |
737 | }; |
738 | |
739 | |
740 | struct PragmaAttributeGroup { |
741 | |
742 | SourceLocation Loc; |
743 | |
744 | const IdentifierInfo *Namespace; |
745 | SmallVector<PragmaAttributeEntry, 2> Entries; |
746 | }; |
747 | |
748 | SmallVector<PragmaAttributeGroup, 2> PragmaAttributeStack; |
749 | |
750 | |
751 | |
752 | const Decl *PragmaAttributeCurrentTargetDecl; |
753 | |
754 | |
755 | |
756 | |
757 | SourceLocation OptimizeOffPragmaLocation; |
758 | |
759 | |
760 | |
761 | |
762 | |
763 | bool IsBuildingRecoveryCallExpr; |
764 | |
765 | |
766 | CleanupInfo Cleanup; |
767 | |
768 | |
769 | |
770 | SmallVector<ExprWithCleanups::CleanupObject, 8> ExprCleanupObjects; |
771 | |
772 | |
773 | |
774 | |
775 | |
776 | |
777 | using MaybeODRUseExprSet = llvm::SetVector<Expr *, SmallVector<Expr *, 4>, |
778 | llvm::SmallPtrSet<Expr *, 4>>; |
779 | MaybeODRUseExprSet MaybeODRUseExprs; |
780 | |
781 | std::unique_ptr<sema::FunctionScopeInfo> CachedFunctionScope; |
782 | |
783 | |
784 | |
785 | SmallVector<sema::FunctionScopeInfo *, 4> FunctionScopes; |
786 | |
787 | |
788 | |
789 | unsigned FunctionScopesStart = 0; |
790 | |
791 | ArrayRef<sema::FunctionScopeInfo*> getFunctionScopes() const { |
792 | return llvm::makeArrayRef(FunctionScopes.begin() + FunctionScopesStart, |
793 | FunctionScopes.end()); |
794 | } |
795 | |
796 | |
797 | |
798 | |
799 | |
800 | |
801 | SmallVector<InventedTemplateParameterInfo, 4> InventedParameterInfos; |
802 | |
803 | |
804 | |
805 | unsigned InventedParameterInfosStart = 0; |
806 | |
807 | ArrayRef<InventedTemplateParameterInfo> getInventedParameterInfos() const { |
808 | return llvm::makeArrayRef(InventedParameterInfos.begin() + |
809 | InventedParameterInfosStart, |
810 | InventedParameterInfos.end()); |
811 | } |
812 | |
813 | typedef LazyVector<TypedefNameDecl *, ExternalSemaSource, |
814 | &ExternalSemaSource::ReadExtVectorDecls, 2, 2> |
815 | ExtVectorDeclsType; |
816 | |
817 | |
818 | |
819 | |
820 | ExtVectorDeclsType ExtVectorDecls; |
821 | |
822 | |
823 | std::unique_ptr<CXXFieldCollector> FieldCollector; |
824 | |
825 | typedef llvm::SmallSetVector<NamedDecl *, 16> NamedDeclSetType; |
826 | |
827 | |
828 | NamedDeclSetType UnusedPrivateFields; |
829 | |
830 | |
831 | llvm::SmallSetVector<const TypedefNameDecl *, 4> |
832 | UnusedLocalTypedefNameCandidates; |
833 | |
834 | |
835 | |
836 | |
837 | |
838 | |
839 | typedef std::pair<SourceLocation, bool> DeleteExprLoc; |
840 | typedef llvm::SmallVector<DeleteExprLoc, 4> DeleteLocs; |
841 | llvm::MapVector<FieldDecl *, DeleteLocs> DeleteExprs; |
842 | |
843 | typedef llvm::SmallPtrSet<const CXXRecordDecl*, 8> RecordDeclSetTy; |
844 | |
845 | |
846 | |
847 | |
848 | std::unique_ptr<RecordDeclSetTy> PureVirtualClassDiagSet; |
849 | |
850 | |
851 | |
852 | llvm::SmallPtrSet<const Decl*, 4> ParsingInitForAutoVars; |
853 | |
854 | |
855 | NamedDecl *findLocallyScopedExternCDecl(DeclarationName Name); |
856 | |
857 | typedef LazyVector<VarDecl *, ExternalSemaSource, |
858 | &ExternalSemaSource::ReadTentativeDefinitions, 2, 2> |
859 | TentativeDefinitionsType; |
860 | |
861 | |
862 | TentativeDefinitionsType TentativeDefinitions; |
863 | |
864 | |
865 | SmallVector<VarDecl *, 4> ExternalDeclarations; |
866 | |
867 | typedef LazyVector<const DeclaratorDecl *, ExternalSemaSource, |
868 | &ExternalSemaSource::ReadUnusedFileScopedDecls, 2, 2> |
869 | UnusedFileScopedDeclsType; |
870 | |
871 | |
872 | |
873 | UnusedFileScopedDeclsType UnusedFileScopedDecls; |
874 | |
875 | typedef LazyVector<CXXConstructorDecl *, ExternalSemaSource, |
876 | &ExternalSemaSource::ReadDelegatingConstructors, 2, 2> |
877 | DelegatingCtorDeclsType; |
878 | |
879 | |
880 | |
881 | DelegatingCtorDeclsType DelegatingCtorDecls; |
882 | |
883 | |
884 | |
885 | |
886 | SmallVector<std::pair<const CXXMethodDecl*, const CXXMethodDecl*>, 2> |
887 | DelayedOverridingExceptionSpecChecks; |
888 | |
889 | |
890 | |
891 | |
892 | |
893 | |
894 | SmallVector<std::pair<FunctionDecl*, FunctionDecl*>, 2> |
895 | DelayedEquivalentExceptionSpecChecks; |
896 | |
897 | typedef llvm::MapVector<const FunctionDecl *, |
898 | std::unique_ptr<LateParsedTemplate>> |
899 | LateParsedTemplateMapT; |
900 | LateParsedTemplateMapT LateParsedTemplateMap; |
901 | |
902 | |
903 | typedef void LateTemplateParserCB(void *P, LateParsedTemplate &LPT); |
904 | typedef void LateTemplateParserCleanupCB(void *P); |
905 | LateTemplateParserCB *LateTemplateParser; |
906 | LateTemplateParserCleanupCB *LateTemplateParserCleanup; |
907 | void *OpaqueParser; |
908 | |
909 | void SetLateTemplateParser(LateTemplateParserCB *LTP, |
910 | LateTemplateParserCleanupCB *LTPCleanup, |
911 | void *P) { |
912 | LateTemplateParser = LTP; |
913 | LateTemplateParserCleanup = LTPCleanup; |
914 | OpaqueParser = P; |
915 | } |
916 | |
917 | |
918 | |
919 | void AddSYCLKernelLambda(const FunctionDecl *FD); |
920 | |
921 | class DelayedDiagnostics; |
922 | |
923 | class DelayedDiagnosticsState { |
924 | sema::DelayedDiagnosticPool *SavedPool; |
925 | friend class Sema::DelayedDiagnostics; |
926 | }; |
927 | typedef DelayedDiagnosticsState ParsingDeclState; |
928 | typedef DelayedDiagnosticsState ProcessingContextState; |
929 | |
930 | |
931 | |
932 | class DelayedDiagnostics { |
933 | |
934 | |
935 | sema::DelayedDiagnosticPool *CurPool; |
936 | |
937 | public: |
938 | DelayedDiagnostics() : CurPool(nullptr) {} |
939 | |
940 | |
941 | void add(const sema::DelayedDiagnostic &diag); |
942 | |
943 | |
944 | bool shouldDelayDiagnostics() { return CurPool != nullptr; } |
945 | |
946 | |
947 | sema::DelayedDiagnosticPool *getCurrentPool() const { |
948 | return CurPool; |
949 | } |
950 | |
951 | |
952 | |
953 | DelayedDiagnosticsState push(sema::DelayedDiagnosticPool &pool) { |
954 | DelayedDiagnosticsState state; |
955 | state.SavedPool = CurPool; |
956 | CurPool = &pool; |
957 | return state; |
958 | } |
959 | |
960 | |
961 | |
962 | |
963 | void popWithoutEmitting(DelayedDiagnosticsState state) { |
964 | CurPool = state.SavedPool; |
965 | } |
966 | |
967 | |
968 | |
969 | DelayedDiagnosticsState pushUndelayed() { |
970 | DelayedDiagnosticsState state; |
971 | state.SavedPool = CurPool; |
972 | CurPool = nullptr; |
973 | return state; |
974 | } |
975 | |
976 | |
977 | void popUndelayed(DelayedDiagnosticsState state) { |
978 | assert(CurPool == nullptr); |
979 | CurPool = state.SavedPool; |
980 | } |
981 | } DelayedDiagnostics; |
982 | |
983 | |
984 | class ContextRAII { |
985 | private: |
986 | Sema &S; |
987 | DeclContext *SavedContext; |
988 | ProcessingContextState SavedContextState; |
989 | QualType SavedCXXThisTypeOverride; |
990 | unsigned SavedFunctionScopesStart; |
991 | unsigned SavedInventedParameterInfosStart; |
992 | |
993 | public: |
994 | ContextRAII(Sema &S, DeclContext *ContextToPush, bool NewThisContext = true) |
995 | : S(S), SavedContext(S.CurContext), |
996 | SavedContextState(S.DelayedDiagnostics.pushUndelayed()), |
997 | SavedCXXThisTypeOverride(S.CXXThisTypeOverride), |
998 | SavedFunctionScopesStart(S.FunctionScopesStart), |
999 | SavedInventedParameterInfosStart(S.InventedParameterInfosStart) |
1000 | { |
1001 | assert(ContextToPush && "pushing null context"); |
1002 | S.CurContext = ContextToPush; |
1003 | if (NewThisContext) |
1004 | S.CXXThisTypeOverride = QualType(); |
1005 | |
1006 | S.FunctionScopesStart = S.FunctionScopes.size(); |
1007 | S.InventedParameterInfosStart = S.InventedParameterInfos.size(); |
1008 | } |
1009 | |
1010 | void pop() { |
1011 | if (!SavedContext) return; |
1012 | S.CurContext = SavedContext; |
1013 | S.DelayedDiagnostics.popUndelayed(SavedContextState); |
1014 | S.CXXThisTypeOverride = SavedCXXThisTypeOverride; |
1015 | S.FunctionScopesStart = SavedFunctionScopesStart; |
1016 | S.InventedParameterInfosStart = SavedInventedParameterInfosStart; |
1017 | SavedContext = nullptr; |
1018 | } |
1019 | |
1020 | ~ContextRAII() { |
1021 | pop(); |
1022 | } |
1023 | }; |
1024 | |
1025 | |
1026 | |
1027 | |
1028 | bool RebuildingImmediateInvocation = false; |
1029 | |
1030 | |
1031 | |
1032 | bool isConstantEvaluatedOverride; |
1033 | |
1034 | bool isConstantEvaluated() { |
1035 | return ExprEvalContexts.back().isConstantEvaluated() || |
1036 | isConstantEvaluatedOverride; |
1037 | } |
1038 | |
1039 | |
1040 | |
1041 | class SynthesizedFunctionScope { |
1042 | Sema &S; |
1043 | Sema::ContextRAII SavedContext; |
1044 | bool PushedCodeSynthesisContext = false; |
1045 | |
1046 | public: |
1047 | SynthesizedFunctionScope(Sema &S, DeclContext *DC) |
1048 | : S(S), SavedContext(S, DC) { |
1049 | S.PushFunctionScope(); |
1050 | S.PushExpressionEvaluationContext( |
1051 | Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
1052 | if (auto *FD = dyn_cast<FunctionDecl>(DC)) |
1053 | FD->setWillHaveBody(true); |
1054 | else |
1055 | assert(isa<ObjCMethodDecl>(DC)); |
1056 | } |
1057 | |
1058 | void addContextNote(SourceLocation UseLoc) { |
1059 | assert(!PushedCodeSynthesisContext); |
1060 | |
1061 | Sema::CodeSynthesisContext Ctx; |
1062 | Ctx.Kind = Sema::CodeSynthesisContext::DefiningSynthesizedFunction; |
1063 | Ctx.PointOfInstantiation = UseLoc; |
1064 | Ctx.Entity = cast<Decl>(S.CurContext); |
1065 | S.pushCodeSynthesisContext(Ctx); |
1066 | |
1067 | PushedCodeSynthesisContext = true; |
1068 | } |
1069 | |
1070 | ~SynthesizedFunctionScope() { |
1071 | if (PushedCodeSynthesisContext) |
1072 | S.popCodeSynthesisContext(); |
1073 | if (auto *FD = dyn_cast<FunctionDecl>(S.CurContext)) |
1074 | FD->setWillHaveBody(false); |
1075 | S.PopExpressionEvaluationContext(); |
1076 | S.PopFunctionScopeInfo(); |
1077 | } |
1078 | }; |
1079 | |
1080 | |
1081 | |
1082 | |
1083 | llvm::MapVector<IdentifierInfo *, WeakInfo> WeakUndeclaredIdentifiers; |
1084 | |
1085 | |
1086 | |
1087 | |
1088 | |
1089 | llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*> ExtnameUndeclaredIdentifiers; |
1090 | |
1091 | |
1092 | |
1093 | void LoadExternalWeakUndeclaredIdentifiers(); |
1094 | |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | SmallVector<Decl*,2> WeakTopLevelDecl; |
1101 | |
1102 | IdentifierResolver IdResolver; |
1103 | |
1104 | |
1105 | |
1106 | |
1107 | Scope *TUScope; |
1108 | |
1109 | |
1110 | LazyDeclPtr StdNamespace; |
1111 | |
1112 | |
1113 | |
1114 | LazyDeclPtr StdBadAlloc; |
1115 | |
1116 | |
1117 | |
1118 | LazyDeclPtr StdAlignValT; |
1119 | |
1120 | |
1121 | |
1122 | NamespaceDecl *StdExperimentalNamespaceCache; |
1123 | |
1124 | |
1125 | |
1126 | ClassTemplateDecl *StdInitializerList; |
1127 | |
1128 | |
1129 | |
1130 | ClassTemplateDecl *StdCoroutineTraitsCache; |
1131 | |
1132 | |
1133 | RecordDecl *CXXTypeInfoDecl; |
1134 | |
1135 | |
1136 | RecordDecl *MSVCGuidDecl; |
1137 | |
1138 | |
1139 | std::unique_ptr<NSAPI> NSAPIObj; |
1140 | |
1141 | |
1142 | ObjCInterfaceDecl *NSNumberDecl; |
1143 | |
1144 | |
1145 | ObjCInterfaceDecl *NSValueDecl; |
1146 | |
1147 | |
1148 | QualType NSNumberPointer; |
1149 | |
1150 | |
1151 | QualType NSValuePointer; |
1152 | |
1153 | |
1154 | ObjCMethodDecl *NSNumberLiteralMethods[NSAPI::NumNSNumberLiteralMethods]; |
1155 | |
1156 | |
1157 | ObjCInterfaceDecl *NSStringDecl; |
1158 | |
1159 | |
1160 | QualType NSStringPointer; |
1161 | |
1162 | |
1163 | ObjCMethodDecl *StringWithUTF8StringMethod; |
1164 | |
1165 | |
1166 | ObjCMethodDecl *ValueWithBytesObjCTypeMethod; |
1167 | |
1168 | |
1169 | ObjCInterfaceDecl *NSArrayDecl; |
1170 | |
1171 | |
1172 | ObjCMethodDecl *ArrayWithObjectsMethod; |
1173 | |
1174 | |
1175 | ObjCInterfaceDecl *NSDictionaryDecl; |
1176 | |
1177 | |
1178 | ObjCMethodDecl *DictionaryWithObjectsMethod; |
1179 | |
1180 | |
1181 | QualType QIDNSCopying; |
1182 | |
1183 | |
1184 | Selector RespondsToSelectorSel; |
1185 | |
1186 | |
1187 | |
1188 | bool GlobalNewDeleteDeclared; |
1189 | |
1190 | |
1191 | |
1192 | enum class ExpressionEvaluationContext { |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | Unevaluated, |
1199 | |
1200 | |
1201 | |
1202 | |
1203 | |
1204 | UnevaluatedList, |
1205 | |
1206 | |
1207 | |
1208 | |
1209 | DiscardedStatement, |
1210 | |
1211 | |
1212 | |
1213 | |
1214 | UnevaluatedAbstract, |
1215 | |
1216 | |
1217 | |
1218 | |
1219 | ConstantEvaluated, |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | PotentiallyEvaluated, |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | |
1234 | PotentiallyEvaluatedIfUsed |
1235 | }; |
1236 | |
1237 | using ImmediateInvocationCandidate = llvm::PointerIntPair<ConstantExpr *, 1>; |
1238 | |
1239 | |
1240 | |
1241 | struct ExpressionEvaluationContextRecord { |
1242 | |
1243 | ExpressionEvaluationContext Context; |
1244 | |
1245 | |
1246 | CleanupInfo ParentCleanup; |
1247 | |
1248 | |
1249 | |
1250 | unsigned NumCleanupObjects; |
1251 | |
1252 | |
1253 | |
1254 | unsigned NumTypos; |
1255 | |
1256 | MaybeODRUseExprSet SavedMaybeODRUseExprs; |
1257 | |
1258 | |
1259 | |
1260 | SmallVector<LambdaExpr *, 2> Lambdas; |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | Decl *ManglingContextDecl; |
1266 | |
1267 | |
1268 | |
1269 | SmallVector<CallExpr *, 8> DelayedDecltypeCalls; |
1270 | |
1271 | |
1272 | |
1273 | SmallVector<CXXBindTemporaryExpr *, 8> DelayedDecltypeBinds; |
1274 | |
1275 | llvm::SmallPtrSet<const Expr *, 8> PossibleDerefs; |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | SmallVector<Expr*, 2> VolatileAssignmentLHSs; |
1281 | |
1282 | |
1283 | llvm::SmallVector<ImmediateInvocationCandidate, 4> ImmediateInvocationCandidates; |
1284 | |
1285 | |
1286 | |
1287 | llvm::SmallPtrSet<DeclRefExpr *, 4> ReferenceToConsteval; |
1288 | |
1289 | |
1290 | |
1291 | enum ExpressionKind { |
1292 | EK_Decltype, EK_TemplateArgument, EK_Other |
1293 | } ExprContext; |
1294 | |
1295 | ExpressionEvaluationContextRecord(ExpressionEvaluationContext Context, |
1296 | unsigned NumCleanupObjects, |
1297 | CleanupInfo ParentCleanup, |
1298 | Decl *ManglingContextDecl, |
1299 | ExpressionKind ExprContext) |
1300 | : Context(Context), ParentCleanup(ParentCleanup), |
1301 | NumCleanupObjects(NumCleanupObjects), NumTypos(0), |
1302 | ManglingContextDecl(ManglingContextDecl), ExprContext(ExprContext) {} |
1303 | |
1304 | bool isUnevaluated() const { |
1305 | return Context == ExpressionEvaluationContext::Unevaluated || |
1306 | Context == ExpressionEvaluationContext::UnevaluatedAbstract || |
1307 | Context == ExpressionEvaluationContext::UnevaluatedList; |
1308 | } |
1309 | bool isConstantEvaluated() const { |
1310 | return Context == ExpressionEvaluationContext::ConstantEvaluated; |
1311 | } |
1312 | }; |
1313 | |
1314 | |
1315 | SmallVector<ExpressionEvaluationContextRecord, 8> ExprEvalContexts; |
1316 | |
1317 | |
1318 | void WarnOnPendingNoDerefs(ExpressionEvaluationContextRecord &Rec); |
1319 | |
1320 | |
1321 | |
1322 | |
1323 | |
1324 | |
1325 | std::tuple<MangleNumberingContext *, Decl *> |
1326 | getCurrentMangleNumberContext(const DeclContext *DC); |
1327 | |
1328 | |
1329 | |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | class SpecialMemberOverloadResult { |
1335 | public: |
1336 | enum Kind { |
1337 | NoMemberOrDeleted, |
1338 | Ambiguous, |
1339 | Success |
1340 | }; |
1341 | |
1342 | private: |
1343 | llvm::PointerIntPair<CXXMethodDecl*, 2> Pair; |
1344 | |
1345 | public: |
1346 | SpecialMemberOverloadResult() : Pair() {} |
1347 | SpecialMemberOverloadResult(CXXMethodDecl *MD) |
1348 | : Pair(MD, MD->isDeleted() ? NoMemberOrDeleted : Success) {} |
1349 | |
1350 | CXXMethodDecl *getMethod() const { return Pair.getPointer(); } |
1351 | void setMethod(CXXMethodDecl *MD) { Pair.setPointer(MD); } |
1352 | |
1353 | Kind getKind() const { return static_cast<Kind>(Pair.getInt()); } |
1354 | void setKind(Kind K) { Pair.setInt(K); } |
1355 | }; |
1356 | |
1357 | class SpecialMemberOverloadResultEntry |
1358 | : public llvm::FastFoldingSetNode, |
1359 | public SpecialMemberOverloadResult { |
1360 | public: |
1361 | SpecialMemberOverloadResultEntry(const llvm::FoldingSetNodeID &ID) |
1362 | : FastFoldingSetNode(ID) |
1363 | {} |
1364 | }; |
1365 | |
1366 | |
1367 | |
1368 | llvm::FoldingSet<SpecialMemberOverloadResultEntry> SpecialMemberCache; |
1369 | |
1370 | |
1371 | |
1372 | mutable llvm::DenseMap<const EnumDecl*, llvm::APInt> FlagBitsCache; |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | |
1381 | const TranslationUnitKind TUKind; |
1382 | |
1383 | llvm::BumpPtrAllocator BumpAlloc; |
1384 | |
1385 | |
1386 | unsigned NumSFINAEErrors; |
1387 | |
1388 | typedef llvm::DenseMap<ParmVarDecl *, llvm::TinyPtrVector<ParmVarDecl *>> |
1389 | UnparsedDefaultArgInstantiationsMap; |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | UnparsedDefaultArgInstantiationsMap UnparsedDefaultArgInstantiations; |
1399 | |
1400 | |
1401 | |
1402 | llvm::DenseMap<ParmVarDecl *, SourceLocation> UnparsedDefaultArgLocs; |
1403 | |
1404 | |
1405 | |
1406 | llvm::MapVector<NamedDecl *, SourceLocation> UndefinedButUsed; |
1407 | |
1408 | |
1409 | |
1410 | |
1411 | bool isExternalWithNoLinkageType(ValueDecl *VD); |
1412 | |
1413 | |
1414 | void getUndefinedButUsed( |
1415 | SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined); |
1416 | |
1417 | |
1418 | |
1419 | const llvm::MapVector<FieldDecl *, DeleteLocs> & |
1420 | getMismatchingDeleteExpressions() const; |
1421 | |
1422 | typedef std::pair<ObjCMethodList, ObjCMethodList> GlobalMethods; |
1423 | typedef llvm::DenseMap<Selector, GlobalMethods> GlobalMethodPool; |
1424 | |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | |
1430 | |
1431 | GlobalMethodPool MethodPool; |
1432 | |
1433 | |
1434 | |
1435 | llvm::MapVector<Selector, SourceLocation> ReferencedSelectors; |
1436 | |
1437 | |
1438 | |
1439 | llvm::SmallVector<std::pair<SourceLocation, const BlockDecl *>, 1> |
1440 | ImplicitlyRetainedSelfLocs; |
1441 | |
1442 | |
1443 | enum CXXSpecialMember { |
1444 | CXXDefaultConstructor, |
1445 | CXXCopyConstructor, |
1446 | CXXMoveConstructor, |
1447 | CXXCopyAssignment, |
1448 | CXXMoveAssignment, |
1449 | CXXDestructor, |
1450 | CXXInvalid |
1451 | }; |
1452 | |
1453 | typedef llvm::PointerIntPair<CXXRecordDecl *, 3, CXXSpecialMember> |
1454 | SpecialMemberDecl; |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | llvm::SmallPtrSet<SpecialMemberDecl, 4> SpecialMembersBeingDeclared; |
1460 | |
1461 | |
1462 | enum class DefaultedComparisonKind : unsigned char { |
1463 | |
1464 | None, |
1465 | |
1466 | |
1467 | Equal, |
1468 | |
1469 | |
1470 | ThreeWay, |
1471 | |
1472 | |
1473 | NotEqual, |
1474 | |
1475 | |
1476 | Relational, |
1477 | }; |
1478 | |
1479 | |
1480 | |
1481 | |
1482 | |
1483 | llvm::SmallPtrSet<const NamedDecl *, 4> TypoCorrectedFunctionDefinitions; |
1484 | |
1485 | |
1486 | |
1487 | llvm::SmallVector<QualType, 4> CurrentParameterCopyTypes; |
1488 | |
1489 | void ReadMethodPool(Selector Sel); |
1490 | void updateOutOfDateSelector(Selector Sel); |
1491 | |
1492 | |
1493 | bool isSelfExpr(Expr *RExpr); |
1494 | bool isSelfExpr(Expr *RExpr, const ObjCMethodDecl *Method); |
1495 | |
1496 | |
1497 | |
1498 | |
1499 | void EmitCurrentDiagnostic(unsigned DiagID); |
1500 | |
1501 | |
1502 | |
1503 | class FPFeaturesStateRAII { |
1504 | public: |
1505 | FPFeaturesStateRAII(Sema &S) : S(S), OldFPFeaturesState(S.CurFPFeatures) { |
1506 | OldOverrides = S.FpPragmaStack.CurrentValue; |
1507 | } |
1508 | ~FPFeaturesStateRAII() { |
1509 | S.CurFPFeatures = OldFPFeaturesState; |
1510 | S.FpPragmaStack.CurrentValue = OldOverrides; |
1511 | } |
1512 | FPOptionsOverride getOverrides() { return OldOverrides; } |
1513 | |
1514 | private: |
1515 | Sema& S; |
1516 | FPOptions OldFPFeaturesState; |
1517 | FPOptionsOverride OldOverrides; |
1518 | }; |
1519 | |
1520 | void addImplicitTypedef(StringRef Name, QualType T); |
1521 | |
1522 | bool WarnedStackExhausted = false; |
1523 | |
1524 | |
1525 | |
1526 | |
1527 | llvm::DenseMap<const VarDecl *, int> RefsMinusAssignments; |
1528 | |
1529 | Optional<std::unique_ptr<DarwinSDKInfo>> CachedDarwinSDKInfo; |
1530 | |
1531 | public: |
1532 | Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
1533 | TranslationUnitKind TUKind = TU_Complete, |
1534 | CodeCompleteConsumer *CompletionConsumer = nullptr); |
1535 | ~Sema(); |
1536 | |
1537 | |
1538 | |
1539 | void Initialize(); |
1540 | |
1541 | |
1542 | |
1543 | |
1544 | |
1545 | |
1546 | virtual void anchor(); |
1547 | |
1548 | const LangOptions &getLangOpts() const { return LangOpts; } |
1549 | OpenCLOptions &getOpenCLOptions() { return OpenCLFeatures; } |
1550 | FPOptions &getCurFPFeatures() { return CurFPFeatures; } |
1551 | |
1552 | DiagnosticsEngine &getDiagnostics() const { return Diags; } |
1553 | SourceManager &getSourceManager() const { return SourceMgr; } |
1554 | Preprocessor &getPreprocessor() const { return PP; } |
1555 | ASTContext &getASTContext() const { return Context; } |
1556 | ASTConsumer &getASTConsumer() const { return Consumer; } |
1557 | ASTMutationListener *getASTMutationListener() const; |
1558 | ExternalSemaSource* getExternalSource() const { return ExternalSource; } |
1559 | DarwinSDKInfo *getDarwinSDKInfoForAvailabilityChecking(SourceLocation Loc, |
1560 | StringRef Platform); |
1561 | |
1562 | |
1563 | |
1564 | |
1565 | |
1566 | |
1567 | void addExternalSource(ExternalSemaSource *E); |
1568 | |
1569 | void PrintStats() const; |
1570 | |
1571 | |
1572 | void warnStackExhausted(SourceLocation Loc); |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | |
1578 | void runWithSufficientStackSpace(SourceLocation Loc, |
1579 | llvm::function_ref<void()> Fn); |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | |
1589 | |
1590 | class ImmediateDiagBuilder : public DiagnosticBuilder { |
1591 | Sema &SemaRef; |
1592 | unsigned DiagID; |
1593 | |
1594 | public: |
1595 | ImmediateDiagBuilder(DiagnosticBuilder &DB, Sema &SemaRef, unsigned DiagID) |
1596 | : DiagnosticBuilder(DB), SemaRef(SemaRef), DiagID(DiagID) {} |
1597 | ImmediateDiagBuilder(DiagnosticBuilder &&DB, Sema &SemaRef, unsigned DiagID) |
1598 | : DiagnosticBuilder(DB), SemaRef(SemaRef), DiagID(DiagID) {} |
1599 | |
1600 | |
1601 | |
1602 | |
1603 | |
1604 | |
1605 | |
1606 | ImmediateDiagBuilder(const ImmediateDiagBuilder &) = default; |
1607 | |
1608 | ~ImmediateDiagBuilder() { |
1609 | |
1610 | if (!isActive()) return; |
1611 | |
1612 | |
1613 | |
1614 | |
1615 | |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | Clear(); |
1621 | |
1622 | |
1623 | SemaRef.EmitCurrentDiagnostic(DiagID); |
1624 | } |
1625 | |
1626 | |
1627 | template <typename T> |
1628 | friend const ImmediateDiagBuilder & |
1629 | operator<<(const ImmediateDiagBuilder &Diag, const T &Value) { |
1630 | const DiagnosticBuilder &BaseDiag = Diag; |
1631 | BaseDiag << Value; |
1632 | return Diag; |
1633 | } |
1634 | |
1635 | |
1636 | |
1637 | |
1638 | template <typename T, typename = typename std::enable_if< |
1639 | !std::is_lvalue_reference<T>::value>::type> |
1640 | const ImmediateDiagBuilder &operator<<(T &&V) const { |
1641 | const DiagnosticBuilder &BaseDiag = *this; |
1642 | BaseDiag << std::move(V); |
1643 | return *this; |
1644 | } |
1645 | }; |
1646 | |
1647 | |
1648 | |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | class SemaDiagnosticBuilder { |
1662 | public: |
1663 | enum Kind { |
1664 | |
1665 | K_Nop, |
1666 | |
1667 | K_Immediate, |
1668 | |
1669 | |
1670 | |
1671 | K_ImmediateWithCallStack, |
1672 | |
1673 | |
1674 | |
1675 | K_Deferred |
1676 | }; |
1677 | |
1678 | SemaDiagnosticBuilder(Kind K, SourceLocation Loc, unsigned DiagID, |
1679 | FunctionDecl *Fn, Sema &S); |
1680 | SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D); |
1681 | SemaDiagnosticBuilder(const SemaDiagnosticBuilder &) = default; |
1682 | ~SemaDiagnosticBuilder(); |
1683 | |
1684 | bool isImmediate() const { return ImmediateDiag.hasValue(); } |
1685 | |
1686 | |
1687 | |
1688 | |
1689 | |
1690 | |
1691 | |
1692 | |
1693 | |
1694 | |
1695 | |
1696 | operator bool() const { return isImmediate(); } |
1697 | |
1698 | template <typename T> |
1699 | friend const SemaDiagnosticBuilder & |
1700 | operator<<(const SemaDiagnosticBuilder &Diag, const T &Value) { |
1701 | if (Diag.ImmediateDiag.hasValue()) |
1702 | *Diag.ImmediateDiag << Value; |
1703 | else if (Diag.PartialDiagId.hasValue()) |
1704 | Diag.S.DeviceDeferredDiags[Diag.Fn][*Diag.PartialDiagId].second |
1705 | << Value; |
1706 | return Diag; |
1707 | } |
1708 | |
1709 | |
1710 | |
1711 | |
1712 | template <typename T, typename = typename std::enable_if< |
1713 | !std::is_lvalue_reference<T>::value>::type> |
1714 | const SemaDiagnosticBuilder &operator<<(T &&V) const { |
1715 | if (ImmediateDiag.hasValue()) |
1716 | *ImmediateDiag << std::move(V); |
1717 | else if (PartialDiagId.hasValue()) |
1718 | S.DeviceDeferredDiags[Fn][*PartialDiagId].second << std::move(V); |
1719 | return *this; |
1720 | } |
1721 | |
1722 | friend const SemaDiagnosticBuilder & |
1723 | operator<<(const SemaDiagnosticBuilder &Diag, const PartialDiagnostic &PD) { |
1724 | if (Diag.ImmediateDiag.hasValue()) |
1725 | PD.Emit(*Diag.ImmediateDiag); |
1726 | else if (Diag.PartialDiagId.hasValue()) |
1727 | Diag.S.DeviceDeferredDiags[Diag.Fn][*Diag.PartialDiagId].second = PD; |
1728 | return Diag; |
1729 | } |
1730 | |
1731 | void AddFixItHint(const FixItHint &Hint) const { |
1732 | if (ImmediateDiag.hasValue()) |
1733 | ImmediateDiag->AddFixItHint(Hint); |
1734 | else if (PartialDiagId.hasValue()) |
1735 | S.DeviceDeferredDiags[Fn][*PartialDiagId].second.AddFixItHint(Hint); |
1736 | } |
1737 | |
1738 | friend ExprResult ExprError(const SemaDiagnosticBuilder &) { |
1739 | return ExprError(); |
1740 | } |
1741 | friend StmtResult StmtError(const SemaDiagnosticBuilder &) { |
1742 | return StmtError(); |
1743 | } |
1744 | operator ExprResult() const { return ExprError(); } |
1745 | operator StmtResult() const { return StmtError(); } |
1746 | operator TypeResult() const { return TypeError(); } |
1747 | operator DeclResult() const { return DeclResult(true); } |
1748 | operator MemInitResult() const { return MemInitResult(true); } |
1749 | |
1750 | private: |
1751 | Sema &S; |
1752 | SourceLocation Loc; |
1753 | unsigned DiagID; |
1754 | FunctionDecl *Fn; |
1755 | bool ShowCallStack; |
1756 | |
1757 | |
1758 | |
1759 | llvm::Optional<ImmediateDiagBuilder> ImmediateDiag; |
1760 | llvm::Optional<unsigned> PartialDiagId; |
1761 | }; |
1762 | |
1763 | |
1764 | |
1765 | bool IsLastErrorImmediate = true; |
1766 | |
1767 | |
1768 | SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, |
1769 | bool DeferHint = false); |
1770 | |
1771 | |
1772 | SemaDiagnosticBuilder Diag(SourceLocation Loc, const PartialDiagnostic &PD, |
1773 | bool DeferHint = false); |
1774 | |
1775 | |
1776 | PartialDiagnostic PDiag(unsigned DiagID = 0); |
1777 | |
1778 | |
1779 | bool DeferDiags = false; |
1780 | |
1781 | |
1782 | class DeferDiagsRAII { |
1783 | Sema &S; |
1784 | bool SavedDeferDiags = false; |
1785 | |
1786 | public: |
1787 | DeferDiagsRAII(Sema &S, bool DeferDiags) |
1788 | : S(S), SavedDeferDiags(S.DeferDiags) { |
1789 | S.DeferDiags = DeferDiags; |
1790 | } |
1791 | ~DeferDiagsRAII() { S.DeferDiags = SavedDeferDiags; } |
1792 | }; |
1793 | |
1794 | |
1795 | |
1796 | bool hasUncompilableErrorOccurred() const; |
1797 | |
1798 | bool findMacroSpelling(SourceLocation &loc, StringRef name); |
1799 | |
1800 | |
1801 | std::string |
1802 | getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const; |
1803 | std::string getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const; |
1804 | |
1805 | |
1806 | SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0); |
1807 | |
1808 | |
1809 | ModuleLoader &getModuleLoader() const; |
1810 | |
1811 | |
1812 | IdentifierInfo * |
1813 | InventAbbreviatedTemplateParameterTypeName(IdentifierInfo *ParamName, |
1814 | unsigned Index); |
1815 | |
1816 | void emitAndClearUnusedLocalTypedefWarnings(); |
1817 | |
1818 | private: |
1819 | |
1820 | |
1821 | llvm::SmallSetVector<Decl *, 4> DeclsToCheckForDeferredDiags; |
1822 | |
1823 | public: |
1824 | |
1825 | void emitDeferredDiags(); |
1826 | |
1827 | enum TUFragmentKind { |
1828 | |
1829 | Global, |
1830 | |
1831 | |
1832 | |
1833 | Normal, |
1834 | |
1835 | |
1836 | Private |
1837 | }; |
1838 | |
1839 | void ActOnStartOfTranslationUnit(); |
1840 | void ActOnEndOfTranslationUnit(); |
1841 | void ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind); |
1842 | |
1843 | void CheckDelegatingCtorCycles(); |
1844 | |
1845 | Scope *getScopeForContext(DeclContext *Ctx); |
1846 | |
1847 | void PushFunctionScope(); |
1848 | void PushBlockScope(Scope *BlockScope, BlockDecl *Block); |
1849 | sema::LambdaScopeInfo *PushLambdaScope(); |
1850 | |
1851 | |
1852 | |
1853 | |
1854 | void RecordParsingTemplateParameterDepth(unsigned Depth); |
1855 | |
1856 | void PushCapturedRegionScope(Scope *RegionScope, CapturedDecl *CD, |
1857 | RecordDecl *RD, CapturedRegionKind K, |
1858 | unsigned OpenMPCaptureLevel = 0); |
1859 | |
1860 | |
1861 | |
1862 | class PoppedFunctionScopeDeleter { |
1863 | Sema *Self; |
1864 | |
1865 | public: |
1866 | explicit PoppedFunctionScopeDeleter(Sema *Self) : Self(Self) {} |
1867 | void operator()(sema::FunctionScopeInfo *Scope) const; |
1868 | }; |
1869 | |
1870 | using PoppedFunctionScopePtr = |
1871 | std::unique_ptr<sema::FunctionScopeInfo, PoppedFunctionScopeDeleter>; |
1872 | |
1873 | PoppedFunctionScopePtr |
1874 | PopFunctionScopeInfo(const sema::AnalysisBasedWarnings::Policy *WP = nullptr, |
1875 | const Decl *D = nullptr, |
1876 | QualType BlockType = QualType()); |
1877 | |
1878 | sema::FunctionScopeInfo *getCurFunction() const { |
1879 | return FunctionScopes.empty() ? nullptr : FunctionScopes.back(); |
1880 | } |
1881 | |
1882 | sema::FunctionScopeInfo *getEnclosingFunction() const; |
1883 | |
1884 | void setFunctionHasBranchIntoScope(); |
1885 | void setFunctionHasBranchProtectedScope(); |
1886 | void setFunctionHasIndirectGoto(); |
1887 | void setFunctionHasMustTail(); |
1888 | |
1889 | void PushCompoundScope(bool IsStmtExpr); |
1890 | void PopCompoundScope(); |
1891 | |
1892 | sema::CompoundScopeInfo &getCurCompoundScope() const; |
1893 | |
1894 | bool hasAnyUnrecoverableErrorsInThisFunction() const; |
1895 | |
1896 | |
1897 | sema::BlockScopeInfo *getCurBlock(); |
1898 | |
1899 | |
1900 | |
1901 | |
1902 | sema::LambdaScopeInfo *getEnclosingLambda() const; |
1903 | |
1904 | |
1905 | |
1906 | |
1907 | |
1908 | sema::LambdaScopeInfo * |
1909 | getCurLambda(bool IgnoreNonLambdaCapturingScope = false); |
1910 | |
1911 | |
1912 | sema::LambdaScopeInfo *getCurGenericLambda(); |
1913 | |
1914 | |
1915 | sema::CapturedRegionScopeInfo *getCurCapturedRegion(); |
1916 | |
1917 | |
1918 | |
1919 | sema::FunctionScopeInfo *getCurFunctionAvailabilityContext(); |
1920 | |
1921 | |
1922 | SmallVectorImpl<Decl *> &WeakTopLevelDecls() { return WeakTopLevelDecl; } |
1923 | |
1924 | |
1925 | |
1926 | void ActOnStartFunctionDeclarationDeclarator(Declarator &D, |
1927 | unsigned TemplateParameterDepth); |
1928 | |
1929 | |
1930 | |
1931 | void ActOnFinishFunctionDeclarationDeclarator(Declarator &D); |
1932 | |
1933 | void ActOnComment(SourceRange Comment); |
1934 | |
1935 | |
1936 | |
1937 | |
1938 | |
1939 | QualType BuildQualifiedType(QualType T, SourceLocation Loc, Qualifiers Qs, |
1940 | const DeclSpec *DS = nullptr); |
1941 | QualType BuildQualifiedType(QualType T, SourceLocation Loc, unsigned CVRA, |
1942 | const DeclSpec *DS = nullptr); |
1943 | QualType BuildPointerType(QualType T, |
1944 | SourceLocation Loc, DeclarationName Entity); |
1945 | QualType BuildReferenceType(QualType T, bool LValueRef, |
1946 | SourceLocation Loc, DeclarationName Entity); |
1947 | QualType BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
1948 | Expr *ArraySize, unsigned Quals, |
1949 | SourceRange Brackets, DeclarationName Entity); |
1950 | QualType BuildVectorType(QualType T, Expr *VecSize, SourceLocation AttrLoc); |
1951 | QualType BuildExtVectorType(QualType T, Expr *ArraySize, |
1952 | SourceLocation AttrLoc); |
1953 | QualType BuildMatrixType(QualType T, Expr *NumRows, Expr *NumColumns, |
1954 | SourceLocation AttrLoc); |
1955 | |
1956 | QualType BuildAddressSpaceAttr(QualType &T, LangAS ASIdx, Expr *AddrSpace, |
1957 | SourceLocation AttrLoc); |
1958 | |
1959 | |
1960 | QualType BuildAddressSpaceAttr(QualType &T, Expr *AddrSpace, |
1961 | SourceLocation AttrLoc); |
1962 | |
1963 | bool CheckQualifiedFunctionForTypeId(QualType T, SourceLocation Loc); |
1964 | |
1965 | bool CheckFunctionReturnType(QualType T, SourceLocation Loc); |
1966 | |
1967 | |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | |
1974 | |
1975 | |
1976 | |
1977 | |
1978 | |
1979 | |
1980 | |
1981 | |
1982 | |
1983 | |
1984 | |
1985 | |
1986 | |
1987 | |
1988 | |
1989 | |
1990 | |
1991 | |
1992 | |
1993 | |
1994 | QualType BuildFunctionType(QualType T, |
1995 | MutableArrayRef<QualType> ParamTypes, |
1996 | SourceLocation Loc, DeclarationName Entity, |
1997 | const FunctionProtoType::ExtProtoInfo &EPI); |
1998 | |
1999 | QualType BuildMemberPointerType(QualType T, QualType Class, |
2000 | SourceLocation Loc, |
2001 | DeclarationName Entity); |
2002 | QualType BuildBlockPointerType(QualType T, |
2003 | SourceLocation Loc, DeclarationName Entity); |
2004 | QualType BuildParenType(QualType T); |
2005 | QualType BuildAtomicType(QualType T, SourceLocation Loc); |
2006 | QualType BuildReadPipeType(QualType T, |
2007 | SourceLocation Loc); |
2008 | QualType BuildWritePipeType(QualType T, |
2009 | SourceLocation Loc); |
2010 | QualType BuildExtIntType(bool IsUnsigned, Expr *BitWidth, SourceLocation Loc); |
2011 | |
2012 | TypeSourceInfo *GetTypeForDeclarator(Declarator &D, Scope *S); |
2013 | TypeSourceInfo *GetTypeForDeclaratorCast(Declarator &D, QualType FromTy); |
2014 | |
2015 | |
2016 | ParsedType CreateParsedType(QualType T, TypeSourceInfo *TInfo); |
2017 | DeclarationNameInfo GetNameForDeclarator(Declarator &D); |
2018 | DeclarationNameInfo GetNameFromUnqualifiedId(const UnqualifiedId &Name); |
2019 | static QualType GetTypeFromParser(ParsedType Ty, |
2020 | TypeSourceInfo **TInfo = nullptr); |
2021 | CanThrowResult canThrow(const Stmt *E); |
2022 | |
2023 | |
2024 | static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D, |
2025 | SourceLocation Loc = SourceLocation()); |
2026 | const FunctionProtoType *ResolveExceptionSpec(SourceLocation Loc, |
2027 | const FunctionProtoType *FPT); |
2028 | void UpdateExceptionSpec(FunctionDecl *FD, |
2029 | const FunctionProtoType::ExceptionSpecInfo &ESI); |
2030 | bool CheckSpecifiedExceptionType(QualType &T, SourceRange Range); |
2031 | bool CheckDistantExceptionSpec(QualType T); |
2032 | bool CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New); |
2033 | bool CheckEquivalentExceptionSpec( |
2034 | const FunctionProtoType *Old, SourceLocation OldLoc, |
2035 | const FunctionProtoType *New, SourceLocation NewLoc); |
2036 | bool CheckEquivalentExceptionSpec( |
2037 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
2038 | const FunctionProtoType *Old, SourceLocation OldLoc, |
2039 | const FunctionProtoType *New, SourceLocation NewLoc); |
2040 | bool handlerCanCatch(QualType HandlerType, QualType ExceptionType); |
2041 | bool CheckExceptionSpecSubset(const PartialDiagnostic &DiagID, |
2042 | const PartialDiagnostic &NestedDiagID, |
2043 | const PartialDiagnostic &NoteID, |
2044 | const PartialDiagnostic &NoThrowDiagID, |
2045 | const FunctionProtoType *Superset, |
2046 | SourceLocation SuperLoc, |
2047 | const FunctionProtoType *Subset, |
2048 | SourceLocation SubLoc); |
2049 | bool CheckParamExceptionSpec(const PartialDiagnostic &NestedDiagID, |
2050 | const PartialDiagnostic &NoteID, |
2051 | const FunctionProtoType *Target, |
2052 | SourceLocation TargetLoc, |
2053 | const FunctionProtoType *Source, |
2054 | SourceLocation SourceLoc); |
2055 | |
2056 | TypeResult ActOnTypeName(Scope *S, Declarator &D); |
2057 | |
2058 | |
2059 | |
2060 | ParsedType ActOnObjCInstanceType(SourceLocation Loc); |
2061 | |
2062 | |
2063 | struct TypeDiagnoser { |
2064 | TypeDiagnoser() {} |
2065 | |
2066 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) = 0; |
2067 | virtual ~TypeDiagnoser() {} |
2068 | }; |
2069 | |
2070 | static int getPrintable(int I) { return I; } |
2071 | static unsigned getPrintable(unsigned I) { return I; } |
2072 | static bool getPrintable(bool B) { return B; } |
2073 | static const char * getPrintable(const char *S) { return S; } |
2074 | static StringRef getPrintable(StringRef S) { return S; } |
2075 | static const std::string &getPrintable(const std::string &S) { return S; } |
2076 | static const IdentifierInfo *getPrintable(const IdentifierInfo *II) { |
2077 | return II; |
2078 | } |
2079 | static DeclarationName getPrintable(DeclarationName N) { return N; } |
2080 | static QualType getPrintable(QualType T) { return T; } |
2081 | static SourceRange getPrintable(SourceRange R) { return R; } |
2082 | static SourceRange getPrintable(SourceLocation L) { return L; } |
2083 | static SourceRange getPrintable(const Expr *E) { return E->getSourceRange(); } |
2084 | static SourceRange getPrintable(TypeLoc TL) { return TL.getSourceRange();} |
2085 | |
2086 | template <typename... Ts> class BoundTypeDiagnoser : public TypeDiagnoser { |
2087 | protected: |
2088 | unsigned DiagID; |
2089 | std::tuple<const Ts &...> Args; |
2090 | |
2091 | template <std::size_t... Is> |
2092 | void emit(const SemaDiagnosticBuilder &DB, |
2093 | std::index_sequence<Is...>) const { |
2094 | |
2095 | bool Dummy[] = {false, (DB << getPrintable(std::get<Is>(Args)))...}; |
2096 | (void)Dummy; |
2097 | } |
2098 | |
2099 | public: |
2100 | BoundTypeDiagnoser(unsigned DiagID, const Ts &...Args) |
2101 | : TypeDiagnoser(), DiagID(DiagID), Args(Args...) { |
2102 | assert(DiagID != 0 && "no diagnostic for type diagnoser"); |
2103 | } |
2104 | |
2105 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
2106 | const SemaDiagnosticBuilder &DB = S.Diag(Loc, DiagID); |
2107 | emit(DB, std::index_sequence_for<Ts...>()); |
2108 | DB << T; |
2109 | } |
2110 | }; |
2111 | |
2112 | |
2113 | |
2114 | |
2115 | |
2116 | |
2117 | |
2118 | |
2119 | bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc, |
2120 | const ParsedAttr &AL, bool IsAsync); |
2121 | |
2122 | |
2123 | |
2124 | |
2125 | |
2126 | template <typename... Ts> |
2127 | class SizelessTypeDiagnoser : public BoundTypeDiagnoser<Ts...> { |
2128 | public: |
2129 | SizelessTypeDiagnoser(unsigned DiagID, const Ts &... Args) |
2130 | : BoundTypeDiagnoser<Ts...>(DiagID, Args...) {} |
2131 | |
2132 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
2133 | const SemaDiagnosticBuilder &DB = S.Diag(Loc, this->DiagID); |
2134 | this->emit(DB, std::index_sequence_for<Ts...>()); |
2135 | DB << T->isSizelessType() << T; |
2136 | } |
2137 | }; |
2138 | |
2139 | enum class CompleteTypeKind { |
2140 | |
2141 | |
2142 | Normal, |
2143 | |
2144 | |
2145 | |
2146 | AcceptSizeless, |
2147 | |
2148 | |
2149 | |
2150 | Default = AcceptSizeless |
2151 | }; |
2152 | |
2153 | private: |
2154 | |
2155 | |
2156 | |
2157 | |
2158 | |
2159 | |
2160 | |
2161 | void CheckSubscriptAccessOfNoDeref(const ArraySubscriptExpr *E); |
2162 | void CheckAddressOfNoDeref(const Expr *E); |
2163 | void CheckMemberAccessOfNoDeref(const MemberExpr *E); |
2164 | |
2165 | bool RequireCompleteTypeImpl(SourceLocation Loc, QualType T, |
2166 | CompleteTypeKind Kind, TypeDiagnoser *Diagnoser); |
2167 | |
2168 | struct ModuleScope { |
2169 | SourceLocation BeginLoc; |
2170 | clang::Module *Module = nullptr; |
2171 | bool ModuleInterface = false; |
2172 | bool ImplicitGlobalModuleFragment = false; |
2173 | VisibleModuleSet OuterVisibleModules; |
2174 | }; |
2175 | |
2176 | llvm::SmallVector<ModuleScope, 16> ModuleScopes; |
2177 | |
2178 | |
2179 | llvm::SmallPtrSet<const NamespaceDecl*, 8> DeferredExportedNamespaces; |
2180 | |
2181 | |
2182 | Module *getCurrentModule() const { |
2183 | return ModuleScopes.empty() ? nullptr : ModuleScopes.back().Module; |
2184 | } |
2185 | |
2186 | VisibleModuleSet VisibleModules; |
2187 | |
2188 | public: |
2189 | |
2190 | Module *getOwningModule(const Decl *Entity) { |
2191 | return Entity->getOwningModule(); |
2192 | } |
2193 | |
2194 | |
2195 | |
2196 | void makeMergedDefinitionVisible(NamedDecl *ND); |
2197 | |
2198 | bool isModuleVisible(const Module *M, bool ModulePrivate = false); |
2199 | |
2200 | |
2201 | |
2202 | void makeModuleVisible(Module *Mod, SourceLocation ImportLoc) { |
2203 | VisibleModules.setVisible(Mod, ImportLoc); |
2204 | } |
2205 | |
2206 | |
2207 | bool isVisible(const NamedDecl *D) { |
2208 | return D->isUnconditionallyVisible() || isVisibleSlow(D); |
2209 | } |
2210 | |
2211 | |
2212 | bool |
2213 | hasVisibleDeclaration(const NamedDecl *D, |
2214 | llvm::SmallVectorImpl<Module *> *Modules = nullptr) { |
2215 | return isVisible(D) || hasVisibleDeclarationSlow(D, Modules); |
2216 | } |
2217 | bool hasVisibleDeclarationSlow(const NamedDecl *D, |
2218 | llvm::SmallVectorImpl<Module *> *Modules); |
2219 | |
2220 | bool hasVisibleMergedDefinition(NamedDecl *Def); |
2221 | bool hasMergedDefinitionInCurrentModule(NamedDecl *Def); |
2222 | |
2223 | |
2224 | |
2225 | bool hasStructuralCompatLayout(Decl *D, Decl *Suggested); |
2226 | |
2227 | |
2228 | |
2229 | bool hasVisibleDefinition(NamedDecl *D, NamedDecl **Suggested, |
2230 | bool OnlyNeedComplete = false); |
2231 | bool hasVisibleDefinition(const NamedDecl *D) { |
2232 | NamedDecl *Hidden; |
2233 | return hasVisibleDefinition(const_cast<NamedDecl*>(D), &Hidden); |
2234 | } |
2235 | |
2236 | |
2237 | bool |
2238 | hasVisibleDefaultArgument(const NamedDecl *D, |
2239 | llvm::SmallVectorImpl<Module *> *Modules = nullptr); |
2240 | |
2241 | |
2242 | |
2243 | |
2244 | bool hasVisibleExplicitSpecialization( |
2245 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules = nullptr); |
2246 | |
2247 | |
2248 | |
2249 | bool hasVisibleMemberSpecialization( |
2250 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules = nullptr); |
2251 | |
2252 | |
2253 | |
2254 | |
2255 | bool isEquivalentInternalLinkageDeclaration(const NamedDecl *A, |
2256 | const NamedDecl *B); |
2257 | void diagnoseEquivalentInternalLinkageDeclarations( |
2258 | SourceLocation Loc, const NamedDecl *D, |
2259 | ArrayRef<const NamedDecl *> Equiv); |
2260 | |
2261 | bool isUsualDeallocationFunction(const CXXMethodDecl *FD); |
2262 | |
2263 | bool isCompleteType(SourceLocation Loc, QualType T, |
2264 | CompleteTypeKind Kind = CompleteTypeKind::Default) { |
2265 | return !RequireCompleteTypeImpl(Loc, T, Kind, nullptr); |
2266 | } |
2267 | bool RequireCompleteType(SourceLocation Loc, QualType T, |
2268 | CompleteTypeKind Kind, TypeDiagnoser &Diagnoser); |
2269 | bool RequireCompleteType(SourceLocation Loc, QualType T, |
2270 | CompleteTypeKind Kind, unsigned DiagID); |
2271 | |
2272 | bool RequireCompleteType(SourceLocation Loc, QualType T, |
2273 | TypeDiagnoser &Diagnoser) { |
2274 | return RequireCompleteType(Loc, T, CompleteTypeKind::Default, Diagnoser); |
2275 | } |
2276 | bool RequireCompleteType(SourceLocation Loc, QualType T, unsigned DiagID) { |
2277 | return RequireCompleteType(Loc, T, CompleteTypeKind::Default, DiagID); |
2278 | } |
2279 | |
2280 | template <typename... Ts> |
2281 | bool RequireCompleteType(SourceLocation Loc, QualType T, unsigned DiagID, |
2282 | const Ts &...Args) { |
2283 | BoundTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...); |
2284 | return RequireCompleteType(Loc, T, Diagnoser); |
2285 | } |
2286 | |
2287 | template <typename... Ts> |
2288 | bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, |
2289 | const Ts &... Args) { |
2290 | SizelessTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...); |
2291 | return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); |
2292 | } |
2293 | |
2294 | |
2295 | |
2296 | |
2297 | |
2298 | |
2299 | |
2300 | |
2301 | |
2302 | QualType getCompletedType(Expr *E); |
2303 | |
2304 | void completeExprArrayBound(Expr *E); |
2305 | bool RequireCompleteExprType(Expr *E, CompleteTypeKind Kind, |
2306 | TypeDiagnoser &Diagnoser); |
2307 | bool RequireCompleteExprType(Expr *E, unsigned DiagID); |
2308 | |
2309 | template <typename... Ts> |
2310 | bool RequireCompleteExprType(Expr *E, unsigned DiagID, const Ts &...Args) { |
2311 | BoundTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...); |
2312 | return RequireCompleteExprType(E, CompleteTypeKind::Default, Diagnoser); |
2313 | } |
2314 | |
2315 | template <typename... Ts> |
2316 | bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, |
2317 | const Ts &... Args) { |
2318 | SizelessTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...); |
2319 | return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser); |
2320 | } |
2321 | |
2322 | bool RequireLiteralType(SourceLocation Loc, QualType T, |
2323 | TypeDiagnoser &Diagnoser); |
2324 | bool RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID); |
2325 | |
2326 | template <typename... Ts> |
2327 | bool RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID, |
2328 | const Ts &...Args) { |
2329 | BoundTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...); |
2330 | return RequireLiteralType(Loc, T, Diagnoser); |
2331 | } |
2332 | |
2333 | QualType getElaboratedType(ElaboratedTypeKeyword Keyword, |
2334 | const CXXScopeSpec &SS, QualType T, |
2335 | TagDecl *OwnedTagDecl = nullptr); |
2336 | |
2337 | QualType getDecltypeForParenthesizedExpr(Expr *E); |
2338 | QualType BuildTypeofExprType(Expr *E, SourceLocation Loc); |
2339 | |
2340 | |
2341 | QualType BuildDecltypeType(Expr *E, SourceLocation Loc, |
2342 | bool AsUnevaluated = true); |
2343 | QualType BuildUnaryTransformType(QualType BaseType, |
2344 | UnaryTransformType::UTTKind UKind, |
2345 | SourceLocation Loc); |
2346 | |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | struct SkipBodyInfo { |
2352 | SkipBodyInfo() |
2353 | : ShouldSkip(false), CheckSameAsPrevious(false), Previous(nullptr), |
2354 | New(nullptr) {} |
2355 | bool ShouldSkip; |
2356 | bool CheckSameAsPrevious; |
2357 | NamedDecl *Previous; |
2358 | NamedDecl *New; |
2359 | }; |
|