| 1 | //===- DeclBase.cpp - Declaration AST Node Implementation -----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Decl and DeclContext classes. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/DeclBase.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ASTLambda.h" |
| 16 | #include "clang/AST/ASTMutationListener.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" |
| 21 | #include "clang/AST/DeclContextInternals.h" |
| 22 | #include "clang/AST/DeclFriend.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
| 24 | #include "clang/AST/DeclOpenACC.h" |
| 25 | #include "clang/AST/DeclOpenMP.h" |
| 26 | #include "clang/AST/DeclTemplate.h" |
| 27 | #include "clang/AST/DependentDiagnostic.h" |
| 28 | #include "clang/AST/ExternalASTSource.h" |
| 29 | #include "clang/AST/Stmt.h" |
| 30 | #include "clang/AST/Type.h" |
| 31 | #include "clang/Basic/IdentifierTable.h" |
| 32 | #include "clang/Basic/LLVM.h" |
| 33 | #include "clang/Basic/Module.h" |
| 34 | #include "clang/Basic/ObjCRuntime.h" |
| 35 | #include "clang/Basic/PartialDiagnostic.h" |
| 36 | #include "clang/Basic/SourceLocation.h" |
| 37 | #include "clang/Basic/TargetInfo.h" |
| 38 | #include "llvm/ADT/PointerIntPair.h" |
| 39 | #include "llvm/ADT/StringRef.h" |
| 40 | #include "llvm/Support/ErrorHandling.h" |
| 41 | #include "llvm/Support/MathExtras.h" |
| 42 | #include "llvm/Support/VersionTuple.h" |
| 43 | #include "llvm/Support/raw_ostream.h" |
| 44 | #include <algorithm> |
| 45 | #include <cassert> |
| 46 | #include <cstddef> |
| 47 | #include <string> |
| 48 | #include <tuple> |
| 49 | #include <utility> |
| 50 | |
| 51 | using namespace clang; |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // Statistics |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
| 57 | #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0; |
| 58 | #define ABSTRACT_DECL(DECL) |
| 59 | #include "clang/AST/DeclNodes.inc" |
| 60 | |
| 61 | #define DECL(DERIVED, BASE) \ |
| 62 | static_assert(alignof(Decl) >= alignof(DERIVED##Decl), \ |
| 63 | "Alignment sufficient after objects prepended to " #DERIVED); |
| 64 | #define ABSTRACT_DECL(DECL) |
| 65 | #include "clang/AST/DeclNodes.inc" |
| 66 | |
| 67 | void *Decl::operator new(std::size_t Size, const ASTContext &Context, |
| 68 | GlobalDeclID ID, std::size_t ) { |
| 69 | // Allocate an extra 8 bytes worth of storage, which ensures that the |
| 70 | // resulting pointer will still be 8-byte aligned. |
| 71 | static_assert(sizeof(uint64_t) >= alignof(Decl), "Decl won't be misaligned" ); |
| 72 | void *Start = Context.Allocate(Size: Size + Extra + 8); |
| 73 | void *Result = (char*)Start + 8; |
| 74 | |
| 75 | uint64_t *PrefixPtr = (uint64_t *)Result - 1; |
| 76 | |
| 77 | *PrefixPtr = ID.getRawValue(); |
| 78 | |
| 79 | // We leave the upper 16 bits to store the module IDs. 48 bits should be |
| 80 | // sufficient to store a declaration ID. See the comments in setOwningModuleID |
| 81 | // for details. |
| 82 | assert((*PrefixPtr < llvm::maskTrailingOnes<uint64_t>(48)) && |
| 83 | "Current Implementation limits the number of module files to not " |
| 84 | "exceed 2^16. Contact Clang Developers to remove the limitation." ); |
| 85 | |
| 86 | return Result; |
| 87 | } |
| 88 | |
| 89 | void *Decl::operator new(std::size_t Size, const ASTContext &Ctx, |
| 90 | DeclContext *Parent, std::size_t ) { |
| 91 | assert(!Parent || &Parent->getParentASTContext() == &Ctx); |
| 92 | // With local visibility enabled, we track the owning module even for local |
| 93 | // declarations. We create the TU decl early and may not yet know what the |
| 94 | // LangOpts are, so conservatively allocate the storage. |
| 95 | if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) { |
| 96 | // Ensure required alignment of the resulting object by adding extra |
| 97 | // padding at the start if required. |
| 98 | size_t = |
| 99 | llvm::offsetToAlignment(Value: sizeof(Module *), Alignment: llvm::Align(alignof(Decl))); |
| 100 | auto *Buffer = reinterpret_cast<char *>( |
| 101 | ::operator new(Bytes: ExtraAlign + sizeof(Module *) + Size + Extra, C: Ctx)); |
| 102 | Buffer += ExtraAlign; |
| 103 | auto *ParentModule = |
| 104 | Parent ? cast<Decl>(Val: Parent)->getOwningModule() : nullptr; |
| 105 | return new (Buffer) Module*(ParentModule) + 1; |
| 106 | } |
| 107 | return ::operator new(Bytes: Size + Extra, C: Ctx); |
| 108 | } |
| 109 | |
| 110 | GlobalDeclID Decl::getGlobalID() const { |
| 111 | if (!isFromASTFile()) |
| 112 | return GlobalDeclID(); |
| 113 | // See the comments in `Decl::operator new` for details. |
| 114 | uint64_t ID = *((const uint64_t *)this - 1); |
| 115 | return GlobalDeclID(ID & llvm::maskTrailingOnes<uint64_t>(N: 48)); |
| 116 | } |
| 117 | |
| 118 | unsigned Decl::getOwningModuleID() const { |
| 119 | if (!isFromASTFile()) |
| 120 | return 0; |
| 121 | |
| 122 | uint64_t ID = *((const uint64_t *)this - 1); |
| 123 | return ID >> 48; |
| 124 | } |
| 125 | |
| 126 | void Decl::setOwningModuleID(unsigned ID) { |
| 127 | assert(isFromASTFile() && "Only works on a deserialized declaration" ); |
| 128 | // Currently, we use 64 bits to store the GlobalDeclID and the module ID |
| 129 | // to save the space. See `Decl::operator new` for details. To make it, |
| 130 | // we split the higher 32 bits to 2 16bits for the module file index of |
| 131 | // GlobalDeclID and the module ID. This introduces a limitation that the |
| 132 | // number of modules can't exceed 2^16. (The number of module files should be |
| 133 | // less than the number of modules). |
| 134 | // |
| 135 | // It is counter-intuitive to store both the module file index and the |
| 136 | // module ID as it seems redundant. However, this is not true. |
| 137 | // The module ID may be different from the module file where it is serialized |
| 138 | // from for implicit template instantiations. See |
| 139 | // https://github.com/llvm/llvm-project/issues/101939 |
| 140 | // |
| 141 | // If we reach the limitation, we have to remove the limitation by asking |
| 142 | // every deserialized declaration to pay for yet another 32 bits, or we have |
| 143 | // to review the above issue to decide what we should do for it. |
| 144 | assert((ID < llvm::maskTrailingOnes<unsigned>(16)) && |
| 145 | "Current Implementation limits the number of modules to not exceed " |
| 146 | "2^16. Contact Clang Developers to remove the limitation." ); |
| 147 | uint64_t *IDAddress = (uint64_t *)this - 1; |
| 148 | *IDAddress &= llvm::maskTrailingOnes<uint64_t>(N: 48); |
| 149 | *IDAddress |= (uint64_t)ID << 48; |
| 150 | } |
| 151 | |
| 152 | Module *Decl::getTopLevelOwningNamedModule() const { |
| 153 | if (getOwningModule() && |
| 154 | getOwningModule()->getTopLevelModule()->isNamedModule()) |
| 155 | return getOwningModule()->getTopLevelModule(); |
| 156 | |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | Module *Decl::getOwningModuleSlow() const { |
| 161 | assert(isFromASTFile() && "Not from AST file?" ); |
| 162 | return getASTContext().getExternalSource()->getModule(ID: getOwningModuleID()); |
| 163 | } |
| 164 | |
| 165 | bool Decl::hasLocalOwningModuleStorage() const { |
| 166 | return getASTContext().getLangOpts().trackLocalOwningModule(); |
| 167 | } |
| 168 | |
| 169 | const char *Decl::getDeclKindName() const { |
| 170 | switch (DeclKind) { |
| 171 | default: llvm_unreachable("Declaration not in DeclNodes.inc!" ); |
| 172 | #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED; |
| 173 | #define ABSTRACT_DECL(DECL) |
| 174 | #include "clang/AST/DeclNodes.inc" |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void Decl::setInvalidDecl(bool Invalid) { |
| 179 | InvalidDecl = Invalid; |
| 180 | assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition()); |
| 181 | if (!Invalid) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | if (!isa<ParmVarDecl>(Val: this)) { |
| 186 | // Defensive maneuver for ill-formed code: we're likely not to make it to |
| 187 | // a point where we set the access specifier, so default it to "public" |
| 188 | // to avoid triggering asserts elsewhere in the front end. |
| 189 | setAccess(AS_public); |
| 190 | } |
| 191 | |
| 192 | // Marking a DecompositionDecl as invalid implies all the child BindingDecl's |
| 193 | // are invalid too. |
| 194 | if (auto *DD = dyn_cast<DecompositionDecl>(Val: this)) { |
| 195 | for (auto *Binding : DD->bindings()) { |
| 196 | Binding->setInvalidDecl(); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | bool DeclContext::hasValidDeclKind() const { |
| 202 | switch (getDeclKind()) { |
| 203 | #define DECL(DERIVED, BASE) case Decl::DERIVED: return true; |
| 204 | #define ABSTRACT_DECL(DECL) |
| 205 | #include "clang/AST/DeclNodes.inc" |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | const char *DeclContext::getDeclKindName() const { |
| 211 | switch (getDeclKind()) { |
| 212 | #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED; |
| 213 | #define ABSTRACT_DECL(DECL) |
| 214 | #include "clang/AST/DeclNodes.inc" |
| 215 | } |
| 216 | llvm_unreachable("Declaration context not in DeclNodes.inc!" ); |
| 217 | } |
| 218 | |
| 219 | bool Decl::StatisticsEnabled = false; |
| 220 | void Decl::EnableStatistics() { |
| 221 | StatisticsEnabled = true; |
| 222 | } |
| 223 | |
| 224 | void Decl::PrintStats() { |
| 225 | llvm::errs() << "\n*** Decl Stats:\n" ; |
| 226 | |
| 227 | int totalDecls = 0; |
| 228 | #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s; |
| 229 | #define ABSTRACT_DECL(DECL) |
| 230 | #include "clang/AST/DeclNodes.inc" |
| 231 | llvm::errs() << " " << totalDecls << " decls total.\n" ; |
| 232 | |
| 233 | int totalBytes = 0; |
| 234 | #define DECL(DERIVED, BASE) \ |
| 235 | if (n##DERIVED##s > 0) { \ |
| 236 | totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \ |
| 237 | llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \ |
| 238 | << sizeof(DERIVED##Decl) << " each (" \ |
| 239 | << n##DERIVED##s * sizeof(DERIVED##Decl) \ |
| 240 | << " bytes)\n"; \ |
| 241 | } |
| 242 | #define ABSTRACT_DECL(DECL) |
| 243 | #include "clang/AST/DeclNodes.inc" |
| 244 | |
| 245 | llvm::errs() << "Total bytes = " << totalBytes << "\n" ; |
| 246 | } |
| 247 | |
| 248 | void Decl::add(Kind k) { |
| 249 | switch (k) { |
| 250 | #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break; |
| 251 | #define ABSTRACT_DECL(DECL) |
| 252 | #include "clang/AST/DeclNodes.inc" |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | bool Decl::isTemplateParameterPack() const { |
| 257 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: this)) |
| 258 | return TTP->isParameterPack(); |
| 259 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: this)) |
| 260 | return NTTP->isParameterPack(); |
| 261 | if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: this)) |
| 262 | return TTP->isParameterPack(); |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | bool Decl::isParameterPack() const { |
| 267 | if (const auto *Var = dyn_cast<ValueDecl>(Val: this)) |
| 268 | return Var->isParameterPack(); |
| 269 | |
| 270 | return isTemplateParameterPack(); |
| 271 | } |
| 272 | |
| 273 | FunctionDecl *Decl::getAsFunction() { |
| 274 | if (auto *FD = dyn_cast<FunctionDecl>(Val: this)) |
| 275 | return FD; |
| 276 | if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: this)) |
| 277 | return FTD->getTemplatedDecl(); |
| 278 | return nullptr; |
| 279 | } |
| 280 | |
| 281 | bool Decl::isTemplateDecl() const { |
| 282 | return isa<TemplateDecl>(Val: this); |
| 283 | } |
| 284 | |
| 285 | TemplateDecl *Decl::getDescribedTemplate() const { |
| 286 | if (auto *FD = dyn_cast<FunctionDecl>(Val: this)) |
| 287 | return FD->getDescribedFunctionTemplate(); |
| 288 | if (auto *RD = dyn_cast<CXXRecordDecl>(Val: this)) |
| 289 | return RD->getDescribedClassTemplate(); |
| 290 | if (auto *VD = dyn_cast<VarDecl>(Val: this)) |
| 291 | return VD->getDescribedVarTemplate(); |
| 292 | if (auto *AD = dyn_cast<TypeAliasDecl>(Val: this)) |
| 293 | return AD->getDescribedAliasTemplate(); |
| 294 | |
| 295 | return nullptr; |
| 296 | } |
| 297 | |
| 298 | const TemplateParameterList *Decl::getDescribedTemplateParams() const { |
| 299 | if (auto *TD = getDescribedTemplate()) |
| 300 | return TD->getTemplateParameters(); |
| 301 | if (auto *CTPSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(Val: this)) |
| 302 | return CTPSD->getTemplateParameters(); |
| 303 | if (auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl>(Val: this)) |
| 304 | return VTPSD->getTemplateParameters(); |
| 305 | return nullptr; |
| 306 | } |
| 307 | |
| 308 | bool Decl::isTemplated() const { |
| 309 | // A declaration is templated if it is a template or a template pattern, or |
| 310 | // is within (lexcially for a friend or local function declaration, |
| 311 | // semantically otherwise) a dependent context. |
| 312 | if (auto *AsDC = dyn_cast<DeclContext>(Val: this)) |
| 313 | return AsDC->isDependentContext(); |
| 314 | auto *DC = getFriendObjectKind() || isLocalExternDecl() |
| 315 | ? getLexicalDeclContext() : getDeclContext(); |
| 316 | return DC->isDependentContext() || isTemplateDecl() || |
| 317 | getDescribedTemplateParams(); |
| 318 | } |
| 319 | |
| 320 | unsigned Decl::getTemplateDepth() const { |
| 321 | if (auto *DC = dyn_cast<DeclContext>(Val: this)) |
| 322 | if (DC->isFileContext()) |
| 323 | return 0; |
| 324 | |
| 325 | if (auto *TPL = getDescribedTemplateParams()) |
| 326 | return TPL->getDepth() + 1; |
| 327 | |
| 328 | // If this is a dependent lambda, there might be an enclosing variable |
| 329 | // template. In this case, the next step is not the parent DeclContext (or |
| 330 | // even a DeclContext at all). |
| 331 | auto *RD = dyn_cast<CXXRecordDecl>(Val: this); |
| 332 | if (RD && RD->isDependentLambda()) |
| 333 | if (Decl *Context = RD->getLambdaContextDecl()) |
| 334 | return Context->getTemplateDepth(); |
| 335 | |
| 336 | const DeclContext *DC = |
| 337 | getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext(); |
| 338 | return cast<Decl>(Val: DC)->getTemplateDepth(); |
| 339 | } |
| 340 | |
| 341 | const DeclContext *Decl::getParentFunctionOrMethod(bool LexicalParent) const { |
| 342 | for (const DeclContext *DC = LexicalParent ? getLexicalDeclContext() |
| 343 | : getDeclContext(); |
| 344 | DC && !DC->isFileContext(); DC = DC->getParent()) |
| 345 | if (DC->isFunctionOrMethod()) |
| 346 | return DC; |
| 347 | |
| 348 | return nullptr; |
| 349 | } |
| 350 | |
| 351 | //===----------------------------------------------------------------------===// |
| 352 | // PrettyStackTraceDecl Implementation |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | |
| 355 | void PrettyStackTraceDecl::print(raw_ostream &OS) const { |
| 356 | SourceLocation TheLoc = Loc; |
| 357 | if (TheLoc.isInvalid() && TheDecl) |
| 358 | TheLoc = TheDecl->getLocation(); |
| 359 | |
| 360 | if (TheLoc.isValid()) { |
| 361 | TheLoc.print(OS, SM); |
| 362 | OS << ": " ; |
| 363 | } |
| 364 | |
| 365 | OS << Message; |
| 366 | |
| 367 | if (const auto *DN = dyn_cast_or_null<NamedDecl>(Val: TheDecl)) { |
| 368 | OS << " '" ; |
| 369 | DN->printQualifiedName(OS); |
| 370 | OS << '\''; |
| 371 | } |
| 372 | OS << '\n'; |
| 373 | } |
| 374 | |
| 375 | //===----------------------------------------------------------------------===// |
| 376 | // Decl Implementation |
| 377 | //===----------------------------------------------------------------------===// |
| 378 | |
| 379 | // Out-of-line virtual method providing a home for Decl. |
| 380 | Decl::~Decl() = default; |
| 381 | |
| 382 | void Decl::setDeclContext(DeclContext *DC) { |
| 383 | DeclCtx = DC; |
| 384 | } |
| 385 | |
| 386 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 387 | if (DC == getLexicalDeclContext()) |
| 388 | return; |
| 389 | |
| 390 | if (isInSemaDC()) { |
| 391 | setDeclContextsImpl(SemaDC: getDeclContext(), LexicalDC: DC, Ctx&: getASTContext()); |
| 392 | } else { |
| 393 | getMultipleDC()->LexicalDC = DC; |
| 394 | } |
| 395 | |
| 396 | // FIXME: We shouldn't be changing the lexical context of declarations |
| 397 | // imported from AST files. |
| 398 | if (!isFromASTFile()) { |
| 399 | setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC)); |
| 400 | if (hasOwningModule()) |
| 401 | setLocalOwningModule(cast<Decl>(Val: DC)->getOwningModule()); |
| 402 | } |
| 403 | |
| 404 | assert( |
| 405 | ((getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported && |
| 406 | getModuleOwnershipKind() != ModuleOwnershipKind::VisiblePromoted) || |
| 407 | getOwningModule()) && |
| 408 | "hidden declaration has no owning module" ); |
| 409 | } |
| 410 | |
| 411 | void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, |
| 412 | ASTContext &Ctx) { |
| 413 | if (SemaDC == LexicalDC) { |
| 414 | DeclCtx = SemaDC; |
| 415 | } else { |
| 416 | auto *MDC = new (Ctx) Decl::MultipleDC(); |
| 417 | MDC->SemanticDC = SemaDC; |
| 418 | MDC->LexicalDC = LexicalDC; |
| 419 | DeclCtx = MDC; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | bool Decl::isInLocalScopeForInstantiation() const { |
| 424 | const DeclContext *LDC = getLexicalDeclContext(); |
| 425 | if (!LDC->isDependentContext()) |
| 426 | return false; |
| 427 | while (true) { |
| 428 | if (LDC->isFunctionOrMethod()) |
| 429 | return true; |
| 430 | if (!isa<TagDecl>(Val: LDC)) |
| 431 | return false; |
| 432 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(Val: LDC)) |
| 433 | if (CRD->isLambda()) |
| 434 | return true; |
| 435 | LDC = LDC->getLexicalParent(); |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | bool Decl::isInAnonymousNamespace() const { |
| 441 | for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) { |
| 442 | if (const auto *ND = dyn_cast<NamespaceDecl>(Val: DC)) |
| 443 | if (ND->isAnonymousNamespace()) |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | bool Decl::isInStdNamespace() const { |
| 451 | const DeclContext *DC = getDeclContext(); |
| 452 | return DC && DC->getNonTransparentContext()->isStdNamespace(); |
| 453 | } |
| 454 | |
| 455 | bool Decl::isFileContextDecl() const { |
| 456 | const auto *DC = dyn_cast<DeclContext>(Val: this); |
| 457 | return DC && DC->isFileContext(); |
| 458 | } |
| 459 | |
| 460 | bool Decl::isFlexibleArrayMemberLike( |
| 461 | const ASTContext &Ctx, const Decl *D, QualType Ty, |
| 462 | LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel, |
| 463 | bool IgnoreTemplateOrMacroSubstitution) { |
| 464 | // For compatibility with existing code, we treat arrays of length 0 or |
| 465 | // 1 as flexible array members. |
| 466 | const auto *CAT = Ctx.getAsConstantArrayType(T: Ty); |
| 467 | if (CAT) { |
| 468 | using FAMKind = LangOptions::StrictFlexArraysLevelKind; |
| 469 | |
| 470 | llvm::APInt Size = CAT->getSize(); |
| 471 | if (StrictFlexArraysLevel == FAMKind::IncompleteOnly) |
| 472 | return false; |
| 473 | |
| 474 | // GCC extension, only allowed to represent a FAM. |
| 475 | if (Size.isZero()) |
| 476 | return true; |
| 477 | |
| 478 | if (StrictFlexArraysLevel == FAMKind::ZeroOrIncomplete && Size.uge(RHS: 1)) |
| 479 | return false; |
| 480 | |
| 481 | if (StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete && Size.uge(RHS: 2)) |
| 482 | return false; |
| 483 | } else if (!Ctx.getAsIncompleteArrayType(T: Ty)) { |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | if (const auto *OID = dyn_cast_if_present<ObjCIvarDecl>(Val: D)) |
| 488 | return OID->getNextIvar() == nullptr; |
| 489 | |
| 490 | const auto *FD = dyn_cast_if_present<FieldDecl>(Val: D); |
| 491 | if (!FD) |
| 492 | return false; |
| 493 | |
| 494 | if (CAT) { |
| 495 | // GCC treats an array memeber of a union as an FAM if the size is one or |
| 496 | // zero. |
| 497 | llvm::APInt Size = CAT->getSize(); |
| 498 | if (FD->getParent()->isUnion() && (Size.isZero() || Size.isOne())) |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | // Don't consider sizes resulting from macro expansions or template argument |
| 503 | // substitution to form C89 tail-padded arrays. |
| 504 | if (IgnoreTemplateOrMacroSubstitution) { |
| 505 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
| 506 | while (TInfo) { |
| 507 | TypeLoc TL = TInfo->getTypeLoc(); |
| 508 | |
| 509 | // Look through typedefs. |
| 510 | if (TypedefTypeLoc TTL = TL.getAsAdjusted<TypedefTypeLoc>()) { |
| 511 | TInfo = TTL.getDecl()->getTypeSourceInfo(); |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | if (auto CTL = TL.getAs<ConstantArrayTypeLoc>()) { |
| 516 | if (const Expr *SizeExpr = |
| 517 | dyn_cast_if_present<IntegerLiteral>(Val: CTL.getSizeExpr()); |
| 518 | !SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Test that the field is the last in the structure. |
| 527 | RecordDecl::field_iterator FI( |
| 528 | DeclContext::decl_iterator(const_cast<FieldDecl *>(FD))); |
| 529 | return ++FI == FD->getParent()->field_end(); |
| 530 | } |
| 531 | |
| 532 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
| 533 | if (auto *TUD = dyn_cast<TranslationUnitDecl>(Val: this)) |
| 534 | return TUD; |
| 535 | |
| 536 | DeclContext *DC = getDeclContext(); |
| 537 | assert(DC && "This decl is not contained in a translation unit!" ); |
| 538 | |
| 539 | while (!DC->isTranslationUnit()) { |
| 540 | DC = DC->getParent(); |
| 541 | assert(DC && "This decl is not contained in a translation unit!" ); |
| 542 | } |
| 543 | |
| 544 | return cast<TranslationUnitDecl>(Val: DC); |
| 545 | } |
| 546 | |
| 547 | ASTContext &Decl::getASTContext() const { |
| 548 | return getTranslationUnitDecl()->getASTContext(); |
| 549 | } |
| 550 | |
| 551 | /// Helper to get the language options from the ASTContext. |
| 552 | /// Defined out of line to avoid depending on ASTContext.h. |
| 553 | const LangOptions &Decl::getLangOpts() const { |
| 554 | return getASTContext().getLangOpts(); |
| 555 | } |
| 556 | |
| 557 | ASTMutationListener *Decl::getASTMutationListener() const { |
| 558 | return getASTContext().getASTMutationListener(); |
| 559 | } |
| 560 | |
| 561 | unsigned Decl::getMaxAlignment() const { |
| 562 | if (!hasAttrs()) |
| 563 | return 0; |
| 564 | |
| 565 | unsigned Align = 0; |
| 566 | const AttrVec &V = getAttrs(); |
| 567 | ASTContext &Ctx = getASTContext(); |
| 568 | specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end()); |
| 569 | for (; I != E; ++I) { |
| 570 | if (!I->isAlignmentErrorDependent()) |
| 571 | Align = std::max(a: Align, b: I->getAlignment(Ctx)); |
| 572 | } |
| 573 | return Align; |
| 574 | } |
| 575 | |
| 576 | bool Decl::isUsed(bool CheckUsedAttr) const { |
| 577 | const Decl *CanonD = getCanonicalDecl(); |
| 578 | if (CanonD->Used) |
| 579 | return true; |
| 580 | |
| 581 | // Check for used attribute. |
| 582 | // Ask the most recent decl, since attributes accumulate in the redecl chain. |
| 583 | if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>()) |
| 584 | return true; |
| 585 | |
| 586 | // The information may have not been deserialized yet. Force deserialization |
| 587 | // to complete the needed information. |
| 588 | return getMostRecentDecl()->getCanonicalDecl()->Used; |
| 589 | } |
| 590 | |
| 591 | void Decl::markUsed(ASTContext &C) { |
| 592 | if (isUsed(CheckUsedAttr: false)) |
| 593 | return; |
| 594 | |
| 595 | if (C.getASTMutationListener()) |
| 596 | C.getASTMutationListener()->DeclarationMarkedUsed(D: this); |
| 597 | |
| 598 | setIsUsed(); |
| 599 | } |
| 600 | |
| 601 | bool Decl::isReferenced() const { |
| 602 | if (Referenced) |
| 603 | return true; |
| 604 | |
| 605 | // Check redeclarations. |
| 606 | for (const auto *I : redecls()) |
| 607 | if (I->Referenced) |
| 608 | return true; |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const { |
| 614 | const Decl *Definition = nullptr; |
| 615 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: this)) { |
| 616 | Definition = ID->getDefinition(); |
| 617 | } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(Val: this)) { |
| 618 | Definition = PD->getDefinition(); |
| 619 | } else if (auto *TD = dyn_cast<TagDecl>(Val: this)) { |
| 620 | Definition = TD->getDefinition(); |
| 621 | } |
| 622 | if (!Definition) |
| 623 | Definition = this; |
| 624 | |
| 625 | if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>()) |
| 626 | return attr; |
| 627 | if (auto *dcd = dyn_cast<Decl>(Val: getDeclContext())) { |
| 628 | return dcd->getAttr<ExternalSourceSymbolAttr>(); |
| 629 | } |
| 630 | |
| 631 | return nullptr; |
| 632 | } |
| 633 | |
| 634 | bool Decl::hasDefiningAttr() const { |
| 635 | return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>() || |
| 636 | hasAttr<LoaderUninitializedAttr>(); |
| 637 | } |
| 638 | |
| 639 | const Attr *Decl::getDefiningAttr() const { |
| 640 | if (auto *AA = getAttr<AliasAttr>()) |
| 641 | return AA; |
| 642 | if (auto *IFA = getAttr<IFuncAttr>()) |
| 643 | return IFA; |
| 644 | if (auto *NZA = getAttr<LoaderUninitializedAttr>()) |
| 645 | return NZA; |
| 646 | return nullptr; |
| 647 | } |
| 648 | |
| 649 | static StringRef getRealizedPlatform(const AvailabilityAttr *A, |
| 650 | const ASTContext &Context) { |
| 651 | // Check if this is an App Extension "platform", and if so chop off |
| 652 | // the suffix for matching with the actual platform. |
| 653 | StringRef RealizedPlatform = A->getPlatform()->getName(); |
| 654 | if (!Context.getLangOpts().AppExt) |
| 655 | return RealizedPlatform; |
| 656 | size_t suffix = RealizedPlatform.rfind(Str: "_app_extension" ); |
| 657 | if (suffix != StringRef::npos) |
| 658 | return RealizedPlatform.slice(Start: 0, End: suffix); |
| 659 | return RealizedPlatform; |
| 660 | } |
| 661 | |
| 662 | /// Determine the availability of the given declaration based on |
| 663 | /// the target platform. |
| 664 | /// |
| 665 | /// When it returns an availability result other than \c AR_Available, |
| 666 | /// if the \p Message parameter is non-NULL, it will be set to a |
| 667 | /// string describing why the entity is unavailable. |
| 668 | /// |
| 669 | /// FIXME: Make these strings localizable, since they end up in |
| 670 | /// diagnostics. |
| 671 | static AvailabilityResult CheckAvailability(ASTContext &Context, |
| 672 | const AvailabilityAttr *A, |
| 673 | std::string *Message, |
| 674 | VersionTuple EnclosingVersion) { |
| 675 | if (EnclosingVersion.empty()) |
| 676 | EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion(); |
| 677 | |
| 678 | if (EnclosingVersion.empty()) |
| 679 | return AR_Available; |
| 680 | |
| 681 | StringRef ActualPlatform = A->getPlatform()->getName(); |
| 682 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 683 | |
| 684 | // Match the platform name. |
| 685 | if (getRealizedPlatform(A, Context) != TargetPlatform) |
| 686 | return AR_Available; |
| 687 | |
| 688 | StringRef PrettyPlatformName |
| 689 | = AvailabilityAttr::getPrettyPlatformName(Platform: ActualPlatform); |
| 690 | |
| 691 | if (PrettyPlatformName.empty()) |
| 692 | PrettyPlatformName = ActualPlatform; |
| 693 | |
| 694 | std::string HintMessage; |
| 695 | if (!A->getMessage().empty()) { |
| 696 | HintMessage = " - " ; |
| 697 | HintMessage += A->getMessage(); |
| 698 | } |
| 699 | |
| 700 | // Make sure that this declaration has not been marked 'unavailable'. |
| 701 | if (A->getUnavailable()) { |
| 702 | if (Message) { |
| 703 | Message->clear(); |
| 704 | llvm::raw_string_ostream Out(*Message); |
| 705 | Out << "not available on " << PrettyPlatformName |
| 706 | << HintMessage; |
| 707 | } |
| 708 | |
| 709 | return AR_Unavailable; |
| 710 | } |
| 711 | |
| 712 | // Make sure that this declaration has already been introduced. |
| 713 | if (!A->getIntroduced().empty() && |
| 714 | EnclosingVersion < A->getIntroduced()) { |
| 715 | const IdentifierInfo *IIEnv = A->getEnvironment(); |
| 716 | auto &Triple = Context.getTargetInfo().getTriple(); |
| 717 | StringRef TargetEnv = Triple.getEnvironmentName(); |
| 718 | StringRef EnvName = |
| 719 | llvm::Triple::getEnvironmentTypeName(Kind: Triple.getEnvironment()); |
| 720 | // Matching environment or no environment on attribute. |
| 721 | if (!IIEnv || (Triple.hasEnvironment() && IIEnv->getName() == TargetEnv)) { |
| 722 | if (Message) { |
| 723 | Message->clear(); |
| 724 | llvm::raw_string_ostream Out(*Message); |
| 725 | VersionTuple VTI(A->getIntroduced()); |
| 726 | Out << "introduced in " << PrettyPlatformName << " " << VTI; |
| 727 | if (Triple.hasEnvironment()) |
| 728 | Out << " " << EnvName; |
| 729 | Out << HintMessage; |
| 730 | } |
| 731 | } |
| 732 | // Non-matching environment or no environment on target. |
| 733 | else { |
| 734 | if (Message) { |
| 735 | Message->clear(); |
| 736 | llvm::raw_string_ostream Out(*Message); |
| 737 | Out << "not available on " << PrettyPlatformName; |
| 738 | if (Triple.hasEnvironment()) |
| 739 | Out << " " << EnvName; |
| 740 | Out << HintMessage; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced; |
| 745 | } |
| 746 | |
| 747 | // Make sure that this declaration hasn't been obsoleted. |
| 748 | if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) { |
| 749 | if (Message) { |
| 750 | Message->clear(); |
| 751 | llvm::raw_string_ostream Out(*Message); |
| 752 | VersionTuple VTO(A->getObsoleted()); |
| 753 | Out << "obsoleted in " << PrettyPlatformName << ' ' |
| 754 | << VTO << HintMessage; |
| 755 | } |
| 756 | |
| 757 | return AR_Unavailable; |
| 758 | } |
| 759 | |
| 760 | // Make sure that this declaration hasn't been deprecated. |
| 761 | if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) { |
| 762 | if (Message) { |
| 763 | Message->clear(); |
| 764 | llvm::raw_string_ostream Out(*Message); |
| 765 | VersionTuple VTD(A->getDeprecated()); |
| 766 | Out << "first deprecated in " << PrettyPlatformName << ' ' |
| 767 | << VTD << HintMessage; |
| 768 | } |
| 769 | |
| 770 | return AR_Deprecated; |
| 771 | } |
| 772 | |
| 773 | return AR_Available; |
| 774 | } |
| 775 | |
| 776 | AvailabilityResult Decl::getAvailability(std::string *Message, |
| 777 | VersionTuple EnclosingVersion, |
| 778 | StringRef *RealizedPlatform) const { |
| 779 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: this)) |
| 780 | return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion, |
| 781 | RealizedPlatform); |
| 782 | |
| 783 | AvailabilityResult Result = AR_Available; |
| 784 | std::string ResultMessage; |
| 785 | |
| 786 | for (const auto *A : attrs()) { |
| 787 | if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(Val: A)) { |
| 788 | if (Result >= AR_Deprecated) |
| 789 | continue; |
| 790 | |
| 791 | if (Message) |
| 792 | ResultMessage = std::string(Deprecated->getMessage()); |
| 793 | |
| 794 | Result = AR_Deprecated; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | if (const auto *Unavailable = dyn_cast<UnavailableAttr>(Val: A)) { |
| 799 | if (Message) |
| 800 | *Message = std::string(Unavailable->getMessage()); |
| 801 | return AR_Unavailable; |
| 802 | } |
| 803 | |
| 804 | if (const auto *Availability = dyn_cast<AvailabilityAttr>(Val: A)) { |
| 805 | AvailabilityResult AR = CheckAvailability(Context&: getASTContext(), A: Availability, |
| 806 | Message, EnclosingVersion); |
| 807 | |
| 808 | if (AR == AR_Unavailable) { |
| 809 | if (RealizedPlatform) |
| 810 | *RealizedPlatform = Availability->getPlatform()->getName(); |
| 811 | return AR_Unavailable; |
| 812 | } |
| 813 | |
| 814 | if (AR > Result) { |
| 815 | Result = AR; |
| 816 | if (Message) |
| 817 | ResultMessage.swap(s&: *Message); |
| 818 | } |
| 819 | continue; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (Message) |
| 824 | Message->swap(s&: ResultMessage); |
| 825 | return Result; |
| 826 | } |
| 827 | |
| 828 | VersionTuple Decl::getVersionIntroduced() const { |
| 829 | const ASTContext &Context = getASTContext(); |
| 830 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 831 | for (const auto *A : attrs()) { |
| 832 | if (const auto *Availability = dyn_cast<AvailabilityAttr>(Val: A)) { |
| 833 | if (getRealizedPlatform(A: Availability, Context) != TargetPlatform) |
| 834 | continue; |
| 835 | if (!Availability->getIntroduced().empty()) |
| 836 | return Availability->getIntroduced(); |
| 837 | } |
| 838 | } |
| 839 | return {}; |
| 840 | } |
| 841 | |
| 842 | bool Decl::canBeWeakImported(bool &IsDefinition) const { |
| 843 | IsDefinition = false; |
| 844 | |
| 845 | // Variables, if they aren't definitions. |
| 846 | if (const auto *Var = dyn_cast<VarDecl>(Val: this)) { |
| 847 | if (Var->isThisDeclarationADefinition()) { |
| 848 | IsDefinition = true; |
| 849 | return false; |
| 850 | } |
| 851 | return true; |
| 852 | } |
| 853 | // Functions, if they aren't definitions. |
| 854 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: this)) { |
| 855 | if (FD->hasBody()) { |
| 856 | IsDefinition = true; |
| 857 | return false; |
| 858 | } |
| 859 | return true; |
| 860 | |
| 861 | } |
| 862 | // Objective-C classes, if this is the non-fragile runtime. |
| 863 | if (isa<ObjCInterfaceDecl>(Val: this) && |
| 864 | getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) { |
| 865 | return true; |
| 866 | } |
| 867 | // Nothing else. |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | bool Decl::isWeakImported() const { |
| 872 | bool IsDefinition; |
| 873 | if (!canBeWeakImported(IsDefinition)) |
| 874 | return false; |
| 875 | |
| 876 | for (const auto *A : getMostRecentDecl()->attrs()) { |
| 877 | if (isa<WeakImportAttr>(Val: A)) |
| 878 | return true; |
| 879 | |
| 880 | if (const auto *Availability = dyn_cast<AvailabilityAttr>(Val: A)) { |
| 881 | if (CheckAvailability(Context&: getASTContext(), A: Availability, Message: nullptr, |
| 882 | EnclosingVersion: VersionTuple()) == AR_NotYetIntroduced) |
| 883 | return true; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 891 | switch (DeclKind) { |
| 892 | case Function: |
| 893 | case CXXDeductionGuide: |
| 894 | case CXXMethod: |
| 895 | case CXXConstructor: |
| 896 | case ConstructorUsingShadow: |
| 897 | case CXXDestructor: |
| 898 | case CXXConversion: |
| 899 | case EnumConstant: |
| 900 | case Var: |
| 901 | case ImplicitParam: |
| 902 | case ParmVar: |
| 903 | case ObjCMethod: |
| 904 | case ObjCProperty: |
| 905 | case MSProperty: |
| 906 | case HLSLBuffer: |
| 907 | case HLSLRootSignature: |
| 908 | return IDNS_Ordinary; |
| 909 | case Label: |
| 910 | return IDNS_Label; |
| 911 | |
| 912 | case Binding: |
| 913 | case NonTypeTemplateParm: |
| 914 | case VarTemplate: |
| 915 | case Concept: |
| 916 | // These (C++-only) declarations are found by redeclaration lookup for |
| 917 | // tag types, so we include them in the tag namespace. |
| 918 | return IDNS_Ordinary | IDNS_Tag; |
| 919 | |
| 920 | case ObjCCompatibleAlias: |
| 921 | case ObjCInterface: |
| 922 | return IDNS_Ordinary | IDNS_Type; |
| 923 | |
| 924 | case Typedef: |
| 925 | case TypeAlias: |
| 926 | case TemplateTypeParm: |
| 927 | case ObjCTypeParam: |
| 928 | return IDNS_Ordinary | IDNS_Type; |
| 929 | |
| 930 | case UnresolvedUsingTypename: |
| 931 | return IDNS_Ordinary | IDNS_Type | IDNS_Using; |
| 932 | |
| 933 | case UsingShadow: |
| 934 | return 0; // we'll actually overwrite this later |
| 935 | |
| 936 | case UnresolvedUsingValue: |
| 937 | return IDNS_Ordinary | IDNS_Using; |
| 938 | |
| 939 | case Using: |
| 940 | case UsingPack: |
| 941 | case UsingEnum: |
| 942 | return IDNS_Using; |
| 943 | |
| 944 | case ObjCProtocol: |
| 945 | return IDNS_ObjCProtocol; |
| 946 | |
| 947 | case Field: |
| 948 | case IndirectField: |
| 949 | case ObjCAtDefsField: |
| 950 | case ObjCIvar: |
| 951 | return IDNS_Member; |
| 952 | |
| 953 | case Record: |
| 954 | case CXXRecord: |
| 955 | case Enum: |
| 956 | return IDNS_Tag | IDNS_Type; |
| 957 | |
| 958 | case Namespace: |
| 959 | case NamespaceAlias: |
| 960 | return IDNS_Namespace; |
| 961 | |
| 962 | case FunctionTemplate: |
| 963 | return IDNS_Ordinary; |
| 964 | |
| 965 | case ClassTemplate: |
| 966 | case TemplateTemplateParm: |
| 967 | case TypeAliasTemplate: |
| 968 | return IDNS_Ordinary | IDNS_Tag | IDNS_Type; |
| 969 | |
| 970 | case UnresolvedUsingIfExists: |
| 971 | return IDNS_Type | IDNS_Ordinary; |
| 972 | |
| 973 | case OMPDeclareReduction: |
| 974 | return IDNS_OMPReduction; |
| 975 | |
| 976 | case OMPDeclareMapper: |
| 977 | return IDNS_OMPMapper; |
| 978 | |
| 979 | // Never have names. |
| 980 | case Friend: |
| 981 | case FriendTemplate: |
| 982 | case AccessSpec: |
| 983 | case LinkageSpec: |
| 984 | case Export: |
| 985 | case FileScopeAsm: |
| 986 | case TopLevelStmt: |
| 987 | case StaticAssert: |
| 988 | case ObjCPropertyImpl: |
| 989 | case PragmaComment: |
| 990 | case PragmaDetectMismatch: |
| 991 | case Block: |
| 992 | case Captured: |
| 993 | case OutlinedFunction: |
| 994 | case TranslationUnit: |
| 995 | case ExternCContext: |
| 996 | case Decomposition: |
| 997 | case MSGuid: |
| 998 | case UnnamedGlobalConstant: |
| 999 | case TemplateParamObject: |
| 1000 | |
| 1001 | case UsingDirective: |
| 1002 | case BuiltinTemplate: |
| 1003 | case ClassTemplateSpecialization: |
| 1004 | case ClassTemplatePartialSpecialization: |
| 1005 | case VarTemplateSpecialization: |
| 1006 | case VarTemplatePartialSpecialization: |
| 1007 | case ObjCImplementation: |
| 1008 | case ObjCCategory: |
| 1009 | case ObjCCategoryImpl: |
| 1010 | case Import: |
| 1011 | case OMPThreadPrivate: |
| 1012 | case OMPGroupPrivate: |
| 1013 | case OMPAllocate: |
| 1014 | case OMPRequires: |
| 1015 | case OMPCapturedExpr: |
| 1016 | case Empty: |
| 1017 | case LifetimeExtendedTemporary: |
| 1018 | case RequiresExprBody: |
| 1019 | case ImplicitConceptSpecialization: |
| 1020 | case OpenACCDeclare: |
| 1021 | case OpenACCRoutine: |
| 1022 | // Never looked up by name. |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | llvm_unreachable("Invalid DeclKind!" ); |
| 1027 | } |
| 1028 | |
| 1029 | void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) { |
| 1030 | assert(!HasAttrs && "Decl already contains attrs." ); |
| 1031 | |
| 1032 | AttrVec &AttrBlank = Ctx.getDeclAttrs(D: this); |
| 1033 | assert(AttrBlank.empty() && "HasAttrs was wrong?" ); |
| 1034 | |
| 1035 | AttrBlank = attrs; |
| 1036 | HasAttrs = true; |
| 1037 | } |
| 1038 | |
| 1039 | void Decl::dropAttrs() { |
| 1040 | if (!HasAttrs) return; |
| 1041 | |
| 1042 | HasAttrs = false; |
| 1043 | getASTContext().eraseDeclAttrs(D: this); |
| 1044 | } |
| 1045 | |
| 1046 | void Decl::addAttr(Attr *A) { |
| 1047 | if (!hasAttrs()) { |
| 1048 | setAttrs(AttrVec(1, A)); |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | AttrVec &Attrs = getAttrs(); |
| 1053 | if (!A->isInherited()) { |
| 1054 | Attrs.push_back(Elt: A); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | // Attribute inheritance is processed after attribute parsing. To keep the |
| 1059 | // order as in the source code, add inherited attributes before non-inherited |
| 1060 | // ones. |
| 1061 | auto I = Attrs.begin(), E = Attrs.end(); |
| 1062 | for (; I != E; ++I) { |
| 1063 | if (!(*I)->isInherited()) |
| 1064 | break; |
| 1065 | } |
| 1066 | Attrs.insert(I, Elt: A); |
| 1067 | } |
| 1068 | |
| 1069 | const AttrVec &Decl::getAttrs() const { |
| 1070 | assert(HasAttrs && "No attrs to get!" ); |
| 1071 | return getASTContext().getDeclAttrs(D: this); |
| 1072 | } |
| 1073 | |
| 1074 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
| 1075 | Decl::Kind DK = D->getDeclKind(); |
| 1076 | switch (DK) { |
| 1077 | #define DECL(NAME, BASE) |
| 1078 | #define DECL_CONTEXT(NAME) \ |
| 1079 | case Decl::NAME: \ |
| 1080 | return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D)); |
| 1081 | #include "clang/AST/DeclNodes.inc" |
| 1082 | default: |
| 1083 | llvm_unreachable("a decl that inherits DeclContext isn't handled" ); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
| 1088 | Decl::Kind DK = D->getKind(); |
| 1089 | switch(DK) { |
| 1090 | #define DECL(NAME, BASE) |
| 1091 | #define DECL_CONTEXT(NAME) \ |
| 1092 | case Decl::NAME: \ |
| 1093 | return static_cast<NAME##Decl *>(const_cast<Decl *>(D)); |
| 1094 | #include "clang/AST/DeclNodes.inc" |
| 1095 | default: |
| 1096 | llvm_unreachable("a decl that inherits DeclContext isn't handled" ); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | SourceLocation Decl::getBodyRBrace() const { |
| 1101 | // Special handling of FunctionDecl to avoid de-serializing the body from PCH. |
| 1102 | // FunctionDecl stores EndRangeLoc for this purpose. |
| 1103 | if (const auto *FD = dyn_cast<FunctionDecl>(Val: this)) { |
| 1104 | const FunctionDecl *Definition; |
| 1105 | if (FD->hasBody(Definition)) |
| 1106 | return Definition->getSourceRange().getEnd(); |
| 1107 | return {}; |
| 1108 | } |
| 1109 | |
| 1110 | if (Stmt *Body = getBody()) |
| 1111 | return Body->getSourceRange().getEnd(); |
| 1112 | |
| 1113 | return {}; |
| 1114 | } |
| 1115 | |
| 1116 | bool Decl::AccessDeclContextCheck() const { |
| 1117 | #ifndef NDEBUG |
| 1118 | // Suppress this check if any of the following hold: |
| 1119 | // 1. this is the translation unit (and thus has no parent) |
| 1120 | // 2. this is a template parameter (and thus doesn't belong to its context) |
| 1121 | // 3. this is a non-type template parameter |
| 1122 | // 4. the context is not a record |
| 1123 | // 5. it's invalid |
| 1124 | // 6. it's a C++0x static_assert. |
| 1125 | // 7. it's a block literal declaration |
| 1126 | // 8. it's a temporary with lifetime extended due to being default value. |
| 1127 | if (isa<TranslationUnitDecl>(this) || isa<TemplateTypeParmDecl>(this) || |
| 1128 | isa<NonTypeTemplateParmDecl>(this) || !getDeclContext() || |
| 1129 | !isa<CXXRecordDecl>(getDeclContext()) || isInvalidDecl() || |
| 1130 | isa<StaticAssertDecl>(this) || isa<BlockDecl>(this) || |
| 1131 | // FIXME: a ParmVarDecl can have ClassTemplateSpecialization |
| 1132 | // as DeclContext (?). |
| 1133 | isa<ParmVarDecl>(this) || |
| 1134 | // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have |
| 1135 | // AS_none as access specifier. |
| 1136 | isa<CXXRecordDecl>(this) || isa<LifetimeExtendedTemporaryDecl>(this)) |
| 1137 | return true; |
| 1138 | |
| 1139 | assert(Access != AS_none && |
| 1140 | "Access specifier is AS_none inside a record decl" ); |
| 1141 | #endif |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
| 1145 | bool Decl::isInExportDeclContext() const { |
| 1146 | const DeclContext *DC = getLexicalDeclContext(); |
| 1147 | |
| 1148 | while (DC && !isa<ExportDecl>(Val: DC)) |
| 1149 | DC = DC->getLexicalParent(); |
| 1150 | |
| 1151 | return isa_and_nonnull<ExportDecl>(Val: DC); |
| 1152 | } |
| 1153 | |
| 1154 | bool Decl::isModuleLocal() const { |
| 1155 | if (isa<NamespaceDecl, TranslationUnitDecl>(Val: this)) |
| 1156 | return false; |
| 1157 | auto *M = getOwningModule(); |
| 1158 | return M && M->isNamedModule() && |
| 1159 | getModuleOwnershipKind() == ModuleOwnershipKind::ReachableWhenImported; |
| 1160 | } |
| 1161 | |
| 1162 | bool Decl::isInAnotherModuleUnit() const { |
| 1163 | auto *M = getOwningModule(); |
| 1164 | |
| 1165 | if (!M) |
| 1166 | return false; |
| 1167 | |
| 1168 | // FIXME or NOTE: maybe we need to be clear about the semantics |
| 1169 | // of clang header modules. e.g., if this lives in a clang header |
| 1170 | // module included by the current unit, should we return false |
| 1171 | // here? |
| 1172 | // |
| 1173 | // This is clear for header units as the specification says the |
| 1174 | // header units live in a synthesised translation unit. So we |
| 1175 | // can return false here. |
| 1176 | M = M->getTopLevelModule(); |
| 1177 | if (!M->isNamedModule()) |
| 1178 | return false; |
| 1179 | |
| 1180 | return M != getASTContext().getCurrentNamedModule(); |
| 1181 | } |
| 1182 | |
| 1183 | bool Decl::isInCurrentModuleUnit() const { |
| 1184 | auto *M = getOwningModule(); |
| 1185 | |
| 1186 | if (!M || !M->isNamedModule()) |
| 1187 | return false; |
| 1188 | |
| 1189 | return M == getASTContext().getCurrentNamedModule(); |
| 1190 | } |
| 1191 | |
| 1192 | bool Decl::shouldEmitInExternalSource() const { |
| 1193 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 1194 | if (!Source) |
| 1195 | return false; |
| 1196 | |
| 1197 | return Source->hasExternalDefinitions(D: this) == ExternalASTSource::EK_Always; |
| 1198 | } |
| 1199 | |
| 1200 | bool Decl::isFromExplicitGlobalModule() const { |
| 1201 | return getOwningModule() && getOwningModule()->isExplicitGlobalModule(); |
| 1202 | } |
| 1203 | |
| 1204 | bool Decl::isFromGlobalModule() const { |
| 1205 | return getOwningModule() && getOwningModule()->isGlobalModule(); |
| 1206 | } |
| 1207 | |
| 1208 | bool Decl::isInNamedModule() const { |
| 1209 | return getOwningModule() && getOwningModule()->isNamedModule(); |
| 1210 | } |
| 1211 | |
| 1212 | bool Decl::() const { |
| 1213 | return getOwningModule() && getOwningModule()->isHeaderUnit(); |
| 1214 | } |
| 1215 | |
| 1216 | static Decl::Kind getKind(const Decl *D) { return D->getKind(); } |
| 1217 | static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); } |
| 1218 | |
| 1219 | int64_t Decl::getID() const { |
| 1220 | return getASTContext().getAllocator().identifyKnownAlignedObject<Decl>(Ptr: this); |
| 1221 | } |
| 1222 | |
| 1223 | const FunctionType *Decl::getFunctionType(bool BlocksToo) const { |
| 1224 | QualType Ty; |
| 1225 | if (const auto *D = dyn_cast<ValueDecl>(Val: this)) |
| 1226 | Ty = D->getType(); |
| 1227 | else if (const auto *D = dyn_cast<TypedefNameDecl>(Val: this)) |
| 1228 | Ty = D->getUnderlyingType(); |
| 1229 | else |
| 1230 | return nullptr; |
| 1231 | |
| 1232 | if (Ty.isNull()) { |
| 1233 | // BindingDecls do not have types during parsing, so return nullptr. This is |
| 1234 | // the only known case where `Ty` is null. |
| 1235 | assert(isa<BindingDecl>(this)); |
| 1236 | return nullptr; |
| 1237 | } |
| 1238 | |
| 1239 | if (Ty->isFunctionPointerType()) |
| 1240 | Ty = Ty->castAs<PointerType>()->getPointeeType(); |
| 1241 | else if (Ty->isMemberFunctionPointerType()) |
| 1242 | Ty = Ty->castAs<MemberPointerType>()->getPointeeType(); |
| 1243 | else if (Ty->isFunctionReferenceType()) |
| 1244 | Ty = Ty->castAs<ReferenceType>()->getPointeeType(); |
| 1245 | else if (BlocksToo && Ty->isBlockPointerType()) |
| 1246 | Ty = Ty->castAs<BlockPointerType>()->getPointeeType(); |
| 1247 | |
| 1248 | return Ty->getAs<FunctionType>(); |
| 1249 | } |
| 1250 | |
| 1251 | bool Decl::isFunctionPointerType() const { |
| 1252 | QualType Ty; |
| 1253 | if (const auto *D = dyn_cast<ValueDecl>(Val: this)) |
| 1254 | Ty = D->getType(); |
| 1255 | else if (const auto *D = dyn_cast<TypedefNameDecl>(Val: this)) |
| 1256 | Ty = D->getUnderlyingType(); |
| 1257 | else |
| 1258 | return false; |
| 1259 | |
| 1260 | return Ty.getCanonicalType()->isFunctionPointerType(); |
| 1261 | } |
| 1262 | |
| 1263 | DeclContext *Decl::getNonTransparentDeclContext() { |
| 1264 | assert(getDeclContext()); |
| 1265 | return getDeclContext()->getNonTransparentContext(); |
| 1266 | } |
| 1267 | |
| 1268 | /// Starting at a given context (a Decl or DeclContext), look for a |
| 1269 | /// code context that is not a closure (a lambda, block, etc.). |
| 1270 | template <class T> static Decl *getNonClosureContext(T *D) { |
| 1271 | if (getKind(D) == Decl::CXXMethod) { |
| 1272 | auto *MD = cast<CXXMethodDecl>(D); |
| 1273 | if (MD->getOverloadedOperator() == OO_Call && |
| 1274 | MD->getParent()->isLambda()) |
| 1275 | return getNonClosureContext(MD->getParent()->getParent()); |
| 1276 | return MD; |
| 1277 | } |
| 1278 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1279 | return FD; |
| 1280 | if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1281 | return MD; |
| 1282 | if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 1283 | return getNonClosureContext(BD->getParent()); |
| 1284 | if (auto *CD = dyn_cast<CapturedDecl>(D)) |
| 1285 | return getNonClosureContext(CD->getParent()); |
| 1286 | if (auto *OFD = dyn_cast<OutlinedFunctionDecl>(D)) |
| 1287 | return getNonClosureContext(OFD->getParent()); |
| 1288 | return nullptr; |
| 1289 | } |
| 1290 | |
| 1291 | Decl *Decl::getNonClosureContext() { |
| 1292 | return ::getNonClosureContext(D: this); |
| 1293 | } |
| 1294 | |
| 1295 | Decl *DeclContext::getNonClosureAncestor() { |
| 1296 | return ::getNonClosureContext(D: this); |
| 1297 | } |
| 1298 | |
| 1299 | //===----------------------------------------------------------------------===// |
| 1300 | // DeclContext Implementation |
| 1301 | //===----------------------------------------------------------------------===// |
| 1302 | |
| 1303 | DeclContext::DeclContext(Decl::Kind K) { |
| 1304 | DeclContextBits.DeclKind = K; |
| 1305 | setHasExternalLexicalStorage(false); |
| 1306 | setHasExternalVisibleStorage(false); |
| 1307 | setNeedToReconcileExternalVisibleStorage(false); |
| 1308 | setHasLazyLocalLexicalLookups(false); |
| 1309 | setHasLazyExternalLexicalLookups(false); |
| 1310 | setUseQualifiedLookup(false); |
| 1311 | } |
| 1312 | |
| 1313 | bool DeclContext::classof(const Decl *D) { |
| 1314 | Decl::Kind DK = D->getKind(); |
| 1315 | switch (DK) { |
| 1316 | #define DECL(NAME, BASE) |
| 1317 | #define DECL_CONTEXT(NAME) case Decl::NAME: |
| 1318 | #include "clang/AST/DeclNodes.inc" |
| 1319 | return true; |
| 1320 | default: |
| 1321 | return false; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | DeclContext::~DeclContext() = default; |
| 1326 | |
| 1327 | /// Find the parent context of this context that will be |
| 1328 | /// used for unqualified name lookup. |
| 1329 | /// |
| 1330 | /// Generally, the parent lookup context is the semantic context. However, for |
| 1331 | /// a friend function the parent lookup context is the lexical context, which |
| 1332 | /// is the class in which the friend is declared. |
| 1333 | DeclContext *DeclContext::getLookupParent() { |
| 1334 | // FIXME: Find a better way to identify friends. |
| 1335 | if (isa<FunctionDecl>(Val: this)) |
| 1336 | if (getParent()->getRedeclContext()->isFileContext() && |
| 1337 | getLexicalParent()->getRedeclContext()->isRecord()) |
| 1338 | return getLexicalParent(); |
| 1339 | |
| 1340 | // A lookup within the call operator of a lambda never looks in the lambda |
| 1341 | // class; instead, skip to the context in which that closure type is |
| 1342 | // declared. |
| 1343 | if (isLambdaCallOperator(DC: this)) |
| 1344 | return getParent()->getParent(); |
| 1345 | |
| 1346 | return getParent(); |
| 1347 | } |
| 1348 | |
| 1349 | const BlockDecl *DeclContext::getInnermostBlockDecl() const { |
| 1350 | const DeclContext *Ctx = this; |
| 1351 | |
| 1352 | do { |
| 1353 | if (Ctx->isClosure()) |
| 1354 | return cast<BlockDecl>(Val: Ctx); |
| 1355 | Ctx = Ctx->getParent(); |
| 1356 | } while (Ctx); |
| 1357 | |
| 1358 | return nullptr; |
| 1359 | } |
| 1360 | |
| 1361 | bool DeclContext::isInlineNamespace() const { |
| 1362 | return isNamespace() && |
| 1363 | cast<NamespaceDecl>(Val: this)->isInline(); |
| 1364 | } |
| 1365 | |
| 1366 | bool DeclContext::isStdNamespace() const { |
| 1367 | if (!isNamespace()) |
| 1368 | return false; |
| 1369 | |
| 1370 | const auto *ND = cast<NamespaceDecl>(Val: this); |
| 1371 | if (ND->isInline()) { |
| 1372 | return ND->getParent()->isStdNamespace(); |
| 1373 | } |
| 1374 | |
| 1375 | if (!getParent()->getRedeclContext()->isTranslationUnit()) |
| 1376 | return false; |
| 1377 | |
| 1378 | const IdentifierInfo *II = ND->getIdentifier(); |
| 1379 | return II && II->isStr(Str: "std" ); |
| 1380 | } |
| 1381 | |
| 1382 | bool DeclContext::isDependentContext() const { |
| 1383 | if (isFileContext()) |
| 1384 | return false; |
| 1385 | |
| 1386 | if (isa<ClassTemplatePartialSpecializationDecl>(Val: this)) |
| 1387 | return true; |
| 1388 | |
| 1389 | if (const auto *Record = dyn_cast<CXXRecordDecl>(Val: this)) { |
| 1390 | if (Record->getDescribedClassTemplate()) |
| 1391 | return true; |
| 1392 | |
| 1393 | if (Record->isDependentLambda()) |
| 1394 | return true; |
| 1395 | if (Record->isNeverDependentLambda()) |
| 1396 | return false; |
| 1397 | } |
| 1398 | |
| 1399 | if (const auto *Function = dyn_cast<FunctionDecl>(Val: this)) { |
| 1400 | if (Function->getDescribedFunctionTemplate()) |
| 1401 | return true; |
| 1402 | |
| 1403 | // Friend function declarations are dependent if their *lexical* |
| 1404 | // context is dependent. |
| 1405 | if (cast<Decl>(Val: this)->getFriendObjectKind()) |
| 1406 | return getLexicalParent()->isDependentContext(); |
| 1407 | } |
| 1408 | |
| 1409 | // FIXME: A variable template is a dependent context, but is not a |
| 1410 | // DeclContext. A context within it (such as a lambda-expression) |
| 1411 | // should be considered dependent. |
| 1412 | |
| 1413 | return getParent() && getParent()->isDependentContext(); |
| 1414 | } |
| 1415 | |
| 1416 | bool DeclContext::isTransparentContext() const { |
| 1417 | if (getDeclKind() == Decl::Enum) |
| 1418 | return !cast<EnumDecl>(Val: this)->isScoped(); |
| 1419 | |
| 1420 | return isa<LinkageSpecDecl, ExportDecl, HLSLBufferDecl>(Val: this); |
| 1421 | } |
| 1422 | |
| 1423 | static bool isLinkageSpecContext(const DeclContext *DC, |
| 1424 | LinkageSpecLanguageIDs ID) { |
| 1425 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| 1426 | if (DC->getDeclKind() == Decl::LinkageSpec) |
| 1427 | return cast<LinkageSpecDecl>(Val: DC)->getLanguage() == ID; |
| 1428 | DC = DC->getLexicalParent(); |
| 1429 | } |
| 1430 | return false; |
| 1431 | } |
| 1432 | |
| 1433 | bool DeclContext::isExternCContext() const { |
| 1434 | return isLinkageSpecContext(DC: this, ID: LinkageSpecLanguageIDs::C); |
| 1435 | } |
| 1436 | |
| 1437 | const LinkageSpecDecl *DeclContext::getExternCContext() const { |
| 1438 | const DeclContext *DC = this; |
| 1439 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| 1440 | if (DC->getDeclKind() == Decl::LinkageSpec && |
| 1441 | cast<LinkageSpecDecl>(Val: DC)->getLanguage() == LinkageSpecLanguageIDs::C) |
| 1442 | return cast<LinkageSpecDecl>(Val: DC); |
| 1443 | DC = DC->getLexicalParent(); |
| 1444 | } |
| 1445 | return nullptr; |
| 1446 | } |
| 1447 | |
| 1448 | bool DeclContext::isExternCXXContext() const { |
| 1449 | return isLinkageSpecContext(DC: this, ID: LinkageSpecLanguageIDs::CXX); |
| 1450 | } |
| 1451 | |
| 1452 | bool DeclContext::Encloses(const DeclContext *DC) const { |
| 1453 | if (getPrimaryContext() != this) |
| 1454 | return getPrimaryContext()->Encloses(DC); |
| 1455 | |
| 1456 | for (; DC; DC = DC->getParent()) |
| 1457 | if (!isa<LinkageSpecDecl, ExportDecl>(Val: DC) && |
| 1458 | DC->getPrimaryContext() == this) |
| 1459 | return true; |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
| 1463 | bool DeclContext::LexicallyEncloses(const DeclContext *DC) const { |
| 1464 | if (getPrimaryContext() != this) |
| 1465 | return getPrimaryContext()->LexicallyEncloses(DC); |
| 1466 | |
| 1467 | for (; DC; DC = DC->getLexicalParent()) |
| 1468 | if (!isa<LinkageSpecDecl, ExportDecl>(Val: DC) && |
| 1469 | DC->getPrimaryContext() == this) |
| 1470 | return true; |
| 1471 | return false; |
| 1472 | } |
| 1473 | |
| 1474 | DeclContext *DeclContext::getNonTransparentContext() { |
| 1475 | DeclContext *DC = this; |
| 1476 | while (DC->isTransparentContext()) { |
| 1477 | DC = DC->getParent(); |
| 1478 | assert(DC && "All transparent contexts should have a parent!" ); |
| 1479 | } |
| 1480 | return DC; |
| 1481 | } |
| 1482 | |
| 1483 | DeclContext *DeclContext::getPrimaryContext() { |
| 1484 | switch (getDeclKind()) { |
| 1485 | case Decl::ExternCContext: |
| 1486 | case Decl::LinkageSpec: |
| 1487 | case Decl::Export: |
| 1488 | case Decl::TopLevelStmt: |
| 1489 | case Decl::Block: |
| 1490 | case Decl::Captured: |
| 1491 | case Decl::OutlinedFunction: |
| 1492 | case Decl::OMPDeclareReduction: |
| 1493 | case Decl::OMPDeclareMapper: |
| 1494 | case Decl::RequiresExprBody: |
| 1495 | // There is only one DeclContext for these entities. |
| 1496 | return this; |
| 1497 | |
| 1498 | case Decl::HLSLBuffer: |
| 1499 | // Each buffer, even with the same name, is a distinct construct. |
| 1500 | // Multiple buffers with the same name are allowed for backward |
| 1501 | // compatibility. |
| 1502 | // As long as buffers have unique resource bindings the names don't matter. |
| 1503 | // The names get exposed via the CPU-side reflection API which |
| 1504 | // supports querying bindings, so we cannot remove them. |
| 1505 | return this; |
| 1506 | |
| 1507 | case Decl::TranslationUnit: |
| 1508 | return static_cast<TranslationUnitDecl *>(this)->getFirstDecl(); |
| 1509 | case Decl::Namespace: |
| 1510 | return static_cast<NamespaceDecl *>(this)->getFirstDecl(); |
| 1511 | |
| 1512 | case Decl::ObjCMethod: |
| 1513 | return this; |
| 1514 | |
| 1515 | case Decl::ObjCInterface: |
| 1516 | if (auto *OID = dyn_cast<ObjCInterfaceDecl>(Val: this)) |
| 1517 | if (auto *Def = OID->getDefinition()) |
| 1518 | return Def; |
| 1519 | return this; |
| 1520 | |
| 1521 | case Decl::ObjCProtocol: |
| 1522 | if (auto *OPD = dyn_cast<ObjCProtocolDecl>(Val: this)) |
| 1523 | if (auto *Def = OPD->getDefinition()) |
| 1524 | return Def; |
| 1525 | return this; |
| 1526 | |
| 1527 | case Decl::ObjCCategory: |
| 1528 | return this; |
| 1529 | |
| 1530 | case Decl::ObjCImplementation: |
| 1531 | case Decl::ObjCCategoryImpl: |
| 1532 | return this; |
| 1533 | |
| 1534 | // If this is a tag type that has a definition or is currently |
| 1535 | // being defined, that definition is our primary context. |
| 1536 | case Decl::ClassTemplatePartialSpecialization: |
| 1537 | case Decl::ClassTemplateSpecialization: |
| 1538 | case Decl::CXXRecord: |
| 1539 | return cast<CXXRecordDecl>(Val: this)->getDefinitionOrSelf(); |
| 1540 | case Decl::Record: |
| 1541 | case Decl::Enum: |
| 1542 | return cast<TagDecl>(Val: this)->getDefinitionOrSelf(); |
| 1543 | |
| 1544 | default: |
| 1545 | assert(getDeclKind() >= Decl::firstFunction && |
| 1546 | getDeclKind() <= Decl::lastFunction && "Unknown DeclContext kind" ); |
| 1547 | return this; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | template <typename T> |
| 1552 | static void collectAllContextsImpl(T *Self, |
| 1553 | SmallVectorImpl<DeclContext *> &Contexts) { |
| 1554 | for (T *D = Self->getMostRecentDecl(); D; D = D->getPreviousDecl()) |
| 1555 | Contexts.push_back(Elt: D); |
| 1556 | |
| 1557 | std::reverse(first: Contexts.begin(), last: Contexts.end()); |
| 1558 | } |
| 1559 | |
| 1560 | void DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts) { |
| 1561 | Contexts.clear(); |
| 1562 | |
| 1563 | Decl::Kind Kind = getDeclKind(); |
| 1564 | |
| 1565 | if (Kind == Decl::TranslationUnit) |
| 1566 | collectAllContextsImpl(Self: static_cast<TranslationUnitDecl *>(this), Contexts); |
| 1567 | else if (Kind == Decl::Namespace) |
| 1568 | collectAllContextsImpl(Self: static_cast<NamespaceDecl *>(this), Contexts); |
| 1569 | else |
| 1570 | Contexts.push_back(Elt: this); |
| 1571 | } |
| 1572 | |
| 1573 | std::pair<Decl *, Decl *> |
| 1574 | DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls, |
| 1575 | bool FieldsAlreadyLoaded) { |
| 1576 | // Build up a chain of declarations via the Decl::NextInContextAndBits field. |
| 1577 | Decl *FirstNewDecl = nullptr; |
| 1578 | Decl *PrevDecl = nullptr; |
| 1579 | for (auto *D : Decls) { |
| 1580 | if (FieldsAlreadyLoaded && isa<FieldDecl>(Val: D)) |
| 1581 | continue; |
| 1582 | |
| 1583 | if (PrevDecl) |
| 1584 | PrevDecl->NextInContextAndBits.setPointer(D); |
| 1585 | else |
| 1586 | FirstNewDecl = D; |
| 1587 | |
| 1588 | PrevDecl = D; |
| 1589 | } |
| 1590 | |
| 1591 | return std::make_pair(x&: FirstNewDecl, y&: PrevDecl); |
| 1592 | } |
| 1593 | |
| 1594 | /// We have just acquired external visible storage, and we already have |
| 1595 | /// built a lookup map. For every name in the map, pull in the new names from |
| 1596 | /// the external storage. |
| 1597 | void DeclContext::reconcileExternalVisibleStorage() const { |
| 1598 | assert(hasNeedToReconcileExternalVisibleStorage() && LookupPtr); |
| 1599 | setNeedToReconcileExternalVisibleStorage(false); |
| 1600 | |
| 1601 | for (auto &Lookup : *LookupPtr) |
| 1602 | Lookup.second.setHasExternalDecls(); |
| 1603 | } |
| 1604 | |
| 1605 | /// Load the declarations within this lexical storage from an |
| 1606 | /// external source. |
| 1607 | /// \return \c true if any declarations were added. |
| 1608 | bool |
| 1609 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 1610 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1611 | assert(hasExternalLexicalStorage() && Source && "No external storage?" ); |
| 1612 | |
| 1613 | // Notify that we have a DeclContext that is initializing. |
| 1614 | ExternalASTSource::Deserializing ADeclContext(Source); |
| 1615 | |
| 1616 | // Load the external declarations, if any. |
| 1617 | SmallVector<Decl*, 64> Decls; |
| 1618 | setHasExternalLexicalStorage(false); |
| 1619 | Source->FindExternalLexicalDecls(DC: this, Result&: Decls); |
| 1620 | |
| 1621 | if (Decls.empty()) |
| 1622 | return false; |
| 1623 | |
| 1624 | // We may have already loaded just the fields of this record, in which case |
| 1625 | // we need to ignore them. |
| 1626 | bool FieldsAlreadyLoaded = false; |
| 1627 | if (const auto *RD = dyn_cast<RecordDecl>(Val: this)) |
| 1628 | FieldsAlreadyLoaded = RD->hasLoadedFieldsFromExternalStorage(); |
| 1629 | |
| 1630 | // Splice the newly-read declarations into the beginning of the list |
| 1631 | // of declarations. |
| 1632 | Decl *ExternalFirst, *ExternalLast; |
| 1633 | std::tie(args&: ExternalFirst, args&: ExternalLast) = |
| 1634 | BuildDeclChain(Decls, FieldsAlreadyLoaded); |
| 1635 | ExternalLast->NextInContextAndBits.setPointer(FirstDecl); |
| 1636 | FirstDecl = ExternalFirst; |
| 1637 | if (!LastDecl) |
| 1638 | LastDecl = ExternalLast; |
| 1639 | return true; |
| 1640 | } |
| 1641 | |
| 1642 | DeclContext::lookup_result |
| 1643 | ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 1644 | DeclarationName Name) { |
| 1645 | ASTContext &Context = DC->getParentASTContext(); |
| 1646 | StoredDeclsMap *Map; |
| 1647 | if (!(Map = DC->LookupPtr)) |
| 1648 | Map = DC->CreateStoredDeclsMap(C&: Context); |
| 1649 | if (DC->hasNeedToReconcileExternalVisibleStorage()) |
| 1650 | DC->reconcileExternalVisibleStorage(); |
| 1651 | |
| 1652 | (*Map)[Name].removeExternalDecls(); |
| 1653 | |
| 1654 | return DeclContext::lookup_result(); |
| 1655 | } |
| 1656 | |
| 1657 | DeclContext::lookup_result |
| 1658 | ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC, |
| 1659 | DeclarationName Name, |
| 1660 | ArrayRef<NamedDecl*> Decls) { |
| 1661 | ASTContext &Context = DC->getParentASTContext(); |
| 1662 | StoredDeclsMap *Map; |
| 1663 | if (!(Map = DC->LookupPtr)) |
| 1664 | Map = DC->CreateStoredDeclsMap(C&: Context); |
| 1665 | if (DC->hasNeedToReconcileExternalVisibleStorage()) |
| 1666 | DC->reconcileExternalVisibleStorage(); |
| 1667 | |
| 1668 | StoredDeclsList &List = (*Map)[Name]; |
| 1669 | List.replaceExternalDecls(Decls); |
| 1670 | return List.getLookupResult(); |
| 1671 | } |
| 1672 | |
| 1673 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
| 1674 | if (hasExternalLexicalStorage()) |
| 1675 | LoadLexicalDeclsFromExternalStorage(); |
| 1676 | return decl_iterator(FirstDecl); |
| 1677 | } |
| 1678 | |
| 1679 | bool DeclContext::decls_empty() const { |
| 1680 | if (hasExternalLexicalStorage()) |
| 1681 | LoadLexicalDeclsFromExternalStorage(); |
| 1682 | |
| 1683 | return !FirstDecl; |
| 1684 | } |
| 1685 | |
| 1686 | bool DeclContext::containsDecl(Decl *D) const { |
| 1687 | return (D->getLexicalDeclContext() == this && |
| 1688 | (D->NextInContextAndBits.getPointer() || D == LastDecl)); |
| 1689 | } |
| 1690 | |
| 1691 | bool DeclContext::containsDeclAndLoad(Decl *D) const { |
| 1692 | if (hasExternalLexicalStorage()) |
| 1693 | LoadLexicalDeclsFromExternalStorage(); |
| 1694 | return containsDecl(D); |
| 1695 | } |
| 1696 | |
| 1697 | /// shouldBeHidden - Determine whether a declaration which was declared |
| 1698 | /// within its semantic context should be invisible to qualified name lookup. |
| 1699 | static bool shouldBeHidden(NamedDecl *D) { |
| 1700 | // Skip unnamed declarations. |
| 1701 | if (!D->getDeclName()) |
| 1702 | return true; |
| 1703 | |
| 1704 | // Skip entities that can't be found by name lookup into a particular |
| 1705 | // context. |
| 1706 | if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(Val: D)) || |
| 1707 | D->isTemplateParameter()) |
| 1708 | return true; |
| 1709 | |
| 1710 | // Skip friends and local extern declarations unless they're the first |
| 1711 | // declaration of the entity. |
| 1712 | if ((D->isLocalExternDecl() || D->getFriendObjectKind()) && |
| 1713 | D != D->getCanonicalDecl()) |
| 1714 | return true; |
| 1715 | |
| 1716 | // Skip template specializations. |
| 1717 | // FIXME: This feels like a hack. Should DeclarationName support |
| 1718 | // template-ids, or is there a better way to keep specializations |
| 1719 | // from being visible? |
| 1720 | if (isa<ClassTemplateSpecializationDecl>(Val: D)) |
| 1721 | return true; |
| 1722 | if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) |
| 1723 | if (FD->isFunctionTemplateSpecialization()) |
| 1724 | return true; |
| 1725 | |
| 1726 | // Hide destructors that are invalid. There should always be one destructor, |
| 1727 | // but if it is an invalid decl, another one is created. We need to hide the |
| 1728 | // invalid one from places that expect exactly one destructor, like the |
| 1729 | // serialization code. |
| 1730 | if (isa<CXXDestructorDecl>(Val: D) && D->isInvalidDecl()) |
| 1731 | return true; |
| 1732 | |
| 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | void DeclContext::removeDecl(Decl *D) { |
| 1737 | assert(D->getLexicalDeclContext() == this && |
| 1738 | "decl being removed from non-lexical context" ); |
| 1739 | assert((D->NextInContextAndBits.getPointer() || D == LastDecl) && |
| 1740 | "decl is not in decls list" ); |
| 1741 | |
| 1742 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 1743 | if (D == FirstDecl) { |
| 1744 | if (D == LastDecl) |
| 1745 | FirstDecl = LastDecl = nullptr; |
| 1746 | else |
| 1747 | FirstDecl = D->NextInContextAndBits.getPointer(); |
| 1748 | } else { |
| 1749 | for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) { |
| 1750 | assert(I && "decl not found in linked list" ); |
| 1751 | if (I->NextInContextAndBits.getPointer() == D) { |
| 1752 | I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer()); |
| 1753 | if (D == LastDecl) LastDecl = I; |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | // Mark that D is no longer in the decl chain. |
| 1760 | D->NextInContextAndBits.setPointer(nullptr); |
| 1761 | |
| 1762 | // Remove D from the lookup table if necessary. |
| 1763 | if (isa<NamedDecl>(Val: D)) { |
| 1764 | auto *ND = cast<NamedDecl>(Val: D); |
| 1765 | |
| 1766 | // Do not try to remove the declaration if that is invisible to qualified |
| 1767 | // lookup. E.g. template specializations are skipped. |
| 1768 | if (shouldBeHidden(D: ND)) |
| 1769 | return; |
| 1770 | |
| 1771 | // Remove only decls that have a name |
| 1772 | if (!ND->getDeclName()) |
| 1773 | return; |
| 1774 | |
| 1775 | auto *DC = D->getDeclContext(); |
| 1776 | do { |
| 1777 | StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr; |
| 1778 | if (Map) { |
| 1779 | StoredDeclsMap::iterator Pos = Map->find(Val: ND->getDeclName()); |
| 1780 | assert(Pos != Map->end() && "no lookup entry for decl" ); |
| 1781 | StoredDeclsList &List = Pos->second; |
| 1782 | List.remove(D: ND); |
| 1783 | // Clean up the entry if there are no more decls. |
| 1784 | if (List.isNull()) |
| 1785 | Map->erase(I: Pos); |
| 1786 | } |
| 1787 | } while (DC->isTransparentContext() && (DC = DC->getParent())); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | void DeclContext::addHiddenDecl(Decl *D) { |
| 1792 | assert(D->getLexicalDeclContext() == this && |
| 1793 | "Decl inserted into wrong lexical context" ); |
| 1794 | assert(!D->getNextDeclInContext() && D != LastDecl && |
| 1795 | "Decl already inserted into a DeclContext" ); |
| 1796 | |
| 1797 | if (FirstDecl) { |
| 1798 | LastDecl->NextInContextAndBits.setPointer(D); |
| 1799 | LastDecl = D; |
| 1800 | } else { |
| 1801 | FirstDecl = LastDecl = D; |
| 1802 | } |
| 1803 | |
| 1804 | // Notify a C++ record declaration that we've added a member, so it can |
| 1805 | // update its class-specific state. |
| 1806 | if (auto *Record = dyn_cast<CXXRecordDecl>(Val: this)) |
| 1807 | Record->addedMember(D); |
| 1808 | |
| 1809 | // If this is a newly-created (not de-serialized) import declaration, wire |
| 1810 | // it in to the list of local import declarations. |
| 1811 | if (!D->isFromASTFile()) { |
| 1812 | if (auto *Import = dyn_cast<ImportDecl>(Val: D)) |
| 1813 | D->getASTContext().addedLocalImportDecl(Import); |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | void DeclContext::addDecl(Decl *D) { |
| 1818 | addHiddenDecl(D); |
| 1819 | |
| 1820 | if (auto *ND = dyn_cast<NamedDecl>(Val: D)) |
| 1821 | ND->getDeclContext()->getPrimaryContext()-> |
| 1822 | makeDeclVisibleInContextWithFlags(D: ND, Internal: false, Rediscoverable: true); |
| 1823 | } |
| 1824 | |
| 1825 | void DeclContext::addDeclInternal(Decl *D) { |
| 1826 | addHiddenDecl(D); |
| 1827 | |
| 1828 | if (auto *ND = dyn_cast<NamedDecl>(Val: D)) |
| 1829 | ND->getDeclContext()->getPrimaryContext()-> |
| 1830 | makeDeclVisibleInContextWithFlags(D: ND, Internal: true, Rediscoverable: true); |
| 1831 | } |
| 1832 | |
| 1833 | /// buildLookup - Build the lookup data structure with all of the |
| 1834 | /// declarations in this DeclContext (and any other contexts linked |
| 1835 | /// to it or transparent contexts nested within it) and return it. |
| 1836 | /// |
| 1837 | /// Note that the produced map may miss out declarations from an |
| 1838 | /// external source. If it does, those entries will be marked with |
| 1839 | /// the 'hasExternalDecls' flag. |
| 1840 | StoredDeclsMap *DeclContext::buildLookup() { |
| 1841 | assert(this == getPrimaryContext() && "buildLookup called on non-primary DC" ); |
| 1842 | |
| 1843 | if (!hasLazyLocalLexicalLookups() && |
| 1844 | !hasLazyExternalLexicalLookups()) |
| 1845 | return LookupPtr; |
| 1846 | |
| 1847 | SmallVector<DeclContext *, 2> Contexts; |
| 1848 | collectAllContexts(Contexts); |
| 1849 | |
| 1850 | if (hasLazyExternalLexicalLookups()) { |
| 1851 | setHasLazyExternalLexicalLookups(false); |
| 1852 | for (auto *DC : Contexts) { |
| 1853 | if (DC->hasExternalLexicalStorage()) { |
| 1854 | bool LoadedDecls = DC->LoadLexicalDeclsFromExternalStorage(); |
| 1855 | setHasLazyLocalLexicalLookups( |
| 1856 | hasLazyLocalLexicalLookups() | LoadedDecls ); |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | if (!hasLazyLocalLexicalLookups()) |
| 1861 | return LookupPtr; |
| 1862 | } |
| 1863 | |
| 1864 | for (auto *DC : Contexts) |
| 1865 | buildLookupImpl(DCtx: DC, Internal: hasExternalVisibleStorage()); |
| 1866 | |
| 1867 | // We no longer have any lazy decls. |
| 1868 | setHasLazyLocalLexicalLookups(false); |
| 1869 | return LookupPtr; |
| 1870 | } |
| 1871 | |
| 1872 | /// buildLookupImpl - Build part of the lookup data structure for the |
| 1873 | /// declarations contained within DCtx, which will either be this |
| 1874 | /// DeclContext, a DeclContext linked to it, or a transparent context |
| 1875 | /// nested within it. |
| 1876 | void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) { |
| 1877 | for (auto *D : DCtx->noload_decls()) { |
| 1878 | // Insert this declaration into the lookup structure, but only if |
| 1879 | // it's semantically within its decl context. Any other decls which |
| 1880 | // should be found in this context are added eagerly. |
| 1881 | // |
| 1882 | // If it's from an AST file, don't add it now. It'll get handled by |
| 1883 | // FindExternalVisibleDeclsByName if needed. Exception: if we're not |
| 1884 | // in C++, we do not track external visible decls for the TU, so in |
| 1885 | // that case we need to collect them all here. |
| 1886 | if (auto *ND = dyn_cast<NamedDecl>(Val: D)) |
| 1887 | if (ND->getDeclContext() == DCtx && !shouldBeHidden(D: ND) && |
| 1888 | (!ND->isFromASTFile() || |
| 1889 | (isTranslationUnit() && |
| 1890 | !getParentASTContext().getLangOpts().CPlusPlus))) |
| 1891 | makeDeclVisibleInContextImpl(D: ND, Internal); |
| 1892 | |
| 1893 | // If this declaration is itself a transparent declaration context |
| 1894 | // or inline namespace, add the members of this declaration of that |
| 1895 | // context (recursively). |
| 1896 | if (auto *InnerCtx = dyn_cast<DeclContext>(Val: D)) |
| 1897 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
| 1898 | buildLookupImpl(DCtx: InnerCtx, Internal); |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | DeclContext::lookup_result |
| 1903 | DeclContext::lookup(DeclarationName Name) const { |
| 1904 | // For transparent DeclContext, we should lookup in their enclosing context. |
| 1905 | if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export) |
| 1906 | return getParent()->lookup(Name); |
| 1907 | |
| 1908 | return getPrimaryContext()->lookupImpl(Name, OriginalLookupDC: this); |
| 1909 | } |
| 1910 | |
| 1911 | DeclContext::lookup_result |
| 1912 | DeclContext::lookupImpl(DeclarationName Name, |
| 1913 | const DeclContext *OriginalLookupDC) const { |
| 1914 | assert(this == getPrimaryContext() && |
| 1915 | "lookupImpl should only be called with primary DC!" ); |
| 1916 | assert(getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && |
| 1917 | "We shouldn't lookup in transparent DC." ); |
| 1918 | |
| 1919 | // If we have an external source, ensure that any later redeclarations of this |
| 1920 | // context have been loaded, since they may add names to the result of this |
| 1921 | // lookup (or add external visible storage). |
| 1922 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1923 | if (Source) |
| 1924 | (void)cast<Decl>(Val: this)->getMostRecentDecl(); |
| 1925 | |
| 1926 | if (hasExternalVisibleStorage()) { |
| 1927 | assert(Source && "external visible storage but no external source?" ); |
| 1928 | |
| 1929 | if (hasNeedToReconcileExternalVisibleStorage()) |
| 1930 | reconcileExternalVisibleStorage(); |
| 1931 | |
| 1932 | StoredDeclsMap *Map = LookupPtr; |
| 1933 | |
| 1934 | if (hasLazyLocalLexicalLookups() || |
| 1935 | hasLazyExternalLexicalLookups()) |
| 1936 | // FIXME: Make buildLookup const? |
| 1937 | Map = const_cast<DeclContext*>(this)->buildLookup(); |
| 1938 | |
| 1939 | if (!Map) |
| 1940 | Map = CreateStoredDeclsMap(C&: getParentASTContext()); |
| 1941 | |
| 1942 | // If we have a lookup result with no external decls, we are done. |
| 1943 | std::pair<StoredDeclsMap::iterator, bool> R = Map->try_emplace(Key: Name); |
| 1944 | if (!R.second && !R.first->second.hasExternalDecls()) |
| 1945 | return R.first->second.getLookupResult(); |
| 1946 | |
| 1947 | if (Source->FindExternalVisibleDeclsByName(DC: this, Name, OriginalDC: OriginalLookupDC) || |
| 1948 | !R.second) { |
| 1949 | if (StoredDeclsMap *Map = LookupPtr) { |
| 1950 | StoredDeclsMap::iterator I = Map->find(Val: Name); |
| 1951 | if (I != Map->end()) |
| 1952 | return I->second.getLookupResult(); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | return {}; |
| 1957 | } |
| 1958 | |
| 1959 | StoredDeclsMap *Map = LookupPtr; |
| 1960 | if (hasLazyLocalLexicalLookups() || |
| 1961 | hasLazyExternalLexicalLookups()) |
| 1962 | Map = const_cast<DeclContext*>(this)->buildLookup(); |
| 1963 | |
| 1964 | if (!Map) |
| 1965 | return {}; |
| 1966 | |
| 1967 | StoredDeclsMap::iterator I = Map->find(Val: Name); |
| 1968 | if (I == Map->end()) |
| 1969 | return {}; |
| 1970 | |
| 1971 | return I->second.getLookupResult(); |
| 1972 | } |
| 1973 | |
| 1974 | DeclContext::lookup_result |
| 1975 | DeclContext::noload_lookup(DeclarationName Name) { |
| 1976 | // For transparent DeclContext, we should lookup in their enclosing context. |
| 1977 | if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export) |
| 1978 | return getParent()->noload_lookup(Name); |
| 1979 | |
| 1980 | DeclContext *PrimaryContext = getPrimaryContext(); |
| 1981 | if (PrimaryContext != this) |
| 1982 | return PrimaryContext->noload_lookup(Name); |
| 1983 | |
| 1984 | loadLazyLocalLexicalLookups(); |
| 1985 | StoredDeclsMap *Map = LookupPtr; |
| 1986 | if (!Map) |
| 1987 | return {}; |
| 1988 | |
| 1989 | StoredDeclsMap::iterator I = Map->find(Val: Name); |
| 1990 | return I != Map->end() ? I->second.getLookupResult() |
| 1991 | : lookup_result(); |
| 1992 | } |
| 1993 | |
| 1994 | // If we have any lazy lexical declarations not in our lookup map, add them |
| 1995 | // now. Don't import any external declarations, not even if we know we have |
| 1996 | // some missing from the external visible lookups. |
| 1997 | void DeclContext::loadLazyLocalLexicalLookups() { |
| 1998 | if (hasLazyLocalLexicalLookups()) { |
| 1999 | SmallVector<DeclContext *, 2> Contexts; |
| 2000 | collectAllContexts(Contexts); |
| 2001 | for (auto *Context : Contexts) |
| 2002 | buildLookupImpl(DCtx: Context, Internal: hasExternalVisibleStorage()); |
| 2003 | setHasLazyLocalLexicalLookups(false); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | void DeclContext::localUncachedLookup(DeclarationName Name, |
| 2008 | SmallVectorImpl<NamedDecl *> &Results) { |
| 2009 | Results.clear(); |
| 2010 | |
| 2011 | // If there's no external storage, just perform a normal lookup and copy |
| 2012 | // the results. |
| 2013 | if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) { |
| 2014 | lookup_result LookupResults = lookup(Name); |
| 2015 | llvm::append_range(C&: Results, R&: LookupResults); |
| 2016 | if (!Results.empty()) |
| 2017 | return; |
| 2018 | } |
| 2019 | |
| 2020 | // If we have a lookup table, check there first. Maybe we'll get lucky. |
| 2021 | // FIXME: Should we be checking these flags on the primary context? |
| 2022 | if (Name && !hasLazyLocalLexicalLookups() && |
| 2023 | !hasLazyExternalLexicalLookups()) { |
| 2024 | if (StoredDeclsMap *Map = LookupPtr) { |
| 2025 | StoredDeclsMap::iterator Pos = Map->find(Val: Name); |
| 2026 | if (Pos != Map->end()) { |
| 2027 | Results.insert(I: Results.end(), |
| 2028 | From: Pos->second.getLookupResult().begin(), |
| 2029 | To: Pos->second.getLookupResult().end()); |
| 2030 | return; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | // Slow case: grovel through the declarations in our chain looking for |
| 2036 | // matches. |
| 2037 | // FIXME: If we have lazy external declarations, this will not find them! |
| 2038 | // FIXME: Should we CollectAllContexts and walk them all here? |
| 2039 | for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) { |
| 2040 | if (auto *ND = dyn_cast<NamedDecl>(Val: D)) |
| 2041 | if (ND->getDeclName() == Name) |
| 2042 | Results.push_back(Elt: ND); |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | DeclContext *DeclContext::getRedeclContext() { |
| 2047 | DeclContext *Ctx = this; |
| 2048 | |
| 2049 | // In C, a record type is the redeclaration context for its fields only. If |
| 2050 | // we arrive at a record context after skipping anything else, we should skip |
| 2051 | // the record as well. Currently, this means skipping enumerations because |
| 2052 | // they're the only transparent context that can exist within a struct or |
| 2053 | // union. |
| 2054 | bool SkipRecords = getDeclKind() == Decl::Kind::Enum && |
| 2055 | !getParentASTContext().getLangOpts().CPlusPlus; |
| 2056 | |
| 2057 | // Skip through contexts to get to the redeclaration context. Transparent |
| 2058 | // contexts are always skipped. |
| 2059 | while ((SkipRecords && Ctx->isRecord()) || Ctx->isTransparentContext()) |
| 2060 | Ctx = Ctx->getParent(); |
| 2061 | return Ctx; |
| 2062 | } |
| 2063 | |
| 2064 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 2065 | DeclContext *Ctx = this; |
| 2066 | // Skip through non-namespace, non-translation-unit contexts. |
| 2067 | while (!Ctx->isFileContext()) |
| 2068 | Ctx = Ctx->getParent(); |
| 2069 | return Ctx->getPrimaryContext(); |
| 2070 | } |
| 2071 | |
| 2072 | RecordDecl *DeclContext::getOuterLexicalRecordContext() { |
| 2073 | // Loop until we find a non-record context. |
| 2074 | RecordDecl *OutermostRD = nullptr; |
| 2075 | DeclContext *DC = this; |
| 2076 | while (DC->isRecord()) { |
| 2077 | OutermostRD = cast<RecordDecl>(Val: DC); |
| 2078 | DC = DC->getLexicalParent(); |
| 2079 | } |
| 2080 | return OutermostRD; |
| 2081 | } |
| 2082 | |
| 2083 | bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const { |
| 2084 | // For non-file contexts, this is equivalent to Equals. |
| 2085 | if (!isFileContext()) |
| 2086 | return O->Equals(DC: this); |
| 2087 | |
| 2088 | do { |
| 2089 | if (O->Equals(DC: this)) |
| 2090 | return true; |
| 2091 | |
| 2092 | const auto *NS = dyn_cast<NamespaceDecl>(Val: O); |
| 2093 | if (!NS || !NS->isInline()) |
| 2094 | break; |
| 2095 | O = NS->getParent(); |
| 2096 | } while (O); |
| 2097 | |
| 2098 | return false; |
| 2099 | } |
| 2100 | |
| 2101 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
| 2102 | DeclContext *PrimaryDC = this->getPrimaryContext(); |
| 2103 | DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext(); |
| 2104 | // If the decl is being added outside of its semantic decl context, we |
| 2105 | // need to ensure that we eagerly build the lookup information for it. |
| 2106 | PrimaryDC->makeDeclVisibleInContextWithFlags(D, Internal: false, Rediscoverable: PrimaryDC == DeclDC); |
| 2107 | } |
| 2108 | |
| 2109 | void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, |
| 2110 | bool Recoverable) { |
| 2111 | assert(this == getPrimaryContext() && "expected a primary DC" ); |
| 2112 | |
| 2113 | if (!isLookupContext()) { |
| 2114 | if (isTransparentContext()) |
| 2115 | getParent()->getPrimaryContext() |
| 2116 | ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable); |
| 2117 | return; |
| 2118 | } |
| 2119 | |
| 2120 | // Skip declarations which should be invisible to name lookup. |
| 2121 | if (shouldBeHidden(D)) |
| 2122 | return; |
| 2123 | |
| 2124 | // If we already have a lookup data structure, perform the insertion into |
| 2125 | // it. If we might have externally-stored decls with this name, look them |
| 2126 | // up and perform the insertion. If this decl was declared outside its |
| 2127 | // semantic context, buildLookup won't add it, so add it now. |
| 2128 | // |
| 2129 | // FIXME: As a performance hack, don't add such decls into the translation |
| 2130 | // unit unless we're in C++, since qualified lookup into the TU is never |
| 2131 | // performed. |
| 2132 | if (LookupPtr || hasExternalVisibleStorage() || |
| 2133 | ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) && |
| 2134 | (getParentASTContext().getLangOpts().CPlusPlus || |
| 2135 | !isTranslationUnit()))) { |
| 2136 | // If we have lazily omitted any decls, they might have the same name as |
| 2137 | // the decl which we are adding, so build a full lookup table before adding |
| 2138 | // this decl. |
| 2139 | buildLookup(); |
| 2140 | makeDeclVisibleInContextImpl(D, Internal); |
| 2141 | } else { |
| 2142 | setHasLazyLocalLexicalLookups(true); |
| 2143 | } |
| 2144 | |
| 2145 | // If we are a transparent context or inline namespace, insert into our |
| 2146 | // parent context, too. This operation is recursive. |
| 2147 | if (isTransparentContext() || isInlineNamespace()) |
| 2148 | getParent()->getPrimaryContext()-> |
| 2149 | makeDeclVisibleInContextWithFlags(D, Internal, Recoverable); |
| 2150 | |
| 2151 | auto *DCAsDecl = cast<Decl>(Val: this); |
| 2152 | // Notify that a decl was made visible unless we are a Tag being defined. |
| 2153 | if (!(isa<TagDecl>(Val: DCAsDecl) && cast<TagDecl>(Val: DCAsDecl)->isBeingDefined())) |
| 2154 | if (ASTMutationListener *L = DCAsDecl->getASTMutationListener()) |
| 2155 | L->AddedVisibleDecl(DC: this, D); |
| 2156 | } |
| 2157 | |
| 2158 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) { |
| 2159 | // Find or create the stored declaration map. |
| 2160 | StoredDeclsMap *Map = LookupPtr; |
| 2161 | if (!Map) { |
| 2162 | ASTContext *C = &getParentASTContext(); |
| 2163 | Map = CreateStoredDeclsMap(C&: *C); |
| 2164 | } |
| 2165 | |
| 2166 | // If there is an external AST source, load any declarations it knows about |
| 2167 | // with this declaration's name. |
| 2168 | // If the lookup table contains an entry about this name it means that we |
| 2169 | // have already checked the external source. |
| 2170 | if (!Internal) |
| 2171 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 2172 | if (hasExternalVisibleStorage() && !Map->contains(Val: D->getDeclName())) |
| 2173 | Source->FindExternalVisibleDeclsByName(DC: this, Name: D->getDeclName(), |
| 2174 | OriginalDC: D->getDeclContext()); |
| 2175 | |
| 2176 | // Insert this declaration into the map. |
| 2177 | StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()]; |
| 2178 | |
| 2179 | if (Internal) { |
| 2180 | // If this is being added as part of loading an external declaration, |
| 2181 | // this may not be the only external declaration with this name. |
| 2182 | // In this case, we never try to replace an existing declaration; we'll |
| 2183 | // handle that when we finalize the list of declarations for this name. |
| 2184 | DeclNameEntries.setHasExternalDecls(); |
| 2185 | DeclNameEntries.prependDeclNoReplace(D); |
| 2186 | return; |
| 2187 | } |
| 2188 | |
| 2189 | DeclNameEntries.addOrReplaceDecl(D); |
| 2190 | } |
| 2191 | |
| 2192 | UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const { |
| 2193 | return cast<UsingDirectiveDecl>(Val: *I); |
| 2194 | } |
| 2195 | |
| 2196 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 2197 | /// this context. |
| 2198 | DeclContext::udir_range DeclContext::using_directives() const { |
| 2199 | // FIXME: Use something more efficient than normal lookup for using |
| 2200 | // directives. In C++, using directives are looked up more than anything else. |
| 2201 | lookup_result Result = lookup(Name: UsingDirectiveDecl::getName()); |
| 2202 | return udir_range(Result.begin(), Result.end()); |
| 2203 | } |
| 2204 | |
| 2205 | //===----------------------------------------------------------------------===// |
| 2206 | // Creation and Destruction of StoredDeclsMaps. // |
| 2207 | //===----------------------------------------------------------------------===// |
| 2208 | |
| 2209 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 2210 | assert(!LookupPtr && "context already has a decls map" ); |
| 2211 | assert(getPrimaryContext() == this && |
| 2212 | "creating decls map on non-primary context" ); |
| 2213 | |
| 2214 | StoredDeclsMap *M; |
| 2215 | bool Dependent = isDependentContext(); |
| 2216 | if (Dependent) |
| 2217 | M = new DependentStoredDeclsMap(); |
| 2218 | else |
| 2219 | M = new StoredDeclsMap(); |
| 2220 | M->Previous = C.LastSDM; |
| 2221 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 2222 | LookupPtr = M; |
| 2223 | return M; |
| 2224 | } |
| 2225 | |
| 2226 | void ASTContext::ReleaseDeclContextMaps() { |
| 2227 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 2228 | // pointer because the subclass doesn't add anything that needs to |
| 2229 | // be deleted. |
| 2230 | StoredDeclsMap::DestroyAll(Map: LastSDM.getPointer(), Dependent: LastSDM.getInt()); |
| 2231 | LastSDM.setPointer(nullptr); |
| 2232 | } |
| 2233 | |
| 2234 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 2235 | while (Map) { |
| 2236 | // Advance the iteration before we invalidate memory. |
| 2237 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 2238 | |
| 2239 | if (Dependent) |
| 2240 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 2241 | else |
| 2242 | delete Map; |
| 2243 | |
| 2244 | Map = Next.getPointer(); |
| 2245 | Dependent = Next.getInt(); |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 2250 | DeclContext *Parent, |
| 2251 | const PartialDiagnostic &PDiag) { |
| 2252 | assert(Parent->isDependentContext() |
| 2253 | && "cannot iterate dependent diagnostics of non-dependent context" ); |
| 2254 | Parent = Parent->getPrimaryContext(); |
| 2255 | if (!Parent->LookupPtr) |
| 2256 | Parent->CreateStoredDeclsMap(C); |
| 2257 | |
| 2258 | auto *Map = static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr); |
| 2259 | |
| 2260 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
| 2261 | // BumpPtrAllocator, rather than the ASTContext itself. |
| 2262 | DiagnosticStorage *DiagStorage = nullptr; |
| 2263 | if (PDiag.hasStorage()) |
| 2264 | DiagStorage = new (C) DiagnosticStorage; |
| 2265 | |
| 2266 | auto *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
| 2267 | |
| 2268 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 2269 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 2270 | Map->FirstDiagnostic = DD; |
| 2271 | |
| 2272 | return DD; |
| 2273 | } |
| 2274 | |
| 2275 | unsigned DeclIDBase::getLocalDeclIndex() const { |
| 2276 | return ID & llvm::maskTrailingOnes<DeclID>(N: 32); |
| 2277 | } |
| 2278 | |