clang  7.0.0svn
DeclBase.cpp
Go to the documentation of this file.
1 //===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Decl and DeclContext classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/DeclBase.h"
15 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/AttrIterator.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclFriend.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/DeclOpenMP.h"
25 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/Stmt.h"
29 #include "clang/AST/Type.h"
31 #include "clang/Basic/LLVM.h"
36 #include "clang/Basic/TargetInfo.h"
38 #include "llvm/ADT/ArrayRef.h"
39 #include "llvm/ADT/PointerIntPair.h"
40 #include "llvm/ADT/SmallVector.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/MathExtras.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include <algorithm>
47 #include <cassert>
48 #include <cstddef>
49 #include <string>
50 #include <tuple>
51 #include <utility>
52 
53 using namespace clang;
54 
55 //===----------------------------------------------------------------------===//
56 // Statistics
57 //===----------------------------------------------------------------------===//
58 
59 #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
60 #define ABSTRACT_DECL(DECL)
61 #include "clang/AST/DeclNodes.inc"
62 
65 }
66 
67 #define DECL(DERIVED, BASE) \
68  static_assert(alignof(Decl) >= alignof(DERIVED##Decl), \
69  "Alignment sufficient after objects prepended to " #DERIVED);
70 #define ABSTRACT_DECL(DECL)
71 #include "clang/AST/DeclNodes.inc"
72 
73 void *Decl::operator new(std::size_t Size, const ASTContext &Context,
74  unsigned ID, std::size_t Extra) {
75  // Allocate an extra 8 bytes worth of storage, which ensures that the
76  // resulting pointer will still be 8-byte aligned.
77  static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
78  "Decl won't be misaligned");
79  void *Start = Context.Allocate(Size + Extra + 8);
80  void *Result = (char*)Start + 8;
81 
82  unsigned *PrefixPtr = (unsigned *)Result - 2;
83 
84  // Zero out the first 4 bytes; this is used to store the owning module ID.
85  PrefixPtr[0] = 0;
86 
87  // Store the global declaration ID in the second 4 bytes.
88  PrefixPtr[1] = ID;
89 
90  return Result;
91 }
92 
93 void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
94  DeclContext *Parent, std::size_t Extra) {
95  assert(!Parent || &Parent->getParentASTContext() == &Ctx);
96  // With local visibility enabled, we track the owning module even for local
97  // declarations. We create the TU decl early and may not yet know what the
98  // LangOpts are, so conservatively allocate the storage.
99  if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) {
100  // Ensure required alignment of the resulting object by adding extra
101  // padding at the start if required.
102  size_t ExtraAlign =
103  llvm::OffsetToAlignment(sizeof(Module *), alignof(Decl));
104  char *Buffer = reinterpret_cast<char *>(
105  ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
106  Buffer += ExtraAlign;
107  auto *ParentModule =
108  Parent ? cast<Decl>(Parent)->getOwningModule() : nullptr;
109  return new (Buffer) Module*(ParentModule) + 1;
110  }
111  return ::operator new(Size + Extra, Ctx);
112 }
113 
114 Module *Decl::getOwningModuleSlow() const {
115  assert(isFromASTFile() && "Not from AST file?");
117 }
118 
121 }
122 
123 const char *Decl::getDeclKindName() const {
124  switch (DeclKind) {
125  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
126 #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
127 #define ABSTRACT_DECL(DECL)
128 #include "clang/AST/DeclNodes.inc"
129  }
130 }
131 
132 void Decl::setInvalidDecl(bool Invalid) {
133  InvalidDecl = Invalid;
134  assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
135  if (!Invalid) {
136  return;
137  }
138 
139  if (!isa<ParmVarDecl>(this)) {
140  // Defensive maneuver for ill-formed code: we're likely not to make it to
141  // a point where we set the access specifier, so default it to "public"
142  // to avoid triggering asserts elsewhere in the front end.
144  }
145 
146  // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
147  // are invalid too.
148  if (DecompositionDecl *DD = dyn_cast<DecompositionDecl>(this)) {
149  for (BindingDecl *Binding : DD->bindings()) {
150  Binding->setInvalidDecl();
151  }
152  }
153 }
154 
155 const char *DeclContext::getDeclKindName() const {
156  switch (DeclKind) {
157  default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
158 #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
159 #define ABSTRACT_DECL(DECL)
160 #include "clang/AST/DeclNodes.inc"
161  }
162 }
163 
164 bool Decl::StatisticsEnabled = false;
166  StatisticsEnabled = true;
167 }
168 
170  llvm::errs() << "\n*** Decl Stats:\n";
171 
172  int totalDecls = 0;
173 #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
174 #define ABSTRACT_DECL(DECL)
175 #include "clang/AST/DeclNodes.inc"
176  llvm::errs() << " " << totalDecls << " decls total.\n";
177 
178  int totalBytes = 0;
179 #define DECL(DERIVED, BASE) \
180  if (n##DERIVED##s > 0) { \
181  totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
182  llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
183  << sizeof(DERIVED##Decl) << " each (" \
184  << n##DERIVED##s * sizeof(DERIVED##Decl) \
185  << " bytes)\n"; \
186  }
187 #define ABSTRACT_DECL(DECL)
188 #include "clang/AST/DeclNodes.inc"
189 
190  llvm::errs() << "Total bytes = " << totalBytes << "\n";
191 }
192 
193 void Decl::add(Kind k) {
194  switch (k) {
195 #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
196 #define ABSTRACT_DECL(DECL)
197 #include "clang/AST/DeclNodes.inc"
198  }
199 }
200 
202  if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
203  return TTP->isParameterPack();
204  if (const NonTypeTemplateParmDecl *NTTP
205  = dyn_cast<NonTypeTemplateParmDecl>(this))
206  return NTTP->isParameterPack();
207  if (const TemplateTemplateParmDecl *TTP
208  = dyn_cast<TemplateTemplateParmDecl>(this))
209  return TTP->isParameterPack();
210  return false;
211 }
212 
213 bool Decl::isParameterPack() const {
214  if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
215  return Parm->isParameterPack();
216 
217  return isTemplateParameterPack();
218 }
219 
221  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
222  return FD;
223  if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
224  return FTD->getTemplatedDecl();
225  return nullptr;
226 }
227 
228 bool Decl::isTemplateDecl() const {
229  return isa<TemplateDecl>(this);
230 }
231 
233  if (auto *FD = dyn_cast<FunctionDecl>(this))
234  return FD->getDescribedFunctionTemplate();
235  else if (auto *RD = dyn_cast<CXXRecordDecl>(this))
236  return RD->getDescribedClassTemplate();
237  else if (auto *VD = dyn_cast<VarDecl>(this))
238  return VD->getDescribedVarTemplate();
239  else if (auto *AD = dyn_cast<TypeAliasDecl>(this))
240  return AD->getDescribedAliasTemplate();
241 
242  return nullptr;
243 }
244 
245 bool Decl::isTemplated() const {
246  // A declaration is dependent if it is a template or a template pattern, or
247  // is within (lexcially for a friend, semantically otherwise) a dependent
248  // context.
249  // FIXME: Should local extern declarations be treated like friends?
250  if (auto *AsDC = dyn_cast<DeclContext>(this))
251  return AsDC->isDependentContext();
253  return DC->isDependentContext() || isTemplateDecl() || getDescribedTemplate();
254 }
255 
257  for (const DeclContext *DC = getDeclContext();
258  DC && !DC->isTranslationUnit() && !DC->isNamespace();
259  DC = DC->getParent())
260  if (DC->isFunctionOrMethod())
261  return DC;
262 
263  return nullptr;
264 }
265 
266 //===----------------------------------------------------------------------===//
267 // PrettyStackTraceDecl Implementation
268 //===----------------------------------------------------------------------===//
269 
270 void PrettyStackTraceDecl::print(raw_ostream &OS) const {
271  SourceLocation TheLoc = Loc;
272  if (TheLoc.isInvalid() && TheDecl)
273  TheLoc = TheDecl->getLocation();
274 
275  if (TheLoc.isValid()) {
276  TheLoc.print(OS, SM);
277  OS << ": ";
278  }
279 
280  OS << Message;
281 
282  if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
283  OS << " '";
284  DN->printQualifiedName(OS);
285  OS << '\'';
286  }
287  OS << '\n';
288 }
289 
290 //===----------------------------------------------------------------------===//
291 // Decl Implementation
292 //===----------------------------------------------------------------------===//
293 
294 // Out-of-line virtual method providing a home for Decl.
295 Decl::~Decl() = default;
296 
298  DeclCtx = DC;
299 }
300 
302  if (DC == getLexicalDeclContext())
303  return;
304 
305  if (isInSemaDC()) {
306  setDeclContextsImpl(getDeclContext(), DC, getASTContext());
307  } else {
308  getMultipleDC()->LexicalDC = DC;
309  }
310 
311  // FIXME: We shouldn't be changing the lexical context of declarations
312  // imported from AST files.
313  if (!isFromASTFile()) {
314  setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
315  if (hasOwningModule())
316  setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
317  }
318 
319  assert(
321  getOwningModule()) &&
322  "hidden declaration has no owning module");
323 }
324 
325 void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
326  ASTContext &Ctx) {
327  if (SemaDC == LexicalDC) {
328  DeclCtx = SemaDC;
329  } else {
330  Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
331  MDC->SemanticDC = SemaDC;
332  MDC->LexicalDC = LexicalDC;
333  DeclCtx = MDC;
334  }
335 }
336 
338  const DeclContext *LDC = getLexicalDeclContext();
339  while (true) {
340  if (LDC->isFunctionOrMethod())
341  return true;
342  if (!isa<TagDecl>(LDC))
343  return false;
344  LDC = LDC->getLexicalParent();
345  }
346  return false;
347 }
348 
350  for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) {
351  if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
352  if (ND->isAnonymousNamespace())
353  return true;
354  }
355 
356  return false;
357 }
358 
360  return getDeclContext()->isStdNamespace();
361 }
362 
364  if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
365  return TUD;
366 
367  DeclContext *DC = getDeclContext();
368  assert(DC && "This decl is not contained in a translation unit!");
369 
370  while (!DC->isTranslationUnit()) {
371  DC = DC->getParent();
372  assert(DC && "This decl is not contained in a translation unit!");
373  }
374 
375  return cast<TranslationUnitDecl>(DC);
376 }
377 
380 }
381 
384 }
385 
386 unsigned Decl::getMaxAlignment() const {
387  if (!hasAttrs())
388  return 0;
389 
390  unsigned Align = 0;
391  const AttrVec &V = getAttrs();
392  ASTContext &Ctx = getASTContext();
393  specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
394  for (; I != E; ++I)
395  Align = std::max(Align, I->getAlignment(Ctx));
396  return Align;
397 }
398 
399 bool Decl::isUsed(bool CheckUsedAttr) const {
400  const Decl *CanonD = getCanonicalDecl();
401  if (CanonD->Used)
402  return true;
403 
404  // Check for used attribute.
405  // Ask the most recent decl, since attributes accumulate in the redecl chain.
406  if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
407  return true;
408 
409  // The information may have not been deserialized yet. Force deserialization
410  // to complete the needed information.
411  return getMostRecentDecl()->getCanonicalDecl()->Used;
412 }
413 
415  if (isUsed(false))
416  return;
417 
418  if (C.getASTMutationListener())
420 
421  setIsUsed();
422 }
423 
424 bool Decl::isReferenced() const {
425  if (Referenced)
426  return true;
427 
428  // Check redeclarations.
429  for (auto I : redecls())
430  if (I->Referenced)
431  return true;
432 
433  return false;
434 }
435 
436 bool Decl::isExported() const {
437  if (isModulePrivate())
438  return false;
439  // Namespaces are always exported.
440  if (isa<TranslationUnitDecl>(this) || isa<NamespaceDecl>(this))
441  return true;
442  // Otherwise, this is a strictly lexical check.
443  for (auto *DC = getLexicalDeclContext(); DC; DC = DC->getLexicalParent()) {
444  if (cast<Decl>(DC)->isModulePrivate())
445  return false;
446  if (isa<ExportDecl>(DC))
447  return true;
448  }
449  return false;
450 }
451 
452 ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
453  const Decl *Definition = nullptr;
454  if (auto ID = dyn_cast<ObjCInterfaceDecl>(this)) {
455  Definition = ID->getDefinition();
456  } else if (auto PD = dyn_cast<ObjCProtocolDecl>(this)) {
457  Definition = PD->getDefinition();
458  } else if (auto TD = dyn_cast<TagDecl>(this)) {
459  Definition = TD->getDefinition();
460  }
461  if (!Definition)
462  Definition = this;
463 
464  if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
465  return attr;
466  if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
467  return dcd->getAttr<ExternalSourceSymbolAttr>();
468  }
469 
470  return nullptr;
471 }
472 
473 bool Decl::hasDefiningAttr() const {
474  return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>();
475 }
476 
477 const Attr *Decl::getDefiningAttr() const {
478  if (AliasAttr *AA = getAttr<AliasAttr>())
479  return AA;
480  if (IFuncAttr *IFA = getAttr<IFuncAttr>())
481  return IFA;
482  return nullptr;
483 }
484 
485 static StringRef getRealizedPlatform(const AvailabilityAttr *A,
486  const ASTContext &Context) {
487  // Check if this is an App Extension "platform", and if so chop off
488  // the suffix for matching with the actual platform.
489  StringRef RealizedPlatform = A->getPlatform()->getName();
490  if (!Context.getLangOpts().AppExt)
491  return RealizedPlatform;
492  size_t suffix = RealizedPlatform.rfind("_app_extension");
493  if (suffix != StringRef::npos)
494  return RealizedPlatform.slice(0, suffix);
495  return RealizedPlatform;
496 }
497 
498 /// \brief Determine the availability of the given declaration based on
499 /// the target platform.
500 ///
501 /// When it returns an availability result other than \c AR_Available,
502 /// if the \p Message parameter is non-NULL, it will be set to a
503 /// string describing why the entity is unavailable.
504 ///
505 /// FIXME: Make these strings localizable, since they end up in
506 /// diagnostics.
508  const AvailabilityAttr *A,
509  std::string *Message,
510  VersionTuple EnclosingVersion) {
511  if (EnclosingVersion.empty())
512  EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
513 
514  if (EnclosingVersion.empty())
515  return AR_Available;
516 
517  StringRef ActualPlatform = A->getPlatform()->getName();
518  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
519 
520  // Match the platform name.
521  if (getRealizedPlatform(A, Context) != TargetPlatform)
522  return AR_Available;
523 
524  StringRef PrettyPlatformName
525  = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
526 
527  if (PrettyPlatformName.empty())
528  PrettyPlatformName = ActualPlatform;
529 
530  std::string HintMessage;
531  if (!A->getMessage().empty()) {
532  HintMessage = " - ";
533  HintMessage += A->getMessage();
534  }
535 
536  // Make sure that this declaration has not been marked 'unavailable'.
537  if (A->getUnavailable()) {
538  if (Message) {
539  Message->clear();
540  llvm::raw_string_ostream Out(*Message);
541  Out << "not available on " << PrettyPlatformName
542  << HintMessage;
543  }
544 
545  return AR_Unavailable;
546  }
547 
548  // Make sure that this declaration has already been introduced.
549  if (!A->getIntroduced().empty() &&
550  EnclosingVersion < A->getIntroduced()) {
551  if (Message) {
552  Message->clear();
553  llvm::raw_string_ostream Out(*Message);
554  VersionTuple VTI(A->getIntroduced());
555  VTI.UseDotAsSeparator();
556  Out << "introduced in " << PrettyPlatformName << ' '
557  << VTI << HintMessage;
558  }
559 
560  return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
561  }
562 
563  // Make sure that this declaration hasn't been obsoleted.
564  if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) {
565  if (Message) {
566  Message->clear();
567  llvm::raw_string_ostream Out(*Message);
568  VersionTuple VTO(A->getObsoleted());
569  VTO.UseDotAsSeparator();
570  Out << "obsoleted in " << PrettyPlatformName << ' '
571  << VTO << HintMessage;
572  }
573 
574  return AR_Unavailable;
575  }
576 
577  // Make sure that this declaration hasn't been deprecated.
578  if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) {
579  if (Message) {
580  Message->clear();
581  llvm::raw_string_ostream Out(*Message);
582  VersionTuple VTD(A->getDeprecated());
583  VTD.UseDotAsSeparator();
584  Out << "first deprecated in " << PrettyPlatformName << ' '
585  << VTD << HintMessage;
586  }
587 
588  return AR_Deprecated;
589  }
590 
591  return AR_Available;
592 }
593 
595  VersionTuple EnclosingVersion) const {
596  if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
597  return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion);
598 
600  std::string ResultMessage;
601 
602  for (const auto *A : attrs()) {
603  if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
604  if (Result >= AR_Deprecated)
605  continue;
606 
607  if (Message)
608  ResultMessage = Deprecated->getMessage();
609 
610  Result = AR_Deprecated;
611  continue;
612  }
613 
614  if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
615  if (Message)
616  *Message = Unavailable->getMessage();
617  return AR_Unavailable;
618  }
619 
620  if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
622  Message, EnclosingVersion);
623 
624  if (AR == AR_Unavailable)
625  return AR_Unavailable;
626 
627  if (AR > Result) {
628  Result = AR;
629  if (Message)
630  ResultMessage.swap(*Message);
631  }
632  continue;
633  }
634  }
635 
636  if (Message)
637  Message->swap(ResultMessage);
638  return Result;
639 }
640 
642  const ASTContext &Context = getASTContext();
643  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
644  for (const auto *A : attrs()) {
645  if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
646  if (getRealizedPlatform(Availability, Context) != TargetPlatform)
647  continue;
648  if (!Availability->getIntroduced().empty())
649  return Availability->getIntroduced();
650  }
651  }
652  return VersionTuple();
653 }
654 
655 bool Decl::canBeWeakImported(bool &IsDefinition) const {
656  IsDefinition = false;
657 
658  // Variables, if they aren't definitions.
659  if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
660  if (Var->isThisDeclarationADefinition()) {
661  IsDefinition = true;
662  return false;
663  }
664  return true;
665 
666  // Functions, if they aren't definitions.
667  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
668  if (FD->hasBody()) {
669  IsDefinition = true;
670  return false;
671  }
672  return true;
673 
674  // Objective-C classes, if this is the non-fragile runtime.
675  } else if (isa<ObjCInterfaceDecl>(this) &&
677  return true;
678 
679  // Nothing else.
680  } else {
681  return false;
682  }
683 }
684 
685 bool Decl::isWeakImported() const {
686  bool IsDefinition;
687  if (!canBeWeakImported(IsDefinition))
688  return false;
689 
690  for (const auto *A : attrs()) {
691  if (isa<WeakImportAttr>(A))
692  return true;
693 
694  if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
695  if (CheckAvailability(getASTContext(), Availability, nullptr,
697  return true;
698  }
699  }
700 
701  return false;
702 }
703 
705  switch (DeclKind) {
706  case Function:
707  case CXXDeductionGuide:
708  case CXXMethod:
709  case CXXConstructor:
710  case ConstructorUsingShadow:
711  case CXXDestructor:
712  case CXXConversion:
713  case EnumConstant:
714  case Var:
715  case ImplicitParam:
716  case ParmVar:
717  case ObjCMethod:
718  case ObjCProperty:
719  case MSProperty:
720  return IDNS_Ordinary;
721  case Label:
722  return IDNS_Label;
723  case IndirectField:
724  return IDNS_Ordinary | IDNS_Member;
725 
726  case Binding:
727  case NonTypeTemplateParm:
728  case VarTemplate:
729  // These (C++-only) declarations are found by redeclaration lookup for
730  // tag types, so we include them in the tag namespace.
731  return IDNS_Ordinary | IDNS_Tag;
732 
733  case ObjCCompatibleAlias:
734  case ObjCInterface:
735  return IDNS_Ordinary | IDNS_Type;
736 
737  case Typedef:
738  case TypeAlias:
739  case TemplateTypeParm:
740  case ObjCTypeParam:
741  return IDNS_Ordinary | IDNS_Type;
742 
743  case UnresolvedUsingTypename:
745 
746  case UsingShadow:
747  return 0; // we'll actually overwrite this later
748 
749  case UnresolvedUsingValue:
750  return IDNS_Ordinary | IDNS_Using;
751 
752  case Using:
753  case UsingPack:
754  return IDNS_Using;
755 
756  case ObjCProtocol:
757  return IDNS_ObjCProtocol;
758 
759  case Field:
760  case ObjCAtDefsField:
761  case ObjCIvar:
762  return IDNS_Member;
763 
764  case Record:
765  case CXXRecord:
766  case Enum:
767  return IDNS_Tag | IDNS_Type;
768 
769  case Namespace:
770  case NamespaceAlias:
771  return IDNS_Namespace;
772 
773  case FunctionTemplate:
774  return IDNS_Ordinary;
775 
776  case ClassTemplate:
777  case TemplateTemplateParm:
778  case TypeAliasTemplate:
779  return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
780 
781  case OMPDeclareReduction:
782  return IDNS_OMPReduction;
783 
784  // Never have names.
785  case Friend:
786  case FriendTemplate:
787  case AccessSpec:
788  case LinkageSpec:
789  case Export:
790  case FileScopeAsm:
791  case StaticAssert:
792  case ObjCPropertyImpl:
793  case PragmaComment:
794  case PragmaDetectMismatch:
795  case Block:
796  case Captured:
797  case TranslationUnit:
798  case ExternCContext:
799  case Decomposition:
800 
801  case UsingDirective:
802  case BuiltinTemplate:
803  case ClassTemplateSpecialization:
804  case ClassTemplatePartialSpecialization:
805  case ClassScopeFunctionSpecialization:
806  case VarTemplateSpecialization:
807  case VarTemplatePartialSpecialization:
808  case ObjCImplementation:
809  case ObjCCategory:
810  case ObjCCategoryImpl:
811  case Import:
812  case OMPThreadPrivate:
813  case OMPCapturedExpr:
814  case Empty:
815  // Never looked up by name.
816  return 0;
817  }
818 
819  llvm_unreachable("Invalid DeclKind!");
820 }
821 
822 void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
823  assert(!HasAttrs && "Decl already contains attrs.");
824 
825  AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
826  assert(AttrBlank.empty() && "HasAttrs was wrong?");
827 
828  AttrBlank = attrs;
829  HasAttrs = true;
830 }
831 
833  if (!HasAttrs) return;
834 
835  HasAttrs = false;
837 }
838 
839 const AttrVec &Decl::getAttrs() const {
840  assert(HasAttrs && "No attrs to get!");
841  return getASTContext().getDeclAttrs(this);
842 }
843 
845  Decl::Kind DK = D->getDeclKind();
846  switch(DK) {
847 #define DECL(NAME, BASE)
848 #define DECL_CONTEXT(NAME) \
849  case Decl::NAME: \
850  return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
851 #define DECL_CONTEXT_BASE(NAME)
852 #include "clang/AST/DeclNodes.inc"
853  default:
854 #define DECL(NAME, BASE)
855 #define DECL_CONTEXT_BASE(NAME) \
856  if (DK >= first##NAME && DK <= last##NAME) \
857  return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
858 #include "clang/AST/DeclNodes.inc"
859  llvm_unreachable("a decl that inherits DeclContext isn't handled");
860  }
861 }
862 
864  Decl::Kind DK = D->getKind();
865  switch(DK) {
866 #define DECL(NAME, BASE)
867 #define DECL_CONTEXT(NAME) \
868  case Decl::NAME: \
869  return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
870 #define DECL_CONTEXT_BASE(NAME)
871 #include "clang/AST/DeclNodes.inc"
872  default:
873 #define DECL(NAME, BASE)
874 #define DECL_CONTEXT_BASE(NAME) \
875  if (DK >= first##NAME && DK <= last##NAME) \
876  return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
877 #include "clang/AST/DeclNodes.inc"
878  llvm_unreachable("a decl that inherits DeclContext isn't handled");
879  }
880 }
881 
883  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
884  // FunctionDecl stores EndRangeLoc for this purpose.
885  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
886  const FunctionDecl *Definition;
887  if (FD->hasBody(Definition))
888  return Definition->getSourceRange().getEnd();
889  return SourceLocation();
890  }
891 
892  if (Stmt *Body = getBody())
893  return Body->getSourceRange().getEnd();
894 
895  return SourceLocation();
896 }
897 
898 bool Decl::AccessDeclContextSanity() const {
899 #ifndef NDEBUG
900  // Suppress this check if any of the following hold:
901  // 1. this is the translation unit (and thus has no parent)
902  // 2. this is a template parameter (and thus doesn't belong to its context)
903  // 3. this is a non-type template parameter
904  // 4. the context is not a record
905  // 5. it's invalid
906  // 6. it's a C++0x static_assert.
907  // 7. it's a block literal declaration
908  if (isa<TranslationUnitDecl>(this) ||
909  isa<TemplateTypeParmDecl>(this) ||
910  isa<NonTypeTemplateParmDecl>(this) ||
911  !isa<CXXRecordDecl>(getDeclContext()) ||
912  isInvalidDecl() ||
913  isa<StaticAssertDecl>(this) ||
914  isa<BlockDecl>(this) ||
915  // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
916  // as DeclContext (?).
917  isa<ParmVarDecl>(this) ||
918  // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
919  // AS_none as access specifier.
920  isa<CXXRecordDecl>(this) ||
921  isa<ClassScopeFunctionSpecializationDecl>(this))
922  return true;
923 
924  assert(Access != AS_none &&
925  "Access specifier is AS_none inside a record decl");
926 #endif
927  return true;
928 }
929 
930 static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
931 static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
932 
933 const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
934  QualType Ty;
935  if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
936  Ty = D->getType();
937  else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
938  Ty = D->getUnderlyingType();
939  else
940  return nullptr;
941 
942  if (Ty->isFunctionPointerType())
943  Ty = Ty->getAs<PointerType>()->getPointeeType();
944  else if (BlocksToo && Ty->isBlockPointerType())
945  Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
946 
947  return Ty->getAs<FunctionType>();
948 }
949 
950 /// Starting at a given context (a Decl or DeclContext), look for a
951 /// code context that is not a closure (a lambda, block, etc.).
952 template <class T> static Decl *getNonClosureContext(T *D) {
953  if (getKind(D) == Decl::CXXMethod) {
954  CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
955  if (MD->getOverloadedOperator() == OO_Call &&
956  MD->getParent()->isLambda())
957  return getNonClosureContext(MD->getParent()->getParent());
958  return MD;
959  } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
960  return FD;
961  } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
962  return MD;
963  } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
964  return getNonClosureContext(BD->getParent());
965  } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
966  return getNonClosureContext(CD->getParent());
967  } else {
968  return nullptr;
969  }
970 }
971 
974 }
975 
978 }
979 
980 //===----------------------------------------------------------------------===//
981 // DeclContext Implementation
982 //===----------------------------------------------------------------------===//
983 
984 bool DeclContext::classof(const Decl *D) {
985  switch (D->getKind()) {
986 #define DECL(NAME, BASE)
987 #define DECL_CONTEXT(NAME) case Decl::NAME:
988 #define DECL_CONTEXT_BASE(NAME)
989 #include "clang/AST/DeclNodes.inc"
990  return true;
991  default:
992 #define DECL(NAME, BASE)
993 #define DECL_CONTEXT_BASE(NAME) \
994  if (D->getKind() >= Decl::first##NAME && \
995  D->getKind() <= Decl::last##NAME) \
996  return true;
997 #include "clang/AST/DeclNodes.inc"
998  return false;
999  }
1000 }
1001 
1002 DeclContext::~DeclContext() = default;
1003 
1004 /// \brief Find the parent context of this context that will be
1005 /// used for unqualified name lookup.
1006 ///
1007 /// Generally, the parent lookup context is the semantic context. However, for
1008 /// a friend function the parent lookup context is the lexical context, which
1009 /// is the class in which the friend is declared.
1011  // FIXME: Find a better way to identify friends
1012  if (isa<FunctionDecl>(this))
1013  if (getParent()->getRedeclContext()->isFileContext() &&
1014  getLexicalParent()->getRedeclContext()->isRecord())
1015  return getLexicalParent();
1016 
1017  return getParent();
1018 }
1019 
1021  return isNamespace() &&
1022  cast<NamespaceDecl>(this)->isInline();
1023 }
1024 
1026  if (!isNamespace())
1027  return false;
1028 
1029  const NamespaceDecl *ND = cast<NamespaceDecl>(this);
1030  if (ND->isInline()) {
1031  return ND->getParent()->isStdNamespace();
1032  }
1033 
1034  if (!getParent()->getRedeclContext()->isTranslationUnit())
1035  return false;
1036 
1037  const IdentifierInfo *II = ND->getIdentifier();
1038  return II && II->isStr("std");
1039 }
1040 
1042  if (isFileContext())
1043  return false;
1044 
1045  if (isa<ClassTemplatePartialSpecializationDecl>(this))
1046  return true;
1047 
1048  if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
1049  if (Record->getDescribedClassTemplate())
1050  return true;
1051 
1052  if (Record->isDependentLambda())
1053  return true;
1054  }
1055 
1056  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
1057  if (Function->getDescribedFunctionTemplate())
1058  return true;
1059 
1060  // Friend function declarations are dependent if their *lexical*
1061  // context is dependent.
1062  if (cast<Decl>(this)->getFriendObjectKind())
1063  return getLexicalParent()->isDependentContext();
1064  }
1065 
1066  // FIXME: A variable template is a dependent context, but is not a
1067  // DeclContext. A context within it (such as a lambda-expression)
1068  // should be considered dependent.
1069 
1070  return getParent() && getParent()->isDependentContext();
1071 }
1072 
1074  if (DeclKind == Decl::Enum)
1075  return !cast<EnumDecl>(this)->isScoped();
1076  else if (DeclKind == Decl::LinkageSpec || DeclKind == Decl::Export)
1077  return true;
1078 
1079  return false;
1080 }
1081 
1082 static bool isLinkageSpecContext(const DeclContext *DC,
1084  while (DC->getDeclKind() != Decl::TranslationUnit) {
1085  if (DC->getDeclKind() == Decl::LinkageSpec)
1086  return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1087  DC = DC->getLexicalParent();
1088  }
1089  return false;
1090 }
1091 
1094 }
1095 
1097  const DeclContext *DC = this;
1098  while (DC->getDeclKind() != Decl::TranslationUnit) {
1099  if (DC->getDeclKind() == Decl::LinkageSpec &&
1100  cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c)
1101  return cast<LinkageSpecDecl>(DC);
1102  DC = DC->getLexicalParent();
1103  }
1104  return nullptr;
1105 }
1106 
1109 }
1110 
1111 bool DeclContext::Encloses(const DeclContext *DC) const {
1112  if (getPrimaryContext() != this)
1113  return getPrimaryContext()->Encloses(DC);
1114 
1115  for (; DC; DC = DC->getParent())
1116  if (DC->getPrimaryContext() == this)
1117  return true;
1118  return false;
1119 }
1120 
1122  switch (DeclKind) {
1123  case Decl::TranslationUnit:
1124  case Decl::ExternCContext:
1125  case Decl::LinkageSpec:
1126  case Decl::Export:
1127  case Decl::Block:
1128  case Decl::Captured:
1129  case Decl::OMPDeclareReduction:
1130  // There is only one DeclContext for these entities.
1131  return this;
1132 
1133  case Decl::Namespace:
1134  // The original namespace is our primary context.
1135  return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
1136 
1137  case Decl::ObjCMethod:
1138  return this;
1139 
1140  case Decl::ObjCInterface:
1141  if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
1142  return Def;
1143  return this;
1144 
1145  case Decl::ObjCProtocol:
1146  if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
1147  return Def;
1148  return this;
1149 
1150  case Decl::ObjCCategory:
1151  return this;
1152 
1153  case Decl::ObjCImplementation:
1154  case Decl::ObjCCategoryImpl:
1155  return this;
1156 
1157  default:
1158  if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
1159  // If this is a tag type that has a definition or is currently
1160  // being defined, that definition is our primary context.
1161  TagDecl *Tag = cast<TagDecl>(this);
1162 
1163  if (TagDecl *Def = Tag->getDefinition())
1164  return Def;
1165 
1166  if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1167  // Note, TagType::getDecl returns the (partial) definition one exists.
1168  TagDecl *PossiblePartialDef = TagTy->getDecl();
1169  if (PossiblePartialDef->isBeingDefined())
1170  return PossiblePartialDef;
1171  } else {
1172  assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
1173  }
1174 
1175  return Tag;
1176  }
1177 
1178  assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
1179  "Unknown DeclContext kind");
1180  return this;
1181  }
1182 }
1183 
1184 void
1186  Contexts.clear();
1187 
1188  if (DeclKind != Decl::Namespace) {
1189  Contexts.push_back(this);
1190  return;
1191  }
1192 
1193  NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
1194  for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1195  N = N->getPreviousDecl())
1196  Contexts.push_back(N);
1197 
1198  std::reverse(Contexts.begin(), Contexts.end());
1199 }
1200 
1201 std::pair<Decl *, Decl *>
1203  bool FieldsAlreadyLoaded) {
1204  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
1205  Decl *FirstNewDecl = nullptr;
1206  Decl *PrevDecl = nullptr;
1207  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1208  if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1209  continue;
1210 
1211  Decl *D = Decls[I];
1212  if (PrevDecl)
1213  PrevDecl->NextInContextAndBits.setPointer(D);
1214  else
1215  FirstNewDecl = D;
1216 
1217  PrevDecl = D;
1218  }
1219 
1220  return std::make_pair(FirstNewDecl, PrevDecl);
1221 }
1222 
1223 /// \brief We have just acquired external visible storage, and we already have
1224 /// built a lookup map. For every name in the map, pull in the new names from
1225 /// the external storage.
1226 void DeclContext::reconcileExternalVisibleStorage() const {
1227  assert(NeedToReconcileExternalVisibleStorage && LookupPtr);
1228  NeedToReconcileExternalVisibleStorage = false;
1229 
1230  for (auto &Lookup : *LookupPtr)
1231  Lookup.second.setHasExternalDecls();
1232 }
1233 
1234 /// \brief Load the declarations within this lexical storage from an
1235 /// external source.
1236 /// \return \c true if any declarations were added.
1237 bool
1238 DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1239  ExternalASTSource *Source = getParentASTContext().getExternalSource();
1240  assert(hasExternalLexicalStorage() && Source && "No external storage?");
1241 
1242  // Notify that we have a DeclContext that is initializing.
1243  ExternalASTSource::Deserializing ADeclContext(Source);
1244 
1245  // Load the external declarations, if any.
1246  SmallVector<Decl*, 64> Decls;
1247  ExternalLexicalStorage = false;
1248  Source->FindExternalLexicalDecls(this, Decls);
1249 
1250  if (Decls.empty())
1251  return false;
1252 
1253  // We may have already loaded just the fields of this record, in which case
1254  // we need to ignore them.
1255  bool FieldsAlreadyLoaded = false;
1256  if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1257  FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1258 
1259  // Splice the newly-read declarations into the beginning of the list
1260  // of declarations.
1261  Decl *ExternalFirst, *ExternalLast;
1262  std::tie(ExternalFirst, ExternalLast) =
1263  BuildDeclChain(Decls, FieldsAlreadyLoaded);
1264  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1265  FirstDecl = ExternalFirst;
1266  if (!LastDecl)
1267  LastDecl = ExternalLast;
1268  return true;
1269 }
1270 
1273  DeclarationName Name) {
1274  ASTContext &Context = DC->getParentASTContext();
1275  StoredDeclsMap *Map;
1276  if (!(Map = DC->LookupPtr))
1277  Map = DC->CreateStoredDeclsMap(Context);
1278  if (DC->NeedToReconcileExternalVisibleStorage)
1279  DC->reconcileExternalVisibleStorage();
1280 
1281  (*Map)[Name].removeExternalDecls();
1282 
1283  return DeclContext::lookup_result();
1284 }
1285 
1288  DeclarationName Name,
1289  ArrayRef<NamedDecl*> Decls) {
1290  ASTContext &Context = DC->getParentASTContext();
1291  StoredDeclsMap *Map;
1292  if (!(Map = DC->LookupPtr))
1293  Map = DC->CreateStoredDeclsMap(Context);
1294  if (DC->NeedToReconcileExternalVisibleStorage)
1295  DC->reconcileExternalVisibleStorage();
1296 
1297  StoredDeclsList &List = (*Map)[Name];
1298 
1299  // Clear out any old external visible declarations, to avoid quadratic
1300  // performance in the redeclaration checks below.
1301  List.removeExternalDecls();
1302 
1303  if (!List.isNull()) {
1304  // We have both existing declarations and new declarations for this name.
1305  // Some of the declarations may simply replace existing ones. Handle those
1306  // first.
1308  for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1309  if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
1310  Skip.push_back(I);
1311  Skip.push_back(Decls.size());
1312 
1313  // Add in any new declarations.
1314  unsigned SkipPos = 0;
1315  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1316  if (I == Skip[SkipPos])
1317  ++SkipPos;
1318  else
1319  List.AddSubsequentDecl(Decls[I]);
1320  }
1321  } else {
1322  // Convert the array to a StoredDeclsList.
1324  I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1325  if (List.isNull())
1326  List.setOnlyValue(*I);
1327  else
1328  List.AddSubsequentDecl(*I);
1329  }
1330  }
1331 
1332  return List.getLookupResult();
1333 }
1334 
1336  if (hasExternalLexicalStorage())
1337  LoadLexicalDeclsFromExternalStorage();
1338  return decl_iterator(FirstDecl);
1339 }
1340 
1342  if (hasExternalLexicalStorage())
1343  LoadLexicalDeclsFromExternalStorage();
1344 
1345  return !FirstDecl;
1346 }
1347 
1349  return (D->getLexicalDeclContext() == this &&
1350  (D->NextInContextAndBits.getPointer() || D == LastDecl));
1351 }
1352 
1354  assert(D->getLexicalDeclContext() == this &&
1355  "decl being removed from non-lexical context");
1356  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
1357  "decl is not in decls list");
1358 
1359  // Remove D from the decl chain. This is O(n) but hopefully rare.
1360  if (D == FirstDecl) {
1361  if (D == LastDecl)
1362  FirstDecl = LastDecl = nullptr;
1363  else
1364  FirstDecl = D->NextInContextAndBits.getPointer();
1365  } else {
1366  for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
1367  assert(I && "decl not found in linked list");
1368  if (I->NextInContextAndBits.getPointer() == D) {
1369  I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1370  if (D == LastDecl) LastDecl = I;
1371  break;
1372  }
1373  }
1374  }
1375 
1376  // Mark that D is no longer in the decl chain.
1377  D->NextInContextAndBits.setPointer(nullptr);
1378 
1379  // Remove D from the lookup table if necessary.
1380  if (isa<NamedDecl>(D)) {
1381  NamedDecl *ND = cast<NamedDecl>(D);
1382 
1383  // Remove only decls that have a name
1384  if (!ND->getDeclName()) return;
1385 
1386  auto *DC = D->getDeclContext();
1387  do {
1388  StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1389  if (Map) {
1390  StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1391  assert(Pos != Map->end() && "no lookup entry for decl");
1392  if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1393  Pos->second.remove(ND);
1394  }
1395  } while (DC->isTransparentContext() && (DC = DC->getParent()));
1396  }
1397 }
1398 
1400  assert(D->getLexicalDeclContext() == this &&
1401  "Decl inserted into wrong lexical context");
1402  assert(!D->getNextDeclInContext() && D != LastDecl &&
1403  "Decl already inserted into a DeclContext");
1404 
1405  if (FirstDecl) {
1406  LastDecl->NextInContextAndBits.setPointer(D);
1407  LastDecl = D;
1408  } else {
1409  FirstDecl = LastDecl = D;
1410  }
1411 
1412  // Notify a C++ record declaration that we've added a member, so it can
1413  // update its class-specific state.
1414  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1415  Record->addedMember(D);
1416 
1417  // If this is a newly-created (not de-serialized) import declaration, wire
1418  // it in to the list of local import declarations.
1419  if (!D->isFromASTFile()) {
1420  if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1421  D->getASTContext().addedLocalImportDecl(Import);
1422  }
1423 }
1424 
1426  addHiddenDecl(D);
1427 
1428  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1429  ND->getDeclContext()->getPrimaryContext()->
1430  makeDeclVisibleInContextWithFlags(ND, false, true);
1431 }
1432 
1434  addHiddenDecl(D);
1435 
1436  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1437  ND->getDeclContext()->getPrimaryContext()->
1438  makeDeclVisibleInContextWithFlags(ND, true, true);
1439 }
1440 
1441 /// shouldBeHidden - Determine whether a declaration which was declared
1442 /// within its semantic context should be invisible to qualified name lookup.
1443 static bool shouldBeHidden(NamedDecl *D) {
1444  // Skip unnamed declarations.
1445  if (!D->getDeclName())
1446  return true;
1447 
1448  // Skip entities that can't be found by name lookup into a particular
1449  // context.
1450  if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1451  D->isTemplateParameter())
1452  return true;
1453 
1454  // Skip template specializations.
1455  // FIXME: This feels like a hack. Should DeclarationName support
1456  // template-ids, or is there a better way to keep specializations
1457  // from being visible?
1458  if (isa<ClassTemplateSpecializationDecl>(D))
1459  return true;
1460  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1461  if (FD->isFunctionTemplateSpecialization())
1462  return true;
1463 
1464  return false;
1465 }
1466 
1467 /// buildLookup - Build the lookup data structure with all of the
1468 /// declarations in this DeclContext (and any other contexts linked
1469 /// to it or transparent contexts nested within it) and return it.
1470 ///
1471 /// Note that the produced map may miss out declarations from an
1472 /// external source. If it does, those entries will be marked with
1473 /// the 'hasExternalDecls' flag.
1475  assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1476 
1477  if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1478  return LookupPtr;
1479 
1481  collectAllContexts(Contexts);
1482 
1483  if (HasLazyExternalLexicalLookups) {
1484  HasLazyExternalLexicalLookups = false;
1485  for (auto *DC : Contexts) {
1486  if (DC->hasExternalLexicalStorage())
1487  HasLazyLocalLexicalLookups |=
1488  DC->LoadLexicalDeclsFromExternalStorage();
1489  }
1490 
1491  if (!HasLazyLocalLexicalLookups)
1492  return LookupPtr;
1493  }
1494 
1495  for (auto *DC : Contexts)
1496  buildLookupImpl(DC, hasExternalVisibleStorage());
1497 
1498  // We no longer have any lazy decls.
1499  HasLazyLocalLexicalLookups = false;
1500  return LookupPtr;
1501 }
1502 
1503 /// buildLookupImpl - Build part of the lookup data structure for the
1504 /// declarations contained within DCtx, which will either be this
1505 /// DeclContext, a DeclContext linked to it, or a transparent context
1506 /// nested within it.
1507 void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
1508  for (Decl *D : DCtx->noload_decls()) {
1509  // Insert this declaration into the lookup structure, but only if
1510  // it's semantically within its decl context. Any other decls which
1511  // should be found in this context are added eagerly.
1512  //
1513  // If it's from an AST file, don't add it now. It'll get handled by
1514  // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1515  // in C++, we do not track external visible decls for the TU, so in
1516  // that case we need to collect them all here.
1517  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1518  if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1519  (!ND->isFromASTFile() ||
1520  (isTranslationUnit() &&
1521  !getParentASTContext().getLangOpts().CPlusPlus)))
1522  makeDeclVisibleInContextImpl(ND, Internal);
1523 
1524  // If this declaration is itself a transparent declaration context
1525  // or inline namespace, add the members of this declaration of that
1526  // context (recursively).
1527  if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1528  if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1529  buildLookupImpl(InnerCtx, Internal);
1530  }
1531 }
1532 
1533 NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1534 
1537  assert(DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export &&
1538  "should not perform lookups into transparent contexts");
1539 
1540  const DeclContext *PrimaryContext = getPrimaryContext();
1541  if (PrimaryContext != this)
1542  return PrimaryContext->lookup(Name);
1543 
1544  // If we have an external source, ensure that any later redeclarations of this
1545  // context have been loaded, since they may add names to the result of this
1546  // lookup (or add external visible storage).
1547  ExternalASTSource *Source = getParentASTContext().getExternalSource();
1548  if (Source)
1549  (void)cast<Decl>(this)->getMostRecentDecl();
1550 
1551  if (hasExternalVisibleStorage()) {
1552  assert(Source && "external visible storage but no external source?");
1553 
1554  if (NeedToReconcileExternalVisibleStorage)
1555  reconcileExternalVisibleStorage();
1556 
1557  StoredDeclsMap *Map = LookupPtr;
1558 
1559  if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
1560  // FIXME: Make buildLookup const?
1561  Map = const_cast<DeclContext*>(this)->buildLookup();
1562 
1563  if (!Map)
1564  Map = CreateStoredDeclsMap(getParentASTContext());
1565 
1566  // If we have a lookup result with no external decls, we are done.
1567  std::pair<StoredDeclsMap::iterator, bool> R =
1568  Map->insert(std::make_pair(Name, StoredDeclsList()));
1569  if (!R.second && !R.first->second.hasExternalDecls())
1570  return R.first->second.getLookupResult();
1571 
1572  if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
1573  if (StoredDeclsMap *Map = LookupPtr) {
1574  StoredDeclsMap::iterator I = Map->find(Name);
1575  if (I != Map->end())
1576  return I->second.getLookupResult();
1577  }
1578  }
1579 
1580  return lookup_result();
1581  }
1582 
1583  StoredDeclsMap *Map = LookupPtr;
1584  if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
1585  Map = const_cast<DeclContext*>(this)->buildLookup();
1586 
1587  if (!Map)
1588  return lookup_result();
1589 
1590  StoredDeclsMap::iterator I = Map->find(Name);
1591  if (I == Map->end())
1592  return lookup_result();
1593 
1594  return I->second.getLookupResult();
1595 }
1596 
1599  assert(DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export &&
1600  "should not perform lookups into transparent contexts");
1601 
1602  DeclContext *PrimaryContext = getPrimaryContext();
1603  if (PrimaryContext != this)
1604  return PrimaryContext->noload_lookup(Name);
1605 
1606  loadLazyLocalLexicalLookups();
1607  StoredDeclsMap *Map = LookupPtr;
1608  if (!Map)
1609  return lookup_result();
1610 
1611  StoredDeclsMap::iterator I = Map->find(Name);
1612  return I != Map->end() ? I->second.getLookupResult()
1613  : lookup_result();
1614 }
1615 
1616 // If we have any lazy lexical declarations not in our lookup map, add them
1617 // now. Don't import any external declarations, not even if we know we have
1618 // some missing from the external visible lookups.
1619 void DeclContext::loadLazyLocalLexicalLookups() {
1620  if (HasLazyLocalLexicalLookups) {
1622  collectAllContexts(Contexts);
1623  for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1624  buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1625  HasLazyLocalLexicalLookups = false;
1626  }
1627 }
1628 
1630  SmallVectorImpl<NamedDecl *> &Results) {
1631  Results.clear();
1632 
1633  // If there's no external storage, just perform a normal lookup and copy
1634  // the results.
1635  if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1636  lookup_result LookupResults = lookup(Name);
1637  Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1638  return;
1639  }
1640 
1641  // If we have a lookup table, check there first. Maybe we'll get lucky.
1642  // FIXME: Should we be checking these flags on the primary context?
1643  if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1644  if (StoredDeclsMap *Map = LookupPtr) {
1645  StoredDeclsMap::iterator Pos = Map->find(Name);
1646  if (Pos != Map->end()) {
1647  Results.insert(Results.end(),
1648  Pos->second.getLookupResult().begin(),
1649  Pos->second.getLookupResult().end());
1650  return;
1651  }
1652  }
1653  }
1654 
1655  // Slow case: grovel through the declarations in our chain looking for
1656  // matches.
1657  // FIXME: If we have lazy external declarations, this will not find them!
1658  // FIXME: Should we CollectAllContexts and walk them all here?
1659  for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1660  if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1661  if (ND->getDeclName() == Name)
1662  Results.push_back(ND);
1663  }
1664 }
1665 
1667  DeclContext *Ctx = this;
1668  // Skip through transparent contexts.
1669  while (Ctx->isTransparentContext())
1670  Ctx = Ctx->getParent();
1671  return Ctx;
1672 }
1673 
1675  DeclContext *Ctx = this;
1676  // Skip through non-namespace, non-translation-unit contexts.
1677  while (!Ctx->isFileContext())
1678  Ctx = Ctx->getParent();
1679  return Ctx->getPrimaryContext();
1680 }
1681 
1683  // Loop until we find a non-record context.
1684  RecordDecl *OutermostRD = nullptr;
1685  DeclContext *DC = this;
1686  while (DC->isRecord()) {
1687  OutermostRD = cast<RecordDecl>(DC);
1688  DC = DC->getLexicalParent();
1689  }
1690  return OutermostRD;
1691 }
1692 
1694  // For non-file contexts, this is equivalent to Equals.
1695  if (!isFileContext())
1696  return O->Equals(this);
1697 
1698  do {
1699  if (O->Equals(this))
1700  return true;
1701 
1702  const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1703  if (!NS || !NS->isInline())
1704  break;
1705  O = NS->getParent();
1706  } while (O);
1707 
1708  return false;
1709 }
1710 
1712  DeclContext *PrimaryDC = this->getPrimaryContext();
1713  DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1714  // If the decl is being added outside of its semantic decl context, we
1715  // need to ensure that we eagerly build the lookup information for it.
1716  PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1717 }
1718 
1719 void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1720  bool Recoverable) {
1721  assert(this == getPrimaryContext() && "expected a primary DC");
1722 
1723  if (!isLookupContext()) {
1724  if (isTransparentContext())
1725  getParent()->getPrimaryContext()
1726  ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1727  return;
1728  }
1729 
1730  // Skip declarations which should be invisible to name lookup.
1731  if (shouldBeHidden(D))
1732  return;
1733 
1734  // If we already have a lookup data structure, perform the insertion into
1735  // it. If we might have externally-stored decls with this name, look them
1736  // up and perform the insertion. If this decl was declared outside its
1737  // semantic context, buildLookup won't add it, so add it now.
1738  //
1739  // FIXME: As a performance hack, don't add such decls into the translation
1740  // unit unless we're in C++, since qualified lookup into the TU is never
1741  // performed.
1742  if (LookupPtr || hasExternalVisibleStorage() ||
1743  ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1744  (getParentASTContext().getLangOpts().CPlusPlus ||
1745  !isTranslationUnit()))) {
1746  // If we have lazily omitted any decls, they might have the same name as
1747  // the decl which we are adding, so build a full lookup table before adding
1748  // this decl.
1749  buildLookup();
1750  makeDeclVisibleInContextImpl(D, Internal);
1751  } else {
1752  HasLazyLocalLexicalLookups = true;
1753  }
1754 
1755  // If we are a transparent context or inline namespace, insert into our
1756  // parent context, too. This operation is recursive.
1757  if (isTransparentContext() || isInlineNamespace())
1758  getParent()->getPrimaryContext()->
1759  makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1760 
1761  Decl *DCAsDecl = cast<Decl>(this);
1762  // Notify that a decl was made visible unless we are a Tag being defined.
1763  if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1764  if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1765  L->AddedVisibleDecl(this, D);
1766 }
1767 
1768 void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1769  // Find or create the stored declaration map.
1770  StoredDeclsMap *Map = LookupPtr;
1771  if (!Map) {
1772  ASTContext *C = &getParentASTContext();
1773  Map = CreateStoredDeclsMap(*C);
1774  }
1775 
1776  // If there is an external AST source, load any declarations it knows about
1777  // with this declaration's name.
1778  // If the lookup table contains an entry about this name it means that we
1779  // have already checked the external source.
1780  if (!Internal)
1781  if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1782  if (hasExternalVisibleStorage() &&
1783  Map->find(D->getDeclName()) == Map->end())
1784  Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1785 
1786  // Insert this declaration into the map.
1787  StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1788 
1789  if (Internal) {
1790  // If this is being added as part of loading an external declaration,
1791  // this may not be the only external declaration with this name.
1792  // In this case, we never try to replace an existing declaration; we'll
1793  // handle that when we finalize the list of declarations for this name.
1794  DeclNameEntries.setHasExternalDecls();
1795  DeclNameEntries.AddSubsequentDecl(D);
1796  return;
1797  }
1798 
1799  if (DeclNameEntries.isNull()) {
1800  DeclNameEntries.setOnlyValue(D);
1801  return;
1802  }
1803 
1804  if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
1805  // This declaration has replaced an existing one for which
1806  // declarationReplaces returns true.
1807  return;
1808  }
1809 
1810  // Put this declaration into the appropriate slot.
1811  DeclNameEntries.AddSubsequentDecl(D);
1812 }
1813 
1815  return cast<UsingDirectiveDecl>(*I);
1816 }
1817 
1818 /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1819 /// this context.
1821  // FIXME: Use something more efficient than normal lookup for using
1822  // directives. In C++, using directives are looked up more than anything else.
1823  lookup_result Result = lookup(UsingDirectiveDecl::getName());
1824  return udir_range(Result.begin(), Result.end());
1825 }
1826 
1827 //===----------------------------------------------------------------------===//
1828 // Creation and Destruction of StoredDeclsMaps. //
1829 //===----------------------------------------------------------------------===//
1830 
1831 StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1832  assert(!LookupPtr && "context already has a decls map");
1833  assert(getPrimaryContext() == this &&
1834  "creating decls map on non-primary context");
1835 
1836  StoredDeclsMap *M;
1837  bool Dependent = isDependentContext();
1838  if (Dependent)
1839  M = new DependentStoredDeclsMap();
1840  else
1841  M = new StoredDeclsMap();
1842  M->Previous = C.LastSDM;
1843  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1844  LookupPtr = M;
1845  return M;
1846 }
1847 
1848 void ASTContext::ReleaseDeclContextMaps() {
1849  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1850  // pointer because the subclass doesn't add anything that needs to
1851  // be deleted.
1852  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1853 }
1854 
1855 void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1856  while (Map) {
1857  // Advance the iteration before we invalidate memory.
1858  llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1859 
1860  if (Dependent)
1861  delete static_cast<DependentStoredDeclsMap*>(Map);
1862  else
1863  delete Map;
1864 
1865  Map = Next.getPointer();
1866  Dependent = Next.getInt();
1867  }
1868 }
1869 
1872  const PartialDiagnostic &PDiag) {
1873  assert(Parent->isDependentContext()
1874  && "cannot iterate dependent diagnostics of non-dependent context");
1875  Parent = Parent->getPrimaryContext();
1876  if (!Parent->LookupPtr)
1877  Parent->CreateStoredDeclsMap(C);
1878 
1880  static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
1881 
1882  // Allocate the copy of the PartialDiagnostic via the ASTContext's
1883  // BumpPtrAllocator, rather than the ASTContext itself.
1884  PartialDiagnostic::Storage *DiagStorage = nullptr;
1885  if (PDiag.hasStorage())
1886  DiagStorage = new (C) PartialDiagnostic::Storage;
1887 
1888  DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
1889 
1890  // TODO: Maybe we shouldn't reverse the order during insertion.
1891  DD->NextDiagnostic = Map->FirstDiagnostic;
1892  Map->FirstDiagnostic = DD;
1893 
1894  return DD;
1895 }
static StringRef getRealizedPlatform(const AvailabilityAttr *A, const ASTContext &Context)
Definition: DeclBase.cpp:485
Defines the clang::ASTContext interface.
Represents a function declaration or definition.
Definition: Decl.h:1696
AttrVec & getDeclAttrs(const Decl *D)
Retrieve the attributes for the given declaration.
void localUncachedLookup(DeclarationName Name, SmallVectorImpl< NamedDecl *> &Results)
A simplistic name lookup mechanism that performs name lookup into this declaration context without co...
Definition: DeclBase.cpp:1629
VersionTuple getPlatformMinVersion() const
Retrieve the minimum desired version of the platform, to which the program should be compiled...
Definition: TargetInfo.h:1012
static DeclContext * castToDeclContext(const Decl *)
Definition: DeclBase.cpp:863
Module * getOwningModule() const
Get the module that owns this declaration (for visibility purposes).
Definition: DeclBase.h:751
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2289
QualType getPointeeType() const
Definition: Type.h:2302
static bool shouldBeHidden(NamedDecl *D)
shouldBeHidden - Determine whether a declaration which was declared within its semantic context shoul...
Definition: DeclBase.cpp:1443
A (possibly-)qualified type.
Definition: Type.h:653
bool isBlockPointerType() const
Definition: Type.h:5956
TagDecl * getDefinition() const
Returns the TagDecl that actually defines this struct/union/class/enum.
Definition: Decl.cpp:3762
const char * getDeclKindName() const
Definition: DeclBase.cpp:123
llvm::PointerIntPair< Decl *, 2, ModuleOwnershipKind > NextInContextAndBits
The next declaration within the same lexical DeclContext.
Definition: DeclBase.h:239
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
void AddSubsequentDecl(NamedDecl *D)
AddSubsequentDecl - This is called on the second and later decl when it is not a redeclaration to mer...
virtual void FindExternalLexicalDecls(const DeclContext *DC, llvm::function_ref< bool(Decl::Kind)> IsKindWeWant, SmallVectorImpl< Decl *> &Result)
Finds all declarations lexically contained within the given DeclContext, after applying an optional f...
RAII class for safely pairing a StartedDeserializing call with FinishedDeserializing.
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c.h:60
static Decl * castFromDeclContext(const DeclContext *)
Definition: DeclBase.cpp:844
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:978
static bool isLinkageSpecContext(const DeclContext *DC, LinkageSpecDecl::LanguageIDs ID)
Definition: DeclBase.cpp:1082
Stmt - This represents one statement.
Definition: Stmt.h:66
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3062
C Language Family Type Representation.
const char * getDeclKindName() const
Definition: DeclBase.cpp:155
const Type * getTypeForDecl() const
Definition: Decl.h:2802
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
specific_attr_iterator - Iterates over a subrange of an AttrVec, only providing attributes that are o...
Definition: AttrIterator.h:54
static void add(Kind k)
Definition: DeclBase.cpp:193
Defines the C++ template declaration subclasses.
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration, or NULL if there is no previous declaration.
Definition: DeclBase.h:952
Defines types useful for describing an Objective-C runtime.
Represent a C++ namespace.
Definition: Decl.h:514
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:671
unsigned getIdentifierNamespace() const
Definition: DeclBase.h:791
static Decl * getNonClosureContext(T *D)
Starting at a given context (a Decl or DeclContext), look for a code context that is not a closure (a...
Definition: DeclBase.cpp:952
const DeclContext * getParentFunctionOrMethod() const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext...
Definition: DeclBase.cpp:256
unsigned Access
Access - Used by C++ decls for the access specifier.
Definition: DeclBase.h:319
bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer)
HandleRedeclaration - If this is a redeclaration of an existing decl, replace the old one with D and ...
bool hasOwningModule() const
Is this declaration owned by some module?
Definition: DeclBase.h:746
Represents a variable declaration or definition.
Definition: Decl.h:812
ASTMutationListener * getASTMutationListener() const
Definition: DeclBase.cpp:382
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6317
bool hasLocalOwningModuleStorage() const
Definition: DeclBase.cpp:119
bool isExternCContext() const
Determines whether this context or some of its ancestors is a linkage specification context that spec...
Definition: DeclBase.cpp:1092
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
bool isInvalidDecl() const
Definition: DeclBase.h:546
ExternalSourceSymbolAttr * getExternalSourceSymbolAttr() const
Looks on this and related declarations for an applicable external source symbol attribute.
Definition: DeclBase.cpp:452
bool isParameterPack() const
Whether this declaration is a parameter pack.
Definition: DeclBase.cpp:213
Represents a parameter to a function.
Definition: Decl.h:1515
void removeDecl(Decl *D)
Removes a declaration from this context.
Definition: DeclBase.cpp:1353
bool hasWeakClassImport() const
Does this runtime support weakly importing classes?
Definition: ObjCRuntime.h:271
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:269
Types, declared with &#39;struct foo&#39;, typedefs, etc.
Definition: DeclBase.h:130
bool isLexicallyWithinFunctionOrMethod() const
Returns true if this declaration lexically is inside a function.
Definition: DeclBase.cpp:337
Represents a struct/union/class.
Definition: Decl.h:3510
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:297
bool Encloses(const DeclContext *DC) const
Determine whether this declaration context encloses the declaration context DC.
Definition: DeclBase.cpp:1111
One of these records is kept for each identifier that is lexed.
bool isInAnonymousNamespace() const
Definition: DeclBase.cpp:349
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
static DeclContextLookupResult SetExternalVisibleDeclsForName(const DeclContext *DC, DeclarationName Name, ArrayRef< NamedDecl *> Decls)
Definition: DeclBase.cpp:1287
QualType getPointeeType() const
Definition: Type.h:2406
void print(raw_ostream &OS, const SourceManager &SM) const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
bool isInline() const
Returns true if this is an inline namespace declaration.
Definition: Decl.h:575
The results of name lookup within a DeclContext.
Definition: DeclBase.h:1182
bool Equals(const DeclContext *DC) const
Determine whether this declaration context is equivalent to the declaration context DC...
Definition: DeclBase.h:1455
Describes a module or submodule.
Definition: Module.h:65
virtual void updateOutOfDateIdentifier(IdentifierInfo &II)
Update an out-of-date identifier.
DeclContext * getEnclosingNamespaceContext()
Retrieve the nearest enclosing namespace context.
Definition: DeclBase.cpp:1674
virtual bool FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name)
Find all declarations with the given name in the given context, and add them to the context by callin...
Namespaces, declared with &#39;namespace foo {}&#39;.
Definition: DeclBase.h:140
bool isReferenced() const
Whether any declaration of this entity was referenced.
Definition: DeclBase.cpp:424
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
ModuleOwnershipKind getModuleOwnershipKind() const
Get the kind of module ownership for this declaration.
Definition: DeclBase.h:778
StoredDeclsMap * buildLookup()
Ensure the lookup structure is fully-built and return it.
Definition: DeclBase.cpp:1474
static DeclContextLookupResult SetNoExternalVisibleDeclsForName(const DeclContext *DC, DeclarationName Name)
Definition: DeclBase.cpp:1272
ASTContext & getASTContext() const
Definition: Decl.h:119
bool isLambda() const
Determine whether this class describes a lambda function object.
Definition: DeclCXX.h:1163
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition: DeclObjC.h:2229
void eraseDeclAttrs(const Decl *D)
Erase the attributes corresponding to the given declaration.
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2083
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:820
Labels, declared with &#39;x:&#39; and referenced with &#39;goto x&#39;.
Definition: DeclBase.h:117
void setLocalOwningModule(Module *M)
Definition: DeclBase.h:738
Represents the body of a CapturedStmt, and serves as its DeclContext.
Definition: Decl.h:3883
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
Represents a linkage specification.
Definition: DeclCXX.h:2798
A binding in a decomposition declaration.
Definition: DeclCXX.h:3777
bool isExported() const
Whether this declaration is exported (by virtue of being lexically within an ExportDecl or by being a...
Definition: DeclBase.cpp:436
Ordinary names.
Definition: DeclBase.h:144
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:869
void addDeclInternal(Decl *D)
Add the declaration D into this context, but suppress searches for external declarations with the sam...
Definition: DeclBase.cpp:1433
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1536
AvailabilityResult
Captures the result of checking the availability of a declaration.
Definition: DeclBase.h:72
NodeId Parent
Definition: ASTDiff.cpp:192
Decl * getNextDeclInContext()
Definition: DeclBase.h:422
bool canBeWeakImported(bool &IsDefinition) const
Determines whether this symbol can be weak-imported, e.g., whether it would be well-formed to add the...
Definition: DeclBase.cpp:655
udir_range using_directives() const
Returns iterator range [First, Last) of UsingDirectiveDecls stored within this context.
Definition: DeclBase.cpp:1820
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any...
Definition: Decl.cpp:3180
Objective C @protocol.
Definition: DeclBase.h:147
llvm::iterator_range< udir_iterator > udir_range
Definition: DeclBase.h:1857
decl_range noload_decls() const
noload_decls_begin/end - Iterate over the declarations stored in this context that are currently load...
Definition: DeclBase.h:1588
const FunctionType * getFunctionType(bool BlocksToo=true) const
Looks through the Decl&#39;s underlying type to extract a FunctionType when possible. ...
Definition: DeclBase.cpp:933
void removeExternalDecls()
Remove any declarations which were imported from an external AST source.
bool isInlineNamespace() const
Definition: DeclBase.cpp:1020
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition: DeclBase.h:1351
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3713
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:636
const Attr * getDefiningAttr() const
Return this declaration&#39;s defining attribute if it has one.
Definition: DeclBase.cpp:477
Defines the clang::LangOptions interface.
std::string Label
static unsigned getIdentifierNamespaceForKind(Kind DK)
Definition: DeclBase.cpp:704
const FunctionProtoType * T
bool containsDecl(Decl *D) const
Checks whether a declaration is in this context.
Definition: DeclBase.cpp:1348
Declaration of a template type parameter.
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition: DeclBase.cpp:132
This declaration is an OpenMP user defined reduction construction.
Definition: DeclBase.h:178
bool InEnclosingNamespaceSetOf(const DeclContext *NS) const
Test if this context is part of the enclosing namespace set of the context NS, as defined in C++0x [n...
Definition: DeclBase.cpp:1693
Decl * getMostRecentDecl()
Retrieve the most recent declaration that declares the same entity as this declaration (which may be ...
Definition: DeclBase.h:967
bool isFileContext() const
Definition: DeclBase.h:1401
DeclContext * getDeclContext()
Definition: DeclBase.h:425
lookup_result noload_lookup(DeclarationName Name)
Find the declarations with the given name that are visible within this context; don&#39;t attempt to retr...
Definition: DeclBase.cpp:1598
Decl * getNonClosureAncestor()
Find the nearest non-closure ancestor of this context, i.e.
Definition: DeclBase.cpp:976
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
NonTypeTemplateParmDecl - Declares a non-type template parameter, e.g., "Size" in.
bool isTemplateParameter() const
isTemplateParameter - Determines whether this declaration is a template parameter.
Definition: DeclBase.h:1959
bool isTemplateParameterPack() const
isTemplateParameter - Determines whether this declaration is a template parameter pack...
Definition: DeclBase.cpp:201
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
bool isFunctionOrMethod() const
Definition: DeclBase.h:1384
clang::ObjCRuntime ObjCRuntime
Definition: LangOptions.h:134
This declaration has an owning module, and is visible when that module is imported.
unsigned getMaxAlignment() const
getMaxAlignment - return the maximum alignment specified by attributes on this decl, 0 if there are none.
Definition: DeclBase.cpp:386
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1335
void collectAllContexts(SmallVectorImpl< DeclContext *> &Contexts)
Collects all of the declaration contexts that are semantically connected to this declaration context...
Definition: DeclBase.cpp:1185
SourceLocation getEnd() const
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:1041
The result type of a method or function.
const SourceManager & SM
Definition: Format.cpp:1412
void setDeclContext(DeclContext *DC)
setDeclContext - Set both the semantic and lexical DeclContext to DC.
Definition: DeclBase.cpp:297
FriendObjectKind getFriendObjectKind() const
Determines whether this declaration is the object of a friend declaration and, if so...
Definition: DeclBase.h:1102
virtual void DeclarationMarkedUsed(const Decl *D)
A declaration is marked used which was not previously marked used.
bool decls_empty() const
Definition: DeclBase.cpp:1341
AttrVec & getAttrs()
Definition: DeclBase.h:477
bool hasAttrs() const
Definition: DeclBase.h:471
Abstract interface for external sources of AST nodes.
bool isTemplateDecl() const
returns true if this declaration is a template
Definition: DeclBase.cpp:228
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero)...
Definition: VersionTuple.h:69
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1328
void makeDeclVisibleInContext(NamedDecl *D)
Makes a declaration visible within this context.
Definition: DeclBase.cpp:1711
DeclContext::lookup_result getLookupResult()
getLookupResult - Return an array of all the decls that this list represents.
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:220
StringRef getPlatformName() const
Retrieve the name of the platform as it is used in the availability attribute.
Definition: TargetInfo.h:1008
VersionTuple getVersionIntroduced() const
Retrieve the version of the target platform in which this declaration was introduced.
Definition: DeclBase.cpp:641
Encodes a location in the source.
Members, declared with object declarations within tag definitions.
Definition: DeclBase.h:136
void setModuleOwnershipKind(ModuleOwnershipKind MOK)
Set whether this declaration is hidden from name lookup.
Definition: DeclBase.h:783
decl_iterator decls_begin() const
Definition: DeclBase.cpp:1335
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2968
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:378
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2019
bool isStdNamespace() const
Definition: DeclBase.cpp:1025
This file defines OpenMP nodes for declarative directives.
void print(raw_ostream &OS) const override
Definition: DeclBase.cpp:270
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:694
UsingDirectiveDecl * operator*() const
Definition: DeclBase.cpp:1814
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required...
Definition: DeclBase.cpp:399
An array of decls optimized for the common case of only containing one entry.
void dropAttrs()
Definition: DeclBase.cpp:832
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
Definition: DeclBase.cpp:685
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:3988
void * Allocate(size_t Size, unsigned Align=8) const
Definition: ASTContext.h:650
decl_iterator - Iterates through the declarations stored within this context.
Definition: DeclBase.h:1537
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2820
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition: DeclBase.h:940
Dataflow Directional Tag Classes.
void updateOutOfDate(IdentifierInfo &II) const
Update a potentially out-of-date declaration.
Definition: DeclBase.cpp:63
bool isValid() const
Return true if this is a valid SourceLocation object.
virtual Module * getModule(unsigned ID)
Retrieve the module that corresponds to the given module ID.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1256
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:399
bool isRecord() const
Definition: DeclBase.h:1409
attr_range attrs() const
Definition: DeclBase.h:494
ObjCInterfaceDecl * getDefinition()
Retrieve the definition of this class, or NULL if this class has been forward-declared (with @class) ...
Definition: DeclObjC.h:1568
A decomposition declaration.
Definition: DeclCXX.h:3825
RecordDecl * getOuterLexicalRecordContext()
Retrieve the outermost lexically enclosing record context.
Definition: DeclBase.cpp:1682
DeclarationName - The name of a declaration.
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2140
Kind getKind() const
Definition: DeclBase.h:419
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks, lambdas, etc.
Definition: DeclBase.cpp:972
Tags, declared with &#39;struct foo;&#39; and referenced with &#39;struct foo&#39;.
Definition: DeclBase.h:125
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any...
Definition: ASTContext.h:1048
A dependently-generated diagnostic.
Pointer to a block type.
Definition: Type.h:2391
static DependentDiagnostic * Create(ASTContext &Context, DeclContext *Parent, AccessNonce _, SourceLocation Loc, bool IsMemberAccess, AccessSpecifier AS, NamedDecl *TargetDecl, CXXRecordDecl *NamingClass, QualType BaseObjectType, const PartialDiagnostic &PDiag)
void setIsUsed()
Set whether the declaration is used, in the sense of odr-use.
Definition: DeclBase.h:566
LanguageIDs
Represents the language in a linkage specification.
Definition: DeclCXX.h:2808
AvailabilityResult getAvailability(std::string *Message=nullptr, VersionTuple EnclosingVersion=VersionTuple()) const
Determine the availability of the given declaration.
Definition: DeclBase.cpp:594
T * getAttr() const
Definition: DeclBase.h:531
unsigned getOwningModuleID() const
Retrieve the global ID of the module that owns this particular declaration.
Definition: DeclBase.h:706
virtual ~Decl()
void setOnlyValue(NamedDecl *ND)
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1666
const LinkageSpecDecl * getExternCContext() const
Retrieve the nearest enclosing C linkage specification context.
Definition: DeclBase.cpp:1096
void addDecl(Decl *D)
Add the declaration D into this context.
Definition: DeclBase.cpp:1425
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream...
void markUsed(ASTContext &C)
Mark the declaration used, in the sense of odr-use.
Definition: DeclBase.cpp:414
ASTMutationListener * getASTMutationListener() const
Retrieve a pointer to the AST mutation listener associated with this AST context, if any...
Definition: ASTContext.h:1063
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Defines the clang::SourceLocation class and associated facilities.
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
DeclContextLookupResult lookup_result
Definition: DeclBase.h:1780
void addHiddenDecl(Decl *D)
Add the declaration D to this context without modifying any lookup tables.
Definition: DeclBase.cpp:1399
static std::pair< Decl *, Decl * > BuildDeclChain(ArrayRef< Decl *> Decls, bool FieldsAlreadyLoaded)
Build up a chain of declarations.
Definition: DeclBase.cpp:1202
DeclContext * getLookupParent()
Find the parent context of this context that will be used for unqualified name lookup.
Definition: DeclBase.cpp:1010
static bool classof(const Decl *D)
Definition: DeclBase.cpp:984
Defines the clang::TargetInfo interface.
ASTContext & getParentASTContext() const
Definition: DeclBase.h:1364
Defines the clang::VersionTuple class, which represents a version in the form major[.minor[.subminor]].
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: Decl.cpp:3517
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:89
__DEVICE__ int max(int __a, int __b)
The top declaration context.
Definition: Decl.h:107
static void DestroyAll(StoredDeclsMap *Map, bool Dependent)
Definition: DeclBase.cpp:1855
static void PrintStats()
Definition: DeclBase.cpp:169
NamedDecl * getMostRecentDecl()
Definition: Decl.h:445
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
Definition: DeclBase.cpp:1121
bool trackLocalOwningModule() const
Do we need to track the owning module for a local declaration?
Definition: LangOptions.h:189
static void EnableStatistics()
Definition: DeclBase.cpp:165
bool isModulePrivate() const
Whether this declaration was marked as being private to the module in which it was defined...
Definition: DeclBase.h:600
void setLexicalDeclContext(DeclContext *DC)
Definition: DeclBase.cpp:301
This represents a decl that may have a name.
Definition: Decl.h:248
bool isTranslationUnit() const
Definition: DeclBase.h:1405
void setAccess(AccessSpecifier AS)
Definition: DeclBase.h:455
TemplateDecl * getDescribedTemplate() const
If this is a declaration that describes some template, this method returns that template declaration...
Definition: DeclBase.cpp:232
Represents C++ using-directive.
Definition: DeclCXX.h:2898
bool hasDefiningAttr() const
Return true if this declaration has an attribute which acts as definition of the entity, such as &#39;alias&#39; or &#39;ifunc&#39;.
Definition: DeclBase.cpp:473
bool isFunctionPointerType() const
Definition: Type.h:5972
void addedLocalImportDecl(ImportDecl *Import)
Notify the AST context that a new import declaration has been parsed or implicitly created within thi...
bool isInStdNamespace() const
Definition: DeclBase.cpp:359
TranslationUnitDecl * getTranslationUnitDecl()
Definition: DeclBase.cpp:363
const LangOptions & getLangOpts() const
Definition: ASTContext.h:688
bool isExternCXXContext() const
Determines whether this context or some of its ancestors is a linkage specification context that spec...
Definition: DeclBase.cpp:1107
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
Definition: DeclBase.cpp:245
SourceLocation getBodyRBrace() const
getBodyRBrace - Gets the right brace of the body, if a body exists.
Definition: DeclBase.cpp:882
Declaration of a template function.
Definition: DeclTemplate.h:967
Attr - This represents one attribute.
Definition: Attr.h:43
static AvailabilityResult CheckAvailability(ASTContext &Context, const AvailabilityAttr *A, std::string *Message, VersionTuple EnclosingVersion)
Determine the availability of the given declaration based on the target platform. ...
Definition: DeclBase.cpp:507
bool isBeingDefined() const
Return true if this decl is currently being defined.
Definition: Decl.h:3130
This declaration is a using declaration.
Definition: DeclBase.h:163
bool isTransparentContext() const
isTransparentContext - Determines whether this context is a "transparent" context, meaning that the members declared in this context are semantically declared in the nearest enclosing non-transparent (opaque) context but are lexically declared in this context.
Definition: DeclBase.cpp:1073