clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DeclBase.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include/clang/AST -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../include -I /usr/src/gnu/usr.bin/clang/libclangAST/obj -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/lib/AST/DeclBase.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 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/DeclOpenMP.h" |
| 25 | #include "clang/AST/DeclTemplate.h" |
| 26 | #include "clang/AST/DependentDiagnostic.h" |
| 27 | #include "clang/AST/ExternalASTSource.h" |
| 28 | #include "clang/AST/Stmt.h" |
| 29 | #include "clang/AST/Type.h" |
| 30 | #include "clang/Basic/IdentifierTable.h" |
| 31 | #include "clang/Basic/LLVM.h" |
| 32 | #include "clang/Basic/LangOptions.h" |
| 33 | #include "clang/Basic/ObjCRuntime.h" |
| 34 | #include "clang/Basic/PartialDiagnostic.h" |
| 35 | #include "clang/Basic/SourceLocation.h" |
| 36 | #include "clang/Basic/TargetInfo.h" |
| 37 | #include "llvm/ADT/ArrayRef.h" |
| 38 | #include "llvm/ADT/PointerIntPair.h" |
| 39 | #include "llvm/ADT/SmallVector.h" |
| 40 | #include "llvm/ADT/StringRef.h" |
| 41 | #include "llvm/Support/Casting.h" |
| 42 | #include "llvm/Support/ErrorHandling.h" |
| 43 | #include "llvm/Support/MathExtras.h" |
| 44 | #include "llvm/Support/VersionTuple.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 | |
| 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 | |
| 63 | void Decl::updateOutOfDate(IdentifierInfo &II) const { |
| 64 | getASTContext().getExternalSource()->updateOutOfDateIdentifier(II); |
| 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 | |
| 76 | |
| 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 | |
| 85 | PrefixPtr[0] = 0; |
| 86 | |
| 87 | |
| 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 | |
| 97 | |
| 98 | |
| 99 | if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) { |
| 100 | |
| 101 | |
| 102 | size_t ExtraAlign = |
| 103 | llvm::offsetToAlignment(sizeof(Module *), llvm::Align(alignof(Decl))); |
| 104 | auto *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?"); |
| 116 | return getASTContext().getExternalSource()->getModule(getOwningModuleID()); |
| 117 | } |
| 118 | |
| 119 | bool Decl::hasLocalOwningModuleStorage() const { |
| 120 | return getASTContext().getLangOpts().trackLocalOwningModule(); |
| 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 | |
| 141 | |
| 142 | |
| 143 | setAccess(AS_public); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | if (auto *DD = dyn_cast<DecompositionDecl>(this)) { |
| 149 | for (auto *Binding : DD->bindings()) { |
| 150 | Binding->setInvalidDecl(); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const char *DeclContext::getDeclKindName() const { |
| 156 | switch (getDeclKind()) { |
| 157 | #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED; |
| 158 | #define ABSTRACT_DECL(DECL) |
| 159 | #include "clang/AST/DeclNodes.inc" |
| 160 | } |
| 161 | llvm_unreachable("Declaration context not in DeclNodes.inc!"); |
| 162 | } |
| 163 | |
| 164 | bool Decl::StatisticsEnabled = false; |
| 165 | void Decl::EnableStatistics() { |
| 166 | StatisticsEnabled = true; |
| 167 | } |
| 168 | |
| 169 | void Decl::PrintStats() { |
| 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 | |
| 201 | bool Decl::isTemplateParameterPack() const { |
| 202 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(this)) |
| 203 | return TTP->isParameterPack(); |
| 204 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(this)) |
| 205 | return NTTP->isParameterPack(); |
| 206 | if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(this)) |
| 207 | return TTP->isParameterPack(); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | bool Decl::isParameterPack() const { |
| 212 | if (const auto *Var = dyn_cast<VarDecl>(this)) |
| 213 | return Var->isParameterPack(); |
| 214 | |
| 215 | return isTemplateParameterPack(); |
| 216 | } |
| 217 | |
| 218 | FunctionDecl *Decl::getAsFunction() { |
| 219 | if (auto *FD = dyn_cast<FunctionDecl>(this)) |
| 220 | return FD; |
| 221 | if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(this)) |
| 222 | return FTD->getTemplatedDecl(); |
| 223 | return nullptr; |
| 224 | } |
| 225 | |
| 226 | bool Decl::isTemplateDecl() const { |
| 227 | return isa<TemplateDecl>(this); |
| 228 | } |
| 229 | |
| 230 | TemplateDecl *Decl::getDescribedTemplate() const { |
| 231 | if (auto *FD = dyn_cast<FunctionDecl>(this)) |
| 232 | return FD->getDescribedFunctionTemplate(); |
| 233 | if (auto *RD = dyn_cast<CXXRecordDecl>(this)) |
| 234 | return RD->getDescribedClassTemplate(); |
| 235 | if (auto *VD = dyn_cast<VarDecl>(this)) |
| 236 | return VD->getDescribedVarTemplate(); |
| 237 | if (auto *AD = dyn_cast<TypeAliasDecl>(this)) |
| 238 | return AD->getDescribedAliasTemplate(); |
| 239 | |
| 240 | return nullptr; |
| 241 | } |
| 242 | |
| 243 | const TemplateParameterList *Decl::getDescribedTemplateParams() const { |
| 244 | if (auto *TD = getDescribedTemplate()) |
| 245 | return TD->getTemplateParameters(); |
| 246 | if (auto *CTPSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(this)) |
| 247 | return CTPSD->getTemplateParameters(); |
| 248 | if (auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl>(this)) |
| 249 | return VTPSD->getTemplateParameters(); |
| 250 | return nullptr; |
| 251 | } |
| 252 | |
| 253 | bool Decl::isTemplated() const { |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | if (auto *AsDC = dyn_cast<DeclContext>(this)) |
| 259 | return AsDC->isDependentContext(); |
| 260 | auto *DC = getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext(); |
| 261 | return DC->isDependentContext() || isTemplateDecl() || |
| 262 | getDescribedTemplateParams(); |
| 263 | } |
| 264 | |
| 265 | unsigned Decl::getTemplateDepth() const { |
| 266 | if (auto *DC = dyn_cast<DeclContext>(this)) |
| 267 | if (DC->isFileContext()) |
| 268 | return 0; |
| 269 | |
| 270 | if (auto *TPL = getDescribedTemplateParams()) |
| 271 | return TPL->getDepth() + 1; |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | auto *RD = dyn_cast<CXXRecordDecl>(this); |
| 277 | if (RD && RD->isDependentLambda()) |
| 278 | if (Decl *Context = RD->getLambdaContextDecl()) |
| 279 | return Context->getTemplateDepth(); |
| 280 | |
| 281 | const DeclContext *DC = |
| 282 | getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext(); |
| 283 | return cast<Decl>(DC)->getTemplateDepth(); |
| 284 | } |
| 285 | |
| 286 | const DeclContext *Decl::getParentFunctionOrMethod() const { |
| 287 | for (const DeclContext *DC = getDeclContext(); |
| 288 | DC && !DC->isTranslationUnit() && !DC->isNamespace(); |
| 289 | DC = DC->getParent()) |
| 290 | if (DC->isFunctionOrMethod()) |
| 291 | return DC; |
| 292 | |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | void PrettyStackTraceDecl::print(raw_ostream &OS) const { |
| 301 | SourceLocation TheLoc = Loc; |
| 302 | if (TheLoc.isInvalid() && TheDecl) |
| 303 | TheLoc = TheDecl->getLocation(); |
| 304 | |
| 305 | if (TheLoc.isValid()) { |
| 306 | TheLoc.print(OS, SM); |
| 307 | OS << ": "; |
| 308 | } |
| 309 | |
| 310 | OS << Message; |
| 311 | |
| 312 | if (const auto *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) { |
| 313 | OS << " '"; |
| 314 | DN->printQualifiedName(OS); |
| 315 | OS << '\''; |
| 316 | } |
| 317 | OS << '\n'; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | Decl::~Decl() = default; |
| 326 | |
| 327 | void Decl::setDeclContext(DeclContext *DC) { |
| 328 | DeclCtx = DC; |
| 329 | } |
| 330 | |
| 331 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 332 | if (DC == getLexicalDeclContext()) |
| 333 | return; |
| 334 | |
| 335 | if (isInSemaDC()) { |
| 336 | setDeclContextsImpl(getDeclContext(), DC, getASTContext()); |
| 337 | } else { |
| 338 | getMultipleDC()->LexicalDC = DC; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | if (!isFromASTFile()) { |
| 344 | setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC)); |
| 345 | if (hasOwningModule()) |
| 346 | setLocalOwningModule(cast<Decl>(DC)->getOwningModule()); |
| 347 | } |
| 348 | |
| 349 | assert( |
| 350 | (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || |
| 351 | getOwningModule()) && |
| 352 | "hidden declaration has no owning module"); |
| 353 | } |
| 354 | |
| 355 | void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, |
| 356 | ASTContext &Ctx) { |
| 357 | if (SemaDC == LexicalDC) { |
| 358 | DeclCtx = SemaDC; |
| 359 | } else { |
| 360 | auto *MDC = new (Ctx) Decl::MultipleDC(); |
| 361 | MDC->SemanticDC = SemaDC; |
| 362 | MDC->LexicalDC = LexicalDC; |
| 363 | DeclCtx = MDC; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | bool Decl::isInLocalScopeForInstantiation() const { |
| 368 | const DeclContext *LDC = getLexicalDeclContext(); |
| 369 | if (!LDC->isDependentContext()) |
| 370 | return false; |
| 371 | while (true) { |
| 372 | if (LDC->isFunctionOrMethod()) |
| 373 | return true; |
| 374 | if (!isa<TagDecl>(LDC)) |
| 375 | return false; |
| 376 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(LDC)) |
| 377 | if (CRD->isLambda()) |
| 378 | return true; |
| 379 | LDC = LDC->getLexicalParent(); |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | bool Decl::isInAnonymousNamespace() const { |
| 385 | for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) { |
| 386 | if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) |
| 387 | if (ND->isAnonymousNamespace()) |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | bool Decl::isInStdNamespace() const { |
| 395 | const DeclContext *DC = getDeclContext(); |
| 396 | return DC && DC->isStdNamespace(); |
| 397 | } |
| 398 | |
| 399 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
| 400 | if (auto *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 401 | return TUD; |
| 402 | |
| 403 | DeclContext *DC = getDeclContext(); |
| 404 | assert(DC && "This decl is not contained in a translation unit!"); |
| 405 | |
| 406 | while (!DC->isTranslationUnit()) { |
| 407 | DC = DC->getParent(); |
| 408 | assert(DC && "This decl is not contained in a translation unit!"); |
| 409 | } |
| 410 | |
| 411 | return cast<TranslationUnitDecl>(DC); |
| 412 | } |
| 413 | |
| 414 | ASTContext &Decl::getASTContext() const { |
| 415 | return getTranslationUnitDecl()->getASTContext(); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | |
| 420 | const LangOptions &Decl::getLangOpts() const { |
| 421 | return getASTContext().getLangOpts(); |
| 422 | } |
| 423 | |
| 424 | ASTMutationListener *Decl::getASTMutationListener() const { |
| 425 | return getASTContext().getASTMutationListener(); |
| 426 | } |
| 427 | |
| 428 | unsigned Decl::getMaxAlignment() const { |
| 429 | if (!hasAttrs()) |
| 430 | return 0; |
| 431 | |
| 432 | unsigned Align = 0; |
| 433 | const AttrVec &V = getAttrs(); |
| 434 | ASTContext &Ctx = getASTContext(); |
| 435 | specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end()); |
| 436 | for (; I != E; ++I) { |
| 437 | if (!I->isAlignmentErrorDependent()) |
| 438 | Align = std::max(Align, I->getAlignment(Ctx)); |
| 439 | } |
| 440 | return Align; |
| 441 | } |
| 442 | |
| 443 | bool Decl::isUsed(bool CheckUsedAttr) const { |
| 444 | const Decl *CanonD = getCanonicalDecl(); |
| 445 | if (CanonD->Used) |
| 446 | return true; |
| 447 | |
| 448 | |
| 449 | |
| 450 | if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>()) |
| 451 | return true; |
| 452 | |
| 453 | |
| 454 | |
| 455 | return getMostRecentDecl()->getCanonicalDecl()->Used; |
| 456 | } |
| 457 | |
| 458 | void Decl::markUsed(ASTContext &C) { |
| 459 | if (isUsed(false)) |
| 460 | return; |
| 461 | |
| 462 | if (C.getASTMutationListener()) |
| 463 | C.getASTMutationListener()->DeclarationMarkedUsed(this); |
| 464 | |
| 465 | setIsUsed(); |
| 466 | } |
| 467 | |
| 468 | bool Decl::isReferenced() const { |
| 469 | if (Referenced) |
| 470 | return true; |
| 471 | |
| 472 | |
| 473 | for (const auto *I : redecls()) |
| 474 | if (I->Referenced) |
| 475 | return true; |
| 476 | |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const { |
| 481 | const Decl *Definition = nullptr; |
| 482 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) { |
| 483 | Definition = ID->getDefinition(); |
| 484 | } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(this)) { |
| 485 | Definition = PD->getDefinition(); |
| 486 | } else if (auto *TD = dyn_cast<TagDecl>(this)) { |
| 487 | Definition = TD->getDefinition(); |
| 488 | } |
| 489 | if (!Definition) |
| 490 | Definition = this; |
| 491 | |
| 492 | if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>()) |
| 493 | return attr; |
| 494 | if (auto *dcd = dyn_cast<Decl>(getDeclContext())) { |
| 495 | return dcd->getAttr<ExternalSourceSymbolAttr>(); |
| 496 | } |
| 497 | |
| 498 | return nullptr; |
| 499 | } |
| 500 | |
| 501 | bool Decl::hasDefiningAttr() const { |
| 502 | return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>() || |
| 503 | hasAttr<LoaderUninitializedAttr>(); |
| 504 | } |
| 505 | |
| 506 | const Attr *Decl::getDefiningAttr() const { |
| 507 | if (auto *AA = getAttr<AliasAttr>()) |
| 508 | return AA; |
| 509 | if (auto *IFA = getAttr<IFuncAttr>()) |
| 510 | return IFA; |
| 511 | if (auto *NZA = getAttr<LoaderUninitializedAttr>()) |
| 512 | return NZA; |
| 513 | return nullptr; |
| 514 | } |
| 515 | |
| 516 | static StringRef getRealizedPlatform(const AvailabilityAttr *A, |
| 517 | const ASTContext &Context) { |
| 518 | |
| 519 | |
| 520 | StringRef RealizedPlatform = A->getPlatform()->getName(); |
| 521 | if (!Context.getLangOpts().AppExt) |
| 522 | return RealizedPlatform; |
| 523 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 524 | if (suffix != StringRef::npos) |
| 525 | return RealizedPlatform.slice(0, suffix); |
| 526 | return RealizedPlatform; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | static AvailabilityResult CheckAvailability(ASTContext &Context, |
| 539 | const AvailabilityAttr *A, |
| 540 | std::string *Message, |
| 541 | VersionTuple EnclosingVersion) { |
| 542 | if (EnclosingVersion.empty()) |
| 543 | EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion(); |
| 544 | |
| 545 | if (EnclosingVersion.empty()) |
| 546 | return AR_Available; |
| 547 | |
| 548 | StringRef ActualPlatform = A->getPlatform()->getName(); |
| 549 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 550 | |
| 551 | |
| 552 | if (getRealizedPlatform(A, Context) != TargetPlatform) |
| 553 | return AR_Available; |
| 554 | |
| 555 | StringRef PrettyPlatformName |
| 556 | = AvailabilityAttr::getPrettyPlatformName(ActualPlatform); |
| 557 | |
| 558 | if (PrettyPlatformName.empty()) |
| 559 | PrettyPlatformName = ActualPlatform; |
| 560 | |
| 561 | std::string HintMessage; |
| 562 | if (!A->getMessage().empty()) { |
| 563 | HintMessage = " - "; |
| 564 | HintMessage += A->getMessage(); |
| 565 | } |
| 566 | |
| 567 | |
| 568 | if (A->getUnavailable()) { |
| 569 | if (Message) { |
| 570 | Message->clear(); |
| 571 | llvm::raw_string_ostream Out(*Message); |
| 572 | Out << "not available on " << PrettyPlatformName |
| 573 | << HintMessage; |
| 574 | } |
| 575 | |
| 576 | return AR_Unavailable; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | if (!A->getIntroduced().empty() && |
| 581 | EnclosingVersion < A->getIntroduced()) { |
| 582 | if (Message) { |
| 583 | Message->clear(); |
| 584 | llvm::raw_string_ostream Out(*Message); |
| 585 | VersionTuple VTI(A->getIntroduced()); |
| 586 | Out << "introduced in " << PrettyPlatformName << ' ' |
| 587 | << VTI << HintMessage; |
| 588 | } |
| 589 | |
| 590 | return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) { |
| 595 | if (Message) { |
| 596 | Message->clear(); |
| 597 | llvm::raw_string_ostream Out(*Message); |
| 598 | VersionTuple VTO(A->getObsoleted()); |
| 599 | Out << "obsoleted in " << PrettyPlatformName << ' ' |
| 600 | << VTO << HintMessage; |
| 601 | } |
| 602 | |
| 603 | return AR_Unavailable; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) { |
| 608 | if (Message) { |
| 609 | Message->clear(); |
| 610 | llvm::raw_string_ostream Out(*Message); |
| 611 | VersionTuple VTD(A->getDeprecated()); |
| 612 | Out << "first deprecated in " << PrettyPlatformName << ' ' |
| 613 | << VTD << HintMessage; |
| 614 | } |
| 615 | |
| 616 | return AR_Deprecated; |
| 617 | } |
| 618 | |
| 619 | return AR_Available; |
| 620 | } |
| 621 | |
| 622 | AvailabilityResult Decl::getAvailability(std::string *Message, |
| 623 | VersionTuple EnclosingVersion, |
| 624 | StringRef *RealizedPlatform) const { |
| 625 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this)) |
| 626 | return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion, |
| 627 | RealizedPlatform); |
| 628 | |
| 629 | AvailabilityResult Result = AR_Available; |
| 630 | std::string ResultMessage; |
| 631 | |
| 632 | for (const auto *A : attrs()) { |
| 633 | if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
| 634 | if (Result >= AR_Deprecated) |
| 635 | continue; |
| 636 | |
| 637 | if (Message) |
| 638 | ResultMessage = std::string(Deprecated->getMessage()); |
| 639 | |
| 640 | Result = AR_Deprecated; |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
| 645 | if (Message) |
| 646 | *Message = std::string(Unavailable->getMessage()); |
| 647 | return AR_Unavailable; |
| 648 | } |
| 649 | |
| 650 | if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) { |
| 651 | AvailabilityResult AR = CheckAvailability(getASTContext(), Availability, |
| 652 | Message, EnclosingVersion); |
| 653 | |
| 654 | if (AR == AR_Unavailable) { |
| 655 | if (RealizedPlatform) |
| 656 | *RealizedPlatform = Availability->getPlatform()->getName(); |
| 657 | return AR_Unavailable; |
| 658 | } |
| 659 | |
| 660 | if (AR > Result) { |
| 661 | Result = AR; |
| 662 | if (Message) |
| 663 | ResultMessage.swap(*Message); |
| 664 | } |
| 665 | continue; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (Message) |
| 670 | Message->swap(ResultMessage); |
| 671 | return Result; |
| 672 | } |
| 673 | |
| 674 | VersionTuple Decl::getVersionIntroduced() const { |
| 675 | const ASTContext &Context = getASTContext(); |
| 676 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 677 | for (const auto *A : attrs()) { |
| 678 | if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) { |
| 679 | if (getRealizedPlatform(Availability, Context) != TargetPlatform) |
| 680 | continue; |
| 681 | if (!Availability->getIntroduced().empty()) |
| 682 | return Availability->getIntroduced(); |
| 683 | } |
| 684 | } |
| 685 | return {}; |
| 686 | } |
| 687 | |
| 688 | bool Decl::canBeWeakImported(bool &IsDefinition) const { |
| 689 | IsDefinition = false; |
| 690 | |
| 691 | |
| 692 | if (const auto *Var = dyn_cast<VarDecl>(this)) { |
| 693 | if (Var->isThisDeclarationADefinition()) { |
| 694 | IsDefinition = true; |
| 695 | return false; |
| 696 | } |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | if (const auto *FD = dyn_cast<FunctionDecl>(this)) { |
| 701 | if (FD->hasBody()) { |
| 702 | IsDefinition = true; |
| 703 | return false; |
| 704 | } |
| 705 | return true; |
| 706 | |
| 707 | } |
| 708 | |
| 709 | if (isa<ObjCInterfaceDecl>(this) && |
| 710 | getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) { |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | bool Decl::isWeakImported() const { |
| 718 | bool IsDefinition; |
| 719 | if (!canBeWeakImported(IsDefinition)) |
| 720 | return false; |
| 721 | |
| 722 | for (const auto *A : getMostRecentDecl()->attrs()) { |
| 723 | if (isa<WeakImportAttr>(A)) |
| 724 | return true; |
| 725 | |
| 726 | if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) { |
| 727 | if (CheckAvailability(getASTContext(), Availability, nullptr, |
| 728 | VersionTuple()) == AR_NotYetIntroduced) |
| 729 | return true; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 737 | switch (DeclKind) { |
| 738 | case Function: |
| 739 | case CXXDeductionGuide: |
| 740 | case CXXMethod: |
| 741 | case CXXConstructor: |
| 742 | case ConstructorUsingShadow: |
| 743 | case CXXDestructor: |
| 744 | case CXXConversion: |
| 745 | case EnumConstant: |
| 746 | case Var: |
| 747 | case ImplicitParam: |
| 748 | case ParmVar: |
| 749 | case ObjCMethod: |
| 750 | case ObjCProperty: |
| 751 | case MSProperty: |
| 752 | return IDNS_Ordinary; |
| 753 | case Label: |
| 754 | return IDNS_Label; |
| 755 | case IndirectField: |
| 756 | return IDNS_Ordinary | IDNS_Member; |
| 757 | |
| 758 | case Binding: |
| 759 | case NonTypeTemplateParm: |
| 760 | case VarTemplate: |
| 761 | case Concept: |
| 762 | |
| 763 | |
| 764 | return IDNS_Ordinary | IDNS_Tag; |
| 765 | |
| 766 | case ObjCCompatibleAlias: |
| 767 | case ObjCInterface: |
| 768 | return IDNS_Ordinary | IDNS_Type; |
| 769 | |
| 770 | case Typedef: |
| 771 | case TypeAlias: |
| 772 | case TemplateTypeParm: |
| 773 | case ObjCTypeParam: |
| 774 | return IDNS_Ordinary | IDNS_Type; |
| 775 | |
| 776 | case UnresolvedUsingTypename: |
| 777 | return IDNS_Ordinary | IDNS_Type | IDNS_Using; |
| 778 | |
| 779 | case UsingShadow: |
| 780 | return 0; |
| 781 | |
| 782 | case UnresolvedUsingValue: |
| 783 | return IDNS_Ordinary | IDNS_Using; |
| 784 | |
| 785 | case Using: |
| 786 | case UsingPack: |
| 787 | case UsingEnum: |
| 788 | return IDNS_Using; |
| 789 | |
| 790 | case ObjCProtocol: |
| 791 | return IDNS_ObjCProtocol; |
| 792 | |
| 793 | case Field: |
| 794 | case ObjCAtDefsField: |
| 795 | case ObjCIvar: |
| 796 | return IDNS_Member; |
| 797 | |
| 798 | case Record: |
| 799 | case CXXRecord: |
| 800 | case Enum: |
| 801 | return IDNS_Tag | IDNS_Type; |
| 802 | |
| 803 | case Namespace: |
| 804 | case NamespaceAlias: |
| 805 | return IDNS_Namespace; |
| 806 | |
| 807 | case FunctionTemplate: |
| 808 | return IDNS_Ordinary; |
| 809 | |
| 810 | case ClassTemplate: |
| 811 | case TemplateTemplateParm: |
| 812 | case TypeAliasTemplate: |
| 813 | return IDNS_Ordinary | IDNS_Tag | IDNS_Type; |
| 814 | |
| 815 | case UnresolvedUsingIfExists: |
| 816 | return IDNS_Type | IDNS_Ordinary; |
| 817 | |
| 818 | case OMPDeclareReduction: |
| 819 | return IDNS_OMPReduction; |
| 820 | |
| 821 | case OMPDeclareMapper: |
| 822 | return IDNS_OMPMapper; |
| 823 | |
| 824 | |
| 825 | case Friend: |
| 826 | case FriendTemplate: |
| 827 | case AccessSpec: |
| 828 | case LinkageSpec: |
| 829 | case Export: |
| 830 | case FileScopeAsm: |
| 831 | case StaticAssert: |
| 832 | case ObjCPropertyImpl: |
| 833 | case PragmaComment: |
| 834 | case PragmaDetectMismatch: |
| 835 | case Block: |
| 836 | case Captured: |
| 837 | case TranslationUnit: |
| 838 | case ExternCContext: |
| 839 | case Decomposition: |
| 840 | case MSGuid: |
| 841 | case TemplateParamObject: |
| 842 | |
| 843 | case UsingDirective: |
| 844 | case BuiltinTemplate: |
| 845 | case ClassTemplateSpecialization: |
| 846 | case ClassTemplatePartialSpecialization: |
| 847 | case ClassScopeFunctionSpecialization: |
| 848 | case VarTemplateSpecialization: |
| 849 | case VarTemplatePartialSpecialization: |
| 850 | case ObjCImplementation: |
| 851 | case ObjCCategory: |
| 852 | case ObjCCategoryImpl: |
| 853 | case Import: |
| 854 | case OMPThreadPrivate: |
| 855 | case OMPAllocate: |
| 856 | case OMPRequires: |
| 857 | case OMPCapturedExpr: |
| 858 | case Empty: |
| 859 | case LifetimeExtendedTemporary: |
| 860 | case RequiresExprBody: |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | llvm_unreachable("Invalid DeclKind!"); |
| 866 | } |
| 867 | |
| 868 | void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) { |
| 869 | assert(!HasAttrs && "Decl already contains attrs."); |
| 870 | |
| 871 | AttrVec &AttrBlank = Ctx.getDeclAttrs(this); |
| 872 | assert(AttrBlank.empty() && "HasAttrs was wrong?"); |
| 873 | |
| 874 | AttrBlank = attrs; |
| 875 | HasAttrs = true; |
| 876 | } |
| 877 | |
| 878 | void Decl::dropAttrs() { |
| 879 | if (!HasAttrs) return; |
| 880 | |
| 881 | HasAttrs = false; |
| 882 | getASTContext().eraseDeclAttrs(this); |
| 883 | } |
| 884 | |
| 885 | void Decl::addAttr(Attr *A) { |
| 886 | if (!hasAttrs()) { |
| 887 | setAttrs(AttrVec(1, A)); |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | AttrVec &Attrs = getAttrs(); |
| 892 | if (!A->isInherited()) { |
| 893 | Attrs.push_back(A); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | auto I = Attrs.begin(), E = Attrs.end(); |
| 901 | for (; I != E; ++I) { |
| 902 | if (!(*I)->isInherited()) |
| 903 | break; |
| 904 | } |
| 905 | Attrs.insert(I, A); |
| 906 | } |
| 907 | |
| 908 | const AttrVec &Decl::getAttrs() const { |
| 909 | assert(HasAttrs && "No attrs to get!"); |
| 910 | return getASTContext().getDeclAttrs(this); |
| 911 | } |
| 912 | |
| 913 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
| 914 | Decl::Kind DK = D->getDeclKind(); |
| 915 | switch(DK) { |
| 916 | #define DECL(NAME, BASE) |
| 917 | #define DECL_CONTEXT(NAME) \ |
| 918 | case Decl::NAME: \ |
| 919 | return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D)); |
| 920 | #define DECL_CONTEXT_BASE(NAME) |
| 921 | #include "clang/AST/DeclNodes.inc" |
| 922 | default: |
| 923 | #define DECL(NAME, BASE) |
| 924 | #define DECL_CONTEXT_BASE(NAME) \ |
| 925 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 926 | return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D)); |
| 927 | #include "clang/AST/DeclNodes.inc" |
| 928 | llvm_unreachable("a decl that inherits DeclContext isn't handled"); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
| 933 | Decl::Kind DK = D->getKind(); |
| 934 | switch(DK) { |
| 935 | #define DECL(NAME, BASE) |
| 936 | #define DECL_CONTEXT(NAME) \ |
| 937 | case Decl::NAME: \ |
| 938 | return static_cast<NAME##Decl *>(const_cast<Decl *>(D)); |
| 939 | #define DECL_CONTEXT_BASE(NAME) |
| 940 | #include "clang/AST/DeclNodes.inc" |
| 941 | default: |
| 942 | #define DECL(NAME, BASE) |
| 943 | #define DECL_CONTEXT_BASE(NAME) \ |
| 944 | if (DK >= first##NAME && DK <= last##NAME) \ |
| 945 | return static_cast<NAME##Decl *>(const_cast<Decl *>(D)); |
| 946 | #include "clang/AST/DeclNodes.inc" |
| 947 | llvm_unreachable("a decl that inherits DeclContext isn't handled"); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | SourceLocation Decl::getBodyRBrace() const { |
| 952 | |
| 953 | |
| 954 | if (const auto *FD = dyn_cast<FunctionDecl>(this)) { |
| 955 | const FunctionDecl *Definition; |
| 956 | if (FD->hasBody(Definition)) |
| 957 | return Definition->getSourceRange().getEnd(); |
| 958 | return {}; |
| 959 | } |
| 960 | |
| 961 | if (Stmt *Body = getBody()) |
| 962 | return Body->getSourceRange().getEnd(); |
| 963 | |
| 964 | return {}; |
| 965 | } |
| 966 | |
| 967 | bool Decl::AccessDeclContextSanity() const { |
| 968 | #ifndef NDEBUG |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | if (isa<TranslationUnitDecl>(this) || isa<TemplateTypeParmDecl>(this) || |
| 979 | isa<NonTypeTemplateParmDecl>(this) || !getDeclContext() || |
| 980 | !isa<CXXRecordDecl>(getDeclContext()) || isInvalidDecl() || |
| 981 | isa<StaticAssertDecl>(this) || isa<BlockDecl>(this) || |
| 982 | |
| 983 | |
| 984 | isa<ParmVarDecl>(this) || |
| 985 | |
| 986 | |
| 987 | isa<CXXRecordDecl>(this) || |
| 988 | isa<ClassScopeFunctionSpecializationDecl>(this) || |
| 989 | isa<LifetimeExtendedTemporaryDecl>(this)) |
| 990 | return true; |
| 991 | |
| 992 | assert(Access != AS_none && |
| 993 | "Access specifier is AS_none inside a record decl"); |
| 994 | #endif |
| 995 | return true; |
| 996 | } |
| 997 | |
| 998 | static Decl::Kind getKind(const Decl *D) { return D->getKind(); } |
| 999 | static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); } |
| 1000 | |
| 1001 | int64_t Decl::getID() const { |
| 1002 | return getASTContext().getAllocator().identifyKnownAlignedObject<Decl>(this); |
| 1003 | } |
| 1004 | |
| 1005 | const FunctionType *Decl::getFunctionType(bool BlocksToo) const { |
| 1006 | QualType Ty; |
| 1007 | if (const auto *D = dyn_cast<ValueDecl>(this)) |
| 1008 | Ty = D->getType(); |
| 1009 | else if (const auto *D = dyn_cast<TypedefNameDecl>(this)) |
| 1010 | Ty = D->getUnderlyingType(); |
| 1011 | else |
| 1012 | return nullptr; |
| 1013 | |
| 1014 | if (Ty->isFunctionPointerType()) |
| 1015 | Ty = Ty->castAs<PointerType>()->getPointeeType(); |
| 1016 | else if (Ty->isFunctionReferenceType()) |
| 1017 | Ty = Ty->castAs<ReferenceType>()->getPointeeType(); |
| 1018 | else if (BlocksToo && Ty->isBlockPointerType()) |
| 1019 | Ty = Ty->castAs<BlockPointerType>()->getPointeeType(); |
| 1020 | |
| 1021 | return Ty->getAs<FunctionType>(); |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | template <class T> static Decl *getNonClosureContext(T *D) { |
| 1027 | if (getKind(D) == Decl::CXXMethod) { |
| 1028 | auto *MD = cast<CXXMethodDecl>(D); |
| 1029 | if (MD->getOverloadedOperator() == OO_Call && |
| 1030 | MD->getParent()->isLambda()) |
| 1031 | return getNonClosureContext(MD->getParent()->getParent()); |
| 1032 | return MD; |
| 1033 | } |
| 1034 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1035 | return FD; |
| 1036 | if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1037 | return MD; |
| 1038 | if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 1039 | return getNonClosureContext(BD->getParent()); |
| 1040 | if (auto *CD = dyn_cast<CapturedDecl>(D)) |
| 1041 | return getNonClosureContext(CD->getParent()); |
| 1042 | return nullptr; |
| 1043 | } |
| 1044 | |
| 1045 | Decl *Decl::getNonClosureContext() { |
| 1046 | return ::getNonClosureContext(this); |
| 1047 | } |
| 1048 | |
| 1049 | Decl *DeclContext::getNonClosureAncestor() { |
| 1050 | return ::getNonClosureContext(this); |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | DeclContext::DeclContext(Decl::Kind K) { |
| 1058 | DeclContextBits.DeclKind = K; |
| 1059 | setHasExternalLexicalStorage(false); |
| 1060 | setHasExternalVisibleStorage(false); |
| 1061 | setNeedToReconcileExternalVisibleStorage(false); |
| 1062 | setHasLazyLocalLexicalLookups(false); |
| 1063 | setHasLazyExternalLexicalLookups(false); |
| 1064 | setUseQualifiedLookup(false); |
| 1065 | } |
| 1066 | |
| 1067 | bool DeclContext::classof(const Decl *D) { |
| 1068 | switch (D->getKind()) { |
| 1069 | #define DECL(NAME, BASE) |
| 1070 | #define DECL_CONTEXT(NAME) case Decl::NAME: |
| 1071 | #define DECL_CONTEXT_BASE(NAME) |
| 1072 | #include "clang/AST/DeclNodes.inc" |
| 1073 | return true; |
| 1074 | default: |
| 1075 | #define DECL(NAME, BASE) |
| 1076 | #define DECL_CONTEXT_BASE(NAME) \ |
| 1077 | if (D->getKind() >= Decl::first##NAME && \ |
| 1078 | D->getKind() <= Decl::last##NAME) \ |
| 1079 | return true; |
| 1080 | #include "clang/AST/DeclNodes.inc" |
| 1081 | return false; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | DeclContext::~DeclContext() = default; |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | DeclContext *DeclContext::getLookupParent() { |
| 1094 | |
| 1095 | if (isa<FunctionDecl>(this)) |
| 1096 | if (getParent()->getRedeclContext()->isFileContext() && |
| 1097 | getLexicalParent()->getRedeclContext()->isRecord()) |
| 1098 | return getLexicalParent(); |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | if (isLambdaCallOperator(this)) |
| 1104 | return getParent()->getParent(); |
| 1105 | |
| 1106 | return getParent(); |
| 1107 | } |
| 1108 | |
| 1109 | const BlockDecl *DeclContext::getInnermostBlockDecl() const { |
| 1110 | const DeclContext *Ctx = this; |
| 1111 | |
| 1112 | do { |
| 1113 | if (Ctx->isClosure()) |
| 1114 | return cast<BlockDecl>(Ctx); |
| 1115 | Ctx = Ctx->getParent(); |
| 1116 | } while (Ctx); |
| 1117 | |
| 1118 | return nullptr; |
| 1119 | } |
| 1120 | |
| 1121 | bool DeclContext::isInlineNamespace() const { |
| 1122 | return isNamespace() && |
| 1123 | cast<NamespaceDecl>(this)->isInline(); |
| 1124 | } |
| 1125 | |
| 1126 | bool DeclContext::isStdNamespace() const { |
| 1127 | if (!isNamespace()) |
| 1128 | return false; |
| 1129 | |
| 1130 | const auto *ND = cast<NamespaceDecl>(this); |
| 1131 | if (ND->isInline()) { |
| 1132 | return ND->getParent()->isStdNamespace(); |
| 1133 | } |
| 1134 | |
| 1135 | if (!getParent()->getRedeclContext()->isTranslationUnit()) |
| 1136 | return false; |
| 1137 | |
| 1138 | const IdentifierInfo *II = ND->getIdentifier(); |
| 1139 | return II && II->isStr("std"); |
| 1140 | } |
| 1141 | |
| 1142 | bool DeclContext::isDependentContext() const { |
| 1143 | if (isFileContext()) |
| 1144 | return false; |
| 1145 | |
| 1146 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 1147 | return true; |
| 1148 | |
| 1149 | if (const auto *Record = dyn_cast<CXXRecordDecl>(this)) { |
| 1150 | if (Record->getDescribedClassTemplate()) |
| 1151 | return true; |
| 1152 | |
| 1153 | if (Record->isDependentLambda()) |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | if (const auto *Function = dyn_cast<FunctionDecl>(this)) { |
| 1158 | if (Function->getDescribedFunctionTemplate()) |
| 1159 | return true; |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | if (cast<Decl>(this)->getFriendObjectKind()) |
| 1164 | return getLexicalParent()->isDependentContext(); |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | return getParent() && getParent()->isDependentContext(); |
| 1172 | } |
| 1173 | |
| 1174 | bool DeclContext::isTransparentContext() const { |
| 1175 | if (getDeclKind() == Decl::Enum) |
| 1176 | return !cast<EnumDecl>(this)->isScoped(); |
| 1177 | |
| 1178 | return getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export; |
| 1179 | } |
| 1180 | |
| 1181 | static bool isLinkageSpecContext(const DeclContext *DC, |
| 1182 | LinkageSpecDecl::LanguageIDs ID) { |
| 1183 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| 1184 | if (DC->getDeclKind() == Decl::LinkageSpec) |
| 1185 | return cast<LinkageSpecDecl>(DC)->getLanguage() == ID; |
| 1186 | DC = DC->getLexicalParent(); |
| 1187 | } |
| 1188 | return false; |
| 1189 | } |
| 1190 | |
| 1191 | bool DeclContext::isExternCContext() const { |
| 1192 | return isLinkageSpecContext(this, LinkageSpecDecl::lang_c); |
| 1193 | } |
| 1194 | |
| 1195 | const LinkageSpecDecl *DeclContext::getExternCContext() const { |
| 1196 | const DeclContext *DC = this; |
| 1197 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| 1198 | if (DC->getDeclKind() == Decl::LinkageSpec && |
| 1199 | cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c) |
| 1200 | return cast<LinkageSpecDecl>(DC); |
| 1201 | DC = DC->getLexicalParent(); |
| 1202 | } |
| 1203 | return nullptr; |
| 1204 | } |
| 1205 | |
| 1206 | bool DeclContext::isExternCXXContext() const { |
| 1207 | return isLinkageSpecContext(this, LinkageSpecDecl::lang_cxx); |
| 1208 | } |
| 1209 | |
| 1210 | bool DeclContext::Encloses(const DeclContext *DC) const { |
| 1211 | if (getPrimaryContext() != this) |
| 1212 | return getPrimaryContext()->Encloses(DC); |
| 1213 | |
| 1214 | for (; DC; DC = DC->getParent()) |
| 1215 | if (DC->getPrimaryContext() == this) |
| 1216 | return true; |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
| 1220 | DeclContext *DeclContext::getPrimaryContext() { |
| 1221 | switch (getDeclKind()) { |
| 1222 | case Decl::ExternCContext: |
| 1223 | case Decl::LinkageSpec: |
| 1224 | case Decl::Export: |
| 1225 | case Decl::Block: |
| 1226 | case Decl::Captured: |
| 1227 | case Decl::OMPDeclareReduction: |
| 1228 | case Decl::OMPDeclareMapper: |
| 1229 | case Decl::RequiresExprBody: |
| 1230 | |
| 1231 | return this; |
| 1232 | |
| 1233 | case Decl::TranslationUnit: |
| 1234 | return static_cast<TranslationUnitDecl *>(this)->getFirstDecl(); |
| 1235 | case Decl::Namespace: |
| 1236 | |
| 1237 | return static_cast<NamespaceDecl *>(this)->getOriginalNamespace(); |
| 1238 | |
| 1239 | case Decl::ObjCMethod: |
| 1240 | return this; |
| 1241 | |
| 1242 | case Decl::ObjCInterface: |
| 1243 | if (auto *OID = dyn_cast<ObjCInterfaceDecl>(this)) |
| 1244 | if (auto *Def = OID->getDefinition()) |
| 1245 | return Def; |
| 1246 | return this; |
| 1247 | |
| 1248 | case Decl::ObjCProtocol: |
| 1249 | if (auto *OPD = dyn_cast<ObjCProtocolDecl>(this)) |
| 1250 | if (auto *Def = OPD->getDefinition()) |
| 1251 | return Def; |
| 1252 | return this; |
| 1253 | |
| 1254 | case Decl::ObjCCategory: |
| 1255 | return this; |
| 1256 | |
| 1257 | case Decl::ObjCImplementation: |
| 1258 | case Decl::ObjCCategoryImpl: |
| 1259 | return this; |
| 1260 | |
| 1261 | default: |
| 1262 | if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) { |
| 1263 | |
| 1264 | |
| 1265 | auto *Tag = cast<TagDecl>(this); |
| 1266 | |
| 1267 | if (TagDecl *Def = Tag->getDefinition()) |
| 1268 | return Def; |
| 1269 | |
| 1270 | if (const auto *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) { |
| 1271 | |
| 1272 | TagDecl *PossiblePartialDef = TagTy->getDecl(); |
| 1273 | if (PossiblePartialDef->isBeingDefined()) |
| 1274 | return PossiblePartialDef; |
| 1275 | } else { |
| 1276 | assert(isa<InjectedClassNameType>(Tag->getTypeForDecl())); |
| 1277 | } |
| 1278 | |
| 1279 | return Tag; |
| 1280 | } |
| 1281 | |
| 1282 | assert(getDeclKind() >= Decl::firstFunction && |
| 1283 | getDeclKind() <= Decl::lastFunction && |
| 1284 | "Unknown DeclContext kind"); |
| 1285 | return this; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | template <typename T> |
| 1290 | void collectAllContextsImpl(T *Self, SmallVectorImpl<DeclContext *> &Contexts) { |
| 1291 | for (T *D = Self->getMostRecentDecl(); D; D = D->getPreviousDecl()) |
| 1292 | Contexts.push_back(D); |
| 1293 | |
| 1294 | std::reverse(Contexts.begin(), Contexts.end()); |
| 1295 | } |
| 1296 | |
| 1297 | void DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts) { |
| 1298 | Contexts.clear(); |
| 1299 | |
| 1300 | Decl::Kind Kind = getDeclKind(); |
| 1301 | |
| 1302 | if (Kind == Decl::TranslationUnit) |
| 1303 | collectAllContextsImpl(static_cast<TranslationUnitDecl *>(this), Contexts); |
| 1304 | else if (Kind == Decl::Namespace) |
| 1305 | collectAllContextsImpl(static_cast<NamespaceDecl *>(this), Contexts); |
| 1306 | else |
| 1307 | Contexts.push_back(this); |
| 1308 | } |
| 1309 | |
| 1310 | std::pair<Decl *, Decl *> |
| 1311 | DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls, |
| 1312 | bool FieldsAlreadyLoaded) { |
| 1313 | |
| 1314 | Decl *FirstNewDecl = nullptr; |
| 1315 | Decl *PrevDecl = nullptr; |
| 1316 | for (auto *D : Decls) { |
| 1317 | if (FieldsAlreadyLoaded && isa<FieldDecl>(D)) |
| 1318 | continue; |
| 1319 | |
| 1320 | if (PrevDecl) |
| 1321 | PrevDecl->NextInContextAndBits.setPointer(D); |
| 1322 | else |
| 1323 | FirstNewDecl = D; |
| 1324 | |
| 1325 | PrevDecl = D; |
| 1326 | } |
| 1327 | |
| 1328 | return std::make_pair(FirstNewDecl, PrevDecl); |
| 1329 | } |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | void DeclContext::reconcileExternalVisibleStorage() const { |
| 1335 | assert(hasNeedToReconcileExternalVisibleStorage() && LookupPtr); |
| 1336 | setNeedToReconcileExternalVisibleStorage(false); |
| 1337 | |
| 1338 | for (auto &Lookup : *LookupPtr) |
| 1339 | Lookup.second.setHasExternalDecls(); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | bool |
| 1346 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 1347 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1348 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 1349 | |
| 1350 | |
| 1351 | ExternalASTSource::Deserializing ADeclContext(Source); |
| 1352 | |
| 1353 | |
| 1354 | SmallVector<Decl*, 64> Decls; |
| 1355 | setHasExternalLexicalStorage(false); |
| 1356 | Source->FindExternalLexicalDecls(this, Decls); |
| 1357 | |
| 1358 | if (Decls.empty()) |
| 1359 | return false; |
| 1360 | |
| 1361 | |
| 1362 | |
| 1363 | bool FieldsAlreadyLoaded = false; |
| 1364 | if (const auto *RD = dyn_cast<RecordDecl>(this)) |
| 1365 | FieldsAlreadyLoaded = RD->hasLoadedFieldsFromExternalStorage(); |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | Decl *ExternalFirst, *ExternalLast; |
| 1370 | std::tie(ExternalFirst, ExternalLast) = |
| 1371 | BuildDeclChain(Decls, FieldsAlreadyLoaded); |
| 1372 | ExternalLast->NextInContextAndBits.setPointer(FirstDecl); |
| 1373 | FirstDecl = ExternalFirst; |
| 1374 | if (!LastDecl) |
| 1375 | LastDecl = ExternalLast; |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
| 1379 | DeclContext::lookup_result |
| 1380 | ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC, |
| 1381 | DeclarationName Name) { |
| 1382 | ASTContext &Context = DC->getParentASTContext(); |
| 1383 | StoredDeclsMap *Map; |
| 1384 | if (!(Map = DC->LookupPtr)) |
| 1385 | Map = DC->CreateStoredDeclsMap(Context); |
| 1386 | if (DC->hasNeedToReconcileExternalVisibleStorage()) |
| 1387 | DC->reconcileExternalVisibleStorage(); |
| 1388 | |
| 1389 | (*Map)[Name].removeExternalDecls(); |
| 1390 | |
| 1391 | return DeclContext::lookup_result(); |
| 1392 | } |
| 1393 | |
| 1394 | DeclContext::lookup_result |
| 1395 | ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC, |
| 1396 | DeclarationName Name, |
| 1397 | ArrayRef<NamedDecl*> Decls) { |
| 1398 | ASTContext &Context = DC->getParentASTContext(); |
| 1399 | StoredDeclsMap *Map; |
| 1400 | if (!(Map = DC->LookupPtr)) |
| 1401 | Map = DC->CreateStoredDeclsMap(Context); |
| 1402 | if (DC->hasNeedToReconcileExternalVisibleStorage()) |
| 1403 | DC->reconcileExternalVisibleStorage(); |
| 1404 | |
| 1405 | StoredDeclsList &List = (*Map)[Name]; |
| 1406 | List.replaceExternalDecls(Decls); |
| 1407 | return List.getLookupResult(); |
| 1408 | } |
| 1409 | |
| 1410 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
| 1411 | if (hasExternalLexicalStorage()) |
| 1412 | LoadLexicalDeclsFromExternalStorage(); |
| 1413 | return decl_iterator(FirstDecl); |
| 1414 | } |
| 1415 | |
| 1416 | bool DeclContext::decls_empty() const { |
| 1417 | if (hasExternalLexicalStorage()) |
| 1418 | LoadLexicalDeclsFromExternalStorage(); |
| 1419 | |
| 1420 | return !FirstDecl; |
| 1421 | } |
| 1422 | |
| 1423 | bool DeclContext::containsDecl(Decl *D) const { |
| 1424 | return (D->getLexicalDeclContext() == this && |
| 1425 | (D->NextInContextAndBits.getPointer() || D == LastDecl)); |
| 1426 | } |
| 1427 | |
| 1428 | bool DeclContext::containsDeclAndLoad(Decl *D) const { |
| 1429 | if (hasExternalLexicalStorage()) |
| 1430 | LoadLexicalDeclsFromExternalStorage(); |
| 1431 | return containsDecl(D); |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | static bool shouldBeHidden(NamedDecl *D) { |
| 1437 | |
| 1438 | if (!D->getDeclName()) |
| 1439 | return true; |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) || |
| 1444 | D->isTemplateParameter()) |
| 1445 | return true; |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | if ((D->isLocalExternDecl() || D->getFriendObjectKind()) && |
| 1450 | D != D->getCanonicalDecl()) |
| 1451 | return true; |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 1458 | return true; |
| 1459 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1460 | if (FD->isFunctionTemplateSpecialization()) |
| 1461 | return true; |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | if (isa<CXXDestructorDecl>(D) && D->isInvalidDecl()) |
| 1468 | return true; |
| 1469 | |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | void DeclContext::removeDecl(Decl *D) { |
| 1474 | assert(D->getLexicalDeclContext() == this && |
| 1475 | "decl being removed from non-lexical context"); |
| 1476 | assert((D->NextInContextAndBits.getPointer() || D == LastDecl) && |
| 1477 | "decl is not in decls list"); |
| 1478 | |
| 1479 | |
| 1480 | if (D == FirstDecl) { |
| 1481 | if (D == LastDecl) |
| 1482 | FirstDecl = LastDecl = nullptr; |
| 1483 | else |
| 1484 | FirstDecl = D->NextInContextAndBits.getPointer(); |
| 1485 | } else { |
| 1486 | for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) { |
| 1487 | assert(I && "decl not found in linked list"); |
| 1488 | if (I->NextInContextAndBits.getPointer() == D) { |
| 1489 | I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer()); |
| 1490 | if (D == LastDecl) LastDecl = I; |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | D->NextInContextAndBits.setPointer(nullptr); |
| 1498 | |
| 1499 | |
| 1500 | if (isa<NamedDecl>(D)) { |
| 1501 | auto *ND = cast<NamedDecl>(D); |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | if (shouldBeHidden(ND)) |
| 1506 | return; |
| 1507 | |
| 1508 | |
| 1509 | if (!ND->getDeclName()) |
| 1510 | return; |
| 1511 | |
| 1512 | auto *DC = D->getDeclContext(); |
| 1513 | do { |
| 1514 | StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr; |
| 1515 | if (Map) { |
| 1516 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 1517 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 1518 | Pos->second.remove(ND); |
| 1519 | } |
| 1520 | } while (DC->isTransparentContext() && (DC = DC->getParent())); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | void DeclContext::addHiddenDecl(Decl *D) { |
| 1525 | assert(D->getLexicalDeclContext() == this && |
| 1526 | "Decl inserted into wrong lexical context"); |
| 1527 | assert(!D->getNextDeclInContext() && D != LastDecl && |
| 1528 | "Decl already inserted into a DeclContext"); |
| 1529 | |
| 1530 | if (FirstDecl) { |
| 1531 | LastDecl->NextInContextAndBits.setPointer(D); |
| 1532 | LastDecl = D; |
| 1533 | } else { |
| 1534 | FirstDecl = LastDecl = D; |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | if (auto *Record = dyn_cast<CXXRecordDecl>(this)) |
| 1540 | Record->addedMember(D); |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | if (!D->isFromASTFile()) { |
| 1545 | if (auto *Import = dyn_cast<ImportDecl>(D)) |
| 1546 | D->getASTContext().addedLocalImportDecl(Import); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | void DeclContext::addDecl(Decl *D) { |
| 1551 | addHiddenDecl(D); |
| 1552 | |
| 1553 | if (auto *ND = dyn_cast<NamedDecl>(D)) |
| 1554 | ND->getDeclContext()->getPrimaryContext()-> |
| 1555 | makeDeclVisibleInContextWithFlags(ND, false, true); |
| 1556 | } |
| 1557 | |
| 1558 | void DeclContext::addDeclInternal(Decl *D) { |
| 1559 | addHiddenDecl(D); |
| 1560 | |
| 1561 | if (auto *ND = dyn_cast<NamedDecl>(D)) |
| 1562 | ND->getDeclContext()->getPrimaryContext()-> |
| 1563 | makeDeclVisibleInContextWithFlags(ND, true, true); |
| 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | |
| 1573 | StoredDeclsMap *DeclContext::buildLookup() { |
| 1574 | assert(this == getPrimaryContext() && "buildLookup called on non-primary DC"); |
| 1575 | |
| 1576 | if (!hasLazyLocalLexicalLookups() && |
| 1577 | !hasLazyExternalLexicalLookups()) |
| 1578 | return LookupPtr; |
| 1579 | |
| 1580 | SmallVector<DeclContext *, 2> Contexts; |
| 1581 | collectAllContexts(Contexts); |
| 1582 | |
| 1583 | if (hasLazyExternalLexicalLookups()) { |
| 1584 | setHasLazyExternalLexicalLookups(false); |
| 1585 | for (auto *DC : Contexts) { |
| 1586 | if (DC->hasExternalLexicalStorage()) { |
| 1587 | bool LoadedDecls = DC->LoadLexicalDeclsFromExternalStorage(); |
| 1588 | setHasLazyLocalLexicalLookups( |
| 1589 | hasLazyLocalLexicalLookups() | LoadedDecls ); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | if (!hasLazyLocalLexicalLookups()) |
| 1594 | return LookupPtr; |
| 1595 | } |
| 1596 | |
| 1597 | for (auto *DC : Contexts) |
| 1598 | buildLookupImpl(DC, hasExternalVisibleStorage()); |
| 1599 | |
| 1600 | |
| 1601 | setHasLazyLocalLexicalLookups(false); |
| 1602 | return LookupPtr; |
| 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) { |
| 1610 | for (auto *D : DCtx->noload_decls()) { |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | if (auto *ND = dyn_cast<NamedDecl>(D)) |
| 1620 | if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) && |
| 1621 | (!ND->isFromASTFile() || |
| 1622 | (isTranslationUnit() && |
| 1623 | !getParentASTContext().getLangOpts().CPlusPlus))) |
| 1624 | makeDeclVisibleInContextImpl(ND, Internal); |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | if (auto *InnerCtx = dyn_cast<DeclContext>(D)) |
| 1630 | if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) |
| 1631 | buildLookupImpl(InnerCtx, Internal); |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | DeclContext::lookup_result |
| 1636 | DeclContext::lookup(DeclarationName Name) const { |
| 1637 | assert(getDeclKind() != Decl::LinkageSpec && |
| 1638 | getDeclKind() != Decl::Export && |
| 1639 | "should not perform lookups into transparent contexts"); |
| 1640 | |
| 1641 | const DeclContext *PrimaryContext = getPrimaryContext(); |
| 1642 | if (PrimaryContext != this) |
| 5 | | Assuming the condition is false | |
|
| |
| 1643 | return PrimaryContext->lookup(Name); |
| 1644 | |
| 1645 | |
| 1646 | |
| 1647 | |
| 1648 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 7 | | 'Source' initialized here | |
|
| 1649 | if (Source) |
| 8 | | Assuming 'Source' is null | |
|
| |
| 1650 | (void)cast<Decl>(this)->getMostRecentDecl(); |
| 1651 | |
| 1652 | if (hasExternalVisibleStorage()) { |
| 10 | | Assuming the condition is true | |
|
| |
| 1653 | assert(Source && "external visible storage but no external source?"); |
| 1654 | |
| 1655 | if (hasNeedToReconcileExternalVisibleStorage()) |
| 12 | | Assuming the condition is false | |
|
| |
| 1656 | reconcileExternalVisibleStorage(); |
| 1657 | |
| 1658 | StoredDeclsMap *Map = LookupPtr; |
| 1659 | |
| 1660 | if (hasLazyLocalLexicalLookups() || |
| 14 | | Assuming the condition is false | |
|
| |
| 1661 | hasLazyExternalLexicalLookups()) |
| 15 | | Assuming the condition is false | |
|
| 1662 | |
| 1663 | Map = const_cast<DeclContext*>(this)->buildLookup(); |
| 1664 | |
| 1665 | if (!Map) |
| 17 | | Assuming 'Map' is non-null | |
|
| |
| 1666 | Map = CreateStoredDeclsMap(getParentASTContext()); |
| 1667 | |
| 1668 | |
| 1669 | std::pair<StoredDeclsMap::iterator, bool> R = |
| 1670 | Map->insert(std::make_pair(Name, StoredDeclsList())); |
| 1671 | if (!R.second && !R.first->second.hasExternalDecls()) |
| 19 | | Assuming field 'second' is true | |
|
| 1672 | return R.first->second.getLookupResult(); |
| 1673 | |
| 1674 | if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) { |
| 20 | | Called C++ object pointer is null |
|
| 1675 | if (StoredDeclsMap *Map = LookupPtr) { |
| 1676 | StoredDeclsMap::iterator I = Map->find(Name); |
| 1677 | if (I != Map->end()) |
| 1678 | return I->second.getLookupResult(); |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | return {}; |
| 1683 | } |
| 1684 | |
| 1685 | StoredDeclsMap *Map = LookupPtr; |
| 1686 | if (hasLazyLocalLexicalLookups() || |
| 1687 | hasLazyExternalLexicalLookups()) |
| 1688 | Map = const_cast<DeclContext*>(this)->buildLookup(); |
| 1689 | |
| 1690 | if (!Map) |
| 1691 | return {}; |
| 1692 | |
| 1693 | StoredDeclsMap::iterator I = Map->find(Name); |
| 1694 | if (I == Map->end()) |
| 1695 | return {}; |
| 1696 | |
| 1697 | return I->second.getLookupResult(); |
| 1698 | } |
| 1699 | |
| 1700 | DeclContext::lookup_result |
| 1701 | DeclContext::noload_lookup(DeclarationName Name) { |
| 1702 | assert(getDeclKind() != Decl::LinkageSpec && |
| 1703 | getDeclKind() != Decl::Export && |
| 1704 | "should not perform lookups into transparent contexts"); |
| 1705 | |
| 1706 | DeclContext *PrimaryContext = getPrimaryContext(); |
| 1707 | if (PrimaryContext != this) |
| 1708 | return PrimaryContext->noload_lookup(Name); |
| 1709 | |
| 1710 | loadLazyLocalLexicalLookups(); |
| 1711 | StoredDeclsMap *Map = LookupPtr; |
| 1712 | if (!Map) |
| 1713 | return {}; |
| 1714 | |
| 1715 | StoredDeclsMap::iterator I = Map->find(Name); |
| 1716 | return I != Map->end() ? I->second.getLookupResult() |
| 1717 | : lookup_result(); |
| 1718 | } |
| 1719 | |
| 1720 | |
| 1721 | |
| 1722 | |
| 1723 | void DeclContext::loadLazyLocalLexicalLookups() { |
| 1724 | if (hasLazyLocalLexicalLookups()) { |
| 1725 | SmallVector<DeclContext *, 2> Contexts; |
| 1726 | collectAllContexts(Contexts); |
| 1727 | for (auto *Context : Contexts) |
| 1728 | buildLookupImpl(Context, hasExternalVisibleStorage()); |
| 1729 | setHasLazyLocalLexicalLookups(false); |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | void DeclContext::localUncachedLookup(DeclarationName Name, |
| 1734 | SmallVectorImpl<NamedDecl *> &Results) { |
| 1735 | Results.clear(); |
| 1736 | |
| 1737 | |
| 1738 | |
| 1739 | if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) { |
| 1 | Assuming the condition is true | |
|
| 2 | | Assuming the condition is true | |
|
| |
| 1740 | lookup_result LookupResults = lookup(Name); |
| 4 | | Calling 'DeclContext::lookup' | |
|
| 1741 | Results.insert(Results.end(), LookupResults.begin(), LookupResults.end()); |
| 1742 | return; |
| 1743 | } |
| 1744 | |
| 1745 | |
| 1746 | |
| 1747 | if (Name && !hasLazyLocalLexicalLookups() && |
| 1748 | !hasLazyExternalLexicalLookups()) { |
| 1749 | if (StoredDeclsMap *Map = LookupPtr) { |
| 1750 | StoredDeclsMap::iterator Pos = Map->find(Name); |
| 1751 | if (Pos != Map->end()) { |
| 1752 | Results.insert(Results.end(), |
| 1753 | Pos->second.getLookupResult().begin(), |
| 1754 | Pos->second.getLookupResult().end()); |
| 1755 | return; |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | |
| 1761 | |
| 1762 | |
| 1763 | |
| 1764 | for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) { |
| 1765 | if (auto *ND = dyn_cast<NamedDecl>(D)) |
| 1766 | if (ND->getDeclName() == Name) |
| 1767 | Results.push_back(ND); |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | DeclContext *DeclContext::getRedeclContext() { |
| 1772 | DeclContext *Ctx = this; |
| 1773 | |
| 1774 | |
| 1775 | |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | bool SkipRecords = getDeclKind() == Decl::Kind::Enum && |
| 1780 | !getParentASTContext().getLangOpts().CPlusPlus; |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | while ((SkipRecords && Ctx->isRecord()) || Ctx->isTransparentContext()) |
| 1785 | Ctx = Ctx->getParent(); |
| 1786 | return Ctx; |
| 1787 | } |
| 1788 | |
| 1789 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 1790 | DeclContext *Ctx = this; |
| 1791 | |
| 1792 | while (!Ctx->isFileContext()) |
| 1793 | Ctx = Ctx->getParent(); |
| 1794 | return Ctx->getPrimaryContext(); |
| 1795 | } |
| 1796 | |
| 1797 | RecordDecl *DeclContext::getOuterLexicalRecordContext() { |
| 1798 | |
| 1799 | RecordDecl *OutermostRD = nullptr; |
| 1800 | DeclContext *DC = this; |
| 1801 | while (DC->isRecord()) { |
| 1802 | OutermostRD = cast<RecordDecl>(DC); |
| 1803 | DC = DC->getLexicalParent(); |
| 1804 | } |
| 1805 | return OutermostRD; |
| 1806 | } |
| 1807 | |
| 1808 | bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const { |
| 1809 | |
| 1810 | if (!isFileContext()) |
| 1811 | return O->Equals(this); |
| 1812 | |
| 1813 | do { |
| 1814 | if (O->Equals(this)) |
| 1815 | return true; |
| 1816 | |
| 1817 | const auto *NS = dyn_cast<NamespaceDecl>(O); |
| 1818 | if (!NS || !NS->isInline()) |
| 1819 | break; |
| 1820 | O = NS->getParent(); |
| 1821 | } while (O); |
| 1822 | |
| 1823 | return false; |
| 1824 | } |
| 1825 | |
| 1826 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
| 1827 | DeclContext *PrimaryDC = this->getPrimaryContext(); |
| 1828 | DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext(); |
| 1829 | |
| 1830 | |
| 1831 | PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC); |
| 1832 | } |
| 1833 | |
| 1834 | void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, |
| 1835 | bool Recoverable) { |
| 1836 | assert(this == getPrimaryContext() && "expected a primary DC"); |
| 1837 | |
| 1838 | if (!isLookupContext()) { |
| 1839 | if (isTransparentContext()) |
| 1840 | getParent()->getPrimaryContext() |
| 1841 | ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable); |
| 1842 | return; |
| 1843 | } |
| 1844 | |
| 1845 | |
| 1846 | if (shouldBeHidden(D)) |
| 1847 | return; |
| 1848 | |
| 1849 | |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | |
| 1857 | if (LookupPtr || hasExternalVisibleStorage() || |
| 1858 | ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) && |
| 1859 | (getParentASTContext().getLangOpts().CPlusPlus || |
| 1860 | !isTranslationUnit()))) { |
| 1861 | |
| 1862 | |
| 1863 | |
| 1864 | buildLookup(); |
| 1865 | makeDeclVisibleInContextImpl(D, Internal); |
| 1866 | } else { |
| 1867 | setHasLazyLocalLexicalLookups(true); |
| 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | if (isTransparentContext() || isInlineNamespace()) |
| 1873 | getParent()->getPrimaryContext()-> |
| 1874 | makeDeclVisibleInContextWithFlags(D, Internal, Recoverable); |
| 1875 | |
| 1876 | auto *DCAsDecl = cast<Decl>(this); |
| 1877 | |
| 1878 | if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined())) |
| 1879 | if (ASTMutationListener *L = DCAsDecl->getASTMutationListener()) |
| 1880 | L->AddedVisibleDecl(this, D); |
| 1881 | } |
| 1882 | |
| 1883 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) { |
| 1884 | |
| 1885 | StoredDeclsMap *Map = LookupPtr; |
| 1886 | if (!Map) { |
| 1887 | ASTContext *C = &getParentASTContext(); |
| 1888 | Map = CreateStoredDeclsMap(*C); |
| 1889 | } |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | if (!Internal) |
| 1896 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 1897 | if (hasExternalVisibleStorage() && |
| 1898 | Map->find(D->getDeclName()) == Map->end()) |
| 1899 | Source->FindExternalVisibleDeclsByName(this, D->getDeclName()); |
| 1900 | |
| 1901 | |
| 1902 | StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()]; |
| 1903 | |
| 1904 | if (Internal) { |
| 1905 | |
| 1906 | |
| 1907 | |
| 1908 | |
| 1909 | DeclNameEntries.setHasExternalDecls(); |
| 1910 | DeclNameEntries.prependDeclNoReplace(D); |
| 1911 | return; |
| 1912 | } |
| 1913 | |
| 1914 | DeclNameEntries.addOrReplaceDecl(D); |
| 1915 | } |
| 1916 | |
| 1917 | UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const { |
| 1918 | return cast<UsingDirectiveDecl>(*I); |
| 1919 | } |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | DeclContext::udir_range DeclContext::using_directives() const { |
| 1924 | |
| 1925 | |
| 1926 | lookup_result Result = lookup(UsingDirectiveDecl::getName()); |
| 1927 | return udir_range(Result.begin(), Result.end()); |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | |
| 1934 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 1935 | assert(!LookupPtr && "context already has a decls map"); |
| 1936 | assert(getPrimaryContext() == this && |
| 1937 | "creating decls map on non-primary context"); |
| 1938 | |
| 1939 | StoredDeclsMap *M; |
| 1940 | bool Dependent = isDependentContext(); |
| 1941 | if (Dependent) |
| 1942 | M = new DependentStoredDeclsMap(); |
| 1943 | else |
| 1944 | M = new StoredDeclsMap(); |
| 1945 | M->Previous = C.LastSDM; |
| 1946 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 1947 | LookupPtr = M; |
| 1948 | return M; |
| 1949 | } |
| 1950 | |
| 1951 | void ASTContext::ReleaseDeclContextMaps() { |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 1956 | } |
| 1957 | |
| 1958 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 1959 | while (Map) { |
| 1960 | |
| 1961 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 1962 | |
| 1963 | if (Dependent) |
| 1964 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 1965 | else |
| 1966 | delete Map; |
| 1967 | |
| 1968 | Map = Next.getPointer(); |
| 1969 | Dependent = Next.getInt(); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 1974 | DeclContext *Parent, |
| 1975 | const PartialDiagnostic &PDiag) { |
| 1976 | assert(Parent->isDependentContext() |
| 1977 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 1978 | Parent = Parent->getPrimaryContext(); |
| 1979 | if (!Parent->LookupPtr) |
| 1980 | Parent->CreateStoredDeclsMap(C); |
| 1981 | |
| 1982 | auto *Map = static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr); |
| 1983 | |
| 1984 | |
| 1985 | |
| 1986 | DiagnosticStorage *DiagStorage = nullptr; |
| 1987 | if (PDiag.hasStorage()) |
| 1988 | DiagStorage = new (C) DiagnosticStorage; |
| 1989 | |
| 1990 | auto *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
| 1991 | |
| 1992 | |
| 1993 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1994 | Map->FirstDiagnostic = DD; |
| 1995 | |
| 1996 | return DD; |
| 1997 | } |