1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/AST/Decl.h" |
14 | #include "Linkage.h" |
15 | #include "clang/AST/ASTContext.h" |
16 | #include "clang/AST/ASTDiagnostic.h" |
17 | #include "clang/AST/ASTLambda.h" |
18 | #include "clang/AST/ASTMutationListener.h" |
19 | #include "clang/AST/Attr.h" |
20 | #include "clang/AST/CanonicalType.h" |
21 | #include "clang/AST/DeclBase.h" |
22 | #include "clang/AST/DeclCXX.h" |
23 | #include "clang/AST/DeclObjC.h" |
24 | #include "clang/AST/DeclOpenMP.h" |
25 | #include "clang/AST/DeclTemplate.h" |
26 | #include "clang/AST/DeclarationName.h" |
27 | #include "clang/AST/Expr.h" |
28 | #include "clang/AST/ExprCXX.h" |
29 | #include "clang/AST/ExternalASTSource.h" |
30 | #include "clang/AST/ODRHash.h" |
31 | #include "clang/AST/PrettyDeclStackTrace.h" |
32 | #include "clang/AST/PrettyPrinter.h" |
33 | #include "clang/AST/Redeclarable.h" |
34 | #include "clang/AST/Stmt.h" |
35 | #include "clang/AST/TemplateBase.h" |
36 | #include "clang/AST/Type.h" |
37 | #include "clang/AST/TypeLoc.h" |
38 | #include "clang/Basic/Builtins.h" |
39 | #include "clang/Basic/IdentifierTable.h" |
40 | #include "clang/Basic/LLVM.h" |
41 | #include "clang/Basic/LangOptions.h" |
42 | #include "clang/Basic/Linkage.h" |
43 | #include "clang/Basic/Module.h" |
44 | #include "clang/Basic/NoSanitizeList.h" |
45 | #include "clang/Basic/PartialDiagnostic.h" |
46 | #include "clang/Basic/Sanitizers.h" |
47 | #include "clang/Basic/SourceLocation.h" |
48 | #include "clang/Basic/SourceManager.h" |
49 | #include "clang/Basic/Specifiers.h" |
50 | #include "clang/Basic/TargetCXXABI.h" |
51 | #include "clang/Basic/TargetInfo.h" |
52 | #include "clang/Basic/Visibility.h" |
53 | #include "llvm/ADT/APSInt.h" |
54 | #include "llvm/ADT/ArrayRef.h" |
55 | #include "llvm/ADT/None.h" |
56 | #include "llvm/ADT/Optional.h" |
57 | #include "llvm/ADT/STLExtras.h" |
58 | #include "llvm/ADT/SmallVector.h" |
59 | #include "llvm/ADT/StringRef.h" |
60 | #include "llvm/ADT/StringSwitch.h" |
61 | #include "llvm/ADT/Triple.h" |
62 | #include "llvm/Support/Casting.h" |
63 | #include "llvm/Support/ErrorHandling.h" |
64 | #include "llvm/Support/raw_ostream.h" |
65 | #include <algorithm> |
66 | #include <cassert> |
67 | #include <cstddef> |
68 | #include <cstring> |
69 | #include <memory> |
70 | #include <string> |
71 | #include <tuple> |
72 | #include <type_traits> |
73 | |
74 | using namespace clang; |
75 | |
76 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
77 | return D->getASTContext().getPrimaryMergedDecl(D); |
78 | } |
79 | |
80 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
81 | SourceLocation Loc = this->Loc; |
82 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
83 | if (Loc.isValid()) { |
84 | Loc.print(OS, Context.getSourceManager()); |
85 | OS << ": "; |
86 | } |
87 | OS << Message; |
88 | |
89 | if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) { |
90 | OS << " '"; |
91 | ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); |
92 | OS << "'"; |
93 | } |
94 | |
95 | OS << '\n'; |
96 | } |
97 | |
98 | |
99 | bool Decl::isOutOfLine() const { |
100 | return !getLexicalDeclContext()->Equals(getDeclContext()); |
101 | } |
102 | |
103 | TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) |
104 | : Decl(TranslationUnit, nullptr, SourceLocation()), |
105 | DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {} |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
160 | return computation.IgnoreExplicitVisibility; |
161 | } |
162 | |
163 | |
164 | |
165 | static LVComputationKind |
166 | withExplicitVisibilityAlready(LVComputationKind Kind) { |
167 | Kind.IgnoreExplicitVisibility = true; |
168 | return Kind; |
169 | } |
170 | |
171 | static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
172 | LVComputationKind kind) { |
173 | assert(!kind.IgnoreExplicitVisibility && |
174 | "asking for explicit visibility when we shouldn't be"); |
175 | return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); |
176 | } |
177 | |
178 | |
179 | |
180 | static bool usesTypeVisibility(const NamedDecl *D) { |
181 | return isa<TypeDecl>(D) || |
182 | isa<ClassTemplateDecl>(D) || |
183 | isa<ObjCInterfaceDecl>(D); |
184 | } |
185 | |
186 | |
187 | |
188 | template <class T> static typename |
189 | std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type |
190 | isExplicitMemberSpecialization(const T *D) { |
191 | if (const MemberSpecializationInfo *member = |
192 | D->getMemberSpecializationInfo()) { |
193 | return member->isExplicitSpecialization(); |
194 | } |
195 | return false; |
196 | } |
197 | |
198 | |
199 | |
200 | |
201 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
202 | return D->isMemberSpecialization(); |
203 | } |
204 | |
205 | |
206 | |
207 | template <class T> |
208 | static Visibility getVisibilityFromAttr(const T *attr) { |
209 | switch (attr->getVisibility()) { |
210 | case T::Default: |
211 | return DefaultVisibility; |
212 | case T::Hidden: |
213 | return HiddenVisibility; |
214 | case T::Protected: |
215 | return ProtectedVisibility; |
216 | } |
217 | llvm_unreachable("bad visibility kind"); |
218 | } |
219 | |
220 | |
221 | static Optional<Visibility> getVisibilityOf(const NamedDecl *D, |
222 | NamedDecl::ExplicitVisibilityKind kind) { |
223 | |
224 | |
225 | if (kind == NamedDecl::VisibilityForType) { |
226 | if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { |
227 | return getVisibilityFromAttr(A); |
228 | } |
229 | } |
230 | |
231 | |
232 | if (const auto *A = D->getAttr<VisibilityAttr>()) { |
233 | return getVisibilityFromAttr(A); |
234 | } |
235 | |
236 | return None; |
237 | } |
238 | |
239 | LinkageInfo LinkageComputer::getLVForType(const Type &T, |
240 | LVComputationKind computation) { |
241 | if (computation.IgnoreAllVisibility) |
242 | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
243 | return getTypeLinkageAndVisibility(&T); |
244 | } |
245 | |
246 | |
247 | |
248 | |
249 | LinkageInfo LinkageComputer::getLVForTemplateParameterList( |
250 | const TemplateParameterList *Params, LVComputationKind computation) { |
251 | LinkageInfo LV; |
252 | for (const NamedDecl *P : *Params) { |
253 | |
254 | |
255 | if (isa<TemplateTypeParmDecl>(P)) |
256 | continue; |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
263 | |
264 | if (!NTTP->isExpandedParameterPack()) { |
265 | if (!NTTP->getType()->isDependentType()) { |
266 | LV.merge(getLVForType(*NTTP->getType(), computation)); |
267 | } |
268 | continue; |
269 | } |
270 | |
271 | |
272 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
273 | QualType type = NTTP->getExpansionType(i); |
274 | if (!type->isDependentType()) |
275 | LV.merge(getTypeLinkageAndVisibility(type)); |
276 | } |
277 | continue; |
278 | } |
279 | |
280 | |
281 | |
282 | const auto *TTP = cast<TemplateTemplateParmDecl>(P); |
283 | |
284 | |
285 | if (!TTP->isExpandedParameterPack()) { |
286 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), |
287 | computation)); |
288 | continue; |
289 | } |
290 | |
291 | |
292 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
293 | i != n; ++i) { |
294 | LV.merge(getLVForTemplateParameterList( |
295 | TTP->getExpansionTemplateParameters(i), computation)); |
296 | } |
297 | } |
298 | |
299 | return LV; |
300 | } |
301 | |
302 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
303 | const Decl *Ret = nullptr; |
304 | const DeclContext *DC = D->getDeclContext(); |
305 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
306 | if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) |
307 | Ret = cast<Decl>(DC); |
308 | DC = DC->getParent(); |
309 | } |
310 | return Ret; |
311 | } |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | |
318 | LinkageInfo |
319 | LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
320 | LVComputationKind computation) { |
321 | LinkageInfo LV; |
322 | |
323 | for (const TemplateArgument &Arg : Args) { |
324 | switch (Arg.getKind()) { |
325 | case TemplateArgument::Null: |
326 | case TemplateArgument::Integral: |
327 | case TemplateArgument::Expression: |
328 | continue; |
329 | |
330 | case TemplateArgument::Type: |
331 | LV.merge(getLVForType(*Arg.getAsType(), computation)); |
332 | continue; |
333 | |
334 | case TemplateArgument::Declaration: { |
335 | const NamedDecl *ND = Arg.getAsDecl(); |
336 | assert(!usesTypeVisibility(ND)); |
337 | LV.merge(getLVForDecl(ND, computation)); |
338 | continue; |
339 | } |
340 | |
341 | case TemplateArgument::NullPtr: |
342 | LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); |
343 | continue; |
344 | |
345 | case TemplateArgument::Template: |
346 | case TemplateArgument::TemplateExpansion: |
347 | if (TemplateDecl *Template = |
348 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
349 | LV.merge(getLVForDecl(Template, computation)); |
350 | continue; |
351 | |
352 | case TemplateArgument::Pack: |
353 | LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); |
354 | continue; |
355 | } |
356 | llvm_unreachable("bad template argument kind"); |
357 | } |
358 | |
359 | return LV; |
360 | } |
361 | |
362 | LinkageInfo |
363 | LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
364 | LVComputationKind computation) { |
365 | return getLVForTemplateArgumentList(TArgs.asArray(), computation); |
366 | } |
367 | |
368 | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
369 | const FunctionTemplateSpecializationInfo *specInfo) { |
370 | |
371 | |
372 | |
373 | |
374 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
375 | return true; |
376 | |
377 | return !fn->hasAttr<VisibilityAttr>(); |
378 | } |
379 | |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | void LinkageComputer::mergeTemplateLV( |
388 | LinkageInfo &LV, const FunctionDecl *fn, |
389 | const FunctionTemplateSpecializationInfo *specInfo, |
390 | LVComputationKind computation) { |
391 | bool considerVisibility = |
392 | shouldConsiderTemplateVisibility(fn, specInfo); |
393 | |
394 | |
395 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
396 | LinkageInfo tempLV = |
397 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
398 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
399 | |
400 | |
401 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
402 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
403 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
404 | } |
405 | |
406 | |
407 | |
408 | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
409 | LVComputationKind computation) { |
410 | if (computation.IgnoreAllVisibility) |
411 | return false; |
412 | |
413 | return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || |
414 | D->hasAttr<VisibilityAttr>(); |
415 | } |
416 | |
417 | |
418 | |
419 | static bool shouldConsiderTemplateVisibility( |
420 | const ClassTemplateSpecializationDecl *spec, |
421 | LVComputationKind computation) { |
422 | |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | |
433 | |
434 | |
435 | |
436 | |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | if (!spec->isExplicitInstantiationOrSpecialization()) |
443 | return true; |
444 | |
445 | |
446 | if (spec->isExplicitSpecialization() && |
447 | hasExplicitVisibilityAlready(computation)) |
448 | return false; |
449 | |
450 | return !hasDirectVisibilityAttribute(spec, computation); |
451 | } |
452 | |
453 | |
454 | |
455 | void LinkageComputer::mergeTemplateLV( |
456 | LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, |
457 | LVComputationKind computation) { |
458 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
459 | |
460 | |
461 | |
462 | |
463 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
464 | LinkageInfo tempLV = |
465 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
466 | LV.mergeMaybeWithVisibility(tempLV, |
467 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
468 | |
469 | |
470 | |
471 | |
472 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
473 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
474 | if (considerVisibility) |
475 | LV.mergeVisibility(argsLV); |
476 | LV.mergeExternalVisibility(argsLV); |
477 | } |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | static bool shouldConsiderTemplateVisibility( |
484 | const VarTemplateSpecializationDecl *spec, |
485 | LVComputationKind computation) { |
486 | |
487 | |
488 | |
489 | |
490 | if (!spec->isExplicitInstantiationOrSpecialization()) |
491 | return true; |
492 | |
493 | |
494 | |
495 | |
496 | |
497 | if (spec->isExplicitSpecialization() && |
498 | hasExplicitVisibilityAlready(computation)) |
499 | return false; |
500 | |
501 | return !hasDirectVisibilityAttribute(spec, computation); |
502 | } |
503 | |
504 | |
505 | |
506 | |
507 | void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, |
508 | const VarTemplateSpecializationDecl *spec, |
509 | LVComputationKind computation) { |
510 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
511 | |
512 | |
513 | |
514 | |
515 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
516 | LinkageInfo tempLV = |
517 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
518 | LV.mergeMaybeWithVisibility(tempLV, |
519 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
520 | |
521 | |
522 | |
523 | |
524 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
525 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
526 | if (considerVisibility) |
527 | LV.mergeVisibility(argsLV); |
528 | LV.mergeExternalVisibility(argsLV); |
529 | } |
530 | |
531 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
532 | |
533 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
534 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
535 | return false; |
536 | |
537 | const auto *FD = dyn_cast<FunctionDecl>(D); |
538 | if (!FD) |
539 | return false; |
540 | |
541 | TemplateSpecializationKind TSK = TSK_Undeclared; |
542 | if (FunctionTemplateSpecializationInfo *spec |
543 | = FD->getTemplateSpecializationInfo()) { |
544 | TSK = spec->getTemplateSpecializationKind(); |
545 | } else if (MemberSpecializationInfo *MSI = |
546 | FD->getMemberSpecializationInfo()) { |
547 | TSK = MSI->getTemplateSpecializationKind(); |
548 | } |
549 | |
550 | const FunctionDecl *Def = nullptr; |
551 | |
552 | |
553 | |
554 | return TSK != TSK_ExplicitInstantiationDeclaration && |
555 | TSK != TSK_ExplicitInstantiationDefinition && |
556 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
557 | } |
558 | |
559 | template <typename T> static bool isFirstInExternCContext(T *D) { |
560 | const T *First = D->getFirstDecl(); |
561 | return First->isInExternCContext(); |
562 | } |
563 | |
564 | static bool isSingleLineLanguageLinkage(const Decl &D) { |
565 | if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) |
566 | if (!SD->hasBraces()) |
567 | return true; |
568 | return false; |
569 | } |
570 | |
571 | |
572 | static bool isInModulePurview(const NamedDecl *D) { |
573 | if (auto *M = D->getOwningModule()) |
574 | return M->isModulePurview(); |
575 | return false; |
576 | } |
577 | |
578 | static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { |
579 | |
580 | switch (D->getModuleOwnershipKind()) { |
581 | case Decl::ModuleOwnershipKind::Unowned: |
582 | case Decl::ModuleOwnershipKind::ModulePrivate: |
583 | return false; |
584 | case Decl::ModuleOwnershipKind::Visible: |
585 | case Decl::ModuleOwnershipKind::VisibleWhenImported: |
586 | return isInModulePurview(D); |
587 | } |
588 | llvm_unreachable("unexpected module ownership kind"); |
589 | } |
590 | |
591 | static LinkageInfo getInternalLinkageFor(const NamedDecl *D) { |
592 | |
593 | |
594 | |
595 | |
596 | if (isInModulePurview(D)) |
597 | return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false); |
598 | |
599 | return LinkageInfo::internal(); |
600 | } |
601 | |
602 | static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { |
603 | |
604 | |
605 | |
606 | |
607 | if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit( |
608 | cast<NamedDecl>(D->getCanonicalDecl()))) |
609 | return LinkageInfo(ModuleLinkage, DefaultVisibility, false); |
610 | |
611 | return LinkageInfo::external(); |
612 | } |
613 | |
614 | static StorageClass getStorageClass(const Decl *D) { |
615 | if (auto *TD = dyn_cast<TemplateDecl>(D)) |
616 | D = TD->getTemplatedDecl(); |
617 | if (D) { |
618 | if (auto *VD = dyn_cast<VarDecl>(D)) |
619 | return VD->getStorageClass(); |
620 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
621 | return FD->getStorageClass(); |
622 | } |
623 | return SC_None; |
624 | } |
625 | |
626 | LinkageInfo |
627 | LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, |
628 | LVComputationKind computation, |
629 | bool IgnoreVarTypeLinkage) { |
630 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
631 | "Not a name having namespace scope"); |
632 | ASTContext &Context = D->getASTContext(); |
633 | |
634 | |
635 | |
636 | |
637 | |
638 | if (getStorageClass(D->getCanonicalDecl()) == SC_Static) { |
639 | |
640 | |
641 | |
642 | return getInternalLinkageFor(D); |
643 | } |
644 | |
645 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | |
652 | if (Context.getLangOpts().CPlusPlus && |
653 | Var->getType().isConstQualified() && |
654 | !Var->getType().isVolatileQualified() && |
655 | !Var->isInline() && |
656 | !isExportedFromModuleInterfaceUnit(Var) && |
657 | !isa<VarTemplateSpecializationDecl>(Var) && |
658 | !Var->getDescribedVarTemplate()) { |
659 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
660 | if (PrevVar) |
661 | return getLVForDecl(PrevVar, computation); |
662 | |
663 | if (Var->getStorageClass() != SC_Extern && |
664 | Var->getStorageClass() != SC_PrivateExtern && |
665 | !isSingleLineLanguageLinkage(*Var)) |
666 | return getInternalLinkageFor(Var); |
667 | } |
668 | |
669 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
670 | PrevVar = PrevVar->getPreviousDecl()) { |
671 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
672 | Var->getStorageClass() == SC_None) |
673 | return getDeclLinkageAndVisibility(PrevVar); |
674 | |
675 | if (PrevVar->getStorageClass() == SC_Static) |
676 | return getInternalLinkageFor(Var); |
677 | } |
678 | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { |
679 | |
680 | const VarDecl *VD = IFD->getVarDecl(); |
681 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); |
682 | return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); |
683 | } |
684 | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); |
685 | |
686 | |
687 | |
688 | if (D->isInAnonymousNamespace()) { |
689 | const auto *Var = dyn_cast<VarDecl>(D); |
690 | const auto *Func = dyn_cast<FunctionDecl>(D); |
691 | |
692 | |
693 | |
694 | |
695 | |
696 | |
697 | |
698 | if ((!Var || !isFirstInExternCContext(Var)) && |
699 | (!Func || !isFirstInExternCContext(Func))) |
700 | return getInternalLinkageFor(D); |
701 | } |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | |
708 | |
709 | LinkageInfo LV = getExternalLinkageFor(D); |
710 | |
711 | if (!hasExplicitVisibilityAlready(computation)) { |
712 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
713 | LV.mergeVisibility(*Vis, true); |
714 | } else { |
715 | |
716 | |
717 | for (const DeclContext *DC = D->getDeclContext(); |
718 | !isa<TranslationUnitDecl>(DC); |
719 | DC = DC->getParent()) { |
720 | const auto *ND = dyn_cast<NamespaceDecl>(DC); |
721 | if (!ND) continue; |
722 | if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { |
723 | LV.mergeVisibility(*Vis, true); |
724 | break; |
725 | } |
726 | } |
727 | } |
728 | |
729 | |
730 | if (!LV.isVisibilityExplicit()) { |
731 | |
732 | Visibility globalVisibility = |
733 | computation.isValueVisibility() |
734 | ? Context.getLangOpts().getValueVisibilityMode() |
735 | : Context.getLangOpts().getTypeVisibilityMode(); |
736 | LV.mergeVisibility(globalVisibility, false); |
737 | |
738 | |
739 | |
740 | if (useInlineVisibilityHidden(D)) |
741 | LV.mergeVisibility(HiddenVisibility, false); |
742 | } |
743 | } |
744 | |
745 | |
746 | |
747 | |
748 | |
749 | |
750 | |
751 | |
752 | |
753 | |
754 | |
755 | |
756 | |
757 | |
758 | |
759 | |
760 | |
761 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
762 | |
763 | |
764 | |
765 | |
766 | |
767 | |
768 | |
769 | |
770 | |
771 | |
772 | |
773 | |
774 | |
775 | |
776 | |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && |
784 | !IgnoreVarTypeLinkage) { |
785 | LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); |
786 | if (!isExternallyVisible(TypeLV.getLinkage())) |
787 | return LinkageInfo::uniqueExternal(); |
788 | if (!LV.isVisibilityExplicit()) |
789 | LV.mergeVisibility(TypeLV); |
790 | } |
791 | |
792 | if (Var->getStorageClass() == SC_PrivateExtern) |
793 | LV.mergeVisibility(HiddenVisibility, true); |
794 | |
795 | |
796 | |
797 | |
798 | |
799 | |
800 | |
801 | |
802 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
803 | mergeTemplateLV(LV, spec, computation); |
804 | } |
805 | |
806 | |
807 | } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
808 | |
809 | |
810 | |
811 | |
812 | |
813 | if (Function->getStorageClass() == SC_PrivateExtern) |
814 | LV.mergeVisibility(HiddenVisibility, true); |
815 | |
816 | |
817 | |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | |
824 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { |
825 | |
826 | |
827 | QualType TypeAsWritten = Function->getType(); |
828 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
829 | TypeAsWritten = TSI->getType(); |
830 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
831 | return LinkageInfo::uniqueExternal(); |
832 | } |
833 | |
834 | |
835 | |
836 | |
837 | if (FunctionTemplateSpecializationInfo *specInfo |
838 | = Function->getTemplateSpecializationInfo()) { |
839 | mergeTemplateLV(LV, Function, specInfo, computation); |
840 | } |
841 | |
842 | |
843 | |
844 | |
845 | |
846 | |
847 | |
848 | } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { |
849 | |
850 | if (!Tag->hasNameForLinkage()) |
851 | return LinkageInfo::none(); |
852 | |
853 | |
854 | |
855 | |
856 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
857 | mergeTemplateLV(LV, spec, computation); |
858 | } |
859 | |
860 | |
861 | |
862 | } else if (isa<EnumConstantDecl>(D)) { |
863 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
864 | computation); |
865 | if (!isExternalFormalLinkage(EnumLV.getLinkage())) |
866 | return LinkageInfo::none(); |
867 | LV.merge(EnumLV); |
868 | |
869 | |
870 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
871 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
872 | LinkageInfo tempLV = |
873 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
874 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
875 | |
876 | |
877 | |
878 | |
879 | |
880 | |
881 | } else if (isa<NamespaceDecl>(D)) { |
882 | return LV; |
883 | |
884 | |
885 | |
886 | } else if (isa<ObjCInterfaceDecl>(D)) { |
887 | |
888 | |
889 | } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
890 | |
891 | |
892 | if (!TD->getAnonDeclWithTypedefName(true)) |
893 | return LinkageInfo::none(); |
894 | |
895 | } else if (isa<MSGuidDecl>(D)) { |
896 | |
897 | |
898 | |
899 | |
900 | } else { |
901 | return LinkageInfo::none(); |
902 | } |
903 | |
904 | |
905 | |
906 | if (!isExternallyVisible(LV.getLinkage())) |
907 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
908 | |
909 | |
910 | if (Context.getLangOpts().OpenMP && Context.getLangOpts().OpenMPIsDevice) |
911 | LV.mergeVisibility(HiddenVisibility, false); |
912 | |
913 | return LV; |
914 | } |
915 | |
916 | LinkageInfo |
917 | LinkageComputer::getLVForClassMember(const NamedDecl *D, |
918 | LVComputationKind computation, |
919 | bool IgnoreVarTypeLinkage) { |
920 | |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | |
928 | |
929 | |
930 | if (!(isa<CXXMethodDecl>(D) || |
931 | isa<VarDecl>(D) || |
932 | isa<FieldDecl>(D) || |
933 | isa<IndirectFieldDecl>(D) || |
934 | isa<TagDecl>(D) || |
935 | isa<TemplateDecl>(D))) |
936 | return LinkageInfo::none(); |
937 | |
938 | LinkageInfo LV; |
939 | |
940 | |
941 | if (!hasExplicitVisibilityAlready(computation)) { |
942 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
943 | LV.mergeVisibility(*Vis, true); |
944 | |
945 | |
946 | |
947 | |
948 | |
949 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
950 | LV.mergeVisibility(HiddenVisibility, false); |
951 | } |
952 | |
953 | |
954 | |
955 | |
956 | LVComputationKind classComputation = computation; |
957 | if (LV.isVisibilityExplicit()) |
958 | classComputation = withExplicitVisibilityAlready(computation); |
959 | |
960 | LinkageInfo classLV = |
961 | getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); |
962 | |
963 | |
964 | |
965 | if (!isExternallyVisible(classLV.getLinkage())) |
966 | return classLV; |
967 | |
968 | |
969 | |
970 | |
971 | |
972 | |
973 | const NamedDecl *explicitSpecSuppressor = nullptr; |
974 | |
975 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
976 | |
977 | |
978 | QualType TypeAsWritten = MD->getType(); |
979 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
980 | TypeAsWritten = TSI->getType(); |
981 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
982 | return LinkageInfo::uniqueExternal(); |
983 | |
984 | |
985 | |
986 | if (FunctionTemplateSpecializationInfo *spec |
987 | = MD->getTemplateSpecializationInfo()) { |
988 | mergeTemplateLV(LV, MD, spec, computation); |
989 | if (spec->isExplicitSpecialization()) { |
990 | explicitSpecSuppressor = MD; |
991 | } else if (isExplicitMemberSpecialization(spec->getTemplate())) { |
992 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
993 | } |
994 | } else if (isExplicitMemberSpecialization(MD)) { |
995 | explicitSpecSuppressor = MD; |
996 | } |
997 | |
998 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
999 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
1000 | mergeTemplateLV(LV, spec, computation); |
1001 | if (spec->isExplicitSpecialization()) { |
1002 | explicitSpecSuppressor = spec; |
1003 | } else { |
1004 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
1005 | if (isExplicitMemberSpecialization(temp)) { |
1006 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
1007 | } |
1008 | } |
1009 | } else if (isExplicitMemberSpecialization(RD)) { |
1010 | explicitSpecSuppressor = RD; |
1011 | } |
1012 | |
1013 | |
1014 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
1015 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) |
1016 | mergeTemplateLV(LV, spec, computation); |
1017 | |
1018 | |
1019 | |
1020 | if (!IgnoreVarTypeLinkage) { |
1021 | LinkageInfo typeLV = getLVForType(*VD->getType(), computation); |
1022 | |
1023 | |
1024 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
1025 | LV.mergeVisibility(typeLV); |
1026 | LV.mergeExternalVisibility(typeLV); |
1027 | } |
1028 | |
1029 | if (isExplicitMemberSpecialization(VD)) { |
1030 | explicitSpecSuppressor = VD; |
1031 | } |
1032 | |
1033 | |
1034 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
1035 | bool considerVisibility = |
1036 | (!LV.isVisibilityExplicit() && |
1037 | !classLV.isVisibilityExplicit() && |
1038 | !hasExplicitVisibilityAlready(computation)); |
1039 | LinkageInfo tempLV = |
1040 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
1041 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
1042 | |
1043 | if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { |
1044 | if (isExplicitMemberSpecialization(redeclTemp)) { |
1045 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
1046 | } |
1047 | } |
1048 | } |
1049 | |
1050 | |
1051 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
1052 | |
1053 | |
1054 | |
1055 | bool considerClassVisibility = true; |
1056 | if (explicitSpecSuppressor && |
1057 | |
1058 | LV.isVisibilityExplicit() && |
1059 | classLV.getVisibility() != DefaultVisibility && |
1060 | hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { |
1061 | considerClassVisibility = false; |
1062 | } |
1063 | |
1064 | |
1065 | LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); |
1066 | return LV; |
1067 | } |
1068 | |
1069 | void NamedDecl::anchor() {} |
1070 | |
1071 | bool NamedDecl::isLinkageValid() const { |
1072 | if (!hasCachedLinkage()) |
1073 | return true; |
1074 | |
1075 | Linkage L = LinkageComputer{} |
1076 | .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) |
1077 | .getLinkage(); |
1078 | return L == getCachedLinkage(); |
1079 | } |
1080 | |
1081 | ReservedIdentifierStatus |
1082 | NamedDecl::isReserved(const LangOptions &LangOpts) const { |
1083 | const IdentifierInfo *II = getIdentifier(); |
1084 | |
1085 | |
1086 | |
1087 | if (!II) |
1088 | return ReservedIdentifierStatus::NotReserved; |
1089 | |
1090 | ReservedIdentifierStatus Status = II->isReserved(LangOpts); |
1091 | if (Status == ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope) { |
1092 | |
1093 | if (isa<ParmVarDecl>(this) || isTemplateParameter()) |
1094 | return ReservedIdentifierStatus::NotReserved; |
1095 | const DeclContext *DC = getDeclContext()->getRedeclContext(); |
1096 | if (!DC->isTranslationUnit()) |
1097 | return ReservedIdentifierStatus::NotReserved; |
1098 | } |
1099 | |
1100 | return Status; |
1101 | } |
1102 | |
1103 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
1104 | StringRef name = getName(); |
1105 | if (name.empty()) return SFF_None; |
1106 | |
1107 | if (name.front() == 'C') |
1108 | if (name == "CFStringCreateWithFormat" || |
1109 | name == "CFStringCreateWithFormatAndArguments" || |
1110 | name == "CFStringAppendFormat" || |
1111 | name == "CFStringAppendFormatAndArguments") |
1112 | return SFF_CFString; |
1113 | return SFF_None; |
1114 | } |
1115 | |
1116 | Linkage NamedDecl::getLinkageInternal() const { |
1117 | |
1118 | |
1119 | return LinkageComputer{} |
1120 | .getLVForDecl(this, LVComputationKind::forLinkageOnly()) |
1121 | .getLinkage(); |
1122 | } |
1123 | |
1124 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
1125 | return LinkageComputer{}.getDeclLinkageAndVisibility(this); |
1126 | } |
1127 | |
1128 | static Optional<Visibility> |
1129 | getExplicitVisibilityAux(const NamedDecl *ND, |
1130 | NamedDecl::ExplicitVisibilityKind kind, |
1131 | bool IsMostRecent) { |
1132 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
1133 | |
1134 | |
1135 | if (Optional<Visibility> V = getVisibilityOf(ND, kind)) |
1136 | return V; |
1137 | |
1138 | |
1139 | |
1140 | if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { |
1141 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
1142 | if (InstantiatedFrom) |
1143 | return getVisibilityOf(InstantiatedFrom, kind); |
1144 | } |
1145 | |
1146 | |
1147 | |
1148 | |
1149 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
1150 | |
1151 | |
1152 | const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); |
1153 | while (TD != nullptr) { |
1154 | auto Vis = getVisibilityOf(TD, kind); |
1155 | if (Vis != None) |
1156 | return Vis; |
1157 | TD = TD->getPreviousDecl(); |
1158 | } |
1159 | return None; |
1160 | } |
1161 | |
1162 | |
1163 | if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { |
1164 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
1165 | if (MostRecent != ND) |
1166 | return getExplicitVisibilityAux(MostRecent, kind, true); |
1167 | } |
1168 | |
1169 | if (const auto *Var = dyn_cast<VarDecl>(ND)) { |
1170 | if (Var->isStaticDataMember()) { |
1171 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
1172 | if (InstantiatedFrom) |
1173 | return getVisibilityOf(InstantiatedFrom, kind); |
1174 | } |
1175 | |
1176 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) |
1177 | return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
1178 | kind); |
1179 | |
1180 | return None; |
1181 | } |
1182 | |
1183 | if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { |
1184 | |
1185 | |
1186 | if (FunctionTemplateSpecializationInfo *templateInfo |
1187 | = fn->getTemplateSpecializationInfo()) |
1188 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
1189 | kind); |
1190 | |
1191 | |
1192 | |
1193 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
1194 | if (InstantiatedFrom) |
1195 | return getVisibilityOf(InstantiatedFrom, kind); |
1196 | |
1197 | return None; |
1198 | } |
1199 | |
1200 | |
1201 | if (const auto *TD = dyn_cast<TemplateDecl>(ND)) |
1202 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
1203 | |
1204 | return None; |
1205 | } |
1206 | |
1207 | Optional<Visibility> |
1208 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
1209 | return getExplicitVisibilityAux(this, kind, false); |
1210 | } |
1211 | |
1212 | LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, |
1213 | Decl *ContextDecl, |
1214 | LVComputationKind computation) { |
1215 | |
1216 | const NamedDecl *Owner; |
1217 | if (!ContextDecl) |
1218 | Owner = dyn_cast<NamedDecl>(DC); |
1219 | else if (isa<ParmVarDecl>(ContextDecl)) |
1220 | Owner = |
1221 | dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); |
1222 | else |
1223 | Owner = cast<NamedDecl>(ContextDecl); |
1224 | |
1225 | if (!Owner) |
1226 | return LinkageInfo::none(); |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | auto *VD = dyn_cast<VarDecl>(Owner); |
1233 | LinkageInfo OwnerLV = |
1234 | VD && VD->getType()->getContainedDeducedType() |
1235 | ? computeLVForDecl(Owner, computation, true) |
1236 | : getLVForDecl(Owner, computation); |
1237 | |
1238 | |
1239 | |
1240 | if (!isExternallyVisible(OwnerLV.getLinkage())) |
1241 | return LinkageInfo::none(); |
1242 | return LinkageInfo(VisibleNoLinkage, OwnerLV.getVisibility(), |
1243 | OwnerLV.isVisibilityExplicit()); |
1244 | } |
1245 | |
1246 | LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, |
1247 | LVComputationKind computation) { |
1248 | if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
1249 | if (Function->isInAnonymousNamespace() && |
1250 | !isFirstInExternCContext(Function)) |
1251 | return getInternalLinkageFor(Function); |
1252 | |
1253 | |
1254 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
1255 | return getInternalLinkageFor(Function); |
1256 | |
1257 | LinkageInfo LV; |
1258 | if (!hasExplicitVisibilityAlready(computation)) { |
1259 | if (Optional<Visibility> Vis = |
1260 | getExplicitVisibility(Function, computation)) |
1261 | LV.mergeVisibility(*Vis, true); |
1262 | } |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | return LV; |
1269 | } |
1270 | |
1271 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
1272 | if (Var->hasExternalStorage()) { |
1273 | if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) |
1274 | return getInternalLinkageFor(Var); |
1275 | |
1276 | LinkageInfo LV; |
1277 | if (Var->getStorageClass() == SC_PrivateExtern) |
1278 | LV.mergeVisibility(HiddenVisibility, true); |
1279 | else if (!hasExplicitVisibilityAlready(computation)) { |
1280 | if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) |
1281 | LV.mergeVisibility(*Vis, true); |
1282 | } |
1283 | |
1284 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
1285 | LinkageInfo PrevLV = getLVForDecl(Prev, computation); |
1286 | if (PrevLV.getLinkage()) |
1287 | LV.setLinkage(PrevLV.getLinkage()); |
1288 | LV.mergeVisibility(PrevLV); |
1289 | } |
1290 | |
1291 | return LV; |
1292 | } |
1293 | |
1294 | if (!Var->isStaticLocal()) |
1295 | return LinkageInfo::none(); |
1296 | } |
1297 | |
1298 | ASTContext &Context = D->getASTContext(); |
1299 | if (!Context.getLangOpts().CPlusPlus) |
1300 | return LinkageInfo::none(); |
1301 | |
1302 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
1303 | if (!OuterD || OuterD->isInvalidDecl()) |
1304 | return LinkageInfo::none(); |
1305 | |
1306 | LinkageInfo LV; |
1307 | if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { |
1308 | if (!BD->getBlockManglingNumber()) |
1309 | return LinkageInfo::none(); |
1310 | |
1311 | LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), |
1312 | BD->getBlockManglingContextDecl(), computation); |
1313 | } else { |
1314 | const auto *FD = cast<FunctionDecl>(OuterD); |
1315 | if (!FD->isInlined() && |
1316 | !isTemplateInstantiation(FD->getTemplateSpecializationKind())) |
1317 | return LinkageInfo::none(); |
1318 | |
1319 | |
1320 | |
1321 | |
1322 | LV = getLVForDecl(FD, computation); |
1323 | if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && |
1324 | !LV.isVisibilityExplicit() && |
1325 | !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) { |
1326 | assert(cast<VarDecl>(D)->isStaticLocal()); |
1327 | |
1328 | |
1329 | |
1330 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
1331 | LV = getLVForDecl(MD->getParent(), computation); |
1332 | if (!LV.isVisibilityExplicit()) { |
1333 | Visibility globalVisibility = |
1334 | computation.isValueVisibility() |
1335 | ? Context.getLangOpts().getValueVisibilityMode() |
1336 | : Context.getLangOpts().getTypeVisibilityMode(); |
1337 | return LinkageInfo(VisibleNoLinkage, globalVisibility, |
1338 | false); |
1339 | } |
1340 | } |
1341 | } |
1342 | if (!isExternallyVisible(LV.getLinkage())) |
1343 | return LinkageInfo::none(); |
1344 | return LinkageInfo(VisibleNoLinkage, LV.getVisibility(), |
1345 | LV.isVisibilityExplicit()); |
1346 | } |
1347 | |
1348 | LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, |
1349 | LVComputationKind computation, |
1350 | bool IgnoreVarTypeLinkage) { |
1351 | |
1352 | if (D->hasAttr<InternalLinkageAttr>()) |
1353 | return getInternalLinkageFor(D); |
1354 | |
1355 | |
1356 | |
1357 | switch (D->getKind()) { |
1358 | default: |
1359 | break; |
1360 | |
1361 | |
1362 | |
1363 | |
1364 | |
1365 | |
1366 | |
1367 | case Decl::ImplicitParam: |
1368 | case Decl::Label: |
1369 | case Decl::NamespaceAlias: |
1370 | case Decl::ParmVar: |
1371 | case Decl::Using: |
1372 | case Decl::UsingEnum: |
1373 | case Decl::UsingShadow: |
1374 | case Decl::UsingDirective: |
1375 | return LinkageInfo::none(); |
1376 | |
1377 | case Decl::EnumConstant: |
1378 | |
1379 | if (D->getASTContext().getLangOpts().CPlusPlus) |
1380 | return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); |
1381 | return LinkageInfo::visible_none(); |
1382 | |
1383 | case Decl::Typedef: |
1384 | case Decl::TypeAlias: |
1385 | |
1386 | |
1387 | if (!cast<TypedefNameDecl>(D) |
1388 | ->getAnonDeclWithTypedefName(true)) |
1389 | return LinkageInfo::none(); |
1390 | break; |
1391 | |
1392 | case Decl::TemplateTemplateParm: |
1393 | case Decl::NonTypeTemplateParm: |
1394 | case Decl::ObjCAtDefsField: |
1395 | case Decl::ObjCCategory: |
1396 | case Decl::ObjCCategoryImpl: |
1397 | case Decl::ObjCCompatibleAlias: |
1398 | case Decl::ObjCImplementation: |
1399 | case Decl::ObjCMethod: |
1400 | case Decl::ObjCProperty: |
1401 | case Decl::ObjCPropertyImpl: |
1402 | case Decl::ObjCProtocol: |
1403 | return getExternalLinkageFor(D); |
1404 | |
1405 | case Decl::CXXRecord: { |
1406 | const auto *Record = cast<CXXRecordDecl>(D); |
1407 | if (Record->isLambda()) { |
1408 | if (Record->hasKnownLambdaInternalLinkage() || |
1409 | !Record->getLambdaManglingNumber()) { |
1410 | |
1411 | return getInternalLinkageFor(D); |
1412 | } |
1413 | |
1414 | return getLVForClosure( |
1415 | Record->getDeclContext()->getRedeclContext(), |
1416 | Record->getLambdaContextDecl(), computation); |
1417 | } |
1418 | |
1419 | break; |
1420 | } |
1421 | |
1422 | case Decl::TemplateParamObject: { |
1423 | |
1424 | |
1425 | auto *TPO = cast<TemplateParamObjectDecl>(D); |
1426 | LinkageInfo LV = getLVForType(*TPO->getType(), computation); |
1427 | LV.merge(getLVForValue(TPO->getValue(), computation)); |
1428 | return LV; |
1429 | } |
1430 | } |
1431 | |
1432 | |
1433 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
1434 | return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); |
1435 | |
1436 | |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | |
1442 | |
1443 | if (D->getDeclContext()->isRecord()) |
1444 | return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); |
1445 | |
1446 | |
1447 | |
1448 | |
1449 | |
1450 | |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | if (D->getDeclContext()->isFunctionOrMethod()) |
1458 | return getLVForLocalDecl(D, computation); |
1459 | |
1460 | |
1461 | |
1462 | return LinkageInfo::none(); |
1463 | } |
1464 | |
1465 | |
1466 | LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, |
1467 | LVComputationKind computation) { |
1468 | |
1469 | if (D->hasAttr<InternalLinkageAttr>()) |
1470 | return getInternalLinkageFor(D); |
1471 | |
1472 | if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) |
1473 | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
1474 | |
1475 | if (llvm::Optional<LinkageInfo> LI = lookup(D, computation)) |
1476 | return *LI; |
1477 | |
1478 | LinkageInfo LV = computeLVForDecl(D, computation); |
1479 | if (D->hasCachedLinkage()) |
1480 | assert(D->getCachedLinkage() == LV.getLinkage()); |
1481 | |
1482 | D->setCachedLinkage(LV.getLinkage()); |
1483 | cache(D, computation, LV); |
1484 | |
1485 | #ifndef NDEBUG |
1486 | |
1487 | |
1488 | |
1489 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
1490 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
1491 | return LV; |
1492 | |
1493 | |
1494 | |
1495 | |
1496 | NamedDecl *Old = nullptr; |
1497 | for (auto I : D->redecls()) { |
1498 | auto *T = cast<NamedDecl>(I); |
1499 | if (T == D) |
1500 | continue; |
1501 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
1502 | Old = T; |
1503 | break; |
1504 | } |
1505 | } |
1506 | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
1507 | #endif |
1508 | |
1509 | return LV; |
1510 | } |
1511 | |
1512 | LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { |
1513 | NamedDecl::ExplicitVisibilityKind EK = usesTypeVisibility(D) |
1514 | ? NamedDecl::VisibilityForType |
1515 | : NamedDecl::VisibilityForValue; |
1516 | LVComputationKind CK(EK); |
1517 | return getLVForDecl(D, D->getASTContext().getLangOpts().IgnoreXCOFFVisibility |
1518 | ? CK.forLinkageOnly() |
1519 | : CK); |
1520 | } |
1521 | |
1522 | Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { |
1523 | Module *M = getOwningModule(); |
1524 | if (!M) |
1525 | return nullptr; |
1526 | |
1527 | switch (M->Kind) { |
1528 | case Module::ModuleMapModule: |
1529 | |
1530 | return nullptr; |
1531 | |
1532 | case Module::ModuleInterfaceUnit: |
1533 | return M; |
1534 | |
1535 | case Module::GlobalModuleFragment: { |
1536 | |
1537 | |
1538 | |
1539 | |
1540 | if (IgnoreLinkage) |
1541 | return nullptr; |
1542 | bool InternalLinkage; |
1543 | if (auto *ND = dyn_cast<NamedDecl>(this)) |
1544 | InternalLinkage = !ND->hasExternalFormalLinkage(); |
1545 | else { |
1546 | auto *NSD = dyn_cast<NamespaceDecl>(this); |
1547 | InternalLinkage = (NSD && NSD->isAnonymousNamespace()) || |
1548 | isInAnonymousNamespace(); |
1549 | } |
1550 | return InternalLinkage ? M->Parent : nullptr; |
1551 | } |
1552 | |
1553 | case Module::PrivateModuleFragment: |
1554 | |
1555 | |
1556 | return M->Parent; |
1557 | } |
1558 | |
1559 | llvm_unreachable("unknown module kind"); |
1560 | } |
1561 | |
1562 | void NamedDecl::printName(raw_ostream &os) const { |
1563 | os << Name; |
1564 | } |
1565 | |
1566 | std::string NamedDecl::getQualifiedNameAsString() const { |
1567 | std::string QualName; |
1568 | llvm::raw_string_ostream OS(QualName); |
1569 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
1570 | return OS.str(); |
1571 | } |
1572 | |
1573 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
1574 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
1575 | } |
1576 | |
1577 | void NamedDecl::printQualifiedName(raw_ostream &OS, |
1578 | const PrintingPolicy &P) const { |
1579 | if (getDeclContext()->isFunctionOrMethod()) { |
1580 | |
1581 | printName(OS); |
1582 | return; |
1583 | } |
1584 | printNestedNameSpecifier(OS, P); |
1585 | if (getDeclName()) |
1586 | OS << *this; |
1587 | else { |
1588 | |
1589 | |
1590 | SmallString<64> NameBuffer; |
1591 | llvm::raw_svector_ostream NameOS(NameBuffer); |
1592 | printName(NameOS); |
1593 | if (NameBuffer.empty()) |
1594 | OS << "(anonymous)"; |
1595 | else |
1596 | OS << NameBuffer; |
1597 | } |
1598 | } |
1599 | |
1600 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS) const { |
1601 | printNestedNameSpecifier(OS, getASTContext().getPrintingPolicy()); |
1602 | } |
1603 | |
1604 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS, |
1605 | const PrintingPolicy &P) const { |
1606 | const DeclContext *Ctx = getDeclContext(); |
1607 | |
1608 | |
1609 | |
1610 | if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) { |
1611 | if (auto *ID = MD->getClassInterface()) |
1612 | Ctx = ID; |
1613 | } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { |
1614 | if (auto *MD = PD->getGetterMethodDecl()) |
1615 | if (auto *ID = MD->getClassInterface()) |
1616 | Ctx = ID; |
1617 | } else if (auto *ID = dyn_cast<ObjCIvarDecl>(this)) { |
1618 | if (auto *CI = ID->getContainingInterface()) |
1619 | Ctx = CI; |
1620 | } |
1621 | |
1622 | if (Ctx->isFunctionOrMethod()) |
1623 | return; |
1624 | |
1625 | using ContextsTy = SmallVector<const DeclContext *, 8>; |
1626 | ContextsTy Contexts; |
1627 | |
1628 | |
1629 | DeclarationName NameInScope = getDeclName(); |
1630 | for (; Ctx; Ctx = Ctx->getParent()) { |
1631 | |
1632 | if (P.SuppressUnwrittenScope && isa<NamespaceDecl>(Ctx) && |
1633 | cast<NamespaceDecl>(Ctx)->isAnonymousNamespace()) |
1634 | continue; |
1635 | |
1636 | |
1637 | if (P.SuppressInlineNamespace && Ctx->isInlineNamespace() && NameInScope && |
1638 | cast<NamespaceDecl>(Ctx)->isRedundantInlineQualifierFor(NameInScope)) |
1639 | continue; |
1640 | |
1641 | |
1642 | const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx); |
1643 | if (!ND) |
1644 | continue; |
1645 | |
1646 | Contexts.push_back(Ctx); |
1647 | NameInScope = ND->getDeclName(); |
1648 | } |
1649 | |
1650 | for (unsigned I = Contexts.size(); I != 0; --I) { |
1651 | const DeclContext *DC = Contexts[I - 1]; |
1652 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
1653 | OS << Spec->getName(); |
1654 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
1655 | printTemplateArgumentList( |
1656 | OS, TemplateArgs.asArray(), P, |
1657 | Spec->getSpecializedTemplate()->getTemplateParameters()); |
1658 | } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
1659 | if (ND->isAnonymousNamespace()) { |
1660 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
1661 | : "(anonymous namespace)"); |
1662 | } |
1663 | else |
1664 | OS << *ND; |
1665 | } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { |
1666 | if (!RD->getIdentifier()) |
1667 | OS << "(anonymous " << RD->getKindName() << ')'; |
1668 | else |
1669 | OS << *RD; |
1670 | } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { |
1671 | const FunctionProtoType *FT = nullptr; |
1672 | if (FD->hasWrittenPrototype()) |
1673 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
1674 | |
1675 | OS << *FD << '('; |
1676 | if (FT) { |
1677 | unsigned NumParams = FD->getNumParams(); |
1678 | for (unsigned i = 0; i < NumParams; ++i) { |
1679 | if (i) |
1680 | OS << ", "; |
1681 | OS << FD->getParamDecl(i)->getType().stream(P); |
1682 | } |
1683 | |
1684 | if (FT->isVariadic()) { |
1685 | if (NumParams > 0) |
1686 | OS << ", "; |
1687 | OS << "..."; |
1688 | } |
1689 | } |
1690 | OS << ')'; |
1691 | } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { |
1692 | |
1693 | |
1694 | |
1695 | |
1696 | |
1697 | |
1698 | |
1699 | if (ED->isScoped()) |
1700 | OS << *ED; |
1701 | else |
1702 | continue; |
1703 | } else { |
1704 | OS << *cast<NamedDecl>(DC); |
1705 | } |
1706 | OS << "::"; |
1707 | } |
1708 | } |
1709 | |
1710 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
1711 | const PrintingPolicy &Policy, |
1712 | bool Qualified) const { |
1713 | if (Qualified) |
1714 | printQualifiedName(OS, Policy); |
1715 | else |
1716 | printName(OS); |
1717 | } |
1718 | |
1719 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
1720 | return true; |
1721 | } |
1722 | static bool isRedeclarableImpl(...) { return false; } |
1723 | static bool isRedeclarable(Decl::Kind K) { |
1724 | switch (K) { |
1725 | #define DECL(Type, Base) \ |
1726 | case Decl::Type: \ |
1727 | return isRedeclarableImpl((Type##Decl *)nullptr); |
1728 | #define ABSTRACT_DECL(DECL) |
1729 | #include "clang/AST/DeclNodes.inc" |
1730 | } |
1731 | llvm_unreachable("unknown decl kind"); |
1732 | } |
1733 | |
1734 | bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { |
1735 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
1736 | |
1737 | |
1738 | |
1739 | if (OldD->isFromASTFile() && isFromASTFile()) |
1740 | return false; |
1741 | |
1742 | |
1743 | if (OldD->getKind() != getKind()) |
1744 | return false; |
1745 | |
1746 | |
1747 | if (isa<ObjCMethodDecl>(this)) |
1748 | return false; |
1749 | |
1750 | |
1751 | |
1752 | if (isa<ParmVarDecl>(this)) |
1753 | return true; |
1754 | |
1755 | |
1756 | |
1757 | |
1758 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
1759 | OldD->getDeclContext()->getRedeclContext())) |
1760 | return false; |
1761 | |
1762 | |
1763 | |
1764 | if (auto *UD = dyn_cast<UsingDecl>(this)) { |
1765 | ASTContext &Context = getASTContext(); |
1766 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
1767 | Context.getCanonicalNestedNameSpecifier( |
1768 | cast<UsingDecl>(OldD)->getQualifier()); |
1769 | } |
1770 | if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
1771 | ASTContext &Context = getASTContext(); |
1772 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
1773 | Context.getCanonicalNestedNameSpecifier( |
1774 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
1775 | } |
1776 | |
1777 | if (isRedeclarable(getKind())) { |
1778 | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
1779 | return false; |
1780 | |
1781 | if (IsKnownNewer) |
1782 | return true; |
1783 | |
1784 | |
1785 | |
1786 | |
1787 | for (auto D : redecls()) { |
1788 | if (D == OldD) |
1789 | break; |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | |
1795 | if (D->isCanonicalDecl()) |
1796 | return false; |
1797 | } |
1798 | |
1799 | |
1800 | |
1801 | return true; |
1802 | } |
1803 | |
1804 | |
1805 | |
1806 | |
1807 | return false; |
1808 | } |
1809 | |
1810 | bool NamedDecl::hasLinkage() const { |
1811 | return getFormalLinkage() != NoLinkage; |
1812 | } |
1813 | |
1814 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
1815 | NamedDecl *ND = this; |
1816 | while (auto *UD = dyn_cast<UsingShadowDecl>(ND)) |
1817 | ND = UD->getTargetDecl(); |
1818 | |
1819 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
1820 | return AD->getClassInterface(); |
1821 | |
1822 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) |
1823 | return AD->getNamespace(); |
1824 | |
1825 | return ND; |
1826 | } |
1827 | |
1828 | bool NamedDecl::isCXXInstanceMember() const { |
1829 | if (!isCXXClassMember()) |
1830 | return false; |
1831 | |
1832 | const NamedDecl *D = this; |
1833 | if (isa<UsingShadowDecl>(D)) |
1834 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
1835 | |
1836 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
1837 | return true; |
1838 | if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) |
1839 | return MD->isInstance(); |
1840 | return false; |
1841 | } |
1842 | |
1843 | |
1844 | |
1845 | |
1846 | |
1847 | template <typename DeclT> |
1848 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
1849 | if (decl->getNumTemplateParameterLists() > 0) |
1850 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
1851 | return decl->getInnerLocStart(); |
1852 | } |
1853 | |
1854 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
1855 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
1856 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
1857 | return SourceLocation(); |
1858 | } |
1859 | |
1860 | SourceLocation DeclaratorDecl::getTypeSpecEndLoc() const { |
1861 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
1862 | if (TSI) return TSI->getTypeLoc().getEndLoc(); |
1863 | return SourceLocation(); |
1864 | } |
1865 | |
1866 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
1867 | if (QualifierLoc) { |
1868 | |
1869 | if (!hasExtInfo()) { |
1870 | |
1871 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
1872 | |
1873 | DeclInfo = new (getASTContext()) ExtInfo; |
1874 | |
1875 | getExtInfo()->TInfo = savedTInfo; |
1876 | } |
1877 | |
1878 | getExtInfo()->QualifierLoc = QualifierLoc; |
1879 | } else if (hasExtInfo()) { |
1880 | |
1881 | getExtInfo()->QualifierLoc = QualifierLoc; |
1882 | } |
1883 | } |
1884 | |
1885 | void DeclaratorDecl::setTrailingRequiresClause(Expr *TrailingRequiresClause) { |
1886 | assert(TrailingRequiresClause); |
1887 | |
1888 | if (!hasExtInfo()) { |
1889 | |
1890 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
1891 | |
1892 | DeclInfo = new (getASTContext()) ExtInfo; |
1893 | |
1894 | getExtInfo()->TInfo = savedTInfo; |
1895 | } |
1896 | |
1897 | getExtInfo()->TrailingRequiresClause = TrailingRequiresClause; |
1898 | } |
1899 | |
1900 | void DeclaratorDecl::setTemplateParameterListsInfo( |
1901 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
1902 | assert(!TPLists.empty()); |
1903 | |
1904 | if (!hasExtInfo()) { |
1905 | |
1906 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
1907 | |
1908 | DeclInfo = new (getASTContext()) ExtInfo; |
1909 | |
1910 | getExtInfo()->TInfo = savedTInfo; |
1911 | } |
1912 | |
1913 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
1914 | } |
1915 | |
1916 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
1917 | return getTemplateOrInnerLocStart(this); |
1918 | } |
1919 | |
1920 | |
1921 | |
1922 | static bool typeIsPostfix(QualType QT) { |
1923 | while (true) { |
1924 | const Type* T = QT.getTypePtr(); |
1925 | switch (T->getTypeClass()) { |
1926 | default: |
1927 | return false; |
1928 | case Type::Pointer: |
1929 | QT = cast<PointerType>(T)->getPointeeType(); |
1930 | break; |
1931 | case Type::BlockPointer: |
1932 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
1933 | break; |
1934 | case Type::MemberPointer: |
1935 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
1936 | break; |
1937 | case Type::LValueReference: |
1938 | case Type::RValueReference: |
1939 | QT = cast<ReferenceType>(T)->getPointeeType(); |
1940 | break; |
1941 | case Type::PackExpansion: |
1942 | QT = cast<PackExpansionType>(T)->getPattern(); |
1943 | break; |
1944 | case Type::Paren: |
1945 | case Type::ConstantArray: |
1946 | case Type::DependentSizedArray: |
1947 | case Type::IncompleteArray: |
1948 | case Type::VariableArray: |
1949 | case Type::FunctionProto: |
1950 | case Type::FunctionNoProto: |
1951 | return true; |
1952 | } |
1953 | } |
1954 | } |
1955 | |
1956 | SourceRange DeclaratorDecl::getSourceRange() const { |
1957 | SourceLocation RangeEnd = getLocation(); |
1958 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
1959 | |
1960 | |
1961 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
1962 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
1963 | } |
1964 | return SourceRange(getOuterLocStart(), RangeEnd); |
1965 | } |
1966 | |
1967 | void QualifierInfo::setTemplateParameterListsInfo( |
1968 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
1969 | |
1970 | if (NumTemplParamLists > 0) { |
1971 | Context.Deallocate(TemplParamLists); |
1972 | TemplParamLists = nullptr; |
1973 | NumTemplParamLists = 0; |
1974 | } |
1975 | |
1976 | if (!TPLists.empty()) { |
1977 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
1978 | NumTemplParamLists = TPLists.size(); |
1979 | std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); |
1980 | } |
1981 | } |
1982 | |
1983 | |
1984 | |
1985 | |
1986 | |
1987 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
1988 | switch (SC) { |
1989 | case SC_None: break; |
1990 | case SC_Auto: return "auto"; |
1991 | case SC_Extern: return "extern"; |
1992 | case SC_PrivateExtern: return "__private_extern__"; |
1993 | case SC_Register: return "register"; |
1994 | case SC_Static: return "static"; |
1995 | } |
1996 | |
1997 | llvm_unreachable("Invalid storage class"); |
1998 | } |
1999 | |
2000 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
2001 | SourceLocation StartLoc, SourceLocation IdLoc, |
2002 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
2003 | StorageClass SC) |
2004 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
2005 | redeclarable_base(C) { |
2006 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
2007 | "VarDeclBitfields too large!"); |
2008 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
2009 | "ParmVarDeclBitfields too large!"); |
2010 | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
2011 | "NonParmVarDeclBitfields too large!"); |
2012 | AllBits = 0; |
2013 | VarDeclBits.SClass = SC; |
2014 | |
2015 | } |
2016 | |
2017 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
2018 | SourceLocation StartL, SourceLocation IdL, |
2019 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
2020 | StorageClass S) { |
2021 | return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S); |
2022 | } |
2023 | |
2024 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
2025 | return new (C, ID) |
2026 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
2027 | QualType(), nullptr, SC_None); |
2028 | } |
2029 | |
2030 | void VarDecl::setStorageClass(StorageClass SC) { |
2031 | assert(isLegalForVariable(SC)); |
2032 | VarDeclBits.SClass = SC; |
2033 | } |
2034 | |
2035 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
2036 | switch (VarDeclBits.TSCSpec) { |
2037 | case TSCS_unspecified: |
2038 | if (!hasAttr<ThreadAttr>() && |
2039 | !(getASTContext().getLangOpts().OpenMPUseTLS && |
2040 | getASTContext().getTargetInfo().isTLSSupported() && |
2041 | hasAttr<OMPThreadPrivateDeclAttr>())) |
2042 | return TLS_None; |
2043 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
2044 | LangOptions::MSVC2015)) || |
2045 | hasAttr<OMPThreadPrivateDeclAttr>()) |
2046 | ? TLS_Dynamic |
2047 | : TLS_Static; |
2048 | case TSCS___thread: |
2049 | case TSCS__Thread_local: |
2050 | return TLS_Static; |
2051 | case TSCS_thread_local: |
2052 | return TLS_Dynamic; |
2053 | } |
2054 | llvm_unreachable("Unknown thread storage class specifier!"); |
2055 | } |
2056 | |
2057 | SourceRange VarDecl::getSourceRange() const { |
2058 | if (const Expr *Init = getInit()) { |
2059 | SourceLocation InitEnd = Init->getEndLoc(); |
2060 | |
2061 | |
2062 | if (InitEnd.isValid() && InitEnd != getLocation()) |
2063 | return SourceRange(getOuterLocStart(), InitEnd); |
2064 | } |
2065 | return DeclaratorDecl::getSourceRange(); |
2066 | } |
2067 | |
2068 | template<typename T> |
2069 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
2070 | |
2071 | |
2072 | if (!D.hasExternalFormalLinkage()) |
2073 | return NoLanguageLinkage; |
2074 | |
2075 | |
2076 | |
2077 | ASTContext &Context = D.getASTContext(); |
2078 | if (!Context.getLangOpts().CPlusPlus) |
2079 | return CLanguageLinkage; |
2080 | |
2081 | |
2082 | |
2083 | |
2084 | const DeclContext *DC = D.getDeclContext(); |
2085 | if (DC->isRecord()) |
2086 | return CXXLanguageLinkage; |
2087 | |
2088 | |
2089 | |
2090 | |
2091 | if (isFirstInExternCContext(&D)) |
2092 | return CLanguageLinkage; |
2093 | return CXXLanguageLinkage; |
2094 | } |
2095 | |
2096 | template<typename T> |
2097 | static bool isDeclExternC(const T &D) { |
2098 | |
2099 | |
2100 | const DeclContext *DC = D.getDeclContext(); |
2101 | if (DC->isRecord()) { |
2102 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
2103 | return false; |
2104 | } |
2105 | |
2106 | return D.getLanguageLinkage() == CLanguageLinkage; |
2107 | } |
2108 | |
2109 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
2110 | return getDeclLanguageLinkage(*this); |
2111 | } |
2112 | |
2113 | bool VarDecl::isExternC() const { |
2114 | return isDeclExternC(*this); |
2115 | } |
2116 | |
2117 | bool VarDecl::isInExternCContext() const { |
2118 | return getLexicalDeclContext()->isExternCContext(); |
2119 | } |
2120 | |
2121 | bool VarDecl::isInExternCXXContext() const { |
2122 | return getLexicalDeclContext()->isExternCXXContext(); |
2123 | } |
2124 | |
2125 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
2126 | |
2127 | VarDecl::DefinitionKind |
2128 | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
2129 | if (isThisDeclarationADemotedDefinition()) |
2130 | return DeclarationOnly; |
2131 | |
2132 | |
2133 | |
2134 | |
2135 | |
2136 | |
2137 | |
2138 | |
2139 | |
2140 | |
2141 | |
2142 | |
2143 | |
2144 | |
2145 | if (isStaticDataMember()) { |
2146 | if (isOutOfLine() && |
2147 | !(getCanonicalDecl()->isInline() && |
2148 | getCanonicalDecl()->isConstexpr()) && |
2149 | (hasInit() || |
2150 | |
2151 | |
2152 | |
2153 | (getFirstDecl()->isOutOfLine() |
2154 | ? getTemplateSpecializationKind() == TSK_Undeclared |
2155 | : getTemplateSpecializationKind() != |
2156 | TSK_ExplicitSpecialization) || |
2157 | isa<VarTemplatePartialSpecializationDecl>(this))) |
2158 | return Definition; |
2159 | if (!isOutOfLine() && isInline()) |
2160 | return Definition; |
2161 | return DeclarationOnly; |
2162 | } |
2163 | |
2164 | |
2165 | |
2166 | |
2167 | |
2168 | |
2169 | |
2170 | if (hasInit()) |
2171 | return Definition; |
2172 | |
2173 | if (hasDefiningAttr()) |
2174 | return Definition; |
2175 | |
2176 | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
2177 | if (!SAA->isInherited()) |
2178 | return Definition; |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { |
2184 | if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
2185 | !isa<VarTemplatePartialSpecializationDecl>(VTSD) && |
2186 | !VTSD->IsCompleteDefinition) |
2187 | return DeclarationOnly; |
2188 | } |
2189 | |
2190 | if (hasExternalStorage()) |
2191 | return DeclarationOnly; |
2192 | |
2193 | |
2194 | |
2195 | |
2196 | |
2197 | if (isSingleLineLanguageLinkage(*this)) |
2198 | return DeclarationOnly; |
2199 | |
2200 | |
2201 | |
2202 | |
2203 | |
2204 | |
2205 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
2206 | return TentativeDefinition; |
2207 | |
2208 | |
2209 | |
2210 | return Definition; |
2211 | } |
2212 | |
2213 | VarDecl *VarDecl::getActingDefinition() { |
2214 | DefinitionKind Kind = isThisDeclarationADefinition(); |
2215 | if (Kind != TentativeDefinition) |
2216 | return nullptr; |
2217 | |
2218 | VarDecl *LastTentative = nullptr; |
2219 | VarDecl *First = getFirstDecl(); |
2220 | for (auto I : First->redecls()) { |
2221 | Kind = I->isThisDeclarationADefinition(); |
2222 | if (Kind == Definition) |
2223 | return nullptr; |
2224 | if (Kind == TentativeDefinition) |
2225 | LastTentative = I; |
2226 | } |
2227 | return LastTentative; |
2228 | } |
2229 | |
2230 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
2231 | VarDecl *First = getFirstDecl(); |
2232 | for (auto I : First->redecls()) { |
2233 | if (I->isThisDeclarationADefinition(C) == Definition) |
2234 | return I; |
2235 | } |
2236 | return nullptr; |
2237 | } |
2238 | |
2239 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
2240 | DefinitionKind Kind = DeclarationOnly; |
2241 | |
2242 | const VarDecl *First = getFirstDecl(); |
2243 | for (auto I : First->redecls()) { |
2244 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
2245 | if (Kind == Definition) |
2246 | break; |
2247 | } |
2248 | |
2249 | return Kind; |
2250 | } |
2251 | |
2252 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
2253 | for (auto I : redecls()) { |
2254 | if (auto Expr = I->getInit()) { |
2255 | D = I; |
2256 | return Expr; |
2257 | } |
2258 | } |
2259 | return nullptr; |
2260 | } |
2261 | |
2262 | bool VarDecl::hasInit() const { |
2263 | if (auto *P = dyn_cast<ParmVarDecl>(this)) |
2264 | if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) |
2265 | return false; |
2266 | |
2267 | return !Init.isNull(); |
2268 | } |
2269 | |
2270 | Expr *VarDecl::getInit() { |
2271 | if (!hasInit()) |
2272 | return nullptr; |
2273 | |
2274 | if (auto *S = Init.dyn_cast<Stmt *>()) |
2275 | return cast<Expr>(S); |
2276 | |
2277 | return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value); |
2278 | } |
2279 | |
2280 | Stmt **VarDecl::getInitAddress() { |
2281 | if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) |
2282 | return &ES->Value; |
2283 | |
2284 | return Init.getAddrOfPtr1(); |
2285 | } |
2286 | |
2287 | VarDecl *VarDecl::getInitializingDeclaration() { |
2288 | VarDecl *Def = nullptr; |
2289 | for (auto I : redecls()) { |
2290 | if (I->hasInit()) |
2291 | return I; |
2292 | |
2293 | if (I->isThisDeclarationADefinition()) { |
2294 | if (isStaticDataMember()) |
2295 | return I; |
2296 | Def = I; |
2297 | } |
2298 | } |
2299 | return Def; |
2300 | } |
2301 | |
2302 | bool VarDecl::isOutOfLine() const { |
2303 | if (Decl::isOutOfLine()) |
2304 | return true; |
2305 | |
2306 | if (!isStaticDataMember()) |
2307 | return false; |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
2313 | return VD->isOutOfLine(); |
2314 | |
2315 | return false; |
2316 | } |
2317 | |
2318 | void VarDecl::setInit(Expr *I) { |
2319 | if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
2320 | Eval->~EvaluatedStmt(); |
2321 | getASTContext().Deallocate(Eval); |
2322 | } |
2323 | |
2324 | Init = I; |
2325 | } |
2326 | |
2327 | bool VarDecl::mightBeUsableInConstantExpressions(const ASTContext &C) const { |
2328 | const LangOptions &Lang = C.getLangOpts(); |
2329 | |
2330 | |
2331 | |
2332 | if (!Lang.CPlusPlus && !Lang.OpenCL) |
2333 | return false; |
2334 | |
2335 | |
2336 | if (isa<ParmVarDecl>(this)) |
2337 | return false; |
2338 | |
2339 | |
2340 | if (isWeak()) |
2341 | return false; |
2342 | |
2343 | |
2344 | |
2345 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
2346 | return true; |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | if (!getType().isConstant(C) || getType().isVolatileQualified()) |
2352 | return false; |
2353 | |
2354 | |
2355 | |
2356 | if (getType()->isIntegralOrEnumerationType()) |
2357 | return true; |
2358 | |
2359 | |
2360 | |
2361 | return Lang.CPlusPlus11 && isConstexpr(); |
2362 | } |
2363 | |
2364 | bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const { |
2365 | |
2366 | |
2367 | |
2368 | const VarDecl *DefVD = nullptr; |
2369 | const Expr *Init = getAnyInitializer(DefVD); |
2370 | if (!Init || Init->isValueDependent() || getType()->isDependentType()) |
2371 | return false; |
2372 | |
2373 | |
2374 | if (!DefVD->mightBeUsableInConstantExpressions(Context)) |
2375 | return false; |
2376 | |
2377 | if (Context.getLangOpts().CPlusPlus && !DefVD->hasConstantInitialization()) |
2378 | return false; |
2379 | |
2380 | |
2381 | |
2382 | |
2383 | if ((Context.getLangOpts().CPlusPlus || Context.getLangOpts().OpenCL) && |
2384 | !Context.getLangOpts().CPlusPlus11 && !DefVD->hasICEInitializer(Context)) |
2385 | return false; |
2386 | return true; |
2387 | } |
2388 | |
2389 | |
2390 | |
2391 | |
2392 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
2393 | auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
2394 | if (!Eval) { |
2395 | |
2396 | |
2397 | |
2398 | |
2399 | Eval = new (getASTContext()) EvaluatedStmt; |
2400 | Eval->Value = Init.get<Stmt *>(); |
2401 | Init = Eval; |
2402 | } |
2403 | return Eval; |
2404 | } |
2405 | |
2406 | EvaluatedStmt *VarDecl::getEvaluatedStmt() const { |
2407 | return Init.dyn_cast<EvaluatedStmt *>(); |
2408 | } |
2409 | |
2410 | APValue *VarDecl::evaluateValue() const { |
2411 | SmallVector<PartialDiagnosticAt, 8> Notes; |
2412 | return evaluateValueImpl(Notes, hasConstantInitialization()); |
2413 | } |
2414 | |
2415 | APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
2416 | bool IsConstantInitialization) const { |
2417 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
2418 | |
2419 | const auto *Init = cast<Expr>(Eval->Value); |
2420 | assert(!Init->isValueDependent()); |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | if (Eval->WasEvaluated) |
2426 | return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; |
2427 | |
2428 | if (Eval->IsEvaluating) { |
2429 | |
2430 | return nullptr; |
2431 | } |
2432 | |
2433 | Eval->IsEvaluating = true; |
2434 | |
2435 | ASTContext &Ctx = getASTContext(); |
2436 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes, |
2437 | IsConstantInitialization); |
2438 | |
2439 | |
2440 | |
2441 | |
2442 | if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11 && |
2443 | !Notes.empty()) |
2444 | Result = false; |
2445 | |
2446 | |
2447 | |
2448 | |
2449 | if (!Result) |
2450 | Eval->Evaluated = APValue(); |
2451 | else if (Eval->Evaluated.needsCleanup()) |
2452 | Ctx.addDestruction(&Eval->Evaluated); |
2453 | |
2454 | Eval->IsEvaluating = false; |
2455 | Eval->WasEvaluated = true; |
2456 | |
2457 | return Result ? &Eval->Evaluated : nullptr; |
2458 | } |
2459 | |
2460 | APValue *VarDecl::getEvaluatedValue() const { |
2461 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
2462 | if (Eval->WasEvaluated) |
2463 | return &Eval->Evaluated; |
2464 | |
2465 | return nullptr; |
2466 | } |
2467 | |
2468 | bool VarDecl::hasICEInitializer(const ASTContext &Context) const { |
2469 | const Expr *Init = getInit(); |
2470 | assert(Init && "no initializer"); |
2471 | |
2472 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
2473 | if (!Eval->CheckedForICEInit) { |
2474 | Eval->CheckedForICEInit = true; |
2475 | Eval->HasICEInit = Init->isIntegerConstantExpr(Context); |
2476 | } |
2477 | return Eval->HasICEInit; |
2478 | } |
2479 | |
2480 | bool VarDecl::hasConstantInitialization() const { |
2481 | |
2482 | if (hasGlobalStorage() && !getASTContext().getLangOpts().CPlusPlus) |
2483 | return true; |
2484 | |
2485 | |
2486 | |
2487 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
2488 | return Eval->HasConstantInitialization; |
2489 | |
2490 | return false; |
2491 | } |
2492 | |
2493 | bool VarDecl::checkForConstantInitialization( |
2494 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
2495 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
2496 | |
2497 | |
2498 | |
2499 | assert(!Eval->WasEvaluated && |
2500 | "already evaluated var value before checking for constant init"); |
2501 | assert(getASTContext().getLangOpts().CPlusPlus && "only meaningful in C++"); |
2502 | |
2503 | assert(!cast<Expr>(Eval->Value)->isValueDependent()); |
2504 | |
2505 | |
2506 | Eval->HasConstantInitialization = |
2507 | evaluateValueImpl(Notes, true) && Notes.empty(); |
2508 | |
2509 | |
2510 | |
2511 | if (!Eval->HasConstantInitialization) |
2512 | Eval->WasEvaluated = false; |
2513 | |
2514 | return Eval->HasConstantInitialization; |
2515 | } |
2516 | |
2517 | bool VarDecl::isParameterPack() const { |
2518 | return isa<PackExpansionType>(getType()); |
2519 | } |
2520 | |
2521 | template<typename DeclT> |
2522 | static DeclT *getDefinitionOrSelf(DeclT *D) { |
2523 | assert(D); |
2524 | if (auto *Def = D->getDefinition()) |
2525 | return Def; |
2526 | return D; |
2527 | } |
2528 | |
2529 | bool VarDecl::isEscapingByref() const { |
2530 | return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; |
2531 | } |
2532 | |
2533 | bool VarDecl::isNonEscapingByref() const { |
2534 | return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; |
2535 | } |
2536 | |
2537 | bool VarDecl::hasDependentAlignment() const { |
2538 | QualType T = getType(); |
2539 | return T->isDependentType() || T->isUndeducedAutoType() || |
2540 | llvm::any_of(specific_attrs<AlignedAttr>(), [](const AlignedAttr *AA) { |
2541 | return AA->isAlignmentDependent(); |
2542 | }); |
2543 | } |
2544 | |
2545 | VarDecl *VarDecl::getTemplateInstantiationPattern() const { |
2546 | const VarDecl *VD = this; |
2547 | |
2548 | |
2549 | |
2550 | if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { |
2551 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
2552 | VD = VD->getInstantiatedFromStaticDataMember(); |
2553 | while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) |
2554 | VD = NewVD; |
2555 | } |
2556 | } |
2557 | |
2558 | |
2559 | |
2560 | if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
2561 | if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) { |
2562 | auto From = VDTemplSpec->getInstantiatedFrom(); |
2563 | if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { |
2564 | while (!VTD->isMemberSpecialization()) { |
2565 | auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); |
2566 | if (!NewVTD) |
2567 | break; |
2568 | VTD = NewVTD; |
2569 | } |
2570 | return getDefinitionOrSelf(VTD->getTemplatedDecl()); |
2571 | } |
2572 | if (auto *VTPSD = |
2573 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
2574 | while (!VTPSD->isMemberSpecialization()) { |
2575 | auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); |
2576 | if (!NewVTPSD) |
2577 | break; |
2578 | VTPSD = NewVTPSD; |
2579 | } |
2580 | return getDefinitionOrSelf<VarDecl>(VTPSD); |
2581 | } |
2582 | } |
2583 | } |
2584 | |
2585 | |
2586 | |
2587 | if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { |
2588 | while (!VarTemplate->isMemberSpecialization()) { |
2589 | auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); |
2590 | if (!NewVT) |
2591 | break; |
2592 | VarTemplate = NewVT; |
2593 | } |
2594 | |
2595 | return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); |
2596 | } |
2597 | |
2598 | if (VD == this) |
2599 | return nullptr; |
2600 | return getDefinitionOrSelf(const_cast<VarDecl*>(VD)); |
2601 | } |
2602 | |
2603 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
2604 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2605 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
2606 | |
2607 | return nullptr; |
2608 | } |
2609 | |
2610 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
2611 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
2612 | return Spec->getSpecializationKind(); |
2613 | |
2614 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2615 | return MSI->getTemplateSpecializationKind(); |
2616 | |
2617 | return TSK_Undeclared; |
2618 | } |
2619 | |
2620 | TemplateSpecializationKind |
2621 | VarDecl::getTemplateSpecializationKindForInstantiation() const { |
2622 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2623 | return MSI->getTemplateSpecializationKind(); |
2624 | |
2625 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
2626 | return Spec->getSpecializationKind(); |
2627 | |
2628 | return TSK_Undeclared; |
2629 | } |
2630 | |
2631 | SourceLocation VarDecl::getPointOfInstantiation() const { |
2632 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
2633 | return Spec->getPointOfInstantiation(); |
2634 | |
2635 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2636 | return MSI->getPointOfInstantiation(); |
2637 | |
2638 | return SourceLocation(); |
2639 | } |
2640 | |
2641 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
2642 | return getASTContext().getTemplateOrSpecializationInfo(this) |
2643 | .dyn_cast<VarTemplateDecl *>(); |
2644 | } |
2645 | |
2646 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
2647 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
2648 | } |
2649 | |
2650 | bool VarDecl::isKnownToBeDefined() const { |
2651 | const auto &LangOpts = getASTContext().getLangOpts(); |
2652 | |
2653 | |
2654 | |
2655 | |
2656 | |
2657 | |
2658 | |
2659 | if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && |
2660 | hasExternalStorage() && hasAttr<CUDASharedAttr>() && |
2661 | isa<IncompleteArrayType>(getType())) |
2662 | return true; |
2663 | |
2664 | return hasDefinition(); |
2665 | } |
2666 | |
2667 | bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { |
2668 | return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || |
2669 | (!Ctx.getLangOpts().RegisterStaticDestructors && |
2670 | !hasAttr<AlwaysDestroyAttr>())); |
2671 | } |
2672 | |
2673 | QualType::DestructionKind |
2674 | VarDecl::needsDestruction(const ASTContext &Ctx) const { |
2675 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
2676 | if (Eval->HasConstantDestruction) |
2677 | return QualType::DK_none; |
2678 | |
2679 | if (isNoDestroy(Ctx)) |
2680 | return QualType::DK_none; |
2681 | |
2682 | return getType().isDestructedType(); |
2683 | } |
2684 | |
2685 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
2686 | if (isStaticDataMember()) |
2687 | |
2688 | |
2689 | return getASTContext().getTemplateOrSpecializationInfo(this) |
2690 | .dyn_cast<MemberSpecializationInfo *>(); |
2691 | return nullptr; |
2692 | } |
2693 | |
2694 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
2695 | SourceLocation PointOfInstantiation) { |
2696 | assert((isa<VarTemplateSpecializationDecl>(this) || |
2697 | getMemberSpecializationInfo()) && |
2698 | "not a variable or static data member template specialization"); |
2699 | |
2700 | if (VarTemplateSpecializationDecl *Spec = |
2701 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
2702 | Spec->setSpecializationKind(TSK); |
2703 | if (TSK != TSK_ExplicitSpecialization && |
2704 | PointOfInstantiation.isValid() && |
2705 | Spec->getPointOfInstantiation().isInvalid()) { |
2706 | Spec->setPointOfInstantiation(PointOfInstantiation); |
2707 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
2708 | L->InstantiationRequested(this); |
2709 | } |
2710 | } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
2711 | MSI->setTemplateSpecializationKind(TSK); |
2712 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
2713 | MSI->getPointOfInstantiation().isInvalid()) { |
2714 | MSI->setPointOfInstantiation(PointOfInstantiation); |
2715 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
2716 | L->InstantiationRequested(this); |
2717 | } |
2718 | } |
2719 | } |
2720 | |
2721 | void |
2722 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
2723 | TemplateSpecializationKind TSK) { |
2724 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
2725 | "Previous template or instantiation?"); |
2726 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
2727 | } |
2728 | |
2729 | |
2730 | |
2731 | |
2732 | |
2733 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
2734 | SourceLocation StartLoc, |
2735 | SourceLocation IdLoc, IdentifierInfo *Id, |
2736 | QualType T, TypeSourceInfo *TInfo, |
2737 | StorageClass S, Expr *DefArg) { |
2738 | return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo, |
2739 | S, DefArg); |
2740 | } |
2741 | |
2742 | QualType ParmVarDecl::getOriginalType() const { |
2743 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
2744 | QualType T = TSI ? TSI->getType() : getType(); |
2745 | if (const auto *DT = dyn_cast<DecayedType>(T)) |
2746 | return DT->getOriginalType(); |
2747 | return T; |
2748 | } |
2749 | |
2750 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
2751 | return new (C, ID) |
2752 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
2753 | nullptr, QualType(), nullptr, SC_None, nullptr); |
2754 | } |
2755 | |
2756 | SourceRange ParmVarDecl::getSourceRange() const { |
2757 | if (!hasInheritedDefaultArg()) { |
2758 | SourceRange ArgRange = getDefaultArgRange(); |
2759 | if (ArgRange.isValid()) |
2760 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
2761 | } |
2762 | |
2763 | |
2764 | |
2765 | if (isa<ObjCMethodDecl>(getDeclContext())) |
2766 | return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); |
2767 | |
2768 | return DeclaratorDecl::getSourceRange(); |
2769 | } |
2770 | |
2771 | bool ParmVarDecl::isDestroyedInCallee() const { |
2772 | if (hasAttr<NSConsumedAttr>()) |
2773 | return true; |
2774 | |
2775 | auto *RT = getType()->getAs<RecordType>(); |
2776 | if (RT && RT->getDecl()->isParamDestroyedInCallee()) |
2777 | return true; |
2778 | |
2779 | return false; |
2780 | } |
2781 | |
2782 | Expr *ParmVarDecl::getDefaultArg() { |
2783 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
2784 | assert(!hasUninstantiatedDefaultArg() && |
2785 | "Default argument is not yet instantiated!"); |
2786 | |
2787 | Expr *Arg = getInit(); |
2788 | if (auto *E = dyn_cast_or_null<FullExpr>(Arg)) |
2789 | return E->getSubExpr(); |
2790 | |
2791 | return Arg; |
2792 | } |
2793 | |
2794 | void ParmVarDecl::setDefaultArg(Expr *defarg) { |
2795 | ParmVarDeclBits.DefaultArgKind = DAK_Normal; |
2796 | Init = defarg; |
2797 | } |
2798 | |
2799 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
2800 | switch (ParmVarDeclBits.DefaultArgKind) { |
2801 | case DAK_None: |
2802 | case DAK_Unparsed: |
2803 | |
2804 | return SourceRange(); |
2805 | |
2806 | case DAK_Uninstantiated: |
2807 | return getUninstantiatedDefaultArg()->getSourceRange(); |
2808 | |
2809 | case DAK_Normal: |
2810 | if (const Expr *E = getInit()) |
2811 | return E->getSourceRange(); |
2812 | |
2813 | |
2814 | return SourceRange(); |
2815 | } |
2816 | llvm_unreachable("Invalid default argument kind."); |
2817 | } |
2818 | |
2819 | void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { |
2820 | ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; |
2821 | Init = arg; |
2822 | } |
2823 | |
2824 | Expr *ParmVarDecl::getUninstantiatedDefaultArg() { |
2825 | assert(hasUninstantiatedDefaultArg() && |
2826 | "Wrong kind of initialization expression!"); |
2827 | return cast_or_null<Expr>(Init.get<Stmt *>()); |
2828 | } |
2829 | |
2830 | bool ParmVarDecl::hasDefaultArg() const { |
2831 | |
2832 | |
2833 | |
2834 | return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || |
2835 | !Init.isNull(); |
2836 | } |
2837 | |
2838 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
2839 | getASTContext().setParameterIndex(this, parameterIndex); |
2840 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
2841 | } |
2842 | |
2843 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
2844 | return getASTContext().getParameterIndex(this); |
2845 | } |
2846 | |
2847 | |
2848 | |
2849 | |
2850 | |
2851 | FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, |
2852 | SourceLocation StartLoc, |
2853 | const DeclarationNameInfo &NameInfo, QualType T, |
2854 | TypeSourceInfo *TInfo, StorageClass S, |
2855 | bool isInlineSpecified, |
2856 | ConstexprSpecKind ConstexprKind, |
2857 | Expr *TrailingRequiresClause) |
2858 | : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, |
2859 | StartLoc), |
2860 | DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0), |
2861 | EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { |
2862 | assert(T.isNull() || T->isFunctionType()); |
2863 | FunctionDeclBits.SClass = S; |
2864 | FunctionDeclBits.IsInline = isInlineSpecified; |
2865 | FunctionDeclBits.IsInlineSpecified = isInlineSpecified; |
2866 | FunctionDeclBits.IsVirtualAsWritten = false; |
2867 | FunctionDeclBits.IsPure = false; |
2868 | FunctionDeclBits.HasInheritedPrototype = false; |
2869 | FunctionDeclBits.HasWrittenPrototype = true; |
2870 | FunctionDeclBits.IsDeleted = false; |
2871 | FunctionDeclBits.IsTrivial = false; |
2872 | FunctionDeclBits.IsTrivialForCall = false; |
2873 | FunctionDeclBits.IsDefaulted = false; |
2874 | FunctionDeclBits.IsExplicitlyDefaulted = false; |
2875 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
2876 | FunctionDeclBits.HasImplicitReturnZero = false; |
2877 | FunctionDeclBits.IsLateTemplateParsed = false; |
2878 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind); |
2879 | FunctionDeclBits.InstantiationIsPending = false; |
2880 | FunctionDeclBits.UsesSEHTry = false; |
2881 | FunctionDeclBits.UsesFPIntrin = false; |
2882 | FunctionDeclBits.HasSkippedBody = false; |
2883 | FunctionDeclBits.WillHaveBody = false; |
2884 | FunctionDeclBits.IsMultiVersion = false; |
2885 | FunctionDeclBits.IsCopyDeductionCandidate = false; |
2886 | FunctionDeclBits.HasODRHash = false; |
2887 | if (TrailingRequiresClause) |
2888 | setTrailingRequiresClause(TrailingRequiresClause); |
2889 | } |
2890 | |
2891 | void FunctionDecl::getNameForDiagnostic( |
2892 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
2893 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
2894 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
2895 | if (TemplateArgs) |
2896 | printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); |
2897 | } |
2898 | |
2899 | bool FunctionDecl::isVariadic() const { |
2900 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
2901 | return FT->isVariadic(); |
2902 | return false; |
2903 | } |
2904 | |
2905 | FunctionDecl::DefaultedFunctionInfo * |
2906 | FunctionDecl::DefaultedFunctionInfo::Create(ASTContext &Context, |
2907 | ArrayRef<DeclAccessPair> Lookups) { |
2908 | DefaultedFunctionInfo *Info = new (Context.Allocate( |
2909 | totalSizeToAlloc<DeclAccessPair>(Lookups.size()), |
2910 | std::max(alignof(DefaultedFunctionInfo), alignof(DeclAccessPair)))) |
2911 | DefaultedFunctionInfo; |
2912 | Info->NumLookups = Lookups.size(); |
2913 | std::uninitialized_copy(Lookups.begin(), Lookups.end(), |
2914 | Info->getTrailingObjects<DeclAccessPair>()); |
2915 | return Info; |
2916 | } |
2917 | |
2918 | void FunctionDecl::setDefaultedFunctionInfo(DefaultedFunctionInfo *Info) { |
2919 | assert(!FunctionDeclBits.HasDefaultedFunctionInfo && "already have this"); |
2920 | assert(!Body && "can't replace function body with defaulted function info"); |
2921 | |
2922 | FunctionDeclBits.HasDefaultedFunctionInfo = true; |
2923 | DefaultedInfo = Info; |
2924 | } |
2925 | |
2926 | FunctionDecl::DefaultedFunctionInfo * |
2927 | FunctionDecl::getDefaultedFunctionInfo() const { |
2928 | return FunctionDeclBits.HasDefaultedFunctionInfo ? DefaultedInfo : nullptr; |
2929 | } |
2930 | |
2931 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
2932 | for (auto I : redecls()) { |
2933 | if (I->doesThisDeclarationHaveABody()) { |
2934 | Definition = I; |
2935 | return true; |
2936 | } |
2937 | } |
2938 | |
2939 | return false; |
2940 | } |
2941 | |
2942 | bool FunctionDecl::hasTrivialBody() const { |
2943 | Stmt *S = getBody(); |
2944 | if (!S) { |
2945 | |
2946 | |
2947 | return false; |
2948 | } |
2949 | |
2950 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
2951 | return true; |
2952 | return false; |
2953 | } |
2954 | |
2955 | bool FunctionDecl::isThisDeclarationInstantiatedFromAFriendDefinition() const { |
2956 | if (!getFriendObjectKind()) |
2957 | return false; |
2958 | |
2959 | |
2960 | |
2961 | if (const FunctionDecl *InstantiatedFrom = |
2962 | getInstantiatedFromMemberFunction()) |
2963 | return InstantiatedFrom->getFriendObjectKind() && |
2964 | InstantiatedFrom->isThisDeclarationADefinition(); |
2965 | |
2966 | |
2967 | |
2968 | if (const FunctionTemplateDecl *Template = getDescribedFunctionTemplate()) { |
2969 | if (const FunctionTemplateDecl *InstantiatedFrom = |
2970 | Template->getInstantiatedFromMemberTemplate()) |
2971 | return InstantiatedFrom->getFriendObjectKind() && |
2972 | InstantiatedFrom->isThisDeclarationADefinition(); |
2973 | } |
2974 | |
2975 | return false; |
2976 | } |
2977 | |
2978 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition, |
2979 | bool CheckForPendingFriendDefinition) const { |
2980 | for (const FunctionDecl *FD : redecls()) { |
2981 | if (FD->isThisDeclarationADefinition()) { |
2982 | Definition = FD; |
2983 | return true; |
2984 | } |
2985 | |
2986 | |
2987 | |
2988 | |
2989 | |
2990 | |
2991 | |
2992 | |
2993 | |
2994 | |
2995 | |
2996 | |
2997 | |
2998 | |
2999 | |
3000 | |
3001 | if (CheckForPendingFriendDefinition && |
3002 | FD->isThisDeclarationInstantiatedFromAFriendDefinition()) { |
3003 | Definition = FD; |
3004 | return true; |
3005 | } |
3006 | } |
3007 | |
3008 | return false; |
3009 | } |
3010 | |
3011 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
3012 | if (!hasBody(Definition)) |
3013 | return nullptr; |
3014 | |
3015 | assert(!Definition->FunctionDeclBits.HasDefaultedFunctionInfo && |
3016 | "definition should not have a body"); |
3017 | if (Definition->Body) |
3018 | return Definition->Body.get(getASTContext().getExternalSource()); |
3019 | |
3020 | return nullptr; |
3021 | } |
3022 | |
3023 | void FunctionDecl::setBody(Stmt *B) { |
3024 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
3025 | Body = LazyDeclStmtPtr(B); |
3026 | if (B) |
3027 | EndRangeLoc = B->getEndLoc(); |
3028 | } |
3029 | |
3030 | void FunctionDecl::setPure(bool P) { |
3031 | FunctionDeclBits.IsPure = P; |
3032 | if (P) |
3033 | if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
3034 | Parent->markedVirtualFunctionPure(); |
3035 | } |
3036 | |
3037 | template<std::size_t Len> |
3038 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
3039 | IdentifierInfo *II = ND->getIdentifier(); |
3040 | return II && II->isStr(Str); |
3041 | } |
3042 | |
3043 | bool FunctionDecl::isMain() const { |
3044 | const TranslationUnitDecl *tunit = |
3045 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
3046 | return tunit && |
3047 | !tunit->getASTContext().getLangOpts().Freestanding && |
3048 | isNamed(this, "main"); |
3049 | } |
3050 | |
3051 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
3052 | const TranslationUnitDecl *TUnit = |
3053 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
3054 | if (!TUnit) |
3055 | return false; |
3056 | |
3057 | |
3058 | |
3059 | |
3060 | |
3061 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
3062 | return false; |
3063 | |
3064 | |
3065 | if (!getIdentifier()) |
3066 | return false; |
3067 | |
3068 | return llvm::StringSwitch<bool>(getName()) |
3069 | .Cases("main", |
3070 | "wmain", |
3071 | "WinMain", |
3072 | "wWinMain", |
3073 | "DllMain", |
3074 | true) |
3075 | .Default(false); |
3076 | } |
3077 | |
3078 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
3079 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
3080 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
3081 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
3082 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
3083 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
3084 | |
3085 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
3086 | return false; |
3087 | |
3088 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
3089 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
3090 | return false; |
3091 | |
3092 | ASTContext &Context = |
3093 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
3094 | ->getASTContext(); |
3095 | |
3096 | |
3097 | |
3098 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
3099 | } |
3100 | |
3101 | bool FunctionDecl::isReplaceableGlobalAllocationFunction( |
3102 | Optional<unsigned> *AlignmentParam, bool *IsNothrow) const { |
3103 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
3104 | return false; |
3105 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
3106 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
3107 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
3108 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
3109 | return false; |
3110 | |
3111 | if (isa<CXXRecordDecl>(getDeclContext())) |
3112 | return false; |
3113 | |
3114 | |
3115 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
3116 | return false; |
3117 | |
3118 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
3119 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic()) |
3120 | return false; |
3121 | |
3122 | |
3123 | |
3124 | if (FPT->getNumParams() == 1) |
3125 | return true; |
3126 | |
3127 | unsigned Params = 1; |
3128 | QualType Ty = FPT->getParamType(Params); |
3129 | ASTContext &Ctx = getASTContext(); |
3130 | |
3131 | auto Consume = [&] { |
3132 | ++Params; |
3133 | Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); |
3134 | }; |
3135 | |
3136 | |
3137 | bool IsSizedDelete = false; |
3138 | if (Ctx.getLangOpts().SizedDeallocation && |
3139 | (getDeclName().getCXXOverloadedOperator() == OO_Delete || |
3140 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && |
3141 | Ctx.hasSameType(Ty, Ctx.getSizeType())) { |
3142 | IsSizedDelete = true; |
3143 | Consume(); |
3144 | } |
3145 | |
3146 | |
3147 | |
3148 | if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { |
3149 | Consume(); |
3150 | if (AlignmentParam) |
3151 | *AlignmentParam = Params; |
3152 | } |
3153 | |
3154 | |
3155 | |
3156 | if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { |
3157 | Ty = Ty->getPointeeType(); |
3158 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
3159 | return false; |
3160 | if (Ty->isNothrowT()) { |
3161 | if (IsNothrow) |
3162 | *IsNothrow = true; |
3163 | Consume(); |
3164 | } |
3165 | } |
3166 | |
3167 | return Params == FPT->getNumParams(); |
3168 | } |
3169 | |
3170 | bool FunctionDecl::isInlineBuiltinDeclaration() const { |
3171 | if (!getBuiltinID()) |
3172 | return false; |
3173 | |
3174 | const FunctionDecl *Definition; |
3175 | return hasBody(Definition) && Definition->isInlineSpecified(); |
3176 | } |
3177 | |
3178 | bool FunctionDecl::isDestroyingOperatorDelete() const { |
3179 | |
3180 | |
3181 | |
3182 | |
3183 | if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || |
3184 | getNumParams() < 2) |
3185 | return false; |
3186 | |
3187 | auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); |
3188 | return RD && RD->isInStdNamespace() && RD->getIdentifier() && |
3189 | RD->getIdentifier()->isStr("destroying_delete_t"); |
3190 | } |
3191 | |
3192 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
3193 | return getDeclLanguageLinkage(*this); |
3194 | } |
3195 | |
3196 | bool FunctionDecl::isExternC() const { |
3197 | return isDeclExternC(*this); |
3198 | } |
3199 | |
3200 | bool FunctionDecl::isInExternCContext() const { |
3201 | if (hasAttr<OpenCLKernelAttr>()) |
3202 | return true; |
3203 | return getLexicalDeclContext()->isExternCContext(); |
3204 | } |
3205 | |
3206 | bool FunctionDecl::isInExternCXXContext() const { |
3207 | return getLexicalDeclContext()->isExternCXXContext(); |
3208 | } |
3209 | |
3210 | bool FunctionDecl::isGlobal() const { |
3211 | if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) |
3212 | return Method->isStatic(); |
3213 | |
3214 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
3215 | return false; |
3216 | |
3217 | for (const DeclContext *DC = getDeclContext(); |
3218 | DC->isNamespace(); |
3219 | DC = DC->getParent()) { |
3220 | if (const auto *Namespace = cast<NamespaceDecl>(DC)) { |
3221 | if (!Namespace->getDeclName()) |
3222 | return false; |
3223 | break; |
3224 | } |
3225 | } |
3226 | |
3227 | return true; |
3228 | } |
3229 | |
3230 | bool FunctionDecl::isNoReturn() const { |
3231 | if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
3232 | hasAttr<C11NoReturnAttr>()) |
3233 | return true; |
3234 | |
3235 | if (auto *FnTy = getType()->getAs<FunctionType>()) |
3236 | return FnTy->getNoReturnAttr(); |
3237 | |
3238 | return false; |
3239 | } |
3240 | |
3241 | |
3242 | MultiVersionKind FunctionDecl::getMultiVersionKind() const { |
3243 | if (hasAttr<TargetAttr>()) |
3244 | return MultiVersionKind::Target; |
3245 | if (hasAttr<CPUDispatchAttr>()) |
3246 | return MultiVersionKind::CPUDispatch; |
3247 | if (hasAttr<CPUSpecificAttr>()) |
3248 | return MultiVersionKind::CPUSpecific; |
3249 | return MultiVersionKind::None; |
3250 | } |
3251 | |
3252 | bool FunctionDecl::isCPUDispatchMultiVersion() const { |
3253 | return isMultiVersion() && hasAttr<CPUDispatchAttr>(); |
3254 | } |
3255 | |
3256 | bool FunctionDecl::isCPUSpecificMultiVersion() const { |
3257 | return isMultiVersion() && hasAttr<CPUSpecificAttr>(); |
3258 | } |
3259 | |
3260 | bool FunctionDecl::isTargetMultiVersion() const { |
3261 | return isMultiVersion() && hasAttr<TargetAttr>(); |
3262 | } |
3263 | |
3264 | void |
3265 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
3266 | redeclarable_base::setPreviousDecl(PrevDecl); |
3267 | |
3268 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
3269 | FunctionTemplateDecl *PrevFunTmpl |
3270 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
3271 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
3272 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
3273 | } |
3274 | |
3275 | if (PrevDecl && PrevDecl->isInlined()) |
3276 | setImplicitlyInline(true); |
3277 | } |
3278 | |
3279 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
3280 | |
3281 | |
3282 | |
3283 | |
3284 | |
3285 | |
3286 | |
3287 | |
3288 | |
3289 | |
3290 | |
3291 | |
3292 | |
3293 | |
3294 | unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { |
3295 | unsigned BuiltinID = 0; |
3296 | |
3297 | if (const auto *ABAA = getAttr<ArmBuiltinAliasAttr>()) { |
3298 | BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); |
3299 | } else if (const auto *BAA = getAttr<BuiltinAliasAttr>()) { |
3300 | BuiltinID = BAA->getBuiltinName()->getBuiltinID(); |
3301 | } else if (const auto *A = getAttr<BuiltinAttr>()) { |
3302 | BuiltinID = A->getID(); |
3303 | } |
3304 | |
3305 | if (!BuiltinID) |
3306 | return 0; |
3307 | |
3308 | |
3309 | |
3310 | if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>() && |
3311 | (!hasAttr<ArmBuiltinAliasAttr>() && !hasAttr<BuiltinAliasAttr>())) |
3312 | return 0; |
3313 | |
3314 | ASTContext &Context = getASTContext(); |
3315 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
3316 | return BuiltinID; |
3317 | |
3318 | |
3319 | |
3320 | |
3321 | |
3322 | |
3323 | if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) |
3324 | return 0; |
3325 | |
3326 | |
3327 | |
3328 | if (Context.getLangOpts().OpenCL && |
3329 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
3330 | return 0; |
3331 | |
3332 | |
3333 | |
3334 | if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && |
3335 | !hasAttr<CUDAHostAttr>() && |
3336 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
3337 | return 0; |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | if (Context.getTargetInfo().getTriple().isAMDGCN() && |
3343 | Context.getLangOpts().OpenMPIsDevice && |
3344 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
3345 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
3346 | return 0; |
3347 | |
3348 | return BuiltinID; |
3349 | } |
3350 | |
3351 | |
3352 | |
3353 | |
3354 | unsigned FunctionDecl::getNumParams() const { |
3355 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
3356 | return FPT ? FPT->getNumParams() : 0; |
3357 | } |
3358 | |
3359 | void FunctionDecl::setParams(ASTContext &C, |
3360 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
3361 | assert(!ParamInfo && "Already has param info!"); |
3362 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
3363 | |
3364 | |
3365 | if (!NewParamInfo.empty()) { |
3366 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
3367 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
3368 | } |
3369 | } |
3370 | |
3371 | |
3372 | |
3373 | |
3374 | |
3375 | unsigned FunctionDecl::getMinRequiredArguments() const { |
3376 | if (!getASTContext().getLangOpts().CPlusPlus) |
3377 | return getNumParams(); |
3378 | |
3379 | |
3380 | |
3381 | unsigned NumRequiredArgs = 0; |
3382 | unsigned MinParamsSoFar = 0; |
3383 | for (auto *Param : parameters()) { |
3384 | if (!Param->isParameterPack()) { |
3385 | ++MinParamsSoFar; |
3386 | if (!Param->hasDefaultArg()) |
3387 | NumRequiredArgs = MinParamsSoFar; |
3388 | } |
3389 | } |
3390 | return NumRequiredArgs; |
3391 | } |
3392 | |
3393 | bool FunctionDecl::hasOneParamOrDefaultArgs() const { |
3394 | return getNumParams() == 1 || |
3395 | (getNumParams() > 1 && |
3396 | std::all_of(param_begin() + 1, param_end(), |
3397 | [](ParmVarDecl *P) { return P->hasDefaultArg(); })); |
3398 | } |
3399 | |
3400 | |
3401 | |
3402 | |
3403 | |
3404 | |
3405 | bool FunctionDecl::isMSExternInline() const { |
3406 | assert(isInlined() && "expected to get called on an inlined function!"); |
3407 | |
3408 | const ASTContext &Context = getASTContext(); |
3409 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
3410 | !hasAttr<DLLExportAttr>()) |
3411 | return false; |
3412 | |
3413 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
3414 | FD = FD->getPreviousDecl()) |
3415 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
3416 | return true; |
3417 | |
3418 | return false; |
3419 | } |
3420 | |
3421 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
3422 | if (Redecl->getStorageClass() != SC_Extern) |
3423 | return false; |
3424 | |
3425 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
3426 | FD = FD->getPreviousDecl()) |
3427 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
3428 | return false; |
3429 | |
3430 | return true; |
3431 | } |
3432 | |
3433 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
3434 | |
3435 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
3436 | return false; |
3437 | |
3438 | |
3439 | |
3440 | if (Redecl->isImplicit()) |
3441 | return false; |
3442 | |
3443 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
3444 | return true; |
3445 | |
3446 | return false; |
3447 | } |
3448 | |
3449 | |
3450 | |
3451 | |
3452 | |
3453 | |
3454 | |
3455 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
3456 | assert(!doesThisDeclarationHaveABody() && |
3457 | "Must have a declaration without a body."); |
3458 | |
3459 | ASTContext &Context = getASTContext(); |
3460 | |
3461 | if (Context.getLangOpts().MSVCCompat) { |
3462 | const FunctionDecl *Definition; |
3463 | if (hasBody(Definition) && Definition->isInlined() && |
3464 | redeclForcesDefMSVC(this)) |
3465 | return true; |
3466 | } |
3467 | |
3468 | if (Context.getLangOpts().CPlusPlus) |
3469 | return false; |
3470 | |
3471 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
3472 | |
3473 | |
3474 | |
3475 | |
3476 | |
3477 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
3478 | return false; |
3479 | |
3480 | const FunctionDecl *Prev = this; |
3481 | bool FoundBody = false; |
3482 | while ((Prev = Prev->getPreviousDecl())) { |
3483 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
3484 | |
3485 | if (Prev->doesThisDeclarationHaveABody()) { |
3486 | |
3487 | |
3488 | if (!Prev->isInlineSpecified() || |
3489 | Prev->getStorageClass() != SC_Extern) |
3490 | return false; |
3491 | } else if (Prev->isInlineSpecified() && |
3492 | Prev->getStorageClass() != SC_Extern) { |
3493 | return false; |
3494 | } |
3495 | } |
3496 | return FoundBody; |
3497 | } |
3498 | |
3499 | |
3500 | |
3501 | |
3502 | |
3503 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
3504 | return false; |
3505 | const FunctionDecl *Prev = this; |
3506 | bool FoundBody = false; |
3507 | while ((Prev = Prev->getPreviousDecl())) { |
3508 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
3509 | if (RedeclForcesDefC99(Prev)) |
3510 | return false; |
3511 | } |
3512 | return FoundBody; |
3513 | } |
3514 | |
3515 | FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const { |
3516 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
3517 | return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>() |
3518 | : FunctionTypeLoc(); |
3519 | } |
3520 | |
3521 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
3522 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
3523 | if (!FTL) |
3524 | return SourceRange(); |
3525 | |
3526 | |
3527 | const SourceManager &SM = getASTContext().getSourceManager(); |
3528 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
3529 | SourceLocation Boundary = getNameInfo().getBeginLoc(); |
3530 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
3531 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
3532 | return SourceRange(); |
3533 | |
3534 | return RTRange; |
3535 | } |
3536 | |
3537 | SourceRange FunctionDecl::getParametersSourceRange() const { |
3538 | unsigned NP = getNumParams(); |
3539 | SourceLocation EllipsisLoc = getEllipsisLoc(); |
3540 | |
3541 | if (NP == 0 && EllipsisLoc.isInvalid()) |
3542 | return SourceRange(); |
3543 | |
3544 | SourceLocation Begin = |
3545 | NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() : EllipsisLoc; |
3546 | SourceLocation End = EllipsisLoc.isValid() |
3547 | ? EllipsisLoc |
3548 | : ParamInfo[NP - 1]->getSourceRange().getEnd(); |
3549 | |
3550 | return SourceRange(Begin, End); |
3551 | } |
3552 | |
3553 | SourceRange FunctionDecl::getExceptionSpecSourceRange() const { |
3554 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
3555 | return FTL ? FTL.getExceptionSpecRange() : SourceRange(); |
3556 | } |
3557 | |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | |
3565 | |
3566 | |
3567 | |
3568 | |
3569 | |
3570 | |
3571 | |
3572 | |
3573 | |
3574 | |
3575 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
3576 | assert((doesThisDeclarationHaveABody() || willHaveBody() || |
3577 | hasAttr<AliasAttr>()) && |
3578 | "Must be a function definition"); |
3579 | assert(isInlined() && "Function must be inline"); |
3580 | ASTContext &Context = getASTContext(); |
3581 | |
3582 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
3583 | |
3584 | |
3585 | |
3586 | |
3587 | |
3588 | |
3589 | if (Context.getLangOpts().CPlusPlus) |
3590 | return false; |
3591 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
3592 | return true; |
3593 | |
3594 | |
3595 | |
3596 | for (auto Redecl : redecls()) { |
3597 | if (Redecl->isInlineSpecified() && |
3598 | Redecl->getStorageClass() != SC_Extern) |
3599 | return true; |
3600 | } |
3601 | |
3602 | return false; |
3603 | } |
3604 | |
3605 | |
3606 | assert(!Context.getLangOpts().CPlusPlus && |
3607 | "should not use C inline rules in C++"); |
3608 | |
3609 | |
3610 | |
3611 | |
3612 | |
3613 | for (auto Redecl : redecls()) { |
3614 | if (RedeclForcesDefC99(Redecl)) |
3615 | return true; |
3616 | } |
3617 | |
3618 | |
3619 | |
3620 | |
3621 | |
3622 | return false; |
3623 | } |
3624 | |
3625 | |
3626 | |
3627 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
3628 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
3629 | return getDeclName().getCXXOverloadedOperator(); |
3630 | return OO_None; |
3631 | } |
3632 | |
3633 | |
3634 | |
3635 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
3636 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
3637 | return getDeclName().getCXXLiteralIdentifier(); |
3638 | return nullptr; |
3639 | } |
3640 | |
3641 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
3642 | if (TemplateOrSpecialization.isNull()) |
3643 | return TK_NonTemplate; |
3644 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
3645 | return TK_FunctionTemplate; |
3646 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
3647 | return TK_MemberSpecialization; |
3648 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
3649 | return TK_FunctionTemplateSpecialization; |
3650 | if (TemplateOrSpecialization.is |
3651 | <DependentFunctionTemplateSpecializationInfo*>()) |
3652 | return TK_DependentFunctionTemplateSpecialization; |
3653 | |
3654 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
3655 | } |
3656 | |
3657 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
3658 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
3659 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
3660 | |
3661 | return nullptr; |
3662 | } |
3663 | |
3664 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
3665 | if (auto *MSI = |
3666 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
3667 | return MSI; |
3668 | if (auto *FTSI = TemplateOrSpecialization |
3669 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
3670 | return FTSI->getMemberSpecializationInfo(); |
3671 | return nullptr; |
3672 | } |
3673 | |
3674 | void |
3675 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
3676 | FunctionDecl *FD, |
3677 | TemplateSpecializationKind TSK) { |
3678 | assert(TemplateOrSpecialization.isNull() && |
3679 | "Member function is already a specialization"); |
3680 | MemberSpecializationInfo *Info |
3681 | = new (C) MemberSpecializationInfo(FD, TSK); |
3682 | TemplateOrSpecialization = Info; |
3683 | } |
3684 | |
3685 | FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { |
3686 | return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>(); |
3687 | } |
3688 | |
3689 | void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { |
3690 | assert(TemplateOrSpecialization.isNull() && |
3691 | "Member function is already a specialization"); |
3692 | TemplateOrSpecialization = Template; |
3693 | } |
3694 | |
3695 | bool FunctionDecl::isImplicitlyInstantiable() const { |
3696 | |
3697 | if (isInvalidDecl()) |
3698 | return false; |
3699 | |
3700 | switch (getTemplateSpecializationKindForInstantiation()) { |
3701 | case TSK_Undeclared: |
3702 | case TSK_ExplicitInstantiationDefinition: |
3703 | case TSK_ExplicitSpecialization: |
3704 | return false; |
3705 | |
3706 | case TSK_ImplicitInstantiation: |
3707 | return true; |
3708 | |
3709 | case TSK_ExplicitInstantiationDeclaration: |
3710 | |
3711 | break; |
3712 | } |
3713 | |
3714 | |
3715 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
3716 | bool HasPattern = false; |
3717 | if (PatternDecl) |
3718 | HasPattern = PatternDecl->hasBody(PatternDecl); |
3719 | |
3720 | |
3721 | |
3722 | |
3723 | |
3724 | if (!HasPattern || !PatternDecl) |
3725 | return true; |
3726 | |
3727 | return PatternDecl->isInlined(); |
3728 | } |
3729 | |
3730 | bool FunctionDecl::isTemplateInstantiation() const { |
3731 | |
3732 | |
3733 | return clang::isTemplateInstantiation(getTemplateSpecializationKind()); |
3734 | } |
3735 | |
3736 | FunctionDecl * |
3737 | FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const { |
3738 | |
3739 | |
3740 | |
3741 | |
3742 | |
3743 | |
3744 | |
3745 | |
3746 | |
3747 | if (isGenericLambdaCallOperatorSpecialization( |
3748 | dyn_cast<CXXMethodDecl>(this))) { |
3749 | assert(getPrimaryTemplate() && "not a generic lambda call operator?"); |
3750 | return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); |
3751 | } |
3752 | |
3753 | |
3754 | |
3755 | const FunctionDecl *FD = nullptr; |
3756 | if (!isDefined(FD, true)) |
3757 | FD = this; |
3758 | |
3759 | if (MemberSpecializationInfo *Info = FD->getMemberSpecializationInfo()) { |
3760 | if (ForDefinition && |
3761 | !clang::isTemplateInstantiation(Info->getTemplateSpecializationKind())) |
3762 | return nullptr; |
3763 | return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom())); |
3764 | } |
3765 | |
3766 | if (ForDefinition && |
3767 | !clang::isTemplateInstantiation(getTemplateSpecializationKind())) |
3768 | return nullptr; |
3769 | |
3770 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
3771 | |
3772 | |
3773 | while (!ForDefinition || !Primary->isMemberSpecialization()) { |
3774 | auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); |
3775 | if (!NewPrimary) |
3776 | break; |
3777 | Primary = NewPrimary; |
3778 | } |
3779 | |
3780 | return getDefinitionOrSelf(Primary->getTemplatedDecl()); |
3781 | } |
3782 | |
3783 | return nullptr; |
3784 | } |
3785 | |
3786 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
3787 | if (FunctionTemplateSpecializationInfo *Info |
3788 | = TemplateOrSpecialization |
3789 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
3790 | return Info->getTemplate(); |
3791 | } |
3792 | return nullptr; |
3793 | } |
3794 | |
3795 | FunctionTemplateSpecializationInfo * |
3796 | FunctionDecl::getTemplateSpecializationInfo() const { |
3797 | return TemplateOrSpecialization |
3798 | .dyn_cast<FunctionTemplateSpecializationInfo *>(); |
3799 | } |
3800 | |
3801 | const TemplateArgumentList * |
3802 | FunctionDecl::getTemplateSpecializationArgs() const { |
3803 | if (FunctionTemplateSpecializationInfo *Info |
3804 | = TemplateOrSpecialization |
3805 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
3806 | return Info->TemplateArguments; |
3807 | } |
3808 | return nullptr; |
3809 | } |
3810 | |
3811 | const ASTTemplateArgumentListInfo * |
3812 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
3813 | if (FunctionTemplateSpecializationInfo *Info |
3814 | = TemplateOrSpecialization |
3815 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
3816 | return Info->TemplateArgumentsAsWritten; |
3817 | } |
3818 | return nullptr; |
3819 | } |
3820 | |
3821 | void |
3822 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
3823 | FunctionTemplateDecl *Template, |
3824 | const TemplateArgumentList *TemplateArgs, |
3825 | void *InsertPos, |
3826 | TemplateSpecializationKind TSK, |
3827 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
3828 | SourceLocation PointOfInstantiation) { |
3829 | assert((TemplateOrSpecialization.isNull() || |
3830 | TemplateOrSpecialization.is<MemberSpecializationInfo *>()) && |
3831 | "Member function is already a specialization"); |
3832 | assert(TSK != TSK_Undeclared && |
3833 | "Must specify the type of function template specialization"); |
3834 | assert((TemplateOrSpecialization.isNull() || |
3835 | TSK == TSK_ExplicitSpecialization) && |
3836 | "Member specialization must be an explicit specialization"); |
3837 | FunctionTemplateSpecializationInfo *Info = |
3838 | FunctionTemplateSpecializationInfo::Create( |
3839 | C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, |
3840 | PointOfInstantiation, |
3841 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()); |
3842 | TemplateOrSpecialization = Info; |
3843 | Template->addSpecialization(Info, InsertPos); |
3844 | } |
3845 | |
3846 | void |
3847 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
3848 | const UnresolvedSetImpl &Templates, |
3849 | const TemplateArgumentListInfo &TemplateArgs) { |
3850 | assert(TemplateOrSpecialization.isNull()); |
3851 | DependentFunctionTemplateSpecializationInfo *Info = |
3852 | DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, |
| 1 | Calling 'DependentFunctionTemplateSpecializationInfo::Create' | |
|
3853 | TemplateArgs); |
3854 | TemplateOrSpecialization = Info; |
3855 | } |
3856 | |
3857 | DependentFunctionTemplateSpecializationInfo * |
3858 | FunctionDecl::getDependentSpecializationInfo() const { |
3859 | return TemplateOrSpecialization |
3860 | .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); |
3861 | } |
3862 | |
3863 | DependentFunctionTemplateSpecializationInfo * |
3864 | DependentFunctionTemplateSpecializationInfo::Create( |
3865 | ASTContext &Context, const UnresolvedSetImpl &Ts, |
3866 | const TemplateArgumentListInfo &TArgs) { |
3867 | void *Buffer = Context.Allocate( |
3868 | totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>( |
3869 | TArgs.size(), Ts.size())); |
3870 | return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs); |
| 2 | | Calling constructor for 'DependentFunctionTemplateSpecializationInfo' | |
|
3871 | } |
3872 | |
3873 | DependentFunctionTemplateSpecializationInfo:: |
3874 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
3875 | const TemplateArgumentListInfo &TArgs) |
3876 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
3877 | NumTemplates = Ts.size(); |
3878 | NumArgs = TArgs.size(); |
3879 | |
3880 | FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>(); |
3881 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 3 | | Assuming 'I' is equal to 'E' | |
|
| 4 | | Loop condition is false. Execution continues on line 3884 | |
|
3882 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
3883 | |
3884 | TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>(); |
| 5 | | Calling 'TrailingObjects::getTrailingObjects' | |
|
| 14 | | Returning from 'TrailingObjects::getTrailingObjects' | |
|
| 15 | | 'ArgsArray' initialized here | |
|
3885 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 16 | | Assuming 'I' is not equal to 'E' | |
|
| 17 | | Loop condition is true. Entering loop body | |
|
3886 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 18 | | Storage provided to placement new is only 0 bytes, whereas the allocated type requires 32 bytes |
|
3887 | } |
3888 | |
3889 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
3890 | |
3891 | |
3892 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
3893 | TemplateOrSpecialization |
3894 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
3895 | return FTSInfo->getTemplateSpecializationKind(); |
3896 | |
3897 | if (MemberSpecializationInfo *MSInfo = |
3898 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
3899 | return MSInfo->getTemplateSpecializationKind(); |
3900 | |
3901 | return TSK_Undeclared; |
3902 | } |
3903 | |
3904 | TemplateSpecializationKind |
3905 | FunctionDecl::getTemplateSpecializationKindForInstantiation() const { |
3906 | |
3907 | |
3908 | |
3909 | |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | |
3923 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
3924 | TemplateOrSpecialization |
3925 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) { |
3926 | if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) |
3927 | return MSInfo->getTemplateSpecializationKind(); |
3928 | return FTSInfo->getTemplateSpecializationKind(); |
3929 | } |
3930 | |
3931 | if (MemberSpecializationInfo *MSInfo = |
3932 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
3933 | return MSInfo->getTemplateSpecializationKind(); |
3934 | |
3935 | return TSK_Undeclared; |
3936 | } |
3937 | |
3938 | void |
3939 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
3940 | SourceLocation PointOfInstantiation) { |
3941 | if (FunctionTemplateSpecializationInfo *FTSInfo |
3942 | = TemplateOrSpecialization.dyn_cast< |
3943 | FunctionTemplateSpecializationInfo*>()) { |
3944 | FTSInfo->setTemplateSpecializationKind(TSK); |
3945 | if (TSK != TSK_ExplicitSpecialization && |
3946 | PointOfInstantiation.isValid() && |
3947 | FTSInfo->getPointOfInstantiation().isInvalid()) { |
3948 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
3949 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
3950 | L->InstantiationRequested(this); |
3951 | } |
3952 | } else if (MemberSpecializationInfo *MSInfo |
3953 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
3954 | MSInfo->setTemplateSpecializationKind(TSK); |
3955 | if (TSK != TSK_ExplicitSpecialization && |
3956 | PointOfInstantiation.isValid() && |
3957 | MSInfo->getPointOfInstantiation().isInvalid()) { |
3958 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
3959 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
3960 | L->InstantiationRequested(this); |
3961 | } |
3962 | } else |
3963 | llvm_unreachable("Function cannot have a template specialization kind"); |
3964 | } |
3965 | |
3966 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
3967 | if (FunctionTemplateSpecializationInfo *FTSInfo |
3968 | = TemplateOrSpecialization.dyn_cast< |
3969 | FunctionTemplateSpecializationInfo*>()) |
3970 | return FTSInfo->getPointOfInstantiation(); |
3971 | if (MemberSpecializationInfo *MSInfo = |
3972 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
3973 | return MSInfo->getPointOfInstantiation(); |
3974 | |
3975 | return SourceLocation(); |
3976 | } |
3977 | |
3978 | bool FunctionDecl::isOutOfLine() const { |
3979 | if (Decl::isOutOfLine()) |
3980 | return true; |
3981 | |
3982 | |
3983 | |
3984 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
3985 | const FunctionDecl *Definition; |
3986 | if (FD->hasBody(Definition)) |
3987 | return Definition->isOutOfLine(); |
3988 | } |
3989 | |
3990 | |
3991 | |
3992 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
3993 | const FunctionDecl *Definition; |
3994 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
3995 | return Definition->isOutOfLine(); |
3996 | } |
3997 | |
3998 | return false; |
3999 | } |
4000 | |
4001 | SourceRange FunctionDecl::getSourceRange() const { |
4002 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
4003 | } |
4004 | |
4005 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
4006 | IdentifierInfo *FnInfo = getIdentifier(); |
4007 | |
4008 | if (!FnInfo) |
4009 | return 0; |
4010 | |
4011 | |
4012 | switch (getBuiltinID()) { |
4013 | case Builtin::BI__builtin_memset: |
4014 | case Builtin::BI__builtin___memset_chk: |
4015 | case Builtin::BImemset: |
4016 | return Builtin::BImemset; |
4017 | |
4018 | case Builtin::BI__builtin_memcpy: |
4019 | case Builtin::BI__builtin___memcpy_chk: |
4020 | case Builtin::BImemcpy: |
4021 | return Builtin::BImemcpy; |
4022 | |
4023 | case Builtin::BI__builtin_mempcpy: |
4024 | case Builtin::BI__builtin___mempcpy_chk: |
4025 | case Builtin::BImempcpy: |
4026 | return Builtin::BImempcpy; |
4027 | |
4028 | case Builtin::BI__builtin_memmove: |
4029 | case Builtin::BI__builtin___memmove_chk: |
4030 | case Builtin::BImemmove: |
4031 | return Builtin::BImemmove; |
4032 | |
4033 | case Builtin::BIstrlcpy: |
4034 | case Builtin::BI__builtin___strlcpy_chk: |
4035 | return Builtin::BIstrlcpy; |
4036 | |
4037 | case Builtin::BIstrlcat: |
4038 | case Builtin::BI__builtin___strlcat_chk: |
4039 | return Builtin::BIstrlcat; |
4040 | |
4041 | case Builtin::BI__builtin_memcmp: |
4042 | case Builtin::BImemcmp: |
4043 | return Builtin::BImemcmp; |
4044 | |
4045 | case Builtin::BI__builtin_bcmp: |
4046 | case Builtin::BIbcmp: |
4047 | return Builtin::BIbcmp; |
4048 | |
4049 | case Builtin::BI__builtin_strncpy: |
4050 | case Builtin::BI__builtin___strncpy_chk: |
4051 | case Builtin::BIstrncpy: |
4052 | return Builtin::BIstrncpy; |
4053 | |
4054 | case Builtin::BI__builtin_strncmp: |
4055 | case Builtin::BIstrncmp: |
4056 | return Builtin::BIstrncmp; |
4057 | |
4058 | case Builtin::BI__builtin_strncasecmp: |
4059 | case Builtin::BIstrncasecmp: |
4060 | return Builtin::BIstrncasecmp; |
4061 | |
4062 | case Builtin::BI__builtin_strncat: |
4063 | case Builtin::BI__builtin___strncat_chk: |
4064 | case Builtin::BIstrncat: |
4065 | return Builtin::BIstrncat; |
4066 | |
4067 | case Builtin::BI__builtin_strndup: |
4068 | case Builtin::BIstrndup: |
4069 | return Builtin::BIstrndup; |
4070 | |
4071 | case Builtin::BI__builtin_strlen: |
4072 | case Builtin::BIstrlen: |
4073 | return Builtin::BIstrlen; |
4074 | |
4075 | case Builtin::BI__builtin_bzero: |
4076 | case Builtin::BIbzero: |
4077 | return Builtin::BIbzero; |
4078 | |
4079 | case Builtin::BIfree: |
4080 | return Builtin::BIfree; |
4081 | |
4082 | default: |
4083 | if (isExternC()) { |
4084 | if (FnInfo->isStr("memset")) |
4085 | return Builtin::BImemset; |
4086 | if (FnInfo->isStr("memcpy")) |
4087 | return Builtin::BImemcpy; |
4088 | if (FnInfo->isStr("mempcpy")) |
4089 | return Builtin::BImempcpy; |
4090 | if (FnInfo->isStr("memmove")) |
4091 | return Builtin::BImemmove; |
4092 | if (FnInfo->isStr("memcmp")) |
4093 | return Builtin::BImemcmp; |
4094 | if (FnInfo->isStr("bcmp")) |
4095 | return Builtin::BIbcmp; |
4096 | if (FnInfo->isStr("strncpy")) |
4097 | return Builtin::BIstrncpy; |
4098 | if (FnInfo->isStr("strncmp")) |
4099 | return Builtin::BIstrncmp; |
4100 | if (FnInfo->isStr("strncasecmp")) |
4101 | return Builtin::BIstrncasecmp; |
4102 | if (FnInfo->isStr("strncat")) |
4103 | return Builtin::BIstrncat; |
4104 | if (FnInfo->isStr("strndup")) |
4105 | return Builtin::BIstrndup; |
4106 | if (FnInfo->isStr("strlen")) |
4107 | return Builtin::BIstrlen; |
4108 | if (FnInfo->isStr("bzero")) |
4109 | return Builtin::BIbzero; |
4110 | } else if (isInStdNamespace()) { |
4111 | if (FnInfo->isStr("free")) |
4112 | return Builtin::BIfree; |
4113 | } |
4114 | break; |
4115 | } |
4116 | return 0; |
4117 | } |
4118 | |
4119 | unsigned FunctionDecl::getODRHash() const { |
4120 | assert(hasODRHash()); |
4121 | return ODRHash; |
4122 | } |
4123 | |
4124 | unsigned FunctionDecl::getODRHash() { |
4125 | if (hasODRHash()) |
4126 | return ODRHash; |
4127 | |
4128 | if (auto *FT = getInstantiatedFromMemberFunction()) { |
4129 | setHasODRHash(true); |
4130 | ODRHash = FT->getODRHash(); |
4131 | return ODRHash; |
4132 | } |
4133 | |
4134 | class ODRHash Hash; |
4135 | Hash.AddFunctionDecl(this); |
4136 | setHasODRHash(true); |
4137 | ODRHash = Hash.CalculateHash(); |
4138 | return ODRHash; |
4139 | } |
4140 | |
4141 | |
4142 | |
4143 | |
4144 | |
4145 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
4146 | SourceLocation StartLoc, SourceLocation IdLoc, |
4147 | IdentifierInfo *Id, QualType T, |
4148 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
4149 | InClassInitStyle InitStyle) { |
4150 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
4151 | BW, Mutable, InitStyle); |
4152 | } |
4153 | |
4154 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4155 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
4156 | SourceLocation(), nullptr, QualType(), nullptr, |
4157 | nullptr, false, ICIS_NoInit); |
4158 | } |
4159 | |
4160 | bool FieldDecl::isAnonymousStructOrUnion() const { |
4161 | if (!isImplicit() || getDeclName()) |
4162 | return false; |
4163 | |
4164 | if (const auto *Record = getType()->getAs<RecordType>()) |
4165 | return Record->getDecl()->isAnonymousStructOrUnion(); |
4166 | |
4167 | return false; |
4168 | } |
4169 | |
4170 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
4171 | assert(isBitField() && "not a bitfield"); |
4172 | return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); |
4173 | } |
4174 | |
4175 | bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { |
4176 | return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && |
4177 | getBitWidthValue(Ctx) == 0; |
4178 | } |
4179 | |
4180 | bool FieldDecl::isZeroSize(const ASTContext &Ctx) const { |
4181 | if (isZeroLengthBitField(Ctx)) |
4182 | return true; |
4183 | |
4184 | |
4185 | |
4186 | |
4187 | if (!hasAttr<NoUniqueAddressAttr>()) |
4188 | return false; |
4189 | |
4190 | |
4191 | const auto *RT = getType()->getAs<RecordType>(); |
4192 | if (!RT) |
4193 | return false; |
4194 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
4195 | if (!RD) { |
4196 | assert(isInvalidDecl() && "valid field has incomplete type"); |
4197 | return false; |
4198 | } |
4199 | |
4200 | |
4201 | |
4202 | const auto *CXXRD = cast<CXXRecordDecl>(RD); |
4203 | if (!CXXRD->isEmpty()) |
4204 | return false; |
4205 | |
4206 | |
4207 | |
4208 | |
4209 | |
4210 | return true; |
4211 | } |
4212 | |
4213 | unsigned FieldDecl::getFieldIndex() const { |
4214 | const FieldDecl *Canonical = getCanonicalDecl(); |
4215 | if (Canonical != this) |
4216 | return Canonical->getFieldIndex(); |
4217 | |
4218 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
4219 | |
4220 | unsigned Index = 0; |
4221 | const RecordDecl *RD = getParent()->getDefinition(); |
4222 | assert(RD && "requested index for field of struct with no definition"); |
4223 | |
4224 | for (auto *Field : RD->fields()) { |
4225 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
4226 | ++Index; |
4227 | } |
4228 | |
4229 | assert(CachedFieldIndex && "failed to find field in parent"); |
4230 | return CachedFieldIndex - 1; |
4231 | } |
4232 | |
4233 | SourceRange FieldDecl::getSourceRange() const { |
4234 | const Expr *FinalExpr = getInClassInitializer(); |
4235 | if (!FinalExpr) |
4236 | FinalExpr = getBitWidth(); |
4237 | if (FinalExpr) |
4238 | return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); |
4239 | return DeclaratorDecl::getSourceRange(); |
4240 | } |
4241 | |
4242 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
4243 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
4244 | "capturing type in non-lambda or captured record."); |
4245 | assert(InitStorage.getInt() == ISK_NoInit && |
4246 | InitStorage.getPointer() == nullptr && |
4247 | "bit width, initializer or captured type already set"); |
4248 | InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), |
4249 | ISK_CapturedVLAType); |
4250 | } |
4251 | |
4252 | |
4253 | |
4254 | |
4255 | |
4256 | TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
4257 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
4258 | SourceLocation StartL) |
4259 | : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), |
4260 | TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { |
4261 | assert((DK != Enum || TK == TTK_Enum) && |
4262 | "EnumDecl not matched with TTK_Enum"); |
4263 | setPreviousDecl(PrevDecl); |
4264 | setTagKind(TK); |
4265 | setCompleteDefinition(false); |
4266 | setBeingDefined(false); |
4267 | setEmbeddedInDeclarator(false); |
4268 | setFreeStanding(false); |
4269 | setCompleteDefinitionRequired(false); |
4270 | } |
4271 | |
4272 | SourceLocation TagDecl::getOuterLocStart() const { |
4273 | return getTemplateOrInnerLocStart(this); |
4274 | } |
4275 | |
4276 | SourceRange TagDecl::getSourceRange() const { |
4277 | SourceLocation RBraceLoc = BraceRange.getEnd(); |
4278 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
4279 | return SourceRange(getOuterLocStart(), E); |
4280 | } |
4281 | |
4282 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
4283 | |
4284 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
4285 | TypedefNameDeclOrQualifier = TDD; |
4286 | if (const Type *T = getTypeForDecl()) { |
4287 | (void)T; |
4288 | assert(T->isLinkageValid()); |
4289 | } |
4290 | assert(isLinkageValid()); |
4291 | } |
4292 | |
4293 | void TagDecl::startDefinition() { |
4294 | setBeingDefined(true); |
4295 | |
4296 | if (auto *D = dyn_cast<CXXRecordDecl>(this)) { |
4297 | struct CXXRecordDecl::DefinitionData *Data = |
4298 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
4299 | for (auto I : redecls()) |
4300 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
4301 | } |
4302 | } |
4303 | |
4304 | void TagDecl::completeDefinition() { |
4305 | assert((!isa<CXXRecordDecl>(this) || |
4306 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
4307 | "definition completed but not started"); |
4308 | |
4309 | setCompleteDefinition(true); |
4310 | setBeingDefined(false); |
4311 | |
4312 | if (ASTMutationListener *L = getASTMutationListener()) |
4313 | L->CompletedTagDefinition(this); |
4314 | } |
4315 | |
4316 | TagDecl *TagDecl::getDefinition() const { |
4317 | if (isCompleteDefinition()) |
4318 | return const_cast<TagDecl *>(this); |
4319 | |
4320 | |
4321 | if (mayHaveOutOfDateDef()) { |
4322 | if (IdentifierInfo *II = getIdentifier()) { |
4323 | if (II->isOutOfDate()) { |
4324 | updateOutOfDate(*II); |
4325 | } |
4326 | } |
4327 | } |
4328 | |
4329 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
4330 | return CXXRD->getDefinition(); |
4331 | |
4332 | for (auto R : redecls()) |
4333 | if (R->isCompleteDefinition()) |
4334 | return R; |
4335 | |
4336 | return nullptr; |
4337 | } |
4338 | |
4339 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
4340 | if (QualifierLoc) { |
4341 | |
4342 | if (!hasExtInfo()) |
4343 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
4344 | |
4345 | getExtInfo()->QualifierLoc = QualifierLoc; |
4346 | } else { |
4347 | |
4348 | if (hasExtInfo()) { |
4349 | if (getExtInfo()->NumTemplParamLists == 0) { |
4350 | getASTContext().Deallocate(getExtInfo()); |
4351 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
4352 | } |
4353 | else |
4354 | getExtInfo()->QualifierLoc = QualifierLoc; |
4355 | } |
4356 | } |
4357 | } |
4358 | |
4359 | void TagDecl::setTemplateParameterListsInfo( |
4360 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
4361 | assert(!TPLists.empty()); |
4362 | |
4363 | if (!hasExtInfo()) |
4364 | |
4365 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
4366 | |
4367 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
4368 | } |
4369 | |
4370 | |
4371 | |
4372 | |
4373 | |
4374 | EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
4375 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
4376 | bool Scoped, bool ScopedUsingClassTag, bool Fixed) |
4377 | : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
4378 | assert(Scoped || !ScopedUsingClassTag); |
4379 | IntegerType = nullptr; |
4380 | setNumPositiveBits(0); |
4381 | setNumNegativeBits(0); |
4382 | setScoped(Scoped); |
4383 | setScopedUsingClassTag(ScopedUsingClassTag); |
4384 | setFixed(Fixed); |
4385 | setHasODRHash(false); |
4386 | ODRHash = 0; |
4387 | } |
4388 | |
4389 | void EnumDecl::anchor() {} |
4390 | |
4391 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
4392 | SourceLocation StartLoc, SourceLocation IdLoc, |
4393 | IdentifierInfo *Id, |
4394 | EnumDecl *PrevDecl, bool IsScoped, |
4395 | bool IsScopedUsingClassTag, bool IsFixed) { |
4396 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
4397 | IsScoped, IsScopedUsingClassTag, IsFixed); |
4398 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4399 | C.getTypeDeclType(Enum, PrevDecl); |
4400 | return Enum; |
4401 | } |
4402 | |
4403 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4404 | EnumDecl *Enum = |
4405 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
4406 | nullptr, nullptr, false, false, false); |
4407 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4408 | return Enum; |
4409 | } |
4410 | |
4411 | SourceRange EnumDecl::getIntegerTypeRange() const { |
4412 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
4413 | return TI->getTypeLoc().getSourceRange(); |
4414 | return SourceRange(); |
4415 | } |
4416 | |
4417 | void EnumDecl::completeDefinition(QualType NewType, |
4418 | QualType NewPromotionType, |
4419 | unsigned NumPositiveBits, |
4420 | unsigned NumNegativeBits) { |
4421 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
4422 | if (!IntegerType) |
4423 | IntegerType = NewType.getTypePtr(); |
4424 | PromotionType = NewPromotionType; |
4425 | setNumPositiveBits(NumPositiveBits); |
4426 | setNumNegativeBits(NumNegativeBits); |
4427 | TagDecl::completeDefinition(); |
4428 | } |
4429 | |
4430 | bool EnumDecl::isClosed() const { |
4431 | if (const auto *A = getAttr<EnumExtensibilityAttr>()) |
4432 | return A->getExtensibility() == EnumExtensibilityAttr::Closed; |
4433 | return true; |
4434 | } |
4435 | |
4436 | bool EnumDecl::isClosedFlag() const { |
4437 | return isClosed() && hasAttr<FlagEnumAttr>(); |
4438 | } |
4439 | |
4440 | bool EnumDecl::isClosedNonFlag() const { |
4441 | return isClosed() && !hasAttr<FlagEnumAttr>(); |
4442 | } |
4443 | |
4444 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
4445 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
4446 | return MSI->getTemplateSpecializationKind(); |
4447 | |
4448 | return TSK_Undeclared; |
4449 | } |
4450 | |
4451 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
4452 | SourceLocation PointOfInstantiation) { |
4453 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
4454 | assert(MSI && "Not an instantiated member enumeration?"); |
4455 | MSI->setTemplateSpecializationKind(TSK); |
4456 | if (TSK != TSK_ExplicitSpecialization && |
4457 | PointOfInstantiation.isValid() && |
4458 | MSI->getPointOfInstantiation().isInvalid()) |
4459 | MSI->setPointOfInstantiation(PointOfInstantiation); |
4460 | } |
4461 | |
4462 | EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { |
4463 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
4464 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
4465 | EnumDecl *ED = getInstantiatedFromMemberEnum(); |
4466 | while (auto *NewED = ED->getInstantiatedFromMemberEnum()) |
4467 | ED = NewED; |
4468 | return getDefinitionOrSelf(ED); |
4469 | } |
4470 | } |
4471 | |
4472 | assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && |
4473 | "couldn't find pattern for enum instantiation"); |
4474 | return nullptr; |
4475 | } |
4476 | |
4477 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
4478 | if (SpecializationInfo) |
4479 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
4480 | |
4481 | return nullptr; |
4482 | } |
4483 | |
4484 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
4485 | TemplateSpecializationKind TSK) { |
4486 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
4487 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
4488 | } |
4489 | |
4490 | unsigned EnumDecl::getODRHash() { |
4491 | if (hasODRHash()) |
4492 | return ODRHash; |
4493 | |
4494 | class ODRHash Hash; |
4495 | Hash.AddEnumDecl(this); |
4496 | setHasODRHash(true); |
4497 | ODRHash = Hash.CalculateHash(); |
4498 | return ODRHash; |
4499 | } |
4500 | |
4501 | |
4502 | |
4503 | |
4504 | |
4505 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
4506 | DeclContext *DC, SourceLocation StartLoc, |
4507 | SourceLocation IdLoc, IdentifierInfo *Id, |
4508 | RecordDecl *PrevDecl) |
4509 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
4510 | assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); |
4511 | setHasFlexibleArrayMember(false); |
4512 | setAnonymousStructOrUnion(false); |
4513 | setHasObjectMember(false); |
4514 | setHasVolatileMember(false); |
4515 | setHasLoadedFieldsFromExternalStorage(false); |
4516 | setNonTrivialToPrimitiveDefaultInitialize(false); |
4517 | setNonTrivialToPrimitiveCopy(false); |
4518 | setNonTrivialToPrimitiveDestroy(false); |
4519 | setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); |
4520 | setHasNonTrivialToPrimitiveDestructCUnion(false); |
4521 | setHasNonTrivialToPrimitiveCopyCUnion(false); |
4522 | setParamDestroyedInCallee(false); |
4523 | setArgPassingRestrictions(APK_CanPassInRegs); |
4524 | } |
4525 | |
4526 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
4527 | SourceLocation StartLoc, SourceLocation IdLoc, |
4528 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
4529 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
4530 | StartLoc, IdLoc, Id, PrevDecl); |
4531 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4532 | |
4533 | C.getTypeDeclType(R, PrevDecl); |
4534 | return R; |
4535 | } |
4536 | |
4537 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
4538 | RecordDecl *R = |
4539 | new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), |
4540 | SourceLocation(), nullptr, nullptr); |
4541 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4542 | return R; |
4543 | } |
4544 | |
4545 | bool RecordDecl::isInjectedClassName() const { |
4546 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
4547 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
4548 | } |
4549 | |
4550 | bool RecordDecl::isLambda() const { |
4551 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
4552 | return RD->isLambda(); |
4553 | return false; |
4554 | } |
4555 | |
4556 | bool RecordDecl::isCapturedRecord() const { |
4557 | return hasAttr<CapturedRecordAttr>(); |
4558 | } |
4559 | |
4560 | void RecordDecl::setCapturedRecord() { |
4561 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
4562 | } |
4563 | |
4564 | bool RecordDecl::isOrContainsUnion() const { |
4565 | if (isUnion()) |
4566 | return true; |
4567 | |
4568 | if (const RecordDecl *Def = getDefinition()) { |
4569 | for (const FieldDecl *FD : Def->fields()) { |
4570 | const RecordType *RT = FD->getType()->getAs<RecordType>(); |
4571 | if (RT && RT->getDecl()->isOrContainsUnion()) |
4572 | return true; |
4573 | } |
4574 | } |
4575 | |
4576 | return false; |
4577 | } |
4578 | |
4579 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
4580 | if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) |
4581 | LoadFieldsFromExternalStorage(); |
4582 | |
4583 | return field_iterator(decl_iterator(FirstDecl)); |
4584 | } |
4585 | |
4586 | |
4587 | |
4588 | void RecordDecl::completeDefinition() { |
4589 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
4590 | TagDecl::completeDefinition(); |
4591 | |
4592 | ASTContext &Ctx = getASTContext(); |
4593 | |
4594 | |
4595 | |
4596 | if (Ctx.getLangOpts().DumpRecordLayoutsComplete) |
4597 | (void)Ctx.getASTRecordLayout(this); |
4598 | } |
4599 | |
4600 | |
4601 | |
4602 | |
4603 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
4604 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
4605 | } |
4606 | |
4607 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
4608 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
4609 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
4610 | |
4611 | |
4612 | ExternalASTSource::Deserializing TheFields(Source); |
4613 | |
4614 | SmallVector<Decl*, 64> Decls; |
4615 | setHasLoadedFieldsFromExternalStorage(true); |
4616 | Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { |
4617 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
4618 | }, Decls); |
4619 | |
4620 | #ifndef NDEBUG |
4621 | |
4622 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
4623 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
4624 | #endif |
4625 | |
4626 | if (Decls.empty()) |
4627 | return; |
4628 | |
4629 | std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
4630 | false); |
4631 | } |
4632 | |
4633 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
4634 | ASTContext &Context = getASTContext(); |
4635 | const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & |
4636 | (SanitizerKind::Address | SanitizerKind::KernelAddress); |
4637 | if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) |
4638 | return false; |
4639 | const auto &NoSanitizeList = Context.getNoSanitizeList(); |
4640 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); |
4641 | |
4642 | int ReasonToReject = -1; |
4643 | if (!CXXRD || CXXRD->isExternCContext()) |
4644 | ReasonToReject = 0; |
4645 | else if (CXXRD->hasAttr<PackedAttr>()) |
4646 | ReasonToReject = 1; |
4647 | else if (CXXRD->isUnion()) |
4648 | ReasonToReject = 2; |
4649 | else if (CXXRD->isTriviallyCopyable()) |
4650 | ReasonToReject = 3; |
4651 | else if (CXXRD->hasTrivialDestructor()) |
4652 | ReasonToReject = 4; |
4653 | else if (CXXRD->isStandardLayout()) |
4654 | ReasonToReject = 5; |
4655 | else if (NoSanitizeList.containsLocation(EnabledAsanMask, getLocation(), |
4656 | "field-padding")) |
4657 | ReasonToReject = 6; |
4658 | else if (NoSanitizeList.containsType( |
4659 | EnabledAsanMask, getQualifiedNameAsString(), "field-padding")) |
4660 | ReasonToReject = 7; |
4661 | |
4662 | if (EmitRemark) { |
4663 | if (ReasonToReject >= 0) |
4664 | Context.getDiagnostics().Report( |
4665 | getLocation(), |
4666 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
4667 | << getQualifiedNameAsString() << ReasonToReject; |
4668 | else |
4669 | Context.getDiagnostics().Report( |
4670 | getLocation(), |
4671 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
4672 | << getQualifiedNameAsString(); |
4673 | } |
4674 | return ReasonToReject < 0; |
4675 | } |
4676 | |
4677 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
4678 | for (const auto *I : fields()) { |
4679 | if (I->getIdentifier()) |
4680 | return I; |
4681 | |
4682 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
4683 | if (const FieldDecl *NamedDataMember = |
4684 | RT->getDecl()->findFirstNamedDataMember()) |
4685 | return NamedDataMember; |
4686 | } |
4687 | |
4688 | |
4689 | return nullptr; |
4690 | } |
4691 | |
4692 | |
4693 | |
4694 | |
4695 | |
4696 | BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) |
4697 | : Decl(Block, DC, CaretLoc), DeclContext(Block) { |
4698 | setIsVariadic(false); |
4699 | setCapturesCXXThis(false); |
4700 | setBlockMissingReturnType(true); |
4701 | setIsConversionFromLambda(false); |
4702 | setDoesNotEscape(false); |
4703 | setCanAvoidCopyToHeap(false); |
4704 | } |
4705 | |
4706 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
4707 | assert(!ParamInfo && "Already has param info!"); |
4708 | |
4709 | |
4710 | if (!NewParamInfo.empty()) { |
4711 | NumParams = NewParamInfo.size(); |
4712 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
4713 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
4714 | } |
4715 | } |
4716 | |
4717 | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
4718 | bool CapturesCXXThis) { |
4719 | this->setCapturesCXXThis(CapturesCXXThis); |
4720 | this->NumCaptures = Captures.size(); |
4721 | |
4722 | if (Captures.empty()) { |
4723 | this->Captures = nullptr; |
4724 | return; |
4725 | } |
4726 | |
4727 | this->Captures = Captures.copy(Context).data(); |
4728 | } |
4729 | |
4730 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
4731 | for (const auto &I : captures()) |
4732 | |
4733 | if (I.getVariable() == variable) |
4734 | return true; |
4735 | |
4736 | return false; |
4737 | } |
4738 | |
4739 | SourceRange BlockDecl::getSourceRange() const { |
4740 | return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); |
4741 | } |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | |
4747 | void TranslationUnitDecl::anchor() {} |
4748 | |
4749 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
4750 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
4751 | } |
4752 | |
4753 | void PragmaCommentDecl::anchor() {} |
4754 | |
4755 | PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, |
4756 | TranslationUnitDecl *DC, |
4757 | SourceLocation CommentLoc, |
4758 | PragmaMSCommentKind CommentKind, |
4759 | StringRef Arg) { |
4760 | PragmaCommentDecl *PCD = |
4761 | new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) |
4762 | PragmaCommentDecl(DC, CommentLoc, CommentKind); |
4763 | memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); |
4764 | PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; |
4765 | return PCD; |
4766 | } |
4767 | |
4768 | PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, |
4769 | unsigned ID, |
4770 | unsigned ArgSize) { |
4771 | return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) |
4772 | PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); |
4773 | } |
4774 | |
4775 | void PragmaDetectMismatchDecl::anchor() {} |
4776 | |
4777 | PragmaDetectMismatchDecl * |
4778 | PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, |
4779 | SourceLocation Loc, StringRef Name, |
4780 | StringRef Value) { |
4781 | size_t ValueStart = Name.size() + 1; |
4782 | PragmaDetectMismatchDecl *PDMD = |
4783 | new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) |
4784 | PragmaDetectMismatchDecl(DC, Loc, ValueStart); |
4785 | memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); |
4786 | PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; |
4787 | memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), |
4788 | Value.size()); |
4789 | PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; |
4790 | return PDMD; |
4791 | } |
4792 | |
4793 | PragmaDetectMismatchDecl * |
4794 | PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
4795 | unsigned NameValueSize) { |
4796 | return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) |
4797 | PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); |
4798 | } |
4799 | |
4800 | void ExternCContextDecl::anchor() {} |
4801 | |
4802 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
4803 | TranslationUnitDecl *DC) { |
4804 | return new (C, DC) ExternCContextDecl(DC); |
4805 | } |
4806 | |
4807 | void LabelDecl::anchor() {} |
4808 | |
4809 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
4810 | SourceLocation IdentL, IdentifierInfo *II) { |
4811 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
4812 | } |
4813 | |
4814 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
4815 | SourceLocation IdentL, IdentifierInfo *II, |
4816 | SourceLocation GnuLabelL) { |
4817 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
4818 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
4819 | } |
4820 | |
4821 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4822 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
4823 | SourceLocation()); |
4824 | } |
4825 | |
4826 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
4827 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
4828 | memcpy(Buffer, Name.data(), Name.size()); |
4829 | Buffer[Name.size()] = '\0'; |
4830 | MSAsmName = Buffer; |
4831 | } |
4832 | |
4833 | void ValueDecl::anchor() {} |
4834 | |
4835 | bool ValueDecl::isWeak() const { |
4836 | auto *MostRecent = getMostRecentDecl(); |
4837 | return MostRecent->hasAttr<WeakAttr>() || |
4838 | MostRecent->hasAttr<WeakRefAttr>() || isWeakImported(); |
4839 | } |
4840 | |
4841 | void ImplicitParamDecl::anchor() {} |
4842 | |
4843 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
4844 | SourceLocation IdLoc, |
4845 | IdentifierInfo *Id, QualType Type, |
4846 | ImplicitParamKind ParamKind) { |
4847 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); |
4848 | } |
4849 | |
4850 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, |
4851 | ImplicitParamKind ParamKind) { |
4852 | return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); |
4853 | } |
4854 | |
4855 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
4856 | unsigned ID) { |
4857 | return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); |
4858 | } |
4859 | |
4860 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
4861 | SourceLocation StartLoc, |
4862 | const DeclarationNameInfo &NameInfo, |
4863 | QualType T, TypeSourceInfo *TInfo, |
4864 | StorageClass SC, bool isInlineSpecified, |
4865 | bool hasWrittenPrototype, |
4866 | ConstexprSpecKind ConstexprKind, |
4867 | Expr *TrailingRequiresClause) { |
4868 | FunctionDecl *New = |
4869 | new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, |
4870 | SC, isInlineSpecified, ConstexprKind, |
4871 | TrailingRequiresClause); |
4872 | New->setHasWrittenPrototype(hasWrittenPrototype); |
4873 | return New; |
4874 | } |
4875 | |
4876 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4877 | return new (C, ID) FunctionDecl( |
4878 | Function, C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), |
4879 | nullptr, SC_None, false, ConstexprSpecKind::Unspecified, nullptr); |
4880 | } |
4881 | |
4882 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
4883 | return new (C, DC) BlockDecl(DC, L); |
4884 | } |
4885 | |
4886 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4887 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
4888 | } |
4889 | |
4890 | CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) |
4891 | : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), |
4892 | NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} |
4893 | |
4894 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
4895 | unsigned NumParams) { |
4896 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
4897 | CapturedDecl(DC, NumParams); |
4898 | } |
4899 | |
4900 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
4901 | unsigned NumParams) { |
4902 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
4903 | CapturedDecl(nullptr, NumParams); |
4904 | } |
4905 | |
4906 | Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } |
4907 | void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
4908 | |
4909 | bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
4910 | void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } |
4911 | |
4912 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
4913 | SourceLocation L, |
4914 | IdentifierInfo *Id, QualType T, |
4915 | Expr *E, const llvm::APSInt &V) { |
4916 | return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); |
4917 | } |
4918 | |
4919 | EnumConstantDecl * |
4920 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4921 | return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, |
4922 | QualType(), nullptr, llvm::APSInt()); |
4923 | } |
4924 | |
4925 | void IndirectFieldDecl::anchor() {} |
4926 | |
4927 | IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, |
4928 | SourceLocation L, DeclarationName N, |
4929 | QualType T, |
4930 | MutableArrayRef<NamedDecl *> CH) |
4931 | : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), |
4932 | ChainingSize(CH.size()) { |
4933 | |
4934 | |
4935 | if (C.getLangOpts().CPlusPlus) |
4936 | IdentifierNamespace |= IDNS_Tag; |
4937 | } |
4938 | |
4939 | IndirectFieldDecl * |
4940 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
4941 | IdentifierInfo *Id, QualType T, |
4942 | llvm::MutableArrayRef<NamedDecl *> CH) { |
4943 | return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); |
4944 | } |
4945 | |
4946 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
4947 | unsigned ID) { |
4948 | return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(), |
4949 | DeclarationName(), QualType(), None); |
4950 | } |
4951 | |
4952 | SourceRange EnumConstantDecl::getSourceRange() const { |
4953 | SourceLocation End = getLocation(); |
4954 | if (Init) |
4955 | End = Init->getEndLoc(); |
4956 | return SourceRange(getLocation(), End); |
4957 | } |
4958 | |
4959 | void TypeDecl::anchor() {} |
4960 | |
4961 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
4962 | SourceLocation StartLoc, SourceLocation IdLoc, |
4963 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
4964 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
4965 | } |
4966 | |
4967 | void TypedefNameDecl::anchor() {} |
4968 | |
4969 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
4970 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
4971 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
4972 | auto *ThisTypedef = this; |
4973 | if (AnyRedecl && OwningTypedef) { |
4974 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
4975 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
4976 | } |
4977 | if (OwningTypedef == ThisTypedef) |
4978 | return TT->getDecl(); |
4979 | } |
4980 | |
4981 | return nullptr; |
4982 | } |
4983 | |
4984 | bool TypedefNameDecl::isTransparentTagSlow() const { |
4985 | auto determineIsTransparent = [&]() { |
4986 | if (auto *TT = getUnderlyingType()->getAs<TagType>()) { |
4987 | if (auto *TD = TT->getDecl()) { |
4988 | if (TD->getName() != getName()) |
4989 | return false; |
4990 | SourceLocation TTLoc = getLocation(); |
4991 | SourceLocation TDLoc = TD->getLocation(); |
4992 | if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) |
4993 | return false; |
4994 | SourceManager &SM = getASTContext().getSourceManager(); |
4995 | return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); |
4996 | } |
4997 | } |
4998 | return false; |
4999 | }; |
5000 | |
5001 | bool isTransparent = determineIsTransparent(); |
5002 | MaybeModedTInfo.setInt((isTransparent << 1) | 1); |
5003 | return isTransparent; |
5004 | } |
5005 | |
5006 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5007 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
5008 | nullptr, nullptr); |
5009 | } |
5010 | |
5011 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
5012 | SourceLocation StartLoc, |
5013 | SourceLocation IdLoc, IdentifierInfo *Id, |
5014 | TypeSourceInfo *TInfo) { |
5015 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
5016 | } |
5017 | |
5018 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5019 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
5020 | SourceLocation(), nullptr, nullptr); |
5021 | } |
5022 | |
5023 | SourceRange TypedefDecl::getSourceRange() const { |
5024 | SourceLocation RangeEnd = getLocation(); |
5025 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
5026 | if (typeIsPostfix(TInfo->getType())) |
5027 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
5028 | } |
5029 | return SourceRange(getBeginLoc(), RangeEnd); |
5030 | } |
5031 | |
5032 | SourceRange TypeAliasDecl::getSourceRange() const { |
5033 | SourceLocation RangeEnd = getBeginLoc(); |
5034 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
5035 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
5036 | return SourceRange(getBeginLoc(), RangeEnd); |
5037 | } |
5038 | |
5039 | void FileScopeAsmDecl::anchor() {} |
5040 | |
5041 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
5042 | StringLiteral *Str, |
5043 | SourceLocation AsmLoc, |
5044 | SourceLocation RParenLoc) { |
5045 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
5046 | } |
5047 | |
5048 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
5049 | unsigned ID) { |
5050 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
5051 | SourceLocation()); |
5052 | } |
5053 | |
5054 | void EmptyDecl::anchor() {} |
5055 | |
5056 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
5057 | return new (C, DC) EmptyDecl(DC, L); |
5058 | } |
5059 | |
5060 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5061 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
5062 | } |
5063 | |
5064 | |
5065 | |
5066 | |
5067 | |
5068 | |
5069 | |
5070 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
5071 | unsigned Result = 1; |
5072 | while (Mod->Parent) { |
5073 | Mod = Mod->Parent; |
5074 | ++Result; |
5075 | } |
5076 | return Result; |
5077 | } |
5078 | |
5079 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
5080 | Module *Imported, |
5081 | ArrayRef<SourceLocation> IdentifierLocs) |
5082 | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
5083 | NextLocalImportAndComplete(nullptr, true) { |
5084 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
5085 | auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
5086 | std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), |
5087 | StoredLocs); |
5088 | } |
5089 | |
5090 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
5091 | Module *Imported, SourceLocation EndLoc) |
5092 | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
5093 | NextLocalImportAndComplete(nullptr, false) { |
5094 | *getTrailingObjects<SourceLocation>() = EndLoc; |
5095 | } |
5096 | |
5097 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
5098 | SourceLocation StartLoc, Module *Imported, |
5099 | ArrayRef<SourceLocation> IdentifierLocs) { |
5100 | return new (C, DC, |
5101 | additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) |
5102 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
5103 | } |
5104 | |
5105 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
5106 | SourceLocation StartLoc, |
5107 | Module *Imported, |
5108 | SourceLocation EndLoc) { |
5109 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) |
5110 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
5111 | Import->setImplicit(); |
5112 | return Import; |
5113 | } |
5114 | |
5115 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
5116 | unsigned NumLocations) { |
5117 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) |
5118 | ImportDecl(EmptyShell()); |
5119 | } |
5120 | |
5121 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
5122 | if (!isImportComplete()) |
5123 | return None; |
5124 | |
5125 | const auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
5126 | return llvm::makeArrayRef(StoredLocs, |
5127 | getNumModuleIdentifiers(getImportedModule())); |
5128 | } |
5129 | |
5130 | SourceRange ImportDecl::getSourceRange() const { |
5131 | if (!isImportComplete()) |
5132 | return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); |
5133 | |
5134 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
5135 | } |
5136 | |
5137 | |
5138 | |
5139 | |
5140 | |
5141 | void ExportDecl::anchor() {} |
5142 | |
5143 | ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, |
5144 | SourceLocation ExportLoc) { |
5145 | return new (C, DC) ExportDecl(DC, ExportLoc); |
5146 | } |
5147 | |
5148 | ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5149 | return new (C, ID) ExportDecl(nullptr, SourceLocation()); |
5150 | } |