clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaTemplate.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/SemaTemplate.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | #include "TreeTransform.h" |
12 | #include "clang/AST/ASTConsumer.h" |
13 | #include "clang/AST/ASTContext.h" |
14 | #include "clang/AST/DeclFriend.h" |
15 | #include "clang/AST/DeclTemplate.h" |
16 | #include "clang/AST/Expr.h" |
17 | #include "clang/AST/ExprCXX.h" |
18 | #include "clang/AST/RecursiveASTVisitor.h" |
19 | #include "clang/AST/TypeVisitor.h" |
20 | #include "clang/Basic/Builtins.h" |
21 | #include "clang/Basic/LangOptions.h" |
22 | #include "clang/Basic/PartialDiagnostic.h" |
23 | #include "clang/Basic/Stack.h" |
24 | #include "clang/Basic/TargetInfo.h" |
25 | #include "clang/Sema/DeclSpec.h" |
26 | #include "clang/Sema/Initialization.h" |
27 | #include "clang/Sema/Lookup.h" |
28 | #include "clang/Sema/Overload.h" |
29 | #include "clang/Sema/ParsedTemplate.h" |
30 | #include "clang/Sema/Scope.h" |
31 | #include "clang/Sema/SemaInternal.h" |
32 | #include "clang/Sema/Template.h" |
33 | #include "clang/Sema/TemplateDeduction.h" |
34 | #include "llvm/ADT/SmallBitVector.h" |
35 | #include "llvm/ADT/SmallString.h" |
36 | #include "llvm/ADT/StringExtras.h" |
37 | |
38 | #include <iterator> |
39 | using namespace clang; |
40 | using namespace sema; |
41 | |
42 | |
43 | SourceRange |
44 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
45 | unsigned N) { |
46 | if (!N) return SourceRange(); |
47 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
48 | } |
49 | |
50 | unsigned Sema::getTemplateDepth(Scope *S) const { |
51 | unsigned Depth = 0; |
52 | |
53 | |
54 | |
55 | for (Scope *TempParamScope = S->getTemplateParamParent(); TempParamScope; |
56 | TempParamScope = TempParamScope->getParent()->getTemplateParamParent()) { |
57 | ++Depth; |
58 | } |
59 | |
60 | |
61 | auto ParamsAtDepth = [&](unsigned D) { Depth = std::max(Depth, D + 1); }; |
62 | |
63 | |
64 | |
65 | for (FunctionScopeInfo *FSI : getFunctionScopes()) { |
66 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(FSI)) { |
67 | if (!LSI->TemplateParams.empty()) { |
68 | ParamsAtDepth(LSI->AutoTemplateParameterDepth); |
69 | break; |
70 | } |
71 | if (LSI->GLTemplateParameterList) { |
72 | ParamsAtDepth(LSI->GLTemplateParameterList->getDepth()); |
73 | break; |
74 | } |
75 | } |
76 | } |
77 | |
78 | |
79 | |
80 | for (const InventedTemplateParameterInfo &Info : |
81 | getInventedParameterInfos()) { |
82 | if (!Info.TemplateParams.empty()) { |
83 | ParamsAtDepth(Info.AutoTemplateParameterDepth); |
84 | break; |
85 | } |
86 | } |
87 | |
88 | return Depth; |
89 | } |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | NamedDecl *Sema::getAsTemplateNameDecl(NamedDecl *D, |
98 | bool AllowFunctionTemplates, |
99 | bool AllowDependent) { |
100 | D = D->getUnderlyingDecl(); |
101 | |
102 | if (isa<TemplateDecl>(D)) { |
103 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
104 | return nullptr; |
105 | |
106 | return D; |
107 | } |
108 | |
109 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | if (Record->isInjectedClassName()) { |
122 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
123 | if (Record->getDescribedClassTemplate()) |
124 | return Record->getDescribedClassTemplate(); |
125 | |
126 | if (ClassTemplateSpecializationDecl *Spec |
127 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
128 | return Spec->getSpecializedTemplate(); |
129 | } |
130 | |
131 | return nullptr; |
132 | } |
133 | |
134 | |
135 | |
136 | |
137 | if (AllowDependent && isa<UnresolvedUsingValueDecl>(D)) |
138 | return D; |
139 | |
140 | return nullptr; |
141 | } |
142 | |
143 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
144 | bool AllowFunctionTemplates, |
145 | bool AllowDependent) { |
146 | LookupResult::Filter filter = R.makeFilter(); |
147 | while (filter.hasNext()) { |
148 | NamedDecl *Orig = filter.next(); |
149 | if (!getAsTemplateNameDecl(Orig, AllowFunctionTemplates, AllowDependent)) |
150 | filter.erase(); |
151 | } |
152 | filter.done(); |
153 | } |
154 | |
155 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
156 | bool AllowFunctionTemplates, |
157 | bool AllowDependent, |
158 | bool AllowNonTemplateFunctions) { |
159 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { |
160 | if (getAsTemplateNameDecl(*I, AllowFunctionTemplates, AllowDependent)) |
161 | return true; |
162 | if (AllowNonTemplateFunctions && |
163 | isa<FunctionDecl>((*I)->getUnderlyingDecl())) |
164 | return true; |
165 | } |
166 | |
167 | return false; |
168 | } |
169 | |
170 | TemplateNameKind Sema::isTemplateName(Scope *S, |
171 | CXXScopeSpec &SS, |
172 | bool hasTemplateKeyword, |
173 | const UnqualifiedId &Name, |
174 | ParsedType ObjectTypePtr, |
175 | bool EnteringContext, |
176 | TemplateTy &TemplateResult, |
177 | bool &MemberOfUnknownSpecialization, |
178 | bool Disambiguation) { |
179 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
180 | |
181 | DeclarationName TName; |
182 | MemberOfUnknownSpecialization = false; |
183 | |
184 | switch (Name.getKind()) { |
185 | case UnqualifiedIdKind::IK_Identifier: |
186 | TName = DeclarationName(Name.Identifier); |
187 | break; |
188 | |
189 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
190 | TName = Context.DeclarationNames.getCXXOperatorName( |
191 | Name.OperatorFunctionId.Operator); |
192 | break; |
193 | |
194 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
195 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
196 | break; |
197 | |
198 | default: |
199 | return TNK_Non_template; |
200 | } |
201 | |
202 | QualType ObjectType = ObjectTypePtr.get(); |
203 | |
204 | AssumedTemplateKind AssumedTemplate; |
205 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
206 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
207 | MemberOfUnknownSpecialization, SourceLocation(), |
208 | &AssumedTemplate, |
209 | !Disambiguation)) |
210 | return TNK_Non_template; |
211 | |
212 | if (AssumedTemplate != AssumedTemplateKind::None) { |
213 | TemplateResult = TemplateTy::make(Context.getAssumedTemplateName(TName)); |
214 | |
215 | |
216 | |
217 | return AssumedTemplate == AssumedTemplateKind::FoundNothing |
218 | ? TNK_Undeclared_template |
219 | : TNK_Function_template; |
220 | } |
221 | |
222 | if (R.empty()) |
223 | return TNK_Non_template; |
224 | |
225 | NamedDecl *D = nullptr; |
226 | if (R.isAmbiguous()) { |
227 | |
228 | |
229 | bool AnyFunctionTemplates = false; |
230 | for (NamedDecl *FoundD : R) { |
231 | if (NamedDecl *FoundTemplate = getAsTemplateNameDecl(FoundD)) { |
232 | if (isa<FunctionTemplateDecl>(FoundTemplate)) |
233 | AnyFunctionTemplates = true; |
234 | else { |
235 | D = FoundTemplate; |
236 | break; |
237 | } |
238 | } |
239 | } |
240 | |
241 | |
242 | |
243 | if (!D && !AnyFunctionTemplates) { |
244 | R.suppressDiagnostics(); |
245 | return TNK_Non_template; |
246 | } |
247 | |
248 | |
249 | |
250 | if (!D) |
251 | FilterAcceptableTemplateNames(R); |
252 | } |
253 | |
254 | |
255 | |
256 | |
257 | |
258 | TemplateName Template; |
259 | TemplateNameKind TemplateKind; |
260 | |
261 | unsigned ResultCount = R.end() - R.begin(); |
262 | if (!D && ResultCount > 1) { |
263 | |
264 | |
265 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
266 | TemplateKind = TNK_Function_template; |
267 | |
268 | |
269 | R.suppressDiagnostics(); |
270 | } else { |
271 | if (!D) { |
272 | D = getAsTemplateNameDecl(*R.begin()); |
273 | assert(D && "unambiguous result is not a template name"); |
274 | } |
275 | |
276 | if (isa<UnresolvedUsingValueDecl>(D)) { |
277 | |
278 | MemberOfUnknownSpecialization = true; |
279 | return TNK_Non_template; |
280 | } |
281 | |
282 | TemplateDecl *TD = cast<TemplateDecl>(D); |
283 | |
284 | if (SS.isSet() && !SS.isInvalid()) { |
285 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
286 | Template = Context.getQualifiedTemplateName(Qualifier, |
287 | hasTemplateKeyword, TD); |
288 | } else { |
289 | Template = TemplateName(TD); |
290 | } |
291 | |
292 | if (isa<FunctionTemplateDecl>(TD)) { |
293 | TemplateKind = TNK_Function_template; |
294 | |
295 | |
296 | R.suppressDiagnostics(); |
297 | } else { |
298 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
299 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
300 | isa<BuiltinTemplateDecl>(TD) || isa<ConceptDecl>(TD)); |
301 | TemplateKind = |
302 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : |
303 | isa<ConceptDecl>(TD) ? TNK_Concept_template : |
304 | TNK_Type_template; |
305 | } |
306 | } |
307 | |
308 | TemplateResult = TemplateTy::make(Template); |
309 | return TemplateKind; |
310 | } |
311 | |
312 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
313 | SourceLocation NameLoc, |
314 | ParsedTemplateTy *Template) { |
315 | CXXScopeSpec SS; |
316 | bool MemberOfUnknownSpecialization = false; |
317 | |
318 | |
319 | |
320 | |
321 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
322 | if (LookupTemplateName(R, S, SS, QualType(), |
323 | false, |
324 | MemberOfUnknownSpecialization)) |
325 | return false; |
326 | |
327 | if (R.empty()) return false; |
328 | if (R.isAmbiguous()) { |
329 | |
330 | R.suppressDiagnostics(); |
331 | return false; |
332 | } |
333 | |
334 | |
335 | |
336 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
337 | if (!TD || !getAsTypeTemplateDecl(TD)) |
338 | return false; |
339 | |
340 | if (Template) |
341 | *Template = TemplateTy::make(TemplateName(TD)); |
342 | return true; |
343 | } |
344 | |
345 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
346 | SourceLocation IILoc, |
347 | Scope *S, |
348 | const CXXScopeSpec *SS, |
349 | TemplateTy &SuggestedTemplate, |
350 | TemplateNameKind &SuggestedKind) { |
351 | |
352 | |
353 | |
354 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
355 | computeDeclContext(*SS)) |
356 | return false; |
357 | |
358 | |
359 | |
360 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
361 | Diag(IILoc, diag::err_template_kw_missing) |
362 | << Qualifier << II.getName() |
363 | << FixItHint::CreateInsertion(IILoc, "template "); |
364 | SuggestedTemplate |
365 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
366 | SuggestedKind = TNK_Dependent_template_name; |
367 | return true; |
368 | } |
369 | |
370 | bool Sema::LookupTemplateName(LookupResult &Found, |
371 | Scope *S, CXXScopeSpec &SS, |
372 | QualType ObjectType, |
373 | bool EnteringContext, |
374 | bool &MemberOfUnknownSpecialization, |
375 | RequiredTemplateKind RequiredTemplate, |
376 | AssumedTemplateKind *ATK, |
377 | bool AllowTypoCorrection) { |
378 | if (ATK) |
| |
379 | *ATK = AssumedTemplateKind::None; |
380 | |
381 | if (SS.isInvalid()) |
| 8 | | Calling 'CXXScopeSpec::isInvalid' | |
|
| 10 | | Returning from 'CXXScopeSpec::isInvalid' | |
|
| |
382 | return true; |
383 | |
384 | Found.setTemplateNameLookup(true); |
385 | |
386 | |
387 | MemberOfUnknownSpecialization = false; |
388 | DeclContext *LookupCtx = nullptr; |
389 | bool IsDependent = false; |
390 | if (!ObjectType.isNull()) { |
| 12 | | Calling 'QualType::isNull' | |
|
| 26 | | Returning from 'QualType::isNull' | |
|
| |
391 | |
392 | |
393 | assert(SS.isEmpty() && "ObjectType and scope specifier cannot coexist"); |
394 | LookupCtx = computeDeclContext(ObjectType); |
395 | IsDependent = !LookupCtx && ObjectType->isDependentType(); |
396 | assert((IsDependent || !ObjectType->isIncompleteType() || |
397 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
398 | "Caller should have completed object type"); |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | |
414 | if (ObjectType->isObjCObjectOrInterfaceType() || |
415 | ObjectType->isVectorType()) { |
416 | Found.clear(); |
417 | return false; |
418 | } |
419 | } else if (SS.isNotEmpty()) { |
| 28 | | Calling 'CXXScopeSpec::isNotEmpty' | |
|
| 34 | | Returning from 'CXXScopeSpec::isNotEmpty' | |
|
| |
420 | |
421 | |
422 | LookupCtx = computeDeclContext(SS, EnteringContext); |
423 | IsDependent = !LookupCtx && isDependentScopeSpecifier(SS); |
| 36 | | Assuming 'LookupCtx' is non-null | |
|
424 | |
425 | |
426 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
| 37 | | Assuming the condition is false | |
|
| |
427 | return true; |
428 | } |
429 | |
430 | bool ObjectTypeSearchedInScope = false; |
431 | bool AllowFunctionTemplatesInLookup = true; |
432 | if (LookupCtx) { |
| |
433 | |
434 | |
435 | |
436 | |
437 | LookupQualifiedName(Found, LookupCtx); |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | |
445 | |
446 | |
447 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
448 | } |
449 | |
450 | if (SS.isEmpty() && (ObjectType.isNull() || Found.empty())) { |
451 | |
452 | |
453 | |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | |
460 | if (S) |
461 | LookupName(Found, S); |
462 | |
463 | if (!ObjectType.isNull()) { |
464 | |
465 | |
466 | |
467 | AllowFunctionTemplatesInLookup = false; |
468 | ObjectTypeSearchedInScope = true; |
469 | } |
470 | |
471 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
472 | } |
473 | |
474 | if (Found.isAmbiguous()) |
| 40 | | Assuming the condition is true | |
|
| |
475 | return false; |
| 42 | | Returning zero, which participates in a condition later | |
|
476 | |
477 | if (ATK && SS.isEmpty() && ObjectType.isNull() && |
478 | !RequiredTemplate.hasTemplateKeyword()) { |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | bool AllFunctions = |
488 | getLangOpts().CPlusPlus20 && |
489 | std::all_of(Found.begin(), Found.end(), [](NamedDecl *ND) { |
490 | return isa<FunctionDecl>(ND->getUnderlyingDecl()); |
491 | }); |
492 | if (AllFunctions || (Found.empty() && !IsDependent)) { |
493 | |
494 | |
495 | |
496 | *ATK = (Found.empty() && Found.getLookupName().isIdentifier()) |
497 | ? AssumedTemplateKind::FoundNothing |
498 | : AssumedTemplateKind::FoundFunctions; |
499 | Found.clear(); |
500 | return false; |
501 | } |
502 | } |
503 | |
504 | if (Found.empty() && !IsDependent && AllowTypoCorrection) { |
505 | |
506 | |
507 | DeclarationName Name = Found.getLookupName(); |
508 | Found.clear(); |
509 | |
510 | DefaultFilterCCC FilterCCC{}; |
511 | FilterCCC.WantTypeSpecifiers = false; |
512 | FilterCCC.WantExpressionKeywords = false; |
513 | FilterCCC.WantRemainingKeywords = false; |
514 | FilterCCC.WantCXXNamedCasts = true; |
515 | if (TypoCorrection Corrected = |
516 | CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S, |
517 | &SS, FilterCCC, CTK_ErrorRecovery, LookupCtx)) { |
518 | if (auto *ND = Corrected.getFoundDecl()) |
519 | Found.addDecl(ND); |
520 | FilterAcceptableTemplateNames(Found); |
521 | if (Found.isAmbiguous()) { |
522 | Found.clear(); |
523 | } else if (!Found.empty()) { |
524 | Found.setLookupName(Corrected.getCorrection()); |
525 | if (LookupCtx) { |
526 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
527 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
528 | Name.getAsString() == CorrectedStr; |
529 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
530 | << Name << LookupCtx << DroppedSpecifier |
531 | << SS.getRange()); |
532 | } else { |
533 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
534 | } |
535 | } |
536 | } |
537 | } |
538 | |
539 | NamedDecl *ExampleLookupResult = |
540 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); |
541 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
542 | if (Found.empty()) { |
543 | if (IsDependent) { |
544 | MemberOfUnknownSpecialization = true; |
545 | return false; |
546 | } |
547 | |
548 | |
549 | |
550 | if (ExampleLookupResult && RequiredTemplate) { |
551 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
552 | << Found.getLookupName() << SS.getRange() |
553 | << RequiredTemplate.hasTemplateKeyword() |
554 | << RequiredTemplate.getTemplateKeywordLoc(); |
555 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
556 | diag::note_template_kw_refers_to_non_template) |
557 | << Found.getLookupName(); |
558 | return true; |
559 | } |
560 | |
561 | return false; |
562 | } |
563 | |
564 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
565 | !getLangOpts().CPlusPlus11) { |
566 | |
567 | |
568 | |
569 | |
570 | |
571 | |
572 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
573 | LookupOrdinaryName); |
574 | FoundOuter.setTemplateNameLookup(true); |
575 | LookupName(FoundOuter, S); |
576 | |
577 | |
578 | FilterAcceptableTemplateNames(FoundOuter, false); |
579 | |
580 | NamedDecl *OuterTemplate; |
581 | if (FoundOuter.empty()) { |
582 | |
583 | |
584 | } else if (FoundOuter.isAmbiguous() || !FoundOuter.isSingleResult() || |
585 | !(OuterTemplate = |
586 | getAsTemplateNameDecl(FoundOuter.getFoundDecl()))) { |
587 | |
588 | |
589 | |
590 | FoundOuter.clear(); |
591 | } else if (!Found.isSuppressingDiagnostics()) { |
592 | |
593 | |
594 | |
595 | if (!Found.isSingleResult() || |
596 | getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() != |
597 | OuterTemplate->getCanonicalDecl()) { |
598 | Diag(Found.getNameLoc(), |
599 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
600 | << Found.getLookupName() |
601 | << ObjectType; |
602 | Diag(Found.getRepresentativeDecl()->getLocation(), |
603 | diag::note_ambig_member_ref_object_type) |
604 | << ObjectType; |
605 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
606 | diag::note_ambig_member_ref_scope); |
607 | |
608 | |
609 | |
610 | } |
611 | } |
612 | } |
613 | |
614 | return false; |
615 | } |
616 | |
617 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
618 | SourceLocation Less, |
619 | SourceLocation Greater) { |
620 | if (TemplateName.isInvalid()) |
621 | return; |
622 | |
623 | DeclarationNameInfo NameInfo; |
624 | CXXScopeSpec SS; |
625 | LookupNameKind LookupKind; |
626 | |
627 | DeclContext *LookupCtx = nullptr; |
628 | NamedDecl *Found = nullptr; |
629 | bool MissingTemplateKeyword = false; |
630 | |
631 | |
632 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
633 | NameInfo = DRE->getNameInfo(); |
634 | SS.Adopt(DRE->getQualifierLoc()); |
635 | LookupKind = LookupOrdinaryName; |
636 | Found = DRE->getFoundDecl(); |
637 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
638 | NameInfo = ME->getMemberNameInfo(); |
639 | SS.Adopt(ME->getQualifierLoc()); |
640 | LookupKind = LookupMemberName; |
641 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
642 | Found = ME->getMemberDecl(); |
643 | } else if (auto *DSDRE = |
644 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
645 | NameInfo = DSDRE->getNameInfo(); |
646 | SS.Adopt(DSDRE->getQualifierLoc()); |
647 | MissingTemplateKeyword = true; |
648 | } else if (auto *DSME = |
649 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
650 | NameInfo = DSME->getMemberNameInfo(); |
651 | SS.Adopt(DSME->getQualifierLoc()); |
652 | MissingTemplateKeyword = true; |
653 | } else { |
654 | llvm_unreachable("unexpected kind of potential template name"); |
655 | } |
656 | |
657 | |
658 | |
659 | if (MissingTemplateKeyword) { |
660 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
661 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
662 | return; |
663 | } |
664 | |
665 | |
666 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
667 | Sema &S; |
668 | TemplateCandidateFilter(Sema &S) : S(S) { |
669 | WantTypeSpecifiers = false; |
670 | WantExpressionKeywords = false; |
671 | WantRemainingKeywords = false; |
672 | WantCXXNamedCasts = true; |
673 | }; |
674 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
675 | if (auto *ND = Candidate.getCorrectionDecl()) |
676 | return S.getAsTemplateNameDecl(ND); |
677 | return Candidate.isKeyword(); |
678 | } |
679 | |
680 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
681 | return std::make_unique<TemplateCandidateFilter>(*this); |
682 | } |
683 | }; |
684 | |
685 | DeclarationName Name = NameInfo.getName(); |
686 | TemplateCandidateFilter CCC(*this); |
687 | if (TypoCorrection Corrected = CorrectTypo(NameInfo, LookupKind, S, &SS, CCC, |
688 | CTK_ErrorRecovery, LookupCtx)) { |
689 | auto *ND = Corrected.getFoundDecl(); |
690 | if (ND) |
691 | ND = getAsTemplateNameDecl(ND); |
692 | if (ND || Corrected.isKeyword()) { |
693 | if (LookupCtx) { |
694 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
695 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
696 | Name.getAsString() == CorrectedStr; |
697 | diagnoseTypo(Corrected, |
698 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
699 | << Name << LookupCtx << DroppedSpecifier |
700 | << SS.getRange(), false); |
701 | } else { |
702 | diagnoseTypo(Corrected, |
703 | PDiag(diag::err_non_template_in_template_id_suggest) |
704 | << Name, false); |
705 | } |
706 | if (Found) |
707 | Diag(Found->getLocation(), |
708 | diag::note_non_template_in_template_id_found); |
709 | return; |
710 | } |
711 | } |
712 | |
713 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
714 | << Name << SourceRange(Less, Greater); |
715 | if (Found) |
716 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
717 | } |
718 | |
719 | |
720 | |
721 | |
722 | ExprResult |
723 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
724 | SourceLocation TemplateKWLoc, |
725 | const DeclarationNameInfo &NameInfo, |
726 | bool isAddressOfOperand, |
727 | const TemplateArgumentListInfo *TemplateArgs) { |
728 | DeclContext *DC = getFunctionLevelDeclContext(); |
729 | |
730 | |
731 | |
732 | |
733 | |
734 | |
735 | |
736 | |
737 | |
738 | |
739 | |
740 | |
741 | bool MightBeCxx11UnevalField = |
742 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
743 | |
744 | |
745 | bool IsEnum = false; |
746 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
747 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
748 | |
749 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
750 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
751 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); |
752 | |
753 | |
754 | |
755 | NamedDecl *FirstQualifierInScope = nullptr; |
756 | |
757 | return CXXDependentScopeMemberExpr::Create( |
758 | Context, nullptr, ThisType, true, |
759 | SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
760 | FirstQualifierInScope, NameInfo, TemplateArgs); |
761 | } |
762 | |
763 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
764 | } |
765 | |
766 | ExprResult |
767 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
768 | SourceLocation TemplateKWLoc, |
769 | const DeclarationNameInfo &NameInfo, |
770 | const TemplateArgumentListInfo *TemplateArgs) { |
771 | |
772 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
773 | if (!QualifierLoc) |
774 | return ExprError(); |
775 | |
776 | return DependentScopeDeclRefExpr::Create( |
777 | Context, QualifierLoc, TemplateKWLoc, NameInfo, TemplateArgs); |
778 | } |
779 | |
780 | |
781 | |
782 | |
783 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
784 | NamedDecl *Instantiation, |
785 | bool InstantiatedFromMember, |
786 | const NamedDecl *Pattern, |
787 | const NamedDecl *PatternDef, |
788 | TemplateSpecializationKind TSK, |
789 | bool Complain ) { |
790 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
791 | isa<VarDecl>(Instantiation)); |
792 | |
793 | bool IsEntityBeingDefined = false; |
794 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
795 | IsEntityBeingDefined = TD->isBeingDefined(); |
796 | |
797 | if (PatternDef && !IsEntityBeingDefined) { |
798 | NamedDecl *SuggestedDef = nullptr; |
799 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
800 | false)) { |
801 | |
802 | bool Recover = Complain && !isSFINAEContext(); |
803 | if (Complain) |
804 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
805 | Sema::MissingImportKind::Definition, Recover); |
806 | return !Recover; |
807 | } |
808 | return false; |
809 | } |
810 | |
811 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
812 | return true; |
813 | |
814 | llvm::Optional<unsigned> Note; |
815 | QualType InstantiationTy; |
816 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
817 | InstantiationTy = Context.getTypeDeclType(TD); |
818 | if (PatternDef) { |
819 | Diag(PointOfInstantiation, |
820 | diag::err_template_instantiate_within_definition) |
821 | << (TSK != TSK_ImplicitInstantiation) |
822 | << InstantiationTy; |
823 | |
824 | |
825 | Instantiation->setInvalidDecl(); |
826 | } else if (InstantiatedFromMember) { |
827 | if (isa<FunctionDecl>(Instantiation)) { |
828 | Diag(PointOfInstantiation, |
829 | diag::err_explicit_instantiation_undefined_member) |
830 | << 1 << Instantiation->getDeclName() |
831 | << Instantiation->getDeclContext(); |
832 | Note = diag::note_explicit_instantiation_here; |
833 | } else { |
834 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
835 | Diag(PointOfInstantiation, |
836 | diag::err_implicit_instantiate_member_undefined) |
837 | << InstantiationTy; |
838 | Note = diag::note_member_declared_at; |
839 | } |
840 | } else { |
841 | if (isa<FunctionDecl>(Instantiation)) { |
842 | Diag(PointOfInstantiation, |
843 | diag::err_explicit_instantiation_undefined_func_template) |
844 | << Pattern; |
845 | Note = diag::note_explicit_instantiation_here; |
846 | } else if (isa<TagDecl>(Instantiation)) { |
847 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
848 | << (TSK != TSK_ImplicitInstantiation) |
849 | << InstantiationTy; |
850 | Note = diag::note_template_decl_here; |
851 | } else { |
852 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
853 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
854 | Diag(PointOfInstantiation, |
855 | diag::err_explicit_instantiation_undefined_var_template) |
856 | << Instantiation; |
857 | Instantiation->setInvalidDecl(); |
858 | } else |
859 | Diag(PointOfInstantiation, |
860 | diag::err_explicit_instantiation_undefined_member) |
861 | << 2 << Instantiation->getDeclName() |
862 | << Instantiation->getDeclContext(); |
863 | Note = diag::note_explicit_instantiation_here; |
864 | } |
865 | } |
866 | if (Note) |
867 | Diag(Pattern->getLocation(), Note.getValue()); |
868 | |
869 | |
870 | |
871 | |
872 | |
873 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
874 | Instantiation->setInvalidDecl(); |
875 | return true; |
876 | } |
877 | |
878 | |
879 | |
880 | |
881 | |
882 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
883 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
884 | |
885 | |
886 | |
887 | |
888 | |
889 | |
890 | unsigned DiagId = getLangOpts().MSVCCompat ? diag::ext_template_param_shadow |
891 | : diag::err_template_param_shadow; |
892 | Diag(Loc, DiagId) << cast<NamedDecl>(PrevDecl)->getDeclName(); |
893 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
894 | } |
895 | |
896 | |
897 | |
898 | |
899 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
900 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
901 | D = Temp->getTemplatedDecl(); |
902 | return Temp; |
903 | } |
904 | return nullptr; |
905 | } |
906 | |
907 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
908 | SourceLocation EllipsisLoc) const { |
909 | assert(Kind == Template && |
910 | "Only template template arguments can be pack expansions here"); |
911 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
912 | "Template template argument pack expansion without packs"); |
913 | ParsedTemplateArgument Result(*this); |
914 | Result.EllipsisLoc = EllipsisLoc; |
915 | return Result; |
916 | } |
917 | |
918 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
919 | const ParsedTemplateArgument &Arg) { |
920 | |
921 | switch (Arg.getKind()) { |
922 | case ParsedTemplateArgument::Type: { |
923 | TypeSourceInfo *DI; |
924 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
925 | if (!DI) |
926 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
927 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
928 | } |
929 | |
930 | case ParsedTemplateArgument::NonType: { |
931 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
932 | return TemplateArgumentLoc(TemplateArgument(E), E); |
933 | } |
934 | |
935 | case ParsedTemplateArgument::Template: { |
936 | TemplateName Template = Arg.getAsTemplate().get(); |
937 | TemplateArgument TArg; |
938 | if (Arg.getEllipsisLoc().isValid()) |
939 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
940 | else |
941 | TArg = Template; |
942 | return TemplateArgumentLoc( |
943 | SemaRef.Context, TArg, |
944 | Arg.getScopeSpec().getWithLocInContext(SemaRef.Context), |
945 | Arg.getLocation(), Arg.getEllipsisLoc()); |
946 | } |
947 | } |
948 | |
949 | llvm_unreachable("Unhandled parsed template argument"); |
950 | } |
951 | |
952 | |
953 | |
954 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
955 | TemplateArgumentListInfo &TemplateArgs) { |
956 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
957 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
958 | TemplateArgsIn[I])); |
959 | } |
960 | |
961 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
962 | SourceLocation Loc, |
963 | IdentifierInfo *Name) { |
964 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
965 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
966 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
967 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
968 | } |
969 | |
970 | |
971 | |
972 | |
973 | |
974 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
975 | TypeSourceInfo *TInfo; |
976 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
977 | if (T.isNull()) |
978 | return ParsedTemplateArgument(); |
979 | assert(TInfo && "template argument with no location"); |
980 | |
981 | |
982 | |
983 | if (getLangOpts().CPlusPlus17) { |
984 | TypeLoc TL = TInfo->getTypeLoc(); |
985 | SourceLocation EllipsisLoc; |
986 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
987 | EllipsisLoc = PET.getEllipsisLoc(); |
988 | TL = PET.getPatternLoc(); |
989 | } |
990 | |
991 | CXXScopeSpec SS; |
992 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
993 | SS.Adopt(ET.getQualifierLoc()); |
994 | TL = ET.getNamedTypeLoc(); |
995 | } |
996 | |
997 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
998 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
999 | if (SS.isSet()) |
1000 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
1001 | false, |
1002 | Name.getAsTemplateDecl()); |
1003 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
1004 | DTST.getTemplateNameLoc()); |
1005 | if (EllipsisLoc.isValid()) |
1006 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
1007 | return Result; |
1008 | } |
1009 | } |
1010 | |
1011 | |
1012 | |
1013 | |
1014 | |
1015 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
1016 | ParsedType.get().getAsOpaquePtr(), |
1017 | TInfo->getTypeLoc().getBeginLoc()); |
1018 | } |
1019 | |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | |
1025 | |
1026 | |
1027 | |
1028 | |
1029 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
1030 | SourceLocation EllipsisLoc, |
1031 | SourceLocation KeyLoc, |
1032 | IdentifierInfo *ParamName, |
1033 | SourceLocation ParamNameLoc, |
1034 | unsigned Depth, unsigned Position, |
1035 | SourceLocation EqualLoc, |
1036 | ParsedType DefaultArg, |
1037 | bool HasTypeConstraint) { |
1038 | assert(S->isTemplateParamScope() && |
1039 | "Template type parameter not in template parameter scope!"); |
1040 | |
1041 | bool IsParameterPack = EllipsisLoc.isValid(); |
1042 | TemplateTypeParmDecl *Param |
1043 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
1044 | KeyLoc, ParamNameLoc, Depth, Position, |
1045 | ParamName, Typename, IsParameterPack, |
1046 | HasTypeConstraint); |
1047 | Param->setAccess(AS_public); |
1048 | |
1049 | if (Param->isParameterPack()) |
1050 | if (auto *LSI = getEnclosingLambda()) |
1051 | LSI->LocalPacks.push_back(Param); |
1052 | |
1053 | if (ParamName) { |
1054 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
1055 | |
1056 | |
1057 | S->AddDecl(Param); |
1058 | IdResolver.AddDecl(Param); |
1059 | } |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | if (DefaultArg && IsParameterPack) { |
1065 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1066 | DefaultArg = nullptr; |
1067 | } |
1068 | |
1069 | |
1070 | if (DefaultArg) { |
1071 | TypeSourceInfo *DefaultTInfo; |
1072 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
1073 | |
1074 | assert(DefaultTInfo && "expected source information for type"); |
1075 | |
1076 | |
1077 | if (DiagnoseUnexpandedParameterPack(ParamNameLoc, DefaultTInfo, |
1078 | UPPC_DefaultArgument)) |
1079 | return Param; |
1080 | |
1081 | |
1082 | if (CheckTemplateArgument(DefaultTInfo)) { |
1083 | Param->setInvalidDecl(); |
1084 | return Param; |
1085 | } |
1086 | |
1087 | Param->setDefaultArgument(DefaultTInfo); |
1088 | } |
1089 | |
1090 | return Param; |
1091 | } |
1092 | |
1093 | |
1094 | static TemplateArgumentListInfo |
1095 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
1096 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
1097 | TemplateId.RAngleLoc); |
1098 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
1099 | TemplateId.NumArgs); |
1100 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
1101 | return TemplateArgs; |
1102 | } |
1103 | |
1104 | bool Sema::ActOnTypeConstraint(const CXXScopeSpec &SS, |
1105 | TemplateIdAnnotation *TypeConstr, |
1106 | TemplateTypeParmDecl *ConstrainedParameter, |
1107 | SourceLocation EllipsisLoc) { |
1108 | return BuildTypeConstraint(SS, TypeConstr, ConstrainedParameter, EllipsisLoc, |
1109 | false); |
1110 | } |
1111 | |
1112 | bool Sema::BuildTypeConstraint(const CXXScopeSpec &SS, |
1113 | TemplateIdAnnotation *TypeConstr, |
1114 | TemplateTypeParmDecl *ConstrainedParameter, |
1115 | SourceLocation EllipsisLoc, |
1116 | bool AllowUnexpandedPack) { |
1117 | TemplateName TN = TypeConstr->Template.get(); |
1118 | ConceptDecl *CD = cast<ConceptDecl>(TN.getAsTemplateDecl()); |
1119 | |
1120 | |
1121 | |
1122 | |
1123 | if (!CD->isTypeConcept()) { |
1124 | Diag(TypeConstr->TemplateNameLoc, |
1125 | diag::err_type_constraint_non_type_concept); |
1126 | return true; |
1127 | } |
1128 | |
1129 | bool WereArgsSpecified = TypeConstr->LAngleLoc.isValid(); |
1130 | |
1131 | if (!WereArgsSpecified && |
1132 | CD->getTemplateParameters()->getMinRequiredArguments() > 1) { |
1133 | Diag(TypeConstr->TemplateNameLoc, |
1134 | diag::err_type_constraint_missing_arguments) << CD; |
1135 | return true; |
1136 | } |
1137 | |
1138 | DeclarationNameInfo ConceptName(DeclarationName(TypeConstr->Name), |
1139 | TypeConstr->TemplateNameLoc); |
1140 | |
1141 | TemplateArgumentListInfo TemplateArgs; |
1142 | if (TypeConstr->LAngleLoc.isValid()) { |
1143 | TemplateArgs = |
1144 | makeTemplateArgumentListInfo(*this, *TypeConstr); |
1145 | |
1146 | if (EllipsisLoc.isInvalid() && !AllowUnexpandedPack) { |
1147 | for (TemplateArgumentLoc Arg : TemplateArgs.arguments()) { |
1148 | if (DiagnoseUnexpandedParameterPack(Arg, UPPC_TypeConstraint)) |
1149 | return true; |
1150 | } |
1151 | } |
1152 | } |
1153 | return AttachTypeConstraint( |
1154 | SS.isSet() ? SS.getWithLocInContext(Context) : NestedNameSpecifierLoc(), |
1155 | ConceptName, CD, |
1156 | TypeConstr->LAngleLoc.isValid() ? &TemplateArgs : nullptr, |
1157 | ConstrainedParameter, EllipsisLoc); |
1158 | } |
1159 | |
1160 | template<typename ArgumentLocAppender> |
1161 | static ExprResult formImmediatelyDeclaredConstraint( |
1162 | Sema &S, NestedNameSpecifierLoc NS, DeclarationNameInfo NameInfo, |
1163 | ConceptDecl *NamedConcept, SourceLocation LAngleLoc, |
1164 | SourceLocation RAngleLoc, QualType ConstrainedType, |
1165 | SourceLocation ParamNameLoc, ArgumentLocAppender Appender, |
1166 | SourceLocation EllipsisLoc) { |
1167 | |
1168 | TemplateArgumentListInfo ConstraintArgs; |
1169 | ConstraintArgs.addArgument( |
1170 | S.getTrivialTemplateArgumentLoc(TemplateArgument(ConstrainedType), |
1171 | QualType(), ParamNameLoc)); |
1172 | |
1173 | ConstraintArgs.setRAngleLoc(RAngleLoc); |
1174 | ConstraintArgs.setLAngleLoc(LAngleLoc); |
1175 | Appender(ConstraintArgs); |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | CXXScopeSpec SS; |
1181 | SS.Adopt(NS); |
1182 | ExprResult ImmediatelyDeclaredConstraint = S.CheckConceptTemplateId( |
1183 | SS, SourceLocation(), NameInfo, |
1184 | NamedConcept, NamedConcept, &ConstraintArgs); |
1185 | if (ImmediatelyDeclaredConstraint.isInvalid() || !EllipsisLoc.isValid()) |
1186 | return ImmediatelyDeclaredConstraint; |
1187 | |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | |
1199 | |
1200 | return S.BuildCXXFoldExpr(nullptr, |
1201 | SourceLocation(), |
1202 | ImmediatelyDeclaredConstraint.get(), BO_LAnd, |
1203 | EllipsisLoc, nullptr, |
1204 | SourceLocation(), |
1205 | None); |
1206 | } |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | bool Sema::AttachTypeConstraint(NestedNameSpecifierLoc NS, |
1213 | DeclarationNameInfo NameInfo, |
1214 | ConceptDecl *NamedConcept, |
1215 | const TemplateArgumentListInfo *TemplateArgs, |
1216 | TemplateTypeParmDecl *ConstrainedParameter, |
1217 | SourceLocation EllipsisLoc) { |
1218 | |
1219 | |
1220 | |
1221 | const ASTTemplateArgumentListInfo *ArgsAsWritten = |
1222 | TemplateArgs ? ASTTemplateArgumentListInfo::Create(Context, |
1223 | *TemplateArgs) : nullptr; |
1224 | |
1225 | QualType ParamAsArgument(ConstrainedParameter->getTypeForDecl(), 0); |
1226 | |
1227 | ExprResult ImmediatelyDeclaredConstraint = |
1228 | formImmediatelyDeclaredConstraint( |
1229 | *this, NS, NameInfo, NamedConcept, |
1230 | TemplateArgs ? TemplateArgs->getLAngleLoc() : SourceLocation(), |
1231 | TemplateArgs ? TemplateArgs->getRAngleLoc() : SourceLocation(), |
1232 | ParamAsArgument, ConstrainedParameter->getLocation(), |
1233 | [&] (TemplateArgumentListInfo &ConstraintArgs) { |
1234 | if (TemplateArgs) |
1235 | for (const auto &ArgLoc : TemplateArgs->arguments()) |
1236 | ConstraintArgs.addArgument(ArgLoc); |
1237 | }, EllipsisLoc); |
1238 | if (ImmediatelyDeclaredConstraint.isInvalid()) |
1239 | return true; |
1240 | |
1241 | ConstrainedParameter->setTypeConstraint(NS, NameInfo, |
1242 | NamedConcept, |
1243 | NamedConcept, ArgsAsWritten, |
1244 | ImmediatelyDeclaredConstraint.get()); |
1245 | return false; |
1246 | } |
1247 | |
1248 | bool Sema::AttachTypeConstraint(AutoTypeLoc TL, NonTypeTemplateParmDecl *NTTP, |
1249 | SourceLocation EllipsisLoc) { |
1250 | if (NTTP->getType() != TL.getType() || |
1251 | TL.getAutoKeyword() != AutoTypeKeyword::Auto) { |
1252 | Diag(NTTP->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
1253 | diag::err_unsupported_placeholder_constraint) |
1254 | << NTTP->getTypeSourceInfo()->getTypeLoc().getSourceRange(); |
1255 | return true; |
1256 | } |
1257 | |
1258 | |
1259 | DeclRefExpr *Ref = |
1260 | BuildDeclRefExpr(NTTP, NTTP->getType(), VK_PRValue, NTTP->getLocation()); |
1261 | if (!Ref) |
1262 | return true; |
1263 | ExprResult ImmediatelyDeclaredConstraint = |
1264 | formImmediatelyDeclaredConstraint( |
1265 | *this, TL.getNestedNameSpecifierLoc(), TL.getConceptNameInfo(), |
1266 | TL.getNamedConcept(), TL.getLAngleLoc(), TL.getRAngleLoc(), |
1267 | BuildDecltypeType(Ref, NTTP->getLocation()), NTTP->getLocation(), |
1268 | [&] (TemplateArgumentListInfo &ConstraintArgs) { |
1269 | for (unsigned I = 0, C = TL.getNumArgs(); I != C; ++I) |
1270 | ConstraintArgs.addArgument(TL.getArgLoc(I)); |
1271 | }, EllipsisLoc); |
1272 | if (ImmediatelyDeclaredConstraint.isInvalid() || |
1273 | !ImmediatelyDeclaredConstraint.isUsable()) |
1274 | return true; |
1275 | |
1276 | NTTP->setPlaceholderTypeConstraint(ImmediatelyDeclaredConstraint.get()); |
1277 | return false; |
1278 | } |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | |
1285 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
1286 | SourceLocation Loc) { |
1287 | if (TSI->getType()->isUndeducedType()) { |
1288 | |
1289 | |
1290 | |
1291 | |
1292 | |
1293 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
1294 | } |
1295 | |
1296 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
1297 | } |
1298 | |
1299 | |
1300 | |
1301 | |
1302 | bool Sema::RequireStructuralType(QualType T, SourceLocation Loc) { |
1303 | if (T->isDependentType()) |
1304 | return false; |
1305 | |
1306 | if (RequireCompleteType(Loc, T, diag::err_template_nontype_parm_incomplete)) |
1307 | return true; |
1308 | |
1309 | if (T->isStructuralType()) |
1310 | return false; |
1311 | |
1312 | |
1313 | if (T->isRValueReferenceType()) { |
1314 | Diag(Loc, diag::err_template_nontype_parm_rvalue_ref) << T; |
1315 | return true; |
1316 | } |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | |
1322 | if (!getLangOpts().CPlusPlus20 || |
1323 | (!T->isScalarType() && !T->isRecordType())) { |
1324 | Diag(Loc, diag::err_template_nontype_parm_bad_type) << T; |
1325 | return true; |
1326 | } |
1327 | |
1328 | |
1329 | if (RequireLiteralType(Loc, T, diag::err_template_nontype_parm_not_literal)) |
1330 | return true; |
1331 | |
1332 | Diag(Loc, diag::err_template_nontype_parm_not_structural) << T; |
1333 | |
1334 | |
1335 | while (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { |
1336 | |
1337 | |
1338 | |
1339 | for (const FieldDecl *FD : RD->fields()) { |
1340 | if (FD->getAccess() != AS_public) { |
1341 | Diag(FD->getLocation(), diag::note_not_structural_non_public) << T << 0; |
1342 | return true; |
1343 | } |
1344 | if (FD->isMutable()) { |
1345 | Diag(FD->getLocation(), diag::note_not_structural_mutable_field) << T; |
1346 | return true; |
1347 | } |
1348 | if (FD->getType()->isRValueReferenceType()) { |
1349 | Diag(FD->getLocation(), diag::note_not_structural_rvalue_ref_field) |
1350 | << T; |
1351 | return true; |
1352 | } |
1353 | } |
1354 | |
1355 | |
1356 | for (const auto &BaseSpec : RD->bases()) { |
1357 | if (BaseSpec.getAccessSpecifier() != AS_public) { |
1358 | Diag(BaseSpec.getBaseTypeLoc(), diag::note_not_structural_non_public) |
1359 | << T << 1; |
1360 | return true; |
1361 | } |
1362 | } |
1363 | |
1364 | |
1365 | SourceLocation SubLoc; |
1366 | QualType SubType; |
1367 | int Kind = -1; |
1368 | |
1369 | for (const FieldDecl *FD : RD->fields()) { |
1370 | QualType T = Context.getBaseElementType(FD->getType()); |
1371 | if (!T->isStructuralType()) { |
1372 | SubLoc = FD->getLocation(); |
1373 | SubType = T; |
1374 | Kind = 0; |
1375 | break; |
1376 | } |
1377 | } |
1378 | |
1379 | if (Kind == -1) { |
1380 | for (const auto &BaseSpec : RD->bases()) { |
1381 | QualType T = BaseSpec.getType(); |
1382 | if (!T->isStructuralType()) { |
1383 | SubLoc = BaseSpec.getBaseTypeLoc(); |
1384 | SubType = T; |
1385 | Kind = 1; |
1386 | break; |
1387 | } |
1388 | } |
1389 | } |
1390 | |
1391 | assert(Kind != -1 && "couldn't find reason why type is not structural"); |
1392 | Diag(SubLoc, diag::note_not_structural_subobject) |
1393 | << T << Kind << SubType; |
1394 | T = SubType; |
1395 | RD = T->getAsCXXRecordDecl(); |
1396 | } |
1397 | |
1398 | return true; |
1399 | } |
1400 | |
1401 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
1402 | SourceLocation Loc) { |
1403 | |
1404 | |
1405 | if (T->isVariablyModifiedType()) { |
1406 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
1407 | << T; |
1408 | return QualType(); |
1409 | } |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | if (T->isIntegralOrEnumerationType() || |
1418 | |
1419 | T->isPointerType() || |
1420 | |
1421 | T->isLValueReferenceType() || |
1422 | |
1423 | T->isMemberPointerType() || |
1424 | |
1425 | T->isNullPtrType() || |
1426 | |
1427 | T->isUndeducedType()) { |
1428 | |
1429 | |
1430 | return T.getUnqualifiedType(); |
1431 | } |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | if (T->isArrayType() || T->isFunctionType()) |
1439 | return Context.getDecayedType(T); |
1440 | |
1441 | |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | |
1447 | if (T->isDependentType()) |
1448 | return T.getUnqualifiedType(); |
1449 | |
1450 | |
1451 | |
1452 | if (RequireStructuralType(T, Loc)) |
1453 | return QualType(); |
1454 | |
1455 | if (!getLangOpts().CPlusPlus20) { |
1456 | |
1457 | |
1458 | |
1459 | Diag(Loc, diag::err_template_nontype_parm_bad_structural_type) << T; |
1460 | return QualType(); |
1461 | } |
1462 | |
1463 | Diag(Loc, diag::warn_cxx17_compat_template_nontype_parm_type) << T; |
1464 | return T.getUnqualifiedType(); |
1465 | } |
1466 | |
1467 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
1468 | unsigned Depth, |
1469 | unsigned Position, |
1470 | SourceLocation EqualLoc, |
1471 | Expr *Default) { |
1472 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
1473 | |
1474 | |
1475 | auto CheckValidDeclSpecifiers = [this, &D] { |
1476 | |
1477 | |
1478 | |
1479 | |
1480 | |
1481 | |
1482 | |
1483 | |
1484 | |
1485 | |
1486 | |
1487 | const DeclSpec &DS = D.getDeclSpec(); |
1488 | auto EmitDiag = [this](SourceLocation Loc) { |
1489 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
1490 | << FixItHint::CreateRemoval(Loc); |
1491 | }; |
1492 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
1493 | EmitDiag(DS.getStorageClassSpecLoc()); |
1494 | |
1495 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
1496 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
1497 | |
1498 | |
1499 | |
1500 | |
1501 | |
1502 | if (DS.isInlineSpecified()) |
1503 | EmitDiag(DS.getInlineSpecLoc()); |
1504 | |
1505 | |
1506 | |
1507 | |
1508 | |
1509 | |
1510 | if (DS.hasConstexprSpecifier()) |
1511 | EmitDiag(DS.getConstexprSpecLoc()); |
1512 | |
1513 | |
1514 | |
1515 | |
1516 | if (DS.isVirtualSpecified()) |
1517 | EmitDiag(DS.getVirtualSpecLoc()); |
1518 | |
1519 | if (DS.hasExplicitSpecifier()) |
1520 | EmitDiag(DS.getExplicitSpecLoc()); |
1521 | |
1522 | if (DS.isNoreturnSpecified()) |
1523 | EmitDiag(DS.getNoreturnSpecLoc()); |
1524 | }; |
1525 | |
1526 | CheckValidDeclSpecifiers(); |
1527 | |
1528 | if (TInfo->getType()->isUndeducedType()) { |
1529 | Diag(D.getIdentifierLoc(), |
1530 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
1531 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
1532 | } |
1533 | |
1534 | assert(S->isTemplateParamScope() && |
1535 | "Non-type template parameter not in template parameter scope!"); |
1536 | bool Invalid = false; |
1537 | |
1538 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
1539 | if (T.isNull()) { |
1540 | T = Context.IntTy; |
1541 | Invalid = true; |
1542 | } |
1543 | |
1544 | CheckFunctionOrTemplateParamDeclarator(S, D); |
1545 | |
1546 | IdentifierInfo *ParamName = D.getIdentifier(); |
1547 | bool IsParameterPack = D.hasEllipsis(); |
1548 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
1549 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
1550 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
1551 | TInfo); |
1552 | Param->setAccess(AS_public); |
1553 | |
1554 | if (AutoTypeLoc TL = TInfo->getTypeLoc().getContainedAutoTypeLoc()) |
1555 | if (TL.isConstrained()) |
1556 | if (AttachTypeConstraint(TL, Param, D.getEllipsisLoc())) |
1557 | Invalid = true; |
1558 | |
1559 | if (Invalid) |
1560 | Param->setInvalidDecl(); |
1561 | |
1562 | if (Param->isParameterPack()) |
1563 | if (auto *LSI = getEnclosingLambda()) |
1564 | LSI->LocalPacks.push_back(Param); |
1565 | |
1566 | if (ParamName) { |
1567 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
1568 | ParamName); |
1569 | |
1570 | |
1571 | S->AddDecl(Param); |
1572 | IdResolver.AddDecl(Param); |
1573 | } |
1574 | |
1575 | |
1576 | |
1577 | |
1578 | if (Default && IsParameterPack) { |
1579 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1580 | Default = nullptr; |
1581 | } |
1582 | |
1583 | |
1584 | if (Default) { |
1585 | |
1586 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
1587 | return Param; |
1588 | |
1589 | TemplateArgument Converted; |
1590 | ExprResult DefaultRes = |
1591 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
1592 | if (DefaultRes.isInvalid()) { |
1593 | Param->setInvalidDecl(); |
1594 | return Param; |
1595 | } |
1596 | Default = DefaultRes.get(); |
1597 | |
1598 | Param->setDefaultArgument(Default); |
1599 | } |
1600 | |
1601 | return Param; |
1602 | } |
1603 | |
1604 | |
1605 | |
1606 | |
1607 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
1608 | SourceLocation TmpLoc, |
1609 | TemplateParameterList *Params, |
1610 | SourceLocation EllipsisLoc, |
1611 | IdentifierInfo *Name, |
1612 | SourceLocation NameLoc, |
1613 | unsigned Depth, |
1614 | unsigned Position, |
1615 | SourceLocation EqualLoc, |
1616 | ParsedTemplateArgument Default) { |
1617 | assert(S->isTemplateParamScope() && |
1618 | "Template template parameter not in template parameter scope!"); |
1619 | |
1620 | |
1621 | bool IsParameterPack = EllipsisLoc.isValid(); |
1622 | TemplateTemplateParmDecl *Param = |
1623 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
1624 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
1625 | Depth, Position, IsParameterPack, |
1626 | Name, Params); |
1627 | Param->setAccess(AS_public); |
1628 | |
1629 | if (Param->isParameterPack()) |
1630 | if (auto *LSI = getEnclosingLambda()) |
1631 | LSI->LocalPacks.push_back(Param); |
1632 | |
1633 | |
1634 | |
1635 | if (Name) { |
1636 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
1637 | |
1638 | S->AddDecl(Param); |
1639 | IdResolver.AddDecl(Param); |
1640 | } |
1641 | |
1642 | if (Params->size() == 0) { |
1643 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
1644 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
1645 | Param->setInvalidDecl(); |
1646 | } |
1647 | |
1648 | |
1649 | |
1650 | |
1651 | if (IsParameterPack && !Default.isInvalid()) { |
1652 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
1653 | Default = ParsedTemplateArgument(); |
1654 | } |
1655 | |
1656 | if (!Default.isInvalid()) { |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | |
1662 | |
1663 | |
1664 | |
1665 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
1666 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
1667 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
1668 | << DefaultArg.getSourceRange(); |
1669 | return Param; |
1670 | } |
1671 | |
1672 | |
1673 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
1674 | DefaultArg.getArgument().getAsTemplate(), |
1675 | UPPC_DefaultArgument)) |
1676 | return Param; |
1677 | |
1678 | Param->setDefaultArgument(Context, DefaultArg); |
1679 | } |
1680 | |
1681 | return Param; |
1682 | } |
1683 | |
1684 | |
1685 | |
1686 | |
1687 | TemplateParameterList * |
1688 | Sema::ActOnTemplateParameterList(unsigned Depth, |
1689 | SourceLocation ExportLoc, |
1690 | SourceLocation TemplateLoc, |
1691 | SourceLocation LAngleLoc, |
1692 | ArrayRef<NamedDecl *> Params, |
1693 | SourceLocation RAngleLoc, |
1694 | Expr *RequiresClause) { |
1695 | if (ExportLoc.isValid()) |
1696 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
1697 | |
1698 | for (NamedDecl *P : Params) |
1699 | warnOnReservedIdentifier(P); |
1700 | |
1701 | return TemplateParameterList::Create( |
1702 | Context, TemplateLoc, LAngleLoc, |
1703 | llvm::makeArrayRef(Params.data(), Params.size()), |
1704 | RAngleLoc, RequiresClause); |
1705 | } |
1706 | |
1707 | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, |
1708 | const CXXScopeSpec &SS) { |
1709 | if (SS.isSet()) |
1710 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
1711 | } |
1712 | |
1713 | DeclResult Sema::CheckClassTemplate( |
1714 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
1715 | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, |
1716 | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, |
1717 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
1718 | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, |
1719 | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { |
1720 | assert(TemplateParams && TemplateParams->size() > 0 && |
1721 | "No template parameters"); |
1722 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
1723 | bool Invalid = false; |
1724 | |
1725 | |
1726 | if (CheckTemplateDeclScope(S, TemplateParams)) |
1727 | return true; |
1728 | |
1729 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
1730 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
1731 | |
1732 | |
1733 | if (!Name) { |
1734 | Diag(KWLoc, diag::err_template_unnamed_class); |
1735 | return true; |
1736 | } |
1737 | |
1738 | |
1739 | |
1740 | |
1741 | DeclContext *SemanticContext; |
1742 | LookupResult Previous(*this, Name, NameLoc, |
1743 | (SS.isEmpty() && TUK == TUK_Friend) |
1744 | ? LookupTagName : LookupOrdinaryName, |
1745 | forRedeclarationInCurContext()); |
1746 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
1747 | SemanticContext = computeDeclContext(SS, true); |
1748 | if (!SemanticContext) { |
1749 | |
1750 | |
1751 | |
1752 | Diag(NameLoc, TUK == TUK_Friend |
1753 | ? diag::warn_template_qualified_friend_ignored |
1754 | : diag::err_template_qualified_declarator_no_match) |
1755 | << SS.getScopeRep() << SS.getRange(); |
1756 | return TUK != TUK_Friend; |
1757 | } |
1758 | |
1759 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
1760 | return true; |
1761 | |
1762 | |
1763 | |
1764 | |
1765 | if (SemanticContext->isDependentContext()) { |
1766 | ContextRAII SavedContext(*this, SemanticContext); |
1767 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
1768 | Invalid = true; |
1769 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
1770 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
1771 | |
1772 | LookupQualifiedName(Previous, SemanticContext); |
1773 | } else { |
1774 | SemanticContext = CurContext; |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | if (TUK != TUK_Friend && |
1781 | DiagnoseClassNameShadow(SemanticContext, |
1782 | DeclarationNameInfo(Name, NameLoc))) |
1783 | return true; |
1784 | |
1785 | LookupName(Previous, S); |
1786 | } |
1787 | |
1788 | if (Previous.isAmbiguous()) |
1789 | return true; |
1790 | |
1791 | NamedDecl *PrevDecl = nullptr; |
1792 | if (Previous.begin() != Previous.end()) |
1793 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
1794 | |
1795 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
1796 | |
1797 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
1798 | |
1799 | PrevDecl = nullptr; |
1800 | } |
1801 | |
1802 | |
1803 | |
1804 | ClassTemplateDecl *PrevClassTemplate = |
1805 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
1806 | |
1807 | |
1808 | |
1809 | |
1810 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
1811 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
1812 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
1813 | PrevClassTemplate |
1814 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
1815 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
1816 | PrevClassTemplate |
1817 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
1818 | ->getSpecializedTemplate(); |
1819 | } |
1820 | } |
1821 | |
1822 | if (TUK == TUK_Friend) { |
1823 | |
1824 | |
1825 | |
1826 | |
1827 | |
1828 | if (!SS.isSet()) { |
1829 | DeclContext *OutermostContext = CurContext; |
1830 | while (!OutermostContext->isFileContext()) |
1831 | OutermostContext = OutermostContext->getLookupParent(); |
1832 | |
1833 | if (PrevDecl && |
1834 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
1835 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
1836 | SemanticContext = PrevDecl->getDeclContext(); |
1837 | } else { |
1838 | |
1839 | |
1840 | |
1841 | PrevDecl = PrevClassTemplate = nullptr; |
1842 | SemanticContext = OutermostContext; |
1843 | |
1844 | |
1845 | |
1846 | Previous.clear(LookupOrdinaryName); |
1847 | DeclContext *LookupContext = SemanticContext; |
1848 | while (LookupContext->isTransparentContext()) |
1849 | LookupContext = LookupContext->getLookupParent(); |
1850 | LookupQualifiedName(Previous, LookupContext); |
1851 | |
1852 | if (Previous.isAmbiguous()) |
1853 | return true; |
1854 | |
1855 | if (Previous.begin() != Previous.end()) |
1856 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
1857 | } |
1858 | } |
1859 | } else if (PrevDecl && |
1860 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
1861 | S, SS.isValid())) |
1862 | PrevDecl = PrevClassTemplate = nullptr; |
1863 | |
1864 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
1865 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
1866 | if (SS.isEmpty() && |
1867 | !(PrevClassTemplate && |
1868 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
1869 | SemanticContext->getRedeclContext()))) { |
1870 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
1871 | Diag(Shadow->getTargetDecl()->getLocation(), |
1872 | diag::note_using_decl_target); |
1873 | Diag(Shadow->getIntroducer()->getLocation(), diag::note_using_decl) << 0; |
1874 | |
1875 | PrevDecl = PrevClassTemplate = nullptr; |
1876 | } |
1877 | } |
1878 | |
1879 | if (PrevClassTemplate) { |
1880 | |
1881 | |
1882 | |
1883 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
1884 | !TemplateParameterListsAreEqual(TemplateParams, |
1885 | PrevClassTemplate->getTemplateParameters(), |
1886 | true, |
1887 | TPL_TemplateMatch)) |
1888 | return true; |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | |
1894 | |
1895 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
1896 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
1897 | TUK == TUK_Definition, KWLoc, Name)) { |
1898 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
1899 | << Name |
1900 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
1901 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
1902 | Kind = PrevRecordDecl->getTagKind(); |
1903 | } |
1904 | |
1905 | |
1906 | if (TUK == TUK_Definition) { |
1907 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
1908 | |
1909 | |
1910 | NamedDecl *Hidden = nullptr; |
1911 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
1912 | SkipBody->ShouldSkip = true; |
1913 | SkipBody->Previous = Def; |
1914 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
1915 | assert(Tmpl && "original definition of a class template is not a " |
1916 | "class template?"); |
1917 | makeMergedDefinitionVisible(Hidden); |
1918 | makeMergedDefinitionVisible(Tmpl); |
1919 | } else { |
1920 | Diag(NameLoc, diag::err_redefinition) << Name; |
1921 | Diag(Def->getLocation(), diag::note_previous_definition); |
1922 | |
1923 | |
1924 | return true; |
1925 | } |
1926 | } |
1927 | } |
1928 | } else if (PrevDecl) { |
1929 | |
1930 | |
1931 | |
1932 | |
1933 | |
1934 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
1935 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
1936 | return true; |
1937 | } |
1938 | |
1939 | |
1940 | |
1941 | |
1942 | |
1943 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
1944 | CheckTemplateParameterList( |
1945 | TemplateParams, |
1946 | PrevClassTemplate |
1947 | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters() |
1948 | : nullptr, |
1949 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
1950 | SemanticContext->isDependentContext()) |
1951 | ? TPC_ClassTemplateMember |
1952 | : TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate, |
1953 | SkipBody)) |
1954 | Invalid = true; |
1955 | |
1956 | if (SS.isSet()) { |
1957 | |
1958 | |
1959 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
1960 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
1961 | : diag::err_member_decl_does_not_match) |
1962 | << Name << SemanticContext << true << SS.getRange(); |
1963 | Invalid = true; |
1964 | } |
1965 | } |
1966 | |
1967 | |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | bool ShouldAddRedecl |
1973 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
1974 | |
1975 | CXXRecordDecl *NewClass = |
1976 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
1977 | PrevClassTemplate && ShouldAddRedecl ? |
1978 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
1979 | true); |
1980 | SetNestedNameSpecifier(*this, NewClass, SS); |
1981 | if (NumOuterTemplateParamLists > 0) |
1982 | NewClass->setTemplateParameterListsInfo( |
1983 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
1984 | NumOuterTemplateParamLists)); |
1985 | |
1986 | |
1987 | |
1988 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
1989 | AddAlignmentAttributesForRecord(NewClass); |
1990 | AddMsStructLayoutForRecord(NewClass); |
1991 | } |
1992 | |
1993 | ClassTemplateDecl *NewTemplate |
1994 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
1995 | DeclarationName(Name), TemplateParams, |
1996 | NewClass); |
1997 | |
1998 | if (ShouldAddRedecl) |
1999 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
2000 | |
2001 | NewClass->setDescribedClassTemplate(NewTemplate); |
2002 | |
2003 | if (ModulePrivateLoc.isValid()) |
2004 | NewTemplate->setModulePrivate(); |
2005 | |
2006 | |
2007 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
2008 | T = Context.getInjectedClassNameType(NewClass, T); |
2009 | assert(T->isDependentType() && "Class template type is not dependent?"); |
2010 | (void)T; |
2011 | |
2012 | |
2013 | |
2014 | if (PrevClassTemplate && |
2015 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
2016 | PrevClassTemplate->setMemberSpecialization(); |
2017 | |
2018 | |
2019 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
2020 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
2021 | |
2022 | |
2023 | NewClass->setLexicalDeclContext(CurContext); |
2024 | NewTemplate->setLexicalDeclContext(CurContext); |
2025 | |
2026 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
2027 | NewClass->startDefinition(); |
2028 | |
2029 | ProcessDeclAttributeList(S, NewClass, Attr); |
2030 | |
2031 | if (PrevClassTemplate) |
2032 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
2033 | |
2034 | AddPushedVisibilityAttribute(NewClass); |
2035 | inferGslOwnerPointerAttribute(NewClass); |
2036 | |
2037 | if (TUK != TUK_Friend) { |
2038 | |
2039 | Scope *Outer = S; |
2040 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
2041 | Outer = Outer->getParent(); |
2042 | PushOnScopeChains(NewTemplate, Outer); |
2043 | } else { |
2044 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
2045 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
2046 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
2047 | } |
2048 | |
2049 | NewTemplate->setObjectOfFriendDecl(); |
2050 | |
2051 | |
2052 | if (!CurContext->isDependentContext()) { |
2053 | DeclContext *DC = SemanticContext->getRedeclContext(); |
2054 | DC->makeDeclVisibleInContext(NewTemplate); |
2055 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
2056 | PushOnScopeChains(NewTemplate, EnclosingScope, |
2057 | false); |
2058 | } |
2059 | |
2060 | FriendDecl *Friend = FriendDecl::Create( |
2061 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
2062 | Friend->setAccess(AS_public); |
2063 | CurContext->addDecl(Friend); |
2064 | } |
2065 | |
2066 | if (PrevClassTemplate) |
2067 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
2068 | |
2069 | if (Invalid) { |
2070 | NewTemplate->setInvalidDecl(); |
2071 | NewClass->setInvalidDecl(); |
2072 | } |
2073 | |
2074 | ActOnDocumentableDecl(NewTemplate); |
2075 | |
2076 | if (SkipBody && SkipBody->ShouldSkip) |
2077 | return SkipBody->Previous; |
2078 | |
2079 | return NewTemplate; |
2080 | } |
2081 | |
2082 | namespace { |
2083 | |
2084 | |
2085 | class ExtractTypeForDeductionGuide |
2086 | : public TreeTransform<ExtractTypeForDeductionGuide> { |
2087 | llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs; |
2088 | |
2089 | public: |
2090 | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; |
2091 | ExtractTypeForDeductionGuide( |
2092 | Sema &SemaRef, |
2093 | llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) |
2094 | : Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs) {} |
2095 | |
2096 | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } |
2097 | |
2098 | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
2099 | ASTContext &Context = SemaRef.getASTContext(); |
2100 | TypedefNameDecl *OrigDecl = TL.getTypedefNameDecl(); |
2101 | TypedefNameDecl *Decl = OrigDecl; |
2102 | |
2103 | |
2104 | if (OrigDecl->getDeclContext()->isDependentContext()) { |
2105 | TypeLocBuilder InnerTLB; |
2106 | QualType Transformed = |
2107 | TransformType(InnerTLB, OrigDecl->getTypeSourceInfo()->getTypeLoc()); |
2108 | TypeSourceInfo *TSI = InnerTLB.getTypeSourceInfo(Context, Transformed); |
2109 | if (isa<TypeAliasDecl>(OrigDecl)) |
2110 | Decl = TypeAliasDecl::Create( |
2111 | Context, Context.getTranslationUnitDecl(), OrigDecl->getBeginLoc(), |
2112 | OrigDecl->getLocation(), OrigDecl->getIdentifier(), TSI); |
2113 | else { |
2114 | assert(isa<TypedefDecl>(OrigDecl) && "Not a Type alias or typedef"); |
2115 | Decl = TypedefDecl::Create( |
2116 | Context, Context.getTranslationUnitDecl(), OrigDecl->getBeginLoc(), |
2117 | OrigDecl->getLocation(), OrigDecl->getIdentifier(), TSI); |
2118 | } |
2119 | MaterializedTypedefs.push_back(Decl); |
2120 | } |
2121 | |
2122 | QualType TDTy = Context.getTypedefType(Decl); |
2123 | TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(TDTy); |
2124 | TypedefTL.setNameLoc(TL.getNameLoc()); |
2125 | |
2126 | return TDTy; |
2127 | } |
2128 | }; |
2129 | |
2130 | |
2131 | |
2132 | struct ConvertConstructorToDeductionGuideTransform { |
2133 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
2134 | ClassTemplateDecl *Template) |
2135 | : SemaRef(S), Template(Template) {} |
2136 | |
2137 | Sema &SemaRef; |
2138 | ClassTemplateDecl *Template; |
2139 | |
2140 | DeclContext *DC = Template->getDeclContext(); |
2141 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
2142 | DeclarationName DeductionGuideName = |
2143 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
2144 | |
2145 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
2146 | |
2147 | |
2148 | |
2149 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
2150 | |
2151 | |
2152 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
2153 | CXXConstructorDecl *CD) { |
2154 | SmallVector<TemplateArgument, 16> SubstArgs; |
2155 | |
2156 | LocalInstantiationScope Scope(SemaRef); |
2157 | |
2158 | |
2159 | |
2160 | |
2161 | |
2162 | |
2163 | |
2164 | |
2165 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
2166 | if (FTD) { |
2167 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
2168 | SmallVector<NamedDecl *, 16> AllParams; |
2169 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
2170 | AllParams.insert(AllParams.begin(), |
2171 | TemplateParams->begin(), TemplateParams->end()); |
2172 | SubstArgs.reserve(InnerParams->size()); |
2173 | |
2174 | |
2175 | |
2176 | for (NamedDecl *Param : *InnerParams) { |
2177 | MultiLevelTemplateArgumentList Args; |
2178 | Args.setKind(TemplateSubstitutionKind::Rewrite); |
2179 | Args.addOuterTemplateArguments(SubstArgs); |
2180 | Args.addOuterRetainedLevel(); |
2181 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
2182 | if (!NewParam) |
2183 | return nullptr; |
2184 | AllParams.push_back(NewParam); |
2185 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
2186 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
2187 | } |
2188 | TemplateParams = TemplateParameterList::Create( |
2189 | SemaRef.Context, InnerParams->getTemplateLoc(), |
2190 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
2191 | nullptr); |
2192 | } |
2193 | |
2194 | |
2195 | |
2196 | |
2197 | MultiLevelTemplateArgumentList Args; |
2198 | Args.setKind(TemplateSubstitutionKind::Rewrite); |
2199 | if (FTD) { |
2200 | Args.addOuterTemplateArguments(SubstArgs); |
2201 | Args.addOuterRetainedLevel(); |
2202 | } |
2203 | |
2204 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
2205 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
2206 | assert(FPTL && "no prototype for constructor declaration"); |
2207 | |
2208 | |
2209 | |
2210 | |
2211 | TypeLocBuilder TLB; |
2212 | SmallVector<ParmVarDecl*, 8> Params; |
2213 | SmallVector<TypedefNameDecl *, 4> MaterializedTypedefs; |
2214 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args, |
2215 | MaterializedTypedefs); |
2216 | if (NewType.isNull()) |
2217 | return nullptr; |
2218 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
2219 | |
2220 | return buildDeductionGuide(TemplateParams, CD, CD->getExplicitSpecifier(), |
2221 | NewTInfo, CD->getBeginLoc(), CD->getLocation(), |
2222 | CD->getEndLoc(), MaterializedTypedefs); |
2223 | } |
2224 | |
2225 | |
2226 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
2227 | SourceLocation Loc = Template->getLocation(); |
2228 | |
2229 | |
2230 | FunctionProtoType::ExtProtoInfo EPI; |
2231 | EPI.HasTrailingReturn = true; |
2232 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
2233 | DeductionGuideName, EPI); |
2234 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
2235 | |
2236 | FunctionProtoTypeLoc FPTL = |
2237 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
2238 | |
2239 | |
2240 | SmallVector<ParmVarDecl*, 4> Params; |
2241 | for (auto T : ParamTypes) { |
2242 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
2243 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
2244 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
2245 | NewParam->setScopeInfo(0, Params.size()); |
2246 | FPTL.setParam(Params.size(), NewParam); |
2247 | Params.push_back(NewParam); |
2248 | } |
2249 | |
2250 | return buildDeductionGuide(Template->getTemplateParameters(), nullptr, |
2251 | ExplicitSpecifier(), TSI, Loc, Loc, Loc); |
2252 | } |
2253 | |
2254 | private: |
2255 | |
2256 | |
2257 | |
2258 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
2259 | MultiLevelTemplateArgumentList &Args) { |
2260 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
2261 | |
2262 | |
2263 | auto *NewTTP = TemplateTypeParmDecl::Create( |
2264 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
2265 | 0, Depth1IndexAdjustment + TTP->getIndex(), |
2266 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
2267 | TTP->isParameterPack(), TTP->hasTypeConstraint(), |
2268 | TTP->isExpandedParameterPack() ? |
2269 | llvm::Optional<unsigned>(TTP->getNumExpansionParameters()) : None); |
2270 | if (const auto *TC = TTP->getTypeConstraint()) { |
2271 | TemplateArgumentListInfo TransformedArgs; |
2272 | const auto *ArgsAsWritten = TC->getTemplateArgsAsWritten(); |
2273 | if (!ArgsAsWritten || |
2274 | SemaRef.Subst(ArgsAsWritten->getTemplateArgs(), |
2275 | ArgsAsWritten->NumTemplateArgs, TransformedArgs, |
2276 | Args)) |
2277 | SemaRef.AttachTypeConstraint( |
2278 | TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), |
2279 | TC->getNamedConcept(), ArgsAsWritten ? &TransformedArgs : nullptr, |
2280 | NewTTP, |
2281 | NewTTP->isParameterPack() |
2282 | ? cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint()) |
2283 | ->getEllipsisLoc() |
2284 | : SourceLocation()); |
2285 | } |
2286 | if (TTP->hasDefaultArgument()) { |
2287 | TypeSourceInfo *InstantiatedDefaultArg = |
2288 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
2289 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
2290 | if (InstantiatedDefaultArg) |
2291 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
2292 | } |
2293 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
2294 | NewTTP); |
2295 | return NewTTP; |
2296 | } |
2297 | |
2298 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
2299 | return transformTemplateParameterImpl(TTP, Args); |
2300 | |
2301 | return transformTemplateParameterImpl( |
2302 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
2303 | } |
2304 | template<typename TemplateParmDecl> |
2305 | TemplateParmDecl * |
2306 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
2307 | MultiLevelTemplateArgumentList &Args) { |
2308 | |
2309 | |
2310 | auto *NewParam = |
2311 | cast<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
2312 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
2313 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
2314 | return NewParam; |
2315 | } |
2316 | |
2317 | QualType transformFunctionProtoType( |
2318 | TypeLocBuilder &TLB, FunctionProtoTypeLoc TL, |
2319 | SmallVectorImpl<ParmVarDecl *> &Params, |
2320 | MultiLevelTemplateArgumentList &Args, |
2321 | SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) { |
2322 | SmallVector<QualType, 4> ParamTypes; |
2323 | const FunctionProtoType *T = TL.getTypePtr(); |
2324 | |
2325 | |
2326 | for (auto *OldParam : TL.getParams()) { |
2327 | ParmVarDecl *NewParam = |
2328 | transformFunctionTypeParam(OldParam, Args, MaterializedTypedefs); |
2329 | if (!NewParam) |
2330 | return QualType(); |
2331 | ParamTypes.push_back(NewParam->getType()); |
2332 | Params.push_back(NewParam); |
2333 | } |
2334 | |
2335 | |
2336 | |
2337 | |
2338 | |
2339 | |
2340 | |
2341 | |
2342 | |
2343 | |
2344 | QualType ReturnType = DeducedType; |
2345 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
2346 | |
2347 | |
2348 | |
2349 | FunctionProtoType::ExtProtoInfo EPI; |
2350 | EPI.Variadic = T->isVariadic(); |
2351 | EPI.HasTrailingReturn = true; |
2352 | |
2353 | QualType Result = SemaRef.BuildFunctionType( |
2354 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
2355 | if (Result.isNull()) |
2356 | return QualType(); |
2357 | |
2358 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
2359 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
2360 | NewTL.setLParenLoc(TL.getLParenLoc()); |
2361 | NewTL.setRParenLoc(TL.getRParenLoc()); |
2362 | NewTL.setExceptionSpecRange(SourceRange()); |
2363 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
2364 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
2365 | NewTL.setParam(I, Params[I]); |
2366 | |
2367 | return Result; |
2368 | } |
2369 | |
2370 | ParmVarDecl *transformFunctionTypeParam( |
2371 | ParmVarDecl *OldParam, MultiLevelTemplateArgumentList &Args, |
2372 | llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs) { |
2373 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
2374 | TypeSourceInfo *NewDI; |
2375 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
2376 | |
2377 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
2378 | NewDI = |
2379 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
2380 | OldParam->getLocation(), OldParam->getDeclName()); |
2381 | if (!NewDI) return nullptr; |
2382 | NewDI = |
2383 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
2384 | PackTL.getTypePtr()->getNumExpansions()); |
2385 | } else |
2386 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
2387 | OldParam->getDeclName()); |
2388 | if (!NewDI) |
2389 | return nullptr; |
2390 | |
2391 | |
2392 | |
2393 | |
2394 | |
2395 | NewDI = ExtractTypeForDeductionGuide(SemaRef, MaterializedTypedefs) |
2396 | .transform(NewDI); |
2397 | |
2398 | |
2399 | |
2400 | ExprResult NewDefArg; |
2401 | if (OldParam->hasDefaultArg()) { |
2402 | |
2403 | |
2404 | QualType ParamTy = NewDI->getType(); |
2405 | NewDefArg = new (SemaRef.Context) |
2406 | OpaqueValueExpr(OldParam->getDefaultArg()->getBeginLoc(), |
2407 | ParamTy.getNonLValueExprType(SemaRef.Context), |
2408 | ParamTy->isLValueReferenceType() ? VK_LValue |
2409 | : ParamTy->isRValueReferenceType() ? VK_XValue |
2410 | : VK_PRValue); |
2411 | } |
2412 | |
2413 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
2414 | OldParam->getInnerLocStart(), |
2415 | OldParam->getLocation(), |
2416 | OldParam->getIdentifier(), |
2417 | NewDI->getType(), |
2418 | NewDI, |
2419 | OldParam->getStorageClass(), |
2420 | NewDefArg.get()); |
2421 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
2422 | OldParam->getFunctionScopeIndex()); |
2423 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
2424 | return NewParam; |
2425 | } |
2426 | |
2427 | FunctionTemplateDecl *buildDeductionGuide( |
2428 | TemplateParameterList *TemplateParams, CXXConstructorDecl *Ctor, |
2429 | ExplicitSpecifier ES, TypeSourceInfo *TInfo, SourceLocation LocStart, |
2430 | SourceLocation Loc, SourceLocation LocEnd, |
2431 | llvm::ArrayRef<TypedefNameDecl *> MaterializedTypedefs = {}) { |
2432 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
2433 | ArrayRef<ParmVarDecl *> Params = |
2434 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
2435 | |
2436 | |
2437 | auto *Guide = |
2438 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, ES, Name, |
2439 | TInfo->getType(), TInfo, LocEnd, Ctor); |
2440 | Guide->setImplicit(); |
2441 | Guide->setParams(Params); |
2442 | |
2443 | for (auto *Param : Params) |
2444 | Param->setDeclContext(Guide); |
2445 | for (auto *TD : MaterializedTypedefs) |
2446 | TD->setDeclContext(Guide); |
2447 | |
2448 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
2449 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
2450 | GuideTemplate->setImplicit(); |
2451 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
2452 | |
2453 | if (isa<CXXRecordDecl>(DC)) { |
2454 | Guide->setAccess(AS_public); |
2455 | GuideTemplate->setAccess(AS_public); |
2456 | } |
2457 | |
2458 | DC->addDecl(GuideTemplate); |
2459 | return GuideTemplate; |
2460 | } |
2461 | }; |
2462 | } |
2463 | |
2464 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
2465 | SourceLocation Loc) { |
2466 | if (CXXRecordDecl *DefRecord = |
2467 | cast<CXXRecordDecl>(Template->getTemplatedDecl())->getDefinition()) { |
2468 | TemplateDecl *DescribedTemplate = DefRecord->getDescribedClassTemplate(); |
2469 | Template = DescribedTemplate ? DescribedTemplate : Template; |
2470 | } |
2471 | |
2472 | DeclContext *DC = Template->getDeclContext(); |
2473 | if (DC->isDependentContext()) |
2474 | return; |
2475 | |
2476 | ConvertConstructorToDeductionGuideTransform Transform( |
2477 | *this, cast<ClassTemplateDecl>(Template)); |
2478 | if (!isCompleteType(Loc, Transform.DeducedType)) |
2479 | return; |
2480 | |
2481 | |
2482 | |
2483 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
2484 | for (auto *D : Existing) |
2485 | if (D->isImplicit()) |
2486 | return; |
2487 | |
2488 | |
2489 | |
2490 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
2491 | |
2492 | |
2493 | |
2494 | |
2495 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
2496 | if (BuildingDeductionGuides.isInvalid()) |
2497 | return; |
2498 | |
2499 | |
2500 | |
2501 | |
2502 | |
2503 | bool AddedAny = false; |
2504 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
2505 | D = D->getUnderlyingDecl(); |
2506 | if (D->isInvalidDecl() || D->isImplicit()) |
2507 | continue; |
2508 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
2509 | |
2510 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
2511 | auto *CD = |
2512 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
2513 | |
2514 | |
2515 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
2516 | continue; |
2517 | |
2518 | |
2519 | if (std::any_of(CD->param_begin(), CD->param_end(), [](ParmVarDecl *P) { |
2520 | return !P || P->hasUnparsedDefaultArg(); |
2521 | })) |
2522 | continue; |
2523 | |
2524 | Transform.transformConstructor(FTD, CD); |
2525 | AddedAny = true; |
2526 | } |
2527 | |
2528 | |
2529 | |
2530 | |
2531 | |
2532 | if (!AddedAny) |
2533 | Transform.buildSimpleDeductionGuide(None); |
2534 | |
2535 | |
2536 | |
2537 | cast<CXXDeductionGuideDecl>( |
2538 | cast<FunctionTemplateDecl>( |
2539 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
2540 | ->getTemplatedDecl()) |
2541 | ->setIsCopyDeductionCandidate(); |
2542 | } |
2543 | |
2544 | |
2545 | |
2546 | |
2547 | |
2548 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
2549 | Sema::TemplateParamListContext TPC, |
2550 | SourceLocation ParamLoc, |
2551 | SourceRange DefArgRange) { |
2552 | switch (TPC) { |
2553 | case Sema::TPC_ClassTemplate: |
2554 | case Sema::TPC_VarTemplate: |
2555 | case Sema::TPC_TypeAliasTemplate: |
2556 | return false; |
2557 | |
2558 | case Sema::TPC_FunctionTemplate: |
2559 | case Sema::TPC_FriendFunctionTemplateDefinition: |
2560 | |
2561 | |
2562 | |
2563 | |
2564 | |
2565 | |
2566 | |
2567 | |
2568 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
2569 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
2570 | : diag::ext_template_parameter_default_in_function_template) |
2571 | << DefArgRange; |
2572 | return false; |
2573 | |
2574 | case Sema::TPC_ClassTemplateMember: |
2575 | |
2576 | |
2577 | |
2578 | |
2579 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
2580 | << DefArgRange; |
2581 | return true; |
2582 | |
2583 | case Sema::TPC_FriendClassTemplate: |
2584 | case Sema::TPC_FriendFunctionTemplate: |
2585 | |
2586 | |
2587 | |
2588 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
2589 | << DefArgRange; |
2590 | return true; |
2591 | |
2592 | |
2593 | |
2594 | |
2595 | } |
2596 | |
2597 | llvm_unreachable("Invalid TemplateParamListContext!"); |
2598 | } |
2599 | |
2600 | |
2601 | |
2602 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
2603 | TemplateTemplateParmDecl *TTP) { |
2604 | |
2605 | |
2606 | if (TTP->isParameterPack()) |
2607 | return false; |
2608 | |
2609 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
2610 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
2611 | NamedDecl *P = Params->getParam(I); |
2612 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(P)) { |
2613 | if (!TTP->isParameterPack()) |
2614 | if (const TypeConstraint *TC = TTP->getTypeConstraint()) |
2615 | if (TC->hasExplicitTemplateArgs()) |
2616 | for (auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments()) |
2617 | if (S.DiagnoseUnexpandedParameterPack(ArgLoc, |
2618 | Sema::UPPC_TypeConstraint)) |
2619 | return true; |
2620 | continue; |
2621 | } |
2622 | |
2623 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
2624 | if (!NTTP->isParameterPack() && |
2625 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
2626 | NTTP->getTypeSourceInfo(), |
2627 | Sema::UPPC_NonTypeTemplateParameterType)) |
2628 | return true; |
2629 | |
2630 | continue; |
2631 | } |
2632 | |
2633 | if (TemplateTemplateParmDecl *InnerTTP |
2634 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
2635 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
2636 | return true; |
2637 | } |
2638 | |
2639 | return false; |
2640 | } |
2641 | |
2642 | |
2643 | |
2644 | |
2645 | |
2646 | |
2647 | |
2648 | |
2649 | |
2650 | |
2651 | |
2652 | |
2653 | |
2654 | |
2655 | |
2656 | |
2657 | |
2658 | |
2659 | |
2660 | |
2661 | |
2662 | |
2663 | |
2664 | |
2665 | |
2666 | |
2667 | |
2668 | |
2669 | |
2670 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
2671 | TemplateParameterList *OldParams, |
2672 | TemplateParamListContext TPC, |
2673 | SkipBodyInfo *SkipBody) { |
2674 | bool Invalid = false; |
2675 | |
2676 | |
2677 | |
2678 | |
2679 | |
2680 | |
2681 | |
2682 | bool SawDefaultArgument = false; |
2683 | SourceLocation PreviousDefaultArgLoc; |
2684 | |
2685 | |
2686 | TemplateParameterList::iterator OldParam = NewParams->end(); |
2687 | if (OldParams) |
2688 | OldParam = OldParams->begin(); |
2689 | |
2690 | bool RemoveDefaultArguments = false; |
2691 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
2692 | NewParamEnd = NewParams->end(); |
2693 | NewParam != NewParamEnd; ++NewParam) { |
2694 | |
2695 | bool RedundantDefaultArg = false; |
2696 | SourceLocation OldDefaultLoc; |
2697 | SourceLocation NewDefaultLoc; |
2698 | |
2699 | |
2700 | bool MissingDefaultArg = false; |
2701 | |
2702 | |
2703 | bool SawParameterPack = false; |
2704 | |
2705 | if (TemplateTypeParmDecl *NewTypeParm |
2706 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
2707 | |
2708 | if (NewTypeParm->hasDefaultArgument() && |
2709 | DiagnoseDefaultTemplateArgument(*this, TPC, |
2710 | NewTypeParm->getLocation(), |
2711 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
2712 | .getSourceRange())) |
2713 | NewTypeParm->removeDefaultArgument(); |
2714 | |
2715 | |
2716 | TemplateTypeParmDecl *OldTypeParm |
2717 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
2718 | if (NewTypeParm->isParameterPack()) { |
2719 | assert(!NewTypeParm->hasDefaultArgument() && |
2720 | "Parameter packs can't have a default argument!"); |
2721 | SawParameterPack = true; |
2722 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
2723 | NewTypeParm->hasDefaultArgument() && |
2724 | (!SkipBody || !SkipBody->ShouldSkip)) { |
2725 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
2726 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
2727 | SawDefaultArgument = true; |
2728 | RedundantDefaultArg = true; |
2729 | PreviousDefaultArgLoc = NewDefaultLoc; |
2730 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
2731 | |
2732 | |
2733 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
2734 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
2735 | } else if (NewTypeParm->hasDefaultArgument()) { |
2736 | SawDefaultArgument = true; |
2737 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
2738 | } else if (SawDefaultArgument) |
2739 | MissingDefaultArg = true; |
2740 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
2741 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
2742 | |
2743 | if (!NewNonTypeParm->isParameterPack() && |
2744 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
2745 | NewNonTypeParm->getTypeSourceInfo(), |
2746 | UPPC_NonTypeTemplateParameterType)) { |
2747 | Invalid = true; |
2748 | continue; |
2749 | } |
2750 | |
2751 | |
2752 | if (NewNonTypeParm->hasDefaultArgument() && |
2753 | DiagnoseDefaultTemplateArgument(*this, TPC, |
2754 | NewNonTypeParm->getLocation(), |
2755 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
2756 | NewNonTypeParm->removeDefaultArgument(); |
2757 | } |
2758 | |
2759 | |
2760 | NonTypeTemplateParmDecl *OldNonTypeParm |
2761 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
2762 | if (NewNonTypeParm->isParameterPack()) { |
2763 | assert(!NewNonTypeParm->hasDefaultArgument() && |
2764 | "Parameter packs can't have a default argument!"); |
2765 | if (!NewNonTypeParm->isPackExpansion()) |
2766 | SawParameterPack = true; |
2767 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
2768 | NewNonTypeParm->hasDefaultArgument() && |
2769 | (!SkipBody || !SkipBody->ShouldSkip)) { |
2770 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
2771 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
2772 | SawDefaultArgument = true; |
2773 | RedundantDefaultArg = true; |
2774 | PreviousDefaultArgLoc = NewDefaultLoc; |
2775 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
2776 | |
2777 | |
2778 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
2779 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
2780 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
2781 | SawDefaultArgument = true; |
2782 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
2783 | } else if (SawDefaultArgument) |
2784 | MissingDefaultArg = true; |
2785 | } else { |
2786 | TemplateTemplateParmDecl *NewTemplateParm |
2787 | = cast<TemplateTemplateParmDecl>(*NewParam); |
2788 | |
2789 | |
2790 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
2791 | Invalid = true; |
2792 | continue; |
2793 | } |
2794 | |
2795 | |
2796 | if (NewTemplateParm->hasDefaultArgument() && |
2797 | DiagnoseDefaultTemplateArgument(*this, TPC, |
2798 | NewTemplateParm->getLocation(), |
2799 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
2800 | NewTemplateParm->removeDefaultArgument(); |
2801 | |
2802 | |
2803 | TemplateTemplateParmDecl *OldTemplateParm |
2804 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
2805 | if (NewTemplateParm->isParameterPack()) { |
2806 | assert(!NewTemplateParm->hasDefaultArgument() && |
2807 | "Parameter packs can't have a default argument!"); |
2808 | if (!NewTemplateParm->isPackExpansion()) |
2809 | SawParameterPack = true; |
2810 | } else if (OldTemplateParm && |
2811 | hasVisibleDefaultArgument(OldTemplateParm) && |
2812 | NewTemplateParm->hasDefaultArgument() && |
2813 | (!SkipBody || !SkipBody->ShouldSkip)) { |
2814 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
2815 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
2816 | SawDefaultArgument = true; |
2817 | RedundantDefaultArg = true; |
2818 | PreviousDefaultArgLoc = NewDefaultLoc; |
2819 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
2820 | |
2821 | |
2822 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
2823 | PreviousDefaultArgLoc |
2824 | = OldTemplateParm->getDefaultArgument().getLocation(); |
2825 | } else if (NewTemplateParm->hasDefaultArgument()) { |
2826 | SawDefaultArgument = true; |
2827 | PreviousDefaultArgLoc |
2828 | = NewTemplateParm->getDefaultArgument().getLocation(); |
2829 | } else if (SawDefaultArgument) |
2830 | MissingDefaultArg = true; |
2831 | } |
2832 | |
2833 | |
2834 | |
2835 | |
2836 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
2837 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
2838 | TPC == TPC_TypeAliasTemplate)) { |
2839 | Diag((*NewParam)->getLocation(), |
2840 | diag::err_template_param_pack_must_be_last_template_parameter); |
2841 | Invalid = true; |
2842 | } |
2843 | |
2844 | if (RedundantDefaultArg) { |
2845 | |
2846 | |
2847 | |
2848 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
2849 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
2850 | Invalid = true; |
2851 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
2852 | |
2853 | |
2854 | |
2855 | |
2856 | |
2857 | Diag((*NewParam)->getLocation(), |
2858 | diag::err_template_param_default_arg_missing); |
2859 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
2860 | Invalid = true; |
2861 | RemoveDefaultArguments = true; |
2862 | } |
2863 | |
2864 | |
2865 | |
2866 | if (OldParams) |
2867 | ++OldParam; |
2868 | } |
2869 | |
2870 | |
2871 | |
2872 | if (RemoveDefaultArguments) { |
2873 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
2874 | NewParamEnd = NewParams->end(); |
2875 | NewParam != NewParamEnd; ++NewParam) { |
2876 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
2877 | TTP->removeDefaultArgument(); |
2878 | else if (NonTypeTemplateParmDecl *NTTP |
2879 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
2880 | NTTP->removeDefaultArgument(); |
2881 | else |
2882 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
2883 | } |
2884 | } |
2885 | |
2886 | return Invalid; |
2887 | } |
2888 | |
2889 | namespace { |
2890 | |
2891 | |
2892 | |
2893 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
2894 | typedef RecursiveASTVisitor<DependencyChecker> super; |
2895 | |
2896 | unsigned Depth; |
2897 | |
2898 | |
2899 | |
2900 | |
2901 | |
2902 | bool IgnoreNonTypeDependent; |
2903 | |
2904 | bool Match; |
2905 | SourceLocation MatchLoc; |
2906 | |
2907 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
2908 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
2909 | Match(false) {} |
2910 | |
2911 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
2912 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
2913 | NamedDecl *ND = Params->getParam(0); |
2914 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
2915 | Depth = PD->getDepth(); |
2916 | } else if (NonTypeTemplateParmDecl *PD = |
2917 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
2918 | Depth = PD->getDepth(); |
2919 | } else { |
2920 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
2921 | } |
2922 | } |
2923 | |
2924 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
2925 | if (ParmDepth >= Depth) { |
2926 | Match = true; |
2927 | MatchLoc = Loc; |
2928 | return true; |
2929 | } |
2930 | return false; |
2931 | } |
2932 | |
2933 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
2934 | |
2935 | |
2936 | |
2937 | |
2938 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
2939 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
2940 | return true; |
2941 | return super::TraverseStmt(S, Q); |
2942 | } |
2943 | |
2944 | bool TraverseTypeLoc(TypeLoc TL) { |
2945 | if (IgnoreNonTypeDependent && !TL.isNull() && |
2946 | !TL.getType()->isDependentType()) |
2947 | return true; |
2948 | return super::TraverseTypeLoc(TL); |
2949 | } |
2950 | |
2951 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
2952 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
2953 | } |
2954 | |
2955 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
2956 | |
2957 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
2958 | } |
2959 | |
2960 | bool TraverseTemplateName(TemplateName N) { |
2961 | if (TemplateTemplateParmDecl *PD = |
2962 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
2963 | if (Matches(PD->getDepth())) |
2964 | return false; |
2965 | return super::TraverseTemplateName(N); |
2966 | } |
2967 | |
2968 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
2969 | if (NonTypeTemplateParmDecl *PD = |
2970 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
2971 | if (Matches(PD->getDepth(), E->getExprLoc())) |
2972 | return false; |
2973 | return super::VisitDeclRefExpr(E); |
2974 | } |
2975 | |
2976 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
2977 | return TraverseType(T->getReplacementType()); |
2978 | } |
2979 | |
2980 | bool |
2981 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
2982 | return TraverseTemplateArgument(T->getArgumentPack()); |
2983 | } |
2984 | |
2985 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
2986 | return TraverseType(T->getInjectedSpecializationType()); |
2987 | } |
2988 | }; |
2989 | } |
2990 | |
2991 | |
2992 | |
2993 | static bool |
2994 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
2995 | if (!Params->size()) |
2996 | return false; |
2997 | |
2998 | DependencyChecker Checker(Params, false); |
2999 | Checker.TraverseType(T); |
3000 | return Checker.Match; |
3001 | } |
3002 | |
3003 | |
3004 | |
3005 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
3006 | QualType T, |
3007 | const CXXScopeSpec &SS) { |
3008 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
3009 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
3010 | if (const Type *CurType = NNS->getAsType()) { |
3011 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
3012 | return NNSLoc.getTypeLoc().getSourceRange(); |
3013 | } else |
3014 | break; |
3015 | |
3016 | NNSLoc = NNSLoc.getPrefix(); |
3017 | } |
3018 | |
3019 | return SourceRange(); |
3020 | } |
3021 | |
3022 | |
3023 | |
3024 | |
3025 | |
3026 | |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | |
3032 | |
3033 | |
3034 | |
3035 | |
3036 | |
3037 | |
3038 | |
3039 | |
3040 | |
3041 | |
3042 | |
3043 | |
3044 | |
3045 | |
3046 | |
3047 | |
3048 | |
3049 | |
3050 | |
3051 | |
3052 | |
3053 | |
3054 | |
3055 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
3056 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
3057 | TemplateIdAnnotation *TemplateId, |
3058 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
3059 | bool &IsMemberSpecialization, bool &Invalid, bool SuppressDiagnostic) { |
3060 | IsMemberSpecialization = false; |
3061 | Invalid = false; |
3062 | |
3063 | |
3064 | |
3065 | |
3066 | SmallVector<QualType, 4> NestedTypes; |
3067 | QualType T; |
3068 | if (SS.getScopeRep()) { |
3069 | if (CXXRecordDecl *Record |
3070 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
3071 | T = Context.getTypeDeclType(Record); |
3072 | else |
3073 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
3074 | } |
3075 | |
3076 | |
3077 | |
3078 | |
3079 | SourceLocation ExplicitSpecLoc; |
3080 | |
3081 | while (!T.isNull()) { |
3082 | NestedTypes.push_back(T); |
3083 | |
3084 | |
3085 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
3086 | |
3087 | if (ClassTemplateSpecializationDecl *Spec |
3088 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
3089 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
3090 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
3091 | ExplicitSpecLoc = Spec->getLocation(); |
3092 | break; |
3093 | } |
3094 | } else if (Record->getTemplateSpecializationKind() |
3095 | == TSK_ExplicitSpecialization) { |
3096 | ExplicitSpecLoc = Record->getLocation(); |
3097 | break; |
3098 | } |
3099 | |
3100 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
3101 | T = Context.getTypeDeclType(Parent); |
3102 | else |
3103 | T = QualType(); |
3104 | continue; |
3105 | } |
3106 | |
3107 | if (const TemplateSpecializationType *TST |
3108 | = T->getAs<TemplateSpecializationType>()) { |
3109 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
3110 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
3111 | T = Context.getTypeDeclType(Parent); |
3112 | else |
3113 | T = QualType(); |
3114 | continue; |
3115 | } |
3116 | } |
3117 | |
3118 | |
3119 | if (const DependentTemplateSpecializationType *DependentTST |
3120 | = T->getAs<DependentTemplateSpecializationType>()) { |
3121 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
3122 | T = QualType(NNS->getAsType(), 0); |
3123 | else |
3124 | T = QualType(); |
3125 | continue; |
3126 | } |
3127 | |
3128 | |
3129 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
3130 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
3131 | T = QualType(NNS->getAsType(), 0); |
3132 | else |
3133 | T = QualType(); |
3134 | continue; |
3135 | } |
3136 | |
3137 | |
3138 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
3139 | |
3140 | |
3141 | EnumDecl *Enum = EnumT->getDecl(); |
3142 | |
3143 | |
3144 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
3145 | T = Context.getTypeDeclType(Parent); |
3146 | else |
3147 | T = QualType(); |
3148 | continue; |
3149 | } |
3150 | |
3151 | T = QualType(); |
3152 | } |
3153 | |
3154 | |
3155 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
3156 | |
3157 | |
3158 | |
3159 | |
3160 | |
3161 | |
3162 | |
3163 | bool SawNonEmptyTemplateParameterList = false; |
3164 | |
3165 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
3166 | if (SawNonEmptyTemplateParameterList) { |
3167 | if (!SuppressDiagnostic) |
3168 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
3169 | << !Recovery << Range; |
3170 | Invalid = true; |
3171 | IsMemberSpecialization = false; |
3172 | return true; |
3173 | } |
3174 | |
3175 | return false; |
3176 | }; |
3177 | |
3178 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
3179 | |
3180 | if (CheckExplicitSpecialization(Range, true)) |
3181 | return true; |
3182 | |
3183 | |
3184 | SourceLocation ExpectedTemplateLoc; |
3185 | if (!ParamLists.empty()) |
3186 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
3187 | else |
3188 | ExpectedTemplateLoc = DeclStartLoc; |
3189 | |
3190 | if (!SuppressDiagnostic) |
3191 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
3192 | << Range |
3193 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
3194 | return false; |
3195 | }; |
3196 | |
3197 | unsigned ParamIdx = 0; |
3198 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
3199 | ++TypeIdx) { |
3200 | T = NestedTypes[TypeIdx]; |
3201 | |
3202 | |
3203 | bool NeedEmptyTemplateHeader = false; |
3204 | |
3205 | |
3206 | bool NeedNonemptyTemplateHeader = false; |
3207 | |
3208 | |
3209 | |
3210 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
3211 | |
3212 | |
3213 | |
3214 | |
3215 | |
3216 | |
3217 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
3218 | if (ClassTemplatePartialSpecializationDecl *Partial |
3219 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
3220 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
3221 | NeedNonemptyTemplateHeader = true; |
3222 | } else if (Record->isDependentType()) { |
3223 | if (Record->getDescribedClassTemplate()) { |
3224 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
3225 | ->getTemplateParameters(); |
3226 | NeedNonemptyTemplateHeader = true; |
3227 | } |
3228 | } else if (ClassTemplateSpecializationDecl *Spec |
3229 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
3230 | |
3231 | |
3232 | |
3233 | |
3234 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
3235 | NeedEmptyTemplateHeader = true; |
3236 | else |
3237 | continue; |
3238 | } else if (Record->getTemplateSpecializationKind()) { |
3239 | if (Record->getTemplateSpecializationKind() |
3240 | != TSK_ExplicitSpecialization && |
3241 | TypeIdx == NumTypes - 1) |
3242 | IsMemberSpecialization = true; |
3243 | |
3244 | continue; |
3245 | } |
3246 | } else if (const TemplateSpecializationType *TST |
3247 | = T->getAs<TemplateSpecializationType>()) { |
3248 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
3249 | ExpectedTemplateParams = Template->getTemplateParameters(); |
3250 | NeedNonemptyTemplateHeader = true; |
3251 | } |
3252 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
3253 | |
3254 | |
3255 | NeedNonemptyTemplateHeader = false; |
3256 | } |
3257 | |
3258 | |
3259 | |
3260 | |
3261 | |
3262 | |
3263 | |
3264 | |
3265 | if (ParamIdx < ParamLists.size()) { |
3266 | if (ParamLists[ParamIdx]->size() == 0) { |
3267 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
3268 | false)) |
3269 | return nullptr; |
3270 | } else |
3271 | SawNonEmptyTemplateParameterList = true; |
3272 | } |
3273 | |
3274 | if (NeedEmptyTemplateHeader) { |
3275 | |
3276 | |
3277 | if (TypeIdx == NumTypes - 1) |
3278 | IsMemberSpecialization = true; |
3279 | |
3280 | if (ParamIdx < ParamLists.size()) { |
3281 | if (ParamLists[ParamIdx]->size() > 0) { |
3282 | |
3283 | if (!SuppressDiagnostic) |
3284 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
3285 | diag::err_template_param_list_matches_nontemplate) |
3286 | << T |
3287 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
3288 | ParamLists[ParamIdx]->getRAngleLoc()) |
3289 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
3290 | Invalid = true; |
3291 | return nullptr; |
3292 | } |
3293 | |
3294 | |
3295 | ++ParamIdx; |
3296 | continue; |
3297 | } |
3298 | |
3299 | if (!IsFriend) |
3300 | if (DiagnoseMissingExplicitSpecialization( |
3301 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
3302 | return nullptr; |
3303 | |
3304 | continue; |
3305 | } |
3306 | |
3307 | if (NeedNonemptyTemplateHeader) { |
3308 | |
3309 | |
3310 | |
3311 | |
3312 | if (IsFriend && T->isDependentType()) { |
3313 | if (ParamIdx < ParamLists.size() && |
3314 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
3315 | ExpectedTemplateParams = nullptr; |
3316 | else |
3317 | continue; |
3318 | } |
3319 | |
3320 | if (ParamIdx < ParamLists.size()) { |
3321 | |
3322 | if (ExpectedTemplateParams && |
3323 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
3324 | ExpectedTemplateParams, |
3325 | !SuppressDiagnostic, TPL_TemplateMatch)) |
3326 | Invalid = true; |
3327 | |
3328 | if (!Invalid && |
3329 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
3330 | TPC_ClassTemplateMember)) |
3331 | Invalid = true; |
3332 | |
3333 | ++ParamIdx; |
3334 | continue; |
3335 | } |
3336 | |
3337 | if (!SuppressDiagnostic) |
3338 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
3339 | << T |
3340 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
3341 | Invalid = true; |
3342 | continue; |
3343 | } |
3344 | } |
3345 | |
3346 | |
3347 | |
3348 | |
3349 | if (ParamIdx >= ParamLists.size()) { |
3350 | if (TemplateId && !IsFriend) { |
3351 | |
3352 | |
3353 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
3354 | TemplateId->RAngleLoc)); |
3355 | |
3356 | |
3357 | return TemplateParameterList::Create(Context, SourceLocation(), |
3358 | SourceLocation(), None, |
3359 | SourceLocation(), nullptr); |
3360 | } |
3361 | |
3362 | return nullptr; |
3363 | } |
3364 | |
3365 | |
3366 | if (ParamIdx < ParamLists.size() - 1) { |
3367 | bool HasAnyExplicitSpecHeader = false; |
3368 | bool AllExplicitSpecHeaders = true; |
3369 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
3370 | if (ParamLists[I]->size() == 0) |
3371 | HasAnyExplicitSpecHeader = true; |
3372 | else |
3373 | AllExplicitSpecHeaders = false; |
3374 | } |
3375 | |
3376 | if (!SuppressDiagnostic) |
3377 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
3378 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
3379 | : diag::err_template_spec_extra_headers) |
3380 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
3381 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
3382 | |
3383 | |
3384 | |
3385 | |
3386 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader && |
3387 | !SuppressDiagnostic) |
3388 | Diag(ExplicitSpecLoc, |
3389 | diag::note_explicit_template_spec_does_not_need_header) |
3390 | << NestedTypes.back(); |
3391 | |
3392 | |
3393 | |
3394 | |
3395 | if (!AllExplicitSpecHeaders) |
3396 | Invalid = true; |
3397 | } |
3398 | |
3399 | |
3400 | |
3401 | |
3402 | |
3403 | |
3404 | |
3405 | |
3406 | if (ParamLists.back()->size() == 0 && |
3407 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
3408 | false)) |
3409 | return nullptr; |
3410 | |
3411 | |
3412 | |
3413 | return ParamLists.back(); |
3414 | } |
3415 | |
3416 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
3417 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
3418 | Diag(Template->getLocation(), diag::note_template_declared_here) |
3419 | << (isa<FunctionTemplateDecl>(Template) |
3420 | ? 0 |
3421 | : isa<ClassTemplateDecl>(Template) |
3422 | ? 1 |
3423 | : isa<VarTemplateDecl>(Template) |
3424 | ? 2 |
3425 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
3426 | << Template->getDeclName(); |
3427 | return; |
3428 | } |
3429 | |
3430 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
3431 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
3432 | IEnd = OST->end(); |
3433 | I != IEnd; ++I) |
3434 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
3435 | << 0 << (*I)->getDeclName(); |
3436 | |
3437 | return; |
3438 | } |
3439 | } |
3440 | |
3441 | static QualType |
3442 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
3443 | const SmallVectorImpl<TemplateArgument> &Converted, |
3444 | SourceLocation TemplateLoc, |
3445 | TemplateArgumentListInfo &TemplateArgs) { |
3446 | ASTContext &Context = SemaRef.getASTContext(); |
3447 | switch (BTD->getBuiltinTemplateKind()) { |
3448 | case BTK__make_integer_seq: { |
3449 | |
3450 | |
3451 | |
3452 | |
3453 | |
3454 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
3455 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
3456 | diag::err_integer_sequence_integral_element_type); |
3457 | return QualType(); |
3458 | } |
3459 | |
3460 | |
3461 | |
3462 | TemplateArgument NumArgsArg = Converted[2]; |
3463 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
3464 | if (NumArgs < 0) { |
3465 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
3466 | diag::err_integer_sequence_negative_length); |
3467 | return QualType(); |
3468 | } |
3469 | |
3470 | QualType ArgTy = NumArgsArg.getIntegralType(); |
3471 | TemplateArgumentListInfo SyntheticTemplateArgs; |
3472 | |
3473 | |
3474 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
3475 | |
3476 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
3477 | I < NumArgs; ++I) { |
3478 | TemplateArgument TA(Context, I, ArgTy); |
3479 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
3480 | TA, ArgTy, TemplateArgs[2].getLocation())); |
3481 | } |
3482 | |
3483 | |
3484 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
3485 | TemplateLoc, SyntheticTemplateArgs); |
3486 | } |
3487 | |
3488 | case BTK__type_pack_element: |
3489 | |
3490 | |
3491 | |
3492 | assert(Converted.size() == 2 && |
3493 | "__type_pack_element should be given an index and a parameter pack"); |
3494 | |
3495 | |
3496 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
3497 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
3498 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
3499 | "type std::size_t, and hence be non-negative"); |
3500 | if (Index >= Ts.pack_size()) { |
3501 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
3502 | diag::err_type_pack_element_out_of_bounds); |
3503 | return QualType(); |
3504 | } |
3505 | |
3506 | |
3507 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
3508 | return Nth->getAsType(); |
3509 | } |
3510 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
3511 | } |
3512 | |
3513 | |
3514 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
3515 | return AliasTemplate->getName().equals("enable_if_t"); |
3516 | } |
3517 | |
3518 | |
3519 | |
3520 | |
3521 | |
3522 | |
3523 | |
3524 | static void collectConjunctionTerms(Expr *Clause, |
3525 | SmallVectorImpl<Expr *> &Terms) { |
3526 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
3527 | if (BinOp->getOpcode() == BO_LAnd) { |
3528 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
3529 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
3530 | } |
3531 | |
3532 | return; |
3533 | } |
3534 | |
3535 | Terms.push_back(Clause); |
3536 | } |
3537 | |
3538 | |
3539 | |
3540 | |
3541 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
3542 | |
3543 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
3544 | if (!BinOp) return Cond; |
3545 | |
3546 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
3547 | |
3548 | |
3549 | Expr *LHS = BinOp->getLHS(); |
3550 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
3551 | if (!InnerBinOp) return Cond; |
3552 | |
3553 | if (InnerBinOp->getOpcode() != BO_EQ || |
3554 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
3555 | return Cond; |
3556 | |
3557 | |
3558 | |
3559 | |
3560 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
3561 | if (!Loc.isMacroID()) return Cond; |
3562 | |
3563 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
3564 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
3565 | return BinOp->getRHS(); |
3566 | |
3567 | return Cond; |
3568 | } |
3569 | |
3570 | namespace { |
3571 | |
3572 | |
3573 | |
3574 | |
3575 | class FailedBooleanConditionPrinterHelper : public PrinterHelper { |
3576 | public: |
3577 | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) |
3578 | : Policy(P) {} |
3579 | |
3580 | bool handledStmt(Stmt *E, raw_ostream &OS) override { |
3581 | const auto *DR = dyn_cast<DeclRefExpr>(E); |
3582 | if (DR && DR->getQualifier()) { |
3583 | |
3584 | |
3585 | DR->getQualifier()->print(OS, Policy, true); |
3586 | |
3587 | const ValueDecl *VD = DR->getDecl(); |
3588 | OS << VD->getName(); |
3589 | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
3590 | |
3591 | printTemplateArgumentList( |
3592 | OS, IV->getTemplateArgs().asArray(), Policy, |
3593 | IV->getSpecializedTemplate()->getTemplateParameters()); |
3594 | } |
3595 | return true; |
3596 | } |
3597 | return false; |
3598 | } |
3599 | |
3600 | private: |
3601 | const PrintingPolicy Policy; |
3602 | }; |
3603 | |
3604 | } |
3605 | |
3606 | std::pair<Expr *, std::string> |
3607 | Sema::findFailedBooleanCondition(Expr *Cond) { |
3608 | Cond = lookThroughRangesV3Condition(PP, Cond); |
3609 | |
3610 | |
3611 | SmallVector<Expr *, 4> Terms; |
3612 | collectConjunctionTerms(Cond, Terms); |
3613 | |
3614 | |
3615 | Expr *FailedCond = nullptr; |
3616 | for (Expr *Term : Terms) { |
3617 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
3618 | |
3619 | |
3620 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
3621 | isa<IntegerLiteral>(TermAsWritten)) |
3622 | continue; |
3623 | |
3624 | |
3625 | |
3626 | EnterExpressionEvaluationContext ConstantEvaluated( |
3627 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
3628 | |
3629 | bool Succeeded; |
3630 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
3631 | !Succeeded) { |
3632 | FailedCond = TermAsWritten; |
3633 | break; |
3634 | } |
3635 | } |
3636 | if (!FailedCond) |
3637 | FailedCond = Cond->IgnoreParenImpCasts(); |
3638 | |
3639 | std::string Description; |
3640 | { |
3641 | llvm::raw_string_ostream Out(Description); |
3642 | PrintingPolicy Policy = getPrintingPolicy(); |
3643 | Policy.PrintCanonicalTypes = true; |
3644 | FailedBooleanConditionPrinterHelper Helper(Policy); |
3645 | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); |
3646 | } |
3647 | return { FailedCond, Description }; |
3648 | } |
3649 | |
3650 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
3651 | SourceLocation TemplateLoc, |
3652 | TemplateArgumentListInfo &TemplateArgs) { |
3653 | DependentTemplateName *DTN |
3654 | = Name.getUnderlying().getAsDependentTemplateName(); |
3655 | if (DTN && DTN->isIdentifier()) |
3656 | |
3657 | |
3658 | |
3659 | |
3660 | return Context.getDependentTemplateSpecializationType(ETK_None, |
3661 | DTN->getQualifier(), |
3662 | DTN->getIdentifier(), |
3663 | TemplateArgs); |
3664 | |
3665 | if (Name.getAsAssumedTemplateName() && |
3666 | resolveAssumedTemplateNameAsType(nullptr, Name, TemplateLoc)) |
3667 | return QualType(); |
3668 | |
3669 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
3670 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
3671 | isa<VarTemplateDecl>(Template) || isa<ConceptDecl>(Template)) { |
3672 | |
3673 | |
3674 | if (Name.getAsSubstTemplateTemplateParmPack()) |
3675 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
3676 | |
3677 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
3678 | << Name; |
3679 | NoteAllFoundTemplates(Name); |
3680 | return QualType(); |
3681 | } |
3682 | |
3683 | |
3684 | |
3685 | SmallVector<TemplateArgument, 4> Converted; |
3686 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
3687 | false, Converted, |
3688 | true)) |
3689 | return QualType(); |
3690 | |
3691 | QualType CanonType; |
3692 | |
3693 | if (TypeAliasTemplateDecl *AliasTemplate = |
3694 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
3695 | |
3696 | |
3697 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
3698 | if (Pattern->isInvalidDecl()) |
3699 | return QualType(); |
3700 | |
3701 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
3702 | Converted); |
3703 | |
3704 | |
3705 | MultiLevelTemplateArgumentList TemplateArgLists; |
3706 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
3707 | TemplateArgLists.addOuterRetainedLevels( |
3708 | AliasTemplate->getTemplateParameters()->getDepth()); |
3709 | |
3710 | LocalInstantiationScope Scope(*this); |
3711 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
3712 | if (Inst.isInvalid()) |
3713 | return QualType(); |
3714 | |
3715 | CanonType = SubstType(Pattern->getUnderlyingType(), |
3716 | TemplateArgLists, AliasTemplate->getLocation(), |
3717 | AliasTemplate->getDeclName()); |
3718 | if (CanonType.isNull()) { |
3719 | |
3720 | |
3721 | |
3722 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
3723 | if (auto DeductionInfo = isSFINAEContext()) { |
3724 | if (*DeductionInfo && |
3725 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
3726 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
3727 | diag::err_typename_nested_not_found_enable_if && |
3728 | TemplateArgs[0].getArgument().getKind() |
3729 | == TemplateArgument::Expression) { |
3730 | Expr *FailedCond; |
3731 | std::string FailedDescription; |
3732 | std::tie(FailedCond, FailedDescription) = |
3733 | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); |
3734 | |
3735 | |
3736 | PartialDiagnosticAt OldDiag = |
3737 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
3738 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
3739 | |
3740 | |
3741 | |
3742 | (*DeductionInfo)->addSFINAEDiagnostic( |
3743 | OldDiag.first, |
3744 | PDiag(diag::err_typename_nested_not_found_requirement) |
3745 | << FailedDescription |
3746 | << FailedCond->getSourceRange()); |
3747 | } |
3748 | } |
3749 | } |
3750 | |
3751 | return QualType(); |
3752 | } |
3753 | } else if (Name.isDependent() || |
3754 | TemplateSpecializationType::anyDependentTemplateArguments( |
3755 | TemplateArgs, Converted)) { |
3756 | |
3757 | |
3758 | |
3759 | |
3760 | |
3761 | |
3762 | |
3763 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
3764 | |
3765 | |
3766 | |
3767 | |
3768 | |
3769 | |
3770 | |
3771 | if (isa<ClassTemplateDecl>(Template)) { |
3772 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
3773 | |
3774 | if (Ctx->isFileContext()) break; |
3775 | |
3776 | |
3777 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
3778 | if (!Record) continue; |
3779 | |
3780 | |
3781 | |
3782 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
3783 | !Record->getDescribedClassTemplate()) |
3784 | continue; |
3785 | |
3786 | |
3787 | |
3788 | QualType ICNT = Context.getTypeDeclType(Record); |
3789 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
3790 | ->getInjectedSpecializationType(); |
3791 | |
3792 | if (CanonType != Injected->getCanonicalTypeInternal()) |
3793 | continue; |
3794 | |
3795 | |
3796 | |
3797 | assert(ICNT.isCanonical()); |
3798 | CanonType = ICNT; |
3799 | break; |
3800 | } |
3801 | } |
3802 | } else if (ClassTemplateDecl *ClassTemplate |
3803 | = dyn_cast<ClassTemplateDecl>(Template)) { |
3804 | |
3805 | |
3806 | void *InsertPos = nullptr; |
3807 | ClassTemplateSpecializationDecl *Decl |
3808 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
3809 | if (!Decl) { |
3810 | |
3811 | |
3812 | |
3813 | Decl = ClassTemplateSpecializationDecl::Create( |
3814 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
3815 | ClassTemplate->getDeclContext(), |
3816 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
3817 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
3818 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
3819 | if (ClassTemplate->isOutOfLine()) |
3820 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
3821 | } |
3822 | |
3823 | if (Decl->getSpecializationKind() == TSK_Undeclared && |
3824 | ClassTemplate->getTemplatedDecl()->hasAttrs()) { |
3825 | InstantiatingTemplate Inst(*this, TemplateLoc, Decl); |
3826 | if (!Inst.isInvalid()) { |
3827 | MultiLevelTemplateArgumentList TemplateArgLists; |
3828 | TemplateArgLists.addOuterTemplateArguments(Converted); |
3829 | InstantiateAttrsForDecl(TemplateArgLists, |
3830 | ClassTemplate->getTemplatedDecl(), Decl); |
3831 | } |
3832 | } |
3833 | |
3834 | |
3835 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
3836 | |
3837 | CanonType = Context.getTypeDeclType(Decl); |
3838 | assert(isa<RecordType>(CanonType) && |
3839 | "type of non-dependent specialization is not a RecordType"); |
3840 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
3841 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
3842 | TemplateArgs); |
3843 | } |
3844 | |
3845 | |
3846 | |
3847 | |
3848 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
3849 | } |
3850 | |
3851 | void Sema::ActOnUndeclaredTypeTemplateName(Scope *S, TemplateTy &ParsedName, |
3852 | TemplateNameKind &TNK, |
3853 | SourceLocation NameLoc, |
3854 | IdentifierInfo *&II) { |
3855 | assert(TNK == TNK_Undeclared_template && "not an undeclared template name"); |
3856 | |
3857 | TemplateName Name = ParsedName.get(); |
3858 | auto *ATN = Name.getAsAssumedTemplateName(); |
3859 | assert(ATN && "not an assumed template name"); |
3860 | II = ATN->getDeclName().getAsIdentifierInfo(); |
3861 | |
3862 | if (!resolveAssumedTemplateNameAsType(S, Name, NameLoc, false)) { |
3863 | |
3864 | ParsedName = TemplateTy::make(Name); |
3865 | TNK = TNK_Type_template; |
3866 | } |
3867 | } |
3868 | |
3869 | bool Sema::resolveAssumedTemplateNameAsType(Scope *S, TemplateName &Name, |
3870 | SourceLocation NameLoc, |
3871 | bool Diagnose) { |
3872 | |
3873 | |
3874 | |
3875 | AssumedTemplateStorage *ATN = Name.getAsAssumedTemplateName(); |
3876 | assert(ATN && "not an assumed template name"); |
3877 | |
3878 | LookupResult R(*this, ATN->getDeclName(), NameLoc, LookupOrdinaryName); |
3879 | struct CandidateCallback : CorrectionCandidateCallback { |
3880 | bool ValidateCandidate(const TypoCorrection &TC) override { |
3881 | return TC.getCorrectionDecl() && |
3882 | getAsTypeTemplateDecl(TC.getCorrectionDecl()); |
3883 | } |
3884 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
3885 | return std::make_unique<CandidateCallback>(*this); |
3886 | } |
3887 | } FilterCCC; |
3888 | |
3889 | TypoCorrection Corrected = |
3890 | CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr, |
3891 | FilterCCC, CTK_ErrorRecovery); |
3892 | if (Corrected && Corrected.getFoundDecl()) { |
3893 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) |
3894 | << ATN->getDeclName()); |
3895 | Name = TemplateName(Corrected.getCorrectionDeclAs<TemplateDecl>()); |
3896 | return false; |
3897 | } |
3898 | |
3899 | if (Diagnose) |
3900 | Diag(R.getNameLoc(), diag::err_no_template) << R.getLookupName(); |
3901 | return true; |
3902 | } |
3903 | |
3904 | TypeResult Sema::ActOnTemplateIdType( |
3905 | Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
3906 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
3907 | SourceLocation TemplateIILoc, SourceLocation LAngleLoc, |
3908 | ASTTemplateArgsPtr TemplateArgsIn, SourceLocation RAngleLoc, |
3909 | bool IsCtorOrDtorName, bool IsClassName) { |
3910 | if (SS.isInvalid()) |
3911 | return true; |
3912 | |
3913 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
3914 | DeclContext *LookupCtx = computeDeclContext(SS, false); |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
3923 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
3924 | << SS.getScopeRep() << TemplateII->getName(); |
3925 | |
3926 | |
3927 | |
3928 | |
3929 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
3930 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
3931 | TemplateArgsIn, RAngleLoc); |
3932 | } |
3933 | |
3934 | |
3935 | |
3936 | |
3937 | |
3938 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
3939 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
3940 | Diag(TemplateIILoc, |
3941 | TemplateKWLoc.isInvalid() |
3942 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
3943 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
3944 | << TemplateII << 0 |
3945 | << 1 ; |
3946 | } |
3947 | } |
3948 | |
3949 | TemplateName Template = TemplateD.get(); |
3950 | if (Template.getAsAssumedTemplateName() && |
3951 | resolveAssumedTemplateNameAsType(S, Template, TemplateIILoc)) |
3952 | return true; |
3953 | |
3954 | |
3955 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
3956 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
3957 | |
3958 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
3959 | QualType T |
3960 | = Context.getDependentTemplateSpecializationType(ETK_None, |
3961 | DTN->getQualifier(), |
3962 | DTN->getIdentifier(), |
3963 | TemplateArgs); |
3964 | |
3965 | TypeLocBuilder TLB; |
3966 | DependentTemplateSpecializationTypeLoc SpecTL |
3967 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
3968 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
3969 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
3970 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
3971 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
3972 | SpecTL.setLAngleLoc(LAngleLoc); |
3973 | SpecTL.setRAngleLoc(RAngleLoc); |
3974 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
3975 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
3976 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
3977 | } |
3978 | |
3979 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
3980 | if (Result.isNull()) |
3981 | return true; |
3982 | |
3983 | |
3984 | TypeLocBuilder TLB; |
3985 | TemplateSpecializationTypeLoc SpecTL |
3986 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
3987 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
3988 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
3989 | SpecTL.setLAngleLoc(LAngleLoc); |
3990 | SpecTL.setRAngleLoc(RAngleLoc); |
3991 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
3992 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
3993 | |
3994 | |
3995 | |
3996 | |
3997 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
3998 | |
3999 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
4000 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
4001 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
4002 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4003 | } |
4004 | |
4005 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
4006 | } |
4007 | |
4008 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
4009 | TypeSpecifierType TagSpec, |
4010 | SourceLocation TagLoc, |
4011 | CXXScopeSpec &SS, |
4012 | SourceLocation TemplateKWLoc, |
4013 | TemplateTy TemplateD, |
4014 | SourceLocation TemplateLoc, |
4015 | SourceLocation LAngleLoc, |
4016 | ASTTemplateArgsPtr TemplateArgsIn, |
4017 | SourceLocation RAngleLoc) { |
4018 | if (SS.isInvalid()) |
4019 | return TypeResult(true); |
4020 | |
4021 | TemplateName Template = TemplateD.get(); |
4022 | |
4023 | |
4024 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
4025 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
4026 | |
4027 | |
4028 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
4029 | ElaboratedTypeKeyword Keyword |
4030 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
4031 | |
4032 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
4033 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
4034 | DTN->getQualifier(), |
4035 | DTN->getIdentifier(), |
4036 | TemplateArgs); |
4037 | |
4038 | |
4039 | TypeLocBuilder TLB; |
4040 | DependentTemplateSpecializationTypeLoc SpecTL |
4041 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
4042 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
4043 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4044 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
4045 | SpecTL.setTemplateNameLoc(TemplateLoc); |
4046 | SpecTL.setLAngleLoc(LAngleLoc); |
4047 | SpecTL.setRAngleLoc(RAngleLoc); |
4048 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
4049 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
4050 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
4051 | } |
4052 | |
4053 | if (TypeAliasTemplateDecl *TAT = |
4054 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
4055 | |
4056 | |
4057 | |
4058 | |
4059 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
4060 | << TAT << NTK_TypeAliasTemplate << TagKind; |
4061 | Diag(TAT->getLocation(), diag::note_declared_at); |
4062 | } |
4063 | |
4064 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
4065 | if (Result.isNull()) |
4066 | return TypeResult(true); |
4067 | |
4068 | |
4069 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
4070 | RecordDecl *D = RT->getDecl(); |
4071 | |
4072 | IdentifierInfo *Id = D->getIdentifier(); |
4073 | assert(Id && "templated class must have an identifier"); |
4074 | |
4075 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
4076 | TagLoc, Id)) { |
4077 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
4078 | << Result |
4079 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
4080 | Diag(D->getLocation(), diag::note_previous_use); |
4081 | } |
4082 | } |
4083 | |
4084 | |
4085 | TypeLocBuilder TLB; |
4086 | TemplateSpecializationTypeLoc SpecTL |
4087 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
4088 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
4089 | SpecTL.setTemplateNameLoc(TemplateLoc); |
4090 | SpecTL.setLAngleLoc(LAngleLoc); |
4091 | SpecTL.setRAngleLoc(RAngleLoc); |
4092 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
4093 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
4094 | |
4095 | |
4096 | |
4097 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
4098 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
4099 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
4100 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
4101 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
4102 | } |
4103 | |
4104 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
4105 | NamedDecl *PrevDecl, |
4106 | SourceLocation Loc, |
4107 | bool IsPartialSpecialization); |
4108 | |
4109 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
4110 | |
4111 | static bool isTemplateArgumentTemplateParameter( |
4112 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
4113 | switch (Arg.getKind()) { |
4114 | case TemplateArgument::Null: |
4115 | case TemplateArgument::NullPtr: |
4116 | case TemplateArgument::Integral: |
4117 | case TemplateArgument::Declaration: |
4118 | case TemplateArgument::Pack: |
4119 | case TemplateArgument::TemplateExpansion: |
4120 | return false; |
4121 | |
4122 | case TemplateArgument::Type: { |
4123 | QualType Type = Arg.getAsType(); |
4124 | const TemplateTypeParmType *TPT = |
4125 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
4126 | return TPT && !Type.hasQualifiers() && |
4127 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
4128 | } |
4129 | |
4130 | case TemplateArgument::Expression: { |
4131 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
4132 | if (!DRE || !DRE->getDecl()) |
4133 | return false; |
4134 | const NonTypeTemplateParmDecl *NTTP = |
4135 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
4136 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
4137 | } |
4138 | |
4139 | case TemplateArgument::Template: |
4140 | const TemplateTemplateParmDecl *TTP = |
4141 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
4142 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
4143 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
4144 | } |
4145 | llvm_unreachable("unexpected kind of template argument"); |
4146 | } |
4147 | |
4148 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
4149 | ArrayRef<TemplateArgument> Args) { |
4150 | if (Params->size() != Args.size()) |
4151 | return false; |
4152 | |
4153 | unsigned Depth = Params->getDepth(); |
4154 | |
4155 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
4156 | TemplateArgument Arg = Args[I]; |
4157 | |
4158 | |
4159 | |
4160 | if (Params->getParam(I)->isParameterPack()) { |
4161 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
4162 | !Arg.pack_begin()->isPackExpansion()) |
4163 | return false; |
4164 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
4165 | } |
4166 | |
4167 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
4168 | return false; |
4169 | } |
4170 | |
4171 | return true; |
4172 | } |
4173 | |
4174 | template<typename PartialSpecDecl> |
4175 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
4176 | if (Partial->getDeclContext()->isDependentContext()) |
4177 | return; |
4178 | |
4179 | |
4180 | |
4181 | TemplateDeductionInfo Info(Partial->getLocation()); |
4182 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
4183 | return; |
4184 | |
4185 | auto *Template = Partial->getSpecializedTemplate(); |
4186 | S.Diag(Partial->getLocation(), |
4187 | diag::ext_partial_spec_not_more_specialized_than_primary) |
4188 | << isa<VarTemplateDecl>(Template); |
4189 | |
4190 | if (Info.hasSFINAEDiagnostic()) { |
4191 | PartialDiagnosticAt Diag = {SourceLocation(), |
4192 | PartialDiagnostic::NullDiagnostic()}; |
4193 | Info.takeSFINAEDiagnostic(Diag); |
4194 | SmallString<128> SFINAEArgString; |
4195 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
4196 | S.Diag(Diag.first, |
4197 | diag::note_partial_spec_not_more_specialized_than_primary) |
4198 | << SFINAEArgString; |
4199 | } |
4200 | |
4201 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
4202 | SmallVector<const Expr *, 3> PartialAC, TemplateAC; |
4203 | Template->getAssociatedConstraints(TemplateAC); |
4204 | Partial->getAssociatedConstraints(PartialAC); |
4205 | S.MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Partial, PartialAC, Template, |
4206 | TemplateAC); |
4207 | } |
4208 | |
4209 | static void |
4210 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
4211 | const llvm::SmallBitVector &DeducibleParams) { |
4212 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
4213 | if (!DeducibleParams[I]) { |
4214 | NamedDecl *Param = TemplateParams->getParam(I); |
4215 | if (Param->getDeclName()) |
4216 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
4217 | << Param->getDeclName(); |
4218 | else |
4219 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
4220 | << "(anonymous)"; |
4221 | } |
4222 | } |
4223 | } |
4224 | |
4225 | |
4226 | template<typename PartialSpecDecl> |
4227 | static void checkTemplatePartialSpecialization(Sema &S, |
4228 | PartialSpecDecl *Partial) { |
4229 | |
4230 | |
4231 | |
4232 | checkMoreSpecializedThanPrimary(S, Partial); |
4233 | |
4234 | |
4235 | |
4236 | |
4237 | |
4238 | |
4239 | |
4240 | |
4241 | auto *TemplateParams = Partial->getTemplateParameters(); |
4242 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
4243 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
4244 | TemplateParams->getDepth(), DeducibleParams); |
4245 | |
4246 | if (!DeducibleParams.all()) { |
4247 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
4248 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
4249 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
4250 | << (NumNonDeducible > 1) |
4251 | << SourceRange(Partial->getLocation(), |
4252 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
4253 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
4254 | } |
4255 | } |
4256 | |
4257 | void Sema::CheckTemplatePartialSpecialization( |
4258 | ClassTemplatePartialSpecializationDecl *Partial) { |
4259 | checkTemplatePartialSpecialization(*this, Partial); |
4260 | } |
4261 | |
4262 | void Sema::CheckTemplatePartialSpecialization( |
4263 | VarTemplatePartialSpecializationDecl *Partial) { |
4264 | checkTemplatePartialSpecialization(*this, Partial); |
4265 | } |
4266 | |
4267 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
4268 | |
4269 | |
4270 | |
4271 | |
4272 | auto *TemplateParams = TD->getTemplateParameters(); |
4273 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
4274 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
4275 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
4276 | |
4277 | auto *Param = TemplateParams->getParam(I); |
4278 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
4279 | DeducibleParams[I] = true; |
4280 | } |
4281 | |
4282 | if (!DeducibleParams.all()) { |
4283 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
4284 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
4285 | << (NumNonDeducible > 1); |
4286 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
4287 | } |
4288 | } |
4289 | |
4290 | DeclResult Sema::ActOnVarTemplateSpecialization( |
4291 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
4292 | TemplateParameterList *TemplateParams, StorageClass SC, |
4293 | bool IsPartialSpecialization) { |
4294 | |
4295 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
4296 | "Variable template specialization is declared with a template it."); |
4297 | |
4298 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
4299 | TemplateArgumentListInfo TemplateArgs = |
4300 | makeTemplateArgumentListInfo(*this, *TemplateId); |
4301 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
4302 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
4303 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
4304 | |
4305 | TemplateName Name = TemplateId->Template.get(); |
4306 | |
4307 | |
4308 | VarTemplateDecl *VarTemplate = |
4309 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
4310 | if (!VarTemplate) { |
4311 | NamedDecl *FnTemplate; |
4312 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
4313 | FnTemplate = *OTS->begin(); |
4314 | else |
4315 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
4316 | if (FnTemplate) |
4317 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
4318 | << FnTemplate->getDeclName(); |
4319 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
4320 | << IsPartialSpecialization; |
4321 | } |
4322 | |
4323 | |
4324 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
4325 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
4326 | UPPC_PartialSpecialization)) |
4327 | return true; |
4328 | |
4329 | |
4330 | |
4331 | SmallVector<TemplateArgument, 4> Converted; |
4332 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
4333 | false, Converted, |
4334 | true)) |
4335 | return true; |
4336 | |
4337 | |
4338 | |
4339 | if (IsPartialSpecialization) { |
4340 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
4341 | TemplateArgs.size(), Converted)) |
4342 | return true; |
4343 | |
4344 | |
4345 | |
4346 | if (!Name.isDependent() && |
4347 | !TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs, |
4348 | Converted)) { |
4349 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
4350 | << VarTemplate->getDeclName(); |
4351 | IsPartialSpecialization = false; |
4352 | } |
4353 | |
4354 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
4355 | Converted) && |
4356 | (!Context.getLangOpts().CPlusPlus20 || |
4357 | !TemplateParams->hasAssociatedConstraints())) { |
4358 | |
4359 | |
4360 | |
4361 | |
4362 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
4363 | << 1 |
4364 | << (SC != SC_Extern && !CurContext->isRecord()) |
4365 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
4366 | |
4367 | |
4368 | return true; |
4369 | } |
4370 | } |
4371 | |
4372 | void *InsertPos = nullptr; |
4373 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
4374 | |
4375 | if (IsPartialSpecialization) |
4376 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, TemplateParams, |
4377 | InsertPos); |
4378 | else |
4379 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
4380 | |
4381 | VarTemplateSpecializationDecl *Specialization = nullptr; |
4382 | |
4383 | |
4384 | |
4385 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
4386 | TemplateNameLoc, |
4387 | IsPartialSpecialization)) |
4388 | return true; |
4389 | |
4390 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
4391 | |
4392 | |
4393 | |
4394 | |
4395 | Specialization = PrevDecl; |
4396 | Specialization->setLocation(TemplateNameLoc); |
4397 | PrevDecl = nullptr; |
4398 | } else if (IsPartialSpecialization) { |
4399 | |
4400 | VarTemplatePartialSpecializationDecl *PrevPartial = |
4401 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
4402 | VarTemplatePartialSpecializationDecl *Partial = |
4403 | VarTemplatePartialSpecializationDecl::Create( |
4404 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
4405 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
4406 | Converted, TemplateArgs); |
4407 | |
4408 | if (!PrevPartial) |
4409 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
4410 | Specialization = Partial; |
4411 | |
4412 | |
4413 | |
4414 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
4415 | PrevPartial->setMemberSpecialization(); |
4416 | |
4417 | CheckTemplatePartialSpecialization(Partial); |
4418 | } else { |
4419 | |
4420 | |
4421 | Specialization = VarTemplateSpecializationDecl::Create( |
4422 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
4423 | VarTemplate, DI->getType(), DI, SC, Converted); |
4424 | Specialization->setTemplateArgsInfo(TemplateArgs); |
4425 | |
4426 | if (!PrevDecl) |
4427 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
4428 | } |
4429 | |
4430 | |
4431 | |
4432 | |
4433 | |
4434 | |
4435 | |
4436 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
4437 | bool Okay = false; |
4438 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
4439 | |
4440 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
4441 | Okay = true; |
4442 | break; |
4443 | } |
4444 | } |
4445 | |
4446 | if (!Okay) { |
4447 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
4448 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
4449 | << Name << Range; |
4450 | |
4451 | Diag(PrevDecl->getPointOfInstantiation(), |
4452 | diag::note_instantiation_required_here) |
4453 | << (PrevDecl->getTemplateSpecializationKind() != |
4454 | TSK_ImplicitInstantiation); |
4455 | return true; |
4456 | } |
4457 | } |
4458 | |
4459 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
4460 | Specialization->setLexicalDeclContext(CurContext); |
4461 | |
4462 | |
4463 | |
4464 | |
4465 | CurContext->addDecl(Specialization); |
4466 | |
4467 | |
4468 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
4469 | |
4470 | if (PrevDecl) { |
4471 | |
4472 | |
4473 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
4474 | forRedeclarationInCurContext()); |
4475 | PrevSpec.addDecl(PrevDecl); |
4476 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
4477 | } else if (Specialization->isStaticDataMember() && |
4478 | Specialization->isOutOfLine()) { |
4479 | Specialization->setAccess(VarTemplate->getAccess()); |
4480 | } |
4481 | |
4482 | return Specialization; |
4483 | } |
4484 | |
4485 | namespace { |
4486 | |
4487 | |
4488 | struct PartialSpecMatchResult { |
4489 | VarTemplatePartialSpecializationDecl *Partial; |
4490 | TemplateArgumentList *Args; |
4491 | }; |
4492 | } |
4493 | |
4494 | DeclResult |
4495 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
4496 | SourceLocation TemplateNameLoc, |
4497 | const TemplateArgumentListInfo &TemplateArgs) { |
4498 | assert(Template && "A variable template id without template?"); |
4499 | |
4500 | |
4501 | SmallVector<TemplateArgument, 4> Converted; |
4502 | if (CheckTemplateArgumentList( |
4503 | Template, TemplateNameLoc, |
4504 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
4505 | Converted, true)) |
4506 | return true; |
4507 | |
4508 | |
4509 | if (Template->getDeclContext()->isDependentContext() || |
4510 | TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs, |
4511 | Converted)) |
4512 | return DeclResult(); |
4513 | |
4514 | |
4515 | |
4516 | void *InsertPos = nullptr; |
4517 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
4518 | Converted, InsertPos)) { |
4519 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
4520 | |
4521 | return Spec; |
4522 | } |
4523 | |
4524 | |
4525 | |
4526 | |
4527 | |
4528 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
4529 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
4530 | Converted); |
4531 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
4532 | bool AmbiguousPartialSpec = false; |
4533 | typedef PartialSpecMatchResult MatchResult; |
4534 | SmallVector<MatchResult, 4> Matched; |
4535 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
4536 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
4537 | false); |
4538 | |
4539 | |
4540 | |
4541 | |
4542 | |
4543 | |
4544 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
4545 | Template->getPartialSpecializations(PartialSpecs); |
4546 | |
4547 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
4548 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
4549 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
4550 | |
4551 | if (TemplateDeductionResult Result = |
4552 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
4553 | |
4554 | |
4555 | FailedCandidates.addCandidate().set( |
4556 | DeclAccessPair::make(Template, AS_public), Partial, |
4557 | MakeDeductionFailureInfo(Context, Result, Info)); |
4558 | (void)Result; |
4559 | } else { |
4560 | Matched.push_back(PartialSpecMatchResult()); |
4561 | Matched.back().Partial = Partial; |
4562 | Matched.back().Args = Info.take(); |
4563 | } |
4564 | } |
4565 | |
4566 | if (Matched.size() >= 1) { |
4567 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
4568 | if (Matched.size() == 1) { |
4569 | |
4570 | |
4571 | |
4572 | } else { |
4573 | |
4574 | |
4575 | |
4576 | |
4577 | |
4578 | |
4579 | |
4580 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
4581 | PEnd = Matched.end(); |
4582 | P != PEnd; ++P) { |
4583 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
4584 | PointOfInstantiation) == |
4585 | P->Partial) |
4586 | Best = P; |
4587 | } |
4588 | |
4589 | |
4590 | |
4591 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
4592 | PEnd = Matched.end(); |
4593 | P != PEnd; ++P) { |
4594 | if (P != Best && getMoreSpecializedPartialSpecialization( |
4595 | P->Partial, Best->Partial, |
4596 | PointOfInstantiation) != Best->Partial) { |
4597 | AmbiguousPartialSpec = true; |
4598 | break; |
4599 | } |
4600 | } |
4601 | } |
4602 | |
4603 | |
4604 | InstantiationPattern = Best->Partial; |
4605 | InstantiationArgs = Best->Args; |
4606 | } else { |
4607 | |
4608 | |
4609 | |
4610 | } |
4611 | |
4612 | |
4613 | |
4614 | |
4615 | |
4616 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
4617 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
4618 | Converted, TemplateNameLoc ); |
4619 | if (!Decl) |
4620 | return true; |
4621 | |
4622 | if (AmbiguousPartialSpec) { |
4623 | |
4624 | Decl->setInvalidDecl(); |
4625 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
4626 | << Decl; |
4627 | |
4628 | |
4629 | for (MatchResult P : Matched) |
4630 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
4631 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
4632 | *P.Args); |
4633 | return true; |
4634 | } |
4635 | |
4636 | if (VarTemplatePartialSpecializationDecl *D = |
4637 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
4638 | Decl->setInstantiationOf(D, InstantiationArgs); |
4639 | |
4640 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
4641 | |
4642 | assert(Decl && "No variable template specialization?"); |
4643 | return Decl; |
4644 | } |
4645 | |
4646 | ExprResult |
4647 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
4648 | const DeclarationNameInfo &NameInfo, |
4649 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
4650 | const TemplateArgumentListInfo *TemplateArgs) { |
4651 | |
4652 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 62 | | Forming reference to null pointer |
|
4653 | *TemplateArgs); |
4654 | if (Decl.isInvalid()) |
4655 | return ExprError(); |
4656 | |
4657 | if (!Decl.get()) |
4658 | return ExprResult(); |
4659 | |
4660 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
4661 | if (!Var->getTemplateSpecializationKind()) |
4662 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
4663 | NameInfo.getLoc()); |
4664 | |
4665 | |
4666 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
4667 | nullptr, TemplateArgs); |
4668 | } |
4669 | |
4670 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
4671 | SourceLocation Loc) { |
4672 | Diag(Loc, diag::err_template_missing_args) |
4673 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
4674 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
4675 | Diag(TD->getLocation(), diag::note_template_decl_here) |
4676 | << TD->getTemplateParameters()->getSourceRange(); |
4677 | } |
4678 | } |
4679 | |
4680 | ExprResult |
4681 | Sema::CheckConceptTemplateId(const CXXScopeSpec &SS, |
4682 | SourceLocation TemplateKWLoc, |
4683 | const DeclarationNameInfo &ConceptNameInfo, |
4684 | NamedDecl *FoundDecl, |
4685 | ConceptDecl *NamedConcept, |
4686 | const TemplateArgumentListInfo *TemplateArgs) { |
4687 | assert(NamedConcept && "A concept template id without a template?"); |
4688 | |
4689 | llvm::SmallVector<TemplateArgument, 4> Converted; |
4690 | if (CheckTemplateArgumentList(NamedConcept, ConceptNameInfo.getLoc(), |
4691 | const_cast<TemplateArgumentListInfo&>(*TemplateArgs), |
4692 | false, Converted, |
4693 | false)) |
4694 | return ExprError(); |
4695 | |
4696 | ConstraintSatisfaction Satisfaction; |
4697 | bool AreArgsDependent = |
4698 | TemplateSpecializationType::anyDependentTemplateArguments(*TemplateArgs, |
4699 | Converted); |
4700 | if (!AreArgsDependent && |
4701 | CheckConstraintSatisfaction( |
4702 | NamedConcept, {NamedConcept->getConstraintExpr()}, Converted, |
4703 | SourceRange(SS.isSet() ? SS.getBeginLoc() : ConceptNameInfo.getLoc(), |
4704 | TemplateArgs->getRAngleLoc()), |
4705 | Satisfaction)) |
4706 | return ExprError(); |
4707 | |
4708 | return ConceptSpecializationExpr::Create(Context, |
4709 | SS.isSet() ? SS.getWithLocInContext(Context) : NestedNameSpecifierLoc{}, |
4710 | TemplateKWLoc, ConceptNameInfo, FoundDecl, NamedConcept, |
4711 | ASTTemplateArgumentListInfo::Create(Context, *TemplateArgs), Converted, |
4712 | AreArgsDependent ? nullptr : &Satisfaction); |
4713 | } |
4714 | |
4715 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
4716 | SourceLocation TemplateKWLoc, |
4717 | LookupResult &R, |
4718 | bool RequiresADL, |
4719 | const TemplateArgumentListInfo *TemplateArgs) { |
4720 | |
4721 | |
4722 | |
4723 | |
4724 | |
4725 | |
4726 | |
4727 | |
4728 | |
4729 | |
4730 | |
4731 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
4732 | |
4733 | |
4734 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 53 | | Assuming 'TD' is non-null | |
|
| |
4735 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 55 | | Assuming 'TemplateArgs' is null | |
|
| 56 | | Assuming 'TD' is a 'FunctionTemplateDecl' | |
|
| |
4736 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
4737 | return ExprError(); |
4738 | } |
4739 | } |
4740 | |
4741 | |
4742 | if (R.getAsSingle<VarTemplateDecl>()) { |
| 58 | | Assuming the condition is true | |
|
| |
4743 | ExprResult Res = CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 61 | | Calling 'Sema::CheckVarTemplateId' | |
|
4744 | R.getAsSingle<VarTemplateDecl>(), |
4745 | TemplateKWLoc, TemplateArgs); |
| 60 | | Passing null pointer value via 5th parameter 'TemplateArgs' | |
|
4746 | if (Res.isInvalid() || Res.isUsable()) |
4747 | return Res; |
4748 | |
4749 | } |
4750 | |
4751 | if (R.getAsSingle<ConceptDecl>()) { |
4752 | return CheckConceptTemplateId(SS, TemplateKWLoc, R.getLookupNameInfo(), |
4753 | R.getFoundDecl(), |
4754 | R.getAsSingle<ConceptDecl>(), TemplateArgs); |
4755 | } |
4756 | |
4757 | |
4758 | R.suppressDiagnostics(); |
4759 | |
4760 | UnresolvedLookupExpr *ULE |
4761 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
4762 | SS.getWithLocInContext(Context), |
4763 | TemplateKWLoc, |
4764 | R.getLookupNameInfo(), |
4765 | RequiresADL, TemplateArgs, |
4766 | R.begin(), R.end()); |
4767 | |
4768 | return ULE; |
4769 | } |
4770 | |
4771 | |
4772 | ExprResult |
4773 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
4774 | SourceLocation TemplateKWLoc, |
4775 | const DeclarationNameInfo &NameInfo, |
4776 | const TemplateArgumentListInfo *TemplateArgs) { |
4777 | |
4778 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
4779 | DeclContext *DC; |
4780 | if (!(DC = computeDeclContext(SS, false)) || |
| 1 | Assuming pointer value is null | |
|
| 2 | | Assuming 'DC' is non-null | |
|
| |
4781 | DC->isDependentContext() || |
| 3 | | Assuming the condition is false | |
|
4782 | RequireCompleteDeclContext(SS, DC)) |
| 4 | | Assuming the condition is false | |
|
4783 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
4784 | |
4785 | bool MemberOfUnknownSpecialization; |
4786 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
4787 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 6 | | Calling 'Sema::LookupTemplateName' | |
|
| 43 | | Returning from 'Sema::LookupTemplateName' | |
|
| |
4788 | false, MemberOfUnknownSpecialization, |
4789 | TemplateKWLoc)) |
4790 | return ExprError(); |
4791 | |
4792 | if (R.isAmbiguous()) |
| 45 | | Assuming the condition is false | |
|
| |
4793 | return ExprError(); |
4794 | |
4795 | if (R.empty()) { |
| 47 | | Assuming the condition is false | |
|
| |
4796 | Diag(NameInfo.getLoc(), diag::err_no_member) |
4797 | << NameInfo.getName() << DC << SS.getRange(); |
4798 | return ExprError(); |
4799 | } |
4800 | |
4801 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 49 | | Assuming 'Temp' is null | |
|
| |
4802 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
4803 | << SS.getScopeRep() |
4804 | << NameInfo.getName().getAsString() << SS.getRange(); |
4805 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
4806 | return ExprError(); |
4807 | } |
4808 | |
4809 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs); |
| 51 | | Passing value via 5th parameter 'TemplateArgs' | |
|
| 52 | | Calling 'Sema::BuildTemplateIdExpr' | |
|
4810 | } |
4811 | |
4812 | |
4813 | |
4814 | |
4815 | |
4816 | |
4817 | |
4818 | |
4819 | |
4820 | |
4821 | |
4822 | |
4823 | |
4824 | |
4825 | |
4826 | TemplateNameKind Sema::ActOnTemplateName(Scope *S, |
4827 | CXXScopeSpec &SS, |
4828 | SourceLocation TemplateKWLoc, |
4829 | const UnqualifiedId &Name, |
4830 | ParsedType ObjectType, |
4831 | bool EnteringContext, |
4832 | TemplateTy &Result, |
4833 | bool AllowInjectedClassName) { |
4834 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
4835 | Diag(TemplateKWLoc, |
4836 | getLangOpts().CPlusPlus11 ? |
4837 | diag::warn_cxx98_compat_template_outside_of_template : |
4838 | diag::ext_template_outside_of_template) |
4839 | << FixItHint::CreateRemoval(TemplateKWLoc); |
4840 | |
4841 | if (SS.isInvalid()) |
4842 | return TNK_Non_template; |
4843 | |
4844 | |
4845 | DeclContext *LookupCtx = nullptr; |
4846 | if (SS.isNotEmpty()) |
4847 | LookupCtx = computeDeclContext(SS, EnteringContext); |
4848 | else if (ObjectType) |
4849 | LookupCtx = computeDeclContext(GetTypeFromParser(ObjectType)); |
4850 | |
4851 | |
4852 | |
4853 | |
4854 | |
4855 | |
4856 | |
4857 | |
4858 | |
4859 | |
4860 | |
4861 | |
4862 | |
4863 | |
4864 | |
4865 | |
4866 | |
4867 | bool MemberOfUnknownSpecialization; |
4868 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
4869 | ObjectType, EnteringContext, Result, |
4870 | MemberOfUnknownSpecialization); |
4871 | if (TNK != TNK_Non_template) { |
4872 | |
4873 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
4874 | if (!AllowInjectedClassName && SS.isNotEmpty() && LookupRD && |
4875 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
4876 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
4877 | |
4878 | |
4879 | |
4880 | |
4881 | |
4882 | |
4883 | |
4884 | |
4885 | |
4886 | Diag(Name.getBeginLoc(), |
4887 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
4888 | << Name.Identifier |
4889 | << 0 |
4890 | << TemplateKWLoc.isValid(); |
4891 | } |
4892 | return TNK; |
4893 | } |
4894 | |
4895 | if (!MemberOfUnknownSpecialization) { |
4896 | |
4897 | |
4898 | |
4899 | |
4900 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
4901 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), |
4902 | LookupOrdinaryName); |
4903 | bool MOUS; |
4904 | |
4905 | |
4906 | RequiredTemplateKind RTK = TemplateKWLoc.isValid() |
4907 | ? RequiredTemplateKind(TemplateKWLoc) |
4908 | : TemplateNameIsRequired; |
4909 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, MOUS, |
4910 | RTK, nullptr, false) && |
4911 | !R.isAmbiguous()) { |
4912 | if (LookupCtx) |
4913 | Diag(Name.getBeginLoc(), diag::err_no_member) |
4914 | << DNI.getName() << LookupCtx << SS.getRange(); |
4915 | else |
4916 | Diag(Name.getBeginLoc(), diag::err_undeclared_use) |
4917 | << DNI.getName() << SS.getRange(); |
4918 | } |
4919 | return TNK_Non_template; |
4920 | } |
4921 | |
4922 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
4923 | |
4924 | switch (Name.getKind()) { |
4925 | case UnqualifiedIdKind::IK_Identifier: |
4926 | Result = TemplateTy::make( |
4927 | Context.getDependentTemplateName(Qualifier, Name.Identifier)); |
4928 | return TNK_Dependent_template_name; |
4929 | |
4930 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
4931 | Result = TemplateTy::make(Context.getDependentTemplateName( |
4932 | Qualifier, Name.OperatorFunctionId.Operator)); |
4933 | return TNK_Function_template; |
4934 | |
4935 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
4936 | |
4937 | |
4938 | break; |
4939 | |
4940 | default: |
4941 | break; |
4942 | } |
4943 | |
4944 | |
4945 | |
4946 | Diag(Name.getBeginLoc(), |
4947 | diag::err_template_kw_refers_to_dependent_non_template) |
4948 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() |
4949 | << TemplateKWLoc.isValid() << TemplateKWLoc; |
4950 | return TNK_Non_template; |
4951 | } |
4952 | |
4953 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
4954 | TemplateArgumentLoc &AL, |
4955 | SmallVectorImpl<TemplateArgument> &Converted) { |
4956 | const TemplateArgument &Arg = AL.getArgument(); |
4957 | QualType ArgType; |
4958 | TypeSourceInfo *TSI = nullptr; |
4959 | |
4960 | |
4961 | switch(Arg.getKind()) { |
4962 | case TemplateArgument::Type: |
4963 | |
4964 | |
4965 | |
4966 | ArgType = Arg.getAsType(); |
4967 | TSI = AL.getTypeSourceInfo(); |
4968 | break; |
4969 | case TemplateArgument::Template: |
4970 | case TemplateArgument::TemplateExpansion: { |
4971 | |
4972 | |
4973 | SourceRange SR = AL.getSourceRange(); |
4974 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); |
4975 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); |
4976 | return true; |
4977 | } |
4978 | case TemplateArgument::Expression: { |
4979 | |
4980 | |
4981 | CXXScopeSpec SS; |
4982 | DeclarationNameInfo NameInfo; |
4983 | |
4984 | if (DependentScopeDeclRefExpr *ArgExpr = |
4985 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
4986 | SS.Adopt(ArgExpr->getQualifierLoc()); |
4987 | NameInfo = ArgExpr->getNameInfo(); |
4988 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
4989 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
4990 | if (ArgExpr->isImplicitAccess()) { |
4991 | SS.Adopt(ArgExpr->getQualifierLoc()); |
4992 | NameInfo = ArgExpr->getMemberNameInfo(); |
4993 | } |
4994 | } |
4995 | |
4996 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
4997 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
4998 | LookupParsedName(Result, CurScope, &SS); |
4999 | |
5000 | if (Result.getAsSingle<TypeDecl>() || |
5001 | Result.getResultKind() == |
5002 | LookupResult::NotFoundInCurrentInstantiation) { |
5003 | assert(SS.getScopeRep() && "dependent scope expr must has a scope!"); |
5004 | |
5005 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
5006 | Diag(Loc, getLangOpts().MSVCCompat |
5007 | ? diag::ext_ms_template_type_arg_missing_typename |
5008 | : diag::err_template_arg_must_be_type_suggest) |
5009 | << FixItHint::CreateInsertion(Loc, "typename "); |
5010 | Diag(Param->getLocation(), diag::note_template_param_here); |
5011 | |
5012 | |
5013 | |
5014 | ArgType = |
5015 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
5016 | TypeLocBuilder TLB; |
5017 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
5018 | TL.setElaboratedKeywordLoc(SourceLocation()); |
5019 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
5020 | TL.setNameLoc(NameInfo.getLoc()); |
5021 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
5022 | |
5023 | |
5024 | |
5025 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
5026 | TemplateArgumentLocInfo(TSI)); |
5027 | |
5028 | break; |
5029 | } |
5030 | } |
5031 | |
5032 | LLVM_FALLTHROUGH; |
5033 | } |
5034 | default: { |
5035 | |
5036 | |
5037 | SourceRange SR = AL.getSourceRange(); |
5038 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
5039 | Diag(Param->getLocation(), diag::note_template_param_here); |
5040 | |
5041 | return true; |
5042 | } |
5043 | } |
5044 | |
5045 | if (CheckTemplateArgument(TSI)) |
5046 | return true; |
5047 | |
5048 | |
5049 | ArgType = Context.getCanonicalType(ArgType); |
5050 | |
5051 | |
5052 | |
5053 | |
5054 | if (getLangOpts().ObjCAutoRefCount && |
5055 | ArgType->isObjCLifetimeType() && |
5056 | !ArgType.getObjCLifetime()) { |
5057 | Qualifiers Qs; |
5058 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
5059 | ArgType = Context.getQualifiedType(ArgType, Qs); |
5060 | } |
5061 | |
5062 | Converted.push_back(TemplateArgument(ArgType)); |
5063 | return false; |
5064 | } |
5065 | |
5066 | |
5067 | |
5068 | |
5069 | |
5070 | |
5071 | |
5072 | |
5073 | |
5074 | |
5075 | |
5076 | |
5077 | |
5078 | |
5079 | |
5080 | |
5081 | |
5082 | |
5083 | |
5084 | |
5085 | |
5086 | |
5087 | static TypeSourceInfo * |
5088 | SubstDefaultTemplateArgument(Sema &SemaRef, |
5089 | TemplateDecl *Template, |
5090 | SourceLocation TemplateLoc, |
5091 | SourceLocation RAngleLoc, |
5092 | TemplateTypeParmDecl *Param, |
5093 | SmallVectorImpl<TemplateArgument> &Converted) { |
5094 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
5095 | |
5096 | |
5097 | |
5098 | if (ArgType->getType()->isInstantiationDependentType()) { |
5099 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
5100 | Param, Template, Converted, |
5101 | SourceRange(TemplateLoc, RAngleLoc)); |
5102 | if (Inst.isInvalid()) |
5103 | return nullptr; |
5104 | |
5105 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
5106 | |
5107 | |
5108 | MultiLevelTemplateArgumentList TemplateArgLists; |
5109 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
5110 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
5111 | TemplateArgLists.addOuterTemplateArguments(None); |
5112 | |
5113 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
5114 | ArgType = |
5115 | SemaRef.SubstType(ArgType, TemplateArgLists, |
5116 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
5117 | } |
5118 | |
5119 | return ArgType; |
5120 | } |
5121 | |
5122 | |
5123 | |
5124 | |
5125 | |
5126 | |
5127 | |
5128 | |
5129 | |
5130 | |
5131 | |
5132 | |
5133 | |
5134 | |
5135 | |
5136 | |
5137 | |
5138 | |
5139 | |
5140 | |
5141 | |
5142 | |
5143 | |
5144 | static ExprResult |
5145 | SubstDefaultTemplateArgument(Sema &SemaRef, |
5146 | TemplateDecl *Template, |
5147 | SourceLocation TemplateLoc, |
5148 | SourceLocation RAngleLoc, |
5149 | NonTypeTemplateParmDecl *Param, |
5150 | SmallVectorImpl<TemplateArgument> &Converted) { |
5151 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
5152 | Param, Template, Converted, |
5153 | SourceRange(TemplateLoc, RAngleLoc)); |
5154 | if (Inst.isInvalid()) |
5155 | return ExprError(); |
5156 | |
5157 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
5158 | |
5159 | |
5160 | MultiLevelTemplateArgumentList TemplateArgLists; |
5161 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
5162 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
5163 | TemplateArgLists.addOuterTemplateArguments(None); |
5164 | |
5165 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
5166 | EnterExpressionEvaluationContext ConstantEvaluated( |
5167 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
5168 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
5169 | } |
5170 | |
5171 | |
5172 | |
5173 | |
5174 | |
5175 | |
5176 | |
5177 | |
5178 | |
5179 | |
5180 | |
5181 | |
5182 | |
5183 | |
5184 | |
5185 | |
5186 | |
5187 | |
5188 | |
5189 | |
5190 | |
5191 | |
5192 | |
5193 | |
5194 | |
5195 | |
5196 | static TemplateName |
5197 | SubstDefaultTemplateArgument(Sema &SemaRef, |
5198 | TemplateDecl *Template, |
5199 | SourceLocation TemplateLoc, |
5200 | SourceLocation RAngleLoc, |
5201 | TemplateTemplateParmDecl *Param, |
5202 | SmallVectorImpl<TemplateArgument> &Converted, |
5203 | NestedNameSpecifierLoc &QualifierLoc) { |
5204 | Sema::InstantiatingTemplate Inst( |
5205 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
5206 | SourceRange(TemplateLoc, RAngleLoc)); |
5207 | if (Inst.isInvalid()) |
5208 | return TemplateName(); |
5209 | |
5210 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
5211 | |
5212 | |
5213 | MultiLevelTemplateArgumentList TemplateArgLists; |
5214 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
5215 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
5216 | TemplateArgLists.addOuterTemplateArguments(None); |
5217 | |
5218 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
5219 | |
5220 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
5221 | if (QualifierLoc) { |
5222 | QualifierLoc = |
5223 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
5224 | if (!QualifierLoc) |
5225 | return TemplateName(); |
5226 | } |
5227 | |
5228 | return SemaRef.SubstTemplateName( |
5229 | QualifierLoc, |
5230 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
5231 | Param->getDefaultArgument().getTemplateNameLoc(), |
5232 | TemplateArgLists); |
5233 | } |
5234 | |
5235 | |
5236 | |
5237 | |
5238 | TemplateArgumentLoc |
5239 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
5240 | SourceLocation TemplateLoc, |
5241 | SourceLocation RAngleLoc, |
5242 | Decl *Param, |
5243 | SmallVectorImpl<TemplateArgument> |
5244 | &Converted, |
5245 | bool &HasDefaultArg) { |
5246 | HasDefaultArg = false; |
5247 | |
5248 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
5249 | if (!hasVisibleDefaultArgument(TypeParm)) |
5250 | return TemplateArgumentLoc(); |
5251 | |
5252 | HasDefaultArg = true; |
5253 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
5254 | TemplateLoc, |
5255 | RAngleLoc, |
5256 | TypeParm, |
5257 | Converted); |
5258 | if (DI) |
5259 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
5260 | |
5261 | return TemplateArgumentLoc(); |
5262 | } |
5263 | |
5264 | if (NonTypeTemplateParmDecl *NonTypeParm |
5265 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
5266 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
5267 | return TemplateArgumentLoc(); |
5268 | |
5269 | HasDefaultArg = true; |
5270 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
5271 | TemplateLoc, |
5272 | RAngleLoc, |
5273 | NonTypeParm, |
5274 | Converted); |
5275 | if (Arg.isInvalid()) |
5276 | return TemplateArgumentLoc(); |
5277 | |
5278 | Expr *ArgE = Arg.getAs<Expr>(); |
5279 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
5280 | } |
5281 | |
5282 | TemplateTemplateParmDecl *TempTempParm |
5283 | = cast<TemplateTemplateParmDecl>(Param); |
5284 | if (!hasVisibleDefaultArgument(TempTempParm)) |
5285 | return TemplateArgumentLoc(); |
5286 | |
5287 | HasDefaultArg = true; |
5288 | NestedNameSpecifierLoc QualifierLoc; |
5289 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
5290 | TemplateLoc, |
5291 | RAngleLoc, |
5292 | TempTempParm, |
5293 | Converted, |
5294 | QualifierLoc); |
5295 | if (TName.isNull()) |
5296 | return TemplateArgumentLoc(); |
5297 | |
5298 | return TemplateArgumentLoc( |
5299 | Context, TemplateArgument(TName), |
5300 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
5301 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
5302 | } |
5303 | |
5304 | |
5305 | |
5306 | |
5307 | static TemplateArgumentLoc |
5308 | convertTypeTemplateArgumentToTemplate(ASTContext &Context, TypeLoc TLoc) { |
5309 | |
5310 | NestedNameSpecifierLoc QualLoc; |
5311 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
5312 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
5313 | return TemplateArgumentLoc(); |
5314 | |
5315 | QualLoc = ETLoc.getQualifierLoc(); |
5316 | TLoc = ETLoc.getNamedTypeLoc(); |
5317 | } |
5318 | |
5319 | |
5320 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
5321 | return TemplateArgumentLoc(Context, InjLoc.getTypePtr()->getTemplateName(), |
5322 | QualLoc, InjLoc.getNameLoc()); |
5323 | |
5324 | |
5325 | |
5326 | |
5327 | |
5328 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
5329 | if (auto *CTSD = |
5330 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
5331 | return TemplateArgumentLoc(Context, |
5332 | TemplateName(CTSD->getSpecializedTemplate()), |
5333 | QualLoc, RecLoc.getNameLoc()); |
5334 | |
5335 | return TemplateArgumentLoc(); |
5336 | } |
5337 | |
5338 | |
5339 | |
5340 | |
5341 | |
5342 | |
5343 | |
5344 | |
5345 | |
5346 | |
5347 | |
5348 | |
5349 | |
5350 | |
5351 | |
5352 | |
5353 | |
5354 | |
5355 | |
5356 | |
5357 | |
5358 | |
5359 | |
5360 | |
5361 | |
5362 | |
5363 | |
5364 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
5365 | TemplateArgumentLoc &Arg, |
5366 | NamedDecl *Template, |
5367 | SourceLocation TemplateLoc, |
5368 | SourceLocation RAngleLoc, |
5369 | unsigned ArgumentPackIndex, |
5370 | SmallVectorImpl<TemplateArgument> &Converted, |
5371 | CheckTemplateArgumentKind CTAK) { |
5372 | |
5373 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
5374 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
5375 | |
5376 | |
5377 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
5378 | |
5379 | |
5380 | |
5381 | QualType NTTPType = NTTP->getType(); |
5382 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
5383 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
5384 | |
5385 | if (NTTPType->isInstantiationDependentType() && |
5386 | !isa<TemplateTemplateParmDecl>(Template) && |
5387 | !Template->getDeclContext()->isDependentContext()) { |
5388 | |
5389 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
5390 | NTTP, Converted, |
5391 | SourceRange(TemplateLoc, RAngleLoc)); |
5392 | if (Inst.isInvalid()) |
5393 | return true; |
5394 | |
5395 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
5396 | Converted); |
5397 | |
5398 | |
5399 | if (auto *PET = NTTPType->getAs<PackExpansionType>()) { |
5400 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, |
5401 | ArgumentPackIndex); |
5402 | NTTPType = SubstType(PET->getPattern(), |
5403 | MultiLevelTemplateArgumentList(TemplateArgs), |
5404 | NTTP->getLocation(), |
5405 | NTTP->getDeclName()); |
5406 | } else { |
5407 | NTTPType = SubstType(NTTPType, |
5408 | MultiLevelTemplateArgumentList(TemplateArgs), |
5409 | NTTP->getLocation(), |
5410 | NTTP->getDeclName()); |
5411 | } |
5412 | |
5413 | |
5414 | |
5415 | if (!NTTPType.isNull()) |
5416 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
5417 | NTTP->getLocation()); |
5418 | if (NTTPType.isNull()) |
5419 | return true; |
5420 | } |
5421 | |
5422 | switch (Arg.getArgument().getKind()) { |
5423 | case TemplateArgument::Null: |
5424 | llvm_unreachable("Should never see a NULL template argument here"); |
5425 | |
5426 | case TemplateArgument::Expression: { |
5427 | TemplateArgument Result; |
5428 | unsigned CurSFINAEErrors = NumSFINAEErrors; |
5429 | ExprResult Res = |
5430 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
5431 | Result, CTAK); |
5432 | if (Res.isInvalid()) |
5433 | return true; |
5434 | |
5435 | if (CurSFINAEErrors < NumSFINAEErrors) |
5436 | return true; |
5437 | |
5438 | |
5439 | |
5440 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
5441 | TemplateArgument TA(Res.get()); |
5442 | Arg = TemplateArgumentLoc(TA, Res.get()); |
5443 | } |
5444 | |
5445 | Converted.push_back(Result); |
5446 | break; |
5447 | } |
5448 | |
5449 | case TemplateArgument::Declaration: |
5450 | case TemplateArgument::Integral: |
5451 | case TemplateArgument::NullPtr: |
5452 | |
5453 | |
5454 | Converted.push_back(Arg.getArgument()); |
5455 | break; |
5456 | |
5457 | case TemplateArgument::Template: |
5458 | case TemplateArgument::TemplateExpansion: |
5459 | |
5460 | |
5461 | if (DependentTemplateName *DTN |
5462 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
5463 | .getAsDependentTemplateName()) { |
5464 | |
5465 | |
5466 | |
5467 | |
5468 | |
5469 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
5470 | Arg.getTemplateNameLoc()); |
5471 | |
5472 | CXXScopeSpec SS; |
5473 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
5474 | |
5475 | |
5476 | |
5477 | SourceLocation TemplateKWLoc; |
5478 | ExprResult E = DependentScopeDeclRefExpr::Create( |
5479 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
5480 | nullptr); |
5481 | |
5482 | |
5483 | |
5484 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
5485 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
5486 | if (E.isInvalid()) |
5487 | return true; |
5488 | } |
5489 | |
5490 | TemplateArgument Result; |
5491 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
5492 | if (E.isInvalid()) |
5493 | return true; |
5494 | |
5495 | Converted.push_back(Result); |
5496 | break; |
5497 | } |
5498 | |
5499 | |
5500 | |
5501 | |
5502 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
5503 | << Arg.getSourceRange(); |
5504 | |
5505 | Diag(Param->getLocation(), diag::note_template_param_here); |
5506 | return true; |
5507 | |
5508 | case TemplateArgument::Type: { |
5509 | |
5510 | |
5511 | |
5512 | |
5513 | |
5514 | |
5515 | |
5516 | |
5517 | |
5518 | |
5519 | QualType T = Arg.getArgument().getAsType(); |
5520 | SourceRange SR = Arg.getSourceRange(); |
5521 | if (T->isFunctionType()) |
5522 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
5523 | else |
5524 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
5525 | Diag(Param->getLocation(), diag::note_template_param_here); |
5526 | return true; |
5527 | } |
5528 | |
5529 | case TemplateArgument::Pack: |
5530 | llvm_unreachable("Caller must expand template argument packs"); |
5531 | } |
5532 | |
5533 | return false; |
5534 | } |
5535 | |
5536 | |
5537 | |
5538 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
5539 | |
5540 | TemplateParameterList *Params = TempParm->getTemplateParameters(); |
5541 | if (TempParm->isExpandedParameterPack()) |
5542 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); |
5543 | |
5544 | |
5545 | |
5546 | |
5547 | |
5548 | |
5549 | { |
5550 | |
5551 | LocalInstantiationScope Scope(*this); |
5552 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
5553 | TempParm, Converted, |
5554 | SourceRange(TemplateLoc, RAngleLoc)); |
5555 | if (Inst.isInvalid()) |
5556 | return true; |
5557 | |
5558 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
5559 | Params = SubstTemplateParams(Params, CurContext, |
5560 | MultiLevelTemplateArgumentList(TemplateArgs)); |
5561 | if (!Params) |
5562 | return true; |
5563 | } |
5564 | |
5565 | |
5566 | |
5567 | |
5568 | |
5569 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
5570 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
5571 | Context, Arg.getTypeSourceInfo()->getTypeLoc()); |
5572 | if (!ConvertedArg.getArgument().isNull()) |
5573 | Arg = ConvertedArg; |
5574 | } |
5575 | |
5576 | switch (Arg.getArgument().getKind()) { |
5577 | case TemplateArgument::Null: |
5578 | llvm_unreachable("Should never see a NULL template argument here"); |
5579 | |
5580 | case TemplateArgument::Template: |
5581 | case TemplateArgument::TemplateExpansion: |
5582 | if (CheckTemplateTemplateArgument(TempParm, Params, Arg)) |
5583 | return true; |
5584 | |
5585 | Converted.push_back(Arg.getArgument()); |
5586 | break; |
5587 | |
5588 | case TemplateArgument::Expression: |
5589 | case TemplateArgument::Type: |
5590 | |
5591 | |
5592 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
5593 | << getLangOpts().CPlusPlus11; |
5594 | return true; |
5595 | |
5596 | case TemplateArgument::Declaration: |
5597 | llvm_unreachable("Declaration argument with template template parameter"); |
5598 | case TemplateArgument::Integral: |
5599 | llvm_unreachable("Integral argument with template template parameter"); |
5600 | case TemplateArgument::NullPtr: |
5601 | llvm_unreachable("Null pointer argument with template template parameter"); |
5602 | |
5603 | case TemplateArgument::Pack: |
5604 | llvm_unreachable("Caller must expand template argument packs"); |
5605 | } |
5606 | |
5607 | return false; |
5608 | } |
5609 | |
5610 | |
5611 | template<typename TemplateParmDecl> |
5612 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
5613 | TemplateDecl *TD, |
5614 | const TemplateParmDecl *D, |
5615 | TemplateArgumentListInfo &Args) { |
5616 | |
5617 | |
5618 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
5619 | ->getTemplateParameters() |
5620 | ->getParam(D->getIndex())); |
5621 | |
5622 | |
5623 | |
5624 | llvm::SmallVector<Module*, 8> Modules; |
5625 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
5626 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
5627 | D->getDefaultArgumentLoc(), Modules, |
5628 | Sema::MissingImportKind::DefaultArgument, |
5629 | true); |
5630 | return true; |
5631 | } |
5632 | |
5633 | |
5634 | |
5635 | |
5636 | TemplateParameterList *Params = TD->getTemplateParameters(); |
5637 | |
5638 | S.Diag(Loc, diag::err_template_arg_list_different_arity) |
5639 | << 0 |
5640 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) |
5641 | << TD; |
5642 | S.Diag(TD->getLocation(), diag::note_template_decl_here) |
5643 | << Params->getSourceRange(); |
5644 | return true; |
5645 | } |
5646 | |
5647 | |
5648 | |
5649 | bool Sema::CheckTemplateArgumentList( |
5650 | TemplateDecl *Template, SourceLocation TemplateLoc, |
5651 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
5652 | SmallVectorImpl<TemplateArgument> &Converted, |
5653 | bool UpdateArgsWithConversions, bool *ConstraintsNotSatisfied) { |
5654 | |
5655 | if (ConstraintsNotSatisfied) |
5656 | *ConstraintsNotSatisfied = false; |
5657 | |
5658 | |
5659 | |
5660 | |
5661 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
5662 | |
5663 | |
5664 | |
5665 | |
5666 | TemplateParameterList *Params = |
5667 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
5668 | ->getTemplateParameters(); |
5669 | |
5670 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
5671 | |
5672 | |
5673 | |
5674 | |
5675 | |
5676 | |
5677 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
5678 | SmallVector<TemplateArgument, 2> ArgumentPack; |
5679 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
5680 | LocalInstantiationScope InstScope(*this, true); |
5681 | for (TemplateParameterList::iterator Param = Params->begin(), |
5682 | ParamEnd = Params->end(); |
5683 | Param != ParamEnd; ) { |
5684 | |
5685 | |
5686 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
5687 | if (*Expansions == ArgumentPack.size()) { |
5688 | |
5689 | |
5690 | Converted.push_back( |
5691 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
5692 | ArgumentPack.clear(); |
5693 | |
5694 | |
5695 | ++Param; |
5696 | continue; |
5697 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
5698 | |
5699 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
5700 | << 0 |
5701 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
5702 | << Template; |
5703 | Diag(Template->getLocation(), diag::note_template_decl_here) |
5704 | << Params->getSourceRange(); |
5705 | return true; |
5706 | } |
5707 | } |
5708 | |
5709 | if (ArgIdx < NumArgs) { |
5710 | |
5711 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
5712 | TemplateLoc, RAngleLoc, |
5713 | ArgumentPack.size(), Converted)) |
5714 | return true; |
5715 | |
5716 | bool PackExpansionIntoNonPack = |
5717 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
5718 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
5719 | if (PackExpansionIntoNonPack && (isa<TypeAliasTemplateDecl>(Template) || |
5720 | isa<ConceptDecl>(Template))) { |
5721 | |
5722 | |
5723 | |
5724 | |
5725 | |
5726 | Diag(NewArgs[ArgIdx].getLocation(), |
5727 | diag::err_template_expansion_into_fixed_list) |
5728 | << (isa<ConceptDecl>(Template) ? 1 : 0) |
5729 | << NewArgs[ArgIdx].getSourceRange(); |
5730 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
5731 | return true; |
5732 | } |
5733 | |
5734 | |
5735 | ++ArgIdx; |
5736 | |
5737 | if ((*Param)->isTemplateParameterPack()) { |
5738 | |
5739 | |
5740 | |
5741 | |
5742 | ArgumentPack.push_back(Converted.pop_back_val()); |
5743 | } else { |
5744 | |
5745 | ++Param; |
5746 | } |
5747 | |
5748 | |
5749 | |
5750 | |
5751 | if (PackExpansionIntoNonPack) { |
5752 | if (!ArgumentPack.empty()) { |
5753 | |
5754 | |
5755 | Converted.insert(Converted.end(), |
5756 | ArgumentPack.begin(), ArgumentPack.end()); |
5757 | ArgumentPack.clear(); |
5758 | } |
5759 | |
5760 | while (ArgIdx < NumArgs) { |
5761 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
5762 | ++ArgIdx; |
5763 | } |
5764 | |
5765 | return false; |
5766 | } |
5767 | |
5768 | continue; |
5769 | } |
5770 | |
5771 | |
5772 | if (PartialTemplateArgs) { |
5773 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
5774 | Converted.push_back( |
5775 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
5776 | return false; |
5777 | } |
5778 | |
5779 | |
5780 | |
5781 | if ((*Param)->isTemplateParameterPack()) { |
5782 | assert(!getExpandedPackSize(*Param) && |
5783 | "Should have dealt with this already"); |
5784 | |
5785 | |
5786 | |
5787 | |
5788 | if (Param + 1 != ParamEnd) |
5789 | return true; |
5790 | |
5791 | Converted.push_back( |
5792 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
5793 | ArgumentPack.clear(); |
5794 | |
5795 | ++Param; |
5796 | continue; |
5797 | } |
5798 | |
5799 | |
5800 | TemplateArgumentLoc Arg; |
5801 | |
5802 | |
5803 | |
5804 | |
5805 | |
5806 | |
5807 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
5808 | if (!hasVisibleDefaultArgument(TTP)) |
5809 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
5810 | NewArgs); |
5811 | |
5812 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
5813 | Template, |
5814 | TemplateLoc, |
5815 | RAngleLoc, |
5816 | TTP, |
5817 | Converted); |
5818 | if (!ArgType) |
5819 | return true; |
5820 | |
5821 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
5822 | ArgType); |
5823 | } else if (NonTypeTemplateParmDecl *NTTP |
5824 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
5825 | if (!hasVisibleDefaultArgument(NTTP)) |
5826 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
5827 | NewArgs); |
5828 | |
5829 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
5830 | TemplateLoc, |
5831 | RAngleLoc, |
5832 | NTTP, |
5833 | Converted); |
5834 | if (E.isInvalid()) |
5835 | return true; |
5836 | |
5837 | Expr *Ex = E.getAs<Expr>(); |
5838 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
5839 | } else { |
5840 | TemplateTemplateParmDecl *TempParm |
5841 | = cast<TemplateTemplateParmDecl>(*Param); |
5842 | |
5843 | if (!hasVisibleDefaultArgument(TempParm)) |
5844 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
5845 | NewArgs); |
5846 | |
5847 | NestedNameSpecifierLoc QualifierLoc; |
5848 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
5849 | TemplateLoc, |
5850 | RAngleLoc, |
5851 | TempParm, |
5852 | Converted, |
5853 | QualifierLoc); |
5854 | if (Name.isNull()) |
5855 | return true; |
5856 | |
5857 | Arg = TemplateArgumentLoc( |
5858 | Context, TemplateArgument(Name), QualifierLoc, |
5859 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
5860 | } |
5861 | |
5862 | |
5863 | |
5864 | |
5865 | |
5866 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
5867 | SourceRange(TemplateLoc, RAngleLoc)); |
5868 | if (Inst.isInvalid()) |
5869 | return true; |
5870 | |
5871 | |
5872 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
5873 | RAngleLoc, 0, Converted)) |
5874 | return true; |
5875 | |
5876 | |
5877 | |
5878 | |
5879 | if (isTemplateTemplateParameter) |
5880 | NewArgs.addArgument(Arg); |
5881 | |
5882 | |
5883 | ++Param; |
5884 | ++ArgIdx; |
5885 | } |
5886 | |
5887 | |
5888 | |
5889 | |
5890 | |
5891 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
5892 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
5893 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
5894 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
5895 | } |
5896 | |
5897 | |
5898 | |
5899 | if (ArgIdx < NumArgs) { |
5900 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
5901 | << 1 |
5902 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
5903 | << Template |
5904 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); |
5905 | Diag(Template->getLocation(), diag::note_template_decl_here) |
5906 | << Params->getSourceRange(); |
5907 | return true; |
5908 | } |
5909 | |
5910 | |
5911 | |
5912 | if (UpdateArgsWithConversions) |
5913 | TemplateArgs = std::move(NewArgs); |
5914 | |
5915 | if (!PartialTemplateArgs && |
5916 | EnsureTemplateArgumentListConstraints( |
5917 | Template, Converted, SourceRange(TemplateLoc, |
5918 | TemplateArgs.getRAngleLoc()))) { |
5919 | if (ConstraintsNotSatisfied) |
5920 | *ConstraintsNotSatisfied = true; |
5921 | return true; |
5922 | } |
5923 | |
5924 | return false; |
5925 | } |
5926 | |
5927 | namespace { |
5928 | class UnnamedLocalNoLinkageFinder |
5929 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
5930 | { |
5931 | Sema &S; |
5932 | SourceRange SR; |
5933 | |
5934 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
5935 | |
5936 | public: |
5937 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
5938 | |
5939 | bool Visit(QualType T) { |
5940 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
5941 | } |
5942 | |
5943 | #define TYPE(Class, Parent) \ |
5944 | bool Visit##Class##Type(const Class##Type *); |
5945 | #define ABSTRACT_TYPE(Class, Parent) \ |
5946 | bool Visit##Class##Type(const Class##Type *) { return false; } |
5947 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
5948 | bool Visit##Class##Type(const Class##Type *) { return false; } |
5949 | #include "clang/AST/TypeNodes.inc" |
5950 | |
5951 | bool VisitTagDecl(const TagDecl *Tag); |
5952 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
5953 | }; |
5954 | } |
5955 | |
5956 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
5957 | return false; |
5958 | } |
5959 | |
5960 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
5961 | return Visit(T->getElementType()); |
5962 | } |
5963 | |
5964 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
5965 | return Visit(T->getPointeeType()); |
5966 | } |
5967 | |
5968 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
5969 | const BlockPointerType* T) { |
5970 | return Visit(T->getPointeeType()); |
5971 | } |
5972 | |
5973 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
5974 | const LValueReferenceType* T) { |
5975 | return Visit(T->getPointeeType()); |
5976 | } |
5977 | |
5978 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
5979 | const RValueReferenceType* T) { |
5980 | return Visit(T->getPointeeType()); |
5981 | } |
5982 | |
5983 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
5984 | const MemberPointerType* T) { |
5985 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
5986 | } |
5987 | |
5988 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
5989 | const ConstantArrayType* T) { |
5990 | return Visit(T->getElementType()); |
5991 | } |
5992 | |
5993 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
5994 | const IncompleteArrayType* T) { |
5995 | return Visit(T->getElementType()); |
5996 | } |
5997 | |
5998 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
5999 | const VariableArrayType* T) { |
6000 | return Visit(T->getElementType()); |
6001 | } |
6002 | |
6003 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
6004 | const DependentSizedArrayType* T) { |
6005 | return Visit(T->getElementType()); |
6006 | } |
6007 | |
6008 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
6009 | const DependentSizedExtVectorType* T) { |
6010 | return Visit(T->getElementType()); |
6011 | } |
6012 | |
6013 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedMatrixType( |
6014 | const DependentSizedMatrixType *T) { |
6015 | return Visit(T->getElementType()); |
6016 | } |
6017 | |
6018 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
6019 | const DependentAddressSpaceType *T) { |
6020 | return Visit(T->getPointeeType()); |
6021 | } |
6022 | |
6023 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
6024 | return Visit(T->getElementType()); |
6025 | } |
6026 | |
6027 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( |
6028 | const DependentVectorType *T) { |
6029 | return Visit(T->getElementType()); |
6030 | } |
6031 | |
6032 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
6033 | return Visit(T->getElementType()); |
6034 | } |
6035 | |
6036 | bool UnnamedLocalNoLinkageFinder::VisitConstantMatrixType( |
6037 | const ConstantMatrixType *T) { |
6038 | return Visit(T->getElementType()); |
6039 | } |
6040 | |
6041 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
6042 | const FunctionProtoType* T) { |
6043 | for (const auto &A : T->param_types()) { |
6044 | if (Visit(A)) |
6045 | return true; |
6046 | } |
6047 | |
6048 | return Visit(T->getReturnType()); |
6049 | } |
6050 | |
6051 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
6052 | const FunctionNoProtoType* T) { |
6053 | return Visit(T->getReturnType()); |
6054 | } |
6055 | |
6056 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
6057 | const UnresolvedUsingType*) { |
6058 | return false; |
6059 | } |
6060 | |
6061 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
6062 | return false; |
6063 | } |
6064 | |
6065 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
6066 | return Visit(T->getUnderlyingType()); |
6067 | } |
6068 | |
6069 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
6070 | return false; |
6071 | } |
6072 | |
6073 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
6074 | const UnaryTransformType*) { |
6075 | return false; |
6076 | } |
6077 | |
6078 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
6079 | return Visit(T->getDeducedType()); |
6080 | } |
6081 | |
6082 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
6083 | const DeducedTemplateSpecializationType *T) { |
6084 | return Visit(T->getDeducedType()); |
6085 | } |
6086 | |
6087 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
6088 | return VisitTagDecl(T->getDecl()); |
6089 | } |
6090 | |
6091 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
6092 | return VisitTagDecl(T->getDecl()); |
6093 | } |
6094 | |
6095 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
6096 | const TemplateTypeParmType*) { |
6097 | return false; |
6098 | } |
6099 | |
6100 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
6101 | const SubstTemplateTypeParmPackType *) { |
6102 | return false; |
6103 | } |
6104 | |
6105 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
6106 | const TemplateSpecializationType*) { |
6107 | return false; |
6108 | } |
6109 | |
6110 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
6111 | const InjectedClassNameType* T) { |
6112 | return VisitTagDecl(T->getDecl()); |
6113 | } |
6114 | |
6115 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
6116 | const DependentNameType* T) { |
6117 | return VisitNestedNameSpecifier(T->getQualifier()); |
6118 | } |
6119 | |
6120 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
6121 | const DependentTemplateSpecializationType* T) { |
6122 | if (auto *Q = T->getQualifier()) |
6123 | return VisitNestedNameSpecifier(Q); |
6124 | return false; |
6125 | } |
6126 | |
6127 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
6128 | const PackExpansionType* T) { |
6129 | return Visit(T->getPattern()); |
6130 | } |
6131 | |
6132 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
6133 | return false; |
6134 | } |
6135 | |
6136 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
6137 | const ObjCInterfaceType *) { |
6138 | return false; |
6139 | } |
6140 | |
6141 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
6142 | const ObjCObjectPointerType *) { |
6143 | return false; |
6144 | } |
6145 | |
6146 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
6147 | return Visit(T->getValueType()); |
6148 | } |
6149 | |
6150 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
6151 | return false; |
6152 | } |
6153 | |
6154 | bool UnnamedLocalNoLinkageFinder::VisitExtIntType(const ExtIntType *T) { |
6155 | return false; |
6156 | } |
6157 | |
6158 | bool UnnamedLocalNoLinkageFinder::VisitDependentExtIntType( |
6159 | const DependentExtIntType *T) { |
6160 | return false; |
6161 | } |
6162 | |
6163 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
6164 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
6165 | S.Diag(SR.getBegin(), |
6166 | S.getLangOpts().CPlusPlus11 ? |
6167 | diag::warn_cxx98_compat_template_arg_local_type : |
6168 | diag::ext_template_arg_local_type) |
6169 | << S.Context.getTypeDeclType(Tag) << SR; |
6170 | return true; |
6171 | } |
6172 | |
6173 | if (!Tag->hasNameForLinkage()) { |
6174 | S.Diag(SR.getBegin(), |
6175 | S.getLangOpts().CPlusPlus11 ? |
6176 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
6177 | diag::ext_template_arg_unnamed_type) << SR; |
6178 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
6179 | return true; |
6180 | } |
6181 | |
6182 | return false; |
6183 | } |
6184 | |
6185 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
6186 | NestedNameSpecifier *NNS) { |
6187 | assert(NNS); |
6188 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
6189 | return true; |
6190 | |
6191 | switch (NNS->getKind()) { |
6192 | case NestedNameSpecifier::Identifier: |
6193 | case NestedNameSpecifier::Namespace: |
6194 | case NestedNameSpecifier::NamespaceAlias: |
6195 | case NestedNameSpecifier::Global: |
6196 | case NestedNameSpecifier::Super: |
6197 | return false; |
6198 | |
6199 | case NestedNameSpecifier::TypeSpec: |
6200 | case NestedNameSpecifier::TypeSpecWithTemplate: |
6201 | return Visit(QualType(NNS->getAsType(), 0)); |
6202 | } |
6203 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
6204 | } |
6205 | |
6206 | |
6207 | |
6208 | |
6209 | |
6210 | |
6211 | bool Sema::CheckTemplateArgument(TypeSourceInfo *ArgInfo) { |
6212 | assert(ArgInfo && "invalid TypeSourceInfo"); |
6213 | QualType Arg = ArgInfo->getType(); |
6214 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
6215 | |
6216 | if (Arg->isVariablyModifiedType()) { |
6217 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
6218 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
6219 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
6220 | } |
6221 | |
6222 | |
6223 | |
6224 | |
6225 | |
6226 | |
6227 | |
6228 | |
6229 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
6230 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
6231 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
6232 | } |
6233 | |
6234 | return false; |
6235 | } |
6236 | |
6237 | enum NullPointerValueKind { |
6238 | NPV_NotNullPointer, |
6239 | NPV_NullPointer, |
6240 | NPV_Error |
6241 | }; |
6242 | |
6243 | |
6244 | |
6245 | static NullPointerValueKind |
6246 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
6247 | QualType ParamType, Expr *Arg, |
6248 | Decl *Entity = nullptr) { |
6249 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
6250 | return NPV_NotNullPointer; |
6251 | |
6252 | |
6253 | |
6254 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
6255 | return NPV_NotNullPointer; |
6256 | |
6257 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
6258 | llvm_unreachable( |
6259 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
6260 | |
6261 | if (!S.getLangOpts().CPlusPlus11) |
6262 | return NPV_NotNullPointer; |
6263 | |
6264 | |
6265 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
6266 | if (ArgRV.isInvalid()) |
6267 | return NPV_Error; |
6268 | Arg = ArgRV.get(); |
6269 | |
6270 | Expr::EvalResult EvalResult; |
6271 | SmallVector<PartialDiagnosticAt, 8> Notes; |
6272 | EvalResult.Diag = &Notes; |
6273 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
6274 | EvalResult.HasSideEffects) { |
6275 | SourceLocation DiagLoc = Arg->getExprLoc(); |
6276 | |
6277 | |
6278 | |
6279 | |
6280 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
6281 | diag::note_invalid_subexpr_in_const_expr) { |
6282 | DiagLoc = Notes[0].first; |
6283 | Notes.clear(); |
6284 | } |
6285 | |
6286 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
6287 | << Arg->getType() << Arg->getSourceRange(); |
6288 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
6289 | S.Diag(Notes[I].first, Notes[I].second); |
6290 | |
6291 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6292 | return NPV_Error; |
6293 | } |
6294 | |
6295 | |
6296 | |
6297 | if (Arg->getType()->isNullPtrType()) |
6298 | return NPV_NullPointer; |
6299 | |
6300 | |
6301 | |
6302 | |
6303 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
6304 | (EvalResult.Val.isMemberPointer() && |
6305 | !EvalResult.Val.getMemberPointerDecl())) { |
6306 | |
6307 | bool ObjCLifetimeConversion; |
6308 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
6309 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
6310 | ObjCLifetimeConversion)) |
6311 | return NPV_NullPointer; |
6312 | |
6313 | |
6314 | |
6315 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
6316 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
6317 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6318 | return NPV_NullPointer; |
6319 | } |
6320 | |
6321 | |
6322 | |
6323 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
6324 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
6325 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
6326 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) |
6327 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), |
6328 | ")"); |
6329 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6330 | return NPV_NullPointer; |
6331 | } |
6332 | |
6333 | |
6334 | |
6335 | |
6336 | return NPV_NotNullPointer; |
6337 | } |
6338 | |
6339 | |
6340 | |
6341 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
6342 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
6343 | Expr *Arg, QualType ArgType) { |
6344 | bool ObjCLifetimeConversion; |
6345 | if (ParamType->isPointerType() && |
6346 | !ParamType->castAs<PointerType>()->getPointeeType()->isFunctionType() && |
6347 | S.IsQualificationConversion(ArgType, ParamType, false, |
6348 | ObjCLifetimeConversion)) { |
6349 | |
6350 | |
6351 | } else { |
6352 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
6353 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
6354 | |
6355 | |
6356 | |
6357 | |
6358 | |
6359 | |
6360 | |
6361 | |
6362 | |
6363 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
6364 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
6365 | |
6366 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
6367 | S.Diag(Arg->getBeginLoc(), |
6368 | diag::err_template_arg_ref_bind_ignores_quals) |
6369 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
6370 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6371 | return true; |
6372 | } |
6373 | } |
6374 | } |
6375 | |
6376 | |
6377 | |
6378 | |
6379 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
6380 | ParamType.getNonReferenceType())) { |
6381 | |
6382 | if (ParamType->isReferenceType()) |
6383 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) |
6384 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
6385 | else |
6386 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
6387 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
6388 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6389 | return true; |
6390 | } |
6391 | } |
6392 | |
6393 | return false; |
6394 | } |
6395 | |
6396 | |
6397 | |
6398 | static bool |
6399 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
6400 | NonTypeTemplateParmDecl *Param, |
6401 | QualType ParamType, |
6402 | Expr *ArgIn, |
6403 | TemplateArgument &Converted) { |
6404 | bool Invalid = false; |
6405 | Expr *Arg = ArgIn; |
6406 | QualType ArgType = Arg->getType(); |
6407 | |
6408 | bool AddressTaken = false; |
6409 | SourceLocation AddrOpLoc; |
6410 | if (S.getLangOpts().MicrosoftExt) { |
6411 | |
6412 | |
6413 | Arg = Arg->IgnoreParenCasts(); |
6414 | |
6415 | bool ExtWarnMSTemplateArg = false; |
6416 | UnaryOperatorKind FirstOpKind; |
6417 | SourceLocation FirstOpLoc; |
6418 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
6419 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
6420 | if (UnOpKind == UO_Deref) |
6421 | ExtWarnMSTemplateArg = true; |
6422 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
6423 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
6424 | if (!AddrOpLoc.isValid()) { |
6425 | FirstOpKind = UnOpKind; |
6426 | FirstOpLoc = UnOp->getOperatorLoc(); |
6427 | } |
6428 | } else |
6429 | break; |
6430 | } |
6431 | if (FirstOpLoc.isValid()) { |
6432 | if (ExtWarnMSTemplateArg) |
6433 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) |
6434 | << ArgIn->getSourceRange(); |
6435 | |
6436 | if (FirstOpKind == UO_AddrOf) |
6437 | AddressTaken = true; |
6438 | else if (Arg->getType()->isPointerType()) { |
6439 | |
6440 | |
6441 | assert(FirstOpKind == UO_Deref); |
6442 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
6443 | << Arg->getSourceRange(); |
6444 | } |
6445 | } |
6446 | } else { |
6447 | |
6448 | Arg = Arg->IgnoreImpCasts(); |
6449 | |
6450 | |
6451 | |
6452 | |
6453 | |
6454 | |
6455 | |
6456 | |
6457 | |
6458 | |
6459 | |
6460 | |
6461 | |
6462 | |
6463 | |
6464 | bool ExtraParens = false; |
6465 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
6466 | if (!Invalid && !ExtraParens) { |
6467 | S.Diag(Arg->getBeginLoc(), |
6468 | S.getLangOpts().CPlusPlus11 |
6469 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
6470 | : diag::ext_template_arg_extra_parens) |
6471 | << Arg->getSourceRange(); |
6472 | ExtraParens = true; |
6473 | } |
6474 | |
6475 | Arg = Parens->getSubExpr(); |
6476 | } |
6477 | |
6478 | while (SubstNonTypeTemplateParmExpr *subst = |
6479 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
6480 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
6481 | |
6482 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
6483 | if (UnOp->getOpcode() == UO_AddrOf) { |
6484 | Arg = UnOp->getSubExpr(); |
6485 | AddressTaken = true; |
6486 | AddrOpLoc = UnOp->getOperatorLoc(); |
6487 | } |
6488 | } |
6489 | |
6490 | while (SubstNonTypeTemplateParmExpr *subst = |
6491 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
6492 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
6493 | } |
6494 | |
6495 | ValueDecl *Entity = nullptr; |
6496 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg)) |
6497 | Entity = DRE->getDecl(); |
6498 | else if (CXXUuidofExpr *CUE = dyn_cast<CXXUuidofExpr>(Arg)) |
6499 | Entity = CUE->getGuidDecl(); |
6500 | |
6501 | |
6502 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
6503 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
6504 | Entity)) { |
6505 | case NPV_NullPointer: |
6506 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
6507 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
6508 | true); |
6509 | return false; |
6510 | |
6511 | case NPV_Error: |
6512 | return true; |
6513 | |
6514 | case NPV_NotNullPointer: |
6515 | break; |
6516 | } |
6517 | } |
6518 | |
6519 | |
6520 | |
6521 | if (Arg->isValueDependent()) { |
6522 | Converted = TemplateArgument(ArgIn); |
6523 | return false; |
6524 | } |
6525 | |
6526 | if (!Entity) { |
6527 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
6528 | << Arg->getSourceRange(); |
6529 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6530 | return true; |
6531 | } |
6532 | |
6533 | |
6534 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
6535 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) |
6536 | << Entity << Arg->getSourceRange(); |
6537 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6538 | return true; |
6539 | } |
6540 | |
6541 | |
6542 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
6543 | if (!Method->isStatic()) { |
6544 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) |
6545 | << Method << Arg->getSourceRange(); |
6546 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6547 | return true; |
6548 | } |
6549 | } |
6550 | |
6551 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
6552 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
6553 | MSGuidDecl *Guid = dyn_cast<MSGuidDecl>(Entity); |
6554 | |
6555 | |
6556 | if (!Func && !Var && !Guid) { |
6557 | |
6558 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) |
6559 | << Arg->getSourceRange(); |
6560 | S.Diag(Entity->getLocation(), diag::note_template_arg_refers_here); |
6561 | return true; |
6562 | } |
6563 | |
6564 | |
6565 | if (Entity->getFormalLinkage() == InternalLinkage) { |
6566 | S.Diag(Arg->getBeginLoc(), |
6567 | S.getLangOpts().CPlusPlus11 |
6568 | ? diag::warn_cxx98_compat_template_arg_object_internal |
6569 | : diag::ext_template_arg_object_internal) |
6570 | << !Func << Entity << Arg->getSourceRange(); |
6571 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
6572 | << !Func; |
6573 | } else if (!Entity->hasLinkage()) { |
6574 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) |
6575 | << !Func << Entity << Arg->getSourceRange(); |
6576 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
6577 | << !Func; |
6578 | return true; |
6579 | } |
6580 | |
6581 | if (Var) { |
6582 | |
6583 | if (Var->getType()->isReferenceType()) { |
6584 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) |
6585 | << Var->getType() << Arg->getSourceRange(); |
6586 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6587 | return true; |
6588 | } |
6589 | |
6590 | |
6591 | if (Var->getTLSKind()) { |
6592 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) |
6593 | << Arg->getSourceRange(); |
6594 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
6595 | return true; |
6596 | } |
6597 | } |
6598 | |
6599 | if (AddressTaken && ParamType->isReferenceType()) { |
6600 | |
6601 | |
6602 | |
6603 | if (!S.Context.hasSameUnqualifiedType(Entity->getType(), |
6604 | ParamType.getNonReferenceType())) { |
6605 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
6606 | << ParamType; |
6607 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6608 | return true; |
6609 | } |
6610 | |
6611 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
6612 | << ParamType |
6613 | << FixItHint::CreateRemoval(AddrOpLoc); |
6614 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6615 | |
6616 | ArgType = Entity->getType(); |
6617 | } |
6618 | |
6619 | |
6620 | |
6621 | if (!AddressTaken && ParamType->isPointerType()) { |
6622 | if (Func) { |
6623 | |
6624 | ArgType = S.Context.getPointerType(Func->getType()); |
6625 | } else if (Entity->getType()->isArrayType()) { |
6626 | |
6627 | ArgType = S.Context.getArrayDecayedType(Entity->getType()); |
6628 | } else { |
6629 | |
6630 | |
6631 | |
6632 | ArgType = S.Context.getPointerType(Entity->getType()); |
6633 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
6634 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
6635 | << ParamType; |
6636 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6637 | return true; |
6638 | } |
6639 | |
6640 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
6641 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); |
6642 | |
6643 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6644 | } |
6645 | } |
6646 | |
6647 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
6648 | Arg, ArgType)) |
6649 | return true; |
6650 | |
6651 | |
6652 | Converted = TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), |
6653 | S.Context.getCanonicalType(ParamType)); |
6654 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); |
6655 | return false; |
6656 | } |
6657 | |
6658 | |
6659 | |
6660 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
6661 | NonTypeTemplateParmDecl *Param, |
6662 | QualType ParamType, |
6663 | Expr *&ResultArg, |
6664 | TemplateArgument &Converted) { |
6665 | bool Invalid = false; |
6666 | |
6667 | Expr *Arg = ResultArg; |
6668 | bool ObjCLifetimeConversion; |
6669 | |
6670 | |
6671 | |
6672 | |
6673 | |
6674 | |
6675 | |
6676 | DeclRefExpr *DRE = nullptr; |
6677 | |
6678 | |
6679 | |
6680 | bool ExtraParens = false; |
6681 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
6682 | if (!Invalid && !ExtraParens) { |
6683 | S.Diag(Arg->getBeginLoc(), |
6684 | S.getLangOpts().CPlusPlus11 |
6685 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
6686 | : diag::ext_template_arg_extra_parens) |
6687 | << Arg->getSourceRange(); |
6688 | ExtraParens = true; |
6689 | } |
6690 | |
6691 | Arg = Parens->getSubExpr(); |
6692 | } |
6693 | |
6694 | while (SubstNonTypeTemplateParmExpr *subst = |
6695 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
6696 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
6697 | |
6698 | |
6699 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
6700 | if (UnOp->getOpcode() == UO_AddrOf) { |
6701 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
6702 | if (DRE && !DRE->getQualifier()) |
6703 | DRE = nullptr; |
6704 | } |
6705 | } |
6706 | |
6707 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
6708 | ValueDecl *VD = DRE->getDecl(); |
6709 | if (VD->getType()->isMemberPointerType()) { |
6710 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
6711 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
6712 | Converted = TemplateArgument(Arg); |
6713 | } else { |
6714 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
6715 | Converted = TemplateArgument(VD, ParamType); |
6716 | } |
6717 | return Invalid; |
6718 | } |
6719 | } |
6720 | |
6721 | DRE = nullptr; |
6722 | } |
6723 | |
6724 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
6725 | |
6726 | |
6727 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
6728 | Entity)) { |
6729 | case NPV_Error: |
6730 | return true; |
6731 | case NPV_NullPointer: |
6732 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
6733 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
6734 | true); |
6735 | return false; |
6736 | case NPV_NotNullPointer: |
6737 | break; |
6738 | } |
6739 | |
6740 | if (S.IsQualificationConversion(ResultArg->getType(), |
6741 | ParamType.getNonReferenceType(), false, |
6742 | ObjCLifetimeConversion)) { |
6743 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
6744 | ResultArg->getValueKind()) |
6745 | .get(); |
6746 | } else if (!S.Context.hasSameUnqualifiedType( |
6747 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
6748 | |
6749 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) |
6750 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
6751 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
6752 | return true; |
6753 | } |
6754 | |
6755 | if (!DRE) |
6756 | return S.Diag(Arg->getBeginLoc(), |
6757 | diag::err_template_arg_not_pointer_to_member_form) |
6758 | << Arg->getSourceRange(); |
6759 | |
6760 | if (isa<FieldDecl>(DRE->getDecl()) || |
6761 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
6762 | isa<CXXMethodDecl>(DRE->getDecl())) { |
6763 | assert((isa<FieldDecl>(DRE->getDecl()) || |
6764 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
6765 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
6766 | "Only non-static member pointers can make it here"); |
6767 | |
6768 | |
6769 | |
6770 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
6771 | Converted = TemplateArgument(Arg); |
6772 | } else { |
6773 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
6774 | Converted = TemplateArgument(D, S.Context.getCanonicalType(ParamType)); |
6775 | } |
6776 | return Invalid; |
6777 | } |
6778 | |
6779 | |
6780 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) |
6781 | << Arg->getSourceRange(); |
6782 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
6783 | return true; |
6784 | } |
6785 | |
6786 | |
6787 | |
6788 | |
6789 | |
6790 | |
6791 | |
6792 | |
6793 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
6794 | QualType ParamType, Expr *Arg, |
6795 | TemplateArgument &Converted, |
6796 | CheckTemplateArgumentKind CTAK) { |
6797 | SourceLocation StartLoc = Arg->getBeginLoc(); |
6798 | |
6799 | |
6800 | DeducedType *DeducedT = ParamType->getContainedDeducedType(); |
6801 | if (getLangOpts().CPlusPlus17 && DeducedT && !DeducedT->isDeduced()) { |
6802 | |
6803 | |
6804 | |
6805 | |
6806 | |
6807 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
6808 | auto *AT = dyn_cast<AutoType>(DeducedT); |
6809 | if (AT && AT->isDecltypeAuto()) { |
6810 | Converted = TemplateArgument(Arg); |
6811 | return Arg; |
6812 | } |
6813 | } |
6814 | |
6815 | |
6816 | |
6817 | |
6818 | Optional<unsigned> Depth = Param->getDepth() + 1; |
6819 | Expr *DeductionArg = Arg; |
6820 | if (auto *PE = dyn_cast<PackExpansionExpr>(DeductionArg)) |
6821 | DeductionArg = PE->getPattern(); |
6822 | TypeSourceInfo *TSI = |
6823 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()); |
6824 | if (isa<DeducedTemplateSpecializationType>(DeducedT)) { |
6825 | InitializedEntity Entity = |
6826 | InitializedEntity::InitializeTemplateParameter(ParamType, Param); |
6827 | InitializationKind Kind = InitializationKind::CreateForInit( |
6828 | DeductionArg->getBeginLoc(), false, DeductionArg); |
6829 | Expr *Inits[1] = {DeductionArg}; |
6830 | ParamType = |
6831 | DeduceTemplateSpecializationFromInitializer(TSI, Entity, Kind, Inits); |
6832 | if (ParamType.isNull()) |
6833 | return ExprError(); |
6834 | } else if (DeduceAutoType( |
6835 | TSI, DeductionArg, ParamType, Depth, |
6836 | |
6837 | |
6838 | |
6839 | |
6840 | |
6841 | true) == DAR_Failed) { |
6842 | Diag(Arg->getExprLoc(), |
6843 | diag::err_non_type_template_parm_type_deduction_failure) |
6844 | << Param->getDeclName() << Param->getType() << Arg->getType() |
6845 | << Arg->getSourceRange(); |
6846 | Diag(Param->getLocation(), diag::note_template_param_here); |
6847 | return ExprError(); |
6848 | } |
6849 | |
6850 | |
6851 | |
6852 | |
6853 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
6854 | if (ParamType.isNull()) { |
6855 | Diag(Param->getLocation(), diag::note_template_param_here); |
6856 | return ExprError(); |
6857 | } |
6858 | } |
6859 | |
6860 | |
6861 | assert(!ParamType.hasQualifiers() && |
6862 | "non-type template parameter type cannot be qualified"); |
6863 | |
6864 | |
6865 | if (CTAK == CTAK_Deduced && |
6866 | (ParamType->isReferenceType() |
6867 | ? !Context.hasSameType(ParamType.getNonReferenceType(), |
6868 | Arg->getType()) |
6869 | : !Context.hasSameUnqualifiedType(ParamType, Arg->getType()))) { |
6870 | |
6871 | |
6872 | |
6873 | |
6874 | |
6875 | |
6876 | |
6877 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
6878 | !Arg->getType()->getContainedDeducedType()) { |
6879 | Converted = TemplateArgument(Arg); |
6880 | return Arg; |
6881 | } |
6882 | |
6883 | |
6884 | |
6885 | |
6886 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
6887 | << Arg->getType() |
6888 | << ParamType.getUnqualifiedType(); |
6889 | Diag(Param->getLocation(), diag::note_template_param_here); |
6890 | return ExprError(); |
6891 | } |
6892 | |
6893 | |
6894 | |
6895 | |
6896 | |
6897 | if (ParamType->isDependentType() || Arg->isTypeDependent() || |
6898 | Arg->containsUnexpandedParameterPack()) { |
6899 | |
6900 | auto *PE = dyn_cast<PackExpansionExpr>(Arg); |
6901 | if (PE) |
6902 | Arg = PE->getPattern(); |
6903 | ExprResult E = ImpCastExprToType( |
6904 | Arg, ParamType.getNonLValueExprType(Context), CK_Dependent, |
6905 | ParamType->isLValueReferenceType() ? VK_LValue |
6906 | : ParamType->isRValueReferenceType() ? VK_XValue |
6907 | : VK_PRValue); |
6908 | if (E.isInvalid()) |
6909 | return ExprError(); |
6910 | if (PE) { |
6911 | |
6912 | E = new (Context) |
6913 | PackExpansionExpr(E.get()->getType(), E.get(), PE->getEllipsisLoc(), |
6914 | PE->getNumExpansions()); |
6915 | } |
6916 | Converted = TemplateArgument(E.get()); |
6917 | return E; |
6918 | } |
6919 | |
6920 | |
6921 | |
6922 | EnterExpressionEvaluationContext ConstantEvaluated( |
6923 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
6924 | |
6925 | if (getLangOpts().CPlusPlus17) { |
6926 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
6927 | |
6928 | |
6929 | |
6930 | |
6931 | |
6932 | |
6933 | |
6934 | |
6935 | |
6936 | Expr *InnerArg = Arg->IgnoreParenImpCasts(); |
6937 | if (ParamType->isRecordType() && isa<DeclRefExpr>(InnerArg) && |
6938 | Context.hasSameUnqualifiedType(ParamType, InnerArg->getType())) { |
6939 | NamedDecl *ND = cast<DeclRefExpr>(InnerArg)->getDecl(); |
6940 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) { |
6941 | Converted = TemplateArgument(TPO, CanonParamType); |
6942 | return Arg; |
6943 | } |
6944 | if (isa<NonTypeTemplateParmDecl>(ND)) { |
6945 | Converted = TemplateArgument(Arg); |
6946 | return Arg; |
6947 | } |
6948 | } |
6949 | |
6950 | |
6951 | |
6952 | |
6953 | APValue Value; |
6954 | ExprResult ArgResult = CheckConvertedConstantExpression( |
6955 | Arg, ParamType, Value, CCEK_TemplateArg, Param); |
6956 | if (ArgResult.isInvalid()) |
6957 | return ExprError(); |
6958 | |
6959 | |
6960 | |
6961 | if (ArgResult.get()->isValueDependent()) { |
6962 | Converted = TemplateArgument(ArgResult.get()); |
6963 | return ArgResult; |
6964 | } |
6965 | |
6966 | |
6967 | switch (Value.getKind()) { |
6968 | case APValue::None: |
6969 | assert(ParamType->isNullPtrType()); |
6970 | Converted = TemplateArgument(CanonParamType, true); |
6971 | break; |
6972 | case APValue::Indeterminate: |
6973 | llvm_unreachable("result of constant evaluation should be initialized"); |
6974 | break; |
6975 | case APValue::Int: |
6976 | assert(ParamType->isIntegralOrEnumerationType()); |
6977 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
6978 | break; |
6979 | case APValue::MemberPointer: { |
6980 | assert(ParamType->isMemberPointerType()); |
6981 | |
6982 | |
6983 | if (!Value.getMemberPointerPath().empty()) { |
6984 | Diag(Arg->getBeginLoc(), |
6985 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
6986 | << Value.getMemberPointerDecl() << ParamType |
6987 | << Arg->getSourceRange(); |
6988 | return ExprError(); |
6989 | } |
6990 | |
6991 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
6992 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
6993 | : TemplateArgument(CanonParamType, true); |
6994 | break; |
6995 | } |
6996 | case APValue::LValue: { |
6997 | |
6998 | |
6999 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
7000 | ParamType->isNullPtrType()); |
7001 | |
7002 | |
7003 | |
7004 | |
7005 | APValue::LValueBase Base = Value.getLValueBase(); |
7006 | auto *VD = const_cast<ValueDecl *>(Base.dyn_cast<const ValueDecl *>()); |
7007 | if (Base && (!VD || isa<LifetimeExtendedTemporaryDecl>(VD))) { |
7008 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
7009 | << Arg->getSourceRange(); |
7010 | return ExprError(); |
7011 | } |
7012 | |
7013 | |
7014 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
7015 | VD && VD->getType()->isArrayType() && |
7016 | Value.getLValuePath()[0].getAsArrayIndex() == 0 && |
7017 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
7018 | |
7019 | |
7020 | |
7021 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
7022 | Value.isLValueOnePastTheEnd()) { |
7023 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
7024 | << Value.getAsString(Context, ParamType); |
7025 | return ExprError(); |
7026 | } |
7027 | assert((VD || !ParamType->isReferenceType()) && |
7028 | "null reference should not be a constant expression"); |
7029 | assert((!VD || !ParamType->isNullPtrType()) && |
7030 | "non-null value of type nullptr_t?"); |
7031 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
7032 | : TemplateArgument(CanonParamType, true); |
7033 | break; |
7034 | } |
7035 | case APValue::Struct: |
7036 | case APValue::Union: |
7037 | |
7038 | Converted = TemplateArgument( |
7039 | Context.getTemplateParamObjectDecl(CanonParamType, Value), |
7040 | CanonParamType); |
7041 | break; |
7042 | case APValue::AddrLabelDiff: |
7043 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
7044 | case APValue::FixedPoint: |
7045 | case APValue::Float: |
7046 | case APValue::ComplexInt: |
7047 | case APValue::ComplexFloat: |
7048 | case APValue::Vector: |
7049 | case APValue::Array: |
7050 | return Diag(StartLoc, diag::err_non_type_template_arg_unsupported) |
7051 | << ParamType; |
7052 | } |
7053 | |
7054 | return ArgResult.get(); |
7055 | } |
7056 | |
7057 | |
7058 | |
7059 | |
7060 | |
7061 | |
7062 | |
7063 | if (ParamType->isIntegralOrEnumerationType()) { |
7064 | |
7065 | |
7066 | |
7067 | |
7068 | |
7069 | |
7070 | |
7071 | |
7072 | |
7073 | |
7074 | if (getLangOpts().CPlusPlus11) { |
7075 | |
7076 | |
7077 | |
7078 | |
7079 | |
7080 | |
7081 | |
7082 | llvm::APSInt Value; |
7083 | ExprResult ArgResult = |
7084 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
7085 | CCEK_TemplateArg); |
7086 | if (ArgResult.isInvalid()) |
7087 | return ExprError(); |
7088 | |
7089 | |
7090 | if (ArgResult.get()->isValueDependent()) { |
7091 | Converted = TemplateArgument(ArgResult.get()); |
7092 | return ArgResult; |
7093 | } |
7094 | |
7095 | |
7096 | |
7097 | |
7098 | QualType IntegerType = ParamType; |
7099 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
7100 | IntegerType = Enum->getDecl()->getIntegerType(); |
7101 | Value = Value.extOrTrunc(IntegerType->isExtIntType() |
7102 | ? Context.getIntWidth(IntegerType) |
7103 | : Context.getTypeSize(IntegerType)); |
7104 | |
7105 | Converted = TemplateArgument(Context, Value, |
7106 | Context.getCanonicalType(ParamType)); |
7107 | return ArgResult; |
7108 | } |
7109 | |
7110 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
7111 | if (ArgResult.isInvalid()) |
7112 | return ExprError(); |
7113 | Arg = ArgResult.get(); |
7114 | |
7115 | QualType ArgType = Arg->getType(); |
7116 | |
7117 | |
7118 | |
7119 | |
7120 | |
7121 | |
7122 | |
7123 | |
7124 | llvm::APSInt Value; |
7125 | if (!ArgType->isIntegralOrEnumerationType()) { |
7126 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) |
7127 | << ArgType << Arg->getSourceRange(); |
7128 | Diag(Param->getLocation(), diag::note_template_param_here); |
7129 | return ExprError(); |
7130 | } else if (!Arg->isValueDependent()) { |
7131 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
7132 | QualType T; |
7133 | |
7134 | public: |
7135 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
7136 | |
7137 | SemaDiagnosticBuilder diagnoseNotICE(Sema &S, |
7138 | SourceLocation Loc) override { |
7139 | return S.Diag(Loc, diag::err_template_arg_not_ice) << T; |
7140 | } |
7141 | } Diagnoser(ArgType); |
7142 | |
7143 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser).get(); |
7144 | if (!Arg) |
7145 | return ExprError(); |
7146 | } |
7147 | |
7148 | |
7149 | |
7150 | ArgType = ArgType.getUnqualifiedType(); |
7151 | |
7152 | |
7153 | if (Context.hasSameType(ParamType, ArgType)) { |
7154 | |
7155 | } else if (ParamType->isBooleanType()) { |
7156 | |
7157 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
7158 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
7159 | !ParamType->isEnumeralType()) { |
7160 | |
7161 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
7162 | } else { |
7163 | |
7164 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
7165 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
7166 | Diag(Param->getLocation(), diag::note_template_param_here); |
7167 | return ExprError(); |
7168 | } |
7169 | |
7170 | |
7171 | |
7172 | |
7173 | if (Arg->isValueDependent()) { |
7174 | |
7175 | |
7176 | Converted = TemplateArgument(Arg); |
7177 | return Arg; |
7178 | } |
7179 | |
7180 | QualType IntegerType = Context.getCanonicalType(ParamType); |
7181 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
7182 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
7183 | |
7184 | if (ParamType->isBooleanType()) { |
7185 | |
7186 | Value = Value != 0; |
7187 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
7188 | if (Value.getBitWidth() != AllowedBits) |
7189 | Value = Value.extOrTrunc(AllowedBits); |
7190 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
7191 | } else { |
7192 | llvm::APSInt OldValue = Value; |
7193 | |
7194 | |
7195 | |
7196 | unsigned AllowedBits = IntegerType->isExtIntType() |
7197 | ? Context.getIntWidth(IntegerType) |
7198 | : Context.getTypeSize(IntegerType); |
7199 | if (Value.getBitWidth() != AllowedBits) |
7200 | Value = Value.extOrTrunc(AllowedBits); |
7201 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
7202 | |
7203 | |
7204 | if (IntegerType->isUnsignedIntegerOrEnumerationType() && |
7205 | (OldValue.isSigned() && OldValue.isNegative())) { |
7206 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) |
7207 | << toString(OldValue, 10) << toString(Value, 10) << Param->getType() |
7208 | << Arg->getSourceRange(); |
7209 | Diag(Param->getLocation(), diag::note_template_param_here); |
7210 | } |
7211 | |
7212 | |
7213 | unsigned RequiredBits; |
7214 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
7215 | RequiredBits = OldValue.getActiveBits(); |
7216 | else if (OldValue.isUnsigned()) |
7217 | RequiredBits = OldValue.getActiveBits() + 1; |
7218 | else |
7219 | RequiredBits = OldValue.getMinSignedBits(); |
7220 | if (RequiredBits > AllowedBits) { |
7221 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) |
7222 | << toString(OldValue, 10) << toString(Value, 10) << Param->getType() |
7223 | << Arg->getSourceRange(); |
7224 | Diag(Param->getLocation(), diag::note_template_param_here); |
7225 | } |
7226 | } |
7227 | |
7228 | Converted = TemplateArgument(Context, Value, |
7229 | ParamType->isEnumeralType() |
7230 | ? Context.getCanonicalType(ParamType) |
7231 | : IntegerType); |
7232 | return Arg; |
7233 | } |
7234 | |
7235 | QualType ArgType = Arg->getType(); |
7236 | DeclAccessPair FoundResult; |
7237 | |
7238 | |
7239 | |
7240 | if ( |
7241 | |
7242 | |
7243 | |
7244 | |
7245 | (ParamType->isPointerType() && |
7246 | ParamType->castAs<PointerType>()->getPointeeType()->isFunctionType()) || |
7247 | |
7248 | |
7249 | |
7250 | |
7251 | (ParamType->isReferenceType() && |
7252 | ParamType->castAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
7253 | |
7254 | |
7255 | |
7256 | |
7257 | |
7258 | (ParamType->isMemberPointerType() && |
7259 | ParamType->castAs<MemberPointerType>()->getPointeeType() |
7260 | ->isFunctionType())) { |
7261 | |
7262 | if (Arg->getType() == Context.OverloadTy) { |
7263 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
7264 | true, |
7265 | FoundResult)) { |
7266 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
7267 | return ExprError(); |
7268 | |
7269 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
7270 | ArgType = Arg->getType(); |
7271 | } else |
7272 | return ExprError(); |
7273 | } |
7274 | |
7275 | if (!ParamType->isMemberPointerType()) { |
7276 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
7277 | ParamType, |
7278 | Arg, Converted)) |
7279 | return ExprError(); |
7280 | return Arg; |
7281 | } |
7282 | |
7283 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
7284 | Converted)) |
7285 | return ExprError(); |
7286 | return Arg; |
7287 | } |
7288 | |
7289 | if (ParamType->isPointerType()) { |
7290 | |
7291 | |
7292 | |
7293 | |
7294 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
7295 | "Only object pointers allowed here"); |
7296 | |
7297 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
7298 | ParamType, |
7299 | Arg, Converted)) |
7300 | return ExprError(); |
7301 | return Arg; |
7302 | } |
7303 | |
7304 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
7305 | |
7306 | |
7307 | |
7308 | |
7309 | |
7310 | |
7311 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
7312 | "Only object references allowed here"); |
7313 | |
7314 | if (Arg->getType() == Context.OverloadTy) { |
7315 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
7316 | ParamRefType->getPointeeType(), |
7317 | true, |
7318 | FoundResult)) { |
7319 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
7320 | return ExprError(); |
7321 | |
7322 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
7323 | ArgType = Arg->getType(); |
7324 | } else |
7325 | return ExprError(); |
7326 | } |
7327 | |
7328 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
7329 | ParamType, |
7330 | Arg, Converted)) |
7331 | return ExprError(); |
7332 | return Arg; |
7333 | } |
7334 | |
7335 | |
7336 | if (ParamType->isNullPtrType()) { |
7337 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
7338 | Converted = TemplateArgument(Arg); |
7339 | return Arg; |
7340 | } |
7341 | |
7342 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
7343 | case NPV_NotNullPointer: |
7344 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
7345 | << Arg->getType() << ParamType; |
7346 | Diag(Param->getLocation(), diag::note_template_param_here); |
7347 | return ExprError(); |
7348 | |
7349 | case NPV_Error: |
7350 | return ExprError(); |
7351 | |
7352 | case NPV_NullPointer: |
7353 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
7354 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
7355 | true); |
7356 | return Arg; |
7357 | } |
7358 | } |
7359 | |
7360 | |
7361 | |
7362 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
7363 | |
7364 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
7365 | Converted)) |
7366 | return ExprError(); |
7367 | return Arg; |
7368 | } |
7369 | |
7370 | static void DiagnoseTemplateParameterListArityMismatch( |
7371 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
7372 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
7373 | |
7374 | |
7375 | |
7376 | |
7377 | |
7378 | |
7379 | bool Sema::CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param, |
7380 | TemplateParameterList *Params, |
7381 | TemplateArgumentLoc &Arg) { |
7382 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
7383 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
7384 | if (!Template) { |
7385 | |
7386 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
7387 | return false; |
7388 | } |
7389 | |
7390 | if (Template->isInvalidDecl()) |
7391 | return true; |
7392 | |
7393 | |
7394 | |
7395 | |
7396 | |
7397 | |
7398 | |
7399 | |
7400 | |
7401 | |
7402 | |
7403 | |
7404 | |
7405 | if (!isa<ClassTemplateDecl>(Template) && |
7406 | !isa<TemplateTemplateParmDecl>(Template) && |
7407 | !isa<TypeAliasTemplateDecl>(Template) && |
7408 | !isa<BuiltinTemplateDecl>(Template)) { |
7409 | assert(isa<FunctionTemplateDecl>(Template) && |
7410 | "Only function templates are possible here"); |
7411 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
7412 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
7413 | << Template; |
7414 | } |
7415 | |
7416 | |
7417 | |
7418 | |
7419 | |
7420 | |
7421 | |
7422 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
7423 | |
7424 | |
7425 | |
7426 | |
7427 | if (TemplateParameterListsAreEqual( |
7428 | Template->getTemplateParameters(), Params, false, |
7429 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation()) && |
7430 | |
7431 | |
7432 | |
7433 | !Template->hasAssociatedConstraints()) |
7434 | return false; |
7435 | |
7436 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
7437 | Arg.getLocation())) { |
7438 | |
7439 | |
7440 | |
7441 | |
7442 | SmallVector<const Expr *, 3> ParamsAC, TemplateAC; |
7443 | Params->getAssociatedConstraints(ParamsAC); |
7444 | |
7445 | |
7446 | |
7447 | if (ParamsAC.empty()) |
7448 | return false; |
7449 | Template->getAssociatedConstraints(TemplateAC); |
7450 | bool IsParamAtLeastAsConstrained; |
7451 | if (IsAtLeastAsConstrained(Param, ParamsAC, Template, TemplateAC, |
7452 | IsParamAtLeastAsConstrained)) |
7453 | return true; |
7454 | if (!IsParamAtLeastAsConstrained) { |
7455 | Diag(Arg.getLocation(), |
7456 | diag::err_template_template_parameter_not_at_least_as_constrained) |
7457 | << Template << Param << Arg.getSourceRange(); |
7458 | Diag(Param->getLocation(), diag::note_entity_declared_at) << Param; |
7459 | Diag(Template->getLocation(), diag::note_entity_declared_at) |
7460 | << Template; |
7461 | MaybeEmitAmbiguousAtomicConstraintsDiagnostic(Param, ParamsAC, Template, |
7462 | TemplateAC); |
7463 | return true; |
7464 | } |
7465 | return false; |
7466 | } |
7467 | |
7468 | } |
7469 | |
7470 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
7471 | Params, |
7472 | true, |
7473 | TPL_TemplateTemplateArgumentMatch, |
7474 | Arg.getLocation()); |
7475 | } |
7476 | |
7477 | |
7478 | |
7479 | |
7480 | |
7481 | ExprResult |
7482 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
7483 | QualType ParamType, |
7484 | SourceLocation Loc) { |
7485 | |
7486 | |
7487 | |
7488 | |
7489 | |
7490 | if (ParamType->isArrayType()) |
7491 | ParamType = Context.getArrayDecayedType(ParamType); |
7492 | else if (ParamType->isFunctionType()) |
7493 | ParamType = Context.getPointerType(ParamType); |
7494 | |
7495 | |
7496 | |
7497 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
7498 | return ImpCastExprToType( |
7499 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
7500 | ParamType, |
7501 | ParamType->getAs<MemberPointerType>() |
7502 | ? CK_NullToMemberPointer |
7503 | : CK_NullToPointer); |
7504 | } |
7505 | assert(Arg.getKind() == TemplateArgument::Declaration && |
7506 | "Only declaration template arguments permitted here"); |
7507 | |
7508 | ValueDecl *VD = Arg.getAsDecl(); |
7509 | |
7510 | CXXScopeSpec SS; |
7511 | if (ParamType->isMemberPointerType()) { |
7512 | |
7513 | |
7514 | assert(VD->getDeclContext()->isRecord() && |
7515 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
7516 | isa<IndirectFieldDecl>(VD))); |
7517 | QualType ClassType |
7518 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
7519 | NestedNameSpecifier *Qualifier |
7520 | = NestedNameSpecifier::Create(Context, nullptr, false, |
7521 | ClassType.getTypePtr()); |
7522 | SS.MakeTrivial(Context, Qualifier, Loc); |
7523 | } |
7524 | |
7525 | ExprResult RefExpr = BuildDeclarationNameExpr( |
7526 | SS, DeclarationNameInfo(VD->getDeclName(), Loc), VD); |
7527 | if (RefExpr.isInvalid()) |
7528 | return ExprError(); |
7529 | |
7530 | |
7531 | QualType ElemT(RefExpr.get()->getType()->getArrayElementTypeNoTypeQual(), 0); |
7532 | if (ParamType->isPointerType() && !ElemT.isNull() && |
7533 | Context.hasSimilarType(ElemT, ParamType->getPointeeType())) { |
7534 | |
7535 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
7536 | if (RefExpr.isInvalid()) |
7537 | return ExprError(); |
7538 | } else if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
7539 | |
7540 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
7541 | if (RefExpr.isInvalid()) |
7542 | return ExprError(); |
7543 | } else if (ParamType->isRecordType()) { |
7544 | assert(isa<TemplateParamObjectDecl>(VD) && |
7545 | "arg for class template param not a template parameter object"); |
7546 | |
7547 | return RefExpr; |
7548 | } else { |
7549 | assert(ParamType->isReferenceType() && |
7550 | "unexpected type for decl template argument"); |
7551 | } |
7552 | |
7553 | |
7554 | assert(ParamType->isReferenceType() == RefExpr.get()->isLValue() && |
7555 | "value kind mismatch for non-type template argument"); |
7556 | |
7557 | |
7558 | |
7559 | QualType DestExprType = ParamType.getNonLValueExprType(Context); |
7560 | if (!Context.hasSameType(RefExpr.get()->getType(), DestExprType)) { |
7561 | CastKind CK; |
7562 | QualType Ignored; |
7563 | if (Context.hasSimilarType(RefExpr.get()->getType(), DestExprType) || |
7564 | IsFunctionConversion(RefExpr.get()->getType(), DestExprType, Ignored)) { |
7565 | CK = CK_NoOp; |
7566 | } else if (ParamType->isVoidPointerType() && |
7567 | RefExpr.get()->getType()->isPointerType()) { |
7568 | CK = CK_BitCast; |
7569 | } else { |
7570 | |
7571 | |
7572 | |
7573 | |
7574 | llvm_unreachable( |
7575 | "unexpected conversion required for non-type template argument"); |
7576 | } |
7577 | RefExpr = ImpCastExprToType(RefExpr.get(), DestExprType, CK, |
7578 | RefExpr.get()->getValueKind()); |
7579 | } |
7580 | |
7581 | return RefExpr; |
7582 | } |
7583 | |
7584 | |
7585 | |
7586 | |
7587 | |
7588 | |
7589 | |
7590 | |
7591 | ExprResult |
7592 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
7593 | SourceLocation Loc) { |
7594 | assert(Arg.getKind() == TemplateArgument::Integral && |
7595 | "Operation is only valid for integral template arguments"); |
7596 | QualType OrigT = Arg.getIntegralType(); |
7597 | |
7598 | |
7599 | |
7600 | |
7601 | |
7602 | |
7603 | QualType T = OrigT; |
7604 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
7605 | T = ET->getDecl()->getIntegerType(); |
7606 | |
7607 | Expr *E; |
7608 | if (T->isAnyCharacterType()) { |
7609 | CharacterLiteral::CharacterKind Kind; |
7610 | if (T->isWideCharType()) |
7611 | Kind = CharacterLiteral::Wide; |
7612 | else if (T->isChar8Type() && getLangOpts().Char8) |
7613 | Kind = CharacterLiteral::UTF8; |
7614 | else if (T->isChar16Type()) |
7615 | Kind = CharacterLiteral::UTF16; |
7616 | else if (T->isChar32Type()) |
7617 | Kind = CharacterLiteral::UTF32; |
7618 | else |
7619 | Kind = CharacterLiteral::Ascii; |
7620 | |
7621 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
7622 | Kind, T, Loc); |
7623 | } else if (T->isBooleanType()) { |
7624 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
7625 | T, Loc); |
7626 | } else if (T->isNullPtrType()) { |
7627 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
7628 | } else { |
7629 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
7630 | } |
7631 | |
7632 | if (OrigT->isEnumeralType()) { |
7633 | |
7634 | |
7635 | E = CStyleCastExpr::Create(Context, OrigT, VK_PRValue, CK_IntegralCast, E, |
7636 | nullptr, CurFPFeatureOverrides(), |
7637 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
7638 | Loc, Loc); |
7639 | } |
7640 | |
7641 | return E; |
7642 | } |
7643 | |
7644 | |
7645 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
7646 | bool Complain, |
7647 | Sema::TemplateParameterListEqualKind Kind, |
7648 | SourceLocation TemplateArgLoc) { |
7649 | |
7650 | if (Old->getKind() != New->getKind()) { |
7651 | if (Complain) { |
7652 | unsigned NextDiag = diag::err_template_param_different_kind; |
7653 | if (TemplateArgLoc.isValid()) { |
7654 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
7655 | NextDiag = diag::note_template_param_different_kind; |
7656 | } |
7657 | S.Diag(New->getLocation(), NextDiag) |
7658 | << (Kind != Sema::TPL_TemplateMatch); |
7659 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
7660 | << (Kind != Sema::TPL_TemplateMatch); |
7661 | } |
7662 | |
7663 | return false; |
7664 | } |
7665 | |
7666 | |
7667 | |
7668 | |
7669 | |
7670 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
7671 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
7672 | Old->isTemplateParameterPack())) { |
7673 | if (Complain) { |
7674 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
7675 | if (TemplateArgLoc.isValid()) { |
7676 | S.Diag(TemplateArgLoc, |
7677 | diag::err_template_arg_template_params_mismatch); |
7678 | NextDiag = diag::note_template_parameter_pack_non_pack; |
7679 | } |
7680 | |
7681 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
7682 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
7683 | : 2; |
7684 | S.Diag(New->getLocation(), NextDiag) |
7685 | << ParamKind << New->isParameterPack(); |
7686 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
7687 | << ParamKind << Old->isParameterPack(); |
7688 | } |
7689 | |
7690 | return false; |
7691 | } |
7692 | |
7693 | |
7694 | if (NonTypeTemplateParmDecl *OldNTTP |
7695 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
7696 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
7697 | |
7698 | |
7699 | |
7700 | |
7701 | |
7702 | if (Kind != Sema::TPL_TemplateTemplateArgumentMatch || |
7703 | (!OldNTTP->getType()->isDependentType() && |
7704 | !NewNTTP->getType()->isDependentType())) |
7705 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
7706 | if (Complain) { |
7707 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
7708 | if (TemplateArgLoc.isValid()) { |
7709 | S.Diag(TemplateArgLoc, |
7710 | diag::err_template_arg_template_params_mismatch); |
7711 | NextDiag = diag::note_template_nontype_parm_different_type; |
7712 | } |
7713 | S.Diag(NewNTTP->getLocation(), NextDiag) |
7714 | << NewNTTP->getType() |
7715 | << (Kind != Sema::TPL_TemplateMatch); |
7716 | S.Diag(OldNTTP->getLocation(), |
7717 | diag::note_template_nontype_parm_prev_declaration) |
7718 | << OldNTTP->getType(); |
7719 | } |
7720 | |
7721 | return false; |
7722 | } |
7723 | } |
7724 | |
7725 | |
7726 | |
7727 | else if (TemplateTemplateParmDecl *OldTTP |
7728 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
7729 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
7730 | if (!S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
7731 | OldTTP->getTemplateParameters(), |
7732 | Complain, |
7733 | (Kind == Sema::TPL_TemplateMatch |
7734 | ? Sema::TPL_TemplateTemplateParmMatch |
7735 | : Kind), |
7736 | TemplateArgLoc)) |
7737 | return false; |
7738 | } else if (Kind != Sema::TPL_TemplateTemplateArgumentMatch) { |
7739 | const Expr *NewC = nullptr, *OldC = nullptr; |
7740 | if (const auto *TC = cast<TemplateTypeParmDecl>(New)->getTypeConstraint()) |
7741 | NewC = TC->getImmediatelyDeclaredConstraint(); |
7742 | if (const auto *TC = cast<TemplateTypeParmDecl>(Old)->getTypeConstraint()) |
7743 | OldC = TC->getImmediatelyDeclaredConstraint(); |
7744 | |
7745 | auto Diagnose = [&] { |
7746 | S.Diag(NewC ? NewC->getBeginLoc() : New->getBeginLoc(), |
7747 | diag::err_template_different_type_constraint); |
7748 | S.Diag(OldC ? OldC->getBeginLoc() : Old->getBeginLoc(), |
7749 | diag::note_template_prev_declaration) << 0; |
7750 | }; |
7751 | |
7752 | if (!NewC != !OldC) { |
7753 | if (Complain) |
7754 | Diagnose(); |
7755 | return false; |
7756 | } |
7757 | |
7758 | if (NewC) { |
7759 | llvm::FoldingSetNodeID OldCID, NewCID; |
7760 | OldC->Profile(OldCID, S.Context, true); |
7761 | NewC->Profile(NewCID, S.Context, true); |
7762 | if (OldCID != NewCID) { |
7763 | if (Complain) |
7764 | Diagnose(); |
7765 | return false; |
7766 | } |
7767 | } |
7768 | } |
7769 | |
7770 | return true; |
7771 | } |
7772 | |
7773 | |
7774 | |
7775 | static |
7776 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
7777 | TemplateParameterList *New, |
7778 | TemplateParameterList *Old, |
7779 | Sema::TemplateParameterListEqualKind Kind, |
7780 | SourceLocation TemplateArgLoc) { |
7781 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
7782 | if (TemplateArgLoc.isValid()) { |
7783 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
7784 | NextDiag = diag::note_template_param_list_different_arity; |
7785 | } |
7786 | S.Diag(New->getTemplateLoc(), NextDiag) |
7787 | << (New->size() > Old->size()) |
7788 | << (Kind != Sema::TPL_TemplateMatch) |
7789 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
7790 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
7791 | << (Kind != Sema::TPL_TemplateMatch) |
7792 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
7793 | } |
7794 | |
7795 | |
7796 | |
7797 | |
7798 | |
7799 | |
7800 | |
7801 | |
7802 | |
7803 | |
7804 | |
7805 | |
7806 | |
7807 | |
7808 | |
7809 | |
7810 | |
7811 | |
7812 | |
7813 | |
7814 | |
7815 | |
7816 | |
7817 | |
7818 | bool |
7819 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
7820 | TemplateParameterList *Old, |
7821 | bool Complain, |
7822 | TemplateParameterListEqualKind Kind, |
7823 | SourceLocation TemplateArgLoc) { |
7824 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
7825 | if (Complain) |
7826 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
7827 | TemplateArgLoc); |
7828 | |
7829 | return false; |
7830 | } |
7831 | |
7832 | |
7833 | |
7834 | |
7835 | |
7836 | |
7837 | |
7838 | TemplateParameterList::iterator NewParm = New->begin(); |
7839 | TemplateParameterList::iterator NewParmEnd = New->end(); |
7840 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
7841 | OldParmEnd = Old->end(); |
7842 | OldParm != OldParmEnd; ++OldParm) { |
7843 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
7844 | !(*OldParm)->isTemplateParameterPack()) { |
7845 | if (NewParm == NewParmEnd) { |
7846 | if (Complain) |
7847 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
7848 | TemplateArgLoc); |
7849 | |
7850 | return false; |
7851 | } |
7852 | |
7853 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
7854 | Kind, TemplateArgLoc)) |
7855 | return false; |
7856 | |
7857 | ++NewParm; |
7858 | continue; |
7859 | } |
7860 | |
7861 | |
7862 | |
7863 | |
7864 | |
7865 | |
7866 | |
7867 | |
7868 | for (; NewParm != NewParmEnd; ++NewParm) { |
7869 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
7870 | Kind, TemplateArgLoc)) |
7871 | return false; |
7872 | } |
7873 | } |
7874 | |
7875 | |
7876 | if (NewParm != NewParmEnd) { |
7877 | if (Complain) |
7878 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
7879 | TemplateArgLoc); |
7880 | |
7881 | return false; |
7882 | } |
7883 | |
7884 | if (Kind != TPL_TemplateTemplateArgumentMatch) { |
7885 | const Expr *NewRC = New->getRequiresClause(); |
7886 | const Expr *OldRC = Old->getRequiresClause(); |
7887 | |
7888 | auto Diagnose = [&] { |
7889 | Diag(NewRC ? NewRC->getBeginLoc() : New->getTemplateLoc(), |
7890 | diag::err_template_different_requires_clause); |
7891 | Diag(OldRC ? OldRC->getBeginLoc() : Old->getTemplateLoc(), |
7892 | diag::note_template_prev_declaration) << 0; |
7893 | }; |
7894 | |
7895 | if (!NewRC != !OldRC) { |
7896 | if (Complain) |
7897 | Diagnose(); |
7898 | return false; |
7899 | } |
7900 | |
7901 | if (NewRC) { |
7902 | llvm::FoldingSetNodeID OldRCID, NewRCID; |
7903 | OldRC->Profile(OldRCID, Context, true); |
7904 | NewRC->Profile(NewRCID, Context, true); |
7905 | if (OldRCID != NewRCID) { |
7906 | if (Complain) |
7907 | Diagnose(); |
7908 | return false; |
7909 | } |
7910 | } |
7911 | } |
7912 | |
7913 | return true; |
7914 | } |
7915 | |
7916 | |
7917 | |
7918 | |
7919 | |
7920 | bool |
7921 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
7922 | if (!S) |
7923 | return false; |
7924 | |
7925 | |
7926 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
7927 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
7928 | S = S->getParent(); |
7929 | |
7930 | |
7931 | |
7932 | |
7933 | DeclContext *Ctx = S->getEntity(); |
7934 | if (Ctx && Ctx->isExternCContext()) { |
7935 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
7936 | << TemplateParams->getSourceRange(); |
7937 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
7938 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
7939 | return true; |
7940 | } |
7941 | Ctx = Ctx ? Ctx->getRedeclContext() : nullptr; |
7942 | |
7943 | |
7944 | |
7945 | |
7946 | |
7947 | |
7948 | |
7949 | |
7950 | |
7951 | |
7952 | if (Ctx) { |
7953 | if (Ctx->isFileContext()) |
7954 | return false; |
7955 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
7956 | |
7957 | |
7958 | if (RD->isLocalClass()) |
7959 | return Diag(TemplateParams->getTemplateLoc(), |
7960 | diag::err_template_inside_local_class) |
7961 | << TemplateParams->getSourceRange(); |
7962 | else |
7963 | return false; |
7964 | } |
7965 | } |
7966 | |
7967 | return Diag(TemplateParams->getTemplateLoc(), |
7968 | diag::err_template_outside_namespace_or_class_scope) |
7969 | << TemplateParams->getSourceRange(); |
7970 | } |
7971 | |
7972 | |
7973 | |
7974 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
7975 | if (!D) |
7976 | return TSK_Undeclared; |
7977 | |
7978 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
7979 | return Record->getTemplateSpecializationKind(); |
7980 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
7981 | return Function->getTemplateSpecializationKind(); |
7982 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
7983 | return Var->getTemplateSpecializationKind(); |
7984 | |
7985 | return TSK_Undeclared; |
7986 | } |
7987 | |
7988 | |
7989 | |
7990 | |
7991 | |
7992 | |
7993 | |
7994 | |
7995 | |
7996 | |
7997 | |
7998 | |
7999 | |
8000 | |
8001 | |
8002 | |
8003 | |
8004 | |
8005 | |
8006 | |
8007 | |
8008 | |
8009 | |
8010 | |
8011 | |
8012 | static bool CheckTemplateSpecializationScope(Sema &S, |
8013 | NamedDecl *Specialized, |
8014 | NamedDecl *PrevDecl, |
8015 | SourceLocation Loc, |
8016 | bool IsPartialSpecialization) { |
8017 | |
8018 | |
8019 | int EntityKind = 0; |
8020 | if (isa<ClassTemplateDecl>(Specialized)) |
8021 | EntityKind = IsPartialSpecialization? 1 : 0; |
8022 | else if (isa<VarTemplateDecl>(Specialized)) |
8023 | EntityKind = IsPartialSpecialization ? 3 : 2; |
8024 | else if (isa<FunctionTemplateDecl>(Specialized)) |
8025 | EntityKind = 4; |
8026 | else if (isa<CXXMethodDecl>(Specialized)) |
8027 | EntityKind = 5; |
8028 | else if (isa<VarDecl>(Specialized)) |
8029 | EntityKind = 6; |
8030 | else if (isa<RecordDecl>(Specialized)) |
8031 | EntityKind = 7; |
8032 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
8033 | EntityKind = 8; |
8034 | else { |
8035 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
8036 | << S.getLangOpts().CPlusPlus11; |
8037 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
8038 | return true; |
8039 | } |
8040 | |
8041 | |
8042 | |
8043 | |
8044 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
8045 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
8046 | << Specialized; |
8047 | return true; |
8048 | } |
8049 | |
8050 | |
8051 | |
8052 | |
8053 | DeclContext *SpecializedContext = |
8054 | Specialized->getDeclContext()->getRedeclContext(); |
8055 | DeclContext *DC = S.CurContext->getRedeclContext(); |
8056 | |
8057 | |
8058 | |
8059 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
8060 | : DC->Equals(SpecializedContext))) { |
8061 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
8062 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
8063 | << EntityKind << Specialized; |
8064 | else { |
8065 | auto *ND = cast<NamedDecl>(SpecializedContext); |
8066 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
8067 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
8068 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
8069 | S.Diag(Loc, Diag) << EntityKind << Specialized |
8070 | << ND << isa<CXXRecordDecl>(ND); |
8071 | } |
8072 | |
8073 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
8074 | |
8075 | |
8076 | |
8077 | if (DC->isRecord()) |
8078 | return true; |
8079 | } |
8080 | |
8081 | return false; |
8082 | } |
8083 | |
8084 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
8085 | if (!E->isTypeDependent()) |
8086 | return SourceLocation(); |
8087 | DependencyChecker Checker(Depth, true); |
8088 | Checker.TraverseStmt(E); |
8089 | if (Checker.MatchLoc.isInvalid()) |
8090 | return E->getSourceRange(); |
8091 | return Checker.MatchLoc; |
8092 | } |
8093 | |
8094 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
8095 | if (!TL.getType()->isDependentType()) |
8096 | return SourceLocation(); |
8097 | DependencyChecker Checker(Depth, true); |
8098 | Checker.TraverseTypeLoc(TL); |
8099 | if (Checker.MatchLoc.isInvalid()) |
8100 | return TL.getSourceRange(); |
8101 | return Checker.MatchLoc; |
8102 | } |
8103 | |
8104 | |
8105 | |
8106 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
8107 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
8108 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
8109 | for (unsigned I = 0; I != NumArgs; ++I) { |
8110 | if (Args[I].getKind() == TemplateArgument::Pack) { |
8111 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
8112 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
8113 | Args[I].pack_size(), IsDefaultArgument)) |
8114 | return true; |
8115 | |
8116 | continue; |
8117 | } |
8118 | |
8119 | if (Args[I].getKind() != TemplateArgument::Expression) |
8120 | continue; |
8121 | |
8122 | Expr *ArgExpr = Args[I].getAsExpr(); |
8123 | |
8124 | |
8125 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
8126 | ArgExpr = Expansion->getPattern(); |
8127 | |
8128 | |
8129 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
8130 | ArgExpr = ICE->getSubExpr(); |
8131 | |
8132 | |
8133 | |
8134 | |
8135 | |
8136 | |
8137 | |
8138 | |
8139 | |
8140 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
8141 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
8142 | continue; |
8143 | |
8144 | |
8145 | |
8146 | |
8147 | |
8148 | |
8149 | |
8150 | |
8151 | |
8152 | |
8153 | |
8154 | |
8155 | |
8156 | |
8157 | |
8158 | |
8159 | SourceRange ParamUseRange = |
8160 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
8161 | if (ParamUseRange.isValid()) { |
8162 | if (IsDefaultArgument) { |
8163 | S.Diag(TemplateNameLoc, |
8164 | diag::err_dependent_non_type_arg_in_partial_spec); |
8165 | S.Diag(ParamUseRange.getBegin(), |
8166 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
8167 | << ParamUseRange; |
8168 | } else { |
8169 | S.Diag(ParamUseRange.getBegin(), |
8170 | diag::err_dependent_non_type_arg_in_partial_spec) |
8171 | << ParamUseRange; |
8172 | } |
8173 | return true; |
8174 | } |
8175 | |
8176 | ParamUseRange = findTemplateParameter( |
8177 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
8178 | if (ParamUseRange.isValid()) { |
8179 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), |
8180 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
8181 | << Param->getType(); |
8182 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
8183 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
8184 | << ParamUseRange; |
8185 | return true; |
8186 | } |
8187 | } |
8188 | |
8189 | return false; |
8190 | } |
8191 | |
8192 | |
8193 | |
8194 | |
8195 | |
8196 | |
8197 | |
8198 | |
8199 | |
8200 | |
8201 | |
8202 | |
8203 | bool Sema::CheckTemplatePartialSpecializationArgs( |
8204 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
8205 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
8206 | |
8207 | |
8208 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
8209 | return false; |
8210 | |
8211 | TemplateParameterList *TemplateParams = |
8212 | PrimaryTemplate->getTemplateParameters(); |
8213 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
8214 | NonTypeTemplateParmDecl *Param |
8215 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
8216 | if (!Param) |
8217 | continue; |
8218 | |
8219 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
8220 | Param, &TemplateArgs[I], |
8221 | 1, I >= NumExplicit)) |
8222 | return true; |
8223 | } |
8224 | |
8225 | return false; |
8226 | } |
8227 | |
8228 | DeclResult Sema::ActOnClassTemplateSpecialization( |
8229 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
8230 | SourceLocation ModulePrivateLoc, CXXScopeSpec &SS, |
8231 | TemplateIdAnnotation &TemplateId, const ParsedAttributesView &Attr, |
8232 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { |
8233 | assert(TUK != TUK_Reference && "References are not specializations"); |
8234 | |
8235 | |
8236 | |
8237 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
8238 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
8239 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
8240 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
8241 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
8242 | |
8243 | |
8244 | TemplateName Name = TemplateId.Template.get(); |
8245 | ClassTemplateDecl *ClassTemplate |
8246 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
8247 | |
8248 | if (!ClassTemplate) { |
8249 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
8250 | << (Name.getAsTemplateDecl() && |
8251 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
8252 | return true; |
8253 | } |
8254 | |
8255 | bool isMemberSpecialization = false; |
8256 | bool isPartialSpecialization = false; |
8257 | |
8258 | |
8259 | |
8260 | |
8261 | |
8262 | bool Invalid = false; |
8263 | TemplateParameterList *TemplateParams = |
8264 | MatchTemplateParametersToScopeSpecifier( |
8265 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
8266 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
8267 | Invalid); |
8268 | if (Invalid) |
8269 | return true; |
8270 | |
8271 | |
8272 | if (TemplateParams && CheckTemplateDeclScope(S, TemplateParams)) |
8273 | return true; |
8274 | |
8275 | if (TemplateParams && TemplateParams->size() > 0) { |
8276 | isPartialSpecialization = true; |
8277 | |
8278 | if (TUK == TUK_Friend) { |
8279 | Diag(KWLoc, diag::err_partial_specialization_friend) |
8280 | << SourceRange(LAngleLoc, RAngleLoc); |
8281 | return true; |
8282 | } |
8283 | |
8284 | |
8285 | |
8286 | |
8287 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
8288 | Decl *Param = TemplateParams->getParam(I); |
8289 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
8290 | if (TTP->hasDefaultArgument()) { |
8291 | Diag(TTP->getDefaultArgumentLoc(), |
8292 | diag::err_default_arg_in_partial_spec); |
8293 | TTP->removeDefaultArgument(); |
8294 | } |
8295 | } else if (NonTypeTemplateParmDecl *NTTP |
8296 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
8297 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
8298 | Diag(NTTP->getDefaultArgumentLoc(), |
8299 | diag::err_default_arg_in_partial_spec) |
8300 | << DefArg->getSourceRange(); |
8301 | NTTP->removeDefaultArgument(); |
8302 | } |
8303 | } else { |
8304 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
8305 | if (TTP->hasDefaultArgument()) { |
8306 | Diag(TTP->getDefaultArgument().getLocation(), |
8307 | diag::err_default_arg_in_partial_spec) |
8308 | << TTP->getDefaultArgument().getSourceRange(); |
8309 | TTP->removeDefaultArgument(); |
8310 | } |
8311 | } |
8312 | } |
8313 | } else if (TemplateParams) { |
8314 | if (TUK == TUK_Friend) |
8315 | Diag(KWLoc, diag::err_template_spec_friend) |
8316 | << FixItHint::CreateRemoval( |
8317 | SourceRange(TemplateParams->getTemplateLoc(), |
8318 | TemplateParams->getRAngleLoc())) |
8319 | << SourceRange(LAngleLoc, RAngleLoc); |
8320 | } else { |
8321 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
8322 | } |
8323 | |
8324 | |
8325 | |
8326 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
8327 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
8328 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
8329 | Kind, TUK == TUK_Definition, KWLoc, |
8330 | ClassTemplate->getIdentifier())) { |
8331 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
8332 | << ClassTemplate |
8333 | << FixItHint::CreateReplacement(KWLoc, |
8334 | ClassTemplate->getTemplatedDecl()->getKindName()); |
8335 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
8336 | diag::note_previous_use); |
8337 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
8338 | } |
8339 | |
8340 | |
8341 | TemplateArgumentListInfo TemplateArgs = |
8342 | makeTemplateArgumentListInfo(*this, TemplateId); |
8343 | |
8344 | |
8345 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
8346 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
8347 | UPPC_PartialSpecialization)) |
8348 | return true; |
8349 | |
8350 | |
8351 | |
8352 | SmallVector<TemplateArgument, 4> Converted; |
8353 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
8354 | TemplateArgs, false, Converted, |
8355 | true)) |
8356 | return true; |
8357 | |
8358 | |
8359 | |
8360 | if (isPartialSpecialization) { |
8361 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
8362 | TemplateArgs.size(), Converted)) |
8363 | return true; |
8364 | |
8365 | |
8366 | |
8367 | if (!Name.isDependent() && |
8368 | !TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs, |
8369 | Converted)) { |
8370 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
8371 | << ClassTemplate->getDeclName(); |
8372 | isPartialSpecialization = false; |
8373 | } |
8374 | } |
8375 | |
8376 | void *InsertPos = nullptr; |
8377 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
8378 | |
8379 | if (isPartialSpecialization) |
8380 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, |
8381 | TemplateParams, |
8382 | InsertPos); |
8383 | else |
8384 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
8385 | |
8386 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
8387 | |
8388 | |
8389 | |
8390 | if (TUK != TUK_Friend && |
8391 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
8392 | TemplateNameLoc, |
8393 | isPartialSpecialization)) |
8394 | return true; |
8395 | |
8396 | |
8397 | QualType CanonType; |
8398 | if (isPartialSpecialization) { |
8399 | |
8400 | |
8401 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
8402 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
8403 | Converted); |
8404 | |
8405 | if (Context.hasSameType(CanonType, |
8406 | ClassTemplate->getInjectedClassNameSpecialization()) && |
8407 | (!Context.getLangOpts().CPlusPlus20 || |
8408 | !TemplateParams->hasAssociatedConstraints())) { |
8409 | |
8410 | |
8411 | |
8412 | |
8413 | |
8414 | |
8415 | |
8416 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
8417 | << 0 << (TUK == TUK_Definition) |
8418 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
8419 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
8420 | ClassTemplate->getIdentifier(), |
8421 | TemplateNameLoc, |
8422 | Attr, |
8423 | TemplateParams, |
8424 | AS_none, SourceLocation(), |
8425 | SourceLocation(), |
8426 | TemplateParameterLists.size() - 1, |
8427 | TemplateParameterLists.data()); |
8428 | } |
8429 | |
8430 | |
8431 | ClassTemplatePartialSpecializationDecl *PrevPartial |
8432 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
8433 | ClassTemplatePartialSpecializationDecl *Partial |
8434 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
8435 | ClassTemplate->getDeclContext(), |
8436 | KWLoc, TemplateNameLoc, |
8437 | TemplateParams, |
8438 | ClassTemplate, |
8439 | Converted, |
8440 | TemplateArgs, |
8441 | CanonType, |
8442 | PrevPartial); |
8443 | SetNestedNameSpecifier(*this, Partial, SS); |
8444 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
8445 | Partial->setTemplateParameterListsInfo( |
8446 | Context, TemplateParameterLists.drop_back(1)); |
8447 | } |
8448 | |
8449 | if (!PrevPartial) |
8450 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
8451 | Specialization = Partial; |
8452 | |
8453 | |
8454 | |
8455 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
8456 | PrevPartial->setMemberSpecialization(); |
8457 | |
8458 | CheckTemplatePartialSpecialization(Partial); |
8459 | } else { |
8460 | |
8461 | |
8462 | Specialization |
8463 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
8464 | ClassTemplate->getDeclContext(), |
8465 | KWLoc, TemplateNameLoc, |
8466 | ClassTemplate, |
8467 | Converted, |
8468 | PrevDecl); |
8469 | SetNestedNameSpecifier(*this, Specialization, SS); |
8470 | if (TemplateParameterLists.size() > 0) { |
8471 | Specialization->setTemplateParameterListsInfo(Context, |
8472 | TemplateParameterLists); |
8473 | } |
8474 | |
8475 | if (!PrevDecl) |
8476 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
8477 | |
8478 | if (CurContext->isDependentContext()) { |
8479 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
8480 | CanonType = Context.getTemplateSpecializationType( |
8481 | CanonTemplate, Converted); |
8482 | } else { |
8483 | CanonType = Context.getTypeDeclType(Specialization); |
8484 | } |
8485 | } |
8486 | |
8487 | |
8488 | |
8489 | |
8490 | |
8491 | |
8492 | |
8493 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
8494 | bool Okay = false; |
8495 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
8496 | |
8497 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
8498 | Okay = true; |
8499 | break; |
8500 | } |
8501 | } |
8502 | |
8503 | if (!Okay) { |
8504 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
8505 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
8506 | << Context.getTypeDeclType(Specialization) << Range; |
8507 | |
8508 | Diag(PrevDecl->getPointOfInstantiation(), |
8509 | diag::note_instantiation_required_here) |
8510 | << (PrevDecl->getTemplateSpecializationKind() |
8511 | != TSK_ImplicitInstantiation); |
8512 | return true; |
8513 | } |
8514 | } |
8515 | |
8516 | |
8517 | if (TUK != TUK_Friend) |
8518 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
8519 | |
8520 | |
8521 | if (TUK == TUK_Definition) { |
8522 | RecordDecl *Def = Specialization->getDefinition(); |
8523 | NamedDecl *Hidden = nullptr; |
8524 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
8525 | SkipBody->ShouldSkip = true; |
8526 | SkipBody->Previous = Def; |
8527 | makeMergedDefinitionVisible(Hidden); |
8528 | } else if (Def) { |
8529 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
8530 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
8531 | Diag(Def->getLocation(), diag::note_previous_definition); |
8532 | Specialization->setInvalidDecl(); |
8533 | return true; |
8534 | } |
8535 | } |
8536 | |
8537 | ProcessDeclAttributeList(S, Specialization, Attr); |
8538 | |
8539 | |
8540 | |
8541 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
8542 | AddAlignmentAttributesForRecord(Specialization); |
8543 | AddMsStructLayoutForRecord(Specialization); |
8544 | } |
8545 | |
8546 | if (ModulePrivateLoc.isValid()) |
8547 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
8548 | << (isPartialSpecialization? 1 : 0) |
8549 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
8550 | |
8551 | |
8552 | |
8553 | |
8554 | |
8555 | |
8556 | |
8557 | |
8558 | TypeSourceInfo *WrittenTy |
8559 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
8560 | TemplateArgs, CanonType); |
8561 | if (TUK != TUK_Friend) { |
8562 | Specialization->setTypeAsWritten(WrittenTy); |
8563 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
8564 | } |
8565 | |
8566 | |
8567 | |
8568 | |
8569 | |
8570 | |
8571 | |
8572 | |
8573 | |
8574 | Specialization->setLexicalDeclContext(CurContext); |
8575 | |
8576 | |
8577 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
8578 | Specialization->startDefinition(); |
8579 | |
8580 | if (TUK == TUK_Friend) { |
8581 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
8582 | TemplateNameLoc, |
8583 | WrittenTy, |
8584 | KWLoc); |
8585 | Friend->setAccess(AS_public); |
8586 | CurContext->addDecl(Friend); |
8587 | } else { |
8588 | |
8589 | |
8590 | |
8591 | CurContext->addDecl(Specialization); |
8592 | } |
8593 | |
8594 | if (SkipBody && SkipBody->ShouldSkip) |
8595 | return SkipBody->Previous; |
8596 | |
8597 | return Specialization; |
8598 | } |
8599 | |
8600 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
8601 | MultiTemplateParamsArg TemplateParameterLists, |
8602 | Declarator &D) { |
8603 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
8604 | ActOnDocumentableDecl(NewDecl); |
8605 | return NewDecl; |
8606 | } |
8607 | |
8608 | Decl *Sema::ActOnConceptDefinition(Scope *S, |
8609 | MultiTemplateParamsArg TemplateParameterLists, |
8610 | IdentifierInfo *Name, SourceLocation NameLoc, |
8611 | Expr *ConstraintExpr) { |
8612 | DeclContext *DC = CurContext; |
8613 | |
8614 | if (!DC->getRedeclContext()->isFileContext()) { |
8615 | Diag(NameLoc, |
8616 | diag::err_concept_decls_may_only_appear_in_global_namespace_scope); |
8617 | return nullptr; |
8618 | } |
8619 | |
8620 | if (TemplateParameterLists.size() > 1) { |
8621 | Diag(NameLoc, diag::err_concept_extra_headers); |
8622 | return nullptr; |
8623 | } |
8624 | |
8625 | if (TemplateParameterLists.front()->size() == 0) { |
8626 | Diag(NameLoc, diag::err_concept_no_parameters); |
8627 | return nullptr; |
8628 | } |
8629 | |
8630 | if (DiagnoseUnexpandedParameterPack(ConstraintExpr)) |
8631 | return nullptr; |
8632 | |
8633 | ConceptDecl *NewDecl = ConceptDecl::Create(Context, DC, NameLoc, Name, |
8634 | TemplateParameterLists.front(), |
8635 | ConstraintExpr); |
8636 | |
8637 | if (NewDecl->hasAssociatedConstraints()) { |
8638 | |
8639 | |
8640 | Diag(NameLoc, diag::err_concept_no_associated_constraints); |
8641 | NewDecl->setInvalidDecl(); |
8642 | } |
8643 | |
8644 | |
8645 | DeclarationNameInfo NameInfo(NewDecl->getDeclName(), NameLoc); |
8646 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
8647 | ForVisibleRedeclaration); |
8648 | LookupName(Previous, S); |
8649 | |
8650 | FilterLookupForScope(Previous, DC, S, false, |
8651 | false); |
8652 | if (!Previous.empty()) { |
8653 | auto *Old = Previous.getRepresentativeDecl(); |
8654 | Diag(NameLoc, isa<ConceptDecl>(Old) ? diag::err_redefinition : |
8655 | diag::err_redefinition_different_kind) << NewDecl->getDeclName(); |
8656 | Diag(Old->getLocation(), diag::note_previous_definition); |
8657 | } |
8658 | |
8659 | ActOnDocumentableDecl(NewDecl); |
8660 | PushOnScopeChains(NewDecl, S); |
8661 | return NewDecl; |
8662 | } |
8663 | |
8664 | |
8665 | |
8666 | static void StripImplicitInstantiation(NamedDecl *D) { |
8667 | D->dropAttr<DLLImportAttr>(); |
8668 | D->dropAttr<DLLExportAttr>(); |
8669 | |
8670 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
8671 | FD->setInlineSpecified(false); |
8672 | } |
8673 | |
8674 | |
8675 | |
8676 | static SourceLocation DiagLocForExplicitInstantiation( |
8677 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
8678 | |
8679 | |
8680 | |
8681 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
8682 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
8683 | Prev = Prev->getPreviousDecl()) { |
8684 | PrevDiagLoc = Prev->getLocation(); |
8685 | } |
8686 | assert(PrevDiagLoc.isValid() && |
8687 | "Explicit instantiation without point of instantiation?"); |
8688 | return PrevDiagLoc; |
8689 | } |
8690 | |
8691 | |
8692 | |
8693 | |
8694 | |
8695 | |
8696 | |
8697 | |
8698 | |
8699 | |
8700 | |
8701 | |
8702 | |
8703 | |
8704 | |
8705 | |
8706 | |
8707 | |
8708 | |
8709 | |
8710 | |
8711 | |
8712 | |
8713 | bool |
8714 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
8715 | TemplateSpecializationKind NewTSK, |
8716 | NamedDecl *PrevDecl, |
8717 | TemplateSpecializationKind PrevTSK, |
8718 | SourceLocation PrevPointOfInstantiation, |
8719 | bool &HasNoEffect) { |
8720 | HasNoEffect = false; |
8721 | |
8722 | switch (NewTSK) { |
8723 | case TSK_Undeclared: |
8724 | case TSK_ImplicitInstantiation: |
8725 | assert( |
8726 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
8727 | "previous declaration must be implicit!"); |
8728 | return false; |
8729 | |
8730 | case TSK_ExplicitSpecialization: |
8731 | switch (PrevTSK) { |
8732 | case TSK_Undeclared: |
8733 | case TSK_ExplicitSpecialization: |
8734 | |
8735 | |
8736 | |
8737 | return false; |
8738 | |
8739 | case TSK_ImplicitInstantiation: |
8740 | if (PrevPointOfInstantiation.isInvalid()) { |
8741 | |
8742 | |
8743 | StripImplicitInstantiation(PrevDecl); |
8744 | return false; |
8745 | } |
8746 | |
8747 | LLVM_FALLTHROUGH; |
8748 | |
8749 | case TSK_ExplicitInstantiationDeclaration: |
8750 | case TSK_ExplicitInstantiationDefinition: |
8751 | assert((PrevTSK == TSK_ImplicitInstantiation || |
8752 | PrevPointOfInstantiation.isValid()) && |
8753 | "Explicit instantiation without point of instantiation?"); |
8754 | |
8755 | |
8756 | |
8757 | |
8758 | |
8759 | |
8760 | |
8761 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
8762 | |
8763 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
8764 | return false; |
8765 | } |
8766 | |
8767 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
8768 | << PrevDecl; |
8769 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
8770 | << (PrevTSK != TSK_ImplicitInstantiation); |
8771 | |
8772 | return true; |
8773 | } |
8774 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
8775 | |
8776 | case TSK_ExplicitInstantiationDeclaration: |
8777 | switch (PrevTSK) { |
8778 | case TSK_ExplicitInstantiationDeclaration: |
8779 | |
8780 | HasNoEffect = true; |
8781 | return false; |
8782 | |
8783 | case TSK_Undeclared: |
8784 | case TSK_ImplicitInstantiation: |
8785 | |
8786 | |
8787 | return false; |
8788 | |
8789 | case TSK_ExplicitSpecialization: |
8790 | |
8791 | |
8792 | |
8793 | |
8794 | |
8795 | HasNoEffect = true; |
8796 | return false; |
8797 | |
8798 | case TSK_ExplicitInstantiationDefinition: |
8799 | |
8800 | |
8801 | |
8802 | |
8803 | Diag(NewLoc, |
8804 | diag::err_explicit_instantiation_declaration_after_definition); |
8805 | |
8806 | |
8807 | |
8808 | |
8809 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
8810 | diag::note_explicit_instantiation_definition_here); |
8811 | HasNoEffect = true; |
8812 | return false; |
8813 | } |
8814 | llvm_unreachable("Unexpected TemplateSpecializationKind!"); |
8815 | |
8816 | case TSK_ExplicitInstantiationDefinition: |
8817 | switch (PrevTSK) { |
8818 | case TSK_Undeclared: |
8819 | case TSK_ImplicitInstantiation: |
8820 | |
8821 | |
8822 | return false; |
8823 | |
8824 | case TSK_ExplicitSpecialization: |
8825 | |
8826 | |
8827 | |
8828 | |
8829 | |
8830 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
8831 | << PrevDecl; |
8832 | Diag(PrevDecl->getLocation(), |
8833 | diag::note_previous_template_specialization); |
8834 | HasNoEffect = true; |
8835 | return false; |
8836 | |
8837 | case TSK_ExplicitInstantiationDeclaration: |
8838 | |
8839 | |
8840 | |
8841 | |
8842 | |
8843 | |
8844 | |
8845 | |
8846 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
8847 | |
8848 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
8849 | HasNoEffect = true; |
8850 | break; |
8851 | } |
8852 | } |
8853 | |
8854 | return false; |
8855 | |
8856 | case TSK_ExplicitInstantiationDefinition: |
8857 | |
8858 | |
8859 | |
8860 | |
8861 | |
8862 | |
8863 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
8864 | ? diag::ext_explicit_instantiation_duplicate |
8865 | : diag::err_explicit_instantiation_duplicate) |
8866 | << PrevDecl; |
8867 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
8868 | diag::note_previous_explicit_instantiation); |
8869 | HasNoEffect = true; |
8870 | return false; |
8871 | } |
8872 | } |
8873 | |
8874 | llvm_unreachable("Missing specialization/instantiation case?"); |
8875 | } |
8876 | |
8877 | |
8878 | |
8879 | |
8880 | |
8881 | |
8882 | |
8883 | |
8884 | |
8885 | |
8886 | |
8887 | |
8888 | |
8889 | |
8890 | |
8891 | |
8892 | bool |
8893 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
8894 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
8895 | LookupResult &Previous) { |
8896 | |
8897 | |
8898 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
8899 | LookupResult::Filter F = Previous.makeFilter(); |
8900 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; |
8901 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; |
8902 | while (F.hasNext()) { |
8903 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
8904 | if (!isa<FunctionTemplateDecl>(D)) { |
8905 | F.erase(); |
8906 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); |
8907 | continue; |
8908 | } |
8909 | |
8910 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
8911 | D->getDeclContext()->getRedeclContext())) { |
8912 | F.erase(); |
8913 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); |
8914 | continue; |
8915 | } |
8916 | } |
8917 | F.done(); |
8918 | |
8919 | if (Previous.empty()) { |
8920 | Diag(FD->getLocation(), |
8921 | diag::err_dependent_function_template_spec_no_match); |
8922 | for (auto &P : DiscardedCandidates) |
8923 | Diag(P.second->getLocation(), |
8924 | diag::note_dependent_function_template_spec_discard_reason) |
8925 | << P.first; |
8926 | return true; |
8927 | } |
8928 | |
8929 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
8930 | ExplicitTemplateArgs); |
8931 | return false; |
8932 | } |
8933 | |
8934 | |
8935 | |
8936 | |
8937 | |
8938 | |
8939 | |
8940 | |
8941 | |
8942 | |
8943 | |
8944 | |
8945 | |
8946 | |
8947 | |
8948 | |
8949 | |
8950 | |
8951 | |
8952 | |
8953 | |
8954 | |
8955 | |
8956 | bool Sema::CheckFunctionTemplateSpecialization( |
8957 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
8958 | LookupResult &Previous, bool QualifiedFriend) { |
8959 | |
8960 | |
8961 | UnresolvedSet<8> Candidates; |
8962 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
8963 | false); |
8964 | |
8965 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
8966 | ConvertedTemplateArgs; |
8967 | |
8968 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
8969 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
8970 | I != E; ++I) { |
8971 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
8972 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
8973 | |
8974 | |
8975 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
8976 | Ovl->getDeclContext()->getRedeclContext())) |
8977 | continue; |
8978 | |
8979 | |
8980 | |
8981 | |
8982 | |
8983 | |
8984 | QualType FT = FD->getType(); |
8985 | if (FD->isConstexpr()) { |
8986 | CXXMethodDecl *OldMD = |
8987 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
8988 | if (OldMD && OldMD->isConst()) { |
8989 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
8990 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
8991 | EPI.TypeQuals.addConst(); |
8992 | FT = Context.getFunctionType(FPT->getReturnType(), |
8993 | FPT->getParamTypes(), EPI); |
8994 | } |
8995 | } |
8996 | |
8997 | TemplateArgumentListInfo Args; |
8998 | if (ExplicitTemplateArgs) |
8999 | Args = *ExplicitTemplateArgs; |
9000 | |
9001 | |
9002 | |
9003 | |
9004 | |
9005 | |
9006 | |
9007 | |
9008 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
9009 | FunctionDecl *Specialization = nullptr; |
9010 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
9011 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
9012 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
9013 | Info)) { |
9014 | |
9015 | |
9016 | FailedCandidates.addCandidate().set( |
9017 | I.getPair(), FunTmpl->getTemplatedDecl(), |
9018 | MakeDeductionFailureInfo(Context, TDK, Info)); |
9019 | (void)TDK; |
9020 | continue; |
9021 | } |
9022 | |
9023 | |
9024 | |
9025 | |
9026 | |
9027 | |
9028 | if (LangOpts.CUDA && |
9029 | IdentifyCUDATarget(Specialization, |
9030 | true) != |
9031 | IdentifyCUDATarget(FD, true)) { |
9032 | FailedCandidates.addCandidate().set( |
9033 | I.getPair(), FunTmpl->getTemplatedDecl(), |
9034 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
9035 | continue; |
9036 | } |
9037 | |
9038 | |
9039 | if (ExplicitTemplateArgs) |
9040 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
9041 | Candidates.addDecl(Specialization, I.getAccess()); |
9042 | } |
9043 | } |
9044 | |
9045 | |
9046 | |
9047 | |
9048 | if (QualifiedFriend && Candidates.empty()) { |
9049 | Diag(FD->getLocation(), diag::err_qualified_friend_no_match) |
9050 | << FD->getDeclName() << FDLookupContext; |
9051 | |
9052 | |
9053 | for (auto *OldND : Previous) { |
9054 | if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl())) |
9055 | NoteOverloadCandidate(OldND, OldFD, CRK_None, FD->getType(), false); |
9056 | } |
9057 | FailedCandidates.NoteCandidates(*this, FD->getLocation()); |
9058 | return true; |
9059 | } |
9060 | |
9061 | |
9062 | UnresolvedSetIterator Result = getMostSpecialized( |
9063 | Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), |
9064 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
9065 | PDiag(diag::err_function_template_spec_ambiguous) |
9066 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
9067 | PDiag(diag::note_function_template_spec_matched)); |
9068 | |
9069 | if (Result == Candidates.end()) |
9070 | return true; |
9071 | |
9072 | |
9073 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
9074 | |
9075 | FunctionTemplateSpecializationInfo *SpecInfo |
9076 | = Specialization->getTemplateSpecializationInfo(); |
9077 | assert(SpecInfo && "Function template specialization info missing?"); |
9078 | |
9079 | |
9080 | |
9081 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
9082 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
9083 | Specialization->setLocation(FD->getLocation()); |
9084 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
9085 | |
9086 | |
9087 | |
9088 | |
9089 | |
9090 | |
9091 | Specialization->setConstexprKind(FD->getConstexprKind()); |
9092 | } |
9093 | |
9094 | |
9095 | |
9096 | |
9097 | |
9098 | |
9099 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
9100 | |
9101 | |
9102 | if (!isFriend && |
9103 | CheckTemplateSpecializationScope(*this, |
9104 | Specialization->getPrimaryTemplate(), |
9105 | Specialization, FD->getLocation(), |
9106 | false)) |
9107 | return true; |
9108 | |
9109 | |
9110 | |
9111 | |
9112 | |
9113 | |
9114 | |
9115 | bool HasNoEffect = false; |
9116 | if (!isFriend && |
9117 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
9118 | TSK_ExplicitSpecialization, |
9119 | Specialization, |
9120 | SpecInfo->getTemplateSpecializationKind(), |
9121 | SpecInfo->getPointOfInstantiation(), |
9122 | HasNoEffect)) |
9123 | return true; |
9124 | |
9125 | |
9126 | |
9127 | if (!isFriend) { |
9128 | |
9129 | |
9130 | |
9131 | |
9132 | |
9133 | |
9134 | |
9135 | |
9136 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
9137 | !Specialization->getCanonicalDecl()->isReferenced()) { |
9138 | |
9139 | assert( |
9140 | Specialization->getCanonicalDecl() == Specialization && |
9141 | "This must be the only existing declaration of this specialization"); |
9142 | |
9143 | Specialization->setDeletedAsWritten(false); |
9144 | } |
9145 | |
9146 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
9147 | MarkUnusedFileScopedDecl(Specialization); |
9148 | } |
9149 | |
9150 | |
9151 | |
9152 | |
9153 | |
9154 | const TemplateArgumentList* TemplArgs = new (Context) |
9155 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
9156 | FD->setFunctionTemplateSpecialization( |
9157 | Specialization->getPrimaryTemplate(), TemplArgs, nullptr, |
9158 | SpecInfo->getTemplateSpecializationKind(), |
9159 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
9160 | |
9161 | |
9162 | |
9163 | |
9164 | |
9165 | |
9166 | if (LangOpts.CUDA) |
9167 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
9168 | |
9169 | |
9170 | |
9171 | Previous.clear(); |
9172 | Previous.addDecl(Specialization); |
9173 | return false; |
9174 | } |
9175 | |
9176 | |
9177 | |
9178 | |
9179 | |
9180 | |
9181 | |
9182 | |
9183 | |
9184 | |
9185 | |
9186 | |
9187 | |
9188 | |
9189 | |
9190 | bool |
9191 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
9192 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
9193 | |
9194 | |
9195 | NamedDecl *FoundInstantiation = nullptr; |
9196 | NamedDecl *Instantiation = nullptr; |
9197 | NamedDecl *InstantiatedFrom = nullptr; |
9198 | MemberSpecializationInfo *MSInfo = nullptr; |
9199 | |
9200 | if (Previous.empty()) { |
9201 | |
9202 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
9203 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
9204 | I != E; ++I) { |
9205 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
9206 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
9207 | QualType Adjusted = Function->getType(); |
9208 | if (!hasExplicitCallingConv(Adjusted)) |
9209 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
9210 | |
9211 | |
9212 | if (Context.hasSameType(Adjusted, Method->getType())) { |
9213 | FoundInstantiation = *I; |
9214 | Instantiation = Method; |
9215 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
9216 | MSInfo = Method->getMemberSpecializationInfo(); |
9217 | break; |
9218 | } |
9219 | } |
9220 | } |
9221 | } else if (isa<VarDecl>(Member)) { |
9222 | VarDecl *PrevVar; |
9223 | if (Previous.isSingleResult() && |
9224 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
9225 | if (PrevVar->isStaticDataMember()) { |
9226 | FoundInstantiation = Previous.getRepresentativeDecl(); |
9227 | Instantiation = PrevVar; |
9228 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
9229 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
9230 | } |
9231 | } else if (isa<RecordDecl>(Member)) { |
9232 | CXXRecordDecl *PrevRecord; |
9233 | if (Previous.isSingleResult() && |
9234 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
9235 | FoundInstantiation = Previous.getRepresentativeDecl(); |
9236 | Instantiation = PrevRecord; |
9237 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
9238 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
9239 | } |
9240 | } else if (isa<EnumDecl>(Member)) { |
9241 | EnumDecl *PrevEnum; |
9242 | if (Previous.isSingleResult() && |
9243 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
9244 | FoundInstantiation = Previous.getRepresentativeDecl(); |
9245 | Instantiation = PrevEnum; |
9246 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
9247 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
9248 | } |
9249 | } |
9250 | |
9251 | if (!Instantiation) { |
9252 | |
9253 | |
9254 | |
9255 | return false; |
9256 | } |
9257 | |
9258 | |
9259 | |
9260 | |
9261 | |
9262 | |
9263 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
9264 | |
9265 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
9266 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
9267 | cast<CXXMethodDecl>(InstantiatedFrom), |
9268 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
9269 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
9270 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
9271 | cast<CXXRecordDecl>(InstantiatedFrom), |
9272 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
9273 | } |
9274 | |
9275 | Previous.clear(); |
9276 | Previous.addDecl(FoundInstantiation); |
9277 | return false; |
9278 | } |
9279 | |
9280 | |
9281 | if (!InstantiatedFrom) { |
9282 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
9283 | << Member; |
9284 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
9285 | return true; |
9286 | } |
9287 | |
9288 | |
9289 | |
9290 | |
9291 | |
9292 | |
9293 | |
9294 | assert(MSInfo && "Member specialization info missing?"); |
9295 | |
9296 | bool HasNoEffect = false; |
9297 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
9298 | TSK_ExplicitSpecialization, |
9299 | Instantiation, |
9300 | MSInfo->getTemplateSpecializationKind(), |
9301 | MSInfo->getPointOfInstantiation(), |
9302 | HasNoEffect)) |
9303 | return true; |
9304 | |
9305 | |
9306 | if (CheckTemplateSpecializationScope(*this, |
9307 | InstantiatedFrom, |
9308 | Instantiation, Member->getLocation(), |
9309 | false)) |
9310 | return true; |
9311 | |
9312 | |
9313 | |
9314 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
9315 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
9316 | if (InstantiationFunction->getTemplateSpecializationKind() == |
9317 | TSK_ImplicitInstantiation) { |
9318 | |
9319 | |
9320 | if (InstantiationFunction->isDeleted()) { |
9321 | |
9322 | assert(InstantiationFunction->getCanonicalDecl() == |
9323 | InstantiationFunction); |
9324 | |
9325 | InstantiationFunction->setDeletedAsWritten(false); |
9326 | } |
9327 | } |
9328 | |
9329 | MemberFunction->setInstantiationOfMemberFunction( |
9330 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
9331 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
9332 | MemberVar->setInstantiationOfStaticDataMember( |
9333 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
9334 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
9335 | MemberClass->setInstantiationOfMemberClass( |
9336 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
9337 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
9338 | MemberEnum->setInstantiationOfMemberEnum( |
9339 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
9340 | } else { |
9341 | llvm_unreachable("unknown member specialization kind"); |
9342 | } |
9343 | |
9344 | |
9345 | |
9346 | Previous.clear(); |
9347 | Previous.addDecl(FoundInstantiation); |
9348 | return false; |
9349 | } |
9350 | |
9351 | |
9352 | |
9353 | |
9354 | |
9355 | |
9356 | template<typename DeclT> |
9357 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
9358 | SourceLocation Loc) { |
9359 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
9360 | return; |
9361 | |
9362 | |
9363 | |
9364 | |
9365 | |
9366 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
9367 | OrigD->setLocation(Loc); |
9368 | } |
9369 | |
9370 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
9371 | LookupResult &Previous) { |
9372 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
9373 | if (Instantiation == Member) |
9374 | return; |
9375 | |
9376 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
9377 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
9378 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
9379 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
9380 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
9381 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
9382 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
9383 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
9384 | else |
9385 | llvm_unreachable("unknown member specialization kind"); |
9386 | } |
9387 | |
9388 | |
9389 | |
9390 | |
9391 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
9392 | SourceLocation InstLoc, |
9393 | bool WasQualifiedName) { |
9394 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
9395 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
9396 | |
9397 | if (CurContext->isRecord()) { |
9398 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
9399 | << D; |
9400 | return true; |
9401 | } |
9402 | |
9403 | |
9404 | |
9405 | |
9406 | |
9407 | |
9408 | |
9409 | |
9410 | |
9411 | if (WasQualifiedName) { |
9412 | if (CurContext->Encloses(OrigContext)) |
9413 | return false; |
9414 | } else { |
9415 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
9416 | return false; |
9417 | } |
9418 | |
9419 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
9420 | if (WasQualifiedName) |
9421 | S.Diag(InstLoc, |
9422 | S.getLangOpts().CPlusPlus11? |
9423 | diag::err_explicit_instantiation_out_of_scope : |
9424 | diag::warn_explicit_instantiation_out_of_scope_0x) |
9425 | << D << NS; |
9426 | else |
9427 | S.Diag(InstLoc, |
9428 | S.getLangOpts().CPlusPlus11? |
9429 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
9430 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
9431 | << D << NS; |
9432 | } else |
9433 | S.Diag(InstLoc, |
9434 | S.getLangOpts().CPlusPlus11? |
9435 | diag::err_explicit_instantiation_must_be_global : |
9436 | diag::warn_explicit_instantiation_must_be_global_0x) |
9437 | << D; |
9438 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
9439 | return false; |
9440 | } |
9441 | |
9442 | |
9443 | static bool CheckExplicitInstantiation(Sema &S, NamedDecl *D, |
9444 | SourceLocation InstLoc, |
9445 | bool WasQualifiedName, |
9446 | TemplateSpecializationKind TSK) { |
9447 | |
9448 | |
9449 | |
9450 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
9451 | D->getFormalLinkage() == InternalLinkage) { |
9452 | S.Diag(InstLoc, diag::err_explicit_instantiation_internal_linkage) << D; |
9453 | return true; |
9454 | } |
9455 | |
9456 | |
9457 | |
9458 | |
9459 | if (CheckExplicitInstantiationScope(S, D, InstLoc, WasQualifiedName)) |
9460 | return true; |
9461 | |
9462 | return false; |
9463 | } |
9464 | |
9465 | |
9466 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
9467 | if (!SS.isSet()) |
9468 | return false; |
9469 | |
9470 | |
9471 | |
9472 | |
9473 | |
9474 | |
9475 | |
9476 | |
9477 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
9478 | NNS = NNS->getPrefix()) |
9479 | if (const Type *T = NNS->getAsType()) |
9480 | if (isa<TemplateSpecializationType>(T)) |
9481 | return true; |
9482 | |
9483 | return false; |
9484 | } |
9485 | |
9486 | |
9487 | |
9488 | static void dllExportImportClassTemplateSpecialization( |
9489 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
9490 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
9491 | assert(A && "dllExportImportClassTemplateSpecialization called " |
9492 | "on Def without dllexport or dllimport"); |
9493 | |
9494 | |
9495 | |
9496 | assert(S.DelayedDllExportClasses.empty() && |
9497 | "delayed exports present at explicit instantiation"); |
9498 | S.checkClassLevelDLLAttribute(Def); |
9499 | |
9500 | |
9501 | for (auto &B : Def->bases()) { |
9502 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
9503 | B.getType()->getAsCXXRecordDecl())) |
9504 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); |
9505 | } |
9506 | |
9507 | S.referenceDLLExportedClassMethods(); |
9508 | } |
9509 | |
9510 | |
9511 | DeclResult Sema::ActOnExplicitInstantiation( |
9512 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, |
9513 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, |
9514 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, |
9515 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, |
9516 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { |
9517 | |
9518 | TemplateName Name = TemplateD.get(); |
9519 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
9520 | |
9521 | |
9522 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
9523 | assert(Kind != TTK_Enum && |
9524 | "Invalid enum tag in class template explicit instantiation!"); |
9525 | |
9526 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
9527 | |
9528 | if (!ClassTemplate) { |
9529 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
9530 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
9531 | Diag(TD->getLocation(), diag::note_previous_use); |
9532 | return true; |
9533 | } |
9534 | |
9535 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
9536 | Kind, false, KWLoc, |
9537 | ClassTemplate->getIdentifier())) { |
9538 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
9539 | << ClassTemplate |
9540 | << FixItHint::CreateReplacement(KWLoc, |
9541 | ClassTemplate->getTemplatedDecl()->getKindName()); |
9542 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
9543 | diag::note_previous_use); |
9544 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
9545 | } |
9546 | |
9547 | |
9548 | |
9549 | |
9550 | |
9551 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
9552 | ? TSK_ExplicitInstantiationDefinition |
9553 | : TSK_ExplicitInstantiationDeclaration; |
9554 | |
9555 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
9556 | !Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { |
9557 | |
9558 | |
9559 | for (const ParsedAttr &AL : Attr) { |
9560 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
9561 | Diag(ExternLoc, |
9562 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
9563 | Diag(AL.getLoc(), diag::note_attribute); |
9564 | break; |
9565 | } |
9566 | } |
9567 | |
9568 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
9569 | Diag(ExternLoc, |
9570 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
9571 | Diag(A->getLocation(), diag::note_attribute); |
9572 | } |
9573 | } |
9574 | |
9575 | |
9576 | |
9577 | bool DLLImportExplicitInstantiationDef = false; |
9578 | if (TSK == TSK_ExplicitInstantiationDefinition && |
9579 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
9580 | |
9581 | bool DLLImport = |
9582 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
9583 | for (const ParsedAttr &AL : Attr) { |
9584 | if (AL.getKind() == ParsedAttr::AT_DLLImport) |
9585 | DLLImport = true; |
9586 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
9587 | |
9588 | DLLImport = false; |
9589 | break; |
9590 | } |
9591 | } |
9592 | if (DLLImport) { |
9593 | TSK = TSK_ExplicitInstantiationDeclaration; |
9594 | DLLImportExplicitInstantiationDef = true; |
9595 | } |
9596 | } |
9597 | |
9598 | |
9599 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
9600 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
9601 | |
9602 | |
9603 | |
9604 | SmallVector<TemplateArgument, 4> Converted; |
9605 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
9606 | TemplateArgs, false, Converted, |
9607 | true)) |
9608 | return true; |
9609 | |
9610 | |
9611 | |
9612 | void *InsertPos = nullptr; |
9613 | ClassTemplateSpecializationDecl *PrevDecl |
9614 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
9615 | |
9616 | TemplateSpecializationKind PrevDecl_TSK |
9617 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
9618 | |
9619 | if (TSK == TSK_ExplicitInstantiationDefinition && PrevDecl != nullptr && |
9620 | Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { |
9621 | |
9622 | |
9623 | for (const ParsedAttr &AL : Attr) { |
9624 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
9625 | Diag(AL.getLoc(), |
9626 | diag::warn_attribute_dllexport_explicit_instantiation_def); |
9627 | break; |
9628 | } |
9629 | } |
9630 | } |
9631 | |
9632 | if (CheckExplicitInstantiation(*this, ClassTemplate, TemplateNameLoc, |
9633 | SS.isSet(), TSK)) |
9634 | return true; |
9635 | |
9636 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
9637 | |
9638 | bool HasNoEffect = false; |
9639 | if (PrevDecl) { |
9640 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
9641 | PrevDecl, PrevDecl_TSK, |
9642 | PrevDecl->getPointOfInstantiation(), |
9643 | HasNoEffect)) |
9644 | return PrevDecl; |
9645 | |
9646 | |
9647 | |
9648 | |
9649 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
9650 | PrevDecl_TSK == TSK_Undeclared) { |
9651 | |
9652 | |
9653 | |
9654 | |
9655 | |
9656 | Specialization = PrevDecl; |
9657 | Specialization->setLocation(TemplateNameLoc); |
9658 | PrevDecl = nullptr; |
9659 | } |
9660 | |
9661 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
9662 | DLLImportExplicitInstantiationDef) { |
9663 | |
9664 | HasNoEffect = false; |
9665 | } |
9666 | } |
9667 | |
9668 | if (!Specialization) { |
9669 | |
9670 | |
9671 | Specialization |
9672 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
9673 | ClassTemplate->getDeclContext(), |
9674 | KWLoc, TemplateNameLoc, |
9675 | ClassTemplate, |
9676 | Converted, |
9677 | PrevDecl); |
9678 | SetNestedNameSpecifier(*this, Specialization, SS); |
9679 | |
9680 | if (!HasNoEffect && !PrevDecl) { |
9681 | |
9682 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
9683 | } |
9684 | } |
9685 | |
9686 | |
9687 | |
9688 | |
9689 | |
9690 | |
9691 | |
9692 | |
9693 | TypeSourceInfo *WrittenTy |
9694 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
9695 | TemplateArgs, |
9696 | Context.getTypeDeclType(Specialization)); |
9697 | Specialization->setTypeAsWritten(WrittenTy); |
9698 | |
9699 | |
9700 | Specialization->setExternLoc(ExternLoc); |
9701 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
9702 | Specialization->setBraceRange(SourceRange()); |
9703 | |
9704 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
9705 | ProcessDeclAttributeList(S, Specialization, Attr); |
9706 | |
9707 | |
9708 | |
9709 | |
9710 | Specialization->setLexicalDeclContext(CurContext); |
9711 | CurContext->addDecl(Specialization); |
9712 | |
9713 | |
9714 | if (HasNoEffect) { |
9715 | |
9716 | Specialization->setTemplateSpecializationKind(TSK); |
9717 | return Specialization; |
9718 | } |
9719 | |
9720 | |
9721 | |
9722 | |
9723 | |
9724 | |
9725 | |
9726 | |
9727 | ClassTemplateSpecializationDecl *Def |
9728 | = cast_or_null<ClassTemplateSpecializationDecl>( |
9729 | Specialization->getDefinition()); |
9730 | if (!Def) |
9731 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
9732 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
9733 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
9734 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
9735 | } |
9736 | |
9737 | |
9738 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
9739 | Specialization->getDefinition()); |
9740 | if (Def) { |
9741 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
9742 | |
9743 | |
9744 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
9745 | (TSK == TSK_ExplicitInstantiationDefinition || |
9746 | DLLImportExplicitInstantiationDef)) { |
9747 | |
9748 | Def->setTemplateSpecializationKind(TSK); |
9749 | |
9750 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
9751 | (Context.getTargetInfo().shouldDLLImportComdatSymbols() && |
9752 | !Context.getTargetInfo().getTriple().isPS4CPU())) { |
9753 | |
9754 | |
9755 | |
9756 | auto *A = cast<InheritableAttr>( |
9757 | getDLLAttr(Specialization)->clone(getASTContext())); |
9758 | A->setInherited(true); |
9759 | Def->addAttr(A); |
9760 | dllExportImportClassTemplateSpecialization(*this, Def); |
9761 | } |
9762 | } |
9763 | |
9764 | |
9765 | |
9766 | bool NewlyDLLExported = |
9767 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
9768 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
9769 | (Context.getTargetInfo().shouldDLLImportComdatSymbols() && |
9770 | !Context.getTargetInfo().getTriple().isPS4CPU())) { |
9771 | |
9772 | |
9773 | |
9774 | |
9775 | |
9776 | |
9777 | |
9778 | |
9779 | |
9780 | |
9781 | |
9782 | |
9783 | assert(Def == Specialization && |
9784 | "Def and Specialization should match for implicit instantiation"); |
9785 | dllExportImportClassTemplateSpecialization(*this, Def); |
9786 | } |
9787 | |
9788 | |
9789 | |
9790 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
9791 | Context.getTargetInfo().getTriple().isWindowsGNUEnvironment() && |
9792 | PrevDecl->hasAttr<DLLExportAttr>()) { |
9793 | dllExportImportClassTemplateSpecialization(*this, Def); |
9794 | } |
9795 | |
9796 | if (Def->hasAttr<MSInheritanceAttr>()) { |
9797 | Specialization->addAttr(Def->getAttr<MSInheritanceAttr>()); |
9798 | Consumer.AssignInheritanceModel(Specialization); |
9799 | } |
9800 | |
9801 | |
9802 | |
9803 | Specialization->setTemplateSpecializationKind(TSK); |
9804 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
9805 | } else { |
9806 | |
9807 | |
9808 | Specialization->setTemplateSpecializationKind(TSK); |
9809 | } |
9810 | |
9811 | return Specialization; |
9812 | } |
9813 | |
9814 | |
9815 | DeclResult |
9816 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, |
9817 | SourceLocation TemplateLoc, unsigned TagSpec, |
9818 | SourceLocation KWLoc, CXXScopeSpec &SS, |
9819 | IdentifierInfo *Name, SourceLocation NameLoc, |
9820 | const ParsedAttributesView &Attr) { |
9821 | |
9822 | bool Owned = false; |
9823 | bool IsDependent = false; |
9824 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
9825 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
9826 | SourceLocation(), |
9827 | MultiTemplateParamsArg(), Owned, IsDependent, |
9828 | SourceLocation(), false, TypeResult(), |
9829 | false, |
9830 | false); |
9831 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
9832 | |
9833 | if (!TagD) |
9834 | return true; |
9835 | |
9836 | TagDecl *Tag = cast<TagDecl>(TagD); |
9837 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
9838 | |
9839 | if (Tag->isInvalidDecl()) |
9840 | return true; |
9841 | |
9842 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
9843 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
9844 | if (!Pattern) { |
9845 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
9846 | << Context.getTypeDeclType(Record); |
9847 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
9848 | return true; |
9849 | } |
9850 | |
9851 | |
9852 | |
9853 | |
9854 | |
9855 | |
9856 | |
9857 | if (!ScopeSpecifierHasTemplateId(SS)) |
9858 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
9859 | << Record << SS.getRange(); |
9860 | |
9861 | |
9862 | |
9863 | |
9864 | |
9865 | TemplateSpecializationKind TSK |
9866 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
9867 | : TSK_ExplicitInstantiationDeclaration; |
9868 | |
9869 | CheckExplicitInstantiation(*this, Record, NameLoc, true, TSK); |
9870 | |
9871 | |
9872 | CXXRecordDecl *PrevDecl |
9873 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
9874 | if (!PrevDecl && Record->getDefinition()) |
9875 | PrevDecl = Record; |
9876 | if (PrevDecl) { |
9877 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
9878 | bool HasNoEffect = false; |
9879 | assert(MSInfo && "No member specialization information?"); |
9880 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
9881 | PrevDecl, |
9882 | MSInfo->getTemplateSpecializationKind(), |
9883 | MSInfo->getPointOfInstantiation(), |
9884 | HasNoEffect)) |
9885 | return true; |
9886 | if (HasNoEffect) |
9887 | return TagD; |
9888 | } |
9889 | |
9890 | CXXRecordDecl *RecordDef |
9891 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
9892 | if (!RecordDef) { |
9893 | |
9894 | |
9895 | |
9896 | CXXRecordDecl *Def |
9897 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
9898 | if (!Def) { |
9899 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
9900 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
9901 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
9902 | << Pattern; |
9903 | return true; |
9904 | } else { |
9905 | if (InstantiateClass(NameLoc, Record, Def, |
9906 | getTemplateInstantiationArgs(Record), |
9907 | TSK)) |
9908 | return true; |
9909 | |
9910 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
9911 | if (!RecordDef) |
9912 | return true; |
9913 | } |
9914 | } |
9915 | |
9916 | |
9917 | InstantiateClassMembers(NameLoc, RecordDef, |
9918 | getTemplateInstantiationArgs(Record), TSK); |
9919 | |
9920 | if (TSK == TSK_ExplicitInstantiationDefinition) |
9921 | MarkVTableUsed(NameLoc, RecordDef, true); |
9922 | |
9923 | |
9924 | |
9925 | |
9926 | |
9927 | return TagD; |
9928 | } |
9929 | |
9930 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
9931 | SourceLocation ExternLoc, |
9932 | SourceLocation TemplateLoc, |
9933 | Declarator &D) { |
9934 | |
9935 | |
9936 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
9937 | DeclarationName Name = NameInfo.getName(); |
9938 | if (!Name) { |
9939 | if (!D.isInvalidType()) |
9940 | Diag(D.getDeclSpec().getBeginLoc(), |
9941 | diag::err_explicit_instantiation_requires_name) |
9942 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
9943 | |
9944 | return true; |
9945 | } |
9946 | |
9947 | |
9948 | |
9949 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
9950 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
9951 | S = S->getParent(); |
9952 | |
9953 | |
9954 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
9955 | QualType R = T->getType(); |
9956 | if (R.isNull()) |
9957 | return true; |
9958 | |
9959 | |
9960 | |
9961 | |
9962 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
9963 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
9964 | << Name; |
9965 | return true; |
9966 | } else if (D.getDeclSpec().getStorageClassSpec() |
9967 | != DeclSpec::SCS_unspecified) { |
9968 | |
9969 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
9970 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
9971 | |
9972 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
9973 | } |
9974 | |
9975 | |
9976 | |
9977 | |
9978 | |
9979 | |
9980 | if (D.getDeclSpec().isInlineSpecified()) |
9981 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
9982 | getLangOpts().CPlusPlus11 ? |
9983 | diag::err_explicit_instantiation_inline : |
9984 | diag::warn_explicit_instantiation_inline_0x) |
9985 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
9986 | if (D.getDeclSpec().hasConstexprSpecifier() && R->isFunctionType()) |
9987 | |
9988 | |
9989 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
9990 | diag::err_explicit_instantiation_constexpr); |
9991 | |
9992 | |
9993 | |
9994 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
9995 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) |
9996 | << 0; |
9997 | return true; |
9998 | } |
9999 | |
10000 | |
10001 | |
10002 | |
10003 | |
10004 | TemplateSpecializationKind TSK |
10005 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
10006 | : TSK_ExplicitInstantiationDeclaration; |
10007 | |
10008 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
10009 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
10010 | |
10011 | if (!R->isFunctionType()) { |
10012 | |
10013 | |
10014 | |
10015 | |
10016 | |
10017 | |
10018 | |
10019 | if (Previous.isAmbiguous()) |
10020 | return true; |
10021 | |
10022 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
10023 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
10024 | |
10025 | if (!PrevTemplate) { |
10026 | if (!Prev || !Prev->isStaticDataMember()) { |
10027 | |
10028 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
10029 | << Name; |
10030 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
10031 | P != PEnd; ++P) |
10032 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
10033 | return true; |
10034 | } |
10035 | |
10036 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
10037 | |
10038 | Diag(D.getIdentifierLoc(), |
10039 | diag::err_explicit_instantiation_data_member_not_instantiated) |
10040 | << Prev; |
10041 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
10042 | |
10043 | return true; |
10044 | } |
10045 | } else { |
10046 | |
10047 | |
10048 | |
10049 | |
10050 | |
10051 | |
10052 | |
10053 | if (R->isUndeducedType()) { |
10054 | Diag(T->getTypeLoc().getBeginLoc(), |
10055 | diag::err_auto_not_allowed_var_inst); |
10056 | return true; |
10057 | } |
10058 | |
10059 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
10060 | |
10061 | |
10062 | |
10063 | Diag(D.getIdentifierLoc(), |
10064 | diag::err_explicit_instantiation_without_template_id) |
10065 | << PrevTemplate; |
10066 | Diag(PrevTemplate->getLocation(), |
10067 | diag::note_explicit_instantiation_here); |
10068 | return true; |
10069 | } |
10070 | |
10071 | |
10072 | TemplateArgumentListInfo TemplateArgs = |
10073 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
10074 | |
10075 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
10076 | D.getIdentifierLoc(), TemplateArgs); |
10077 | if (Res.isInvalid()) |
10078 | return true; |
10079 | |
10080 | if (!Res.isUsable()) { |
10081 | |
10082 | |
10083 | |
10084 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_dependent); |
10085 | return true; |
10086 | } |
10087 | |
10088 | |
10089 | |
10090 | Prev = cast<VarDecl>(Res.get()); |
10091 | } |
10092 | |
10093 | |
10094 | |
10095 | |
10096 | |
10097 | |
10098 | |
10099 | |
10100 | |
10101 | |
10102 | |
10103 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
10104 | Diag(D.getIdentifierLoc(), |
10105 | diag::ext_explicit_instantiation_without_qualified_id) |
10106 | << Prev << D.getCXXScopeSpec().getRange(); |
10107 | |
10108 | CheckExplicitInstantiation(*this, Prev, D.getIdentifierLoc(), true, TSK); |
10109 | |
10110 | |
10111 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
10112 | SourceLocation POI = Prev->getPointOfInstantiation(); |
10113 | bool HasNoEffect = false; |
10114 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
10115 | PrevTSK, POI, HasNoEffect)) |
10116 | return true; |
10117 | |
10118 | if (!HasNoEffect) { |
10119 | |
10120 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
10121 | |
10122 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); |
10123 | if (TSK == TSK_ExplicitInstantiationDefinition) |
10124 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
10125 | } |
10126 | |
10127 | |
10128 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
10129 | Diag(T->getTypeLoc().getBeginLoc(), |
10130 | diag::err_invalid_var_template_spec_type) |
10131 | << 0 << PrevTemplate << R << Prev->getType(); |
10132 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
10133 | << 2 << PrevTemplate->getDeclName(); |
10134 | return true; |
10135 | } |
10136 | |
10137 | |
10138 | return (Decl*) nullptr; |
10139 | } |
10140 | |
10141 | |
10142 | |
10143 | bool HasExplicitTemplateArgs = false; |
10144 | TemplateArgumentListInfo TemplateArgs; |
10145 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
10146 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
10147 | HasExplicitTemplateArgs = true; |
10148 | } |
10149 | |
10150 | |
10151 | |
10152 | |
10153 | |
10154 | |
10155 | UnresolvedSet<8> TemplateMatches; |
10156 | FunctionDecl *NonTemplateMatch = nullptr; |
10157 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
10158 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
10159 | P != PEnd; ++P) { |
10160 | NamedDecl *Prev = *P; |
10161 | if (!HasExplicitTemplateArgs) { |
10162 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
10163 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
10164 | true); |
10165 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
10166 | if (Method->getPrimaryTemplate()) { |
10167 | TemplateMatches.addDecl(Method, P.getAccess()); |
10168 | } else { |
10169 | |
10170 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
10171 | NonTemplateMatch = Method; |
10172 | } |
10173 | } |
10174 | } |
10175 | } |
10176 | |
10177 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
10178 | if (!FunTmpl) |
10179 | continue; |
10180 | |
10181 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
10182 | FunctionDecl *Specialization = nullptr; |
10183 | if (TemplateDeductionResult TDK |
10184 | = DeduceTemplateArguments(FunTmpl, |
10185 | (HasExplicitTemplateArgs ? &TemplateArgs |
10186 | : nullptr), |
10187 | R, Specialization, Info)) { |
10188 | |
10189 | FailedCandidates.addCandidate() |
10190 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
10191 | MakeDeductionFailureInfo(Context, TDK, Info)); |
10192 | (void)TDK; |
10193 | continue; |
10194 | } |
10195 | |
10196 | |
10197 | |
10198 | |
10199 | |
10200 | |
10201 | if (LangOpts.CUDA && |
10202 | IdentifyCUDATarget(Specialization, |
10203 | true) != |
10204 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { |
10205 | FailedCandidates.addCandidate().set( |
10206 | P.getPair(), FunTmpl->getTemplatedDecl(), |
10207 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
10208 | continue; |
10209 | } |
10210 | |
10211 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
10212 | } |
10213 | |
10214 | FunctionDecl *Specialization = NonTemplateMatch; |
10215 | if (!Specialization) { |
10216 | |
10217 | UnresolvedSetIterator Result = getMostSpecialized( |
10218 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
10219 | D.getIdentifierLoc(), |
10220 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
10221 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
10222 | PDiag(diag::note_explicit_instantiation_candidate)); |
10223 | |
10224 | if (Result == TemplateMatches.end()) |
10225 | return true; |
10226 | |
10227 | |
10228 | Specialization = cast<FunctionDecl>(*Result); |
10229 | } |
10230 | |
10231 | |
10232 | |
10233 | |
10234 | |
10235 | |
10236 | |
10237 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
10238 | if (FPT->hasExceptionSpec()) { |
10239 | unsigned DiagID = |
10240 | diag::err_mismatched_exception_spec_explicit_instantiation; |
10241 | if (getLangOpts().MicrosoftExt) |
10242 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
10243 | bool Result = CheckEquivalentExceptionSpec( |
10244 | PDiag(DiagID) << Specialization->getType(), |
10245 | PDiag(diag::note_explicit_instantiation_here), |
10246 | Specialization->getType()->getAs<FunctionProtoType>(), |
10247 | Specialization->getLocation(), FPT, D.getBeginLoc()); |
10248 | |
10249 | |
10250 | if (!getLangOpts().MicrosoftExt && Result) |
10251 | return true; |
10252 | } |
10253 | |
10254 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
10255 | Diag(D.getIdentifierLoc(), |
10256 | diag::err_explicit_instantiation_member_function_not_instantiated) |
10257 | << Specialization |
10258 | << (Specialization->getTemplateSpecializationKind() == |
10259 | TSK_ExplicitSpecialization); |
10260 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
10261 | return true; |
10262 | } |
10263 | |
10264 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
10265 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
10266 | PrevDecl = Specialization; |
10267 | |
10268 | if (PrevDecl) { |
10269 | bool HasNoEffect = false; |
10270 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
10271 | PrevDecl, |
10272 | PrevDecl->getTemplateSpecializationKind(), |
10273 | PrevDecl->getPointOfInstantiation(), |
10274 | HasNoEffect)) |
10275 | return true; |
10276 | |
10277 | |
10278 | |
10279 | if (HasNoEffect) |
10280 | return (Decl*) nullptr; |
10281 | } |
10282 | |
10283 | |
10284 | |
10285 | |
10286 | |
10287 | |
10288 | |
10289 | if (Specialization->hasAttr<InternalLinkageAttr>() && |
10290 | TSK == TSK_ExplicitInstantiationDeclaration) { |
10291 | if (auto *RD = dyn_cast<CXXRecordDecl>(Specialization->getDeclContext())) |
10292 | if (RD->getIdentifier() && RD->getIdentifier()->isStr("valarray") && |
10293 | RD->isInStdNamespace()) |
10294 | return (Decl*) nullptr; |
10295 | } |
10296 | |
10297 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); |
10298 | |
10299 | |
10300 | |
10301 | if (TSK == TSK_ExplicitInstantiationDefinition && |
10302 | Specialization->hasAttr<DLLImportAttr>() && |
10303 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
10304 | TSK = TSK_ExplicitInstantiationDeclaration; |
10305 | |
10306 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
10307 | |
10308 | if (Specialization->isDefined()) { |
10309 | |
10310 | |
10311 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
10312 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
10313 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
10314 | |
10315 | |
10316 | |
10317 | |
10318 | |
10319 | |
10320 | |
10321 | |
10322 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
10323 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
10324 | D.getCXXScopeSpec().isSet() && |
10325 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
10326 | Diag(D.getIdentifierLoc(), |
10327 | diag::ext_explicit_instantiation_without_qualified_id) |
10328 | << Specialization << D.getCXXScopeSpec().getRange(); |
10329 | |
10330 | CheckExplicitInstantiation( |
10331 | *this, |
10332 | FunTmpl ? (NamedDecl *)FunTmpl |
10333 | : Specialization->getInstantiatedFromMemberFunction(), |
10334 | D.getIdentifierLoc(), D.getCXXScopeSpec().isSet(), TSK); |
10335 | |
10336 | |
10337 | return (Decl*) nullptr; |
10338 | } |
10339 | |
10340 | TypeResult |
10341 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
10342 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
10343 | SourceLocation TagLoc, SourceLocation NameLoc) { |
10344 | |
10345 | assert(Name && "Expected a name in a dependent tag"); |
10346 | |
10347 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
10348 | if (!NNS) |
10349 | return true; |
10350 | |
10351 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
10352 | |
10353 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
10354 | Diag(NameLoc, diag::err_dependent_tag_decl) |
10355 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
10356 | return true; |
10357 | } |
10358 | |
10359 | |
10360 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
10361 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
10362 | |
10363 | |
10364 | TypeLocBuilder TLB; |
10365 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
10366 | TL.setElaboratedKeywordLoc(TagLoc); |
10367 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
10368 | TL.setNameLoc(NameLoc); |
10369 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
10370 | } |
10371 | |
10372 | TypeResult |
10373 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
10374 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
10375 | SourceLocation IdLoc) { |
10376 | if (SS.isInvalid()) |
10377 | return true; |
10378 | |
10379 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
10380 | Diag(TypenameLoc, |
10381 | getLangOpts().CPlusPlus11 ? |
10382 | diag::warn_cxx98_compat_typename_outside_of_template : |
10383 | diag::ext_typename_outside_of_template) |
10384 | << FixItHint::CreateRemoval(TypenameLoc); |
10385 | |
10386 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
10387 | TypeSourceInfo *TSI = nullptr; |
10388 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
10389 | TypenameLoc, QualifierLoc, II, IdLoc, &TSI, |
10390 | true); |
10391 | if (T.isNull()) |
10392 | return true; |
10393 | return CreateParsedType(T, TSI); |
10394 | } |
10395 | |
10396 | TypeResult |
10397 | Sema::ActOnTypenameType(Scope *S, |
10398 | SourceLocation TypenameLoc, |
10399 | const CXXScopeSpec &SS, |
10400 | SourceLocation TemplateKWLoc, |
10401 | TemplateTy TemplateIn, |
10402 | IdentifierInfo *TemplateII, |
10403 | SourceLocation TemplateIILoc, |
10404 | SourceLocation LAngleLoc, |
10405 | ASTTemplateArgsPtr TemplateArgsIn, |
10406 | SourceLocation RAngleLoc) { |
10407 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
10408 | Diag(TypenameLoc, |
10409 | getLangOpts().CPlusPlus11 ? |
10410 | diag::warn_cxx98_compat_typename_outside_of_template : |
10411 | diag::ext_typename_outside_of_template) |
10412 | << FixItHint::CreateRemoval(TypenameLoc); |
10413 | |
10414 | |
10415 | |
10416 | if (TypenameLoc.isValid()) { |
10417 | auto *LookupRD = |
10418 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
10419 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
10420 | Diag(TemplateIILoc, |
10421 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
10422 | << TemplateII << 0 |
10423 | << (TemplateKWLoc.isValid() ? 1 : 0 ); |
10424 | } |
10425 | } |
10426 | |
10427 | |
10428 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
10429 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
10430 | |
10431 | TemplateName Template = TemplateIn.get(); |
10432 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
10433 | |
10434 | assert(DTN && "dependent template has non-dependent name?"); |
10435 | assert(DTN->getQualifier() == SS.getScopeRep()); |
10436 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
10437 | DTN->getQualifier(), |
10438 | DTN->getIdentifier(), |
10439 | TemplateArgs); |
10440 | |
10441 | |
10442 | TypeLocBuilder Builder; |
10443 | DependentTemplateSpecializationTypeLoc SpecTL |
10444 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
10445 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
10446 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
10447 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
10448 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
10449 | SpecTL.setLAngleLoc(LAngleLoc); |
10450 | SpecTL.setRAngleLoc(RAngleLoc); |
10451 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
10452 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
10453 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
10454 | } |
10455 | |
10456 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
10457 | if (T.isNull()) |
10458 | return true; |
10459 | |
10460 | |
10461 | TypeLocBuilder Builder; |
10462 | TemplateSpecializationTypeLoc SpecTL |
10463 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
10464 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
10465 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
10466 | SpecTL.setLAngleLoc(LAngleLoc); |
10467 | SpecTL.setRAngleLoc(RAngleLoc); |
10468 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
10469 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
10470 | |
10471 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
10472 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
10473 | TL.setElaboratedKeywordLoc(TypenameLoc); |
10474 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
10475 | |
10476 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
10477 | return CreateParsedType(T, TSI); |
10478 | } |
10479 | |
10480 | |
10481 | |
10482 | |
10483 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
10484 | SourceRange &CondRange, Expr *&Cond) { |
10485 | |
10486 | if (!II.isStr("type")) |
10487 | return false; |
10488 | |
10489 | |
10490 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
10491 | return false; |
10492 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
10493 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
10494 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
10495 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
10496 | return false; |
10497 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
10498 | |
10499 | |
10500 | const TemplateDecl *EnableIfDecl = |
10501 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
10502 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
10503 | return false; |
10504 | |
10505 | |
10506 | const IdentifierInfo *EnableIfII = |
10507 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
10508 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
10509 | return false; |
10510 | |
10511 | |
10512 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
10513 | |
10514 | |
10515 | Cond = nullptr; |
10516 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
10517 | != TemplateArgument::Expression) |
10518 | return true; |
10519 | |
10520 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
10521 | |
10522 | |
10523 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
10524 | Cond = nullptr; |
10525 | |
10526 | return true; |
10527 | } |
10528 | |
10529 | QualType |
10530 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
10531 | SourceLocation KeywordLoc, |
10532 | NestedNameSpecifierLoc QualifierLoc, |
10533 | const IdentifierInfo &II, |
10534 | SourceLocation IILoc, |
10535 | TypeSourceInfo **TSI, |
10536 | bool DeducedTSTContext) { |
10537 | QualType T = CheckTypenameType(Keyword, KeywordLoc, QualifierLoc, II, IILoc, |
10538 | DeducedTSTContext); |
10539 | if (T.isNull()) |
10540 | return QualType(); |
10541 | |
10542 | *TSI = Context.CreateTypeSourceInfo(T); |
10543 | if (isa<DependentNameType>(T)) { |
10544 | DependentNameTypeLoc TL = |
10545 | (*TSI)->getTypeLoc().castAs<DependentNameTypeLoc>(); |
10546 | TL.setElaboratedKeywordLoc(KeywordLoc); |
10547 | TL.setQualifierLoc(QualifierLoc); |
10548 | TL.setNameLoc(IILoc); |
10549 | } else { |
10550 | ElaboratedTypeLoc TL = (*TSI)->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
10551 | TL.setElaboratedKeywordLoc(KeywordLoc); |
10552 | TL.setQualifierLoc(QualifierLoc); |
10553 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IILoc); |
10554 | } |
10555 | return T; |
10556 | } |
10557 | |
10558 | |
10559 | |
10560 | QualType |
10561 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
10562 | SourceLocation KeywordLoc, |
10563 | NestedNameSpecifierLoc QualifierLoc, |
10564 | const IdentifierInfo &II, |
10565 | SourceLocation IILoc, bool DeducedTSTContext) { |
10566 | CXXScopeSpec SS; |
10567 | SS.Adopt(QualifierLoc); |
10568 | |
10569 | DeclContext *Ctx = nullptr; |
10570 | if (QualifierLoc) { |
10571 | Ctx = computeDeclContext(SS); |
10572 | if (!Ctx) { |
10573 | |
10574 | |
10575 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
10576 | return Context.getDependentNameType(Keyword, |
10577 | QualifierLoc.getNestedNameSpecifier(), |
10578 | &II); |
10579 | } |
10580 | |
10581 | |
10582 | |
10583 | |
10584 | |
10585 | |
10586 | |
10587 | if (RequireCompleteDeclContext(SS, Ctx)) |
10588 | return QualType(); |
10589 | } |
10590 | |
10591 | DeclarationName Name(&II); |
10592 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
10593 | if (Ctx) |
10594 | LookupQualifiedName(Result, Ctx, SS); |
10595 | else |
10596 | LookupName(Result, CurScope); |
10597 | unsigned DiagID = 0; |
10598 | Decl *Referenced = nullptr; |
10599 | switch (Result.getResultKind()) { |
10600 | case LookupResult::NotFound: { |
10601 | |
10602 | |
10603 | SourceRange CondRange; |
10604 | Expr *Cond = nullptr; |
10605 | if (Ctx && isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
10606 | |
10607 | |
10608 | if (Cond) { |
10609 | Expr *FailedCond; |
10610 | std::string FailedDescription; |
10611 | std::tie(FailedCond, FailedDescription) = |
10612 | findFailedBooleanCondition(Cond); |
10613 | |
10614 | Diag(FailedCond->getExprLoc(), |
10615 | diag::err_typename_nested_not_found_requirement) |
10616 | << FailedDescription |
10617 | << FailedCond->getSourceRange(); |
10618 | return QualType(); |
10619 | } |
10620 | |
10621 | Diag(CondRange.getBegin(), |
10622 | diag::err_typename_nested_not_found_enable_if) |
10623 | << Ctx << CondRange; |
10624 | return QualType(); |
10625 | } |
10626 | |
10627 | DiagID = Ctx ? diag::err_typename_nested_not_found |
10628 | : diag::err_unknown_typename; |
10629 | break; |
10630 | } |
10631 | |
10632 | case LookupResult::FoundUnresolvedValue: { |
10633 | |
10634 | |
10635 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
10636 | IILoc); |
10637 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
10638 | << Name << Ctx << FullRange; |
10639 | if (UnresolvedUsingValueDecl *Using |
10640 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
10641 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
10642 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
10643 | << FixItHint::CreateInsertion(Loc, "typename "); |
10644 | } |
10645 | } |
10646 | |
10647 | |
10648 | LLVM_FALLTHROUGH; |
10649 | |
10650 | case LookupResult::NotFoundInCurrentInstantiation: |
10651 | |
10652 | return Context.getDependentNameType(Keyword, |
10653 | QualifierLoc.getNestedNameSpecifier(), |
10654 | &II); |
10655 | |
10656 | case LookupResult::Found: |
10657 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
10658 | |
10659 | |
10660 | |
10661 | |
10662 | |
10663 | |
10664 | |
10665 | |
10666 | |
10667 | |
10668 | |
10669 | |
10670 | |
10671 | |
10672 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
10673 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
10674 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
10675 | FoundRD->isInjectedClassName() && |
10676 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
10677 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
10678 | << &II << 1 << 0 ; |
10679 | |
10680 | |
10681 | |
10682 | MarkAnyDeclReferenced(Type->getLocation(), Type, false); |
10683 | return Context.getElaboratedType(Keyword, |
10684 | QualifierLoc.getNestedNameSpecifier(), |
10685 | Context.getTypeDeclType(Type)); |
10686 | } |
10687 | |
10688 | |
10689 | |
10690 | |
10691 | |
10692 | if (getLangOpts().CPlusPlus17) { |
10693 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
10694 | if (!DeducedTSTContext) { |
10695 | QualType T(QualifierLoc |
10696 | ? QualifierLoc.getNestedNameSpecifier()->getAsType() |
10697 | : nullptr, 0); |
10698 | if (!T.isNull()) |
10699 | Diag(IILoc, diag::err_dependent_deduced_tst) |
10700 | << (int)getTemplateNameKindForDiagnostics(TemplateName(TD)) << T; |
10701 | else |
10702 | Diag(IILoc, diag::err_deduced_tst) |
10703 | << (int)getTemplateNameKindForDiagnostics(TemplateName(TD)); |
10704 | Diag(TD->getLocation(), diag::note_template_decl_here); |
10705 | return QualType(); |
10706 | } |
10707 | return Context.getElaboratedType( |
10708 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
10709 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
10710 | QualType(), false)); |
10711 | } |
10712 | } |
10713 | |
10714 | DiagID = Ctx ? diag::err_typename_nested_not_type |
10715 | : diag::err_typename_not_type; |
10716 | Referenced = Result.getFoundDecl(); |
10717 | break; |
10718 | |
10719 | case LookupResult::FoundOverloaded: |
10720 | DiagID = Ctx ? diag::err_typename_nested_not_type |
10721 | : diag::err_typename_not_type; |
10722 | Referenced = *Result.begin(); |
10723 | break; |
10724 | |
10725 | case LookupResult::Ambiguous: |
10726 | return QualType(); |
10727 | } |
10728 | |
10729 | |
10730 | |
10731 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
10732 | IILoc); |
10733 | if (Ctx) |
10734 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
10735 | else |
10736 | Diag(IILoc, DiagID) << FullRange << Name; |
10737 | if (Referenced) |
10738 | Diag(Referenced->getLocation(), |
10739 | Ctx ? diag::note_typename_member_refers_here |
10740 | : diag::note_typename_refers_here) |
10741 | << Name; |
10742 | return QualType(); |
10743 | } |
10744 | |
10745 | namespace { |
10746 | |
10747 | class CurrentInstantiationRebuilder |
10748 | : public TreeTransform<CurrentInstantiationRebuilder> { |
10749 | SourceLocation Loc; |
10750 | DeclarationName Entity; |
10751 | |
10752 | public: |
10753 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
10754 | |
10755 | CurrentInstantiationRebuilder(Sema &SemaRef, |
10756 | SourceLocation Loc, |
10757 | DeclarationName Entity) |
10758 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
10759 | Loc(Loc), Entity(Entity) { } |
10760 | |
10761 | |
10762 | |
10763 | |
10764 | |
10765 | |
10766 | bool AlreadyTransformed(QualType T) { |
10767 | return T.isNull() || !T->isInstantiationDependentType(); |
10768 | } |
10769 | |
10770 | |
10771 | |
10772 | SourceLocation getBaseLocation() { return Loc; } |
10773 | |
10774 | |
10775 | DeclarationName getBaseEntity() { return Entity; } |
10776 | |
10777 | |
10778 | |
10779 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
10780 | this->Loc = Loc; |
10781 | this->Entity = Entity; |
10782 | } |
10783 | |
10784 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
10785 | |
10786 | return E; |
10787 | } |
10788 | }; |
10789 | } |
10790 | |
10791 | |
10792 | |
10793 | |
10794 | |
10795 | |
10796 | |
10797 | |
10798 | |
10799 | |
10800 | |
10801 | |
10802 | |
10803 | |
10804 | |
10805 | |
10806 | |
10807 | |
10808 | |
10809 | |
10810 | |
10811 | |
10812 | |
10813 | |
10814 | |
10815 | |
10816 | |
10817 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
10818 | SourceLocation Loc, |
10819 | DeclarationName Name) { |
10820 | if (!T || !T->getType()->isInstantiationDependentType()) |
10821 | return T; |
10822 | |
10823 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
10824 | return Rebuilder.TransformType(T); |
10825 | } |
10826 | |
10827 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
10828 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
10829 | DeclarationName()); |
10830 | return Rebuilder.TransformExpr(E); |
10831 | } |
10832 | |
10833 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
10834 | if (SS.isInvalid()) |
10835 | return true; |
10836 | |
10837 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
10838 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
10839 | DeclarationName()); |
10840 | NestedNameSpecifierLoc Rebuilt |
10841 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
10842 | if (!Rebuilt) |
10843 | return true; |
10844 | |
10845 | SS.Adopt(Rebuilt); |
10846 | return false; |
10847 | } |
10848 | |
10849 | |
10850 | |
10851 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
10852 | TemplateParameterList *Params) { |
10853 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
10854 | Decl *Param = Params->getParam(I); |
10855 | |
10856 | |
10857 | if (isa<TemplateTypeParmDecl>(Param)) |
10858 | continue; |
10859 | |
10860 | |
10861 | if (TemplateTemplateParmDecl *TTP |
10862 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
10863 | if (RebuildTemplateParamsInCurrentInstantiation( |
10864 | TTP->getTemplateParameters())) |
10865 | return true; |
10866 | |
10867 | continue; |
10868 | } |
10869 | |
10870 | |
10871 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
10872 | TypeSourceInfo *NewTSI |
10873 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
10874 | NTTP->getLocation(), |
10875 | NTTP->getDeclName()); |
10876 | if (!NewTSI) |
10877 | return true; |
10878 | |
10879 | if (NewTSI->getType()->isUndeducedType()) { |
10880 | |
10881 | |
10882 | |
10883 | |
10884 | |
10885 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); |
10886 | } |
10887 | |
10888 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
10889 | NTTP->setTypeSourceInfo(NewTSI); |
10890 | NTTP->setType(NewTSI->getType()); |
10891 | } |
10892 | } |
10893 | |
10894 | return false; |
10895 | } |
10896 | |
10897 | |
10898 | |
10899 | std::string |
10900 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
10901 | const TemplateArgumentList &Args) { |
10902 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
10903 | } |
10904 | |
10905 | std::string |
10906 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
10907 | const TemplateArgument *Args, |
10908 | unsigned NumArgs) { |
10909 | SmallString<128> Str; |
10910 | llvm::raw_svector_ostream Out(Str); |
10911 | |
10912 | if (!Params || Params->size() == 0 || NumArgs == 0) |
10913 | return std::string(); |
10914 | |
10915 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
10916 | if (I >= NumArgs) |
10917 | break; |
10918 | |
10919 | if (I == 0) |
10920 | Out << "[with "; |
10921 | else |
10922 | Out << ", "; |
10923 | |
10924 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
10925 | Out << Id->getName(); |
10926 | } else { |
10927 | Out << '$' << I; |
10928 | } |
10929 | |
10930 | Out << " = "; |
10931 | Args[I].print( |
10932 | getPrintingPolicy(), Out, |
10933 | TemplateParameterList::shouldIncludeTypeForArgument(Params, I)); |
10934 | } |
10935 | |
10936 | Out << ']'; |
10937 | return std::string(Out.str()); |
10938 | } |
10939 | |
10940 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
10941 | CachedTokens &Toks) { |
10942 | if (!FD) |
10943 | return; |
10944 | |
10945 | auto LPT = std::make_unique<LateParsedTemplate>(); |
10946 | |
10947 | |
10948 | LPT->Toks.swap(Toks); |
10949 | LPT->D = FnD; |
10950 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
10951 | |
10952 | FD->setLateTemplateParsed(true); |
10953 | } |
10954 | |
10955 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
10956 | if (!FD) |
10957 | return; |
10958 | FD->setLateTemplateParsed(false); |
10959 | } |
10960 | |
10961 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
10962 | DeclContext *DC = CurContext; |
10963 | |
10964 | while (DC) { |
10965 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
10966 | const FunctionDecl *FD = RD->isLocalClass(); |
10967 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
10968 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
10969 | return false; |
10970 | |
10971 | DC = DC->getParent(); |
10972 | } |
10973 | return false; |
10974 | } |
10975 | |
10976 | namespace { |
10977 | |
10978 | |
10979 | |
10980 | |
10981 | |
10982 | |
10983 | |
10984 | |
10985 | |
10986 | |
10987 | |
10988 | |
10989 | |
10990 | |
10991 | |
10992 | |
10993 | |
10994 | class ExplicitSpecializationVisibilityChecker { |
10995 | Sema &S; |
10996 | SourceLocation Loc; |
10997 | llvm::SmallVector<Module *, 8> Modules; |
10998 | |
10999 | public: |
11000 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
11001 | : S(S), Loc(Loc) {} |
11002 | |
11003 | void check(NamedDecl *ND) { |
11004 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
11005 | return checkImpl(FD); |
11006 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
11007 | return checkImpl(RD); |
11008 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
11009 | return checkImpl(VD); |
11010 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
11011 | return checkImpl(ED); |
11012 | } |
11013 | |
11014 | private: |
11015 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
11016 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
11017 | : Sema::MissingImportKind::ExplicitSpecialization; |
11018 | const bool Recover = true; |
11019 | |
11020 | |
11021 | |
11022 | |
11023 | if (Modules.empty()) |
11024 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
11025 | else |
11026 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
11027 | } |
11028 | |
11029 | |
11030 | |
11031 | |
11032 | |
11033 | |
11034 | |
11035 | |
11036 | |
11037 | |
11038 | |
11039 | |
11040 | |
11041 | template<typename SpecDecl> |
11042 | void checkImpl(SpecDecl *Spec) { |
11043 | bool IsHiddenExplicitSpecialization = false; |
11044 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
11045 | IsHiddenExplicitSpecialization = |
11046 | Spec->getMemberSpecializationInfo() |
11047 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
11048 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
11049 | } else { |
11050 | checkInstantiated(Spec); |
11051 | } |
11052 | |
11053 | if (IsHiddenExplicitSpecialization) |
11054 | diagnose(Spec->getMostRecentDecl(), false); |
11055 | } |
11056 | |
11057 | void checkInstantiated(FunctionDecl *FD) { |
11058 | if (auto *TD = FD->getPrimaryTemplate()) |
11059 | checkTemplate(TD); |
11060 | } |
11061 | |
11062 | void checkInstantiated(CXXRecordDecl *RD) { |
11063 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
11064 | if (!SD) |
11065 | return; |
11066 | |
11067 | auto From = SD->getSpecializedTemplateOrPartial(); |
11068 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
11069 | checkTemplate(TD); |
11070 | else if (auto *TD = |
11071 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
11072 | if (!S.hasVisibleDeclaration(TD)) |
11073 | diagnose(TD, true); |
11074 | checkTemplate(TD); |
11075 | } |
11076 | } |
11077 | |
11078 | void checkInstantiated(VarDecl *RD) { |
11079 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
11080 | if (!SD) |
11081 | return; |
11082 | |
11083 | auto From = SD->getSpecializedTemplateOrPartial(); |
11084 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
11085 | checkTemplate(TD); |
11086 | else if (auto *TD = |
11087 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
11088 | if (!S.hasVisibleDeclaration(TD)) |
11089 | diagnose(TD, true); |
11090 | checkTemplate(TD); |
11091 | } |
11092 | } |
11093 | |
11094 | void checkInstantiated(EnumDecl *FD) {} |
11095 | |
11096 | template<typename TemplDecl> |
11097 | void checkTemplate(TemplDecl *TD) { |
11098 | if (TD->isMemberSpecialization()) { |
11099 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
11100 | diagnose(TD->getMostRecentDecl(), false); |
11101 | } |
11102 | } |
11103 | }; |
11104 | } |
11105 | |
11106 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
11107 | if (!getLangOpts().Modules) |
11108 | return; |
11109 | |
11110 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
11111 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | #ifndef LLVM_CLANG_SEMA_DECLSPEC_H |
23 | #define LLVM_CLANG_SEMA_DECLSPEC_H |
24 | |
25 | #include "clang/AST/DeclCXX.h" |
26 | #include "clang/AST/DeclObjCCommon.h" |
27 | #include "clang/AST/NestedNameSpecifier.h" |
28 | #include "clang/Basic/ExceptionSpecificationType.h" |
29 | #include "clang/Basic/Lambda.h" |
30 | #include "clang/Basic/OperatorKinds.h" |
31 | #include "clang/Basic/Specifiers.h" |
32 | #include "clang/Lex/Token.h" |
33 | #include "clang/Sema/Ownership.h" |
34 | #include "clang/Sema/ParsedAttr.h" |
35 | #include "llvm/ADT/SmallVector.h" |
36 | #include "llvm/Support/Compiler.h" |
37 | #include "llvm/Support/ErrorHandling.h" |
38 | |
39 | namespace clang { |
40 | class ASTContext; |
41 | class CXXRecordDecl; |
42 | class TypeLoc; |
43 | class LangOptions; |
44 | class IdentifierInfo; |
45 | class NamespaceAliasDecl; |
46 | class NamespaceDecl; |
47 | class ObjCDeclSpec; |
48 | class Sema; |
49 | class Declarator; |
50 | struct TemplateIdAnnotation; |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | class CXXScopeSpec { |
65 | SourceRange Range; |
66 | NestedNameSpecifierLocBuilder Builder; |
67 | |
68 | public: |
69 | SourceRange getRange() const { return Range; } |
70 | void setRange(SourceRange R) { Range = R; } |
71 | void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); } |
72 | void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); } |
73 | SourceLocation getBeginLoc() const { return Range.getBegin(); } |
74 | SourceLocation getEndLoc() const { return Range.getEnd(); } |
75 | |
76 | |
77 | NestedNameSpecifier *getScopeRep() const { |
78 | return Builder.getRepresentation(); |
79 | } |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL, |
93 | SourceLocation ColonColonLoc); |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | void Extend(ASTContext &Context, IdentifierInfo *Identifier, |
107 | SourceLocation IdentifierLoc, SourceLocation ColonColonLoc); |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | void Extend(ASTContext &Context, NamespaceDecl *Namespace, |
121 | SourceLocation NamespaceLoc, SourceLocation ColonColonLoc); |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | void Extend(ASTContext &Context, NamespaceAliasDecl *Alias, |
136 | SourceLocation AliasLoc, SourceLocation ColonColonLoc); |
137 | |
138 | |
139 | |
140 | void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc); |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | void MakeSuper(ASTContext &Context, CXXRecordDecl *RD, |
156 | SourceLocation SuperLoc, SourceLocation ColonColonLoc); |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 | void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier, |
165 | SourceRange R); |
166 | |
167 | |
168 | |
169 | void Adopt(NestedNameSpecifierLoc Other); |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const; |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | SourceLocation getLastQualifierNameLoc() const; |
188 | |
189 | |
190 | bool isEmpty() const { return Range.isInvalid() && getScopeRep() == nullptr; } |
| 30 | | Assuming the condition is false | |
|
| 31 | | Returning zero, which participates in a condition later | |
|
191 | |
192 | bool isNotEmpty() const { return !isEmpty(); } |
| 29 | | Calling 'CXXScopeSpec::isEmpty' | |
|
| 32 | | Returning from 'CXXScopeSpec::isEmpty' | |
|
| 33 | | Returning the value 1, which participates in a condition later | |
|
193 | |
194 | |
195 | bool isInvalid() const { return Range.isValid() && getScopeRep() == nullptr; } |
| 9 | | Returning zero, which participates in a condition later | |
|
196 | |
197 | bool isValid() const { return getScopeRep() != nullptr; } |
198 | |
199 | |
200 | void SetInvalid(SourceRange R) { |
201 | assert(R.isValid() && "Must have a valid source range"); |
202 | if (Range.getBegin().isInvalid()) |
203 | Range.setBegin(R.getBegin()); |
204 | Range.setEnd(R.getEnd()); |
205 | Builder.Clear(); |
206 | } |
207 | |
208 | |
209 | |
210 | bool isSet() const { return getScopeRep() != nullptr; } |
211 | |
212 | void clear() { |
213 | Range = SourceRange(); |
214 | Builder.Clear(); |
215 | } |
216 | |
217 | |
218 | char *location_data() const { return Builder.getBuffer().first; } |
219 | |
220 | |
221 | |
222 | unsigned location_size() const { return Builder.getBuffer().second; } |
223 | }; |
224 | |
225 | |
226 | |
227 | |
228 | |
229 | class DeclSpec { |
230 | public: |
231 | |
232 | |
233 | enum SCS { |
234 | SCS_unspecified = 0, |
235 | SCS_typedef, |
236 | SCS_extern, |
237 | SCS_static, |
238 | SCS_auto, |
239 | SCS_register, |
240 | SCS_private_extern, |
241 | SCS_mutable |
242 | }; |
243 | |
244 | |
245 | |
246 | typedef ThreadStorageClassSpecifier TSCS; |
247 | static const TSCS TSCS_unspecified = clang::TSCS_unspecified; |
248 | static const TSCS TSCS___thread = clang::TSCS___thread; |
249 | static const TSCS TSCS_thread_local = clang::TSCS_thread_local; |
250 | static const TSCS TSCS__Thread_local = clang::TSCS__Thread_local; |
251 | |
252 | enum TSC { |
253 | TSC_unspecified, |
254 | TSC_imaginary, |
255 | TSC_complex |
256 | }; |
257 | |
258 | |
259 | typedef TypeSpecifierType TST; |
260 | static const TST TST_unspecified = clang::TST_unspecified; |
261 | static const TST TST_void = clang::TST_void; |
262 | static const TST TST_char = clang::TST_char; |
263 | static const TST TST_wchar = clang::TST_wchar; |
264 | static const TST TST_char8 = clang::TST_char8; |
265 | static const TST TST_char16 = clang::TST_char16; |
266 | static const TST TST_char32 = clang::TST_char32; |
267 | static const TST TST_int = clang::TST_int; |
268 | static const TST TST_int128 = clang::TST_int128; |
269 | static const TST TST_extint = clang::TST_extint; |
270 | static const TST TST_half = clang::TST_half; |
271 | static const TST TST_BFloat16 = clang::TST_BFloat16; |
272 | static const TST TST_float = clang::TST_float; |
273 | static const TST TST_double = clang::TST_double; |
274 | static const TST TST_float16 = clang::TST_Float16; |
275 | static const TST TST_accum = clang::TST_Accum; |
276 | static const TST TST_fract = clang::TST_Fract; |
277 | static const TST TST_float128 = clang::TST_float128; |
278 | static const TST TST_bool = clang::TST_bool; |
279 | static const TST TST_decimal32 = clang::TST_decimal32; |
280 | static const TST TST_decimal64 = clang::TST_decimal64; |
281 | static const TST TST_decimal128 = clang::TST_decimal128; |
282 | static const TST TST_enum = clang::TST_enum; |
283 | static const TST TST_union = clang::TST_union; |
284 | static const TST TST_struct = clang::TST_struct; |
285 | static const TST TST_interface = clang::TST_interface; |
286 | static const TST TST_class = clang::TST_class; |
287 | static const TST TST_typename = clang::TST_typename; |
288 | static const TST TST_typeofType = clang::TST_typeofType; |
289 | static const TST TST_typeofExpr = clang::TST_typeofExpr; |
290 | static const TST TST_decltype = clang::TST_decltype; |
291 | static const TST TST_decltype_auto = clang::TST_decltype_auto; |
292 | static const TST TST_underlyingType = clang::TST_underlyingType; |
293 | static const TST TST_auto = clang::TST_auto; |
294 | static const TST TST_auto_type = clang::TST_auto_type; |
295 | static const TST TST_unknown_anytype = clang::TST_unknown_anytype; |
296 | static const TST TST_atomic = clang::TST_atomic; |
297 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
298 | static const TST TST_##ImgType##_t = clang::TST_##ImgType##_t; |
299 | #include "clang/Basic/OpenCLImageTypes.def" |
300 | static const TST TST_error = clang::TST_error; |
301 | |
302 | |
303 | enum TQ { |
304 | TQ_unspecified = 0, |
305 | TQ_const = 1, |
306 | TQ_restrict = 2, |
307 | TQ_volatile = 4, |
308 | TQ_unaligned = 8, |
309 | |
310 | |
311 | TQ_atomic = 16 |
312 | }; |
313 | |
314 | |
315 | |
316 | enum ParsedSpecifiers { |
317 | PQ_None = 0, |
318 | PQ_StorageClassSpecifier = 1, |
319 | PQ_TypeSpecifier = 2, |
320 | PQ_TypeQualifier = 4, |
321 | PQ_FunctionSpecifier = 8 |
322 | |
323 | }; |
324 | |
325 | private: |
326 | |
327 | unsigned StorageClassSpec : 3; |
328 | unsigned ThreadStorageClassSpec : 2; |
329 | unsigned SCS_extern_in_linkage_spec : 1; |
330 | |
331 | |
332 | unsigned TypeSpecWidth : 2; |
333 | unsigned TypeSpecComplex : 2; |
334 | unsigned TypeSpecSign : 2; |
335 | unsigned TypeSpecType : 6; |
336 | unsigned TypeAltiVecVector : 1; |
337 | unsigned TypeAltiVecPixel : 1; |
338 | unsigned TypeAltiVecBool : 1; |
339 | unsigned TypeSpecOwned : 1; |
340 | unsigned TypeSpecPipe : 1; |
341 | unsigned TypeSpecSat : 1; |
342 | unsigned ConstrainedAuto : 1; |
343 | |
344 | |
345 | unsigned TypeQualifiers : 5; |
346 | |
347 | |
348 | unsigned FS_inline_specified : 1; |
349 | unsigned FS_forceinline_specified: 1; |
350 | unsigned FS_virtual_specified : 1; |
351 | unsigned FS_noreturn_specified : 1; |
352 | |
353 | |
354 | unsigned Friend_specified : 1; |
355 | |
356 | |
357 | unsigned ConstexprSpecifier : 2; |
358 | |
359 | union { |
360 | UnionParsedType TypeRep; |
361 | Decl *DeclRep; |
362 | Expr *ExprRep; |
363 | TemplateIdAnnotation *TemplateIdRep; |
364 | }; |
365 | |
366 | |
367 | ExplicitSpecifier FS_explicit_specifier; |
368 | |
369 | |
370 | ParsedAttributes Attrs; |
371 | |
372 | |
373 | CXXScopeSpec TypeScope; |
374 | |
375 | |
376 | |
377 | SourceRange Range; |
378 | |
379 | SourceLocation StorageClassSpecLoc, ThreadStorageClassSpecLoc; |
380 | SourceRange TSWRange; |
381 | SourceLocation TSCLoc, TSSLoc, TSTLoc, AltiVecLoc, TSSatLoc; |
382 | |
383 | |
384 | |
385 | |
386 | SourceLocation TSTNameLoc; |
387 | SourceRange TypeofParensRange; |
388 | SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc, |
389 | TQ_unalignedLoc; |
390 | SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc; |
391 | SourceLocation FS_explicitCloseParenLoc; |
392 | SourceLocation FS_forceinlineLoc; |
393 | SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc; |
394 | SourceLocation TQ_pipeLoc; |
395 | |
396 | WrittenBuiltinSpecs writtenBS; |
397 | void SaveWrittenBuiltinSpecs(); |
398 | |
399 | ObjCDeclSpec *ObjCQualifiers; |
400 | |
401 | static bool isTypeRep(TST T) { |
402 | return (T == TST_typename || T == TST_typeofType || |
403 | T == TST_underlyingType || T == TST_atomic); |
404 | } |
405 | static bool isExprRep(TST T) { |
406 | return (T == TST_typeofExpr || T == TST_decltype || T == TST_extint); |
407 | } |
408 | static bool isTemplateIdRep(TST T) { |
409 | return (T == TST_auto || T == TST_decltype_auto); |
410 | } |
411 | |
412 | DeclSpec(const DeclSpec &) = delete; |
413 | void operator=(const DeclSpec &) = delete; |
414 | public: |
415 | static bool isDeclRep(TST T) { |
416 | return (T == TST_enum || T == TST_struct || |
417 | T == TST_interface || T == TST_union || |
418 | T == TST_class); |
419 | } |
420 | |
421 | DeclSpec(AttributeFactory &attrFactory) |
422 | : StorageClassSpec(SCS_unspecified), |
423 | ThreadStorageClassSpec(TSCS_unspecified), |
424 | SCS_extern_in_linkage_spec(false), |
425 | TypeSpecWidth(static_cast<unsigned>(TypeSpecifierWidth::Unspecified)), |
426 | TypeSpecComplex(TSC_unspecified), |
427 | TypeSpecSign(static_cast<unsigned>(TypeSpecifierSign::Unspecified)), |
428 | TypeSpecType(TST_unspecified), TypeAltiVecVector(false), |
429 | TypeAltiVecPixel(false), TypeAltiVecBool(false), TypeSpecOwned(false), |
430 | TypeSpecPipe(false), TypeSpecSat(false), ConstrainedAuto(false), |
431 | TypeQualifiers(TQ_unspecified), FS_inline_specified(false), |
432 | FS_forceinline_specified(false), FS_virtual_specified(false), |
433 | FS_noreturn_specified(false), Friend_specified(false), |
434 | ConstexprSpecifier( |
435 | static_cast<unsigned>(ConstexprSpecKind::Unspecified)), |
436 | FS_explicit_specifier(), Attrs(attrFactory), writtenBS(), |
437 | ObjCQualifiers(nullptr) {} |
438 | |
439 | |
440 | SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; } |
441 | TSCS getThreadStorageClassSpec() const { |
442 | return (TSCS)ThreadStorageClassSpec; |
443 | } |
444 | bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; } |
445 | void setExternInLinkageSpec(bool Value) { |
446 | SCS_extern_in_linkage_spec = Value; |
447 | } |
448 | |
449 | SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; } |
450 | SourceLocation getThreadStorageClassSpecLoc() const { |
451 | return ThreadStorageClassSpecLoc; |
452 | } |
453 | |
454 | void ClearStorageClassSpecs() { |
455 | StorageClassSpec = DeclSpec::SCS_unspecified; |
456 | ThreadStorageClassSpec = DeclSpec::TSCS_unspecified; |
457 | SCS_extern_in_linkage_spec = false; |
458 | StorageClassSpecLoc = SourceLocation(); |
459 | ThreadStorageClassSpecLoc = SourceLocation(); |
460 | } |
461 | |
462 | void ClearTypeSpecType() { |
463 | TypeSpecType = DeclSpec::TST_unspecified; |
464 | TypeSpecOwned = false; |
465 | TSTLoc = SourceLocation(); |
466 | } |
467 | |
468 | |
469 | TypeSpecifierWidth getTypeSpecWidth() const { |
470 | return static_cast<TypeSpecifierWidth>(TypeSpecWidth); |
471 | } |
472 | TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; } |
473 | TypeSpecifierSign getTypeSpecSign() const { |
474 | return static_cast<TypeSpecifierSign>(TypeSpecSign); |
475 | } |
476 | TST getTypeSpecType() const { return (TST)TypeSpecType; } |
477 | bool isTypeAltiVecVector() const { return TypeAltiVecVector; } |
478 | bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; } |
479 | bool isTypeAltiVecBool() const { return TypeAltiVecBool; } |
480 | bool isTypeSpecOwned() const { return TypeSpecOwned; } |
481 | bool isTypeRep() const { return isTypeRep((TST) TypeSpecType); } |
482 | bool isTypeSpecPipe() const { return TypeSpecPipe; } |
483 | bool isTypeSpecSat() const { return TypeSpecSat; } |
484 | bool isConstrainedAuto() const { return ConstrainedAuto; } |
485 | |
486 | ParsedType getRepAsType() const { |
487 | assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type"); |
488 | return TypeRep; |
489 | } |
490 | Decl *getRepAsDecl() const { |
491 | assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl"); |
492 | return DeclRep; |
493 | } |
494 | Expr *getRepAsExpr() const { |
495 | assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr"); |
496 | return ExprRep; |
497 | } |
498 | TemplateIdAnnotation *getRepAsTemplateId() const { |
499 | assert(isTemplateIdRep((TST) TypeSpecType) && |
500 | "DeclSpec does not store a template id"); |
501 | return TemplateIdRep; |
502 | } |
503 | CXXScopeSpec &getTypeSpecScope() { return TypeScope; } |
504 | const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; } |
505 | |
506 | SourceRange getSourceRange() const LLVM_READONLY { return Range; } |
507 | SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } |
508 | SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } |
509 | |
510 | SourceLocation getTypeSpecWidthLoc() const { return TSWRange.getBegin(); } |
511 | SourceRange getTypeSpecWidthRange() const { return TSWRange; } |
512 | SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; } |
513 | SourceLocation getTypeSpecSignLoc() const { return TSSLoc; } |
514 | SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; } |
515 | SourceLocation getAltiVecLoc() const { return AltiVecLoc; } |
516 | SourceLocation getTypeSpecSatLoc() const { return TSSatLoc; } |
517 | |
518 | SourceLocation getTypeSpecTypeNameLoc() const { |
519 | assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename); |
520 | return TSTNameLoc; |
521 | } |
522 | |
523 | SourceRange getTypeofParensRange() const { return TypeofParensRange; } |
524 | void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; } |
525 | |
526 | bool hasAutoTypeSpec() const { |
527 | return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type || |
528 | TypeSpecType == TST_decltype_auto); |
529 | } |
530 | |
531 | bool hasTagDefinition() const; |
532 | |
533 | |
534 | static const char *getSpecifierName(DeclSpec::TST T, |
535 | const PrintingPolicy &Policy); |
536 | static const char *getSpecifierName(DeclSpec::TQ Q); |
537 | static const char *getSpecifierName(TypeSpecifierSign S); |
538 | static const char *getSpecifierName(DeclSpec::TSC C); |
539 | static const char *getSpecifierName(TypeSpecifierWidth W); |
540 | static const char *getSpecifierName(DeclSpec::SCS S); |
541 | static const char *getSpecifierName(DeclSpec::TSCS S); |
542 | static const char *getSpecifierName(ConstexprSpecKind C); |
543 | |
544 | |
545 | |
546 | |
547 | unsigned getTypeQualifiers() const { return TypeQualifiers; } |
548 | SourceLocation getConstSpecLoc() const { return TQ_constLoc; } |
549 | SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; } |
550 | SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; } |
551 | SourceLocation getAtomicSpecLoc() const { return TQ_atomicLoc; } |
552 | SourceLocation getUnalignedSpecLoc() const { return TQ_unalignedLoc; } |
553 | SourceLocation getPipeLoc() const { return TQ_pipeLoc; } |
554 | |
555 | |
556 | void ClearTypeQualifiers() { |
557 | TypeQualifiers = 0; |
558 | TQ_constLoc = SourceLocation(); |
559 | TQ_restrictLoc = SourceLocation(); |
560 | TQ_volatileLoc = SourceLocation(); |
561 | TQ_atomicLoc = SourceLocation(); |
562 | TQ_unalignedLoc = SourceLocation(); |
563 | TQ_pipeLoc = SourceLocation(); |
564 | } |
565 | |
566 | |
567 | bool isInlineSpecified() const { |
568 | return FS_inline_specified | FS_forceinline_specified; |
569 | } |
570 | SourceLocation getInlineSpecLoc() const { |
571 | return FS_inline_specified ? FS_inlineLoc : FS_forceinlineLoc; |
572 | } |
573 | |
574 | ExplicitSpecifier getExplicitSpecifier() const { |
575 | return FS_explicit_specifier; |
576 | } |
577 | |
578 | bool isVirtualSpecified() const { return FS_virtual_specified; } |
579 | SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; } |
580 | |
581 | bool hasExplicitSpecifier() const { |
582 | return FS_explicit_specifier.isSpecified(); |
583 | } |
584 | SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; } |
585 | SourceRange getExplicitSpecRange() const { |
586 | return FS_explicit_specifier.getExpr() |
587 | ? SourceRange(FS_explicitLoc, FS_explicitCloseParenLoc) |
588 | : SourceRange(FS_explicitLoc); |
589 | } |
590 | |
591 | bool isNoreturnSpecified() const { return FS_noreturn_specified; } |
592 | SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; } |
593 | |
594 | void ClearFunctionSpecs() { |
595 | FS_inline_specified = false; |
596 | FS_inlineLoc = SourceLocation(); |
597 | FS_forceinline_specified = false; |
598 | FS_forceinlineLoc = SourceLocation(); |
599 | FS_virtual_specified = false; |
600 | FS_virtualLoc = SourceLocation(); |
601 | FS_explicit_specifier = ExplicitSpecifier(); |
602 | FS_explicitLoc = SourceLocation(); |
603 | FS_explicitCloseParenLoc = SourceLocation(); |
604 | FS_noreturn_specified = false; |
605 | FS_noreturnLoc = SourceLocation(); |
606 | } |
607 | |
608 | |
609 | |
610 | |
611 | void forEachCVRUQualifier( |
612 | llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle); |
613 | |
614 | |
615 | |
616 | |
617 | void forEachQualifier( |
618 | llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle); |
619 | |
620 | |
621 | bool hasTypeSpecifier() const { |
622 | return getTypeSpecType() != DeclSpec::TST_unspecified || |
623 | getTypeSpecWidth() != TypeSpecifierWidth::Unspecified || |
624 | getTypeSpecComplex() != DeclSpec::TSC_unspecified || |
625 | getTypeSpecSign() != TypeSpecifierSign::Unspecified; |
626 | } |
627 | |
628 | |
629 | |
630 | unsigned getParsedSpecifiers() const; |
631 | |
632 | |
633 | |
634 | bool isEmpty() const { |
635 | return getParsedSpecifiers() == DeclSpec::PQ_None; |
636 | } |
637 | |
638 | void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); } |
639 | void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); } |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc, |
652 | const char *&PrevSpec, unsigned &DiagID, |
653 | const PrintingPolicy &Policy); |
654 | bool SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc, |
655 | const char *&PrevSpec, unsigned &DiagID); |
656 | bool SetTypeSpecWidth(TypeSpecifierWidth W, SourceLocation Loc, |
657 | const char *&PrevSpec, unsigned &DiagID, |
658 | const PrintingPolicy &Policy); |
659 | bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec, |
660 | unsigned &DiagID); |
661 | bool SetTypeSpecSign(TypeSpecifierSign S, SourceLocation Loc, |
662 | const char *&PrevSpec, unsigned &DiagID); |
663 | bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, |
664 | unsigned &DiagID, const PrintingPolicy &Policy); |
665 | bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, |
666 | unsigned &DiagID, ParsedType Rep, |
667 | const PrintingPolicy &Policy); |
668 | bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, |
669 | unsigned &DiagID, TypeResult Rep, |
670 | const PrintingPolicy &Policy) { |
671 | if (Rep.isInvalid()) |
672 | return SetTypeSpecError(); |
673 | return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Rep.get(), Policy); |
674 | } |
675 | bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, |
676 | unsigned &DiagID, Decl *Rep, bool Owned, |
677 | const PrintingPolicy &Policy); |
678 | bool SetTypeSpecType(TST T, SourceLocation TagKwLoc, |
679 | SourceLocation TagNameLoc, const char *&PrevSpec, |
680 | unsigned &DiagID, ParsedType Rep, |
681 | const PrintingPolicy &Policy); |
682 | bool SetTypeSpecType(TST T, SourceLocation TagKwLoc, |
683 | SourceLocation TagNameLoc, const char *&PrevSpec, |
684 | unsigned &DiagID, Decl *Rep, bool Owned, |
685 | const PrintingPolicy &Policy); |
686 | bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, |
687 | unsigned &DiagID, TemplateIdAnnotation *Rep, |
688 | const PrintingPolicy &Policy); |
689 | |
690 | bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, |
691 | unsigned &DiagID, Expr *Rep, |
692 | const PrintingPolicy &policy); |
693 | bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, |
694 | const char *&PrevSpec, unsigned &DiagID, |
695 | const PrintingPolicy &Policy); |
696 | bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, |
697 | const char *&PrevSpec, unsigned &DiagID, |
698 | const PrintingPolicy &Policy); |
699 | bool SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc, |
700 | const char *&PrevSpec, unsigned &DiagID, |
701 | const PrintingPolicy &Policy); |
702 | bool SetTypePipe(bool isPipe, SourceLocation Loc, |
703 | const char *&PrevSpec, unsigned &DiagID, |
704 | const PrintingPolicy &Policy); |
705 | bool SetExtIntType(SourceLocation KWLoc, Expr *BitWidth, |
706 | const char *&PrevSpec, unsigned &DiagID, |
707 | const PrintingPolicy &Policy); |
708 | bool SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec, |
709 | unsigned &DiagID); |
710 | bool SetTypeSpecError(); |
711 | void UpdateDeclRep(Decl *Rep) { |
712 | assert(isDeclRep((TST) TypeSpecType)); |
713 | DeclRep = Rep; |
714 | } |
715 | void UpdateTypeRep(ParsedType Rep) { |
716 | assert(isTypeRep((TST) TypeSpecType)); |
717 | TypeRep = Rep; |
718 | } |
719 | void UpdateExprRep(Expr *Rep) { |
720 | assert(isExprRep((TST) TypeSpecType)); |
721 | ExprRep = Rep; |
722 | } |
723 | |
724 | bool SetTypeQual(TQ T, SourceLocation Loc); |
725 | |
726 | bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
727 | unsigned &DiagID, const LangOptions &Lang); |
728 | |
729 | bool setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, |
730 | unsigned &DiagID); |
731 | bool setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec, |
732 | unsigned &DiagID); |
733 | bool setFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, |
734 | unsigned &DiagID); |
735 | bool setFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, |
736 | unsigned &DiagID, ExplicitSpecifier ExplicitSpec, |
737 | SourceLocation CloseParenLoc); |
738 | bool setFunctionSpecNoreturn(SourceLocation Loc, const char *&PrevSpec, |
739 | unsigned &DiagID); |
740 | |
741 | bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, |
742 | unsigned &DiagID); |
743 | bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, |
744 | unsigned &DiagID); |
745 | bool SetConstexprSpec(ConstexprSpecKind ConstexprKind, SourceLocation Loc, |
746 | const char *&PrevSpec, unsigned &DiagID); |
747 | |
748 | bool isFriendSpecified() const { return Friend_specified; } |
749 | SourceLocation getFriendSpecLoc() const { return FriendLoc; } |
750 | |
751 | bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); } |
752 | SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; } |
753 | |
754 | ConstexprSpecKind getConstexprSpecifier() const { |
755 | return ConstexprSpecKind(ConstexprSpecifier); |
756 | } |
757 | |
758 | SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; } |
759 | bool hasConstexprSpecifier() const { |
760 | return getConstexprSpecifier() != ConstexprSpecKind::Unspecified; |
761 | } |
762 | |
763 | void ClearConstexprSpec() { |
764 | ConstexprSpecifier = static_cast<unsigned>(ConstexprSpecKind::Unspecified); |
765 | ConstexprLoc = SourceLocation(); |
766 | } |
767 | |
768 | AttributePool &getAttributePool() const { |
769 | return Attrs.getPool(); |
770 | } |
771 | |
772 | |
773 | |
774 | |
775 | |
776 | |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | |
787 | |
788 | |
789 | void addAttributes(ParsedAttributesView &AL) { |
790 | Attrs.addAll(AL.begin(), AL.end()); |
791 | } |
792 | |
793 | bool hasAttributes() const { return !Attrs.empty(); } |
794 | |
795 | ParsedAttributes &getAttributes() { return Attrs; } |
796 | const ParsedAttributes &getAttributes() const { return Attrs; } |
797 | |
798 | void takeAttributesFrom(ParsedAttributes &attrs) { |
799 | Attrs.takeAllFrom(attrs); |
800 | } |
801 | |
802 | |
803 | |
804 | |
805 | void Finish(Sema &S, const PrintingPolicy &Policy); |
806 | |
807 | const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const { |
808 | return writtenBS; |
809 | } |
810 | |
811 | ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; } |
812 | void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; } |
813 | |
814 | |
815 | |
816 | |
817 | bool isMissingDeclaratorOk(); |
818 | }; |
819 | |
820 | |
821 | |
822 | class ObjCDeclSpec { |
823 | public: |
824 | |
825 | |
826 | |
827 | |
828 | |
829 | |
830 | enum ObjCDeclQualifier { |
831 | DQ_None = 0x0, |
832 | DQ_In = 0x1, |
833 | DQ_Inout = 0x2, |
834 | DQ_Out = 0x4, |
835 | DQ_Bycopy = 0x8, |
836 | DQ_Byref = 0x10, |
837 | DQ_Oneway = 0x20, |
838 | DQ_CSNullability = 0x40 |
839 | }; |
840 | |
841 | ObjCDeclSpec() |
842 | : objcDeclQualifier(DQ_None), |
843 | PropertyAttributes(ObjCPropertyAttribute::kind_noattr), Nullability(0), |
844 | GetterName(nullptr), SetterName(nullptr) {} |
845 | |
846 | ObjCDeclQualifier getObjCDeclQualifier() const { |
847 | return (ObjCDeclQualifier)objcDeclQualifier; |
848 | } |
849 | void setObjCDeclQualifier(ObjCDeclQualifier DQVal) { |
850 | objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal); |
851 | } |
852 | void clearObjCDeclQualifier(ObjCDeclQualifier DQVal) { |
853 | objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier & ~DQVal); |
854 | } |
855 | |
856 | ObjCPropertyAttribute::Kind getPropertyAttributes() const { |
857 | return ObjCPropertyAttribute::Kind(PropertyAttributes); |
858 | } |
859 | void setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal) { |
860 | PropertyAttributes = |
861 | (ObjCPropertyAttribute::Kind)(PropertyAttributes | PRVal); |
862 | } |
863 | |
864 | NullabilityKind getNullability() const { |
865 | assert( |
866 | ((getObjCDeclQualifier() & DQ_CSNullability) || |
867 | (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) && |
868 | "Objective-C declspec doesn't have nullability"); |
869 | return static_cast<NullabilityKind>(Nullability); |
870 | } |
871 | |
872 | SourceLocation getNullabilityLoc() const { |
873 | assert( |
874 | ((getObjCDeclQualifier() & DQ_CSNullability) || |
875 | (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) && |
876 | "Objective-C declspec doesn't have nullability"); |
877 | return NullabilityLoc; |
878 | } |
879 | |
880 | void setNullability(SourceLocation loc, NullabilityKind kind) { |
881 | assert( |
882 | ((getObjCDeclQualifier() & DQ_CSNullability) || |
883 | (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) && |
884 | "Set the nullability declspec or property attribute first"); |
885 | Nullability = static_cast<unsigned>(kind); |
886 | NullabilityLoc = loc; |
887 | } |
888 | |
889 | const IdentifierInfo *getGetterName() const { return GetterName; } |
890 | IdentifierInfo *getGetterName() { return GetterName; } |
891 | SourceLocation getGetterNameLoc() const { return GetterNameLoc; } |
892 | void setGetterName(IdentifierInfo *name, SourceLocation loc) { |
893 | GetterName = name; |
894 | GetterNameLoc = loc; |
895 | } |
896 | |
897 | const IdentifierInfo *getSetterName() const { return SetterName; } |
898 | IdentifierInfo *getSetterName() { return SetterName; } |
899 | SourceLocation getSetterNameLoc() const { return SetterNameLoc; } |
900 | void setSetterName(IdentifierInfo *name, SourceLocation loc) { |
901 | SetterName = name; |
902 | SetterNameLoc = loc; |
903 | } |
904 | |
905 | private: |
906 | |
907 | |
908 | |
909 | unsigned objcDeclQualifier : 7; |
910 | |
911 | |
912 | unsigned PropertyAttributes : NumObjCPropertyAttrsBits; |
913 | |
914 | unsigned Nullability : 2; |
915 | |
916 | SourceLocation NullabilityLoc; |
917 | |
918 | IdentifierInfo *GetterName; |
919 | IdentifierInfo *SetterName; |
920 | SourceLocation GetterNameLoc; |
921 | SourceLocation SetterNameLoc; |
922 | |
923 | }; |
924 | |
925 | |
926 | enum class UnqualifiedIdKind { |
927 | |
928 | IK_Identifier, |
929 | |
930 | IK_OperatorFunctionId, |
931 | |
932 | IK_ConversionFunctionId, |
933 | |
934 | IK_LiteralOperatorId, |
935 | |
936 | IK_ConstructorName, |
937 | |
938 | IK_ConstructorTemplateId, |
939 | |
940 | IK_DestructorName, |
941 | |
942 | IK_TemplateId, |
943 | |
944 | IK_ImplicitSelfParam, |
945 | |
946 | IK_DeductionGuideName |
947 | }; |
948 | |
949 | |
950 | class UnqualifiedId { |
951 | private: |
952 | UnqualifiedId(const UnqualifiedId &Other) = delete; |
953 | const UnqualifiedId &operator=(const UnqualifiedId &) = delete; |
954 | |
955 | public: |
956 | |
957 | UnqualifiedIdKind Kind; |
958 | |
959 | struct OFI { |
960 | |
961 | OverloadedOperatorKind Operator; |
962 | |
963 | |
964 | |
965 | |
966 | |
967 | |
968 | |
969 | |
970 | SourceLocation SymbolLocations[3]; |
971 | }; |
972 | |
973 | |
974 | |
975 | union { |
976 | |
977 | |
978 | IdentifierInfo *Identifier; |
979 | |
980 | |
981 | |
982 | struct OFI OperatorFunctionId; |
983 | |
984 | |
985 | |
986 | UnionParsedType ConversionFunctionId; |
987 | |
988 | |
989 | |
990 | UnionParsedType ConstructorName; |
991 | |
992 | |
993 | |
994 | UnionParsedType DestructorName; |
995 | |
996 | |
997 | UnionParsedTemplateTy TemplateName; |
998 | |
999 | |
1000 | |
1001 | |
1002 | TemplateIdAnnotation *TemplateId; |
1003 | }; |
1004 | |
1005 | |
1006 | |
1007 | |
1008 | SourceLocation StartLocation; |
1009 | |
1010 | |
1011 | SourceLocation EndLocation; |
1012 | |
1013 | UnqualifiedId() |
1014 | : Kind(UnqualifiedIdKind::IK_Identifier), Identifier(nullptr) {} |
1015 | |
1016 | |
1017 | |
1018 | void clear() { |
1019 | Kind = UnqualifiedIdKind::IK_Identifier; |
1020 | Identifier = nullptr; |
1021 | StartLocation = SourceLocation(); |
1022 | EndLocation = SourceLocation(); |
1023 | } |
1024 | |
1025 | |
1026 | bool isValid() const { return StartLocation.isValid(); } |
1027 | |
1028 | |
1029 | bool isInvalid() const { return !isValid(); } |
1030 | |
1031 | |
1032 | UnqualifiedIdKind getKind() const { return Kind; } |
1033 | |
1034 | |
1035 | |
1036 | |
1037 | |
1038 | void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) { |
1039 | Kind = UnqualifiedIdKind::IK_Identifier; |
1040 | Identifier = const_cast<IdentifierInfo *>(Id); |
1041 | StartLocation = EndLocation = IdLoc; |
1042 | } |
1043 | |
1044 | |
1045 | |
1046 | |
1047 | |
1048 | |
1049 | |
1050 | |
1051 | |
1052 | |
1053 | void setOperatorFunctionId(SourceLocation OperatorLoc, |
1054 | OverloadedOperatorKind Op, |
1055 | SourceLocation SymbolLocations[3]); |
1056 | |
1057 | |
1058 | |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | void setConversionFunctionId(SourceLocation OperatorLoc, |
1066 | ParsedType Ty, |
1067 | SourceLocation EndLoc) { |
1068 | Kind = UnqualifiedIdKind::IK_ConversionFunctionId; |
1069 | StartLocation = OperatorLoc; |
1070 | EndLocation = EndLoc; |
1071 | ConversionFunctionId = Ty; |
1072 | } |
1073 | |
1074 | |
1075 | |
1076 | |
1077 | |
1078 | |
1079 | |
1080 | |
1081 | |
1082 | void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc, |
1083 | SourceLocation IdLoc) { |
1084 | Kind = UnqualifiedIdKind::IK_LiteralOperatorId; |
1085 | Identifier = const_cast<IdentifierInfo *>(Id); |
1086 | StartLocation = OpLoc; |
1087 | EndLocation = IdLoc; |
1088 | } |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | |
1096 | |
1097 | void setConstructorName(ParsedType ClassType, |
1098 | SourceLocation ClassNameLoc, |
1099 | SourceLocation EndLoc) { |
1100 | Kind = UnqualifiedIdKind::IK_ConstructorName; |
1101 | StartLocation = ClassNameLoc; |
1102 | EndLocation = EndLoc; |
1103 | ConstructorName = ClassType; |
1104 | } |
1105 | |
1106 | |
1107 | |
1108 | |
1109 | |
1110 | |
1111 | |
1112 | void setConstructorTemplateId(TemplateIdAnnotation *TemplateId); |
1113 | |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | |
1119 | |
1120 | void setDestructorName(SourceLocation TildeLoc, |
1121 | ParsedType ClassType, |
1122 | SourceLocation EndLoc) { |
1123 | Kind = UnqualifiedIdKind::IK_DestructorName; |
1124 | StartLocation = TildeLoc; |
1125 | EndLocation = EndLoc; |
1126 | DestructorName = ClassType; |
1127 | } |
1128 | |
1129 | |
1130 | |
1131 | |
1132 | |
1133 | |
1134 | void setTemplateId(TemplateIdAnnotation *TemplateId); |
1135 | |
1136 | |
1137 | |
1138 | |
1139 | |
1140 | |
1141 | void setDeductionGuideName(ParsedTemplateTy Template, |
1142 | SourceLocation TemplateLoc) { |
1143 | Kind = UnqualifiedIdKind::IK_DeductionGuideName; |
1144 | TemplateName = Template; |
1145 | StartLocation = EndLocation = TemplateLoc; |
1146 | } |
1147 | |
1148 | |
1149 | |
1150 | |
1151 | |
1152 | void setImplicitSelfParam(const IdentifierInfo *Id) { |
1153 | Kind = UnqualifiedIdKind::IK_ImplicitSelfParam; |
1154 | Identifier = const_cast<IdentifierInfo *>(Id); |
1155 | StartLocation = EndLocation = SourceLocation(); |
1156 | } |
1157 | |
1158 | |
1159 | SourceRange getSourceRange() const LLVM_READONLY { |
1160 | return SourceRange(StartLocation, EndLocation); |
1161 | } |
1162 | SourceLocation getBeginLoc() const LLVM_READONLY { return StartLocation; } |
1163 | SourceLocation getEndLoc() const LLVM_READONLY { return EndLocation; } |
1164 | }; |
1165 | |
1166 | |
1167 | typedef SmallVector<Token, 4> CachedTokens; |
1168 | |
1169 | |
1170 | |
1171 | |
1172 | |
1173 | struct DeclaratorChunk { |
1174 | DeclaratorChunk() {}; |
1175 | |
1176 | enum { |
1177 | Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren, Pipe |
1178 | } Kind; |
1179 | |
1180 | |
1181 | SourceLocation Loc; |
1182 | |
1183 | SourceLocation EndLoc; |
1184 | |
1185 | SourceRange getSourceRange() const { |
1186 | if (EndLoc.isInvalid()) |
1187 | return SourceRange(Loc, Loc); |
1188 | return SourceRange(Loc, EndLoc); |
1189 | } |
1190 | |
1191 | ParsedAttributesView AttrList; |
1192 | |
1193 | struct PointerTypeInfo { |
1194 | |
1195 | unsigned TypeQuals : 5; |
1196 | |
1197 | |
1198 | SourceLocation ConstQualLoc; |
1199 | |
1200 | |
1201 | SourceLocation VolatileQualLoc; |
1202 | |
1203 | |
1204 | SourceLocation RestrictQualLoc; |
1205 | |
1206 | |
1207 | SourceLocation AtomicQualLoc; |
1208 | |
1209 | |
1210 | SourceLocation UnalignedQualLoc; |
1211 | |
1212 | void destroy() { |
1213 | } |
1214 | }; |
1215 | |
1216 | struct ReferenceTypeInfo { |
1217 | |
1218 | bool HasRestrict : 1; |
1219 | |
1220 | bool LValueRef : 1; |
1221 | void destroy() { |
1222 | } |
1223 | }; |
1224 | |
1225 | struct ArrayTypeInfo { |
1226 | |
1227 | |
1228 | unsigned TypeQuals : 5; |
1229 | |
1230 | |
1231 | unsigned hasStatic : 1; |
1232 | |
1233 | |
1234 | unsigned isStar : 1; |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | Expr *NumElts; |
1240 | |
1241 | void destroy() {} |
1242 | }; |
1243 | |
1244 | |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | |
1250 | |
1251 | struct ParamInfo { |
1252 | IdentifierInfo *Ident; |
1253 | SourceLocation IdentLoc; |
1254 | Decl *Param; |
1255 | |
1256 | |
1257 | |
1258 | |
1259 | |
1260 | |
1261 | std::unique_ptr<CachedTokens> DefaultArgTokens; |
1262 | |
1263 | ParamInfo() = default; |
1264 | ParamInfo(IdentifierInfo *ident, SourceLocation iloc, |
1265 | Decl *param, |
1266 | std::unique_ptr<CachedTokens> DefArgTokens = nullptr) |
1267 | : Ident(ident), IdentLoc(iloc), Param(param), |
1268 | DefaultArgTokens(std::move(DefArgTokens)) {} |
1269 | }; |
1270 | |
1271 | struct TypeAndRange { |
1272 | ParsedType Ty; |
1273 | SourceRange Range; |
1274 | }; |
1275 | |
1276 | struct FunctionTypeInfo { |
1277 | |
1278 | |
1279 | |
1280 | unsigned hasPrototype : 1; |
1281 | |
1282 | |
1283 | |
1284 | |
1285 | unsigned isVariadic : 1; |
1286 | |
1287 | |
1288 | unsigned isAmbiguous : 1; |
1289 | |
1290 | |
1291 | |
1292 | unsigned RefQualifierIsLValueRef : 1; |
1293 | |
1294 | |
1295 | unsigned ExceptionSpecType : 4; |
1296 | |
1297 | |
1298 | unsigned DeleteParams : 1; |
1299 | |
1300 | |
1301 | |
1302 | unsigned HasTrailingReturnType : 1; |
1303 | |
1304 | |
1305 | SourceLocation LParenLoc; |
1306 | |
1307 | |
1308 | SourceLocation EllipsisLoc; |
1309 | |
1310 | |
1311 | SourceLocation RParenLoc; |
1312 | |
1313 | |
1314 | |
1315 | unsigned NumParams; |
1316 | |
1317 | |
1318 | |
1319 | |
1320 | unsigned NumExceptionsOrDecls; |
1321 | |
1322 | |
1323 | |
1324 | |
1325 | SourceLocation RefQualifierLoc; |
1326 | |
1327 | |
1328 | |
1329 | SourceLocation MutableLoc; |
1330 | |
1331 | |
1332 | SourceLocation ExceptionSpecLocBeg; |
1333 | |
1334 | |
1335 | SourceLocation ExceptionSpecLocEnd; |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | ParamInfo *Params; |
1341 | |
1342 | |
1343 | DeclSpec *MethodQualifiers; |
1344 | |
1345 | |
1346 | AttributeFactory *QualAttrFactory; |
1347 | |
1348 | union { |
1349 | |
1350 | |
1351 | |
1352 | TypeAndRange *Exceptions; |
1353 | |
1354 | |
1355 | |
1356 | Expr *NoexceptExpr; |
1357 | |
1358 | |
1359 | |
1360 | CachedTokens *ExceptionSpecTokens; |
1361 | |
1362 | |
1363 | |
1364 | |
1365 | NamedDecl **DeclsInPrototype; |
1366 | }; |
1367 | |
1368 | |
1369 | |
1370 | UnionParsedType TrailingReturnType; |
1371 | |
1372 | |
1373 | |
1374 | SourceLocation TrailingReturnTypeLoc; |
1375 | |
1376 | |
1377 | |
1378 | |
1379 | void freeParams() { |
1380 | for (unsigned I = 0; I < NumParams; ++I) |
1381 | Params[I].DefaultArgTokens.reset(); |
1382 | if (DeleteParams) { |
1383 | delete[] Params; |
1384 | DeleteParams = false; |
1385 | } |
1386 | NumParams = 0; |
1387 | } |
1388 | |
1389 | void destroy() { |
1390 | freeParams(); |
1391 | delete QualAttrFactory; |
1392 | delete MethodQualifiers; |
1393 | switch (getExceptionSpecType()) { |
1394 | default: |
1395 | break; |
1396 | case EST_Dynamic: |
1397 | delete[] Exceptions; |
1398 | break; |
1399 | case EST_Unparsed: |
1400 | delete ExceptionSpecTokens; |
1401 | break; |
1402 | case EST_None: |
1403 | if (NumExceptionsOrDecls != 0) |
1404 | delete[] DeclsInPrototype; |
1405 | break; |
1406 | } |
1407 | } |
1408 | |
1409 | DeclSpec &getOrCreateMethodQualifiers() { |
1410 | if (!MethodQualifiers) { |
1411 | QualAttrFactory = new AttributeFactory(); |
1412 | MethodQualifiers = new DeclSpec(*QualAttrFactory); |
1413 | } |
1414 | return *MethodQualifiers; |
1415 | } |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | bool isKNRPrototype() const { return !hasPrototype && NumParams != 0; } |
1421 | |
1422 | SourceLocation getLParenLoc() const { return LParenLoc; } |
1423 | |
1424 | SourceLocation getEllipsisLoc() const { return EllipsisLoc; } |
1425 | |
1426 | SourceLocation getRParenLoc() const { return RParenLoc; } |
1427 | |
1428 | SourceLocation getExceptionSpecLocBeg() const { |
1429 | return ExceptionSpecLocBeg; |
1430 | } |
1431 | |
1432 | SourceLocation getExceptionSpecLocEnd() const { |
1433 | return ExceptionSpecLocEnd; |
1434 | } |
1435 | |
1436 | SourceRange getExceptionSpecRange() const { |
1437 | return SourceRange(getExceptionSpecLocBeg(), getExceptionSpecLocEnd()); |
1438 | } |
1439 | |
1440 | |
1441 | SourceLocation getRefQualifierLoc() const { return RefQualifierLoc; } |
1442 | |
1443 | |
1444 | SourceLocation getConstQualifierLoc() const { |
1445 | assert(MethodQualifiers); |
1446 | return MethodQualifiers->getConstSpecLoc(); |
1447 | } |
1448 | |
1449 | |
1450 | SourceLocation getVolatileQualifierLoc() const { |
1451 | assert(MethodQualifiers); |
1452 | return MethodQualifiers->getVolatileSpecLoc(); |
1453 | } |
1454 | |
1455 | |
1456 | SourceLocation getRestrictQualifierLoc() const { |
1457 | assert(MethodQualifiers); |
1458 | return MethodQualifiers->getRestrictSpecLoc(); |
1459 | } |
1460 | |
1461 | |
1462 | SourceLocation getMutableLoc() const { return MutableLoc; } |
1463 | |
1464 | |
1465 | |
1466 | bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); } |
1467 | |
1468 | |
1469 | |
1470 | bool hasMutableQualifier() const { return getMutableLoc().isValid(); } |
1471 | |
1472 | |
1473 | bool hasMethodTypeQualifiers() const { |
1474 | return MethodQualifiers && (MethodQualifiers->getTypeQualifiers() || |
1475 | MethodQualifiers->getAttributes().size()); |
1476 | } |
1477 | |
1478 | |
1479 | ExceptionSpecificationType getExceptionSpecType() const { |
1480 | return static_cast<ExceptionSpecificationType>(ExceptionSpecType); |
1481 | } |
1482 | |
1483 | |
1484 | unsigned getNumExceptions() const { |
1485 | assert(ExceptionSpecType != EST_None); |
1486 | return NumExceptionsOrDecls; |
1487 | } |
1488 | |
1489 | |
1490 | |
1491 | ArrayRef<NamedDecl *> getDeclsInPrototype() const { |
1492 | assert(ExceptionSpecType == EST_None); |
1493 | return llvm::makeArrayRef(DeclsInPrototype, NumExceptionsOrDecls); |
1494 | } |
1495 | |
1496 | |
1497 | |
1498 | bool hasTrailingReturnType() const { return HasTrailingReturnType; } |
1499 | |
1500 | |
1501 | ParsedType getTrailingReturnType() const { |
1502 | assert(HasTrailingReturnType); |
1503 | return TrailingReturnType; |
1504 | } |
1505 | |
1506 | |
1507 | SourceLocation getTrailingReturnTypeLoc() const { |
1508 | assert(HasTrailingReturnType); |
1509 | return TrailingReturnTypeLoc; |
1510 | } |
1511 | }; |
1512 | |
1513 | struct BlockPointerTypeInfo { |
1514 | |
1515 | |
1516 | unsigned TypeQuals : 5; |
1517 | |
1518 | void destroy() { |
1519 | } |
1520 | }; |
1521 | |
1522 | struct MemberPointerTypeInfo { |
1523 | |
1524 | unsigned TypeQuals : 5; |
1525 | |
1526 | SourceLocation StarLoc; |
1527 | |
1528 | |
1529 | alignas(CXXScopeSpec) char ScopeMem[sizeof(CXXScopeSpec)]; |
1530 | CXXScopeSpec &Scope() { |
1531 | return *reinterpret_cast<CXXScopeSpec *>(ScopeMem); |
1532 | } |
1533 | const CXXScopeSpec &Scope() const { |
1534 | return *reinterpret_cast<const CXXScopeSpec *>(ScopeMem); |
1535 | } |
1536 | void destroy() { |
1537 | Scope().~CXXScopeSpec(); |
1538 | } |
1539 | }; |
1540 | |
1541 | struct PipeTypeInfo { |
1542 | |
1543 | unsigned AccessWrites : 3; |
1544 | |
1545 | void destroy() {} |
1546 | }; |
1547 | |
1548 | union { |
1549 | PointerTypeInfo Ptr; |
1550 | ReferenceTypeInfo Ref; |
1551 | ArrayTypeInfo Arr; |
1552 | FunctionTypeInfo Fun; |
1553 | BlockPointerTypeInfo Cls; |
1554 | MemberPointerTypeInfo Mem; |
1555 | PipeTypeInfo PipeInfo; |
1556 | }; |
1557 | |
1558 | void destroy() { |
1559 | switch (Kind) { |
1560 | case DeclaratorChunk::Function: return Fun.destroy(); |
1561 | case DeclaratorChunk::Pointer: return Ptr.destroy(); |
1562 | case DeclaratorChunk::BlockPointer: return Cls.destroy(); |
1563 | case DeclaratorChunk::Reference: return Ref.destroy(); |
1564 | case DeclaratorChunk::Array: return Arr.destroy(); |
1565 | case DeclaratorChunk::MemberPointer: return Mem.destroy(); |
1566 | case DeclaratorChunk::Paren: return; |
1567 | case DeclaratorChunk::Pipe: return PipeInfo.destroy(); |
1568 | } |
1569 | } |
1570 | |
1571 | |
1572 | |
1573 | const ParsedAttributesView &getAttrs() const { return AttrList; } |
1574 | ParsedAttributesView &getAttrs() { return AttrList; } |
1575 | |
1576 | |
1577 | static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc, |
1578 | SourceLocation ConstQualLoc, |
1579 | SourceLocation VolatileQualLoc, |
1580 | SourceLocation RestrictQualLoc, |
1581 | SourceLocation AtomicQualLoc, |
1582 | SourceLocation UnalignedQualLoc) { |
1583 | DeclaratorChunk I; |
1584 | I.Kind = Pointer; |
1585 | I.Loc = Loc; |
1586 | new (&I.Ptr) PointerTypeInfo; |
1587 | I.Ptr.TypeQuals = TypeQuals; |
1588 | I.Ptr.ConstQualLoc = ConstQualLoc; |
1589 | I.Ptr.VolatileQualLoc = VolatileQualLoc; |
1590 | I.Ptr.RestrictQualLoc = RestrictQualLoc; |
1591 | I.Ptr.AtomicQualLoc = AtomicQualLoc; |
1592 | I.Ptr.UnalignedQualLoc = UnalignedQualLoc; |
1593 | return I; |
1594 | } |
1595 | |
1596 | |
1597 | static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc, |
1598 | bool lvalue) { |
1599 | DeclaratorChunk I; |
1600 | I.Kind = Reference; |
1601 | I.Loc = Loc; |
1602 | I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0; |
1603 | I.Ref.LValueRef = lvalue; |
1604 | return I; |
1605 | } |
1606 | |
1607 | |
1608 | static DeclaratorChunk getArray(unsigned TypeQuals, |
1609 | bool isStatic, bool isStar, Expr *NumElts, |
1610 | SourceLocation LBLoc, SourceLocation RBLoc) { |
1611 | DeclaratorChunk I; |
1612 | I.Kind = Array; |
1613 | I.Loc = LBLoc; |
1614 | I.EndLoc = RBLoc; |
1615 | I.Arr.TypeQuals = TypeQuals; |
1616 | I.Arr.hasStatic = isStatic; |
1617 | I.Arr.isStar = isStar; |
1618 | I.Arr.NumElts = NumElts; |
1619 | return I; |
1620 | } |
1621 | |
1622 | |
1623 | |
1624 | static DeclaratorChunk getFunction(bool HasProto, |
1625 | bool IsAmbiguous, |
1626 | SourceLocation LParenLoc, |
1627 | ParamInfo *Params, unsigned NumParams, |
1628 | SourceLocation EllipsisLoc, |
1629 | SourceLocation RParenLoc, |
1630 | bool RefQualifierIsLvalueRef, |
1631 | SourceLocation RefQualifierLoc, |
1632 | SourceLocation MutableLoc, |
1633 | ExceptionSpecificationType ESpecType, |
1634 | SourceRange ESpecRange, |
1635 | ParsedType *Exceptions, |
1636 | SourceRange *ExceptionRanges, |
1637 | unsigned NumExceptions, |
1638 | Expr *NoexceptExpr, |
1639 | CachedTokens *ExceptionSpecTokens, |
1640 | ArrayRef<NamedDecl *> DeclsInPrototype, |
1641 | SourceLocation LocalRangeBegin, |
1642 | SourceLocation LocalRangeEnd, |
1643 | Declarator &TheDeclarator, |
1644 | TypeResult TrailingReturnType = |
1645 | TypeResult(), |
1646 | SourceLocation TrailingReturnTypeLoc = |
1647 | SourceLocation(), |
1648 | DeclSpec *MethodQualifiers = nullptr); |
1649 | |
1650 | |
1651 | static DeclaratorChunk getBlockPointer(unsigned TypeQuals, |
1652 | SourceLocation Loc) { |
1653 | DeclaratorChunk I; |
1654 | I.Kind = BlockPointer; |
1655 | I.Loc = Loc; |
1656 | I.Cls.TypeQuals = TypeQuals; |
1657 | return I; |
1658 | } |
1659 | |
1660 | |
1661 | static DeclaratorChunk getPipe(unsigned TypeQuals, |
1662 | SourceLocation Loc) { |
1663 | DeclaratorChunk I; |
1664 | I.Kind = Pipe; |
1665 | I.Loc = Loc; |
1666 | I.Cls.TypeQuals = TypeQuals; |
1667 | return I; |
1668 | } |
1669 | |
1670 | static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS, |
1671 | unsigned TypeQuals, |
1672 | SourceLocation StarLoc, |
1673 | SourceLocation EndLoc) { |
1674 | DeclaratorChunk I; |
1675 | I.Kind = MemberPointer; |
1676 | I.Loc = SS.getBeginLoc(); |
1677 | I.EndLoc = EndLoc; |
1678 | new (&I.Mem) MemberPointerTypeInfo; |
1679 | I.Mem.StarLoc = StarLoc; |
1680 | I.Mem.TypeQuals = TypeQuals; |
1681 | new (I.Mem.ScopeMem) CXXScopeSpec(SS); |
1682 | return I; |
1683 | } |
1684 | |
1685 | |
1686 | static DeclaratorChunk getParen(SourceLocation LParenLoc, |
1687 | SourceLocation RParenLoc) { |
1688 | DeclaratorChunk I; |
1689 | I.Kind = Paren; |
1690 | I.Loc = LParenLoc; |
1691 | I.EndLoc = RParenLoc; |
1692 | return I; |
1693 | } |
1694 | |
1695 | bool isParen() const { |
1696 | return Kind == Paren; |
1697 | } |
1698 | }; |
1699 | |
1700 | |
1701 | |
1702 | class DecompositionDeclarator { |
1703 | public: |
1704 | struct Binding { |
1705 | IdentifierInfo *Name; |
1706 | SourceLocation NameLoc; |
1707 | }; |
1708 | |
1709 | private: |
1710 | |
1711 | SourceLocation LSquareLoc, RSquareLoc; |
1712 | |
1713 | |
1714 | Binding *Bindings; |
1715 | unsigned NumBindings : 31; |
1716 | unsigned DeleteBindings : 1; |
1717 | |
1718 | friend class Declarator; |
1719 | |
1720 | public: |
1721 | DecompositionDeclarator() |
1722 | : Bindings(nullptr), NumBindings(0), DeleteBindings(false) {} |
1723 | DecompositionDeclarator(const DecompositionDeclarator &G) = delete; |
1724 | DecompositionDeclarator &operator=(const DecompositionDeclarator &G) = delete; |
1725 | ~DecompositionDeclarator() { |
1726 | if (DeleteBindings) |
1727 | delete[] Bindings; |
1728 | } |
1729 | |
1730 | void clear() { |
1731 | LSquareLoc = RSquareLoc = SourceLocation(); |
1732 | if (DeleteBindings) |
1733 | delete[] Bindings; |
1734 | Bindings = nullptr; |
1735 | NumBindings = 0; |
1736 | DeleteBindings = false; |
1737 | } |
1738 | |
1739 | ArrayRef<Binding> bindings() const { |
1740 | return llvm::makeArrayRef(Bindings, NumBindings); |
1741 | } |
1742 | |
1743 | bool isSet() const { return LSquareLoc.isValid(); } |
1744 | |
1745 | SourceLocation getLSquareLoc() const { return LSquareLoc; } |
1746 | SourceLocation getRSquareLoc() const { return RSquareLoc; } |
1747 | SourceRange getSourceRange() const { |
1748 | return SourceRange(LSquareLoc, RSquareLoc); |
1749 | } |
1750 | }; |
1751 | |
1752 | |
1753 | |
1754 | enum class FunctionDefinitionKind { |
1755 | Declaration, |
1756 | Definition, |
1757 | Defaulted, |
1758 | Deleted |
1759 | }; |
1760 | |
1761 | enum class DeclaratorContext { |
1762 | File, |
1763 | Prototype, |
1764 | ObjCResult, |
1765 | ObjCParameter, |
1766 | KNRTypeList, |
1767 | TypeName, |
1768 | FunctionalCast, |
1769 | Member, |
1770 | Block, |
1771 | ForInit, |
1772 | SelectionInit, |
1773 | Condition, |
1774 | TemplateParam, |
1775 | CXXNew, |
1776 | CXXCatch, |
1777 | ObjCCatch, |
1778 | BlockLiteral, |
1779 | LambdaExpr, |
1780 | LambdaExprParameter, |
1781 | ConversionId, |
1782 | TrailingReturn, |
1783 | TrailingReturnVar, |
1784 | TemplateArg, |
1785 | TemplateTypeArg, |
1786 | AliasDecl, |
1787 | AliasTemplate, |
1788 | RequiresExpr |
1789 | }; |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | |
1795 | |
1796 | |
1797 | |
1798 | |
1799 | |
1800 | |
1801 | |
1802 | |
1803 | class Declarator { |
1804 | |
1805 | private: |
1806 | const DeclSpec &DS; |
1807 | CXXScopeSpec SS; |
1808 | UnqualifiedId Name; |
1809 | SourceRange Range; |
1810 | |
1811 | |
1812 | DeclaratorContext Context; |
1813 | |
1814 | |
1815 | DecompositionDeclarator BindingGroup; |
1816 | |
1817 | |
1818 | |
1819 | |
1820 | |
1821 | SmallVector<DeclaratorChunk, 8> DeclTypeInfo; |
1822 | |
1823 | |
1824 | unsigned InvalidType : 1; |
1825 | |
1826 | |
1827 | unsigned GroupingParens : 1; |
1828 | |
1829 | |
1830 | |
1831 | |
1832 | |
1833 | unsigned FunctionDefinition : 2; |
1834 | |
1835 | |
1836 | unsigned Redeclaration : 1; |
1837 | |
1838 | |
1839 | unsigned Extension : 1; |
1840 | |
1841 | |
1842 | unsigned ObjCIvar : 1; |
1843 | |
1844 | |
1845 | unsigned ObjCWeakProperty : 1; |
1846 | |
1847 | |
1848 | unsigned InlineStorageUsed : 1; |
1849 | |
1850 | |
1851 | unsigned HasInitializer : 1; |
1852 | |
1853 | |
1854 | ParsedAttributes Attrs; |
1855 | |
1856 | |
1857 | Expr *AsmLabel; |
1858 | |
1859 | |
1860 | |
1861 | Expr *TrailingRequiresClause; |
1862 | |
1863 | |
1864 | ArrayRef<TemplateParameterList *> TemplateParameterLists; |
1865 | |
1866 | |
1867 | |
1868 | |
1869 | TemplateParameterList *InventedTemplateParameterList; |
1870 | |
1871 | #ifndef _MSC_VER |
1872 | union { |
1873 | #endif |
1874 | |
1875 | |
1876 | |
1877 | DeclaratorChunk::ParamInfo InlineParams[16]; |
1878 | DecompositionDeclarator::Binding InlineBindings[16]; |
1879 | #ifndef _MSC_VER |
1880 | }; |
1881 | #endif |
1882 | |
1883 | |
1884 | |
1885 | SourceLocation CommaLoc; |
1886 | |
1887 | |
1888 | |
1889 | SourceLocation EllipsisLoc; |
1890 | |
1891 | friend struct DeclaratorChunk; |
1892 | |
1893 | public: |
1894 | Declarator(const DeclSpec &ds, DeclaratorContext C) |
1895 | : DS(ds), Range(ds.getSourceRange()), Context(C), |
1896 | InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error), |
1897 | GroupingParens(false), FunctionDefinition(static_cast<unsigned>( |
1898 | FunctionDefinitionKind::Declaration)), |
1899 | Redeclaration(false), Extension(false), ObjCIvar(false), |
1900 | ObjCWeakProperty(false), InlineStorageUsed(false), |
1901 | HasInitializer(false), Attrs(ds.getAttributePool().getFactory()), |
1902 | AsmLabel(nullptr), TrailingRequiresClause(nullptr), |
1903 | InventedTemplateParameterList(nullptr) {} |
1904 | |
1905 | ~Declarator() { |
1906 | clear(); |
1907 | } |
1908 | |
1909 | |
1910 | const DeclSpec &getDeclSpec() const { return DS; } |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); } |
1918 | |
1919 | AttributePool &getAttributePool() const { |
1920 | return Attrs.getPool(); |
1921 | } |
1922 | |
1923 | |
1924 | |
1925 | const CXXScopeSpec &getCXXScopeSpec() const { return SS; } |
1926 | CXXScopeSpec &getCXXScopeSpec() { return SS; } |
1927 | |
1928 | |
1929 | UnqualifiedId &getName() { return Name; } |
1930 | |
1931 | const DecompositionDeclarator &getDecompositionDeclarator() const { |
1932 | return BindingGroup; |
1933 | } |
1934 | |
1935 | DeclaratorContext getContext() const { return Context; } |
1936 | |
1937 | bool isPrototypeContext() const { |
1938 | return (Context == DeclaratorContext::Prototype || |
1939 | Context == DeclaratorContext::ObjCParameter || |
1940 | Context == DeclaratorContext::ObjCResult || |
1941 | Context == DeclaratorContext::LambdaExprParameter); |
1942 | } |
1943 | |
1944 | |
1945 | SourceRange getSourceRange() const LLVM_READONLY { return Range; } |
1946 | SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } |
1947 | SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } |
1948 | |
1949 | void SetSourceRange(SourceRange R) { Range = R; } |
1950 | |
1951 | |
1952 | void SetRangeBegin(SourceLocation Loc) { |
1953 | if (!Loc.isInvalid()) |
1954 | Range.setBegin(Loc); |
1955 | } |
1956 | |
1957 | void SetRangeEnd(SourceLocation Loc) { |
1958 | if (!Loc.isInvalid()) |
1959 | Range.setEnd(Loc); |
1960 | } |
1961 | |
1962 | |
1963 | |
1964 | void ExtendWithDeclSpec(const DeclSpec &DS) { |
1965 | SourceRange SR = DS.getSourceRange(); |
1966 | if (Range.getBegin().isInvalid()) |
1967 | Range.setBegin(SR.getBegin()); |
1968 | if (!SR.getEnd().isInvalid()) |
1969 | Range.setEnd(SR.getEnd()); |
1970 | } |
1971 | |
1972 | |
1973 | void clear() { |
1974 | SS.clear(); |
1975 | Name.clear(); |
1976 | Range = DS.getSourceRange(); |
1977 | BindingGroup.clear(); |
1978 | |
1979 | for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i) |
1980 | DeclTypeInfo[i].destroy(); |
1981 | DeclTypeInfo.clear(); |
1982 | Attrs.clear(); |
1983 | AsmLabel = nullptr; |
1984 | InlineStorageUsed = false; |
1985 | HasInitializer = false; |
1986 | ObjCIvar = false; |
1987 | ObjCWeakProperty = false; |
1988 | CommaLoc = SourceLocation(); |
1989 | EllipsisLoc = SourceLocation(); |
1990 | } |
1991 | |
1992 | |
1993 | |
1994 | |
1995 | bool mayOmitIdentifier() const { |
1996 | switch (Context) { |
1997 | case DeclaratorContext::File: |
1998 | case DeclaratorContext::KNRTypeList: |
1999 | case DeclaratorContext::Member: |
2000 | case DeclaratorContext::Block: |
2001 | case DeclaratorContext::ForInit: |
2002 | case DeclaratorContext::SelectionInit: |
2003 | case DeclaratorContext::Condition: |
2004 | return false; |
2005 | |
2006 | case DeclaratorContext::TypeName: |
2007 | case DeclaratorContext::FunctionalCast: |
2008 | case DeclaratorContext::AliasDecl: |
2009 | case DeclaratorContext::AliasTemplate: |
2010 | case DeclaratorContext::Prototype: |
2011 | case DeclaratorContext::LambdaExprParameter: |
2012 | case DeclaratorContext::ObjCParameter: |
2013 | case DeclaratorContext::ObjCResult: |
2014 | case DeclaratorContext::TemplateParam: |
2015 | case DeclaratorContext::CXXNew: |
2016 | case DeclaratorContext::CXXCatch: |
2017 | case DeclaratorContext::ObjCCatch: |
2018 | case DeclaratorContext::BlockLiteral: |
2019 | case DeclaratorContext::LambdaExpr: |
2020 | case DeclaratorContext::ConversionId: |
2021 | case DeclaratorContext::TemplateArg: |
2022 | case DeclaratorContext::TemplateTypeArg: |
2023 | case DeclaratorContext::TrailingReturn: |
2024 | case DeclaratorContext::TrailingReturnVar: |
2025 | case DeclaratorContext::RequiresExpr: |
2026 | return true; |
2027 | } |
2028 | llvm_unreachable("unknown context kind!"); |
2029 | } |
2030 | |
2031 | |
2032 | |
2033 | |
2034 | bool mayHaveIdentifier() const { |
2035 | switch (Context) { |
2036 | case DeclaratorContext::File: |
2037 | case DeclaratorContext::KNRTypeList: |
2038 | case DeclaratorContext::Member: |
2039 | case DeclaratorContext::Block: |
2040 | case DeclaratorContext::ForInit: |
2041 | case DeclaratorContext::SelectionInit: |
2042 | case DeclaratorContext::Condition: |
2043 | case DeclaratorContext::Prototype: |
2044 | case DeclaratorContext::LambdaExprParameter: |
2045 | case DeclaratorContext::TemplateParam: |
2046 | case DeclaratorContext::CXXCatch: |
2047 | case DeclaratorContext::ObjCCatch: |
2048 | case DeclaratorContext::RequiresExpr: |
2049 | return true; |
2050 | |
2051 | case DeclaratorContext::TypeName: |
2052 | case DeclaratorContext::FunctionalCast: |
2053 | case DeclaratorContext::CXXNew: |
2054 | case DeclaratorContext::AliasDecl: |
2055 | case DeclaratorContext::AliasTemplate: |
2056 | case DeclaratorContext::ObjCParameter: |
2057 | case DeclaratorContext::ObjCResult: |
2058 | case DeclaratorContext::BlockLiteral: |
2059 | case DeclaratorContext::LambdaExpr: |
2060 | case DeclaratorContext::ConversionId: |
2061 | case DeclaratorContext::TemplateArg: |
2062 | case DeclaratorContext::TemplateTypeArg: |
2063 | case DeclaratorContext::TrailingReturn: |
2064 | case DeclaratorContext::TrailingReturnVar: |
2065 | return false; |
2066 | } |
2067 | llvm_unreachable("unknown context kind!"); |
2068 | } |
2069 | |
2070 | |
2071 | bool mayHaveDecompositionDeclarator() const { |
2072 | switch (Context) { |
2073 | case DeclaratorContext::File: |
2074 | |
2075 | |
2076 | case DeclaratorContext::Block: |
2077 | case DeclaratorContext::ForInit: |
2078 | case DeclaratorContext::SelectionInit: |
2079 | case DeclaratorContext::Condition: |
2080 | return true; |
2081 | |
2082 | case DeclaratorContext::Member: |
2083 | case DeclaratorContext::Prototype: |
2084 | case DeclaratorContext::TemplateParam: |
2085 | case DeclaratorContext::RequiresExpr: |
2086 | |
2087 | return false; |
2088 | |
2089 | |
2090 | case DeclaratorContext::KNRTypeList: |
2091 | case DeclaratorContext::TypeName: |
2092 | case DeclaratorContext::FunctionalCast: |
2093 | case DeclaratorContext::AliasDecl: |
2094 | case DeclaratorContext::AliasTemplate: |
2095 | case DeclaratorContext::LambdaExprParameter: |
2096 | case DeclaratorContext::ObjCParameter: |
2097 | case DeclaratorContext::ObjCResult: |
2098 | case DeclaratorContext::CXXNew: |
2099 | case DeclaratorContext::CXXCatch: |
2100 | case DeclaratorContext::ObjCCatch: |
2101 | case DeclaratorContext::BlockLiteral: |
2102 | case DeclaratorContext::LambdaExpr: |
2103 | case DeclaratorContext::ConversionId: |
2104 | case DeclaratorContext::TemplateArg: |
2105 | case DeclaratorContext::TemplateTypeArg: |
2106 | case DeclaratorContext::TrailingReturn: |
2107 | case DeclaratorContext::TrailingReturnVar: |
2108 | return false; |
2109 | } |
2110 | llvm_unreachable("unknown context kind!"); |
2111 | } |
2112 | |
2113 | |
2114 | |
2115 | bool mayBeFollowedByCXXDirectInit() const { |
2116 | if (hasGroupingParens()) return false; |
2117 | |
2118 | if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
2119 | return false; |
2120 | |
2121 | if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern && |
2122 | Context != DeclaratorContext::File) |
2123 | return false; |
2124 | |
2125 | |
2126 | if (Name.getKind() != UnqualifiedIdKind::IK_Identifier) |
2127 | return false; |
2128 | |
2129 | switch (Context) { |
2130 | case DeclaratorContext::File: |
2131 | case DeclaratorContext::Block: |
2132 | case DeclaratorContext::ForInit: |
2133 | case DeclaratorContext::SelectionInit: |
2134 | case DeclaratorContext::TrailingReturnVar: |
2135 | return true; |
2136 | |
2137 | case DeclaratorContext::Condition: |
2138 | |
2139 | |
2140 | |
2141 | return true; |
2142 | |
2143 | case DeclaratorContext::KNRTypeList: |
2144 | case DeclaratorContext::Member: |
2145 | case DeclaratorContext::Prototype: |
2146 | case DeclaratorContext::LambdaExprParameter: |
2147 | case DeclaratorContext::ObjCParameter: |
2148 | case DeclaratorContext::ObjCResult: |
2149 | case DeclaratorContext::TemplateParam: |
2150 | case DeclaratorContext::CXXCatch: |
2151 | case DeclaratorContext::ObjCCatch: |
2152 | case DeclaratorContext::TypeName: |
2153 | case DeclaratorContext::FunctionalCast: |
2154 | case DeclaratorContext::CXXNew: |
2155 | case DeclaratorContext::AliasDecl: |
2156 | case DeclaratorContext::AliasTemplate: |
2157 | case DeclaratorContext::BlockLiteral: |
2158 | case DeclaratorContext::LambdaExpr: |
2159 | case DeclaratorContext::ConversionId: |
2160 | case DeclaratorContext::TemplateArg: |
2161 | case DeclaratorContext::TemplateTypeArg: |
2162 | case DeclaratorContext::TrailingReturn: |
2163 | case DeclaratorContext::RequiresExpr: |
2164 | return false; |
2165 | } |
2166 | llvm_unreachable("unknown context kind!"); |
2167 | } |
2168 | |
2169 | |
2170 | |
2171 | |
2172 | bool isPastIdentifier() const { return Name.isValid(); } |
2173 | |
2174 | |
2175 | |
2176 | |
2177 | |
2178 | bool hasName() const { |
2179 | return Name.getKind() != UnqualifiedIdKind::IK_Identifier || |
2180 | Name.Identifier || isDecompositionDeclarator(); |
2181 | } |
2182 | |
2183 | |
2184 | bool isDecompositionDeclarator() const { |
2185 | return BindingGroup.isSet(); |
2186 | } |
2187 | |
2188 | IdentifierInfo *getIdentifier() const { |
2189 | if (Name.getKind() == UnqualifiedIdKind::IK_Identifier) |
2190 | return Name.Identifier; |
2191 | |
2192 | return nullptr; |
2193 | } |
2194 | SourceLocation getIdentifierLoc() const { return Name.StartLocation; } |
2195 | |
2196 | |
2197 | void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) { |
2198 | Name.setIdentifier(Id, IdLoc); |
2199 | } |
2200 | |
2201 | |
2202 | void |
2203 | setDecompositionBindings(SourceLocation LSquareLoc, |
2204 | ArrayRef<DecompositionDeclarator::Binding> Bindings, |
2205 | SourceLocation RSquareLoc); |
2206 | |
2207 | |
2208 | |
2209 | |
2210 | |
2211 | void AddTypeInfo(const DeclaratorChunk &TI, ParsedAttributes &&attrs, |
2212 | SourceLocation EndLoc) { |
2213 | DeclTypeInfo.push_back(TI); |
2214 | DeclTypeInfo.back().getAttrs().addAll(attrs.begin(), attrs.end()); |
2215 | getAttributePool().takeAllFrom(attrs.getPool()); |
2216 | |
2217 | if (!EndLoc.isInvalid()) |
2218 | SetRangeEnd(EndLoc); |
2219 | } |
2220 | |
2221 | |
2222 | |
2223 | void AddTypeInfo(const DeclaratorChunk &TI, SourceLocation EndLoc) { |
2224 | DeclTypeInfo.push_back(TI); |
2225 | |
2226 | if (!EndLoc.isInvalid()) |
2227 | SetRangeEnd(EndLoc); |
2228 | } |
2229 | |
2230 | |
2231 | void AddInnermostTypeInfo(const DeclaratorChunk &TI) { |
2232 | DeclTypeInfo.insert(DeclTypeInfo.begin(), TI); |
2233 | } |
2234 | |
2235 | |
2236 | unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); } |
2237 | |
2238 | |
2239 | |
2240 | const DeclaratorChunk &getTypeObject(unsigned i) const { |
2241 | assert(i < DeclTypeInfo.size() && "Invalid type chunk"); |
2242 | return DeclTypeInfo[i]; |
2243 | } |
2244 | DeclaratorChunk &getTypeObject(unsigned i) { |
2245 | assert(i < DeclTypeInfo.size() && "Invalid type chunk"); |
2246 | return DeclTypeInfo[i]; |
2247 | } |
2248 | |
2249 | typedef SmallVectorImpl<DeclaratorChunk>::const_iterator type_object_iterator; |
2250 | typedef llvm::iterator_range<type_object_iterator> type_object_range; |
2251 | |
2252 | |
2253 | type_object_range type_objects() const { |
2254 | return type_object_range(DeclTypeInfo.begin(), DeclTypeInfo.end()); |
2255 | } |
2256 | |
2257 | void DropFirstTypeObject() { |
2258 | assert(!DeclTypeInfo.empty() && "No type chunks to drop."); |
2259 | DeclTypeInfo.front().destroy(); |
2260 | DeclTypeInfo.erase(DeclTypeInfo.begin()); |
2261 | } |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | const DeclaratorChunk *getInnermostNonParenChunk() const { |
2267 | for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { |
2268 | if (!DeclTypeInfo[i].isParen()) |
2269 | return &DeclTypeInfo[i]; |
2270 | } |
2271 | return nullptr; |
2272 | } |
2273 | |
2274 | |
2275 | |
2276 | |
2277 | const DeclaratorChunk *getOutermostNonParenChunk() const { |
2278 | for (unsigned i = DeclTypeInfo.size(), i_end = 0; i != i_end; --i) { |
2279 | if (!DeclTypeInfo[i-1].isParen()) |
2280 | return &DeclTypeInfo[i-1]; |
2281 | } |
2282 | return nullptr; |
2283 | } |
2284 | |
2285 | |
2286 | |
2287 | |
2288 | bool isArrayOfUnknownBound() const { |
2289 | const DeclaratorChunk *chunk = getInnermostNonParenChunk(); |
2290 | return (chunk && chunk->Kind == DeclaratorChunk::Array && |
2291 | !chunk->Arr.NumElts); |
2292 | } |
2293 | |
2294 | |
2295 | |
2296 | |
2297 | |
2298 | bool isFunctionDeclarator(unsigned& idx) const { |
2299 | for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { |
2300 | switch (DeclTypeInfo[i].Kind) { |
2301 | case DeclaratorChunk::Function: |
2302 | idx = i; |
2303 | return true; |
2304 | case DeclaratorChunk::Paren: |
2305 | continue; |
2306 | case DeclaratorChunk::Pointer: |
2307 | case DeclaratorChunk::Reference: |
2308 | case DeclaratorChunk::Array: |
2309 | case DeclaratorChunk::BlockPointer: |
2310 | case DeclaratorChunk::MemberPointer: |
2311 | case DeclaratorChunk::Pipe: |
2312 | return false; |
2313 | } |
2314 | llvm_unreachable("Invalid type chunk"); |
2315 | } |
2316 | return false; |
2317 | } |
2318 | |
2319 | |
2320 | |
2321 | |
2322 | bool isFunctionDeclarator() const { |
2323 | unsigned index; |
2324 | return isFunctionDeclarator(index); |
2325 | } |
2326 | |
2327 | |
2328 | |
2329 | DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() { |
2330 | assert(isFunctionDeclarator() && "Not a function declarator!"); |
2331 | unsigned index = 0; |
2332 | isFunctionDeclarator(index); |
2333 | return DeclTypeInfo[index].Fun; |
2334 | } |
2335 | |
2336 | |
2337 | |
2338 | const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const { |
2339 | return const_cast<Declarator*>(this)->getFunctionTypeInfo(); |
2340 | } |
2341 | |
2342 | |
2343 | |
2344 | |
2345 | |
2346 | |
2347 | |
2348 | bool isDeclarationOfFunction() const; |
2349 | |
2350 | |
2351 | |
2352 | bool isFunctionDeclarationContext() const { |
2353 | if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
2354 | return false; |
2355 | |
2356 | switch (Context) { |
2357 | case DeclaratorContext::File: |
2358 | case DeclaratorContext::Member: |
2359 | case DeclaratorContext::Block: |
2360 | case DeclaratorContext::ForInit: |
2361 | case DeclaratorContext::SelectionInit: |
2362 | return true; |
2363 | |
2364 | case DeclaratorContext::Condition: |
2365 | case DeclaratorContext::KNRTypeList: |
2366 | case DeclaratorContext::TypeName: |
2367 | case DeclaratorContext::FunctionalCast: |
2368 | case DeclaratorContext::AliasDecl: |
2369 | case DeclaratorContext::AliasTemplate: |
2370 | case DeclaratorContext::Prototype: |
2371 | case DeclaratorContext::LambdaExprParameter: |
2372 | case DeclaratorContext::ObjCParameter: |
2373 | case DeclaratorContext::ObjCResult: |
2374 | case DeclaratorContext::TemplateParam: |
2375 | case DeclaratorContext::CXXNew: |
2376 | case DeclaratorContext::CXXCatch: |
2377 | case DeclaratorContext::ObjCCatch: |
2378 | case DeclaratorContext::BlockLiteral: |
2379 | case DeclaratorContext::LambdaExpr: |
2380 | case DeclaratorContext::ConversionId: |
2381 | case DeclaratorContext::TemplateArg: |
2382 | case DeclaratorContext::TemplateTypeArg: |
2383 | case DeclaratorContext::TrailingReturn: |
2384 | case DeclaratorContext::TrailingReturnVar: |
2385 | case DeclaratorContext::RequiresExpr: |
2386 | return false; |
2387 | } |
2388 | llvm_unreachable("unknown context kind!"); |
2389 | } |
2390 | |
2391 | |
2392 | |
2393 | bool isExpressionContext() const { |
2394 | switch (Context) { |
2395 | case DeclaratorContext::File: |
2396 | case DeclaratorContext::KNRTypeList: |
2397 | case DeclaratorContext::Member: |
2398 | |
2399 | |
2400 | case DeclaratorContext::TypeName: |
2401 | |
2402 | case DeclaratorContext::FunctionalCast: |
2403 | case DeclaratorContext::AliasDecl: |
2404 | case DeclaratorContext::AliasTemplate: |
2405 | case DeclaratorContext::Prototype: |
2406 | case DeclaratorContext::LambdaExprParameter: |
2407 | case DeclaratorContext::ObjCParameter: |
2408 | case DeclaratorContext::ObjCResult: |
2409 | case DeclaratorContext::TemplateParam: |
2410 | case DeclaratorContext::CXXNew: |
2411 | case DeclaratorContext::CXXCatch: |
2412 | case DeclaratorContext::ObjCCatch: |
2413 | case DeclaratorContext::BlockLiteral: |
2414 | case DeclaratorContext::LambdaExpr: |
2415 | case DeclaratorContext::ConversionId: |
2416 | case DeclaratorContext::TrailingReturn: |
2417 | case DeclaratorContext::TrailingReturnVar: |
2418 | case DeclaratorContext::TemplateTypeArg: |
2419 | case DeclaratorContext::RequiresExpr: |
2420 | return false; |
2421 | |
2422 | case DeclaratorContext::Block: |
2423 | case DeclaratorContext::ForInit: |
2424 | case DeclaratorContext::SelectionInit: |
2425 | case DeclaratorContext::Condition: |
2426 | case DeclaratorContext::TemplateArg: |
2427 | return true; |
2428 | } |
2429 | |
2430 | llvm_unreachable("unknown context kind!"); |
2431 | } |
2432 | |
2433 | |
2434 | |
2435 | bool isFunctionDeclaratorAFunctionDeclaration() const { |
2436 | if (!isFunctionDeclarationContext()) |
2437 | return false; |
2438 | |
2439 | for (unsigned I = 0, N = getNumTypeObjects(); I != N; ++I) |
2440 | if (getTypeObject(I).Kind != DeclaratorChunk::Paren) |
2441 | return false; |
2442 | |
2443 | return true; |
2444 | } |
2445 | |
2446 | |
2447 | |
2448 | bool hasTrailingReturnType() const { |
2449 | for (const auto &Chunk : type_objects()) |
2450 | if (Chunk.Kind == DeclaratorChunk::Function && |
2451 | Chunk.Fun.hasTrailingReturnType()) |
2452 | return true; |
2453 | return false; |
2454 | } |
2455 | |
2456 | |
2457 | ParsedType getTrailingReturnType() const { |
2458 | for (const auto &Chunk : type_objects()) |
2459 | if (Chunk.Kind == DeclaratorChunk::Function && |
2460 | Chunk.Fun.hasTrailingReturnType()) |
2461 | return Chunk.Fun.getTrailingReturnType(); |
2462 | return ParsedType(); |
2463 | } |
2464 | |
2465 | |
2466 | void setTrailingRequiresClause(Expr *TRC) { |
2467 | TrailingRequiresClause = TRC; |
2468 | |
2469 | SetRangeEnd(TRC->getEndLoc()); |
2470 | } |
2471 | |
2472 | |
2473 | Expr *getTrailingRequiresClause() { |
2474 | return TrailingRequiresClause; |
2475 | } |
2476 | |
2477 | |
2478 | |
2479 | bool hasTrailingRequiresClause() const { |
2480 | return TrailingRequiresClause != nullptr; |
2481 | } |
2482 | |
2483 | |
2484 | void setTemplateParameterLists(ArrayRef<TemplateParameterList *> TPLs) { |
2485 | TemplateParameterLists = TPLs; |
2486 | } |
2487 | |
2488 | |
2489 | ArrayRef<TemplateParameterList *> getTemplateParameterLists() const { |
2490 | return TemplateParameterLists; |
2491 | } |
2492 | |
2493 | |
2494 | |
2495 | |
2496 | void setInventedTemplateParameterList(TemplateParameterList *Invented) { |
2497 | InventedTemplateParameterList = Invented; |
2498 | } |
2499 | |
2500 | |
2501 | |
2502 | |
2503 | TemplateParameterList * getInventedTemplateParameterList() const { |
2504 | return InventedTemplateParameterList; |
2505 | } |
2506 | |
2507 | |
2508 | |
2509 | |
2510 | |
2511 | |
2512 | |
2513 | |
2514 | |
2515 | |
2516 | void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) { |
2517 | Attrs.takeAllFrom(attrs); |
2518 | |
2519 | if (!lastLoc.isInvalid()) |
2520 | SetRangeEnd(lastLoc); |
2521 | } |
2522 | |
2523 | const ParsedAttributes &getAttributes() const { return Attrs; } |
2524 | ParsedAttributes &getAttributes() { return Attrs; } |
2525 | |
2526 | |
2527 | bool hasAttributes() const { |
2528 | if (!getAttributes().empty() || getDeclSpec().hasAttributes()) |
2529 | return true; |
2530 | for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i) |
2531 | if (!getTypeObject(i).getAttrs().empty()) |
2532 | return true; |
2533 | return false; |
2534 | } |
2535 | |
2536 | |
2537 | |
2538 | void getCXX11AttributeRanges(SmallVectorImpl<SourceRange> &Ranges) { |
2539 | for (const ParsedAttr &AL : Attrs) |
2540 | if (AL.isCXX11Attribute()) |
2541 | Ranges.push_back(AL.getRange()); |
2542 | } |
2543 | |
2544 | void setAsmLabel(Expr *E) { AsmLabel = E; } |
2545 | Expr *getAsmLabel() const { return AsmLabel; } |
2546 | |
2547 | void setExtension(bool Val = true) { Extension = Val; } |
2548 | bool getExtension() const { return Extension; } |
2549 | |
2550 | void setObjCIvar(bool Val = true) { ObjCIvar = Val; } |
2551 | bool isObjCIvar() const { return ObjCIvar; } |
2552 | |
2553 | void setObjCWeakProperty(bool Val = true) { ObjCWeakProperty = Val; } |
2554 | bool isObjCWeakProperty() const { return ObjCWeakProperty; } |
2555 | |
2556 | void setInvalidType(bool Val = true) { InvalidType = Val; } |
2557 | bool isInvalidType() const { |
2558 | return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error; |
2559 | } |
2560 | |
2561 | void setGroupingParens(bool flag) { GroupingParens = flag; } |
2562 | bool hasGroupingParens() const { return GroupingParens; } |
2563 | |
2564 | bool isFirstDeclarator() const { return !CommaLoc.isValid(); } |
2565 | SourceLocation getCommaLoc() const { return CommaLoc; } |
2566 | void setCommaLoc(SourceLocation CL) { CommaLoc = CL; } |
2567 | |
2568 | bool hasEllipsis() const { return EllipsisLoc.isValid(); } |
2569 | SourceLocation getEllipsisLoc() const { return EllipsisLoc; } |
2570 | void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; } |
2571 | |
2572 | void setFunctionDefinitionKind(FunctionDefinitionKind Val) { |
2573 | FunctionDefinition = static_cast<unsigned>(Val); |
2574 | } |
2575 | |
2576 | bool isFunctionDefinition() const { |
2577 | return getFunctionDefinitionKind() != FunctionDefinitionKind::Declaration; |
2578 | } |
2579 | |
2580 | FunctionDefinitionKind getFunctionDefinitionKind() const { |
2581 | return (FunctionDefinitionKind)FunctionDefinition; |
2582 | } |
2583 | |
2584 | void setHasInitializer(bool Val = true) { HasInitializer = Val; } |
2585 | bool hasInitializer() const { return HasInitializer; } |
2586 | |
2587 | |
2588 | bool isFirstDeclarationOfMember() { |
2589 | return getContext() == DeclaratorContext::Member && |
2590 | !getDeclSpec().isFriendSpecified(); |
2591 | } |
2592 | |
2593 | |
2594 | |
2595 | |
2596 | bool isStaticMember(); |
2597 | |
2598 | |
2599 | bool isCtorOrDtor(); |
2600 | |
2601 | void setRedeclaration(bool Val) { Redeclaration = Val; } |
2602 | bool isRedeclaration() const { return Redeclaration; } |
2603 | }; |
2604 | |
2605 | |
2606 | |
2607 | struct FieldDeclarator { |
2608 | Declarator D; |
2609 | Expr *BitfieldSize; |
2610 | explicit FieldDeclarator(const DeclSpec &DS) |
2611 | : D(DS, DeclaratorContext::Member), BitfieldSize(nullptr) {} |
2612 | }; |
2613 | |
2614 | |
2615 | class VirtSpecifiers { |
2616 | public: |
2617 | enum Specifier { |
2618 | VS_None = 0, |
2619 | VS_Override = 1, |
2620 | VS_Final = 2, |
2621 | VS_Sealed = 4, |
2622 | |
2623 | VS_GNU_Final = 8, |
2624 | VS_Abstract = 16 |
2625 | }; |
2626 | |
2627 | VirtSpecifiers() : Specifiers(0), LastSpecifier(VS_None) { } |
2628 | |
2629 | bool SetSpecifier(Specifier VS, SourceLocation Loc, |
2630 | const char *&PrevSpec); |
2631 | |
2632 | bool isUnset() const { return Specifiers == 0; } |
2633 | |
2634 | bool isOverrideSpecified() const { return Specifiers & VS_Override; } |
2635 | SourceLocation getOverrideLoc() const { return VS_overrideLoc; } |
2636 | |
2637 | bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); } |
2638 | bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; } |
2639 | SourceLocation getFinalLoc() const { return VS_finalLoc; } |
2640 | SourceLocation getAbstractLoc() const { return VS_abstractLoc; } |
2641 | |
2642 | void clear() { Specifiers = 0; } |
2643 | |
2644 | static const char *getSpecifierName(Specifier VS); |
2645 | |
2646 | SourceLocation getFirstLocation() const { return FirstLocation; } |
2647 | SourceLocation getLastLocation() const { return LastLocation; } |
2648 | Specifier getLastSpecifier() const { return LastSpecifier; } |
2649 | |
2650 | private: |
2651 | unsigned Specifiers; |
2652 | Specifier LastSpecifier; |
2653 | |
2654 | SourceLocation VS_overrideLoc, VS_finalLoc, VS_abstractLoc; |
2655 | SourceLocation FirstLocation; |
2656 | SourceLocation LastLocation; |
2657 | }; |
2658 | |
2659 | enum class LambdaCaptureInitKind { |
2660 | NoInit, |
2661 | CopyInit, |
2662 | DirectInit, |
2663 | ListInit |
2664 | }; |
2665 | |
2666 | |
2667 | struct LambdaIntroducer { |
2668 | |
2669 | struct LambdaCapture { |
2670 | LambdaCaptureKind Kind; |
2671 | SourceLocation Loc; |
2672 | IdentifierInfo *Id; |
2673 | SourceLocation EllipsisLoc; |
2674 | LambdaCaptureInitKind InitKind; |
2675 | ExprResult Init; |
2676 | ParsedType InitCaptureType; |
2677 | SourceRange ExplicitRange; |
2678 | |
2679 | LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc, |
2680 | IdentifierInfo *Id, SourceLocation EllipsisLoc, |
2681 | LambdaCaptureInitKind InitKind, ExprResult Init, |
2682 | ParsedType InitCaptureType, |
2683 | SourceRange ExplicitRange) |
2684 | : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc), |
2685 | InitKind(InitKind), Init(Init), InitCaptureType(InitCaptureType), |
2686 | ExplicitRange(ExplicitRange) {} |
2687 | }; |
2688 | |
2689 | SourceRange Range; |
2690 | SourceLocation DefaultLoc; |
2691 | LambdaCaptureDefault Default; |
2692 | SmallVector<LambdaCapture, 4> Captures; |
2693 | |
2694 | LambdaIntroducer() |
2695 | : Default(LCD_None) {} |
2696 | |
2697 | |
2698 | void addCapture(LambdaCaptureKind Kind, |
2699 | SourceLocation Loc, |
2700 | IdentifierInfo* Id, |
2701 | SourceLocation EllipsisLoc, |
2702 | LambdaCaptureInitKind InitKind, |
2703 | ExprResult Init, |
2704 | ParsedType InitCaptureType, |
2705 | SourceRange ExplicitRange) { |
2706 | Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init, |
2707 | InitCaptureType, ExplicitRange)); |
2708 | } |
2709 | }; |
2710 | |
2711 | struct InventedTemplateParameterInfo { |
2712 | |
2713 | |
2714 | |
2715 | unsigned NumExplicitTemplateParams = 0; |
2716 | |
2717 | |
2718 | |
2719 | unsigned AutoTemplateParameterDepth = 0; |
2720 | |
2721 | |
2722 | |
2723 | |
2724 | |
2725 | |
2726 | |
2727 | |
2728 | SmallVector<NamedDecl*, 4> TemplateParams; |
2729 | }; |
2730 | |
2731 | } |
2732 | |
2733 | #endif // LLVM_CLANG_SEMA_DECLSPEC_H |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #ifndef LLVM_CLANG_AST_TYPE_H |
18 | #define LLVM_CLANG_AST_TYPE_H |
19 | |
20 | #include "clang/AST/DependenceFlags.h" |
21 | #include "clang/AST/NestedNameSpecifier.h" |
22 | #include "clang/AST/TemplateName.h" |
23 | #include "clang/Basic/AddressSpaces.h" |
24 | #include "clang/Basic/AttrKinds.h" |
25 | #include "clang/Basic/Diagnostic.h" |
26 | #include "clang/Basic/ExceptionSpecificationType.h" |
27 | #include "clang/Basic/LLVM.h" |
28 | #include "clang/Basic/Linkage.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/SourceLocation.h" |
31 | #include "clang/Basic/Specifiers.h" |
32 | #include "clang/Basic/Visibility.h" |
33 | #include "llvm/ADT/APInt.h" |
34 | #include "llvm/ADT/APSInt.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/FoldingSet.h" |
37 | #include "llvm/ADT/None.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/Twine.h" |
43 | #include "llvm/ADT/iterator_range.h" |
44 | #include "llvm/Support/Casting.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/PointerLikeTypeTraits.h" |
48 | #include "llvm/Support/TrailingObjects.h" |
49 | #include "llvm/Support/type_traits.h" |
50 | #include <cassert> |
51 | #include <cstddef> |
52 | #include <cstdint> |
53 | #include <cstring> |
54 | #include <string> |
55 | #include <type_traits> |
56 | #include <utility> |
57 | |
58 | namespace clang { |
59 | |
60 | class ExtQuals; |
61 | class QualType; |
62 | class ConceptDecl; |
63 | class TagDecl; |
64 | class TemplateParameterList; |
65 | class Type; |
66 | |
67 | enum { |
68 | TypeAlignmentInBits = 4, |
69 | TypeAlignment = 1 << TypeAlignmentInBits |
70 | }; |
71 | |
72 | namespace serialization { |
73 | template <class T> class AbstractTypeReader; |
74 | template <class T> class AbstractTypeWriter; |
75 | } |
76 | |
77 | } |
78 | |
79 | namespace llvm { |
80 | |
81 | template <typename T> |
82 | struct PointerLikeTypeTraits; |
83 | template<> |
84 | struct PointerLikeTypeTraits< ::clang::Type*> { |
85 | static inline void *getAsVoidPointer(::clang::Type *P) { return P; } |
86 | |
87 | static inline ::clang::Type *getFromVoidPointer(void *P) { |
88 | return static_cast< ::clang::Type*>(P); |
89 | } |
90 | |
91 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
92 | }; |
93 | |
94 | template<> |
95 | struct PointerLikeTypeTraits< ::clang::ExtQuals*> { |
96 | static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } |
97 | |
98 | static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { |
99 | return static_cast< ::clang::ExtQuals*>(P); |
100 | } |
101 | |
102 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
103 | }; |
104 | |
105 | } |
106 | |
107 | namespace clang { |
108 | |
109 | class ASTContext; |
110 | template <typename> class CanQual; |
111 | class CXXRecordDecl; |
112 | class DeclContext; |
113 | class EnumDecl; |
114 | class Expr; |
115 | class ExtQualsTypeCommonBase; |
116 | class FunctionDecl; |
117 | class IdentifierInfo; |
118 | class NamedDecl; |
119 | class ObjCInterfaceDecl; |
120 | class ObjCProtocolDecl; |
121 | class ObjCTypeParamDecl; |
122 | struct PrintingPolicy; |
123 | class RecordDecl; |
124 | class Stmt; |
125 | class TagDecl; |
126 | class TemplateArgument; |
127 | class TemplateArgumentListInfo; |
128 | class TemplateArgumentLoc; |
129 | class TemplateTypeParmDecl; |
130 | class TypedefNameDecl; |
131 | class UnresolvedUsingTypenameDecl; |
132 | |
133 | using CanQualType = CanQual<Type>; |
134 | |
135 | |
136 | #define TYPE(Class, Base) class Class##Type; |
137 | #include "clang/AST/TypeNodes.inc" |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | class Qualifiers { |
146 | public: |
147 | enum TQ { |
148 | Const = 0x1, |
149 | Restrict = 0x2, |
150 | Volatile = 0x4, |
151 | CVRMask = Const | Volatile | Restrict |
152 | }; |
153 | |
154 | enum GC { |
155 | GCNone = 0, |
156 | Weak, |
157 | Strong |
158 | }; |
159 | |
160 | enum ObjCLifetime { |
161 | |
162 | OCL_None, |
163 | |
164 | |
165 | |
166 | OCL_ExplicitNone, |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | OCL_Strong, |
174 | |
175 | |
176 | OCL_Weak, |
177 | |
178 | |
179 | OCL_Autoreleasing |
180 | }; |
181 | |
182 | enum { |
183 | |
184 | |
185 | MaxAddressSpace = 0x7fffffu, |
186 | |
187 | |
188 | FastWidth = 3, |
189 | |
190 | |
191 | FastMask = (1 << FastWidth) - 1 |
192 | }; |
193 | |
194 | |
195 | |
196 | static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { |
197 | |
198 | if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { |
199 | Qualifiers Q; |
200 | Q.Mask = L.Mask & R.Mask; |
201 | L.Mask &= ~Q.Mask; |
202 | R.Mask &= ~Q.Mask; |
203 | return Q; |
204 | } |
205 | |
206 | Qualifiers Q; |
207 | unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); |
208 | Q.addCVRQualifiers(CommonCRV); |
209 | L.removeCVRQualifiers(CommonCRV); |
210 | R.removeCVRQualifiers(CommonCRV); |
211 | |
212 | if (L.getObjCGCAttr() == R.getObjCGCAttr()) { |
213 | Q.setObjCGCAttr(L.getObjCGCAttr()); |
214 | L.removeObjCGCAttr(); |
215 | R.removeObjCGCAttr(); |
216 | } |
217 | |
218 | if (L.getObjCLifetime() == R.getObjCLifetime()) { |
219 | Q.setObjCLifetime(L.getObjCLifetime()); |
220 | L.removeObjCLifetime(); |
221 | R.removeObjCLifetime(); |
222 | } |
223 | |
224 | if (L.getAddressSpace() == R.getAddressSpace()) { |
225 | Q.setAddressSpace(L.getAddressSpace()); |
226 | L.removeAddressSpace(); |
227 | R.removeAddressSpace(); |
228 | } |
229 | return Q; |
230 | } |
231 | |
232 | static Qualifiers fromFastMask(unsigned Mask) { |
233 | Qualifiers Qs; |
234 | Qs.addFastQualifiers(Mask); |
235 | return Qs; |
236 | } |
237 | |
238 | static Qualifiers fromCVRMask(unsigned CVR) { |
239 | Qualifiers Qs; |
240 | Qs.addCVRQualifiers(CVR); |
241 | return Qs; |
242 | } |
243 | |
244 | static Qualifiers fromCVRUMask(unsigned CVRU) { |
245 | Qualifiers Qs; |
246 | Qs.addCVRUQualifiers(CVRU); |
247 | return Qs; |
248 | } |
249 | |
250 | |
251 | static Qualifiers fromOpaqueValue(unsigned opaque) { |
252 | Qualifiers Qs; |
253 | Qs.Mask = opaque; |
254 | return Qs; |
255 | } |
256 | |
257 | |
258 | unsigned getAsOpaqueValue() const { |
259 | return Mask; |
260 | } |
261 | |
262 | bool hasConst() const { return Mask & Const; } |
263 | bool hasOnlyConst() const { return Mask == Const; } |
264 | void removeConst() { Mask &= ~Const; } |
265 | void addConst() { Mask |= Const; } |
266 | |
267 | bool hasVolatile() const { return Mask & Volatile; } |
268 | bool hasOnlyVolatile() const { return Mask == Volatile; } |
269 | void removeVolatile() { Mask &= ~Volatile; } |
270 | void addVolatile() { Mask |= Volatile; } |
271 | |
272 | bool hasRestrict() const { return Mask & Restrict; } |
273 | bool hasOnlyRestrict() const { return Mask == Restrict; } |
274 | void removeRestrict() { Mask &= ~Restrict; } |
275 | void addRestrict() { Mask |= Restrict; } |
276 | |
277 | bool hasCVRQualifiers() const { return getCVRQualifiers(); } |
278 | unsigned getCVRQualifiers() const { return Mask & CVRMask; } |
279 | unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } |
280 | |
281 | void setCVRQualifiers(unsigned mask) { |
282 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
283 | Mask = (Mask & ~CVRMask) | mask; |
284 | } |
285 | void removeCVRQualifiers(unsigned mask) { |
286 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
287 | Mask &= ~mask; |
288 | } |
289 | void removeCVRQualifiers() { |
290 | removeCVRQualifiers(CVRMask); |
291 | } |
292 | void addCVRQualifiers(unsigned mask) { |
293 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
294 | Mask |= mask; |
295 | } |
296 | void addCVRUQualifiers(unsigned mask) { |
297 | assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits"); |
298 | Mask |= mask; |
299 | } |
300 | |
301 | bool hasUnaligned() const { return Mask & UMask; } |
302 | void setUnaligned(bool flag) { |
303 | Mask = (Mask & ~UMask) | (flag ? UMask : 0); |
304 | } |
305 | void removeUnaligned() { Mask &= ~UMask; } |
306 | void addUnaligned() { Mask |= UMask; } |
307 | |
308 | bool hasObjCGCAttr() const { return Mask & GCAttrMask; } |
309 | GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } |
310 | void setObjCGCAttr(GC type) { |
311 | Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); |
312 | } |
313 | void removeObjCGCAttr() { setObjCGCAttr(GCNone); } |
314 | void addObjCGCAttr(GC type) { |
315 | assert(type); |
316 | setObjCGCAttr(type); |
317 | } |
318 | Qualifiers withoutObjCGCAttr() const { |
319 | Qualifiers qs = *this; |
320 | qs.removeObjCGCAttr(); |
321 | return qs; |
322 | } |
323 | Qualifiers withoutObjCLifetime() const { |
324 | Qualifiers qs = *this; |
325 | qs.removeObjCLifetime(); |
326 | return qs; |
327 | } |
328 | Qualifiers withoutAddressSpace() const { |
329 | Qualifiers qs = *this; |
330 | qs.removeAddressSpace(); |
331 | return qs; |
332 | } |
333 | |
334 | bool hasObjCLifetime() const { return Mask & LifetimeMask; } |
335 | ObjCLifetime getObjCLifetime() const { |
336 | return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); |
337 | } |
338 | void setObjCLifetime(ObjCLifetime type) { |
339 | Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); |
340 | } |
341 | void removeObjCLifetime() { setObjCLifetime(OCL_None); } |
342 | void addObjCLifetime(ObjCLifetime type) { |
343 | assert(type); |
344 | assert(!hasObjCLifetime()); |
345 | Mask |= (type << LifetimeShift); |
346 | } |
347 | |
348 | |
349 | bool hasNonTrivialObjCLifetime() const { |
350 | ObjCLifetime lifetime = getObjCLifetime(); |
351 | return (lifetime > OCL_ExplicitNone); |
352 | } |
353 | |
354 | |
355 | bool hasStrongOrWeakObjCLifetime() const { |
356 | ObjCLifetime lifetime = getObjCLifetime(); |
357 | return (lifetime == OCL_Strong || lifetime == OCL_Weak); |
358 | } |
359 | |
360 | bool hasAddressSpace() const { return Mask & AddressSpaceMask; } |
361 | LangAS getAddressSpace() const { |
362 | return static_cast<LangAS>(Mask >> AddressSpaceShift); |
363 | } |
364 | bool hasTargetSpecificAddressSpace() const { |
365 | return isTargetAddressSpace(getAddressSpace()); |
366 | } |
367 | |
368 | unsigned getAddressSpaceAttributePrintValue() const { |
369 | auto Addr = getAddressSpace(); |
370 | |
371 | |
372 | |
373 | assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace()); |
374 | if (Addr != LangAS::Default) |
375 | return toTargetAddressSpace(Addr); |
376 | |
377 | |
378 | |
379 | return 0; |
380 | } |
381 | void setAddressSpace(LangAS space) { |
382 | assert((unsigned)space <= MaxAddressSpace); |
383 | Mask = (Mask & ~AddressSpaceMask) |
384 | | (((uint32_t) space) << AddressSpaceShift); |
385 | } |
386 | void removeAddressSpace() { setAddressSpace(LangAS::Default); } |
387 | void addAddressSpace(LangAS space) { |
388 | assert(space != LangAS::Default); |
389 | setAddressSpace(space); |
390 | } |
391 | |
392 | |
393 | |
394 | bool hasFastQualifiers() const { return getFastQualifiers(); } |
395 | unsigned getFastQualifiers() const { return Mask & FastMask; } |
396 | void setFastQualifiers(unsigned mask) { |
397 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
398 | Mask = (Mask & ~FastMask) | mask; |
399 | } |
400 | void removeFastQualifiers(unsigned mask) { |
401 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
402 | Mask &= ~mask; |
403 | } |
404 | void removeFastQualifiers() { |
405 | removeFastQualifiers(FastMask); |
406 | } |
407 | void addFastQualifiers(unsigned mask) { |
408 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
409 | Mask |= mask; |
410 | } |
411 | |
412 | |
413 | |
414 | bool hasNonFastQualifiers() const { return Mask & ~FastMask; } |
415 | Qualifiers getNonFastQualifiers() const { |
416 | Qualifiers Quals = *this; |
417 | Quals.setFastQualifiers(0); |
418 | return Quals; |
419 | } |
420 | |
421 | |
422 | bool hasQualifiers() const { return Mask; } |
423 | bool empty() const { return !Mask; } |
424 | |
425 | |
426 | void addQualifiers(Qualifiers Q) { |
427 | |
428 | |
429 | if (!(Q.Mask & ~CVRMask)) |
430 | Mask |= Q.Mask; |
431 | else { |
432 | Mask |= (Q.Mask & CVRMask); |
433 | if (Q.hasAddressSpace()) |
434 | addAddressSpace(Q.getAddressSpace()); |
435 | if (Q.hasObjCGCAttr()) |
436 | addObjCGCAttr(Q.getObjCGCAttr()); |
437 | if (Q.hasObjCLifetime()) |
438 | addObjCLifetime(Q.getObjCLifetime()); |
439 | } |
440 | } |
441 | |
442 | |
443 | void removeQualifiers(Qualifiers Q) { |
444 | |
445 | |
446 | if (!(Q.Mask & ~CVRMask)) |
447 | Mask &= ~Q.Mask; |
448 | else { |
449 | Mask &= ~(Q.Mask & CVRMask); |
450 | if (getObjCGCAttr() == Q.getObjCGCAttr()) |
451 | removeObjCGCAttr(); |
452 | if (getObjCLifetime() == Q.getObjCLifetime()) |
453 | removeObjCLifetime(); |
454 | if (getAddressSpace() == Q.getAddressSpace()) |
455 | removeAddressSpace(); |
456 | } |
457 | } |
458 | |
459 | |
460 | |
461 | void addConsistentQualifiers(Qualifiers qs) { |
462 | assert(getAddressSpace() == qs.getAddressSpace() || |
463 | !hasAddressSpace() || !qs.hasAddressSpace()); |
464 | assert(getObjCGCAttr() == qs.getObjCGCAttr() || |
465 | !hasObjCGCAttr() || !qs.hasObjCGCAttr()); |
466 | assert(getObjCLifetime() == qs.getObjCLifetime() || |
467 | !hasObjCLifetime() || !qs.hasObjCLifetime()); |
468 | Mask |= qs.Mask; |
469 | } |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | |
478 | static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) { |
479 | |
480 | return A == B || |
481 | |
482 | |
483 | (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || |
484 | |
485 | |
486 | |
487 | (A == LangAS::opencl_global && (B == LangAS::opencl_global_device || |
488 | B == LangAS::opencl_global_host)) || |
489 | (A == LangAS::sycl_global && (B == LangAS::sycl_global_device || |
490 | B == LangAS::sycl_global_host)) || |
491 | |
492 | ((isPtrSizeAddressSpace(A) || A == LangAS::Default) && |
493 | (isPtrSizeAddressSpace(B) || B == LangAS::Default)) || |
494 | |
495 | (A == LangAS::Default && |
496 | (B == LangAS::sycl_private || B == LangAS::sycl_local || |
497 | B == LangAS::sycl_global || B == LangAS::sycl_global_device || |
498 | B == LangAS::sycl_global_host)); |
499 | } |
500 | |
501 | |
502 | |
503 | bool isAddressSpaceSupersetOf(Qualifiers other) const { |
504 | return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace()); |
505 | } |
506 | |
507 | |
508 | |
509 | |
510 | bool compatiblyIncludes(Qualifiers other) const { |
511 | return isAddressSpaceSupersetOf(other) && |
512 | |
513 | |
514 | (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || |
515 | !other.hasObjCGCAttr()) && |
516 | |
517 | getObjCLifetime() == other.getObjCLifetime() && |
518 | |
519 | (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && |
520 | |
521 | (!other.hasUnaligned() || hasUnaligned()); |
522 | } |
523 | |
524 | |
525 | |
526 | |
527 | |
528 | |
529 | |
530 | |
531 | bool compatiblyIncludesObjCLifetime(Qualifiers other) const { |
532 | if (getObjCLifetime() == other.getObjCLifetime()) |
533 | return true; |
534 | |
535 | if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) |
536 | return false; |
537 | |
538 | if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) |
539 | return true; |
540 | |
541 | return hasConst(); |
542 | } |
543 | |
544 | |
545 | |
546 | bool isStrictSupersetOf(Qualifiers Other) const; |
547 | |
548 | bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } |
549 | bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } |
550 | |
551 | explicit operator bool() const { return hasQualifiers(); } |
552 | |
553 | Qualifiers &operator+=(Qualifiers R) { |
554 | addQualifiers(R); |
555 | return *this; |
556 | } |
557 | |
558 | |
559 | |
560 | friend Qualifiers operator+(Qualifiers L, Qualifiers R) { |
561 | L += R; |
562 | return L; |
563 | } |
564 | |
565 | Qualifiers &operator-=(Qualifiers R) { |
566 | removeQualifiers(R); |
567 | return *this; |
568 | } |
569 | |
570 | |
571 | friend Qualifiers operator-(Qualifiers L, Qualifiers R) { |
572 | L -= R; |
573 | return L; |
574 | } |
575 | |
576 | std::string getAsString() const; |
577 | std::string getAsString(const PrintingPolicy &Policy) const; |
578 | |
579 | static std::string getAddrSpaceAsString(LangAS AS); |
580 | |
581 | bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; |
582 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
583 | bool appendSpaceIfNonEmpty = false) const; |
584 | |
585 | void Profile(llvm::FoldingSetNodeID &ID) const { |
586 | ID.AddInteger(Mask); |
587 | } |
588 | |
589 | private: |
590 | |
591 | |
592 | uint32_t Mask = 0; |
593 | |
594 | static const uint32_t UMask = 0x8; |
595 | static const uint32_t UShift = 3; |
596 | static const uint32_t GCAttrMask = 0x30; |
597 | static const uint32_t GCAttrShift = 4; |
598 | static const uint32_t LifetimeMask = 0x1C0; |
599 | static const uint32_t LifetimeShift = 6; |
600 | static const uint32_t AddressSpaceMask = |
601 | ~(CVRMask | UMask | GCAttrMask | LifetimeMask); |
602 | static const uint32_t AddressSpaceShift = 9; |
603 | }; |
604 | |
605 | |
606 | |
607 | struct SplitQualType { |
608 | |
609 | const Type *Ty = nullptr; |
610 | |
611 | |
612 | Qualifiers Quals; |
613 | |
614 | SplitQualType() = default; |
615 | SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} |
616 | |
617 | SplitQualType getSingleStepDesugaredType() const; |
618 | |
619 | |
620 | std::pair<const Type *,Qualifiers> asPair() const { |
621 | return std::pair<const Type *, Qualifiers>(Ty, Quals); |
622 | } |
623 | |
624 | friend bool operator==(SplitQualType a, SplitQualType b) { |
625 | return a.Ty == b.Ty && a.Quals == b.Quals; |
626 | } |
627 | friend bool operator!=(SplitQualType a, SplitQualType b) { |
628 | return a.Ty != b.Ty || a.Quals != b.Quals; |
629 | } |
630 | }; |
631 | |
632 | |
633 | |
634 | |
635 | |
636 | |
637 | enum class ObjCSubstitutionContext { |
638 | |
639 | Ordinary, |
640 | |
641 | |
642 | Result, |
643 | |
644 | |
645 | Parameter, |
646 | |
647 | |
648 | Property, |
649 | |
650 | |
651 | Superclass, |
652 | }; |
653 | |
654 | |
655 | |
656 | |
657 | |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | |
668 | class QualType { |
669 | friend class QualifierCollector; |
670 | |
671 | |
672 | llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, |
673 | Qualifiers::FastWidth> Value; |
674 | |
675 | const ExtQuals *getExtQualsUnsafe() const { |
676 | return Value.getPointer().get<const ExtQuals*>(); |
677 | } |
678 | |
679 | const Type *getTypePtrUnsafe() const { |
680 | return Value.getPointer().get<const Type*>(); |
681 | } |
682 | |
683 | const ExtQualsTypeCommonBase *getCommonPtr() const { |
684 | assert(!isNull() && "Cannot retrieve a NULL type pointer"); |
685 | auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); |
686 | CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); |
687 | return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); |
688 | } |
689 | |
690 | public: |
691 | QualType() = default; |
692 | QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
693 | QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
694 | |
695 | unsigned getLocalFastQualifiers() const { return Value.getInt(); } |
696 | void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } |
697 | |
698 | |
699 | |
700 | |
701 | |
702 | const Type *getTypePtr() const; |
703 | |
704 | const Type *getTypePtrOrNull() const; |
705 | |
706 | |
707 | const IdentifierInfo *getBaseTypeIdentifier() const; |
708 | |
709 | |
710 | |
711 | SplitQualType split() const; |
712 | |
713 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } |
714 | |
715 | static QualType getFromOpaquePtr(const void *Ptr) { |
716 | QualType T; |
717 | T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); |
718 | return T; |
719 | } |
720 | |
721 | const Type &operator*() const { |
722 | return *getTypePtr(); |
723 | } |
724 | |
725 | const Type *operator->() const { |
726 | return getTypePtr(); |
727 | } |
728 | |
729 | bool isCanonical() const; |
730 | bool isCanonicalAsParam() const; |
731 | |
732 | |
733 | bool isNull() const { |
734 | return Value.getPointer().isNull(); |
| 13 | | Calling 'PointerUnion::isNull' | |
|
| 24 | | Returning from 'PointerUnion::isNull' | |
|
| 25 | | Returning the value 1, which participates in a condition later | |
|
735 | } |
736 | |
737 | |
738 | |
739 | |
740 | bool isLocalConstQualified() const { |
741 | return (getLocalFastQualifiers() & Qualifiers::Const); |
742 | } |
743 | |
744 | |
745 | bool isConstQualified() const; |
746 | |
747 | |
748 | |
749 | |
750 | bool isLocalRestrictQualified() const { |
751 | return (getLocalFastQualifiers() & Qualifiers::Restrict); |
752 | } |
753 | |
754 | |
755 | bool isRestrictQualified() const; |
756 | |
757 | |
758 | |
759 | |
760 | bool isLocalVolatileQualified() const { |
761 | return (getLocalFastQualifiers() & Qualifiers::Volatile); |
762 | } |
763 | |
764 | |
765 | bool isVolatileQualified() const; |
766 | |
767 | |
768 | |
769 | |
770 | bool hasLocalQualifiers() const { |
771 | return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); |
772 | } |
773 | |
774 | |
775 | bool hasQualifiers() const; |
776 | |
777 | |
778 | |
779 | |
780 | bool hasLocalNonFastQualifiers() const { |
781 | return Value.getPointer().is<const ExtQuals*>(); |
782 | } |
783 | |
784 | |
785 | |
786 | |
787 | Qualifiers getLocalQualifiers() const; |
788 | |
789 | |
790 | Qualifiers getQualifiers() const; |
791 | |
792 | |
793 | |
794 | |
795 | unsigned getLocalCVRQualifiers() const { |
796 | return getLocalFastQualifiers(); |
797 | } |
798 | |
799 | |
800 | |
801 | unsigned getCVRQualifiers() const; |
802 | |
803 | bool isConstant(const ASTContext& Ctx) const { |
804 | return QualType::isConstant(*this, Ctx); |
805 | } |
806 | |
807 | |
808 | bool isPODType(const ASTContext &Context) const; |
809 | |
810 | |
811 | |
812 | bool isCXX98PODType(const ASTContext &Context) const; |
813 | |
814 | |
815 | |
816 | |
817 | |
818 | bool isCXX11PODType(const ASTContext &Context) const; |
819 | |
820 | |
821 | bool isTrivialType(const ASTContext &Context) const; |
822 | |
823 | |
824 | bool isTriviallyCopyableType(const ASTContext &Context) const; |
825 | |
826 | |
827 | |
828 | bool mayBeDynamicClass() const; |
829 | |
830 | |
831 | bool mayBeNotDynamicClass() const; |
832 | |
833 | |
834 | |
835 | |
836 | |
837 | void addConst() { |
838 | addFastQualifiers(Qualifiers::Const); |
839 | } |
840 | QualType withConst() const { |
841 | return withFastQualifiers(Qualifiers::Const); |
842 | } |
843 | |
844 | |
845 | void addVolatile() { |
846 | addFastQualifiers(Qualifiers::Volatile); |
847 | } |
848 | QualType withVolatile() const { |
849 | return withFastQualifiers(Qualifiers::Volatile); |
850 | } |
851 | |
852 | |
853 | void addRestrict() { |
854 | addFastQualifiers(Qualifiers::Restrict); |
855 | } |
856 | QualType withRestrict() const { |
857 | return withFastQualifiers(Qualifiers::Restrict); |
858 | } |
859 | |
860 | QualType withCVRQualifiers(unsigned CVR) const { |
861 | return withFastQualifiers(CVR); |
862 | } |
863 | |
864 | void addFastQualifiers(unsigned TQs) { |
865 | assert(!(TQs & ~Qualifiers::FastMask) |
866 | && "non-fast qualifier bits set in mask!"); |
867 | Value.setInt(Value.getInt() | TQs); |
868 | } |
869 | |
870 | void removeLocalConst(); |
871 | void removeLocalVolatile(); |
872 | void removeLocalRestrict(); |
873 | void removeLocalCVRQualifiers(unsigned Mask); |
874 | |
875 | void removeLocalFastQualifiers() { Value.setInt(0); } |
876 | void removeLocalFastQualifiers(unsigned Mask) { |
877 | assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); |
878 | Value.setInt(Value.getInt() & ~Mask); |
879 | } |
880 | |
881 | |
882 | |
883 | QualType withFastQualifiers(unsigned TQs) const { |
884 | QualType T = *this; |
885 | T.addFastQualifiers(TQs); |
886 | return T; |
887 | } |
888 | |
889 | |
890 | |
891 | QualType withExactLocalFastQualifiers(unsigned TQs) const { |
892 | return withoutLocalFastQualifiers().withFastQualifiers(TQs); |
893 | } |
894 | |
895 | |
896 | QualType withoutLocalFastQualifiers() const { |
897 | QualType T = *this; |
898 | T.removeLocalFastQualifiers(); |
899 | return T; |
900 | } |
901 | |
902 | QualType getCanonicalType() const; |
903 | |
904 | |
905 | |
906 | |
907 | QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | |
914 | |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | inline QualType getUnqualifiedType() const; |
928 | |
929 | |
930 | |
931 | |
932 | |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | inline SplitQualType getSplitUnqualifiedType() const; |
939 | |
940 | |
941 | |
942 | bool isMoreQualifiedThan(QualType Other) const; |
943 | |
944 | |
945 | |
946 | bool isAtLeastAsQualifiedAs(QualType Other) const; |
947 | |
948 | QualType getNonReferenceType() const; |
949 | |
950 | |
951 | |
952 | |
953 | |
954 | |
955 | |
956 | |
957 | |
958 | QualType getNonLValueExprType(const ASTContext &Context) const; |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | QualType getNonPackExpansionType() const; |
965 | |
966 | |
967 | |
968 | |
969 | |
970 | |
971 | |
972 | |
973 | |
974 | QualType getDesugaredType(const ASTContext &Context) const { |
975 | return getDesugaredType(*this, Context); |
976 | } |
977 | |
978 | SplitQualType getSplitDesugaredType() const { |
979 | return getSplitDesugaredType(*this); |
980 | } |
981 | |
982 | |
983 | |
984 | |
985 | |
986 | |
987 | QualType getSingleStepDesugaredType(const ASTContext &Context) const { |
988 | return getSingleStepDesugaredTypeImpl(*this, Context); |
989 | } |
990 | |
991 | |
992 | |
993 | QualType IgnoreParens() const { |
994 | if (isa<ParenType>(*this)) |
995 | return QualType::IgnoreParens(*this); |
996 | return *this; |
997 | } |
998 | |
999 | |
1000 | friend bool operator==(const QualType &LHS, const QualType &RHS) { |
1001 | return LHS.Value == RHS.Value; |
1002 | } |
1003 | friend bool operator!=(const QualType &LHS, const QualType &RHS) { |
1004 | return LHS.Value != RHS.Value; |
1005 | } |
1006 | friend bool operator<(const QualType &LHS, const QualType &RHS) { |
1007 | return LHS.Value < RHS.Value; |
1008 | } |
1009 | |
1010 | static std::string getAsString(SplitQualType split, |
1011 | const PrintingPolicy &Policy) { |
1012 | return getAsString(split.Ty, split.Quals, Policy); |
1013 | } |
1014 | static std::string getAsString(const Type *ty, Qualifiers qs, |
1015 | const PrintingPolicy &Policy); |
1016 | |
1017 | std::string getAsString() const; |
1018 | std::string getAsString(const PrintingPolicy &Policy) const; |
1019 | |
1020 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
1021 | const Twine &PlaceHolder = Twine(), |
1022 | unsigned Indentation = 0) const; |
1023 | |
1024 | static void print(SplitQualType split, raw_ostream &OS, |
1025 | const PrintingPolicy &policy, const Twine &PlaceHolder, |
1026 | unsigned Indentation = 0) { |
1027 | return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); |
1028 | } |
1029 | |
1030 | static void print(const Type *ty, Qualifiers qs, |
1031 | raw_ostream &OS, const PrintingPolicy &policy, |
1032 | const Twine &PlaceHolder, |
1033 | unsigned Indentation = 0); |
1034 | |
1035 | void getAsStringInternal(std::string &Str, |
1036 | const PrintingPolicy &Policy) const; |
1037 | |
1038 | static void getAsStringInternal(SplitQualType split, std::string &out, |
1039 | const PrintingPolicy &policy) { |
1040 | return getAsStringInternal(split.Ty, split.Quals, out, policy); |
1041 | } |
1042 | |
1043 | static void getAsStringInternal(const Type *ty, Qualifiers qs, |
1044 | std::string &out, |
1045 | const PrintingPolicy &policy); |
1046 | |
1047 | class StreamedQualTypeHelper { |
1048 | const QualType &T; |
1049 | const PrintingPolicy &Policy; |
1050 | const Twine &PlaceHolder; |
1051 | unsigned Indentation; |
1052 | |
1053 | public: |
1054 | StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, |
1055 | const Twine &PlaceHolder, unsigned Indentation) |
1056 | : T(T), Policy(Policy), PlaceHolder(PlaceHolder), |
1057 | Indentation(Indentation) {} |
1058 | |
1059 | friend raw_ostream &operator<<(raw_ostream &OS, |
1060 | const StreamedQualTypeHelper &SQT) { |
1061 | SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); |
1062 | return OS; |
1063 | } |
1064 | }; |
1065 | |
1066 | StreamedQualTypeHelper stream(const PrintingPolicy &Policy, |
1067 | const Twine &PlaceHolder = Twine(), |
1068 | unsigned Indentation = 0) const { |
1069 | return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); |
1070 | } |
1071 | |
1072 | void dump(const char *s) const; |
1073 | void dump() const; |
1074 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
1075 | |
1076 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1077 | ID.AddPointer(getAsOpaquePtr()); |
1078 | } |
1079 | |
1080 | |
1081 | inline bool hasAddressSpace() const; |
1082 | |
1083 | |
1084 | inline LangAS getAddressSpace() const; |
1085 | |
1086 | |
1087 | |
1088 | |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | bool isAddressSpaceOverlapping(QualType T) const { |
1095 | Qualifiers Q = getQualifiers(); |
1096 | Qualifiers TQ = T.getQualifiers(); |
1097 | |
1098 | return Q.isAddressSpaceSupersetOf(TQ) || TQ.isAddressSpaceSupersetOf(Q); |
1099 | } |
1100 | |
1101 | |
1102 | inline Qualifiers::GC getObjCGCAttr() const; |
1103 | |
1104 | |
1105 | bool isObjCGCWeak() const { |
1106 | return getObjCGCAttr() == Qualifiers::Weak; |
1107 | } |
1108 | |
1109 | |
1110 | bool isObjCGCStrong() const { |
1111 | return getObjCGCAttr() == Qualifiers::Strong; |
1112 | } |
1113 | |
1114 | |
1115 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1116 | return getQualifiers().getObjCLifetime(); |
1117 | } |
1118 | |
1119 | bool hasNonTrivialObjCLifetime() const { |
1120 | return getQualifiers().hasNonTrivialObjCLifetime(); |
1121 | } |
1122 | |
1123 | bool hasStrongOrWeakObjCLifetime() const { |
1124 | return getQualifiers().hasStrongOrWeakObjCLifetime(); |
1125 | } |
1126 | |
1127 | |
1128 | bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; |
1129 | |
1130 | enum PrimitiveDefaultInitializeKind { |
1131 | |
1132 | |
1133 | |
1134 | PDIK_Trivial, |
1135 | |
1136 | |
1137 | |
1138 | PDIK_ARCStrong, |
1139 | |
1140 | |
1141 | |
1142 | PDIK_ARCWeak, |
1143 | |
1144 | |
1145 | PDIK_Struct |
1146 | }; |
1147 | |
1148 | |
1149 | |
1150 | |
1151 | |
1152 | |
1153 | PrimitiveDefaultInitializeKind |
1154 | isNonTrivialToPrimitiveDefaultInitialize() const; |
1155 | |
1156 | enum PrimitiveCopyKind { |
1157 | |
1158 | |
1159 | |
1160 | PCK_Trivial, |
1161 | |
1162 | |
1163 | |
1164 | |
1165 | PCK_VolatileTrivial, |
1166 | |
1167 | |
1168 | |
1169 | PCK_ARCStrong, |
1170 | |
1171 | |
1172 | |
1173 | PCK_ARCWeak, |
1174 | |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | |
1181 | PCK_Struct |
1182 | }; |
1183 | |
1184 | |
1185 | |
1186 | |
1187 | PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; |
1196 | |
1197 | enum DestructionKind { |
1198 | DK_none, |
1199 | DK_cxx_destructor, |
1200 | DK_objc_strong_lifetime, |
1201 | DK_objc_weak_lifetime, |
1202 | DK_nontrivial_c_struct |
1203 | }; |
1204 | |
1205 | |
1206 | |
1207 | |
1208 | |
1209 | DestructionKind isDestructedType() const { |
1210 | return isDestructedTypeImpl(*this); |
1211 | } |
1212 | |
1213 | |
1214 | |
1215 | |
1216 | |
1217 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | bool hasNonTrivialToPrimitiveDestructCUnion() const; |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | bool hasNonTrivialToPrimitiveCopyCUnion() const; |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | bool isCForbiddenLValueType() const; |
1240 | |
1241 | |
1242 | |
1243 | |
1244 | |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | |
1250 | |
1251 | |
1252 | |
1253 | |
1254 | |
1255 | QualType substObjCTypeArgs(ASTContext &ctx, |
1256 | ArrayRef<QualType> typeArgs, |
1257 | ObjCSubstitutionContext context) const; |
1258 | |
1259 | |
1260 | |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | |
1274 | |
1275 | |
1276 | |
1277 | |
1278 | |
1279 | QualType substObjCMemberType(QualType objectType, |
1280 | const DeclContext *dc, |
1281 | ObjCSubstitutionContext context) const; |
1282 | |
1283 | |
1284 | QualType stripObjCKindOfType(const ASTContext &ctx) const; |
1285 | |
1286 | |
1287 | QualType getAtomicUnqualifiedType() const; |
1288 | |
1289 | private: |
1290 | |
1291 | |
1292 | |
1293 | static bool isConstant(QualType T, const ASTContext& Ctx); |
1294 | static QualType getDesugaredType(QualType T, const ASTContext &Context); |
1295 | static SplitQualType getSplitDesugaredType(QualType T); |
1296 | static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); |
1297 | static QualType getSingleStepDesugaredTypeImpl(QualType type, |
1298 | const ASTContext &C); |
1299 | static QualType IgnoreParens(QualType T); |
1300 | static DestructionKind isDestructedTypeImpl(QualType type); |
1301 | |
1302 | |
1303 | static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); |
1304 | static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); |
1305 | static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); |
1306 | }; |
1307 | |
1308 | } |
1309 | |
1310 | namespace llvm { |
1311 | |
1312 | |
1313 | |
1314 | template<> struct simplify_type< ::clang::QualType> { |
1315 | using SimpleType = const ::clang::Type *; |
1316 | |
1317 | static SimpleType getSimplifiedValue(::clang::QualType Val) { |
1318 | return Val.getTypePtr(); |
1319 | } |
1320 | }; |
1321 | |
1322 | |
1323 | template<> |
1324 | struct PointerLikeTypeTraits<clang::QualType> { |
1325 | static inline void *getAsVoidPointer(clang::QualType P) { |
1326 | return P.getAsOpaquePtr(); |
1327 | } |
1328 | |
1329 | static inline clang::QualType getFromVoidPointer(void *P) { |
1330 | return clang::QualType::getFromOpaquePtr(P); |
1331 | } |
1332 | |
1333 | |
1334 | static constexpr int NumLowBitsAvailable = 0; |
1335 | }; |
1336 | |
1337 | } |
1338 | |
1339 | namespace clang { |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | class ExtQualsTypeCommonBase { |
1345 | friend class ExtQuals; |
1346 | friend class QualType; |
1347 | friend class Type; |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | |
1353 | |
1354 | const Type *const BaseType; |
1355 | |
1356 | |
1357 | QualType CanonicalType; |
1358 | |
1359 | ExtQualsTypeCommonBase(const Type *baseType, QualType canon) |
1360 | : BaseType(baseType), CanonicalType(canon) {} |
1361 | }; |
1362 | |
1363 | |
1364 | |
1365 | |
1366 | |
1367 | |
1368 | |
1369 | |
1370 | |
1371 | |
1372 | |
1373 | class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { |
1374 | |
1375 | |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | Qualifiers Quals; |
1390 | |
1391 | ExtQuals *this_() { return this; } |
1392 | |
1393 | public: |
1394 | ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) |
1395 | : ExtQualsTypeCommonBase(baseType, |
1396 | canon.isNull() ? QualType(this_(), 0) : canon), |
1397 | Quals(quals) { |
1398 | assert(Quals.hasNonFastQualifiers() |
1399 | && "ExtQuals created with no fast qualifiers"); |
1400 | assert(!Quals.hasFastQualifiers() |
1401 | && "ExtQuals created with fast qualifiers"); |
1402 | } |
1403 | |
1404 | Qualifiers getQualifiers() const { return Quals; } |
1405 | |
1406 | bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } |
1407 | Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } |
1408 | |
1409 | bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } |
1410 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1411 | return Quals.getObjCLifetime(); |
1412 | } |
1413 | |
1414 | bool hasAddressSpace() const { return Quals.hasAddressSpace(); } |
1415 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } |
1416 | |
1417 | const Type *getBaseType() const { return BaseType; } |
1418 | |
1419 | public: |
1420 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1421 | Profile(ID, getBaseType(), Quals); |
1422 | } |
1423 | |
1424 | static void Profile(llvm::FoldingSetNodeID &ID, |
1425 | const Type *BaseType, |
1426 | Qualifiers Quals) { |
1427 | assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); |
1428 | ID.AddPointer(BaseType); |
1429 | Quals.Profile(ID); |
1430 | } |
1431 | }; |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | enum RefQualifierKind { |
1437 | |
1438 | RQ_None = 0, |
1439 | |
1440 | |
1441 | RQ_LValue, |
1442 | |
1443 | |
1444 | RQ_RValue |
1445 | }; |
1446 | |
1447 | |
1448 | enum class AutoTypeKeyword { |
1449 | |
1450 | Auto, |
1451 | |
1452 | |
1453 | DecltypeAuto, |
1454 | |
1455 | |
1456 | GNUAutoType |
1457 | }; |
1458 | |
1459 | |
1460 | |
1461 | |
1462 | |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | |
1469 | |
1470 | |
1471 | |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | |
1478 | |
1479 | |
1480 | |
1481 | |
1482 | |
1483 | |
1484 | |
1485 | class alignas(8) Type : public ExtQualsTypeCommonBase { |
1486 | public: |
1487 | enum TypeClass { |
1488 | #define TYPE(Class, Base) Class, |
1489 | #define LAST_TYPE(Class) TypeLast = Class |
1490 | #define ABSTRACT_TYPE(Class, Base) |
1491 | #include "clang/AST/TypeNodes.inc" |
1492 | }; |
1493 | |
1494 | private: |
1495 | |
1496 | class TypeBitfields { |
1497 | friend class Type; |
1498 | template <class T> friend class TypePropertyCache; |
1499 | |
1500 | |
1501 | unsigned TC : 8; |
1502 | |
1503 | |
1504 | unsigned Dependence : llvm::BitWidth<TypeDependence>; |
1505 | |
1506 | |
1507 | |
1508 | mutable unsigned CacheValid : 1; |
1509 | |
1510 | |
1511 | mutable unsigned CachedLinkage : 3; |
1512 | |
1513 | |
1514 | mutable unsigned CachedLocalOrUnnamed : 1; |
1515 | |
1516 | |
1517 | mutable unsigned FromAST : 1; |
1518 | |
1519 | bool isCacheValid() const { |
1520 | return CacheValid; |
1521 | } |
1522 | |
1523 | Linkage getLinkage() const { |
1524 | assert(isCacheValid() && "getting linkage from invalid cache"); |
1525 | return static_cast<Linkage>(CachedLinkage); |
1526 | } |
1527 | |
1528 | bool hasLocalOrUnnamedType() const { |
1529 | assert(isCacheValid() && "getting linkage from invalid cache"); |
1530 | return CachedLocalOrUnnamed; |
1531 | } |
1532 | }; |
1533 | enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 }; |
1534 | |
1535 | protected: |
1536 | |
1537 | |
1538 | |
1539 | class ArrayTypeBitfields { |
1540 | friend class ArrayType; |
1541 | |
1542 | unsigned : NumTypeBits; |
1543 | |
1544 | |
1545 | |
1546 | unsigned IndexTypeQuals : 3; |
1547 | |
1548 | |
1549 | |
1550 | |
1551 | unsigned SizeModifier : 3; |
1552 | }; |
1553 | |
1554 | class ConstantArrayTypeBitfields { |
1555 | friend class ConstantArrayType; |
1556 | |
1557 | unsigned : NumTypeBits + 3 + 3; |
1558 | |
1559 | |
1560 | unsigned HasStoredSizeExpr : 1; |
1561 | }; |
1562 | |
1563 | class BuiltinTypeBitfields { |
1564 | friend class BuiltinType; |
1565 | |
1566 | unsigned : NumTypeBits; |
1567 | |
1568 | |
1569 | unsigned Kind : 8; |
1570 | }; |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | class FunctionTypeBitfields { |
1576 | friend class FunctionProtoType; |
1577 | friend class FunctionType; |
1578 | |
1579 | unsigned : NumTypeBits; |
1580 | |
1581 | |
1582 | |
1583 | unsigned ExtInfo : 13; |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | unsigned RefQualifier : 2; |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | |
1594 | |
1595 | |
1596 | unsigned FastTypeQuals : Qualifiers::FastWidth; |
1597 | |
1598 | unsigned HasExtQuals : 1; |
1599 | |
1600 | |
1601 | |
1602 | |
1603 | |
1604 | unsigned NumParams : 16; |
1605 | |
1606 | |
1607 | unsigned ExceptionSpecType : 4; |
1608 | |
1609 | |
1610 | unsigned HasExtParameterInfos : 1; |
1611 | |
1612 | |
1613 | unsigned Variadic : 1; |
1614 | |
1615 | |
1616 | unsigned HasTrailingReturn : 1; |
1617 | }; |
1618 | |
1619 | class ObjCObjectTypeBitfields { |
1620 | friend class ObjCObjectType; |
1621 | |
1622 | unsigned : NumTypeBits; |
1623 | |
1624 | |
1625 | unsigned NumTypeArgs : 7; |
1626 | |
1627 | |
1628 | unsigned NumProtocols : 6; |
1629 | |
1630 | |
1631 | unsigned IsKindOf : 1; |
1632 | }; |
1633 | |
1634 | class ReferenceTypeBitfields { |
1635 | friend class ReferenceType; |
1636 | |
1637 | unsigned : NumTypeBits; |
1638 | |
1639 | |
1640 | |
1641 | |
1642 | |
1643 | |
1644 | |
1645 | |
1646 | |
1647 | |
1648 | |
1649 | |
1650 | unsigned SpelledAsLValue : 1; |
1651 | |
1652 | |
1653 | |
1654 | unsigned InnerRef : 1; |
1655 | }; |
1656 | |
1657 | class TypeWithKeywordBitfields { |
1658 | friend class TypeWithKeyword; |
1659 | |
1660 | unsigned : NumTypeBits; |
1661 | |
1662 | |
1663 | unsigned Keyword : 8; |
1664 | }; |
1665 | |
1666 | enum { NumTypeWithKeywordBits = 8 }; |
1667 | |
1668 | class ElaboratedTypeBitfields { |
1669 | friend class ElaboratedType; |
1670 | |
1671 | unsigned : NumTypeBits; |
1672 | unsigned : NumTypeWithKeywordBits; |
1673 | |
1674 | |
1675 | unsigned HasOwnedTagDecl : 1; |
1676 | }; |
1677 | |
1678 | class VectorTypeBitfields { |
1679 | friend class VectorType; |
1680 | friend class DependentVectorType; |
1681 | |
1682 | unsigned : NumTypeBits; |
1683 | |
1684 | |
1685 | |
1686 | unsigned VecKind : 3; |
1687 | |
1688 | uint32_t NumElements; |
1689 | }; |
1690 | |
1691 | class AttributedTypeBitfields { |
1692 | friend class AttributedType; |
1693 | |
1694 | unsigned : NumTypeBits; |
1695 | |
1696 | |
1697 | unsigned AttrKind : 32 - NumTypeBits; |
1698 | }; |
1699 | |
1700 | class AutoTypeBitfields { |
1701 | friend class AutoType; |
1702 | |
1703 | unsigned : NumTypeBits; |
1704 | |
1705 | |
1706 | |
1707 | unsigned Keyword : 2; |
1708 | |
1709 | |
1710 | |
1711 | |
1712 | |
1713 | |
1714 | |
1715 | |
1716 | unsigned NumArgs; |
1717 | }; |
1718 | |
1719 | class SubstTemplateTypeParmPackTypeBitfields { |
1720 | friend class SubstTemplateTypeParmPackType; |
1721 | |
1722 | unsigned : NumTypeBits; |
1723 | |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | |
1729 | |
1730 | |
1731 | unsigned NumArgs; |
1732 | }; |
1733 | |
1734 | class TemplateSpecializationTypeBitfields { |
1735 | friend class TemplateSpecializationType; |
1736 | |
1737 | unsigned : NumTypeBits; |
1738 | |
1739 | |
1740 | unsigned TypeAlias : 1; |
1741 | |
1742 | |
1743 | |
1744 | |
1745 | |
1746 | |
1747 | |
1748 | |
1749 | unsigned NumArgs; |
1750 | }; |
1751 | |
1752 | class DependentTemplateSpecializationTypeBitfields { |
1753 | friend class DependentTemplateSpecializationType; |
1754 | |
1755 | unsigned : NumTypeBits; |
1756 | unsigned : NumTypeWithKeywordBits; |
1757 | |
1758 | |
1759 | |
1760 | |
1761 | |
1762 | |
1763 | |
1764 | |
1765 | unsigned NumArgs; |
1766 | }; |
1767 | |
1768 | class PackExpansionTypeBitfields { |
1769 | friend class PackExpansionType; |
1770 | |
1771 | unsigned : NumTypeBits; |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | unsigned NumExpansions; |
1785 | }; |
1786 | |
1787 | union { |
1788 | TypeBitfields TypeBits; |
1789 | ArrayTypeBitfields ArrayTypeBits; |
1790 | ConstantArrayTypeBitfields ConstantArrayTypeBits; |
1791 | AttributedTypeBitfields AttributedTypeBits; |
1792 | AutoTypeBitfields AutoTypeBits; |
1793 | BuiltinTypeBitfields BuiltinTypeBits; |
1794 | FunctionTypeBitfields FunctionTypeBits; |
1795 | ObjCObjectTypeBitfields ObjCObjectTypeBits; |
1796 | ReferenceTypeBitfields ReferenceTypeBits; |
1797 | TypeWithKeywordBitfields TypeWithKeywordBits; |
1798 | ElaboratedTypeBitfields ElaboratedTypeBits; |
1799 | VectorTypeBitfields VectorTypeBits; |
1800 | SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; |
1801 | TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; |
1802 | DependentTemplateSpecializationTypeBitfields |
1803 | DependentTemplateSpecializationTypeBits; |
1804 | PackExpansionTypeBitfields PackExpansionTypeBits; |
1805 | }; |
1806 | |
1807 | private: |
1808 | template <class T> friend class TypePropertyCache; |
1809 | |
1810 | |
1811 | void setFromAST(bool V = true) const { |
1812 | TypeBits.FromAST = V; |
1813 | } |
1814 | |
1815 | protected: |
1816 | friend class ASTContext; |
1817 | |
1818 | Type(TypeClass tc, QualType canon, TypeDependence Dependence) |
1819 | : ExtQualsTypeCommonBase(this, |
1820 | canon.isNull() ? QualType(this_(), 0) : canon) { |
1821 | static_assert(sizeof(*this) <= 8 + sizeof(ExtQualsTypeCommonBase), |
1822 | "changing bitfields changed sizeof(Type)!"); |
1823 | static_assert(alignof(decltype(*this)) % sizeof(void *) == 0, |
1824 | "Insufficient alignment!"); |
1825 | TypeBits.TC = tc; |
1826 | TypeBits.Dependence = static_cast<unsigned>(Dependence); |
1827 | TypeBits.CacheValid = false; |
1828 | TypeBits.CachedLocalOrUnnamed = false; |
1829 | TypeBits.CachedLinkage = NoLinkage; |
1830 | TypeBits.FromAST = false; |
1831 | } |
1832 | |
1833 | |
1834 | Type *this_() { return this; } |
1835 | |
1836 | void setDependence(TypeDependence D) { |
1837 | TypeBits.Dependence = static_cast<unsigned>(D); |
1838 | } |
1839 | |
1840 | void addDependence(TypeDependence D) { setDependence(getDependence() | D); } |
1841 | |
1842 | public: |
1843 | friend class ASTReader; |
1844 | friend class ASTWriter; |
1845 | template <class T> friend class serialization::AbstractTypeReader; |
1846 | template <class T> friend class serialization::AbstractTypeWriter; |
1847 | |
1848 | Type(const Type &) = delete; |
1849 | Type(Type &&) = delete; |
1850 | Type &operator=(const Type &) = delete; |
1851 | Type &operator=(Type &&) = delete; |
1852 | |
1853 | TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } |
1854 | |
1855 | |
1856 | bool isFromAST() const { return TypeBits.FromAST; } |
1857 | |
1858 | |
1859 | |
1860 | |
1861 | |
1862 | |
1863 | |
1864 | |
1865 | |
1866 | |
1867 | |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | |
1873 | bool containsUnexpandedParameterPack() const { |
1874 | return getDependence() & TypeDependence::UnexpandedPack; |
1875 | } |
1876 | |
1877 | |
1878 | |
1879 | bool isCanonicalUnqualified() const { |
1880 | return CanonicalType == QualType(this, 0); |
1881 | } |
1882 | |
1883 | |
1884 | |
1885 | |
1886 | QualType getLocallyUnqualifiedSingleStepDesugaredType() const; |
1887 | |
1888 | |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | |
1894 | bool isSizelessType() const; |
1895 | bool isSizelessBuiltinType() const; |
1896 | |
1897 | |
1898 | |
1899 | |
1900 | bool isVLSTBuiltinType() const; |
1901 | |
1902 | |
1903 | |
1904 | |
1905 | QualType getSveEltType(const ASTContext &Ctx) const; |
1906 | |
1907 | |
1908 | |
1909 | |
1910 | |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | bool isIncompleteType(NamedDecl **Def = nullptr) const; |
1919 | |
1920 | |
1921 | |
1922 | bool isIncompleteOrObjectType() const { |
1923 | return !isFunctionType(); |
1924 | } |
1925 | |
1926 | |
1927 | bool isObjectType() const { |
1928 | |
1929 | |
1930 | |
1931 | return !isReferenceType() && !isFunctionType() && !isVoidType(); |
1932 | } |
1933 | |
1934 | |
1935 | |
1936 | bool isLiteralType(const ASTContext &Ctx) const; |
1937 | |
1938 | |
1939 | bool isStructuralType() const; |
1940 | |
1941 | |
1942 | |
1943 | bool isStandardLayoutType() const; |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | bool isBuiltinType() const; |
1950 | |
1951 | |
1952 | bool isSpecificBuiltinType(unsigned K) const; |
1953 | |
1954 | |
1955 | |
1956 | |
1957 | bool isPlaceholderType() const; |
1958 | const BuiltinType *getAsPlaceholderType() const; |
1959 | |
1960 | |
1961 | bool isSpecificPlaceholderType(unsigned K) const; |
1962 | |
1963 | |
1964 | |
1965 | bool isNonOverloadPlaceholderType() const; |
1966 | |
1967 | |
1968 | |
1969 | bool isIntegerType() const; |
1970 | bool isEnumeralType() const; |
1971 | |
1972 | |
1973 | bool isScopedEnumeralType() const; |
1974 | bool isBooleanType() const; |
1975 | bool isCharType() const; |
1976 | bool isWideCharType() const; |
1977 | bool isChar8Type() const; |
1978 | bool isChar16Type() const; |
1979 | bool isChar32Type() const; |
1980 | bool isAnyCharacterType() const; |
1981 | bool isIntegralType(const ASTContext &Ctx) const; |
1982 | |
1983 | |
1984 | bool isIntegralOrEnumerationType() const; |
1985 | |
1986 | |
1987 | bool isIntegralOrUnscopedEnumerationType() const; |
1988 | bool isUnscopedEnumerationType() const; |
1989 | |
1990 | |
1991 | bool isRealFloatingType() const; |
1992 | |
1993 | |
1994 | bool isComplexType() const; |
1995 | bool isAnyComplexType() const; |
1996 | bool isFloatingType() const; |
1997 | bool isHalfType() const; |
1998 | bool isFloat16Type() const; |
1999 | bool isBFloat16Type() const; |
2000 | bool isFloat128Type() const; |
2001 | bool isRealType() const; |
2002 | bool isArithmeticType() const; |
2003 | bool isVoidType() const; |
2004 | bool isScalarType() const; |
2005 | bool isAggregateType() const; |
2006 | bool isFundamentalType() const; |
2007 | bool isCompoundType() const; |
2008 | |
2009 | |
2010 | |
2011 | bool isFunctionType() const; |
2012 | bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } |
2013 | bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } |
2014 | bool isPointerType() const; |
2015 | bool isAnyPointerType() const; |
2016 | bool isBlockPointerType() const; |
2017 | bool isVoidPointerType() const; |
2018 | bool isReferenceType() const; |
2019 | bool isLValueReferenceType() const; |
2020 | bool isRValueReferenceType() const; |
2021 | bool isObjectPointerType() const; |
2022 | bool isFunctionPointerType() const; |
2023 | bool isFunctionReferenceType() const; |
2024 | bool isMemberPointerType() const; |
2025 | bool isMemberFunctionPointerType() const; |
2026 | bool isMemberDataPointerType() const; |
2027 | bool isArrayType() const; |
2028 | bool isConstantArrayType() const; |
2029 | bool isIncompleteArrayType() const; |
2030 | bool isVariableArrayType() const; |
2031 | bool isDependentSizedArrayType() const; |
2032 | bool isRecordType() const; |
2033 | bool isClassType() const; |
2034 | bool isStructureType() const; |
2035 | bool isObjCBoxableRecordType() const; |
2036 | bool isInterfaceType() const; |
2037 | bool isStructureOrClassType() const; |
2038 | bool isUnionType() const; |
2039 | bool isComplexIntegerType() const; |
2040 | bool isVectorType() const; |
2041 | bool isExtVectorType() const; |
2042 | bool isMatrixType() const; |
2043 | bool isConstantMatrixType() const; |
2044 | bool isDependentAddressSpaceType() const; |
2045 | bool isObjCObjectPointerType() const; |
2046 | bool isObjCRetainableType() const; |
2047 | bool isObjCLifetimeType() const; |
2048 | bool isObjCIndirectLifetimeType() const; |
2049 | bool isObjCNSObjectType() const; |
2050 | bool isObjCIndependentClassType() const; |
2051 | |
2052 | |
2053 | bool isObjCObjectType() const; |
2054 | bool isObjCQualifiedInterfaceType() const; |
2055 | bool isObjCQualifiedIdType() const; |
2056 | bool isObjCQualifiedClassType() const; |
2057 | bool isObjCObjectOrInterfaceType() const; |
2058 | bool isObjCIdType() const; |
2059 | bool isDecltypeType() const; |
2060 | |
2061 | |
2062 | |
2063 | |
2064 | |
2065 | |
2066 | bool isObjCInertUnsafeUnretainedType() const { |
2067 | return hasAttr(attr::ObjCInertUnsafeUnretained); |
2068 | } |
2069 | |
2070 | |
2071 | |
2072 | |
2073 | |
2074 | |
2075 | |
2076 | |
2077 | bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, |
2078 | const ObjCObjectType *&bound) const; |
2079 | |
2080 | bool isObjCClassType() const; |
2081 | |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | |
2087 | |
2088 | bool isObjCClassOrClassKindOfType() const; |
2089 | |
2090 | bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; |
2091 | bool isObjCSelType() const; |
2092 | bool isObjCBuiltinType() const; |
2093 | bool isObjCARCBridgableType() const; |
2094 | bool isCARCBridgableType() const; |
2095 | bool isTemplateTypeParmType() const; |
2096 | bool isNullPtrType() const; |
2097 | bool isNothrowT() const; |
2098 | bool isAlignValT() const; |
2099 | bool isStdByteType() const; |
2100 | bool isAtomicType() const; |
2101 | bool isUndeducedAutoType() const; |
2102 | |
2103 | bool isTypedefNameType() const; |
2104 | |
2105 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2106 | bool is##Id##Type() const; |
2107 | #include "clang/Basic/OpenCLImageTypes.def" |
2108 | |
2109 | bool isImageType() const; |
2110 | |
2111 | bool isSamplerT() const; |
2112 | bool isEventT() const; |
2113 | bool isClkEventT() const; |
2114 | bool isQueueT() const; |
2115 | bool isReserveIDT() const; |
2116 | |
2117 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2118 | bool is##Id##Type() const; |
2119 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2120 | |
2121 | bool isOCLIntelSubgroupAVCType() const; |
2122 | bool isOCLExtOpaqueType() const; |
2123 | |
2124 | bool isPipeType() const; |
2125 | bool isExtIntType() const; |
2126 | bool isOpenCLSpecificType() const; |
2127 | |
2128 | |
2129 | |
2130 | |
2131 | bool isObjCARCImplicitlyUnretainedType() const; |
2132 | |
2133 | |
2134 | bool isCUDADeviceBuiltinSurfaceType() const; |
2135 | |
2136 | bool isCUDADeviceBuiltinTextureType() const; |
2137 | |
2138 | |
2139 | Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; |
2140 | |
2141 | enum ScalarTypeKind { |
2142 | STK_CPointer, |
2143 | STK_BlockPointer, |
2144 | STK_ObjCObjectPointer, |
2145 | STK_MemberPointer, |
2146 | STK_Bool, |
2147 | STK_Integral, |
2148 | STK_Floating, |
2149 | STK_IntegralComplex, |
2150 | STK_FloatingComplex, |
2151 | STK_FixedPoint |
2152 | }; |
2153 | |
2154 | |
2155 | ScalarTypeKind getScalarTypeKind() const; |
2156 | |
2157 | TypeDependence getDependence() const { |
2158 | return static_cast<TypeDependence>(TypeBits.Dependence); |
2159 | } |
2160 | |
2161 | |
2162 | bool containsErrors() const { |
2163 | return getDependence() & TypeDependence::Error; |
2164 | } |
2165 | |
2166 | |
2167 | |
2168 | bool isDependentType() const { |
2169 | return getDependence() & TypeDependence::Dependent; |
2170 | } |
2171 | |
2172 | |
2173 | |
2174 | |
2175 | |
2176 | bool isInstantiationDependentType() const { |
2177 | return getDependence() & TypeDependence::Instantiation; |
2178 | } |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | bool isUndeducedType() const; |
2184 | |
2185 | |
2186 | bool isVariablyModifiedType() const { |
2187 | return getDependence() & TypeDependence::VariablyModified; |
2188 | } |
2189 | |
2190 | |
2191 | |
2192 | bool hasSizedVLAType() const; |
2193 | |
2194 | |
2195 | bool hasUnnamedOrLocalType() const; |
2196 | |
2197 | bool isOverloadableType() const; |
2198 | |
2199 | |
2200 | bool isElaboratedTypeSpecifier() const; |
2201 | |
2202 | bool canDecayToPointerType() const; |
2203 | |
2204 | |
2205 | |
2206 | |
2207 | bool hasPointerRepresentation() const; |
2208 | |
2209 | |
2210 | |
2211 | bool hasObjCPointerRepresentation() const; |
2212 | |
2213 | |
2214 | |
2215 | bool hasIntegerRepresentation() const; |
2216 | |
2217 | |
2218 | |
2219 | bool hasSignedIntegerRepresentation() const; |
2220 | |
2221 | |
2222 | |
2223 | bool hasUnsignedIntegerRepresentation() const; |
2224 | |
2225 | |
2226 | |
2227 | bool hasFloatingRepresentation() const; |
2228 | |
2229 | |
2230 | |
2231 | |
2232 | const RecordType *getAsStructureType() const; |
2233 | |
2234 | const RecordType *getAsUnionType() const; |
2235 | const ComplexType *getAsComplexIntegerType() const; |
2236 | const ObjCObjectType *getAsObjCInterfaceType() const; |
2237 | |
2238 | |
2239 | |
2240 | const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; |
2241 | const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; |
2242 | const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; |
2243 | const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; |
2244 | |
2245 | |
2246 | |
2247 | |
2248 | CXXRecordDecl *getAsCXXRecordDecl() const; |
2249 | |
2250 | |
2251 | RecordDecl *getAsRecordDecl() const; |
2252 | |
2253 | |
2254 | |
2255 | |
2256 | TagDecl *getAsTagDecl() const; |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | |
2262 | |
2263 | const CXXRecordDecl *getPointeeCXXRecordDecl() const; |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | DeducedType *getContainedDeducedType() const; |
2269 | |
2270 | |
2271 | |
2272 | |
2273 | AutoType *getContainedAutoType() const { |
2274 | return dyn_cast_or_null<AutoType>(getContainedDeducedType()); |
2275 | } |
2276 | |
2277 | |
2278 | |
2279 | |
2280 | bool hasAutoForTrailingReturnType() const; |
2281 | |
2282 | |
2283 | |
2284 | |
2285 | |
2286 | |
2287 | |
2288 | template <typename T> const T *getAs() const; |
2289 | |
2290 | |
2291 | |
2292 | |
2293 | |
2294 | |
2295 | template <typename T> const T *getAsAdjusted() const; |
2296 | |
2297 | |
2298 | |
2299 | const ArrayType *getAsArrayTypeUnsafe() const; |
2300 | |
2301 | |
2302 | |
2303 | |
2304 | |
2305 | |
2306 | |
2307 | template <typename T> const T *castAs() const; |
2308 | |
2309 | |
2310 | |
2311 | const ArrayType *castAsArrayTypeUnsafe() const; |
2312 | |
2313 | |
2314 | |
2315 | bool hasAttr(attr::Kind AK) const; |
2316 | |
2317 | |
2318 | |
2319 | |
2320 | const Type *getBaseElementTypeUnsafe() const; |
2321 | |
2322 | |
2323 | |
2324 | |
2325 | const Type *getArrayElementTypeNoTypeQual() const; |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | const Type *getPointeeOrArrayElementType() const; |
2331 | |
2332 | |
2333 | |
2334 | QualType getPointeeType() const; |
2335 | |
2336 | |
2337 | |
2338 | const Type *getUnqualifiedDesugaredType() const; |
2339 | |
2340 | |
2341 | bool isPromotableIntegerType() const; |
2342 | |
2343 | |
2344 | |
2345 | |
2346 | bool isSignedIntegerType() const; |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | bool isUnsignedIntegerType() const; |
2352 | |
2353 | |
2354 | |
2355 | bool isSignedIntegerOrEnumerationType() const; |
2356 | |
2357 | |
2358 | |
2359 | bool isUnsignedIntegerOrEnumerationType() const; |
2360 | |
2361 | |
2362 | |
2363 | bool isFixedPointType() const; |
2364 | |
2365 | |
2366 | bool isFixedPointOrIntegerType() const; |
2367 | |
2368 | |
2369 | |
2370 | bool isSaturatedFixedPointType() const; |
2371 | |
2372 | |
2373 | |
2374 | bool isUnsaturatedFixedPointType() const; |
2375 | |
2376 | |
2377 | |
2378 | bool isSignedFixedPointType() const; |
2379 | |
2380 | |
2381 | |
2382 | bool isUnsignedFixedPointType() const; |
2383 | |
2384 | |
2385 | |
2386 | |
2387 | bool isConstantSizeType() const; |
2388 | |
2389 | |
2390 | |
2391 | bool isSpecifierType() const; |
2392 | |
2393 | |
2394 | Linkage getLinkage() const; |
2395 | |
2396 | |
2397 | Visibility getVisibility() const { |
2398 | return getLinkageAndVisibility().getVisibility(); |
2399 | } |
2400 | |
2401 | |
2402 | bool isVisibilityExplicit() const { |
2403 | return getLinkageAndVisibility().isVisibilityExplicit(); |
2404 | } |
2405 | |
2406 | |
2407 | LinkageInfo getLinkageAndVisibility() const; |
2408 | |
2409 | |
2410 | |
2411 | bool isLinkageValid() const; |
2412 | |
2413 | |
2414 | |
2415 | |
2416 | |
2417 | |
2418 | Optional<NullabilityKind> getNullability(const ASTContext &context) const; |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | bool canHaveNullability(bool ResultIfUnknown = true) const; |
2426 | |
2427 | |
2428 | |
2429 | |
2430 | |
2431 | |
2432 | |
2433 | |
2434 | |
2435 | |
2436 | |
2437 | |
2438 | |
2439 | |
2440 | |
2441 | |
2442 | Optional<ArrayRef<QualType>> |
2443 | getObjCSubstitutions(const DeclContext *dc) const; |
2444 | |
2445 | |
2446 | |
2447 | bool acceptsObjCTypeParams() const; |
2448 | |
2449 | const char *getTypeClassName() const; |
2450 | |
2451 | QualType getCanonicalTypeInternal() const { |
2452 | return CanonicalType; |
2453 | } |
2454 | |
2455 | CanQualType getCanonicalTypeUnqualified() const; |
2456 | void dump() const; |
2457 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
2458 | }; |
2459 | |
2460 | |
2461 | |
2462 | template <> const TypedefType *Type::getAs() const; |
2463 | |
2464 | |
2465 | |
2466 | |
2467 | template <> const TemplateSpecializationType *Type::getAs() const; |
2468 | |
2469 | |
2470 | |
2471 | template <> const AttributedType *Type::getAs() const; |
2472 | |
2473 | |
2474 | |
2475 | #define TYPE(Class, Base) |
2476 | #define LEAF_TYPE(Class) \ |
2477 | template <> inline const Class##Type *Type::getAs() const { \ |
2478 | return dyn_cast<Class##Type>(CanonicalType); \ |
2479 | } \ |
2480 | template <> inline const Class##Type *Type::castAs() const { \ |
2481 | return cast<Class##Type>(CanonicalType); \ |
2482 | } |
2483 | #include "clang/AST/TypeNodes.inc" |
2484 | |
2485 | |
2486 | |
2487 | class BuiltinType : public Type { |
2488 | public: |
2489 | enum Kind { |
2490 | |
2491 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, |
2492 | #include "clang/Basic/OpenCLImageTypes.def" |
2493 | |
2494 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, |
2495 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2496 | |
2497 | #define SVE_TYPE(Name, Id, SingletonId) Id, |
2498 | #include "clang/Basic/AArch64SVEACLETypes.def" |
2499 | |
2500 | #define PPC_VECTOR_TYPE(Name, Id, Size) Id, |
2501 | #include "clang/Basic/PPCTypes.def" |
2502 | |
2503 | #define RVV_TYPE(Name, Id, SingletonId) Id, |
2504 | #include "clang/Basic/RISCVVTypes.def" |
2505 | |
2506 | #define BUILTIN_TYPE(Id, SingletonId) Id, |
2507 | #define LAST_BUILTIN_TYPE(Id) LastKind = Id |
2508 | #include "clang/AST/BuiltinTypes.def" |
2509 | }; |
2510 | |
2511 | private: |
2512 | friend class ASTContext; |
2513 | |
2514 | BuiltinType(Kind K) |
2515 | : Type(Builtin, QualType(), |
2516 | K == Dependent ? TypeDependence::DependentInstantiation |
2517 | : TypeDependence::None) { |
2518 | BuiltinTypeBits.Kind = K; |
2519 | } |
2520 | |
2521 | public: |
2522 | Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } |
2523 | StringRef getName(const PrintingPolicy &Policy) const; |
2524 | |
2525 | const char *getNameAsCString(const PrintingPolicy &Policy) const { |
2526 | |
2527 | StringRef str = getName(Policy); |
2528 | assert(!str.empty() && str.data()[str.size()] == '\0'); |
2529 | return str.data(); |
2530 | } |
2531 | |
2532 | bool isSugared() const { return false; } |
2533 | QualType desugar() const { return QualType(this, 0); } |
2534 | |
2535 | bool isInteger() const { |
2536 | return getKind() >= Bool && getKind() <= Int128; |
2537 | } |
2538 | |
2539 | bool isSignedInteger() const { |
2540 | return getKind() >= Char_S && getKind() <= Int128; |
2541 | } |
2542 | |
2543 | bool isUnsignedInteger() const { |
2544 | return getKind() >= Bool && getKind() <= UInt128; |
2545 | } |
2546 | |
2547 | bool isFloatingPoint() const { |
2548 | return getKind() >= Half && getKind() <= Float128; |
2549 | } |
2550 | |
2551 | |
2552 | static bool isPlaceholderTypeKind(Kind K) { |
2553 | return K >= Overload; |
2554 | } |
2555 | |
2556 | |
2557 | |
2558 | |
2559 | bool isPlaceholderType() const { |
2560 | return isPlaceholderTypeKind(getKind()); |
2561 | } |
2562 | |
2563 | |
2564 | |
2565 | |
2566 | |
2567 | |
2568 | |
2569 | |
2570 | |
2571 | |
2572 | bool isNonOverloadPlaceholderType() const { |
2573 | return getKind() > Overload; |
2574 | } |
2575 | |
2576 | static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } |
2577 | }; |
2578 | |
2579 | |
2580 | |
2581 | class ComplexType : public Type, public llvm::FoldingSetNode { |
2582 | friend class ASTContext; |
2583 | |
2584 | QualType ElementType; |
2585 | |
2586 | ComplexType(QualType Element, QualType CanonicalPtr) |
2587 | : Type(Complex, CanonicalPtr, Element->getDependence()), |
2588 | ElementType(Element) {} |
2589 | |
2590 | public: |
2591 | QualType getElementType() const { return ElementType; } |
2592 | |
2593 | bool isSugared() const { return false; } |
2594 | QualType desugar() const { return QualType(this, 0); } |
2595 | |
2596 | void Profile(llvm::FoldingSetNodeID &ID) { |
2597 | Profile(ID, getElementType()); |
2598 | } |
2599 | |
2600 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { |
2601 | ID.AddPointer(Element.getAsOpaquePtr()); |
2602 | } |
2603 | |
2604 | static bool classof(const Type *T) { return T->getTypeClass() == Complex; } |
2605 | }; |
2606 | |
2607 | |
2608 | class ParenType : public Type, public llvm::FoldingSetNode { |
2609 | friend class ASTContext; |
2610 | |
2611 | QualType Inner; |
2612 | |
2613 | ParenType(QualType InnerType, QualType CanonType) |
2614 | : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {} |
2615 | |
2616 | public: |
2617 | QualType getInnerType() const { return Inner; } |
2618 | |
2619 | bool isSugared() const { return true; } |
2620 | QualType desugar() const { return getInnerType(); } |
2621 | |
2622 | void Profile(llvm::FoldingSetNodeID &ID) { |
2623 | Profile(ID, getInnerType()); |
2624 | } |
2625 | |
2626 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { |
2627 | Inner.Profile(ID); |
2628 | } |
2629 | |
2630 | static bool classof(const Type *T) { return T->getTypeClass() == Paren; } |
2631 | }; |
2632 | |
2633 | |
2634 | class PointerType : public Type, public llvm::FoldingSetNode { |
2635 | friend class ASTContext; |
2636 | |
2637 | QualType PointeeType; |
2638 | |
2639 | PointerType(QualType Pointee, QualType CanonicalPtr) |
2640 | : Type(Pointer, CanonicalPtr, Pointee->getDependence()), |
2641 | PointeeType(Pointee) {} |
2642 | |
2643 | public: |
2644 | QualType getPointeeType() const { return PointeeType; } |
2645 | |
2646 | bool isSugared() const { return false; } |
2647 | QualType desugar() const { return QualType(this, 0); } |
2648 | |
2649 | void Profile(llvm::FoldingSetNodeID &ID) { |
2650 | Profile(ID, getPointeeType()); |
2651 | } |
2652 | |
2653 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2654 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2655 | } |
2656 | |
2657 | static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } |
2658 | }; |
2659 | |
2660 | |
2661 | |
2662 | |
2663 | class AdjustedType : public Type, public llvm::FoldingSetNode { |
2664 | QualType OriginalTy; |
2665 | QualType AdjustedTy; |
2666 | |
2667 | protected: |
2668 | friend class ASTContext; |
2669 | |
2670 | AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, |
2671 | QualType CanonicalPtr) |
2672 | : Type(TC, CanonicalPtr, OriginalTy->getDependence()), |
2673 | OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} |
2674 | |
2675 | public: |
2676 | QualType getOriginalType() const { return OriginalTy; } |
2677 | QualType getAdjustedType() const { return AdjustedTy; } |
2678 | |
2679 | bool isSugared() const { return true; } |
2680 | QualType desugar() const { return AdjustedTy; } |
2681 | |
2682 | void Profile(llvm::FoldingSetNodeID &ID) { |
2683 | Profile(ID, OriginalTy, AdjustedTy); |
2684 | } |
2685 | |
2686 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { |
2687 | ID.AddPointer(Orig.getAsOpaquePtr()); |
2688 | ID.AddPointer(New.getAsOpaquePtr()); |
2689 | } |
2690 | |
2691 | static bool classof(const Type *T) { |
2692 | return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; |
2693 | } |
2694 | }; |
2695 | |
2696 | |
2697 | class DecayedType : public AdjustedType { |
2698 | friend class ASTContext; |
2699 | |
2700 | inline |
2701 | DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); |
2702 | |
2703 | public: |
2704 | QualType getDecayedType() const { return getAdjustedType(); } |
2705 | |
2706 | inline QualType getPointeeType() const; |
2707 | |
2708 | static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } |
2709 | }; |
2710 | |
2711 | |
2712 | |
2713 | |
2714 | class BlockPointerType : public Type, public llvm::FoldingSetNode { |
2715 | friend class ASTContext; |
2716 | |
2717 | |
2718 | QualType PointeeType; |
2719 | |
2720 | BlockPointerType(QualType Pointee, QualType CanonicalCls) |
2721 | : Type(BlockPointer, CanonicalCls, Pointee->getDependence()), |
2722 | PointeeType(Pointee) {} |
2723 | |
2724 | public: |
2725 | |
2726 | QualType getPointeeType() const { return PointeeType; } |
2727 | |
2728 | bool isSugared() const { return false; } |
2729 | QualType desugar() const { return QualType(this, 0); } |
2730 | |
2731 | void Profile(llvm::FoldingSetNodeID &ID) { |
2732 | Profile(ID, getPointeeType()); |
2733 | } |
2734 | |
2735 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2736 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2737 | } |
2738 | |
2739 | static bool classof(const Type *T) { |
2740 | return T->getTypeClass() == BlockPointer; |
2741 | } |
2742 | }; |
2743 | |
2744 | |
2745 | class ReferenceType : public Type, public llvm::FoldingSetNode { |
2746 | QualType PointeeType; |
2747 | |
2748 | protected: |
2749 | ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, |
2750 | bool SpelledAsLValue) |
2751 | : Type(tc, CanonicalRef, Referencee->getDependence()), |
2752 | PointeeType(Referencee) { |
2753 | ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; |
2754 | ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); |
2755 | } |
2756 | |
2757 | public: |
2758 | bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } |
2759 | bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } |
2760 | |
2761 | QualType getPointeeTypeAsWritten() const { return PointeeType; } |
2762 | |
2763 | QualType getPointeeType() const { |
2764 | |
2765 | const ReferenceType *T = this; |
2766 | while (T->isInnerRef()) |
2767 | T = T->PointeeType->castAs<ReferenceType>(); |
2768 | return T->PointeeType; |
2769 | } |
2770 | |
2771 | void Profile(llvm::FoldingSetNodeID &ID) { |
2772 | Profile(ID, PointeeType, isSpelledAsLValue()); |
2773 | } |
2774 | |
2775 | static void Profile(llvm::FoldingSetNodeID &ID, |
2776 | QualType Referencee, |
2777 | bool SpelledAsLValue) { |
2778 | ID.AddPointer(Referencee.getAsOpaquePtr()); |
2779 | ID.AddBoolean(SpelledAsLValue); |
2780 | } |
2781 | |
2782 | static bool classof(const Type *T) { |
2783 | return T->getTypeClass() == LValueReference || |
2784 | T->getTypeClass() == RValueReference; |
2785 | } |
2786 | }; |
2787 | |
2788 | |
2789 | class LValueReferenceType : public ReferenceType { |
2790 | friend class ASTContext; |
2791 | |
2792 | LValueReferenceType(QualType Referencee, QualType CanonicalRef, |
2793 | bool SpelledAsLValue) |
2794 | : ReferenceType(LValueReference, Referencee, CanonicalRef, |
2795 | SpelledAsLValue) {} |
2796 | |
2797 | public: |
2798 | bool isSugared() const { return false; } |
2799 | QualType desugar() const { return QualType(this, 0); } |
2800 | |
2801 | static bool classof(const Type *T) { |
2802 | return T->getTypeClass() == LValueReference; |
2803 | } |
2804 | }; |
2805 | |
2806 | |
2807 | class RValueReferenceType : public ReferenceType { |
2808 | friend class ASTContext; |
2809 | |
2810 | RValueReferenceType(QualType Referencee, QualType CanonicalRef) |
2811 | : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} |
2812 | |
2813 | public: |
2814 | bool isSugared() const { return false; } |
2815 | QualType desugar() const { return QualType(this, 0); } |
2816 | |
2817 | static bool classof(const Type *T) { |
2818 | return T->getTypeClass() == RValueReference; |
2819 | } |
2820 | }; |
2821 | |
2822 | |
2823 | |
2824 | |
2825 | class MemberPointerType : public Type, public llvm::FoldingSetNode { |
2826 | friend class ASTContext; |
2827 | |
2828 | QualType PointeeType; |
2829 | |
2830 | |
2831 | |
2832 | const Type *Class; |
2833 | |
2834 | MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) |
2835 | : Type(MemberPointer, CanonicalPtr, |
2836 | (Cls->getDependence() & ~TypeDependence::VariablyModified) | |
2837 | Pointee->getDependence()), |
2838 | PointeeType(Pointee), Class(Cls) {} |
2839 | |
2840 | public: |
2841 | QualType getPointeeType() const { return PointeeType; } |
2842 | |
2843 | |
2844 | |
2845 | bool isMemberFunctionPointer() const { |
2846 | return PointeeType->isFunctionProtoType(); |
2847 | } |
2848 | |
2849 | |
2850 | |
2851 | bool isMemberDataPointer() const { |
2852 | return !PointeeType->isFunctionProtoType(); |
2853 | } |
2854 | |
2855 | const Type *getClass() const { return Class; } |
2856 | CXXRecordDecl *getMostRecentCXXRecordDecl() const; |
2857 | |
2858 | bool isSugared() const { return false; } |
2859 | QualType desugar() const { return QualType(this, 0); } |
2860 | |
2861 | void Profile(llvm::FoldingSetNodeID &ID) { |
2862 | Profile(ID, getPointeeType(), getClass()); |
2863 | } |
2864 | |
2865 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, |
2866 | const Type *Class) { |
2867 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2868 | ID.AddPointer(Class); |
2869 | } |
2870 | |
2871 | static bool classof(const Type *T) { |
2872 | return T->getTypeClass() == MemberPointer; |
2873 | } |
2874 | }; |
2875 | |
2876 | |
2877 | class ArrayType : public Type, public llvm::FoldingSetNode { |
2878 | public: |
2879 | |
2880 | |
2881 | |
2882 | |
2883 | enum ArraySizeModifier { |
2884 | Normal, Static, Star |
2885 | }; |
2886 | |
2887 | private: |
2888 | |
2889 | QualType ElementType; |
2890 | |
2891 | protected: |
2892 | friend class ASTContext; |
2893 | |
2894 | ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, |
2895 | unsigned tq, const Expr *sz = nullptr); |
2896 | |
2897 | public: |
2898 | QualType getElementType() const { return ElementType; } |
2899 | |
2900 | ArraySizeModifier getSizeModifier() const { |
2901 | return ArraySizeModifier(ArrayTypeBits.SizeModifier); |
2902 | } |
2903 | |
2904 | Qualifiers getIndexTypeQualifiers() const { |
2905 | return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); |
2906 | } |
2907 | |
2908 | unsigned getIndexTypeCVRQualifiers() const { |
2909 | return ArrayTypeBits.IndexTypeQuals; |
2910 | } |
2911 | |
2912 | static bool classof(const Type *T) { |
2913 | return T->getTypeClass() == ConstantArray || |
2914 | T->getTypeClass() == VariableArray || |
2915 | T->getTypeClass() == IncompleteArray || |
2916 | T->getTypeClass() == DependentSizedArray; |
2917 | } |
2918 | }; |
2919 | |
2920 | |
2921 | |
2922 | |
2923 | class ConstantArrayType final |
2924 | : public ArrayType, |
2925 | private llvm::TrailingObjects<ConstantArrayType, const Expr *> { |
2926 | friend class ASTContext; |
2927 | friend TrailingObjects; |
2928 | |
2929 | llvm::APInt Size; |
2930 | |
2931 | ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, |
2932 | const Expr *sz, ArraySizeModifier sm, unsigned tq) |
2933 | : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) { |
2934 | ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr; |
2935 | if (ConstantArrayTypeBits.HasStoredSizeExpr) { |
2936 | assert(!can.isNull() && "canonical constant array should not have size"); |
2937 | *getTrailingObjects<const Expr*>() = sz; |
2938 | } |
2939 | } |
2940 | |
2941 | unsigned numTrailingObjects(OverloadToken<const Expr*>) const { |
2942 | return ConstantArrayTypeBits.HasStoredSizeExpr; |
2943 | } |
2944 | |
2945 | public: |
2946 | const llvm::APInt &getSize() const { return Size; } |
2947 | const Expr *getSizeExpr() const { |
2948 | return ConstantArrayTypeBits.HasStoredSizeExpr |
2949 | ? *getTrailingObjects<const Expr *>() |
2950 | : nullptr; |
2951 | } |
2952 | bool isSugared() const { return false; } |
2953 | QualType desugar() const { return QualType(this, 0); } |
2954 | |
2955 | |
2956 | |
2957 | static unsigned getNumAddressingBits(const ASTContext &Context, |
2958 | QualType ElementType, |
2959 | const llvm::APInt &NumElements); |
2960 | |
2961 | |
2962 | |
2963 | static unsigned getMaxSizeBits(const ASTContext &Context); |
2964 | |
2965 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
2966 | Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(), |
2967 | getSizeModifier(), getIndexTypeCVRQualifiers()); |
2968 | } |
2969 | |
2970 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, |
2971 | QualType ET, const llvm::APInt &ArraySize, |
2972 | const Expr *SizeExpr, ArraySizeModifier SizeMod, |
2973 | unsigned TypeQuals); |
2974 | |
2975 | static bool classof(const Type *T) { |
2976 | return T->getTypeClass() == ConstantArray; |
2977 | } |
2978 | }; |
2979 | |
2980 | |
2981 | |
2982 | |
2983 | class IncompleteArrayType : public ArrayType { |
2984 | friend class ASTContext; |
2985 | |
2986 | IncompleteArrayType(QualType et, QualType can, |
2987 | ArraySizeModifier sm, unsigned tq) |
2988 | : ArrayType(IncompleteArray, et, can, sm, tq) {} |
2989 | |
2990 | public: |
2991 | friend class StmtIteratorBase; |
2992 | |
2993 | bool isSugared() const { return false; } |
2994 | QualType desugar() const { return QualType(this, 0); } |
2995 | |
2996 | static bool classof(const Type *T) { |
2997 | return T->getTypeClass() == IncompleteArray; |
2998 | } |
2999 | |
3000 | void Profile(llvm::FoldingSetNodeID &ID) { |
3001 | Profile(ID, getElementType(), getSizeModifier(), |
3002 | getIndexTypeCVRQualifiers()); |
3003 | } |
3004 | |
3005 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, |
3006 | ArraySizeModifier SizeMod, unsigned TypeQuals) { |
3007 | ID.AddPointer(ET.getAsOpaquePtr()); |
3008 | ID.AddInteger(SizeMod); |
3009 | ID.AddInteger(TypeQuals); |
3010 | } |
3011 | }; |
3012 | |
3013 | |
3014 | |
3015 | |
3016 | |
3017 | |
3018 | |
3019 | |
3020 | |
3021 | |
3022 | |
3023 | |
3024 | |
3025 | |
3026 | |
3027 | class VariableArrayType : public ArrayType { |
3028 | friend class ASTContext; |
3029 | |
3030 | |
3031 | |
3032 | Stmt *SizeExpr; |
3033 | |
3034 | |
3035 | SourceRange Brackets; |
3036 | |
3037 | VariableArrayType(QualType et, QualType can, Expr *e, |
3038 | ArraySizeModifier sm, unsigned tq, |
3039 | SourceRange brackets) |
3040 | : ArrayType(VariableArray, et, can, sm, tq, e), |
3041 | SizeExpr((Stmt*) e), Brackets(brackets) {} |
3042 | |
3043 | public: |
3044 | friend class StmtIteratorBase; |
3045 | |
3046 | Expr *getSizeExpr() const { |
3047 | |
3048 | |
3049 | return (Expr*) SizeExpr; |
3050 | } |
3051 | |
3052 | SourceRange getBracketsRange() const { return Brackets; } |
3053 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3054 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3055 | |
3056 | bool isSugared() const { return false; } |
3057 | QualType desugar() const { return QualType(this, 0); } |
3058 | |
3059 | static bool classof(const Type *T) { |
3060 | return T->getTypeClass() == VariableArray; |
3061 | } |
3062 | |
3063 | void Profile(llvm::FoldingSetNodeID &ID) { |
3064 | llvm_unreachable("Cannot unique VariableArrayTypes."); |
3065 | } |
3066 | }; |
3067 | |
3068 | |
3069 | |
3070 | |
3071 | |
3072 | |
3073 | |
3074 | |
3075 | |
3076 | |
3077 | |
3078 | |
3079 | |
3080 | |
3081 | class DependentSizedArrayType : public ArrayType { |
3082 | friend class ASTContext; |
3083 | |
3084 | const ASTContext &Context; |
3085 | |
3086 | |
3087 | |
3088 | |
3089 | |
3090 | |
3091 | Stmt *SizeExpr; |
3092 | |
3093 | |
3094 | SourceRange Brackets; |
3095 | |
3096 | DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, |
3097 | Expr *e, ArraySizeModifier sm, unsigned tq, |
3098 | SourceRange brackets); |
3099 | |
3100 | public: |
3101 | friend class StmtIteratorBase; |
3102 | |
3103 | Expr *getSizeExpr() const { |
3104 | |
3105 | |
3106 | return (Expr*) SizeExpr; |
3107 | } |
3108 | |
3109 | SourceRange getBracketsRange() const { return Brackets; } |
3110 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3111 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3112 | |
3113 | bool isSugared() const { return false; } |
3114 | QualType desugar() const { return QualType(this, 0); } |
3115 | |
3116 | static bool classof(const Type *T) { |
3117 | return T->getTypeClass() == DependentSizedArray; |
3118 | } |
3119 | |
3120 | void Profile(llvm::FoldingSetNodeID &ID) { |
3121 | Profile(ID, Context, getElementType(), |
3122 | getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); |
3123 | } |
3124 | |
3125 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3126 | QualType ET, ArraySizeModifier SizeMod, |
3127 | unsigned TypeQuals, Expr *E); |
3128 | }; |
3129 | |
3130 | |
3131 | |
3132 | |
3133 | |
3134 | |
3135 | |
3136 | |
3137 | |
3138 | |
3139 | |
3140 | |
3141 | class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { |
3142 | friend class ASTContext; |
3143 | |
3144 | const ASTContext &Context; |
3145 | Expr *AddrSpaceExpr; |
3146 | QualType PointeeType; |
3147 | SourceLocation loc; |
3148 | |
3149 | DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType, |
3150 | QualType can, Expr *AddrSpaceExpr, |
3151 | SourceLocation loc); |
3152 | |
3153 | public: |
3154 | Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } |
3155 | QualType getPointeeType() const { return PointeeType; } |
3156 | SourceLocation getAttributeLoc() const { return loc; } |
3157 | |
3158 | bool isSugared() const { return false; } |
3159 | QualType desugar() const { return QualType(this, 0); } |
3160 | |
3161 | static bool classof(const Type *T) { |
3162 | return T->getTypeClass() == DependentAddressSpace; |
3163 | } |
3164 | |
3165 | void Profile(llvm::FoldingSetNodeID &ID) { |
3166 | Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); |
3167 | } |
3168 | |
3169 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3170 | QualType PointeeType, Expr *AddrSpaceExpr); |
3171 | }; |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | |
3177 | |
3178 | |
3179 | |
3180 | |
3181 | |
3182 | |
3183 | class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { |
3184 | friend class ASTContext; |
3185 | |
3186 | const ASTContext &Context; |
3187 | Expr *SizeExpr; |
3188 | |
3189 | |
3190 | QualType ElementType; |
3191 | |
3192 | SourceLocation loc; |
3193 | |
3194 | DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, |
3195 | QualType can, Expr *SizeExpr, SourceLocation loc); |
3196 | |
3197 | public: |
3198 | Expr *getSizeExpr() const { return SizeExpr; } |
3199 | QualType getElementType() const { return ElementType; } |
3200 | SourceLocation getAttributeLoc() const { return loc; } |
3201 | |
3202 | bool isSugared() const { return false; } |
3203 | QualType desugar() const { return QualType(this, 0); } |
3204 | |
3205 | static bool classof(const Type *T) { |
3206 | return T->getTypeClass() == DependentSizedExtVector; |
3207 | } |
3208 | |
3209 | void Profile(llvm::FoldingSetNodeID &ID) { |
3210 | Profile(ID, Context, getElementType(), getSizeExpr()); |
3211 | } |
3212 | |
3213 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3214 | QualType ElementType, Expr *SizeExpr); |
3215 | }; |
3216 | |
3217 | |
3218 | |
3219 | |
3220 | |
3221 | |
3222 | |
3223 | class VectorType : public Type, public llvm::FoldingSetNode { |
3224 | public: |
3225 | enum VectorKind { |
3226 | |
3227 | GenericVector, |
3228 | |
3229 | |
3230 | AltiVecVector, |
3231 | |
3232 | |
3233 | AltiVecPixel, |
3234 | |
3235 | |
3236 | AltiVecBool, |
3237 | |
3238 | |
3239 | NeonVector, |
3240 | |
3241 | |
3242 | NeonPolyVector, |
3243 | |
3244 | |
3245 | SveFixedLengthDataVector, |
3246 | |
3247 | |
3248 | SveFixedLengthPredicateVector |
3249 | }; |
3250 | |
3251 | protected: |
3252 | friend class ASTContext; |
3253 | |
3254 | |
3255 | QualType ElementType; |
3256 | |
3257 | VectorType(QualType vecType, unsigned nElements, QualType canonType, |
3258 | VectorKind vecKind); |
3259 | |
3260 | VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
3261 | QualType canonType, VectorKind vecKind); |
3262 | |
3263 | public: |
3264 | QualType getElementType() const { return ElementType; } |
3265 | unsigned getNumElements() const { return VectorTypeBits.NumElements; } |
3266 | |
3267 | bool isSugared() const { return false; } |
3268 | QualType desugar() const { return QualType(this, 0); } |
3269 | |
3270 | VectorKind getVectorKind() const { |
3271 | return VectorKind(VectorTypeBits.VecKind); |
3272 | } |
3273 | |
3274 | void Profile(llvm::FoldingSetNodeID &ID) { |
3275 | Profile(ID, getElementType(), getNumElements(), |
3276 | getTypeClass(), getVectorKind()); |
3277 | } |
3278 | |
3279 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3280 | unsigned NumElements, TypeClass TypeClass, |
3281 | VectorKind VecKind) { |
3282 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3283 | ID.AddInteger(NumElements); |
3284 | ID.AddInteger(TypeClass); |
3285 | ID.AddInteger(VecKind); |
3286 | } |
3287 | |
3288 | static bool classof(const Type *T) { |
3289 | return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; |
3290 | } |
3291 | }; |
3292 | |
3293 | |
3294 | |
3295 | |
3296 | |
3297 | |
3298 | |
3299 | |
3300 | |
3301 | |
3302 | class DependentVectorType : public Type, public llvm::FoldingSetNode { |
3303 | friend class ASTContext; |
3304 | |
3305 | const ASTContext &Context; |
3306 | QualType ElementType; |
3307 | Expr *SizeExpr; |
3308 | SourceLocation Loc; |
3309 | |
3310 | DependentVectorType(const ASTContext &Context, QualType ElementType, |
3311 | QualType CanonType, Expr *SizeExpr, |
3312 | SourceLocation Loc, VectorType::VectorKind vecKind); |
3313 | |
3314 | public: |
3315 | Expr *getSizeExpr() const { return SizeExpr; } |
3316 | QualType getElementType() const { return ElementType; } |
3317 | SourceLocation getAttributeLoc() const { return Loc; } |
3318 | VectorType::VectorKind getVectorKind() const { |
3319 | return VectorType::VectorKind(VectorTypeBits.VecKind); |
3320 | } |
3321 | |
3322 | bool isSugared() const { return false; } |
3323 | QualType desugar() const { return QualType(this, 0); } |
3324 | |
3325 | static bool classof(const Type *T) { |
3326 | return T->getTypeClass() == DependentVector; |
3327 | } |
3328 | |
3329 | void Profile(llvm::FoldingSetNodeID &ID) { |
3330 | Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); |
3331 | } |
3332 | |
3333 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3334 | QualType ElementType, const Expr *SizeExpr, |
3335 | VectorType::VectorKind VecKind); |
3336 | }; |
3337 | |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | |
3343 | |
3344 | class ExtVectorType : public VectorType { |
3345 | friend class ASTContext; |
3346 | |
3347 | ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) |
3348 | : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} |
3349 | |
3350 | public: |
3351 | static int getPointAccessorIdx(char c) { |
3352 | switch (c) { |
3353 | default: return -1; |
3354 | case 'x': case 'r': return 0; |
3355 | case 'y': case 'g': return 1; |
3356 | case 'z': case 'b': return 2; |
3357 | case 'w': case 'a': return 3; |
3358 | } |
3359 | } |
3360 | |
3361 | static int getNumericAccessorIdx(char c) { |
3362 | switch (c) { |
3363 | default: return -1; |
3364 | case '0': return 0; |
3365 | case '1': return 1; |
3366 | case '2': return 2; |
3367 | case '3': return 3; |
3368 | case '4': return 4; |
3369 | case '5': return 5; |
3370 | case '6': return 6; |
3371 | case '7': return 7; |
3372 | case '8': return 8; |
3373 | case '9': return 9; |
3374 | case 'A': |
3375 | case 'a': return 10; |
3376 | case 'B': |
3377 | case 'b': return 11; |
3378 | case 'C': |
3379 | case 'c': return 12; |
3380 | case 'D': |
3381 | case 'd': return 13; |
3382 | case 'E': |
3383 | case 'e': return 14; |
3384 | case 'F': |
3385 | case 'f': return 15; |
3386 | } |
3387 | } |
3388 | |
3389 | static int getAccessorIdx(char c, bool isNumericAccessor) { |
3390 | if (isNumericAccessor) |
3391 | return getNumericAccessorIdx(c); |
3392 | else |
3393 | return getPointAccessorIdx(c); |
3394 | } |
3395 | |
3396 | bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { |
3397 | if (int idx = getAccessorIdx(c, isNumericAccessor)+1) |
3398 | return unsigned(idx-1) < getNumElements(); |
3399 | return false; |
3400 | } |
3401 | |
3402 | bool isSugared() const { return false; } |
3403 | QualType desugar() const { return QualType(this, 0); } |
3404 | |
3405 | static bool classof(const Type *T) { |
3406 | return T->getTypeClass() == ExtVector; |
3407 | } |
3408 | }; |
3409 | |
3410 | |
3411 | |
3412 | |
3413 | class MatrixType : public Type, public llvm::FoldingSetNode { |
3414 | protected: |
3415 | friend class ASTContext; |
3416 | |
3417 | |
3418 | QualType ElementType; |
3419 | |
3420 | MatrixType(QualType ElementTy, QualType CanonElementTy); |
3421 | |
3422 | MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy, |
3423 | const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr); |
3424 | |
3425 | public: |
3426 | |
3427 | QualType getElementType() const { return ElementType; } |
3428 | |
3429 | |
3430 | |
3431 | |
3432 | |
3433 | |
3434 | static bool isValidElementType(QualType T) { |
3435 | return T->isDependentType() || |
3436 | (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType()); |
3437 | } |
3438 | |
3439 | bool isSugared() const { return false; } |
3440 | QualType desugar() const { return QualType(this, 0); } |
3441 | |
3442 | static bool classof(const Type *T) { |
3443 | return T->getTypeClass() == ConstantMatrix || |
3444 | T->getTypeClass() == DependentSizedMatrix; |
3445 | } |
3446 | }; |
3447 | |
3448 | |
3449 | class ConstantMatrixType final : public MatrixType { |
3450 | protected: |
3451 | friend class ASTContext; |
3452 | |
3453 | |
3454 | |
3455 | QualType ElementType; |
3456 | |
3457 | |
3458 | unsigned NumRows; |
3459 | unsigned NumColumns; |
3460 | |
3461 | static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1; |
3462 | |
3463 | ConstantMatrixType(QualType MatrixElementType, unsigned NRows, |
3464 | unsigned NColumns, QualType CanonElementType); |
3465 | |
3466 | ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows, |
3467 | unsigned NColumns, QualType CanonElementType); |
3468 | |
3469 | public: |
3470 | |
3471 | unsigned getNumRows() const { return NumRows; } |
3472 | |
3473 | |
3474 | unsigned getNumColumns() const { return NumColumns; } |
3475 | |
3476 | |
3477 | unsigned getNumElementsFlattened() const { |
3478 | return getNumRows() * getNumColumns(); |
3479 | } |
3480 | |
3481 | |
3482 | static constexpr bool isDimensionValid(size_t NumElements) { |
3483 | return NumElements > 0 && NumElements <= MaxElementsPerDimension; |
3484 | } |
3485 | |
3486 | |
3487 | static constexpr unsigned getMaxElementsPerDimension() { |
3488 | return MaxElementsPerDimension; |
3489 | } |
3490 | |
3491 | void Profile(llvm::FoldingSetNodeID &ID) { |
3492 | Profile(ID, getElementType(), getNumRows(), getNumColumns(), |
3493 | getTypeClass()); |
3494 | } |
3495 | |
3496 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3497 | unsigned NumRows, unsigned NumColumns, |
3498 | TypeClass TypeClass) { |
3499 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3500 | ID.AddInteger(NumRows); |
3501 | ID.AddInteger(NumColumns); |
3502 | ID.AddInteger(TypeClass); |
3503 | } |
3504 | |
3505 | static bool classof(const Type *T) { |
3506 | return T->getTypeClass() == ConstantMatrix; |
3507 | } |
3508 | }; |
3509 | |
3510 | |
3511 | |
3512 | class DependentSizedMatrixType final : public MatrixType { |
3513 | friend class ASTContext; |
3514 | |
3515 | const ASTContext &Context; |
3516 | Expr *RowExpr; |
3517 | Expr *ColumnExpr; |
3518 | |
3519 | SourceLocation loc; |
3520 | |
3521 | DependentSizedMatrixType(const ASTContext &Context, QualType ElementType, |
3522 | QualType CanonicalType, Expr *RowExpr, |
3523 | Expr *ColumnExpr, SourceLocation loc); |
3524 | |
3525 | public: |
3526 | QualType getElementType() const { return ElementType; } |
3527 | Expr *getRowExpr() const { return RowExpr; } |
3528 | Expr *getColumnExpr() const { return ColumnExpr; } |
3529 | SourceLocation getAttributeLoc() const { return loc; } |
3530 | |
3531 | bool isSugared() const { return false; } |
3532 | QualType desugar() const { return QualType(this, 0); } |
3533 | |
3534 | static bool classof(const Type *T) { |
3535 | return T->getTypeClass() == DependentSizedMatrix; |
3536 | } |
3537 | |
3538 | void Profile(llvm::FoldingSetNodeID &ID) { |
3539 | Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr()); |
3540 | } |
3541 | |
3542 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3543 | QualType ElementType, Expr *RowExpr, Expr *ColumnExpr); |
3544 | }; |
3545 | |
3546 | |
3547 | |
3548 | class FunctionType : public Type { |
3549 | |
3550 | QualType ResultType; |
3551 | |
3552 | public: |
3553 | |
3554 | |
3555 | |
3556 | |
3557 | |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | |
3565 | |
3566 | |
3567 | |
3568 | |
3569 | |
3570 | |
3571 | |
3572 | |
3573 | |
3574 | class ExtParameterInfo { |
3575 | enum { |
3576 | ABIMask = 0x0F, |
3577 | IsConsumed = 0x10, |
3578 | HasPassObjSize = 0x20, |
3579 | IsNoEscape = 0x40, |
3580 | }; |
3581 | unsigned char Data = 0; |
3582 | |
3583 | public: |
3584 | ExtParameterInfo() = default; |
3585 | |
3586 | |
3587 | ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } |
3588 | ExtParameterInfo withABI(ParameterABI kind) const { |
3589 | ExtParameterInfo copy = *this; |
3590 | copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); |
3591 | return copy; |
3592 | } |
3593 | |
3594 | |
3595 | |
3596 | bool isConsumed() const { return (Data & IsConsumed); } |
3597 | ExtParameterInfo withIsConsumed(bool consumed) const { |
3598 | ExtParameterInfo copy = *this; |
3599 | if (consumed) |
3600 | copy.Data |= IsConsumed; |
3601 | else |
3602 | copy.Data &= ~IsConsumed; |
3603 | return copy; |
3604 | } |
3605 | |
3606 | bool hasPassObjectSize() const { return Data & HasPassObjSize; } |
3607 | ExtParameterInfo withHasPassObjectSize() const { |
3608 | ExtParameterInfo Copy = *this; |
3609 | Copy.Data |= HasPassObjSize; |
3610 | return Copy; |
3611 | } |
3612 | |
3613 | bool isNoEscape() const { return Data & IsNoEscape; } |
3614 | ExtParameterInfo withIsNoEscape(bool NoEscape) const { |
3615 | ExtParameterInfo Copy = *this; |
3616 | if (NoEscape) |
3617 | Copy.Data |= IsNoEscape; |
3618 | else |
3619 | Copy.Data &= ~IsNoEscape; |
3620 | return Copy; |
3621 | } |
3622 | |
3623 | unsigned char getOpaqueValue() const { return Data; } |
3624 | static ExtParameterInfo getFromOpaqueValue(unsigned char data) { |
3625 | ExtParameterInfo result; |
3626 | result.Data = data; |
3627 | return result; |
3628 | } |
3629 | |
3630 | friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3631 | return lhs.Data == rhs.Data; |
3632 | } |
3633 | |
3634 | friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3635 | return lhs.Data != rhs.Data; |
3636 | } |
3637 | }; |
3638 | |
3639 | |
3640 | |
3641 | |
3642 | |
3643 | |
3644 | |
3645 | |
3646 | |
3647 | |
3648 | |
3649 | |
3650 | |
3651 | |
3652 | |
3653 | |
3654 | |
3655 | |
3656 | |
3657 | |
3658 | |
3659 | class ExtInfo { |
3660 | friend class FunctionType; |
3661 | |
3662 | |
3663 | |
3664 | |
3665 | |
3666 | |
3667 | |
3668 | |
3669 | |
3670 | enum { CallConvMask = 0x1F }; |
3671 | enum { NoReturnMask = 0x20 }; |
3672 | enum { ProducesResultMask = 0x40 }; |
3673 | enum { NoCallerSavedRegsMask = 0x80 }; |
3674 | enum { |
3675 | RegParmMask = 0x700, |
3676 | RegParmOffset = 8 |
3677 | }; |
3678 | enum { NoCfCheckMask = 0x800 }; |
3679 | enum { CmseNSCallMask = 0x1000 }; |
3680 | uint16_t Bits = CC_C; |
3681 | |
3682 | ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} |
3683 | |
3684 | public: |
3685 | |
3686 | |
3687 | ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, |
3688 | bool producesResult, bool noCallerSavedRegs, bool NoCfCheck, |
3689 | bool cmseNSCall) { |
3690 | assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); |
3691 | Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | |
3692 | (producesResult ? ProducesResultMask : 0) | |
3693 | (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | |
3694 | (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | |
3695 | (NoCfCheck ? NoCfCheckMask : 0) | |
3696 | (cmseNSCall ? CmseNSCallMask : 0); |
3697 | } |
3698 | |
3699 | |
3700 | |
3701 | ExtInfo() = default; |
3702 | |
3703 | |
3704 | |
3705 | ExtInfo(CallingConv CC) : Bits(CC) {} |
3706 | |
3707 | bool getNoReturn() const { return Bits & NoReturnMask; } |
3708 | bool getProducesResult() const { return Bits & ProducesResultMask; } |
3709 | bool getCmseNSCall() const { return Bits & CmseNSCallMask; } |
3710 | bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } |
3711 | bool getNoCfCheck() const { return Bits & NoCfCheckMask; } |
3712 | bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; } |
3713 | |
3714 | unsigned getRegParm() const { |
3715 | unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; |
3716 | if (RegParm > 0) |
3717 | --RegParm; |
3718 | return RegParm; |
3719 | } |
3720 | |
3721 | CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } |
3722 | |
3723 | bool operator==(ExtInfo Other) const { |
3724 | return Bits == Other.Bits; |
3725 | } |
3726 | bool operator!=(ExtInfo Other) const { |
3727 | return Bits != Other.Bits; |
3728 | } |
3729 | |
3730 | |
3731 | |
3732 | |
3733 | ExtInfo withNoReturn(bool noReturn) const { |
3734 | if (noReturn) |
3735 | return ExtInfo(Bits | NoReturnMask); |
3736 | else |
3737 | return ExtInfo(Bits & ~NoReturnMask); |
3738 | } |
3739 | |
3740 | ExtInfo withProducesResult(bool producesResult) const { |
3741 | if (producesResult) |
3742 | return ExtInfo(Bits | ProducesResultMask); |
3743 | else |
3744 | return ExtInfo(Bits & ~ProducesResultMask); |
3745 | } |
3746 | |
3747 | ExtInfo withCmseNSCall(bool cmseNSCall) const { |
3748 | if (cmseNSCall) |
3749 | return ExtInfo(Bits | CmseNSCallMask); |
3750 | else |
3751 | return ExtInfo(Bits & ~CmseNSCallMask); |
3752 | } |
3753 | |
3754 | ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { |
3755 | if (noCallerSavedRegs) |
3756 | return ExtInfo(Bits | NoCallerSavedRegsMask); |
3757 | else |
3758 | return ExtInfo(Bits & ~NoCallerSavedRegsMask); |
3759 | } |
3760 | |
3761 | ExtInfo withNoCfCheck(bool noCfCheck) const { |
3762 | if (noCfCheck) |
3763 | return ExtInfo(Bits | NoCfCheckMask); |
3764 | else |
3765 | return ExtInfo(Bits & ~NoCfCheckMask); |
3766 | } |
3767 | |
3768 | ExtInfo withRegParm(unsigned RegParm) const { |
3769 | assert(RegParm < 7 && "Invalid regparm value"); |
3770 | return ExtInfo((Bits & ~RegParmMask) | |
3771 | ((RegParm + 1) << RegParmOffset)); |
3772 | } |
3773 | |
3774 | ExtInfo withCallingConv(CallingConv cc) const { |
3775 | return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); |
3776 | } |
3777 | |
3778 | void Profile(llvm::FoldingSetNodeID &ID) const { |
3779 | ID.AddInteger(Bits); |
3780 | } |
3781 | }; |
3782 | |
3783 | |
3784 | |
3785 | |
3786 | struct ExceptionType { QualType Type; }; |
3787 | |
3788 | |
3789 | |
3790 | |
3791 | |
3792 | struct alignas(void *) FunctionTypeExtraBitfields { |
3793 | |
3794 | |
3795 | |
3796 | unsigned NumExceptionType; |
3797 | }; |
3798 | |
3799 | protected: |
3800 | FunctionType(TypeClass tc, QualType res, QualType Canonical, |
3801 | TypeDependence Dependence, ExtInfo Info) |
3802 | : Type(tc, Canonical, Dependence), ResultType(res) { |
3803 | FunctionTypeBits.ExtInfo = Info.Bits; |
3804 | } |
3805 | |
3806 | Qualifiers getFastTypeQuals() const { |
3807 | return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); |
3808 | } |
3809 | |
3810 | public: |
3811 | QualType getReturnType() const { return ResultType; } |
3812 | |
3813 | bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } |
3814 | unsigned getRegParmType() const { return getExtInfo().getRegParm(); } |
3815 | |
3816 | |
3817 | |
3818 | |
3819 | bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } |
3820 | |
3821 | bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); } |
3822 | CallingConv getCallConv() const { return getExtInfo().getCC(); } |
3823 | ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } |
3824 | |
3825 | static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, |
3826 | "Const, volatile and restrict are assumed to be a subset of " |
3827 | "the fast qualifiers."); |
3828 | |
3829 | bool isConst() const { return getFastTypeQuals().hasConst(); } |
3830 | bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } |
3831 | bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } |
3832 | |
3833 | |
3834 | |
3835 | QualType getCallResultType(const ASTContext &Context) const { |
3836 | return getReturnType().getNonLValueExprType(Context); |
3837 | } |
3838 | |
3839 | static StringRef getNameForCallConv(CallingConv CC); |
3840 | |
3841 | static bool classof(const Type *T) { |
3842 | return T->getTypeClass() == FunctionNoProto || |
3843 | T->getTypeClass() == FunctionProto; |
3844 | } |
3845 | }; |
3846 | |
3847 | |
3848 | |
3849 | class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { |
3850 | friend class ASTContext; |
3851 | |
3852 | FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) |
3853 | : FunctionType(FunctionNoProto, Result, Canonical, |
3854 | Result->getDependence() & |
3855 | ~(TypeDependence::DependentInstantiation | |
3856 | TypeDependence::UnexpandedPack), |
3857 | Info) {} |
3858 | |
3859 | public: |
3860 | |
3861 | |
3862 | bool isSugared() const { return false; } |
3863 | QualType desugar() const { return QualType(this, 0); } |
3864 | |
3865 | void Profile(llvm::FoldingSetNodeID &ID) { |
3866 | Profile(ID, getReturnType(), getExtInfo()); |
3867 | } |
3868 | |
3869 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, |
3870 | ExtInfo Info) { |
3871 | Info.Profile(ID); |
3872 | ID.AddPointer(ResultType.getAsOpaquePtr()); |
3873 | } |
3874 | |
3875 | static bool classof(const Type *T) { |
3876 | return T->getTypeClass() == FunctionNoProto; |
3877 | } |
3878 | }; |
3879 | |
3880 | |
3881 | |
3882 | |
3883 | |
3884 | |
3885 | |
3886 | |
3887 | class FunctionProtoType final |
3888 | : public FunctionType, |
3889 | public llvm::FoldingSetNode, |
3890 | private llvm::TrailingObjects< |
3891 | FunctionProtoType, QualType, SourceLocation, |
3892 | FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, |
3893 | Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { |
3894 | friend class ASTContext; |
3895 | friend TrailingObjects; |
3896 | |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | |
3902 | |
3903 | |
3904 | |
3905 | |
3906 | |
3907 | |
3908 | |
3909 | |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | |
3923 | |
3924 | |
3925 | |
3926 | |
3927 | |
3928 | |
3929 | |
3930 | |
3931 | |
3932 | |
3933 | |
3934 | |
3935 | |
3936 | |
3937 | |
3938 | |
3939 | public: |
3940 | |
3941 | |
3942 | |
3943 | |
3944 | struct ExceptionSpecInfo { |
3945 | |
3946 | ExceptionSpecificationType Type = EST_None; |
3947 | |
3948 | |
3949 | ArrayRef<QualType> Exceptions; |
3950 | |
3951 | |
3952 | Expr *NoexceptExpr = nullptr; |
3953 | |
3954 | |
3955 | |
3956 | FunctionDecl *SourceDecl = nullptr; |
3957 | |
3958 | |
3959 | |
3960 | FunctionDecl *SourceTemplate = nullptr; |
3961 | |
3962 | ExceptionSpecInfo() = default; |
3963 | |
3964 | ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} |
3965 | }; |
3966 | |
3967 | |
3968 | |
3969 | |
3970 | struct ExtProtoInfo { |
3971 | FunctionType::ExtInfo ExtInfo; |
3972 | bool Variadic : 1; |
3973 | bool HasTrailingReturn : 1; |
3974 | Qualifiers TypeQuals; |
3975 | RefQualifierKind RefQualifier = RQ_None; |
3976 | ExceptionSpecInfo ExceptionSpec; |
3977 | const ExtParameterInfo *ExtParameterInfos = nullptr; |
3978 | SourceLocation EllipsisLoc; |
3979 | |
3980 | ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} |
3981 | |
3982 | ExtProtoInfo(CallingConv CC) |
3983 | : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {} |
3984 | |
3985 | ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { |
3986 | ExtProtoInfo Result(*this); |
3987 | Result.ExceptionSpec = ESI; |
3988 | return Result; |
3989 | } |
3990 | }; |
3991 | |
3992 | private: |
3993 | unsigned numTrailingObjects(OverloadToken<QualType>) const { |
3994 | return getNumParams(); |
3995 | } |
3996 | |
3997 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { |
3998 | return isVariadic(); |
3999 | } |
4000 | |
4001 | unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { |
4002 | return hasExtraBitfields(); |
4003 | } |
4004 | |
4005 | unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { |
4006 | return getExceptionSpecSize().NumExceptionType; |
4007 | } |
4008 | |
4009 | unsigned numTrailingObjects(OverloadToken<Expr *>) const { |
4010 | return getExceptionSpecSize().NumExprPtr; |
4011 | } |
4012 | |
4013 | unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { |
4014 | return getExceptionSpecSize().NumFunctionDeclPtr; |
4015 | } |
4016 | |
4017 | unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { |
4018 | return hasExtParameterInfos() ? getNumParams() : 0; |
4019 | } |
4020 | |
4021 | |
4022 | |
4023 | static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, |
4024 | unsigned numArgs) { |
4025 | for (unsigned Idx = 0; Idx < numArgs; ++Idx) |
4026 | if (ArgArray[Idx]->containsUnexpandedParameterPack()) |
4027 | return true; |
4028 | |
4029 | return false; |
4030 | } |
4031 | |
4032 | FunctionProtoType(QualType result, ArrayRef<QualType> params, |
4033 | QualType canonical, const ExtProtoInfo &epi); |
4034 | |
4035 | |
4036 | |
4037 | |
4038 | struct ExceptionSpecSizeHolder { |
4039 | unsigned NumExceptionType; |
4040 | unsigned NumExprPtr; |
4041 | unsigned NumFunctionDeclPtr; |
4042 | }; |
4043 | |
4044 | |
4045 | |
4046 | static ExceptionSpecSizeHolder |
4047 | getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { |
4048 | switch (EST) { |
4049 | case EST_None: |
4050 | case EST_DynamicNone: |
4051 | case EST_MSAny: |
4052 | case EST_BasicNoexcept: |
4053 | case EST_Unparsed: |
4054 | case EST_NoThrow: |
4055 | return {0, 0, 0}; |
4056 | |
4057 | case EST_Dynamic: |
4058 | return {NumExceptions, 0, 0}; |
4059 | |
4060 | case EST_DependentNoexcept: |
4061 | case EST_NoexceptFalse: |
4062 | case EST_NoexceptTrue: |
4063 | return {0, 1, 0}; |
4064 | |
4065 | case EST_Uninstantiated: |
4066 | return {0, 0, 2}; |
4067 | |
4068 | case EST_Unevaluated: |
4069 | return {0, 0, 1}; |
4070 | } |
4071 | llvm_unreachable("bad exception specification kind"); |
4072 | } |
4073 | |
4074 | |
4075 | |
4076 | ExceptionSpecSizeHolder getExceptionSpecSize() const { |
4077 | return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); |
4078 | } |
4079 | |
4080 | |
4081 | static bool hasExtraBitfields(ExceptionSpecificationType EST) { |
4082 | |
4083 | |
4084 | return EST == EST_Dynamic; |
4085 | } |
4086 | |
4087 | |
4088 | bool hasExtraBitfields() const { |
4089 | return hasExtraBitfields(getExceptionSpecType()); |
4090 | } |
4091 | |
4092 | bool hasExtQualifiers() const { |
4093 | return FunctionTypeBits.HasExtQuals; |
4094 | } |
4095 | |
4096 | public: |
4097 | unsigned getNumParams() const { return FunctionTypeBits.NumParams; } |
4098 | |
4099 | QualType getParamType(unsigned i) const { |
4100 | assert(i < getNumParams() && "invalid parameter index"); |
4101 | return param_type_begin()[i]; |
4102 | } |
4103 | |
4104 | ArrayRef<QualType> getParamTypes() const { |
4105 | return llvm::makeArrayRef(param_type_begin(), param_type_end()); |
4106 | } |
4107 | |
4108 | ExtProtoInfo getExtProtoInfo() const { |
4109 | ExtProtoInfo EPI; |
4110 | EPI.ExtInfo = getExtInfo(); |
4111 | EPI.Variadic = isVariadic(); |
4112 | EPI.EllipsisLoc = getEllipsisLoc(); |
4113 | EPI.HasTrailingReturn = hasTrailingReturn(); |
4114 | EPI.ExceptionSpec = getExceptionSpecInfo(); |
4115 | EPI.TypeQuals = getMethodQuals(); |
4116 | EPI.RefQualifier = getRefQualifier(); |
4117 | EPI.ExtParameterInfos = getExtParameterInfosOrNull(); |
4118 | return EPI; |
4119 | } |
4120 | |
4121 | |
4122 | ExceptionSpecificationType getExceptionSpecType() const { |
4123 | return static_cast<ExceptionSpecificationType>( |
4124 | FunctionTypeBits.ExceptionSpecType); |
4125 | } |
4126 | |
4127 | |
4128 | bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } |
4129 | |
4130 | |
4131 | bool hasDynamicExceptionSpec() const { |
4132 | return isDynamicExceptionSpec(getExceptionSpecType()); |
4133 | } |
4134 | |
4135 | |
4136 | bool hasNoexceptExceptionSpec() const { |
4137 | return isNoexceptExceptionSpec(getExceptionSpecType()); |
4138 | } |
4139 | |
4140 | |
4141 | bool hasDependentExceptionSpec() const; |
4142 | |
4143 | |
4144 | |
4145 | bool hasInstantiationDependentExceptionSpec() const; |
4146 | |
4147 | |
4148 | ExceptionSpecInfo getExceptionSpecInfo() const { |
4149 | ExceptionSpecInfo Result; |
4150 | Result.Type = getExceptionSpecType(); |
4151 | if (Result.Type == EST_Dynamic) { |
4152 | Result.Exceptions = exceptions(); |
4153 | } else if (isComputedNoexcept(Result.Type)) { |
4154 | Result.NoexceptExpr = getNoexceptExpr(); |
4155 | } else if (Result.Type == EST_Uninstantiated) { |
4156 | Result.SourceDecl = getExceptionSpecDecl(); |
4157 | Result.SourceTemplate = getExceptionSpecTemplate(); |
4158 | } else if (Result.Type == EST_Unevaluated) { |
4159 | Result.SourceDecl = getExceptionSpecDecl(); |
4160 | } |
4161 | return Result; |
4162 | } |
4163 | |
4164 | |
4165 | unsigned getNumExceptions() const { |
4166 | return getExceptionSpecType() == EST_Dynamic |
4167 | ? getTrailingObjects<FunctionTypeExtraBitfields>() |
4168 | ->NumExceptionType |
4169 | : 0; |
4170 | } |
4171 | |
4172 | |
4173 | QualType getExceptionType(unsigned i) const { |
4174 | assert(i < getNumExceptions() && "Invalid exception number!"); |
4175 | return exception_begin()[i]; |
4176 | } |
4177 | |
4178 | |
4179 | |
4180 | Expr *getNoexceptExpr() const { |
4181 | if (!isComputedNoexcept(getExceptionSpecType())) |
4182 | return nullptr; |
4183 | return *getTrailingObjects<Expr *>(); |
4184 | } |
4185 | |
4186 | |
4187 | |
4188 | |
4189 | |
4190 | FunctionDecl *getExceptionSpecDecl() const { |
4191 | if (getExceptionSpecType() != EST_Uninstantiated && |
4192 | getExceptionSpecType() != EST_Unevaluated) |
4193 | return nullptr; |
4194 | return getTrailingObjects<FunctionDecl *>()[0]; |
4195 | } |
4196 | |
4197 | |
4198 | |
4199 | |
4200 | |
4201 | FunctionDecl *getExceptionSpecTemplate() const { |
4202 | if (getExceptionSpecType() != EST_Uninstantiated) |
4203 | return nullptr; |
4204 | return getTrailingObjects<FunctionDecl *>()[1]; |
4205 | } |
4206 | |
4207 | |
4208 | |
4209 | CanThrowResult canThrow() const; |
4210 | |
4211 | |
4212 | |
4213 | |
4214 | bool isNothrow(bool ResultIfDependent = false) const { |
4215 | return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; |
4216 | } |
4217 | |
4218 | |
4219 | bool isVariadic() const { return FunctionTypeBits.Variadic; } |
4220 | |
4221 | SourceLocation getEllipsisLoc() const { |
4222 | return isVariadic() ? *getTrailingObjects<SourceLocation>() |
4223 | : SourceLocation(); |
4224 | } |
4225 | |
4226 | |
4227 | |
4228 | |
4229 | |
4230 | |
4231 | |
4232 | bool isTemplateVariadic() const; |
4233 | |
4234 | |
4235 | bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } |
4236 | |
4237 | Qualifiers getMethodQuals() const { |
4238 | if (hasExtQualifiers()) |
4239 | return *getTrailingObjects<Qualifiers>(); |
4240 | else |
4241 | return getFastTypeQuals(); |
4242 | } |
4243 | |
4244 | |
4245 | RefQualifierKind getRefQualifier() const { |
4246 | return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); |
4247 | } |
4248 | |
4249 | using param_type_iterator = const QualType *; |
4250 | using param_type_range = llvm::iterator_range<param_type_iterator>; |
4251 | |
4252 | param_type_range param_types() const { |
4253 | return param_type_range(param_type_begin(), param_type_end()); |
4254 | } |
4255 | |
4256 | param_type_iterator param_type_begin() const { |
4257 | return getTrailingObjects<QualType>(); |
4258 | } |
4259 | |
4260 | param_type_iterator param_type_end() const { |
4261 | return param_type_begin() + getNumParams(); |
4262 | } |
4263 | |
4264 | using exception_iterator = const QualType *; |
4265 | |
4266 | ArrayRef<QualType> exceptions() const { |
4267 | return llvm::makeArrayRef(exception_begin(), exception_end()); |
4268 | } |
4269 | |
4270 | exception_iterator exception_begin() const { |
4271 | return reinterpret_cast<exception_iterator>( |
4272 | getTrailingObjects<ExceptionType>()); |
4273 | } |
4274 | |
4275 | exception_iterator exception_end() const { |
4276 | return exception_begin() + getNumExceptions(); |
4277 | } |
4278 | |
4279 | |
4280 | |
4281 | bool hasExtParameterInfos() const { |
4282 | return FunctionTypeBits.HasExtParameterInfos; |
4283 | } |
4284 | |
4285 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { |
4286 | assert(hasExtParameterInfos()); |
4287 | return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), |
4288 | getNumParams()); |
4289 | } |
4290 | |
4291 | |
4292 | |
4293 | |
4294 | const ExtParameterInfo *getExtParameterInfosOrNull() const { |
4295 | if (!hasExtParameterInfos()) |
4296 | return nullptr; |
4297 | return getTrailingObjects<ExtParameterInfo>(); |
4298 | } |
4299 | |
4300 | ExtParameterInfo getExtParameterInfo(unsigned I) const { |
4301 | assert(I < getNumParams() && "parameter index out of range"); |
4302 | if (hasExtParameterInfos()) |
4303 | return getTrailingObjects<ExtParameterInfo>()[I]; |
4304 | return ExtParameterInfo(); |
4305 | } |
4306 | |
4307 | ParameterABI getParameterABI(unsigned I) const { |
4308 | assert(I < getNumParams() && "parameter index out of range"); |
4309 | if (hasExtParameterInfos()) |
4310 | return getTrailingObjects<ExtParameterInfo>()[I].getABI(); |
4311 | return ParameterABI::Ordinary; |
4312 | } |
4313 | |
4314 | bool isParamConsumed(unsigned I) const { |
4315 | assert(I < getNumParams() && "parameter index out of range"); |
4316 | if (hasExtParameterInfos()) |
4317 | return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); |
4318 | return false; |
4319 | } |
4320 | |
4321 | bool isSugared() const { return false; } |
4322 | QualType desugar() const { return QualType(this, 0); } |
4323 | |
4324 | void printExceptionSpecification(raw_ostream &OS, |
4325 | const PrintingPolicy &Policy) const; |
4326 | |
4327 | static bool classof(const Type *T) { |
4328 | return T->getTypeClass() == FunctionProto; |
4329 | } |
4330 | |
4331 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); |
4332 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
4333 | param_type_iterator ArgTys, unsigned NumArgs, |
4334 | const ExtProtoInfo &EPI, const ASTContext &Context, |
4335 | bool Canonical); |
4336 | }; |
4337 | |
4338 | |
4339 | |
4340 | |
4341 | |
4342 | |
4343 | class UnresolvedUsingType : public Type { |
4344 | friend class ASTContext; |
4345 | |
4346 | UnresolvedUsingTypenameDecl *Decl; |
4347 | |
4348 | UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) |
4349 | : Type(UnresolvedUsing, QualType(), |
4350 | TypeDependence::DependentInstantiation), |
4351 | Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {} |
4352 | |
4353 | public: |
4354 | UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } |
4355 | |
4356 | bool isSugared() const { return false; } |
4357 | QualType desugar() const { return QualType(this, 0); } |
4358 | |
4359 | static bool classof(const Type *T) { |
4360 | return T->getTypeClass() == UnresolvedUsing; |
4361 | } |
4362 | |
4363 | void Profile(llvm::FoldingSetNodeID &ID) { |
4364 | return Profile(ID, Decl); |
4365 | } |
4366 | |
4367 | static void Profile(llvm::FoldingSetNodeID &ID, |
4368 | UnresolvedUsingTypenameDecl *D) { |
4369 | ID.AddPointer(D); |
4370 | } |
4371 | }; |
4372 | |
4373 | class TypedefType : public Type { |
4374 | TypedefNameDecl *Decl; |
4375 | |
4376 | private: |
4377 | friend class ASTContext; |
4378 | |
4379 | TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying, |
4380 | QualType can); |
4381 | |
4382 | public: |
4383 | TypedefNameDecl *getDecl() const { return Decl; } |
4384 | |
4385 | bool isSugared() const { return true; } |
4386 | QualType desugar() const; |
4387 | |
4388 | static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } |
4389 | }; |
4390 | |
4391 | |
4392 | |
4393 | class MacroQualifiedType : public Type { |
4394 | friend class ASTContext; |
4395 | |
4396 | QualType UnderlyingTy; |
4397 | const IdentifierInfo *MacroII; |
4398 | |
4399 | MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, |
4400 | const IdentifierInfo *MacroII) |
4401 | : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()), |
4402 | UnderlyingTy(UnderlyingTy), MacroII(MacroII) { |
4403 | assert(isa<AttributedType>(UnderlyingTy) && |
4404 | "Expected a macro qualified type to only wrap attributed types."); |
4405 | } |
4406 | |
4407 | public: |
4408 | const IdentifierInfo *getMacroIdentifier() const { return MacroII; } |
4409 | QualType getUnderlyingType() const { return UnderlyingTy; } |
4410 | |
4411 | |
4412 | |
4413 | QualType getModifiedType() const; |
4414 | |
4415 | bool isSugared() const { return true; } |
4416 | QualType desugar() const; |
4417 | |
4418 | static bool classof(const Type *T) { |
4419 | return T->getTypeClass() == MacroQualified; |
4420 | } |
4421 | }; |
4422 | |
4423 | |
4424 | class TypeOfExprType : public Type { |
4425 | Expr *TOExpr; |
4426 | |
4427 | protected: |
4428 | friend class ASTContext; |
4429 | |
4430 | TypeOfExprType(Expr *E, QualType can = QualType()); |
4431 | |
4432 | public: |
4433 | Expr *getUnderlyingExpr() const { return TOExpr; } |
4434 | |
4435 | |
4436 | QualType desugar() const; |
4437 | |
4438 | |
4439 | bool isSugared() const; |
4440 | |
4441 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } |
4442 | }; |
4443 | |
4444 | |
4445 | |
4446 | |
4447 | |
4448 | |
4449 | |
4450 | class DependentTypeOfExprType |
4451 | : public TypeOfExprType, public llvm::FoldingSetNode { |
4452 | const ASTContext &Context; |
4453 | |
4454 | public: |
4455 | DependentTypeOfExprType(const ASTContext &Context, Expr *E) |
4456 | : TypeOfExprType(E), Context(Context) {} |
4457 | |
4458 | void Profile(llvm::FoldingSetNodeID &ID) { |
4459 | Profile(ID, Context, getUnderlyingExpr()); |
4460 | } |
4461 | |
4462 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4463 | Expr *E); |
4464 | }; |
4465 | |
4466 | |
4467 | class TypeOfType : public Type { |
4468 | friend class ASTContext; |
4469 | |
4470 | QualType TOType; |
4471 | |
4472 | TypeOfType(QualType T, QualType can) |
4473 | : Type(TypeOf, can, T->getDependence()), TOType(T) { |
4474 | assert(!isa<TypedefType>(can) && "Invalid canonical type"); |
4475 | } |
4476 | |
4477 | public: |
4478 | QualType getUnderlyingType() const { return TOType; } |
4479 | |
4480 | |
4481 | QualType desugar() const { return getUnderlyingType(); } |
4482 | |
4483 | |
4484 | bool isSugared() const { return true; } |
4485 | |
4486 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } |
4487 | }; |
4488 | |
4489 | |
4490 | class DecltypeType : public Type { |
4491 | Expr *E; |
4492 | QualType UnderlyingType; |
4493 | |
4494 | protected: |
4495 | friend class ASTContext; |
4496 | |
4497 | DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); |
4498 | |
4499 | public: |
4500 | Expr *getUnderlyingExpr() const { return E; } |
4501 | QualType getUnderlyingType() const { return UnderlyingType; } |
4502 | |
4503 | |
4504 | QualType desugar() const; |
4505 | |
4506 | |
4507 | bool isSugared() const; |
4508 | |
4509 | static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } |
4510 | }; |
4511 | |
4512 | |
4513 | |
4514 | |
4515 | |
4516 | |
4517 | |
4518 | class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { |
4519 | const ASTContext &Context; |
4520 | |
4521 | public: |
4522 | DependentDecltypeType(const ASTContext &Context, Expr *E); |
4523 | |
4524 | void Profile(llvm::FoldingSetNodeID &ID) { |
4525 | Profile(ID, Context, getUnderlyingExpr()); |
4526 | } |
4527 | |
4528 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4529 | Expr *E); |
4530 | }; |
4531 | |
4532 | |
4533 | class UnaryTransformType : public Type { |
4534 | public: |
4535 | enum UTTKind { |
4536 | EnumUnderlyingType |
4537 | }; |
4538 | |
4539 | private: |
4540 | |
4541 | QualType BaseType; |
4542 | |
4543 | |
4544 | QualType UnderlyingType; |
4545 | |
4546 | UTTKind UKind; |
4547 | |
4548 | protected: |
4549 | friend class ASTContext; |
4550 | |
4551 | UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, |
4552 | QualType CanonicalTy); |
4553 | |
4554 | public: |
4555 | bool isSugared() const { return !isDependentType(); } |
4556 | QualType desugar() const { return UnderlyingType; } |
4557 | |
4558 | QualType getUnderlyingType() const { return UnderlyingType; } |
4559 | QualType getBaseType() const { return BaseType; } |
4560 | |
4561 | UTTKind getUTTKind() const { return UKind; } |
4562 | |
4563 | static bool classof(const Type *T) { |
4564 | return T->getTypeClass() == UnaryTransform; |
4565 | } |
4566 | }; |
4567 | |
4568 | |
4569 | |
4570 | |
4571 | |
4572 | |
4573 | |
4574 | class DependentUnaryTransformType : public UnaryTransformType, |
4575 | public llvm::FoldingSetNode { |
4576 | public: |
4577 | DependentUnaryTransformType(const ASTContext &C, QualType BaseType, |
4578 | UTTKind UKind); |
4579 | |
4580 | void Profile(llvm::FoldingSetNodeID &ID) { |
4581 | Profile(ID, getBaseType(), getUTTKind()); |
4582 | } |
4583 | |
4584 | static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, |
4585 | UTTKind UKind) { |
4586 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
4587 | ID.AddInteger((unsigned)UKind); |
4588 | } |
4589 | }; |
4590 | |
4591 | class TagType : public Type { |
4592 | friend class ASTReader; |
4593 | template <class T> friend class serialization::AbstractTypeReader; |
4594 | |
4595 | |
4596 | |
4597 | TagDecl *decl; |
4598 | |
4599 | protected: |
4600 | TagType(TypeClass TC, const TagDecl *D, QualType can); |
4601 | |
4602 | public: |
4603 | TagDecl *getDecl() const; |
4604 | |
4605 | |
4606 | bool isBeingDefined() const; |
4607 | |
4608 | static bool classof(const Type *T) { |
4609 | return T->getTypeClass() == Enum || T->getTypeClass() == Record; |
4610 | } |
4611 | }; |
4612 | |
4613 | |
4614 | |
4615 | class RecordType : public TagType { |
4616 | protected: |
4617 | friend class ASTContext; |
4618 | |
4619 | explicit RecordType(const RecordDecl *D) |
4620 | : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4621 | explicit RecordType(TypeClass TC, RecordDecl *D) |
4622 | : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4623 | |
4624 | public: |
4625 | RecordDecl *getDecl() const { |
4626 | return reinterpret_cast<RecordDecl*>(TagType::getDecl()); |
4627 | } |
4628 | |
4629 | |
4630 | |
4631 | bool hasConstFields() const; |
4632 | |
4633 | bool isSugared() const { return false; } |
4634 | QualType desugar() const { return QualType(this, 0); } |
4635 | |
4636 | static bool classof(const Type *T) { return T->getTypeClass() == Record; } |
4637 | }; |
4638 | |
4639 | |
4640 | |
4641 | class EnumType : public TagType { |
4642 | friend class ASTContext; |
4643 | |
4644 | explicit EnumType(const EnumDecl *D) |
4645 | : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4646 | |
4647 | public: |
4648 | EnumDecl *getDecl() const { |
4649 | return reinterpret_cast<EnumDecl*>(TagType::getDecl()); |
4650 | } |
4651 | |
4652 | bool isSugared() const { return false; } |
4653 | QualType desugar() const { return QualType(this, 0); } |
4654 | |
4655 | static bool classof(const Type *T) { return T->getTypeClass() == Enum; } |
4656 | }; |
4657 | |
4658 | |
4659 | |
4660 | |
4661 | |
4662 | |
4663 | |
4664 | |
4665 | |
4666 | |
4667 | |
4668 | |
4669 | |
4670 | class AttributedType : public Type, public llvm::FoldingSetNode { |
4671 | public: |
4672 | using Kind = attr::Kind; |
4673 | |
4674 | private: |
4675 | friend class ASTContext; |
4676 | |
4677 | QualType ModifiedType; |
4678 | QualType EquivalentType; |
4679 | |
4680 | AttributedType(QualType canon, attr::Kind attrKind, QualType modified, |
4681 | QualType equivalent) |
4682 | : Type(Attributed, canon, equivalent->getDependence()), |
4683 | ModifiedType(modified), EquivalentType(equivalent) { |
4684 | AttributedTypeBits.AttrKind = attrKind; |
4685 | } |
4686 | |
4687 | public: |
4688 | Kind getAttrKind() const { |
4689 | return static_cast<Kind>(AttributedTypeBits.AttrKind); |
4690 | } |
4691 | |
4692 | QualType getModifiedType() const { return ModifiedType; } |
4693 | QualType getEquivalentType() const { return EquivalentType; } |
4694 | |
4695 | bool isSugared() const { return true; } |
4696 | QualType desugar() const { return getEquivalentType(); } |
4697 | |
4698 | |
4699 | |
4700 | |
4701 | |
4702 | |
4703 | |
4704 | |
4705 | |
4706 | |
4707 | |
4708 | |
4709 | |
4710 | |
4711 | |
4712 | |
4713 | bool isQualifier() const; |
4714 | |
4715 | bool isMSTypeSpec() const; |
4716 | |
4717 | bool isCallingConv() const; |
4718 | |
4719 | llvm::Optional<NullabilityKind> getImmediateNullability() const; |
4720 | |
4721 | |
4722 | |
4723 | static Kind getNullabilityAttrKind(NullabilityKind kind) { |
4724 | switch (kind) { |
4725 | case NullabilityKind::NonNull: |
4726 | return attr::TypeNonNull; |
4727 | |
4728 | case NullabilityKind::Nullable: |
4729 | return attr::TypeNullable; |
4730 | |
4731 | case NullabilityKind::NullableResult: |
4732 | return attr::TypeNullableResult; |
4733 | |
4734 | case NullabilityKind::Unspecified: |
4735 | return attr::TypeNullUnspecified; |
4736 | } |
4737 | llvm_unreachable("Unknown nullability kind."); |
4738 | } |
4739 | |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | |
4747 | |
4748 | |
4749 | static Optional<NullabilityKind> stripOuterNullability(QualType &T); |
4750 | |
4751 | void Profile(llvm::FoldingSetNodeID &ID) { |
4752 | Profile(ID, getAttrKind(), ModifiedType, EquivalentType); |
4753 | } |
4754 | |
4755 | static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, |
4756 | QualType modified, QualType equivalent) { |
4757 | ID.AddInteger(attrKind); |
4758 | ID.AddPointer(modified.getAsOpaquePtr()); |
4759 | ID.AddPointer(equivalent.getAsOpaquePtr()); |
4760 | } |
4761 | |
4762 | static bool classof(const Type *T) { |
4763 | return T->getTypeClass() == Attributed; |
4764 | } |
4765 | }; |
4766 | |
4767 | class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4768 | friend class ASTContext; |
4769 | |
4770 | |
4771 | struct CanonicalTTPTInfo { |
4772 | unsigned Depth : 15; |
4773 | unsigned ParameterPack : 1; |
4774 | unsigned Index : 16; |
4775 | }; |
4776 | |
4777 | union { |
4778 | |
4779 | CanonicalTTPTInfo CanTTPTInfo; |
4780 | |
4781 | |
4782 | TemplateTypeParmDecl *TTPDecl; |
4783 | }; |
4784 | |
4785 | |
4786 | TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) |
4787 | : Type(TemplateTypeParm, Canon, |
4788 | TypeDependence::DependentInstantiation | |
4789 | (Canon->getDependence() & TypeDependence::UnexpandedPack)), |
4790 | TTPDecl(TTPDecl) {} |
4791 | |
4792 | |
4793 | TemplateTypeParmType(unsigned D, unsigned I, bool PP) |
4794 | : Type(TemplateTypeParm, QualType(this, 0), |
4795 | TypeDependence::DependentInstantiation | |
4796 | (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) { |
4797 | CanTTPTInfo.Depth = D; |
4798 | CanTTPTInfo.Index = I; |
4799 | CanTTPTInfo.ParameterPack = PP; |
4800 | } |
4801 | |
4802 | const CanonicalTTPTInfo& getCanTTPTInfo() const { |
4803 | QualType Can = getCanonicalTypeInternal(); |
4804 | return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; |
4805 | } |
4806 | |
4807 | public: |
4808 | unsigned getDepth() const { return getCanTTPTInfo().Depth; } |
4809 | unsigned getIndex() const { return getCanTTPTInfo().Index; } |
4810 | bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } |
4811 | |
4812 | TemplateTypeParmDecl *getDecl() const { |
4813 | return isCanonicalUnqualified() ? nullptr : TTPDecl; |
4814 | } |
4815 | |
4816 | IdentifierInfo *getIdentifier() const; |
4817 | |
4818 | bool isSugared() const { return false; } |
4819 | QualType desugar() const { return QualType(this, 0); } |
4820 | |
4821 | void Profile(llvm::FoldingSetNodeID &ID) { |
4822 | Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); |
4823 | } |
4824 | |
4825 | static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, |
4826 | unsigned Index, bool ParameterPack, |
4827 | TemplateTypeParmDecl *TTPDecl) { |
4828 | ID.AddInteger(Depth); |
4829 | ID.AddInteger(Index); |
4830 | ID.AddBoolean(ParameterPack); |
4831 | ID.AddPointer(TTPDecl); |
4832 | } |
4833 | |
4834 | static bool classof(const Type *T) { |
4835 | return T->getTypeClass() == TemplateTypeParm; |
4836 | } |
4837 | }; |
4838 | |
4839 | |
4840 | |
4841 | |
4842 | |
4843 | |
4844 | |
4845 | |
4846 | class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4847 | friend class ASTContext; |
4848 | |
4849 | |
4850 | const TemplateTypeParmType *Replaced; |
4851 | |
4852 | SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) |
4853 | : Type(SubstTemplateTypeParm, Canon, Canon->getDependence()), |
4854 | Replaced(Param) {} |
4855 | |
4856 | public: |
4857 | |
4858 | const TemplateTypeParmType *getReplacedParameter() const { |
4859 | return Replaced; |
4860 | } |
4861 | |
4862 | |
4863 | |
4864 | QualType getReplacementType() const { |
4865 | return getCanonicalTypeInternal(); |
4866 | } |
4867 | |
4868 | bool isSugared() const { return true; } |
4869 | QualType desugar() const { return getReplacementType(); } |
4870 | |
4871 | void Profile(llvm::FoldingSetNodeID &ID) { |
4872 | Profile(ID, getReplacedParameter(), getReplacementType()); |
4873 | } |
4874 | |
4875 | static void Profile(llvm::FoldingSetNodeID &ID, |
4876 | const TemplateTypeParmType *Replaced, |
4877 | QualType Replacement) { |
4878 | ID.AddPointer(Replaced); |
4879 | ID.AddPointer(Replacement.getAsOpaquePtr()); |
4880 | } |
4881 | |
4882 | static bool classof(const Type *T) { |
4883 | return T->getTypeClass() == SubstTemplateTypeParm; |
4884 | } |
4885 | }; |
4886 | |
4887 | |
4888 | |
4889 | |
4890 | |
4891 | |
4892 | |
4893 | |
4894 | |
4895 | |
4896 | |
4897 | |
4898 | |
4899 | class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { |
4900 | friend class ASTContext; |
4901 | |
4902 | |
4903 | const TemplateTypeParmType *Replaced; |
4904 | |
4905 | |
4906 | |
4907 | const TemplateArgument *Arguments; |
4908 | |
4909 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, |
4910 | QualType Canon, |
4911 | const TemplateArgument &ArgPack); |
4912 | |
4913 | public: |
4914 | IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } |
4915 | |
4916 | |
4917 | const TemplateTypeParmType *getReplacedParameter() const { |
4918 | return Replaced; |
4919 | } |
4920 | |
4921 | unsigned getNumArgs() const { |
4922 | return SubstTemplateTypeParmPackTypeBits.NumArgs; |
4923 | } |
4924 | |
4925 | bool isSugared() const { return false; } |
4926 | QualType desugar() const { return QualType(this, 0); } |
4927 | |
4928 | TemplateArgument getArgumentPack() const; |
4929 | |
4930 | void Profile(llvm::FoldingSetNodeID &ID); |
4931 | static void Profile(llvm::FoldingSetNodeID &ID, |
4932 | const TemplateTypeParmType *Replaced, |
4933 | const TemplateArgument &ArgPack); |
4934 | |
4935 | static bool classof(const Type *T) { |
4936 | return T->getTypeClass() == SubstTemplateTypeParmPack; |
4937 | } |
4938 | }; |
4939 | |
4940 | |
4941 | |
4942 | |
4943 | |
4944 | |
4945 | |
4946 | |
4947 | |
4948 | class DeducedType : public Type { |
4949 | protected: |
4950 | DeducedType(TypeClass TC, QualType DeducedAsType, |
4951 | TypeDependence ExtraDependence) |
4952 | : Type(TC, |
4953 | |
4954 | DeducedAsType.isNull() ? QualType(this, 0) |
4955 | : DeducedAsType.getCanonicalType(), |
4956 | ExtraDependence | (DeducedAsType.isNull() |
4957 | ? TypeDependence::None |
4958 | : DeducedAsType->getDependence() & |
4959 | ~TypeDependence::VariablyModified)) {} |
4960 | |
4961 | public: |
4962 | bool isSugared() const { return !isCanonicalUnqualified(); } |
4963 | QualType desugar() const { return getCanonicalTypeInternal(); } |
4964 | |
4965 | |
4966 | |
4967 | QualType getDeducedType() const { |
4968 | return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType(); |
4969 | } |
4970 | bool isDeduced() const { |
4971 | return !isCanonicalUnqualified() || isDependentType(); |
4972 | } |
4973 | |
4974 | static bool classof(const Type *T) { |
4975 | return T->getTypeClass() == Auto || |
4976 | T->getTypeClass() == DeducedTemplateSpecialization; |
4977 | } |
4978 | }; |
4979 | |
4980 | |
4981 | |
4982 | class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode { |
4983 | friend class ASTContext; |
4984 | |
4985 | ConceptDecl *TypeConstraintConcept; |
4986 | |
4987 | AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, |
4988 | TypeDependence ExtraDependence, ConceptDecl *CD, |
4989 | ArrayRef<TemplateArgument> TypeConstraintArgs); |
4990 | |
4991 | const TemplateArgument *getArgBuffer() const { |
4992 | return reinterpret_cast<const TemplateArgument*>(this+1); |
4993 | } |
4994 | |
4995 | TemplateArgument *getArgBuffer() { |
4996 | return reinterpret_cast<TemplateArgument*>(this+1); |
4997 | } |
4998 | |
4999 | public: |
5000 | |
5001 | const TemplateArgument *getArgs() const { |
5002 | return getArgBuffer(); |
5003 | } |
5004 | |
5005 | |
5006 | unsigned getNumArgs() const { |
5007 | return AutoTypeBits.NumArgs; |
5008 | } |
5009 | |
5010 | const TemplateArgument &getArg(unsigned Idx) const; |
5011 | |
5012 | ArrayRef<TemplateArgument> getTypeConstraintArguments() const { |
5013 | return {getArgs(), getNumArgs()}; |
5014 | } |
5015 | |
5016 | ConceptDecl *getTypeConstraintConcept() const { |
5017 | return TypeConstraintConcept; |
5018 | } |
5019 | |
5020 | bool isConstrained() const { |
5021 | return TypeConstraintConcept != nullptr; |
5022 | } |
5023 | |
5024 | bool isDecltypeAuto() const { |
5025 | return getKeyword() == AutoTypeKeyword::DecltypeAuto; |
5026 | } |
5027 | |
5028 | AutoTypeKeyword getKeyword() const { |
5029 | return (AutoTypeKeyword)AutoTypeBits.Keyword; |
5030 | } |
5031 | |
5032 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5033 | Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(), |
5034 | getTypeConstraintConcept(), getTypeConstraintArguments()); |
5035 | } |
5036 | |
5037 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
5038 | QualType Deduced, AutoTypeKeyword Keyword, |
5039 | bool IsDependent, ConceptDecl *CD, |
5040 | ArrayRef<TemplateArgument> Arguments); |
5041 | |
5042 | static bool classof(const Type *T) { |
5043 | return T->getTypeClass() == Auto; |
5044 | } |
5045 | }; |
5046 | |
5047 | |
5048 | class DeducedTemplateSpecializationType : public DeducedType, |
5049 | public llvm::FoldingSetNode { |
5050 | friend class ASTContext; |
5051 | |
5052 | |
5053 | TemplateName Template; |
5054 | |
5055 | DeducedTemplateSpecializationType(TemplateName Template, |
5056 | QualType DeducedAsType, |
5057 | bool IsDeducedAsDependent) |
5058 | : DeducedType(DeducedTemplateSpecialization, DeducedAsType, |
5059 | toTypeDependence(Template.getDependence()) | |
5060 | (IsDeducedAsDependent |
5061 | ? TypeDependence::DependentInstantiation |
5062 | : TypeDependence::None)), |
5063 | Template(Template) {} |
5064 | |
5065 | public: |
5066 | |
5067 | TemplateName getTemplateName() const { return Template;} |
5068 | |
5069 | void Profile(llvm::FoldingSetNodeID &ID) { |
5070 | Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); |
5071 | } |
5072 | |
5073 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, |
5074 | QualType Deduced, bool IsDependent) { |
5075 | Template.Profile(ID); |
5076 | ID.AddPointer(Deduced.getAsOpaquePtr()); |
5077 | ID.AddBoolean(IsDependent); |
5078 | } |
5079 | |
5080 | static bool classof(const Type *T) { |
5081 | return T->getTypeClass() == DeducedTemplateSpecialization; |
5082 | } |
5083 | }; |
5084 | |
5085 | |
5086 | |
5087 | |
5088 | |
5089 | |
5090 | |
5091 | |
5092 | |
5093 | |
5094 | |
5095 | |
5096 | |
5097 | |
5098 | |
5099 | |
5100 | |
5101 | |
5102 | |
5103 | |
5104 | |
5105 | class alignas(8) TemplateSpecializationType |
5106 | : public Type, |
5107 | public llvm::FoldingSetNode { |
5108 | friend class ASTContext; |
5109 | |
5110 | |
5111 | |
5112 | |
5113 | |
5114 | |
5115 | |
5116 | |
5117 | TemplateName Template; |
5118 | |
5119 | TemplateSpecializationType(TemplateName T, |
5120 | ArrayRef<TemplateArgument> Args, |
5121 | QualType Canon, |
5122 | QualType Aliased); |
5123 | |
5124 | public: |
5125 | |
5126 | |
5127 | |
5128 | |
5129 | |
5130 | |
5131 | |
5132 | |
5133 | |
5134 | |
5135 | |
5136 | static bool |
5137 | anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, |
5138 | ArrayRef<TemplateArgument> Converted); |
5139 | static bool |
5140 | anyDependentTemplateArguments(const TemplateArgumentListInfo &, |
5141 | ArrayRef<TemplateArgument> Converted); |
5142 | static bool anyInstantiationDependentTemplateArguments( |
5143 | ArrayRef<TemplateArgumentLoc> Args); |
5144 | |
5145 | |
5146 | |
5147 | bool isCurrentInstantiation() const { |
5148 | return isa<InjectedClassNameType>(getCanonicalTypeInternal()); |
5149 | } |
5150 | |
5151 | |
5152 | |
5153 | |
5154 | |
5155 | |
5156 | |
5157 | |
5158 | |
5159 | |
5160 | |
5161 | |
5162 | |
5163 | |
5164 | |
5165 | |
5166 | bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } |
5167 | |
5168 | |
5169 | |
5170 | QualType getAliasedType() const { |
5171 | assert(isTypeAlias() && "not a type alias template specialization"); |
5172 | return *reinterpret_cast<const QualType*>(end()); |
5173 | } |
5174 | |
5175 | using iterator = const TemplateArgument *; |
5176 | |
5177 | iterator begin() const { return getArgs(); } |
5178 | iterator end() const; |
5179 | |
5180 | |
5181 | TemplateName getTemplateName() const { return Template; } |
5182 | |
5183 | |
5184 | const TemplateArgument *getArgs() const { |
5185 | return reinterpret_cast<const TemplateArgument *>(this + 1); |
5186 | } |
5187 | |
5188 | |
5189 | unsigned getNumArgs() const { |
5190 | return TemplateSpecializationTypeBits.NumArgs; |
5191 | } |
5192 | |
5193 | |
5194 | |
5195 | const TemplateArgument &getArg(unsigned Idx) const; |
5196 | |
5197 | ArrayRef<TemplateArgument> template_arguments() const { |
5198 | return {getArgs(), getNumArgs()}; |
5199 | } |
5200 | |
5201 | bool isSugared() const { |
5202 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); |
5203 | } |
5204 | |
5205 | QualType desugar() const { |
5206 | return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); |
5207 | } |
5208 | |
5209 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
5210 | Profile(ID, Template, template_arguments(), Ctx); |
5211 | if (isTypeAlias()) |
5212 | getAliasedType().Profile(ID); |
5213 | } |
5214 | |
5215 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, |
5216 | ArrayRef<TemplateArgument> Args, |
5217 | const ASTContext &Context); |
5218 | |
5219 | static bool classof(const Type *T) { |
5220 | return T->getTypeClass() == TemplateSpecialization; |
5221 | } |
5222 | }; |
5223 | |
5224 | |
5225 | |
5226 | void printTemplateArgumentList(raw_ostream &OS, |
5227 | ArrayRef<TemplateArgument> Args, |
5228 | const PrintingPolicy &Policy, |
5229 | const TemplateParameterList *TPL = nullptr); |
5230 | |
5231 | void printTemplateArgumentList(raw_ostream &OS, |
5232 | ArrayRef<TemplateArgumentLoc> Args, |
5233 | const PrintingPolicy &Policy, |
5234 | const TemplateParameterList *TPL = nullptr); |
5235 | |
5236 | void printTemplateArgumentList(raw_ostream &OS, |
5237 | const TemplateArgumentListInfo &Args, |
5238 | const PrintingPolicy &Policy, |
5239 | const TemplateParameterList *TPL = nullptr); |
5240 | |
5241 | |
5242 | |
5243 | |
5244 | |
5245 | |
5246 | |
5247 | |
5248 | |
5249 | |
5250 | |
5251 | |
5252 | |
5253 | |
5254 | |
5255 | |
5256 | |
5257 | |
5258 | class InjectedClassNameType : public Type { |
5259 | friend class ASTContext; |
5260 | friend class ASTNodeImporter; |
5261 | friend class ASTReader; |
5262 | |
5263 | |
5264 | template <class T> friend class serialization::AbstractTypeReader; |
5265 | |
5266 | CXXRecordDecl *Decl; |
5267 | |
5268 | |
5269 | |
5270 | |
5271 | |
5272 | |
5273 | |
5274 | |
5275 | |
5276 | |
5277 | QualType InjectedType; |
5278 | |
5279 | InjectedClassNameType(CXXRecordDecl *D, QualType TST) |
5280 | : Type(InjectedClassName, QualType(), |
5281 | TypeDependence::DependentInstantiation), |
5282 | Decl(D), InjectedType(TST) { |
5283 | assert(isa<TemplateSpecializationType>(TST)); |
5284 | assert(!TST.hasQualifiers()); |
5285 | assert(TST->isDependentType()); |
5286 | } |
5287 | |
5288 | public: |
5289 | QualType getInjectedSpecializationType() const { return InjectedType; } |
5290 | |
5291 | const TemplateSpecializationType *getInjectedTST() const { |
5292 | return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); |
5293 | } |
5294 | |
5295 | TemplateName getTemplateName() const { |
5296 | return getInjectedTST()->getTemplateName(); |
5297 | } |
5298 | |
5299 | CXXRecordDecl *getDecl() const; |
5300 | |
5301 | bool isSugared() const { return false; } |
5302 | QualType desugar() const { return QualType(this, 0); } |
5303 | |
5304 | static bool classof(const Type *T) { |
5305 | return T->getTypeClass() == InjectedClassName; |
5306 | } |
5307 | }; |
5308 | |
5309 | |
5310 | enum TagTypeKind { |
5311 | |
5312 | TTK_Struct, |
5313 | |
5314 | |
5315 | TTK_Interface, |
5316 | |
5317 | |
5318 | TTK_Union, |
5319 | |
5320 | |
5321 | TTK_Class, |
5322 | |
5323 | |
5324 | TTK_Enum |
5325 | }; |
5326 | |
5327 | |
5328 | |
5329 | enum ElaboratedTypeKeyword { |
5330 | |
5331 | ETK_Struct, |
5332 | |
5333 | |
5334 | ETK_Interface, |
5335 | |
5336 | |
5337 | ETK_Union, |
5338 | |
5339 | |
5340 | ETK_Class, |
5341 | |
5342 | |
5343 | ETK_Enum, |
5344 | |
5345 | |
5346 | |
5347 | ETK_Typename, |
5348 | |
5349 | |
5350 | ETK_None |
5351 | }; |
5352 | |
5353 | |
5354 | |
5355 | |
5356 | |
5357 | class TypeWithKeyword : public Type { |
5358 | protected: |
5359 | TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, |
5360 | QualType Canonical, TypeDependence Dependence) |
5361 | : Type(tc, Canonical, Dependence) { |
5362 | TypeWithKeywordBits.Keyword = Keyword; |
5363 | } |
5364 | |
5365 | public: |
5366 | ElaboratedTypeKeyword getKeyword() const { |
5367 | return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); |
5368 | } |
5369 | |
5370 | |
5371 | static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); |
5372 | |
5373 | |
5374 | |
5375 | static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); |
5376 | |
5377 | |
5378 | static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); |
5379 | |
5380 | |
5381 | |
5382 | |
5383 | static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); |
5384 | |
5385 | static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); |
5386 | |
5387 | static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); |
5388 | |
5389 | static StringRef getTagTypeKindName(TagTypeKind Kind) { |
5390 | return getKeywordName(getKeywordForTagTypeKind(Kind)); |
5391 | } |
5392 | |
5393 | class CannotCastToThisType {}; |
5394 | static CannotCastToThisType classof(const Type *); |
5395 | }; |
5396 | |
5397 | |
5398 | |
5399 | |
5400 | |
5401 | |
5402 | |
5403 | |
5404 | |
5405 | class ElaboratedType final |
5406 | : public TypeWithKeyword, |
5407 | public llvm::FoldingSetNode, |
5408 | private llvm::TrailingObjects<ElaboratedType, TagDecl *> { |
5409 | friend class ASTContext; |
5410 | friend TrailingObjects; |
5411 | |
5412 | |
5413 | NestedNameSpecifier *NNS; |
5414 | |
5415 | |
5416 | QualType NamedType; |
5417 | |
5418 | |
5419 | |
5420 | |
5421 | |
5422 | ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5423 | QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) |
5424 | : TypeWithKeyword(Keyword, Elaborated, CanonType, |
5425 | |
5426 | |
5427 | |
5428 | |
5429 | NamedType->getDependence() | |
5430 | (NNS ? toSyntacticDependence( |
5431 | toTypeDependence(NNS->getDependence())) |
5432 | : TypeDependence::None)), |
5433 | NNS(NNS), NamedType(NamedType) { |
5434 | ElaboratedTypeBits.HasOwnedTagDecl = false; |
5435 | if (OwnedTagDecl) { |
5436 | ElaboratedTypeBits.HasOwnedTagDecl = true; |
5437 | *getTrailingObjects<TagDecl *>() = OwnedTagDecl; |
5438 | } |
5439 | assert(!(Keyword == ETK_None && NNS == nullptr) && |
5440 | "ElaboratedType cannot have elaborated type keyword " |
5441 | "and name qualifier both null."); |
5442 | } |
5443 | |
5444 | public: |
5445 | |
5446 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5447 | |
5448 | |
5449 | QualType getNamedType() const { return NamedType; } |
5450 | |
5451 | |
5452 | QualType desugar() const { return getNamedType(); } |
5453 | |
5454 | |
5455 | bool isSugared() const { return true; } |
5456 | |
5457 | |
5458 | |
5459 | TagDecl *getOwnedTagDecl() const { |
5460 | return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() |
5461 | : nullptr; |
5462 | } |
5463 | |
5464 | void Profile(llvm::FoldingSetNodeID &ID) { |
5465 | Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); |
5466 | } |
5467 | |
5468 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5469 | NestedNameSpecifier *NNS, QualType NamedType, |
5470 | TagDecl *OwnedTagDecl) { |
5471 | ID.AddInteger(Keyword); |
5472 | ID.AddPointer(NNS); |
5473 | NamedType.Profile(ID); |
5474 | ID.AddPointer(OwnedTagDecl); |
5475 | } |
5476 | |
5477 | static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } |
5478 | }; |
5479 | |
5480 | |
5481 | |
5482 | |
5483 | |
5484 | |
5485 | |
5486 | |
5487 | |
5488 | |
5489 | |
5490 | |
5491 | |
5492 | class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { |
5493 | friend class ASTContext; |
5494 | |
5495 | |
5496 | NestedNameSpecifier *NNS; |
5497 | |
5498 | |
5499 | const IdentifierInfo *Name; |
5500 | |
5501 | DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5502 | const IdentifierInfo *Name, QualType CanonType) |
5503 | : TypeWithKeyword(Keyword, DependentName, CanonType, |
5504 | TypeDependence::DependentInstantiation | |
5505 | toTypeDependence(NNS->getDependence())), |
5506 | NNS(NNS), Name(Name) {} |
5507 | |
5508 | public: |
5509 | |
5510 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5511 | |
5512 | |
5513 | |
5514 | |
5515 | |
5516 | |
5517 | const IdentifierInfo *getIdentifier() const { |
5518 | return Name; |
5519 | } |
5520 | |
5521 | bool isSugared() const { return false; } |
5522 | QualType desugar() const { return QualType(this, 0); } |
5523 | |
5524 | void Profile(llvm::FoldingSetNodeID &ID) { |
5525 | Profile(ID, getKeyword(), NNS, Name); |
5526 | } |
5527 | |
5528 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5529 | NestedNameSpecifier *NNS, const IdentifierInfo *Name) { |
5530 | ID.AddInteger(Keyword); |
5531 | ID.AddPointer(NNS); |
5532 | ID.AddPointer(Name); |
5533 | } |
5534 | |
5535 | static bool classof(const Type *T) { |
5536 | return T->getTypeClass() == DependentName; |
5537 | } |
5538 | }; |
5539 | |
5540 | |
5541 | |
5542 | |
5543 | class alignas(8) DependentTemplateSpecializationType |
5544 | : public TypeWithKeyword, |
5545 | public llvm::FoldingSetNode { |
5546 | friend class ASTContext; |
5547 | |
5548 | |
5549 | NestedNameSpecifier *NNS; |
5550 | |
5551 | |
5552 | const IdentifierInfo *Name; |
5553 | |
5554 | DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, |
5555 | NestedNameSpecifier *NNS, |
5556 | const IdentifierInfo *Name, |
5557 | ArrayRef<TemplateArgument> Args, |
5558 | QualType Canon); |
5559 | |
5560 | const TemplateArgument *getArgBuffer() const { |
5561 | return reinterpret_cast<const TemplateArgument*>(this+1); |
5562 | } |
5563 | |
5564 | TemplateArgument *getArgBuffer() { |
5565 | return reinterpret_cast<TemplateArgument*>(this+1); |
5566 | } |
5567 | |
5568 | public: |
5569 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5570 | const IdentifierInfo *getIdentifier() const { return Name; } |
5571 | |
5572 | |
5573 | const TemplateArgument *getArgs() const { |
5574 | return getArgBuffer(); |
5575 | } |
5576 | |
5577 | |
5578 | unsigned getNumArgs() const { |
5579 | return DependentTemplateSpecializationTypeBits.NumArgs; |
5580 | } |
5581 | |
5582 | const TemplateArgument &getArg(unsigned Idx) const; |
5583 | |
5584 | ArrayRef<TemplateArgument> template_arguments() const { |
5585 | return {getArgs(), getNumArgs()}; |
5586 | } |
5587 | |
5588 | using iterator = const TemplateArgument *; |
5589 | |
5590 | iterator begin() const { return getArgs(); } |
5591 | iterator end() const; |
5592 | |
5593 | bool isSugared() const { return false; } |
5594 | QualType desugar() const { return QualType(this, 0); } |
5595 | |
5596 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5597 | Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()}); |
5598 | } |
5599 | |
5600 | static void Profile(llvm::FoldingSetNodeID &ID, |
5601 | const ASTContext &Context, |
5602 | ElaboratedTypeKeyword Keyword, |
5603 | NestedNameSpecifier *Qualifier, |
5604 | const IdentifierInfo *Name, |
5605 | ArrayRef<TemplateArgument> Args); |
5606 | |
5607 | static bool classof(const Type *T) { |
5608 | return T->getTypeClass() == DependentTemplateSpecialization; |
5609 | } |
5610 | }; |
5611 | |
5612 | |
5613 | |
5614 | |
5615 | |
5616 | |
5617 | |
5618 | |
5619 | |
5620 | |
5621 | |
5622 | |
5623 | |
5624 | |
5625 | |
5626 | |
5627 | |
5628 | |
5629 | |
5630 | |
5631 | |
5632 | |
5633 | |
5634 | class PackExpansionType : public Type, public llvm::FoldingSetNode { |
5635 | friend class ASTContext; |
5636 | |
5637 | |
5638 | QualType Pattern; |
5639 | |
5640 | PackExpansionType(QualType Pattern, QualType Canon, |
5641 | Optional<unsigned> NumExpansions) |
5642 | : Type(PackExpansion, Canon, |
5643 | (Pattern->getDependence() | TypeDependence::Dependent | |
5644 | TypeDependence::Instantiation) & |
5645 | ~TypeDependence::UnexpandedPack), |
5646 | Pattern(Pattern) { |
5647 | PackExpansionTypeBits.NumExpansions = |
5648 | NumExpansions ? *NumExpansions + 1 : 0; |
5649 | } |
5650 | |
5651 | public: |
5652 | |
5653 | |
5654 | |
5655 | QualType getPattern() const { return Pattern; } |
5656 | |
5657 | |
5658 | |
5659 | Optional<unsigned> getNumExpansions() const { |
5660 | if (PackExpansionTypeBits.NumExpansions) |
5661 | return PackExpansionTypeBits.NumExpansions - 1; |
5662 | return None; |
5663 | } |
5664 | |
5665 | bool isSugared() const { return false; } |
5666 | QualType desugar() const { return QualType(this, 0); } |
5667 | |
5668 | void Profile(llvm::FoldingSetNodeID &ID) { |
5669 | Profile(ID, getPattern(), getNumExpansions()); |
5670 | } |
5671 | |
5672 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, |
5673 | Optional<unsigned> NumExpansions) { |
5674 | ID.AddPointer(Pattern.getAsOpaquePtr()); |
5675 | ID.AddBoolean(NumExpansions.hasValue()); |
5676 | if (NumExpansions) |
5677 | ID.AddInteger(*NumExpansions); |
5678 | } |
5679 | |
5680 | static bool classof(const Type *T) { |
5681 | return T->getTypeClass() == PackExpansion; |
5682 | } |
5683 | }; |
5684 | |
5685 | |
5686 | |
5687 | template <class T> |
5688 | class ObjCProtocolQualifiers { |
5689 | protected: |
5690 | ObjCProtocolQualifiers() = default; |
5691 | |
5692 | ObjCProtocolDecl * const *getProtocolStorage() const { |
5693 | return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); |
5694 | } |
5695 | |
5696 | ObjCProtocolDecl **getProtocolStorage() { |
5697 | return static_cast<T*>(this)->getProtocolStorageImpl(); |
5698 | } |
5699 | |
5700 | void setNumProtocols(unsigned N) { |
5701 | static_cast<T*>(this)->setNumProtocolsImpl(N); |
5702 | } |
5703 | |
5704 | void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { |
5705 | setNumProtocols(protocols.size()); |
5706 | assert(getNumProtocols() == protocols.size() && |
5707 | "bitfield overflow in protocol count"); |
5708 | if (!protocols.empty()) |
5709 | memcpy(getProtocolStorage(), protocols.data(), |
5710 | protocols.size() * sizeof(ObjCProtocolDecl*)); |
5711 | } |
5712 | |
5713 | public: |
5714 | using qual_iterator = ObjCProtocolDecl * const *; |
5715 | using qual_range = llvm::iterator_range<qual_iterator>; |
5716 | |
5717 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
5718 | qual_iterator qual_begin() const { return getProtocolStorage(); } |
5719 | qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } |
5720 | |
5721 | bool qual_empty() const { return getNumProtocols() == 0; } |
5722 | |
5723 | |
5724 | |
5725 | unsigned getNumProtocols() const { |
5726 | return static_cast<const T*>(this)->getNumProtocolsImpl(); |
5727 | } |
5728 | |
5729 | |
5730 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
5731 | assert(I < getNumProtocols() && "Out-of-range protocol access"); |
5732 | return qual_begin()[I]; |
5733 | } |
5734 | |
5735 | |
5736 | ArrayRef<ObjCProtocolDecl *> getProtocols() const { |
5737 | return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); |
5738 | } |
5739 | }; |
5740 | |
5741 | |
5742 | |
5743 | class ObjCTypeParamType : public Type, |
5744 | public ObjCProtocolQualifiers<ObjCTypeParamType>, |
5745 | public llvm::FoldingSetNode { |
5746 | friend class ASTContext; |
5747 | friend class ObjCProtocolQualifiers<ObjCTypeParamType>; |
5748 | |
5749 | |
5750 | unsigned NumProtocols : 6; |
5751 | |
5752 | ObjCTypeParamDecl *OTPDecl; |
5753 | |
5754 | |
5755 | |
5756 | |
5757 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5758 | |
5759 | |
5760 | |
5761 | unsigned getNumProtocolsImpl() const { |
5762 | return NumProtocols; |
5763 | } |
5764 | |
5765 | void setNumProtocolsImpl(unsigned N) { |
5766 | NumProtocols = N; |
5767 | } |
5768 | |
5769 | ObjCTypeParamType(const ObjCTypeParamDecl *D, |
5770 | QualType can, |
5771 | ArrayRef<ObjCProtocolDecl *> protocols); |
5772 | |
5773 | public: |
5774 | bool isSugared() const { return true; } |
5775 | QualType desugar() const { return getCanonicalTypeInternal(); } |
5776 | |
5777 | static bool classof(const Type *T) { |
5778 | return T->getTypeClass() == ObjCTypeParam; |
5779 | } |
5780 | |
5781 | void Profile(llvm::FoldingSetNodeID &ID); |
5782 | static void Profile(llvm::FoldingSetNodeID &ID, |
5783 | const ObjCTypeParamDecl *OTPDecl, |
5784 | QualType CanonicalType, |
5785 | ArrayRef<ObjCProtocolDecl *> protocols); |
5786 | |
5787 | ObjCTypeParamDecl *getDecl() const { return OTPDecl; } |
5788 | }; |
5789 | |
5790 | |
5791 | |
5792 | |
5793 | |
5794 | |
5795 | |
5796 | |
5797 | |
5798 | |
5799 | |
5800 | |
5801 | |
5802 | |
5803 | |
5804 | |
5805 | |
5806 | |
5807 | |
5808 | |
5809 | |
5810 | |
5811 | |
5812 | |
5813 | |
5814 | |
5815 | |
5816 | |
5817 | |
5818 | class ObjCObjectType : public Type, |
5819 | public ObjCProtocolQualifiers<ObjCObjectType> { |
5820 | friend class ObjCProtocolQualifiers<ObjCObjectType>; |
5821 | |
5822 | |
5823 | |
5824 | |
5825 | |
5826 | |
5827 | |
5828 | |
5829 | |
5830 | |
5831 | |
5832 | |
5833 | |
5834 | |
5835 | QualType BaseType; |
5836 | |
5837 | |
5838 | mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> |
5839 | CachedSuperClassType; |
5840 | |
5841 | QualType *getTypeArgStorage(); |
5842 | const QualType *getTypeArgStorage() const { |
5843 | return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); |
5844 | } |
5845 | |
5846 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5847 | |
5848 | |
5849 | unsigned getNumProtocolsImpl() const { |
5850 | return ObjCObjectTypeBits.NumProtocols; |
5851 | } |
5852 | void setNumProtocolsImpl(unsigned N) { |
5853 | ObjCObjectTypeBits.NumProtocols = N; |
5854 | } |
5855 | |
5856 | protected: |
5857 | enum Nonce_ObjCInterface { Nonce_ObjCInterface }; |
5858 | |
5859 | ObjCObjectType(QualType Canonical, QualType Base, |
5860 | ArrayRef<QualType> typeArgs, |
5861 | ArrayRef<ObjCProtocolDecl *> protocols, |
5862 | bool isKindOf); |
5863 | |
5864 | ObjCObjectType(enum Nonce_ObjCInterface) |
5865 | : Type(ObjCInterface, QualType(), TypeDependence::None), |
5866 | BaseType(QualType(this_(), 0)) { |
5867 | ObjCObjectTypeBits.NumProtocols = 0; |
5868 | ObjCObjectTypeBits.NumTypeArgs = 0; |
5869 | ObjCObjectTypeBits.IsKindOf = 0; |
5870 | } |
5871 | |
5872 | void computeSuperClassTypeSlow() const; |
5873 | |
5874 | public: |
5875 | |
5876 | |
5877 | |
5878 | |
5879 | |
5880 | |
5881 | QualType getBaseType() const { return BaseType; } |
5882 | |
5883 | bool isObjCId() const { |
5884 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); |
5885 | } |
5886 | |
5887 | bool isObjCClass() const { |
5888 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); |
5889 | } |
5890 | |
5891 | bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } |
5892 | bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } |
5893 | bool isObjCUnqualifiedIdOrClass() const { |
5894 | if (!qual_empty()) return false; |
5895 | if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) |
5896 | return T->getKind() == BuiltinType::ObjCId || |
5897 | T->getKind() == BuiltinType::ObjCClass; |
5898 | return false; |
5899 | } |
5900 | bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } |
5901 | bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } |
5902 | |
5903 | |
5904 | |
5905 | ObjCInterfaceDecl *getInterface() const; |
5906 | |
5907 | |
5908 | |
5909 | bool isSpecialized() const; |
5910 | |
5911 | |
5912 | bool isSpecializedAsWritten() const { |
5913 | return ObjCObjectTypeBits.NumTypeArgs > 0; |
5914 | } |
5915 | |
5916 | |
5917 | |
5918 | bool isUnspecialized() const { return !isSpecialized(); } |
5919 | |
5920 | |
5921 | |
5922 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
5923 | |
5924 | |
5925 | ArrayRef<QualType> getTypeArgs() const; |
5926 | |
5927 | |
5928 | |
5929 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
5930 | return llvm::makeArrayRef(getTypeArgStorage(), |
5931 | ObjCObjectTypeBits.NumTypeArgs); |
5932 | } |
5933 | |
5934 | |
5935 | bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } |
5936 | |
5937 | |
5938 | bool isKindOfType() const; |
5939 | |
5940 | |
5941 | |
5942 | |
5943 | |
5944 | |
5945 | |
5946 | QualType getSuperClassType() const { |
5947 | if (!CachedSuperClassType.getInt()) |
5948 | computeSuperClassTypeSlow(); |
5949 | |
5950 | assert(CachedSuperClassType.getInt() && "Superclass not set?"); |
5951 | return QualType(CachedSuperClassType.getPointer(), 0); |
5952 | } |
5953 | |
5954 | |
5955 | |
5956 | QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; |
5957 | |
5958 | bool isSugared() const { return false; } |
5959 | QualType desugar() const { return QualType(this, 0); } |
5960 | |
5961 | static bool classof(const Type *T) { |
5962 | return T->getTypeClass() == ObjCObject || |
5963 | T->getTypeClass() == ObjCInterface; |
5964 | } |
5965 | }; |
5966 | |
5967 | |
5968 | |
5969 | |
5970 | |
5971 | class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { |
5972 | friend class ASTContext; |
5973 | |
5974 | |
5975 | |
5976 | |
5977 | ObjCObjectTypeImpl(QualType Canonical, QualType Base, |
5978 | ArrayRef<QualType> typeArgs, |
5979 | ArrayRef<ObjCProtocolDecl *> protocols, |
5980 | bool isKindOf) |
5981 | : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} |
5982 | |
5983 | public: |
5984 | void Profile(llvm::FoldingSetNodeID &ID); |
5985 | static void Profile(llvm::FoldingSetNodeID &ID, |
5986 | QualType Base, |
5987 | ArrayRef<QualType> typeArgs, |
5988 | ArrayRef<ObjCProtocolDecl *> protocols, |
5989 | bool isKindOf); |
5990 | }; |
5991 | |
5992 | inline QualType *ObjCObjectType::getTypeArgStorage() { |
5993 | return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); |
5994 | } |
5995 | |
5996 | inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { |
5997 | return reinterpret_cast<ObjCProtocolDecl**>( |
5998 | getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); |
5999 | } |
6000 | |
6001 | inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { |
6002 | return reinterpret_cast<ObjCProtocolDecl**>( |
6003 | static_cast<ObjCTypeParamType*>(this)+1); |
6004 | } |
6005 | |
6006 | |
6007 | |
6008 | |
6009 | |
6010 | |
6011 | |
6012 | |
6013 | |
6014 | |
6015 | |
6016 | |
6017 | |
6018 | class ObjCInterfaceType : public ObjCObjectType { |
6019 | friend class ASTContext; |
6020 | friend class ASTReader; |
6021 | friend class ObjCInterfaceDecl; |
6022 | template <class T> friend class serialization::AbstractTypeReader; |
6023 | |
6024 | mutable ObjCInterfaceDecl *Decl; |
6025 | |
6026 | ObjCInterfaceType(const ObjCInterfaceDecl *D) |
6027 | : ObjCObjectType(Nonce_ObjCInterface), |
6028 | Decl(const_cast<ObjCInterfaceDecl*>(D)) {} |
6029 | |
6030 | public: |
6031 | |
6032 | ObjCInterfaceDecl *getDecl() const { return Decl; } |
6033 | |
6034 | bool isSugared() const { return false; } |
6035 | QualType desugar() const { return QualType(this, 0); } |
6036 | |
6037 | static bool classof(const Type *T) { |
6038 | return T->getTypeClass() == ObjCInterface; |
6039 | } |
6040 | |
6041 | |
6042 | |
6043 | |
6044 | |
6045 | enum { |
6046 | qual_iterator, |
6047 | qual_begin, |
6048 | qual_end, |
6049 | getNumProtocols, |
6050 | getProtocol |
6051 | }; |
6052 | }; |
6053 | |
6054 | inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { |
6055 | QualType baseType = getBaseType(); |
6056 | while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { |
6057 | if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) |
6058 | return T->getDecl(); |
6059 | |
6060 | baseType = ObjT->getBaseType(); |
6061 | } |
6062 | |
6063 | return nullptr; |
6064 | } |
6065 | |
6066 | |
6067 | |
6068 | |
6069 | |
6070 | |
6071 | |
6072 | |
6073 | |
6074 | |
6075 | class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { |
6076 | friend class ASTContext; |
6077 | |
6078 | QualType PointeeType; |
6079 | |
6080 | ObjCObjectPointerType(QualType Canonical, QualType Pointee) |
6081 | : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()), |
6082 | PointeeType(Pointee) {} |
6083 | |
6084 | public: |
6085 | |
6086 | |
6087 | QualType getPointeeType() const { return PointeeType; } |
6088 | |
6089 | |
6090 | |
6091 | |
6092 | |
6093 | |
6094 | |
6095 | |
6096 | |
6097 | |
6098 | |
6099 | |
6100 | |
6101 | |
6102 | |
6103 | |
6104 | |
6105 | |
6106 | |
6107 | |
6108 | |
6109 | |
6110 | |
6111 | |
6112 | const ObjCObjectType *getObjectType() const { |
6113 | return PointeeType->castAs<ObjCObjectType>(); |
6114 | } |
6115 | |
6116 | |
6117 | |
6118 | |
6119 | |
6120 | |
6121 | const ObjCInterfaceType *getInterfaceType() const; |
6122 | |
6123 | |
6124 | |
6125 | |
6126 | |
6127 | ObjCInterfaceDecl *getInterfaceDecl() const { |
6128 | return getObjectType()->getInterface(); |
6129 | } |
6130 | |
6131 | |
6132 | |
6133 | bool isObjCIdType() const { |
6134 | return getObjectType()->isObjCUnqualifiedId(); |
6135 | } |
6136 | |
6137 | |
6138 | |
6139 | bool isObjCClassType() const { |
6140 | return getObjectType()->isObjCUnqualifiedClass(); |
6141 | } |
6142 | |
6143 | |
6144 | bool isObjCIdOrClassType() const { |
6145 | return getObjectType()->isObjCUnqualifiedIdOrClass(); |
6146 | } |
6147 | |
6148 | |
6149 | |
6150 | bool isObjCQualifiedIdType() const { |
6151 | return getObjectType()->isObjCQualifiedId(); |
6152 | } |
6153 | |
6154 | |
6155 | |
6156 | bool isObjCQualifiedClassType() const { |
6157 | return getObjectType()->isObjCQualifiedClass(); |
6158 | } |
6159 | |
6160 | |
6161 | bool isKindOfType() const { return getObjectType()->isKindOfType(); } |
6162 | |
6163 | |
6164 | bool isSpecialized() const { return getObjectType()->isSpecialized(); } |
6165 | |
6166 | |
6167 | bool isSpecializedAsWritten() const { |
6168 | return getObjectType()->isSpecializedAsWritten(); |
6169 | } |
6170 | |
6171 | |
6172 | bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } |
6173 | |
6174 | |
6175 | |
6176 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
6177 | |
6178 | |
6179 | ArrayRef<QualType> getTypeArgs() const { |
6180 | return getObjectType()->getTypeArgs(); |
6181 | } |
6182 | |
6183 | |
6184 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
6185 | return getObjectType()->getTypeArgsAsWritten(); |
6186 | } |
6187 | |
6188 | |
6189 | |
6190 | |
6191 | using qual_iterator = ObjCObjectType::qual_iterator; |
6192 | using qual_range = llvm::iterator_range<qual_iterator>; |
6193 | |
6194 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
6195 | |
6196 | qual_iterator qual_begin() const { |
6197 | return getObjectType()->qual_begin(); |
6198 | } |
6199 | |
6200 | qual_iterator qual_end() const { |
6201 | return getObjectType()->qual_end(); |
6202 | } |
6203 | |
6204 | bool qual_empty() const { return getObjectType()->qual_empty(); } |
6205 | |
6206 | |
6207 | unsigned getNumProtocols() const { |
6208 | return getObjectType()->getNumProtocols(); |
6209 | } |
6210 | |
6211 | |
6212 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
6213 | return getObjectType()->getProtocol(I); |
6214 | } |
6215 | |
6216 | bool isSugared() const { return false; } |
6217 | QualType desugar() const { return QualType(this, 0); } |
6218 | |
6219 | |
6220 | |
6221 | |
6222 | |
6223 | |
6224 | |
6225 | QualType getSuperClassType() const; |
6226 | |
6227 | |
6228 | |
6229 | const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( |
6230 | const ASTContext &ctx) const; |
6231 | |
6232 | void Profile(llvm::FoldingSetNodeID &ID) { |
6233 | Profile(ID, getPointeeType()); |
6234 | } |
6235 | |
6236 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6237 | ID.AddPointer(T.getAsOpaquePtr()); |
6238 | } |
6239 | |
6240 | static bool classof(const Type *T) { |
6241 | return T->getTypeClass() == ObjCObjectPointer; |
6242 | } |
6243 | }; |
6244 | |
6245 | class AtomicType : public Type, public llvm::FoldingSetNode { |
6246 | friend class ASTContext; |
6247 | |
6248 | QualType ValueType; |
6249 | |
6250 | AtomicType(QualType ValTy, QualType Canonical) |
6251 | : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {} |
6252 | |
6253 | public: |
6254 | |
6255 | |
6256 | QualType getValueType() const { return ValueType; } |
6257 | |
6258 | bool isSugared() const { return false; } |
6259 | QualType desugar() const { return QualType(this, 0); } |
6260 | |
6261 | void Profile(llvm::FoldingSetNodeID &ID) { |
6262 | Profile(ID, getValueType()); |
6263 | } |
6264 | |
6265 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6266 | ID.AddPointer(T.getAsOpaquePtr()); |
6267 | } |
6268 | |
6269 | static bool classof(const Type *T) { |
6270 | return T->getTypeClass() == Atomic; |
6271 | } |
6272 | }; |
6273 | |
6274 | |
6275 | class PipeType : public Type, public llvm::FoldingSetNode { |
6276 | friend class ASTContext; |
6277 | |
6278 | QualType ElementType; |
6279 | bool isRead; |
6280 | |
6281 | PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) |
6282 | : Type(Pipe, CanonicalPtr, elemType->getDependence()), |
6283 | ElementType(elemType), isRead(isRead) {} |
6284 | |
6285 | public: |
6286 | QualType getElementType() const { return ElementType; } |
6287 | |
6288 | bool isSugared() const { return false; } |
6289 | |
6290 | QualType desugar() const { return QualType(this, 0); } |
6291 | |
6292 | void Profile(llvm::FoldingSetNodeID &ID) { |
6293 | Profile(ID, getElementType(), isReadOnly()); |
6294 | } |
6295 | |
6296 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { |
6297 | ID.AddPointer(T.getAsOpaquePtr()); |
6298 | ID.AddBoolean(isRead); |
6299 | } |
6300 | |
6301 | static bool classof(const Type *T) { |
6302 | return T->getTypeClass() == Pipe; |
6303 | } |
6304 | |
6305 | bool isReadOnly() const { return isRead; } |
6306 | }; |
6307 | |
6308 | |
6309 | class ExtIntType final : public Type, public llvm::FoldingSetNode { |
6310 | friend class ASTContext; |
6311 | unsigned IsUnsigned : 1; |
6312 | unsigned NumBits : 24; |
6313 | |
6314 | protected: |
6315 | ExtIntType(bool isUnsigned, unsigned NumBits); |
6316 | |
6317 | public: |
6318 | bool isUnsigned() const { return IsUnsigned; } |
6319 | bool isSigned() const { return !IsUnsigned; } |
6320 | unsigned getNumBits() const { return NumBits; } |
6321 | |
6322 | bool isSugared() const { return false; } |
6323 | QualType desugar() const { return QualType(this, 0); } |
6324 | |
6325 | void Profile(llvm::FoldingSetNodeID &ID) { |
6326 | Profile(ID, isUnsigned(), getNumBits()); |
6327 | } |
6328 | |
6329 | static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned, |
6330 | unsigned NumBits) { |
6331 | ID.AddBoolean(IsUnsigned); |
6332 | ID.AddInteger(NumBits); |
6333 | } |
6334 | |
6335 | static bool classof(const Type *T) { return T->getTypeClass() == ExtInt; } |
6336 | }; |
6337 | |
6338 | class DependentExtIntType final : public Type, public llvm::FoldingSetNode { |
6339 | friend class ASTContext; |
6340 | const ASTContext &Context; |
6341 | llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned; |
6342 | |
6343 | protected: |
6344 | DependentExtIntType(const ASTContext &Context, bool IsUnsigned, |
6345 | Expr *NumBits); |
6346 | |
6347 | public: |
6348 | bool isUnsigned() const; |
6349 | bool isSigned() const { return !isUnsigned(); } |
6350 | Expr *getNumBitsExpr() const; |
6351 | |
6352 | bool isSugared() const { return false; } |
6353 | QualType desugar() const { return QualType(this, 0); } |
6354 | |
6355 | void Profile(llvm::FoldingSetNodeID &ID) { |
6356 | Profile(ID, Context, isUnsigned(), getNumBitsExpr()); |
6357 | } |
6358 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
6359 | bool IsUnsigned, Expr *NumBitsExpr); |
6360 | |
6361 | static bool classof(const Type *T) { |
6362 | return T->getTypeClass() == DependentExtInt; |
6363 | } |
6364 | }; |
6365 | |
6366 | |
6367 | class QualifierCollector : public Qualifiers { |
6368 | public: |
6369 | QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} |
6370 | |
6371 | |
6372 | |
6373 | |
6374 | const Type *strip(QualType type) { |
6375 | addFastQualifiers(type.getLocalFastQualifiers()); |
6376 | if (!type.hasLocalNonFastQualifiers()) |
6377 | return type.getTypePtrUnsafe(); |
6378 | |
6379 | const ExtQuals *extQuals = type.getExtQualsUnsafe(); |
6380 | addConsistentQualifiers(extQuals->getQualifiers()); |
6381 | return extQuals->getBaseType(); |
6382 | } |
6383 | |
6384 | |
6385 | QualType apply(const ASTContext &Context, QualType QT) const; |
6386 | |
6387 | |
6388 | QualType apply(const ASTContext &Context, const Type* T) const; |
6389 | }; |
6390 | |
6391 | |
6392 | |
6393 | |
6394 | |
6395 | |
6396 | |
6397 | |
6398 | class alignas(8) TypeSourceInfo { |
6399 | |
6400 | |
6401 | friend class ASTContext; |
6402 | |
6403 | QualType Ty; |
6404 | |
6405 | TypeSourceInfo(QualType ty) : Ty(ty) {} |
6406 | |
6407 | public: |
6408 | |
6409 | QualType getType() const { return Ty; } |
6410 | |
6411 | |
6412 | TypeLoc getTypeLoc() const; |
6413 | |
6414 | |
6415 | void overrideType(QualType T) { Ty = T; } |
6416 | }; |
6417 | |
6418 | |
6419 | |
6420 | inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { |
6421 | SplitQualType desugar = |
6422 | Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); |
6423 | desugar.Quals.addConsistentQualifiers(Quals); |
6424 | return desugar; |
6425 | } |
6426 | |
6427 | inline const Type *QualType::getTypePtr() const { |
6428 | return getCommonPtr()->BaseType; |
6429 | } |
6430 | |
6431 | inline const Type *QualType::getTypePtrOrNull() const { |
6432 | return (isNull() ? nullptr : getCommonPtr()->BaseType); |
6433 | } |
6434 | |
6435 | inline SplitQualType QualType::split() const { |
6436 | if (!hasLocalNonFastQualifiers()) |
6437 | return SplitQualType(getTypePtrUnsafe(), |
6438 | Qualifiers::fromFastMask(getLocalFastQualifiers())); |
6439 | |
6440 | const ExtQuals *eq = getExtQualsUnsafe(); |
6441 | Qualifiers qs = eq->getQualifiers(); |
6442 | qs.addFastQualifiers(getLocalFastQualifiers()); |
6443 | return SplitQualType(eq->getBaseType(), qs); |
6444 | } |
6445 | |
6446 | inline Qualifiers QualType::getLocalQualifiers() const { |
6447 | Qualifiers Quals; |
6448 | if (hasLocalNonFastQualifiers()) |
6449 | Quals = getExtQualsUnsafe()->getQualifiers(); |
6450 | Quals.addFastQualifiers(getLocalFastQualifiers()); |
6451 | return Quals; |
6452 | } |
6453 | |
6454 | inline Qualifiers QualType::getQualifiers() const { |
6455 | Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); |
6456 | quals.addFastQualifiers(getLocalFastQualifiers()); |
6457 | return quals; |
6458 | } |
6459 | |
6460 | inline unsigned QualType::getCVRQualifiers() const { |
6461 | unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); |
6462 | cvr |= getLocalCVRQualifiers(); |
6463 | return cvr; |
6464 | } |
6465 | |
6466 | inline QualType QualType::getCanonicalType() const { |
6467 | QualType canon = getCommonPtr()->CanonicalType; |
6468 | return canon.withFastQualifiers(getLocalFastQualifiers()); |
6469 | } |
6470 | |
6471 | inline bool QualType::isCanonical() const { |
6472 | return getTypePtr()->isCanonicalUnqualified(); |
6473 | } |
6474 | |
6475 | inline bool QualType::isCanonicalAsParam() const { |
6476 | if (!isCanonical()) return false; |
6477 | if (hasLocalQualifiers()) return false; |
6478 | |
6479 | const Type *T = getTypePtr(); |
6480 | if (T->isVariablyModifiedType() && T->hasSizedVLAType()) |
6481 | return false; |
6482 | |
6483 | return !isa<FunctionType>(T) && !isa<ArrayType>(T); |
6484 | } |
6485 | |
6486 | inline bool QualType::isConstQualified() const { |
6487 | return isLocalConstQualified() || |
6488 | getCommonPtr()->CanonicalType.isLocalConstQualified(); |
6489 | } |
6490 | |
6491 | inline bool QualType::isRestrictQualified() const { |
6492 | return isLocalRestrictQualified() || |
6493 | getCommonPtr()->CanonicalType.isLocalRestrictQualified(); |
6494 | } |
6495 | |
6496 | |
6497 | inline bool QualType::isVolatileQualified() const { |
6498 | return isLocalVolatileQualified() || |
6499 | getCommonPtr()->CanonicalType.isLocalVolatileQualified(); |
6500 | } |
6501 | |
6502 | inline bool QualType::hasQualifiers() const { |
6503 | return hasLocalQualifiers() || |
6504 | getCommonPtr()->CanonicalType.hasLocalQualifiers(); |
6505 | } |
6506 | |
6507 | inline QualType QualType::getUnqualifiedType() const { |
6508 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6509 | return QualType(getTypePtr(), 0); |
6510 | |
6511 | return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); |
6512 | } |
6513 | |
6514 | inline SplitQualType QualType::getSplitUnqualifiedType() const { |
6515 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6516 | return split(); |
6517 | |
6518 | return getSplitUnqualifiedTypeImpl(*this); |
6519 | } |
6520 | |
6521 | inline void QualType::removeLocalConst() { |
6522 | removeLocalFastQualifiers(Qualifiers::Const); |
6523 | } |
6524 | |
6525 | inline void QualType::removeLocalRestrict() { |
6526 | removeLocalFastQualifiers(Qualifiers::Restrict); |
6527 | } |
6528 | |
6529 | inline void QualType::removeLocalVolatile() { |
6530 | removeLocalFastQualifiers(Qualifiers::Volatile); |
6531 | } |
6532 | |
6533 | inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { |
6534 | assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); |
6535 | static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask, |
6536 | "Fast bits differ from CVR bits!"); |
6537 | |
6538 | |
6539 | removeLocalFastQualifiers(Mask); |
6540 | } |
6541 | |
6542 | |
6543 | inline bool QualType::hasAddressSpace() const { |
6544 | return getQualifiers().hasAddressSpace(); |
6545 | } |
6546 | |
6547 | |
6548 | inline LangAS QualType::getAddressSpace() const { |
6549 | return getQualifiers().getAddressSpace(); |
6550 | } |
6551 | |
6552 | |
6553 | inline Qualifiers::GC QualType::getObjCGCAttr() const { |
6554 | return getQualifiers().getObjCGCAttr(); |
6555 | } |
6556 | |
6557 | inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
6558 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6559 | return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); |
6560 | return false; |
6561 | } |
6562 | |
6563 | inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { |
6564 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6565 | return hasNonTrivialToPrimitiveDestructCUnion(RD); |
6566 | return false; |
6567 | } |
6568 | |
6569 | inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { |
6570 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6571 | return hasNonTrivialToPrimitiveCopyCUnion(RD); |
6572 | return false; |
6573 | } |
6574 | |
6575 | inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { |
6576 | if (const auto *PT = t.getAs<PointerType>()) { |
6577 | if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) |
6578 | return FT->getExtInfo(); |
6579 | } else if (const auto *FT = t.getAs<FunctionType>()) |
6580 | return FT->getExtInfo(); |
6581 | |
6582 | return FunctionType::ExtInfo(); |
6583 | } |
6584 | |
6585 | inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { |
6586 | return getFunctionExtInfo(*t); |
6587 | } |
6588 | |
6589 | |
6590 | |
6591 | |
6592 | |
6593 | |
6594 | inline bool QualType::isMoreQualifiedThan(QualType other) const { |
6595 | Qualifiers MyQuals = getQualifiers(); |
6596 | Qualifiers OtherQuals = other.getQualifiers(); |
6597 | return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals)); |
6598 | } |
6599 | |
6600 | |
6601 | |
6602 | |
6603 | |
6604 | inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { |
6605 | Qualifiers OtherQuals = other.getQualifiers(); |
6606 | |
6607 | |
6608 | if (getUnqualifiedType()->isVoidType()) |
6609 | OtherQuals.removeUnaligned(); |
6610 | |
6611 | return getQualifiers().compatiblyIncludes(OtherQuals); |
6612 | } |
6613 | |
6614 | |
6615 | |
6616 | |
6617 | |
6618 | |
6619 | |
6620 | |
6621 | |
6622 | |
6623 | inline QualType QualType::getNonReferenceType() const { |
6624 | if (const auto *RefType = (*this)->getAs<ReferenceType>()) |
6625 | return RefType->getPointeeType(); |
6626 | else |
6627 | return *this; |
6628 | } |
6629 | |
6630 | inline bool QualType::isCForbiddenLValueType() const { |
6631 | return ((getTypePtr()->isVoidType() && !hasQualifiers()) || |
6632 | getTypePtr()->isFunctionType()); |
6633 | } |
6634 | |
6635 | |
6636 | |
6637 | |
6638 | inline bool Type::isFundamentalType() const { |
6639 | return isVoidType() || |
6640 | isNullPtrType() || |
6641 | |
6642 | |
6643 | (isArithmeticType() && !isEnumeralType()); |
6644 | } |
6645 | |
6646 | |
6647 | |
6648 | |
6649 | inline bool Type::isCompoundType() const { |
6650 | |
6651 | |
6652 | |
6653 | return isArrayType() || |
6654 | |
6655 | isFunctionType() || |
6656 | |
6657 | isPointerType() || |
6658 | |
6659 | isReferenceType() || |
6660 | |
6661 | isRecordType() || |
6662 | |
6663 | |
6664 | isUnionType() || |
6665 | |
6666 | isEnumeralType() || |
6667 | |
6668 | isMemberPointerType(); |
6669 | } |
6670 | |
6671 | inline bool Type::isFunctionType() const { |
6672 | return isa<FunctionType>(CanonicalType); |
6673 | } |
6674 | |
6675 | inline bool Type::isPointerType() const { |
6676 | return isa<PointerType>(CanonicalType); |
6677 | } |
6678 | |
6679 | inline bool Type::isAnyPointerType() const { |
6680 | return isPointerType() || isObjCObjectPointerType(); |
6681 | } |
6682 | |
6683 | inline bool Type::isBlockPointerType() const { |
6684 | return isa<BlockPointerType>(CanonicalType); |
6685 | } |
6686 | |
6687 | inline bool Type::isReferenceType() const { |
6688 | return isa<ReferenceType>(CanonicalType); |
6689 | } |
6690 | |
6691 | inline bool Type::isLValueReferenceType() const { |
6692 | return isa<LValueReferenceType>(CanonicalType); |
6693 | } |
6694 | |
6695 | inline bool Type::isRValueReferenceType() const { |
6696 | return isa<RValueReferenceType>(CanonicalType); |
6697 | } |
6698 | |
6699 | inline bool Type::isObjectPointerType() const { |
6700 | |
6701 | |
6702 | |
6703 | if (const auto *T = getAs<PointerType>()) |
6704 | return !T->getPointeeType()->isFunctionType(); |
6705 | else |
6706 | return false; |
6707 | } |
6708 | |
6709 | inline bool Type::isFunctionPointerType() const { |
6710 | if (const auto *T = getAs<PointerType>()) |
6711 | return T->getPointeeType()->isFunctionType(); |
6712 | else |
6713 | return false; |
6714 | } |
6715 | |
6716 | inline bool Type::isFunctionReferenceType() const { |
6717 | if (const auto *T = getAs<ReferenceType>()) |
6718 | return T->getPointeeType()->isFunctionType(); |
6719 | else |
6720 | return false; |
6721 | } |
6722 | |
6723 | inline bool Type::isMemberPointerType() const { |
6724 | return isa<MemberPointerType>(CanonicalType); |
6725 | } |
6726 | |
6727 | inline bool Type::isMemberFunctionPointerType() const { |
6728 | if (const auto *T = getAs<MemberPointerType>()) |
6729 | return T->isMemberFunctionPointer(); |
6730 | else |
6731 | return false; |
6732 | } |
6733 | |
6734 | inline bool Type::isMemberDataPointerType() const { |
6735 | if (const auto *T = getAs<MemberPointerType>()) |
6736 | return T->isMemberDataPointer(); |
6737 | else |
6738 | return false; |
6739 | } |
6740 | |
6741 | inline bool Type::isArrayType() const { |
6742 | return isa<ArrayType>(CanonicalType); |
6743 | } |
6744 | |
6745 | inline bool Type::isConstantArrayType() const { |
6746 | return isa<ConstantArrayType>(CanonicalType); |
6747 | } |
6748 | |
6749 | inline bool Type::isIncompleteArrayType() const { |
6750 | return isa<IncompleteArrayType>(CanonicalType); |
6751 | } |
6752 | |
6753 | inline bool Type::isVariableArrayType() const { |
6754 | return isa<VariableArrayType>(CanonicalType); |
6755 | } |
6756 | |
6757 | inline bool Type::isDependentSizedArrayType() const { |
6758 | return isa<DependentSizedArrayType>(CanonicalType); |
6759 | } |
6760 | |
6761 | inline bool Type::isBuiltinType() const { |
6762 | return isa<BuiltinType>(CanonicalType); |
6763 | } |
6764 | |
6765 | inline bool Type::isRecordType() const { |
6766 | return isa<RecordType>(CanonicalType); |
6767 | } |
6768 | |
6769 | inline bool Type::isEnumeralType() const { |
6770 | return isa<EnumType>(CanonicalType); |
6771 | } |
6772 | |
6773 | inline bool Type::isAnyComplexType() const { |
6774 | return isa<ComplexType>(CanonicalType); |
6775 | } |
6776 | |
6777 | inline bool Type::isVectorType() const { |
6778 | return isa<VectorType>(CanonicalType); |
6779 | } |
6780 | |
6781 | inline bool Type::isExtVectorType() const { |
6782 | return isa<ExtVectorType>(CanonicalType); |
6783 | } |
6784 | |
6785 | inline bool Type::isMatrixType() const { |
6786 | return isa<MatrixType>(CanonicalType); |
6787 | } |
6788 | |
6789 | inline bool Type::isConstantMatrixType() const { |
6790 | return isa<ConstantMatrixType>(CanonicalType); |
6791 | } |
6792 | |
6793 | inline bool Type::isDependentAddressSpaceType() const { |
6794 | return isa<DependentAddressSpaceType>(CanonicalType); |
6795 | } |
6796 | |
6797 | inline bool Type::isObjCObjectPointerType() const { |
6798 | return isa<ObjCObjectPointerType>(CanonicalType); |
6799 | } |
6800 | |
6801 | inline bool Type::isObjCObjectType() const { |
6802 | return isa<ObjCObjectType>(CanonicalType); |
6803 | } |
6804 | |
6805 | inline bool Type::isObjCObjectOrInterfaceType() const { |
6806 | return isa<ObjCInterfaceType>(CanonicalType) || |
6807 | isa<ObjCObjectType>(CanonicalType); |
6808 | } |
6809 | |
6810 | inline bool Type::isAtomicType() const { |
6811 | return isa<AtomicType>(CanonicalType); |
6812 | } |
6813 | |
6814 | inline bool Type::isUndeducedAutoType() const { |
6815 | return isa<AutoType>(CanonicalType); |
6816 | } |
6817 | |
6818 | inline bool Type::isObjCQualifiedIdType() const { |
6819 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6820 | return OPT->isObjCQualifiedIdType(); |
6821 | return false; |
6822 | } |
6823 | |
6824 | inline bool Type::isObjCQualifiedClassType() const { |
6825 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6826 | return OPT->isObjCQualifiedClassType(); |
6827 | return false; |
6828 | } |
6829 | |
6830 | inline bool Type::isObjCIdType() const { |
6831 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6832 | return OPT->isObjCIdType(); |
6833 | return false; |
6834 | } |
6835 | |
6836 | inline bool Type::isObjCClassType() const { |
6837 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6838 | return OPT->isObjCClassType(); |
6839 | return false; |
6840 | } |
6841 | |
6842 | inline bool Type::isObjCSelType() const { |
6843 | if (const auto *OPT = getAs<PointerType>()) |
6844 | return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); |
6845 | return false; |
6846 | } |
6847 | |
6848 | inline bool Type::isObjCBuiltinType() const { |
6849 | return isObjCIdType() || isObjCClassType() || isObjCSelType(); |
6850 | } |
6851 | |
6852 | inline bool Type::isDecltypeType() const { |
6853 | return isa<DecltypeType>(this); |
6854 | } |
6855 | |
6856 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
6857 | inline bool Type::is##Id##Type() const { \ |
6858 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6859 | } |
6860 | #include "clang/Basic/OpenCLImageTypes.def" |
6861 | |
6862 | inline bool Type::isSamplerT() const { |
6863 | return isSpecificBuiltinType(BuiltinType::OCLSampler); |
6864 | } |
6865 | |
6866 | inline bool Type::isEventT() const { |
6867 | return isSpecificBuiltinType(BuiltinType::OCLEvent); |
6868 | } |
6869 | |
6870 | inline bool Type::isClkEventT() const { |
6871 | return isSpecificBuiltinType(BuiltinType::OCLClkEvent); |
6872 | } |
6873 | |
6874 | inline bool Type::isQueueT() const { |
6875 | return isSpecificBuiltinType(BuiltinType::OCLQueue); |
6876 | } |
6877 | |
6878 | inline bool Type::isReserveIDT() const { |
6879 | return isSpecificBuiltinType(BuiltinType::OCLReserveID); |
6880 | } |
6881 | |
6882 | inline bool Type::isImageType() const { |
6883 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || |
6884 | return |
6885 | #include "clang/Basic/OpenCLImageTypes.def" |
6886 | false; |
6887 | } |
6888 | |
6889 | inline bool Type::isPipeType() const { |
6890 | return isa<PipeType>(CanonicalType); |
6891 | } |
6892 | |
6893 | inline bool Type::isExtIntType() const { |
6894 | return isa<ExtIntType>(CanonicalType); |
6895 | } |
6896 | |
6897 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
6898 | inline bool Type::is##Id##Type() const { \ |
6899 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6900 | } |
6901 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6902 | |
6903 | inline bool Type::isOCLIntelSubgroupAVCType() const { |
6904 | #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ |
6905 | isOCLIntelSubgroupAVC##Id##Type() || |
6906 | return |
6907 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6908 | false; |
6909 | } |
6910 | |
6911 | inline bool Type::isOCLExtOpaqueType() const { |
6912 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || |
6913 | return |
6914 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6915 | false; |
6916 | } |
6917 | |
6918 | inline bool Type::isOpenCLSpecificType() const { |
6919 | return isSamplerT() || isEventT() || isImageType() || isClkEventT() || |
6920 | isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); |
6921 | } |
6922 | |
6923 | inline bool Type::isTemplateTypeParmType() const { |
6924 | return isa<TemplateTypeParmType>(CanonicalType); |
6925 | } |
6926 | |
6927 | inline bool Type::isSpecificBuiltinType(unsigned K) const { |
6928 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
6929 | return BT->getKind() == static_cast<BuiltinType::Kind>(K); |
6930 | } |
6931 | return false; |
6932 | } |
6933 | |
6934 | inline bool Type::isPlaceholderType() const { |
6935 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6936 | return BT->isPlaceholderType(); |
6937 | return false; |
6938 | } |
6939 | |
6940 | inline const BuiltinType *Type::getAsPlaceholderType() const { |
6941 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6942 | if (BT->isPlaceholderType()) |
6943 | return BT; |
6944 | return nullptr; |
6945 | } |
6946 | |
6947 | inline bool Type::isSpecificPlaceholderType(unsigned K) const { |
6948 | assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); |
6949 | return isSpecificBuiltinType(K); |
6950 | } |
6951 | |
6952 | inline bool Type::isNonOverloadPlaceholderType() const { |
6953 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6954 | return BT->isNonOverloadPlaceholderType(); |
6955 | return false; |
6956 | } |
6957 | |
6958 | inline bool Type::isVoidType() const { |
6959 | return isSpecificBuiltinType(BuiltinType::Void); |
6960 | } |
6961 | |
6962 | inline bool Type::isHalfType() const { |
6963 | |
6964 | return isSpecificBuiltinType(BuiltinType::Half); |
6965 | } |
6966 | |
6967 | inline bool Type::isFloat16Type() const { |
6968 | return isSpecificBuiltinType(BuiltinType::Float16); |
6969 | } |
6970 | |
6971 | inline bool Type::isBFloat16Type() const { |
6972 | return isSpecificBuiltinType(BuiltinType::BFloat16); |
6973 | } |
6974 | |
6975 | inline bool Type::isFloat128Type() const { |
6976 | return isSpecificBuiltinType(BuiltinType::Float128); |
6977 | } |
6978 | |
6979 | inline bool Type::isNullPtrType() const { |
6980 | return isSpecificBuiltinType(BuiltinType::NullPtr); |
6981 | } |
6982 | |
6983 | bool IsEnumDeclComplete(EnumDecl *); |
6984 | bool IsEnumDeclScoped(EnumDecl *); |
6985 | |
6986 | inline bool Type::isIntegerType() const { |
6987 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6988 | return BT->getKind() >= BuiltinType::Bool && |
6989 | BT->getKind() <= BuiltinType::Int128; |
6990 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
6991 | |
6992 | |
6993 | return IsEnumDeclComplete(ET->getDecl()) && |
6994 | !IsEnumDeclScoped(ET->getDecl()); |
6995 | } |
6996 | return isExtIntType(); |
6997 | } |
6998 | |
6999 | inline bool Type::isFixedPointType() const { |
7000 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7001 | return BT->getKind() >= BuiltinType::ShortAccum && |
7002 | BT->getKind() <= BuiltinType::SatULongFract; |
7003 | } |
7004 | return false; |
7005 | } |
7006 | |
7007 | inline bool Type::isFixedPointOrIntegerType() const { |
7008 | return isFixedPointType() || isIntegerType(); |
7009 | } |
7010 | |
7011 | inline bool Type::isSaturatedFixedPointType() const { |
7012 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7013 | return BT->getKind() >= BuiltinType::SatShortAccum && |
7014 | BT->getKind() <= BuiltinType::SatULongFract; |
7015 | } |
7016 | return false; |
7017 | } |
7018 | |
7019 | inline bool Type::isUnsaturatedFixedPointType() const { |
7020 | return isFixedPointType() && !isSaturatedFixedPointType(); |
7021 | } |
7022 | |
7023 | inline bool Type::isSignedFixedPointType() const { |
7024 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7025 | return ((BT->getKind() >= BuiltinType::ShortAccum && |
7026 | BT->getKind() <= BuiltinType::LongAccum) || |
7027 | (BT->getKind() >= BuiltinType::ShortFract && |
7028 | BT->getKind() <= BuiltinType::LongFract) || |
7029 | (BT->getKind() >= BuiltinType::SatShortAccum && |
7030 | BT->getKind() <= BuiltinType::SatLongAccum) || |
7031 | (BT->getKind() >= BuiltinType::SatShortFract && |
7032 | BT->getKind() <= BuiltinType::SatLongFract)); |
7033 | } |
7034 | return false; |
7035 | } |
7036 | |
7037 | inline bool Type::isUnsignedFixedPointType() const { |
7038 | return isFixedPointType() && !isSignedFixedPointType(); |
7039 | } |
7040 | |
7041 | inline bool Type::isScalarType() const { |
7042 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7043 | return BT->getKind() > BuiltinType::Void && |
7044 | BT->getKind() <= BuiltinType::NullPtr; |
7045 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
7046 | |
7047 | |
7048 | return IsEnumDeclComplete(ET->getDecl()); |
7049 | return isa<PointerType>(CanonicalType) || |
7050 | isa<BlockPointerType>(CanonicalType) || |
7051 | isa<MemberPointerType>(CanonicalType) || |
7052 | isa<ComplexType>(CanonicalType) || |
7053 | isa<ObjCObjectPointerType>(CanonicalType) || |
7054 | isExtIntType(); |
7055 | } |
7056 | |
7057 | inline bool Type::isIntegralOrEnumerationType() const { |
7058 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7059 | return BT->getKind() >= BuiltinType::Bool && |
7060 | BT->getKind() <= BuiltinType::Int128; |
7061 | |
7062 | |
7063 | |
7064 | if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) |
7065 | return IsEnumDeclComplete(ET->getDecl()); |
7066 | |
7067 | return isExtIntType(); |
7068 | } |
7069 | |
7070 | inline bool Type::isBooleanType() const { |
7071 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7072 | return BT->getKind() == BuiltinType::Bool; |
7073 | return false; |
7074 | } |
7075 | |
7076 | inline bool Type::isUndeducedType() const { |
7077 | auto *DT = getContainedDeducedType(); |
7078 | return DT && !DT->isDeduced(); |
7079 | } |
7080 | |
7081 | |
7082 | |
7083 | inline bool Type::isOverloadableType() const { |
7084 | return isDependentType() || isRecordType() || isEnumeralType(); |
7085 | } |
7086 | |
7087 | |
7088 | inline bool Type::isTypedefNameType() const { |
7089 | if (getAs<TypedefType>()) |
7090 | return true; |
7091 | if (auto *TST = getAs<TemplateSpecializationType>()) |
7092 | return TST->isTypeAlias(); |
7093 | return false; |
7094 | } |
7095 | |
7096 | |
7097 | inline bool Type::canDecayToPointerType() const { |
7098 | return isFunctionType() || isArrayType(); |
7099 | } |
7100 | |
7101 | inline bool Type::hasPointerRepresentation() const { |
7102 | return (isPointerType() || isReferenceType() || isBlockPointerType() || |
7103 | isObjCObjectPointerType() || isNullPtrType()); |
7104 | } |
7105 | |
7106 | inline bool Type::hasObjCPointerRepresentation() const { |
7107 | return isObjCObjectPointerType(); |
7108 | } |
7109 | |
7110 | inline const Type *Type::getBaseElementTypeUnsafe() const { |
7111 | const Type *type = this; |
7112 | while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) |
7113 | type = arrayType->getElementType().getTypePtr(); |
7114 | return type; |
7115 | } |
7116 | |
7117 | inline const Type *Type::getPointeeOrArrayElementType() const { |
7118 | const Type *type = this; |
7119 | if (type->isAnyPointerType()) |
7120 | return type->getPointeeType().getTypePtr(); |
7121 | else if (type->isArrayType()) |
7122 | return type->getBaseElementTypeUnsafe(); |
7123 | return type; |
7124 | } |
7125 | |
7126 | |
7127 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7128 | LangAS AS) { |
7129 | PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), |
7130 | DiagnosticsEngine::ArgumentKind::ak_addrspace); |
7131 | return PD; |
7132 | } |
7133 | |
7134 | |
7135 | |
7136 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7137 | Qualifiers Q) { |
7138 | PD.AddTaggedVal(Q.getAsOpaqueValue(), |
7139 | DiagnosticsEngine::ArgumentKind::ak_qual); |
7140 | return PD; |
7141 | } |
7142 | |
7143 | |
7144 | |
7145 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7146 | QualType T) { |
7147 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), |
7148 | DiagnosticsEngine::ak_qualtype); |
7149 | return PD; |
7150 | } |
7151 | |
7152 | |
7153 | |
7154 | template <typename T> |
7155 | using TypeIsArrayType = |
7156 | std::integral_constant<bool, std::is_same<T, ArrayType>::value || |
7157 | std::is_base_of<ArrayType, T>::value>; |
7158 | |
7159 | |
7160 | template <typename T> const T *Type::getAs() const { |
7161 | static_assert(!TypeIsArrayType<T>::value, |
7162 | "ArrayType cannot be used with getAs!"); |
7163 | |
7164 | |
7165 | if (const auto *Ty = dyn_cast<T>(this)) |
7166 | return Ty; |
7167 | |
7168 | |
7169 | if (!isa<T>(CanonicalType)) |
7170 | return nullptr; |
7171 | |
7172 | |
7173 | |
7174 | return cast<T>(getUnqualifiedDesugaredType()); |
7175 | } |
7176 | |
7177 | template <typename T> const T *Type::getAsAdjusted() const { |
7178 | static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); |
7179 | |
7180 | |
7181 | if (const auto *Ty = dyn_cast<T>(this)) |
7182 | return Ty; |
7183 | |
7184 | |
7185 | if (!isa<T>(CanonicalType)) |
7186 | return nullptr; |
7187 | |
7188 | |
7189 | |
7190 | const Type *Ty = this; |
7191 | while (Ty) { |
7192 | if (const auto *A = dyn_cast<AttributedType>(Ty)) |
7193 | Ty = A->getModifiedType().getTypePtr(); |
7194 | else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) |
7195 | Ty = E->desugar().getTypePtr(); |
7196 | else if (const auto *P = dyn_cast<ParenType>(Ty)) |
7197 | Ty = P->desugar().getTypePtr(); |
7198 | else if (const auto *A = dyn_cast<AdjustedType>(Ty)) |
7199 | Ty = A->desugar().getTypePtr(); |
7200 | else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) |
7201 | Ty = M->desugar().getTypePtr(); |
7202 | else |
7203 | break; |
7204 | } |
7205 | |
7206 | |
7207 | |
7208 | return dyn_cast<T>(Ty); |
7209 | } |
7210 | |
7211 | inline const ArrayType *Type::getAsArrayTypeUnsafe() const { |
7212 | |
7213 | if (const auto *arr = dyn_cast<ArrayType>(this)) |
7214 | return arr; |
7215 | |
7216 | |
7217 | if (!isa<ArrayType>(CanonicalType)) |
7218 | return nullptr; |
7219 | |
7220 | |
7221 | |
7222 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7223 | } |
7224 | |
7225 | template <typename T> const T *Type::castAs() const { |
7226 | static_assert(!TypeIsArrayType<T>::value, |
7227 | "ArrayType cannot be used with castAs!"); |
7228 | |
7229 | if (const auto *ty = dyn_cast<T>(this)) return ty; |
7230 | assert(isa<T>(CanonicalType)); |
7231 | return cast<T>(getUnqualifiedDesugaredType()); |
7232 | } |
7233 | |
7234 | inline const ArrayType *Type::castAsArrayTypeUnsafe() const { |
7235 | assert(isa<ArrayType>(CanonicalType)); |
7236 | if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; |
7237 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7238 | } |
7239 | |
7240 | DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, |
7241 | QualType CanonicalPtr) |
7242 | : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { |
7243 | #ifndef NDEBUG |
7244 | QualType Adjusted = getAdjustedType(); |
7245 | (void)AttributedType::stripOuterNullability(Adjusted); |
7246 | assert(isa<PointerType>(Adjusted)); |
7247 | #endif |
7248 | } |
7249 | |
7250 | QualType DecayedType::getPointeeType() const { |
7251 | QualType Decayed = getDecayedType(); |
7252 | (void)AttributedType::stripOuterNullability(Decayed); |
7253 | return cast<PointerType>(Decayed)->getPointeeType(); |
7254 | } |
7255 | |
7256 | |
7257 | |
7258 | |
7259 | |
7260 | void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, |
7261 | unsigned Scale); |
7262 | |
7263 | } |
7264 | |
7265 | #endif // LLVM_CLANG_AST_TYPE_H |