1 //===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the ASTReader::ReadDeclRecord method, which is the 11 // entrypoint for loading a decl. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/DeclGroup.h" 20 #include "clang/AST/DeclTemplate.h" 21 #include "clang/AST/DeclVisitor.h" 22 #include "clang/AST/Expr.h" 23 #include "clang/Sema/IdentifierResolver.h" 24 #include "clang/Sema/SemaDiagnostic.h" 25 #include "clang/Serialization/ASTReader.h" 26 #include "llvm/Support/SaveAndRestore.h" 27 28 using namespace clang; 29 using namespace clang::serialization; 30 31 //===----------------------------------------------------------------------===// 32 // Declaration deserialization 33 //===----------------------------------------------------------------------===// 34 35 namespace clang { 36 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> { 37 ASTReader &Reader; 38 ASTRecordReader &Record; 39 ASTReader::RecordLocation Loc; 40 const DeclID ThisDeclID; 41 const SourceLocation ThisDeclLoc; 42 typedef ASTReader::RecordData RecordData; 43 TypeID TypeIDForTypeDecl; 44 unsigned AnonymousDeclNumber; 45 GlobalDeclID NamedDeclForTagDecl; 46 IdentifierInfo *TypedefNameForLinkage; 47 48 bool HasPendingBody; 49 50 ///\brief A flag to carry the information for a decl from the entity is 51 /// used. We use it to delay the marking of the canonical decl as used until 52 /// the entire declaration is deserialized and merged. 53 bool IsDeclMarkedUsed; 54 55 uint64_t GetCurrentCursorOffset(); 56 57 uint64_t ReadLocalOffset() { 58 uint64_t LocalOffset = Record.readInt(); 59 assert(LocalOffset < Loc.Offset && "offset point after current record"); 60 return LocalOffset ? Loc.Offset - LocalOffset : 0; 61 } 62 63 uint64_t ReadGlobalOffset() { 64 uint64_t Local = ReadLocalOffset(); 65 return Local ? Record.getGlobalBitOffset(Local) : 0; 66 } 67 68 SourceLocation ReadSourceLocation() { 69 return Record.readSourceLocation(); 70 } 71 72 SourceRange ReadSourceRange() { 73 return Record.readSourceRange(); 74 } 75 76 TypeSourceInfo *GetTypeSourceInfo() { 77 return Record.getTypeSourceInfo(); 78 } 79 80 serialization::DeclID ReadDeclID() { 81 return Record.readDeclID(); 82 } 83 84 std::string ReadString() { 85 return Record.readString(); 86 } 87 88 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) { 89 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I) 90 IDs.push_back(ReadDeclID()); 91 } 92 93 Decl *ReadDecl() { 94 return Record.readDecl(); 95 } 96 97 template<typename T> 98 T *ReadDeclAs() { 99 return Record.readDeclAs<T>(); 100 } 101 102 void ReadQualifierInfo(QualifierInfo &Info) { 103 Record.readQualifierInfo(Info); 104 } 105 106 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) { 107 Record.readDeclarationNameLoc(DNLoc, Name); 108 } 109 110 serialization::SubmoduleID readSubmoduleID() { 111 if (Record.getIdx() == Record.size()) 112 return 0; 113 114 return Record.getGlobalSubmoduleID(Record.readInt()); 115 } 116 117 Module *readModule() { 118 return Record.getSubmodule(readSubmoduleID()); 119 } 120 121 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update); 122 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data, 123 const CXXRecordDecl *D); 124 void MergeDefinitionData(CXXRecordDecl *D, 125 struct CXXRecordDecl::DefinitionData &&NewDD); 126 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data); 127 void MergeDefinitionData(ObjCInterfaceDecl *D, 128 struct ObjCInterfaceDecl::DefinitionData &&NewDD); 129 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data); 130 void MergeDefinitionData(ObjCProtocolDecl *D, 131 struct ObjCProtocolDecl::DefinitionData &&NewDD); 132 133 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader, 134 DeclContext *DC, 135 unsigned Index); 136 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC, 137 unsigned Index, NamedDecl *D); 138 139 /// Results from loading a RedeclarableDecl. 140 class RedeclarableResult { 141 Decl *MergeWith; 142 GlobalDeclID FirstID; 143 bool IsKeyDecl; 144 145 public: 146 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl) 147 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {} 148 149 /// \brief Retrieve the first ID. 150 GlobalDeclID getFirstID() const { return FirstID; } 151 152 /// \brief Is this declaration a key declaration? 153 bool isKeyDecl() const { return IsKeyDecl; } 154 155 /// \brief Get a known declaration that this should be merged with, if 156 /// any. 157 Decl *getKnownMergeTarget() const { return MergeWith; } 158 }; 159 160 /// \brief Class used to capture the result of searching for an existing 161 /// declaration of a specific kind and name, along with the ability 162 /// to update the place where this result was found (the declaration 163 /// chain hanging off an identifier or the DeclContext we searched in) 164 /// if requested. 165 class FindExistingResult { 166 ASTReader &Reader; 167 NamedDecl *New; 168 NamedDecl *Existing; 169 bool AddResult; 170 171 unsigned AnonymousDeclNumber; 172 IdentifierInfo *TypedefNameForLinkage; 173 174 void operator=(FindExistingResult &&) = delete; 175 176 public: 177 FindExistingResult(ASTReader &Reader) 178 : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false), 179 AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {} 180 181 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing, 182 unsigned AnonymousDeclNumber, 183 IdentifierInfo *TypedefNameForLinkage) 184 : Reader(Reader), New(New), Existing(Existing), AddResult(true), 185 AnonymousDeclNumber(AnonymousDeclNumber), 186 TypedefNameForLinkage(TypedefNameForLinkage) {} 187 188 FindExistingResult(FindExistingResult &&Other) 189 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing), 190 AddResult(Other.AddResult), 191 AnonymousDeclNumber(Other.AnonymousDeclNumber), 192 TypedefNameForLinkage(Other.TypedefNameForLinkage) { 193 Other.AddResult = false; 194 } 195 196 ~FindExistingResult(); 197 198 /// \brief Suppress the addition of this result into the known set of 199 /// names. 200 void suppress() { AddResult = false; } 201 202 operator NamedDecl*() const { return Existing; } 203 204 template<typename T> 205 operator T*() const { return dyn_cast_or_null<T>(Existing); } 206 }; 207 208 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader, 209 DeclContext *DC); 210 FindExistingResult findExisting(NamedDecl *D); 211 212 public: 213 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record, 214 ASTReader::RecordLocation Loc, 215 DeclID thisDeclID, SourceLocation ThisDeclLoc) 216 : Reader(Reader), Record(Record), Loc(Loc), 217 ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc), 218 TypeIDForTypeDecl(0), NamedDeclForTagDecl(0), 219 TypedefNameForLinkage(nullptr), HasPendingBody(false), 220 IsDeclMarkedUsed(false) {} 221 222 template <typename T> static 223 void AddLazySpecializations(T *D, 224 SmallVectorImpl<serialization::DeclID>& IDs) { 225 if (IDs.empty()) 226 return; 227 228 // FIXME: We should avoid this pattern of getting the ASTContext. 229 ASTContext &C = D->getASTContext(); 230 231 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations; 232 233 if (auto &Old = LazySpecializations) { 234 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]); 235 std::sort(IDs.begin(), IDs.end()); 236 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end()); 237 } 238 239 auto *Result = new (C) serialization::DeclID[1 + IDs.size()]; 240 *Result = IDs.size(); 241 std::copy(IDs.begin(), IDs.end(), Result + 1); 242 243 LazySpecializations = Result; 244 } 245 246 template <typename DeclT> 247 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D); 248 static Decl *getMostRecentDeclImpl(...); 249 static Decl *getMostRecentDecl(Decl *D); 250 251 template <typename DeclT> 252 static void attachPreviousDeclImpl(ASTReader &Reader, 253 Redeclarable<DeclT> *D, Decl *Previous, 254 Decl *Canon); 255 static void attachPreviousDeclImpl(ASTReader &Reader, ...); 256 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous, 257 Decl *Canon); 258 259 template <typename DeclT> 260 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest); 261 static void attachLatestDeclImpl(...); 262 static void attachLatestDecl(Decl *D, Decl *latest); 263 264 template <typename DeclT> 265 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D); 266 static void markIncompleteDeclChainImpl(...); 267 268 /// \brief Determine whether this declaration has a pending body. 269 bool hasPendingBody() const { return HasPendingBody; } 270 271 void ReadFunctionDefinition(FunctionDecl *FD); 272 void Visit(Decl *D); 273 274 void UpdateDecl(Decl *D, llvm::SmallVectorImpl<serialization::DeclID>&); 275 276 static void setNextObjCCategory(ObjCCategoryDecl *Cat, 277 ObjCCategoryDecl *Next) { 278 Cat->NextClassCategory = Next; 279 } 280 281 void VisitDecl(Decl *D); 282 void VisitPragmaCommentDecl(PragmaCommentDecl *D); 283 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D); 284 void VisitTranslationUnitDecl(TranslationUnitDecl *TU); 285 void VisitNamedDecl(NamedDecl *ND); 286 void VisitLabelDecl(LabelDecl *LD); 287 void VisitNamespaceDecl(NamespaceDecl *D); 288 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 289 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 290 void VisitTypeDecl(TypeDecl *TD); 291 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD); 292 void VisitTypedefDecl(TypedefDecl *TD); 293 void VisitTypeAliasDecl(TypeAliasDecl *TD); 294 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 295 RedeclarableResult VisitTagDecl(TagDecl *TD); 296 void VisitEnumDecl(EnumDecl *ED); 297 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD); 298 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); } 299 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D); 300 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); } 301 RedeclarableResult VisitClassTemplateSpecializationDeclImpl( 302 ClassTemplateSpecializationDecl *D); 303 void VisitClassTemplateSpecializationDecl( 304 ClassTemplateSpecializationDecl *D) { 305 VisitClassTemplateSpecializationDeclImpl(D); 306 } 307 void VisitClassTemplatePartialSpecializationDecl( 308 ClassTemplatePartialSpecializationDecl *D); 309 void VisitClassScopeFunctionSpecializationDecl( 310 ClassScopeFunctionSpecializationDecl *D); 311 RedeclarableResult 312 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D); 313 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) { 314 VisitVarTemplateSpecializationDeclImpl(D); 315 } 316 void VisitVarTemplatePartialSpecializationDecl( 317 VarTemplatePartialSpecializationDecl *D); 318 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 319 void VisitValueDecl(ValueDecl *VD); 320 void VisitEnumConstantDecl(EnumConstantDecl *ECD); 321 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 322 void VisitDeclaratorDecl(DeclaratorDecl *DD); 323 void VisitFunctionDecl(FunctionDecl *FD); 324 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD); 325 void VisitCXXMethodDecl(CXXMethodDecl *D); 326 void VisitCXXConstructorDecl(CXXConstructorDecl *D); 327 void VisitCXXDestructorDecl(CXXDestructorDecl *D); 328 void VisitCXXConversionDecl(CXXConversionDecl *D); 329 void VisitFieldDecl(FieldDecl *FD); 330 void VisitMSPropertyDecl(MSPropertyDecl *FD); 331 void VisitIndirectFieldDecl(IndirectFieldDecl *FD); 332 RedeclarableResult VisitVarDeclImpl(VarDecl *D); 333 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); } 334 void VisitImplicitParamDecl(ImplicitParamDecl *PD); 335 void VisitParmVarDecl(ParmVarDecl *PD); 336 void VisitDecompositionDecl(DecompositionDecl *DD); 337 void VisitBindingDecl(BindingDecl *BD); 338 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 339 DeclID VisitTemplateDecl(TemplateDecl *D); 340 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D); 341 void VisitClassTemplateDecl(ClassTemplateDecl *D); 342 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); 343 void VisitVarTemplateDecl(VarTemplateDecl *D); 344 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 345 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 346 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); 347 void VisitUsingDecl(UsingDecl *D); 348 void VisitUsingPackDecl(UsingPackDecl *D); 349 void VisitUsingShadowDecl(UsingShadowDecl *D); 350 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D); 351 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 352 void VisitExportDecl(ExportDecl *D); 353 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD); 354 void VisitImportDecl(ImportDecl *D); 355 void VisitAccessSpecDecl(AccessSpecDecl *D); 356 void VisitFriendDecl(FriendDecl *D); 357 void VisitFriendTemplateDecl(FriendTemplateDecl *D); 358 void VisitStaticAssertDecl(StaticAssertDecl *D); 359 void VisitBlockDecl(BlockDecl *BD); 360 void VisitCapturedDecl(CapturedDecl *CD); 361 void VisitEmptyDecl(EmptyDecl *D); 362 363 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC); 364 365 template<typename T> 366 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D); 367 368 template<typename T> 369 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl, 370 DeclID TemplatePatternID = 0); 371 372 template<typename T> 373 void mergeRedeclarable(Redeclarable<T> *D, T *Existing, 374 RedeclarableResult &Redecl, 375 DeclID TemplatePatternID = 0); 376 377 template<typename T> 378 void mergeMergeable(Mergeable<T> *D); 379 380 void mergeTemplatePattern(RedeclarableTemplateDecl *D, 381 RedeclarableTemplateDecl *Existing, 382 DeclID DsID, bool IsKeyDecl); 383 384 ObjCTypeParamList *ReadObjCTypeParamList(); 385 386 // FIXME: Reorder according to DeclNodes.td? 387 void VisitObjCMethodDecl(ObjCMethodDecl *D); 388 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); 389 void VisitObjCContainerDecl(ObjCContainerDecl *D); 390 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 391 void VisitObjCIvarDecl(ObjCIvarDecl *D); 392 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 393 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); 394 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 395 void VisitObjCImplDecl(ObjCImplDecl *D); 396 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 397 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 398 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 399 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 400 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 401 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D); 402 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D); 403 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D); 404 }; 405 } // end namespace clang 406 407 namespace { 408 /// Iterator over the redeclarations of a declaration that have already 409 /// been merged into the same redeclaration chain. 410 template<typename DeclT> 411 class MergedRedeclIterator { 412 DeclT *Start, *Canonical, *Current; 413 public: 414 MergedRedeclIterator() : Current(nullptr) {} 415 MergedRedeclIterator(DeclT *Start) 416 : Start(Start), Canonical(nullptr), Current(Start) {} 417 418 DeclT *operator*() { return Current; } 419 420 MergedRedeclIterator &operator++() { 421 if (Current->isFirstDecl()) { 422 Canonical = Current; 423 Current = Current->getMostRecentDecl(); 424 } else 425 Current = Current->getPreviousDecl(); 426 427 // If we started in the merged portion, we'll reach our start position 428 // eventually. Otherwise, we'll never reach it, but the second declaration 429 // we reached was the canonical declaration, so stop when we see that one 430 // again. 431 if (Current == Start || Current == Canonical) 432 Current = nullptr; 433 return *this; 434 } 435 436 friend bool operator!=(const MergedRedeclIterator &A, 437 const MergedRedeclIterator &B) { 438 return A.Current != B.Current; 439 } 440 }; 441 } // end anonymous namespace 442 443 template <typename DeclT> 444 static llvm::iterator_range<MergedRedeclIterator<DeclT>> 445 merged_redecls(DeclT *D) { 446 return llvm::make_range(MergedRedeclIterator<DeclT>(D), 447 MergedRedeclIterator<DeclT>()); 448 } 449 450 uint64_t ASTDeclReader::GetCurrentCursorOffset() { 451 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset; 452 } 453 454 void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) { 455 if (Record.readInt()) 456 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile; 457 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) { 458 CD->NumCtorInitializers = Record.readInt(); 459 if (CD->NumCtorInitializers) 460 CD->CtorInitializers = ReadGlobalOffset(); 461 } 462 // Store the offset of the body so we can lazily load it later. 463 Reader.PendingBodies[FD] = GetCurrentCursorOffset(); 464 HasPendingBody = true; 465 } 466 467 void ASTDeclReader::Visit(Decl *D) { 468 DeclVisitor<ASTDeclReader, void>::Visit(D); 469 470 // At this point we have deserialized and merged the decl and it is safe to 471 // update its canonical decl to signal that the entire entity is used. 472 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed; 473 IsDeclMarkedUsed = false; 474 475 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 476 if (DD->DeclInfo) { 477 DeclaratorDecl::ExtInfo *Info = 478 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>(); 479 Info->TInfo = GetTypeSourceInfo(); 480 } 481 else { 482 DD->DeclInfo = GetTypeSourceInfo(); 483 } 484 } 485 486 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { 487 // We have a fully initialized TypeDecl. Read its type now. 488 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull()); 489 490 // If this is a tag declaration with a typedef name for linkage, it's safe 491 // to load that typedef now. 492 if (NamedDeclForTagDecl) 493 cast<TagDecl>(D)->TypedefNameDeclOrQualifier = 494 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl)); 495 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 496 // if we have a fully initialized TypeDecl, we can safely read its type now. 497 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull(); 498 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 499 // FunctionDecl's body was written last after all other Stmts/Exprs. 500 // We only read it if FD doesn't already have a body (e.g., from another 501 // module). 502 // FIXME: Can we diagnose ODR violations somehow? 503 if (Record.readInt()) 504 ReadFunctionDefinition(FD); 505 } 506 } 507 508 void ASTDeclReader::VisitDecl(Decl *D) { 509 if (D->isTemplateParameter() || D->isTemplateParameterPack() || 510 isa<ParmVarDecl>(D)) { 511 // We don't want to deserialize the DeclContext of a template 512 // parameter or of a parameter of a function template immediately. These 513 // entities might be used in the formulation of its DeclContext (for 514 // example, a function parameter can be used in decltype() in trailing 515 // return type of the function). Use the translation unit DeclContext as a 516 // placeholder. 517 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID(); 518 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID(); 519 if (!LexicalDCIDForTemplateParmDecl) 520 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl; 521 Reader.addPendingDeclContextInfo(D, 522 SemaDCIDForTemplateParmDecl, 523 LexicalDCIDForTemplateParmDecl); 524 D->setDeclContext(Reader.getContext().getTranslationUnitDecl()); 525 } else { 526 DeclContext *SemaDC = ReadDeclAs<DeclContext>(); 527 DeclContext *LexicalDC = ReadDeclAs<DeclContext>(); 528 if (!LexicalDC) 529 LexicalDC = SemaDC; 530 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC); 531 // Avoid calling setLexicalDeclContext() directly because it uses 532 // Decl::getASTContext() internally which is unsafe during derialization. 533 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC, 534 Reader.getContext()); 535 } 536 D->setLocation(ThisDeclLoc); 537 D->setInvalidDecl(Record.readInt()); 538 if (Record.readInt()) { // hasAttrs 539 AttrVec Attrs; 540 Record.readAttributes(Attrs); 541 // Avoid calling setAttrs() directly because it uses Decl::getASTContext() 542 // internally which is unsafe during derialization. 543 D->setAttrsImpl(Attrs, Reader.getContext()); 544 } 545 D->setImplicit(Record.readInt()); 546 D->Used = Record.readInt(); 547 IsDeclMarkedUsed |= D->Used; 548 D->setReferenced(Record.readInt()); 549 D->setTopLevelDeclInObjCContainer(Record.readInt()); 550 D->setAccess((AccessSpecifier)Record.readInt()); 551 D->FromASTFile = true; 552 bool ModulePrivate = Record.readInt(); 553 554 // Determine whether this declaration is part of a (sub)module. If so, it 555 // may not yet be visible. 556 if (unsigned SubmoduleID = readSubmoduleID()) { 557 // Store the owning submodule ID in the declaration. 558 D->setModuleOwnershipKind( 559 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate 560 : Decl::ModuleOwnershipKind::VisibleWhenImported); 561 D->setOwningModuleID(SubmoduleID); 562 563 if (ModulePrivate) { 564 // Module-private declarations are never visible, so there is no work to 565 // do. 566 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) { 567 // If local visibility is being tracked, this declaration will become 568 // hidden and visible as the owning module does. 569 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) { 570 // Mark the declaration as visible when its owning module becomes visible. 571 if (Owner->NameVisibility == Module::AllVisible) 572 D->setVisibleDespiteOwningModule(); 573 else 574 Reader.HiddenNamesMap[Owner].push_back(D); 575 } 576 } else if (ModulePrivate) { 577 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate); 578 } 579 } 580 581 void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) { 582 VisitDecl(D); 583 D->setLocation(ReadSourceLocation()); 584 D->CommentKind = (PragmaMSCommentKind)Record.readInt(); 585 std::string Arg = ReadString(); 586 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size()); 587 D->getTrailingObjects<char>()[Arg.size()] = '\0'; 588 } 589 590 void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) { 591 VisitDecl(D); 592 D->setLocation(ReadSourceLocation()); 593 std::string Name = ReadString(); 594 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size()); 595 D->getTrailingObjects<char>()[Name.size()] = '\0'; 596 597 D->ValueStart = Name.size() + 1; 598 std::string Value = ReadString(); 599 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(), 600 Value.size()); 601 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0'; 602 } 603 604 void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { 605 llvm_unreachable("Translation units are not serialized"); 606 } 607 608 void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) { 609 VisitDecl(ND); 610 ND->setDeclName(Record.readDeclarationName()); 611 AnonymousDeclNumber = Record.readInt(); 612 } 613 614 void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) { 615 VisitNamedDecl(TD); 616 TD->setLocStart(ReadSourceLocation()); 617 // Delay type reading until after we have fully initialized the decl. 618 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt()); 619 } 620 621 ASTDeclReader::RedeclarableResult 622 ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) { 623 RedeclarableResult Redecl = VisitRedeclarable(TD); 624 VisitTypeDecl(TD); 625 TypeSourceInfo *TInfo = GetTypeSourceInfo(); 626 if (Record.readInt()) { // isModed 627 QualType modedT = Record.readType(); 628 TD->setModedTypeSourceInfo(TInfo, modedT); 629 } else 630 TD->setTypeSourceInfo(TInfo); 631 // Read and discard the declaration for which this is a typedef name for 632 // linkage, if it exists. We cannot rely on our type to pull in this decl, 633 // because it might have been merged with a type from another module and 634 // thus might not refer to our version of the declaration. 635 ReadDecl(); 636 return Redecl; 637 } 638 639 void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) { 640 RedeclarableResult Redecl = VisitTypedefNameDecl(TD); 641 mergeRedeclarable(TD, Redecl); 642 } 643 644 void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) { 645 RedeclarableResult Redecl = VisitTypedefNameDecl(TD); 646 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>()) 647 // Merged when we merge the template. 648 TD->setDescribedAliasTemplate(Template); 649 else 650 mergeRedeclarable(TD, Redecl); 651 } 652 653 ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) { 654 RedeclarableResult Redecl = VisitRedeclarable(TD); 655 VisitTypeDecl(TD); 656 657 TD->IdentifierNamespace = Record.readInt(); 658 TD->setTagKind((TagDecl::TagKind)Record.readInt()); 659 if (!isa<CXXRecordDecl>(TD)) 660 TD->setCompleteDefinition(Record.readInt()); 661 TD->setEmbeddedInDeclarator(Record.readInt()); 662 TD->setFreeStanding(Record.readInt()); 663 TD->setCompleteDefinitionRequired(Record.readInt()); 664 TD->setBraceRange(ReadSourceRange()); 665 666 switch (Record.readInt()) { 667 case 0: 668 break; 669 case 1: { // ExtInfo 670 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo(); 671 ReadQualifierInfo(*Info); 672 TD->TypedefNameDeclOrQualifier = Info; 673 break; 674 } 675 case 2: // TypedefNameForAnonDecl 676 NamedDeclForTagDecl = ReadDeclID(); 677 TypedefNameForLinkage = Record.getIdentifierInfo(); 678 break; 679 default: 680 llvm_unreachable("unexpected tag info kind"); 681 } 682 683 if (!isa<CXXRecordDecl>(TD)) 684 mergeRedeclarable(TD, Redecl); 685 return Redecl; 686 } 687 688 void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) { 689 VisitTagDecl(ED); 690 if (TypeSourceInfo *TI = GetTypeSourceInfo()) 691 ED->setIntegerTypeSourceInfo(TI); 692 else 693 ED->setIntegerType(Record.readType()); 694 ED->setPromotionType(Record.readType()); 695 ED->setNumPositiveBits(Record.readInt()); 696 ED->setNumNegativeBits(Record.readInt()); 697 ED->IsScoped = Record.readInt(); 698 ED->IsScopedUsingClassTag = Record.readInt(); 699 ED->IsFixed = Record.readInt(); 700 701 // If this is a definition subject to the ODR, and we already have a 702 // definition, merge this one into it. 703 if (ED->IsCompleteDefinition && 704 Reader.getContext().getLangOpts().Modules && 705 Reader.getContext().getLangOpts().CPlusPlus) { 706 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()]; 707 if (!OldDef) { 708 // This is the first time we've seen an imported definition. Look for a 709 // local definition before deciding that we are the first definition. 710 for (auto *D : merged_redecls(ED->getCanonicalDecl())) { 711 if (!D->isFromASTFile() && D->isCompleteDefinition()) { 712 OldDef = D; 713 break; 714 } 715 } 716 } 717 if (OldDef) { 718 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef)); 719 ED->IsCompleteDefinition = false; 720 Reader.mergeDefinitionVisibility(OldDef, ED); 721 } else { 722 OldDef = ED; 723 } 724 } 725 726 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) { 727 TemplateSpecializationKind TSK = 728 (TemplateSpecializationKind)Record.readInt(); 729 SourceLocation POI = ReadSourceLocation(); 730 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK); 731 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI); 732 } 733 } 734 735 ASTDeclReader::RedeclarableResult 736 ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) { 737 RedeclarableResult Redecl = VisitTagDecl(RD); 738 RD->setHasFlexibleArrayMember(Record.readInt()); 739 RD->setAnonymousStructOrUnion(Record.readInt()); 740 RD->setHasObjectMember(Record.readInt()); 741 RD->setHasVolatileMember(Record.readInt()); 742 return Redecl; 743 } 744 745 void ASTDeclReader::VisitValueDecl(ValueDecl *VD) { 746 VisitNamedDecl(VD); 747 VD->setType(Record.readType()); 748 } 749 750 void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) { 751 VisitValueDecl(ECD); 752 if (Record.readInt()) 753 ECD->setInitExpr(Record.readExpr()); 754 ECD->setInitVal(Record.readAPSInt()); 755 mergeMergeable(ECD); 756 } 757 758 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) { 759 VisitValueDecl(DD); 760 DD->setInnerLocStart(ReadSourceLocation()); 761 if (Record.readInt()) { // hasExtInfo 762 DeclaratorDecl::ExtInfo *Info 763 = new (Reader.getContext()) DeclaratorDecl::ExtInfo(); 764 ReadQualifierInfo(*Info); 765 DD->DeclInfo = Info; 766 } 767 } 768 769 void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) { 770 RedeclarableResult Redecl = VisitRedeclarable(FD); 771 VisitDeclaratorDecl(FD); 772 773 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName()); 774 FD->IdentifierNamespace = Record.readInt(); 775 776 // FunctionDecl's body is handled last at ASTDeclReader::Visit, 777 // after everything else is read. 778 779 FD->SClass = (StorageClass)Record.readInt(); 780 FD->IsInline = Record.readInt(); 781 FD->IsInlineSpecified = Record.readInt(); 782 FD->IsExplicitSpecified = Record.readInt(); 783 FD->IsVirtualAsWritten = Record.readInt(); 784 FD->IsPure = Record.readInt(); 785 FD->HasInheritedPrototype = Record.readInt(); 786 FD->HasWrittenPrototype = Record.readInt(); 787 FD->IsDeleted = Record.readInt(); 788 FD->IsTrivial = Record.readInt(); 789 FD->IsDefaulted = Record.readInt(); 790 FD->IsExplicitlyDefaulted = Record.readInt(); 791 FD->HasImplicitReturnZero = Record.readInt(); 792 FD->IsConstexpr = Record.readInt(); 793 FD->UsesSEHTry = Record.readInt(); 794 FD->HasSkippedBody = Record.readInt(); 795 FD->IsLateTemplateParsed = Record.readInt(); 796 FD->setCachedLinkage(Linkage(Record.readInt())); 797 FD->EndRangeLoc = ReadSourceLocation(); 798 799 switch ((FunctionDecl::TemplatedKind)Record.readInt()) { 800 case FunctionDecl::TK_NonTemplate: 801 mergeRedeclarable(FD, Redecl); 802 break; 803 case FunctionDecl::TK_FunctionTemplate: 804 // Merged when we merge the template. 805 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>()); 806 break; 807 case FunctionDecl::TK_MemberSpecialization: { 808 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>(); 809 TemplateSpecializationKind TSK = 810 (TemplateSpecializationKind)Record.readInt(); 811 SourceLocation POI = ReadSourceLocation(); 812 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK); 813 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); 814 mergeRedeclarable(FD, Redecl); 815 break; 816 } 817 case FunctionDecl::TK_FunctionTemplateSpecialization: { 818 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>(); 819 TemplateSpecializationKind TSK = 820 (TemplateSpecializationKind)Record.readInt(); 821 822 // Template arguments. 823 SmallVector<TemplateArgument, 8> TemplArgs; 824 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true); 825 826 // Template args as written. 827 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs; 828 SourceLocation LAngleLoc, RAngleLoc; 829 bool HasTemplateArgumentsAsWritten = Record.readInt(); 830 if (HasTemplateArgumentsAsWritten) { 831 unsigned NumTemplateArgLocs = Record.readInt(); 832 TemplArgLocs.reserve(NumTemplateArgLocs); 833 for (unsigned i=0; i != NumTemplateArgLocs; ++i) 834 TemplArgLocs.push_back(Record.readTemplateArgumentLoc()); 835 836 LAngleLoc = ReadSourceLocation(); 837 RAngleLoc = ReadSourceLocation(); 838 } 839 840 SourceLocation POI = ReadSourceLocation(); 841 842 ASTContext &C = Reader.getContext(); 843 TemplateArgumentList *TemplArgList 844 = TemplateArgumentList::CreateCopy(C, TemplArgs); 845 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 846 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i) 847 TemplArgsInfo.addArgument(TemplArgLocs[i]); 848 FunctionTemplateSpecializationInfo *FTInfo 849 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK, 850 TemplArgList, 851 HasTemplateArgumentsAsWritten ? &TemplArgsInfo 852 : nullptr, 853 POI); 854 FD->TemplateOrSpecialization = FTInfo; 855 856 if (FD->isCanonicalDecl()) { // if canonical add to template's set. 857 // The template that contains the specializations set. It's not safe to 858 // use getCanonicalDecl on Template since it may still be initializing. 859 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>(); 860 // Get the InsertPos by FindNodeOrInsertPos() instead of calling 861 // InsertNode(FTInfo) directly to avoid the getASTContext() call in 862 // FunctionTemplateSpecializationInfo's Profile(). 863 // We avoid getASTContext because a decl in the parent hierarchy may 864 // be initializing. 865 llvm::FoldingSetNodeID ID; 866 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C); 867 void *InsertPos = nullptr; 868 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr(); 869 FunctionTemplateSpecializationInfo *ExistingInfo = 870 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos); 871 if (InsertPos) 872 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos); 873 else { 874 assert(Reader.getContext().getLangOpts().Modules && 875 "already deserialized this template specialization"); 876 mergeRedeclarable(FD, ExistingInfo->Function, Redecl); 877 } 878 } 879 break; 880 } 881 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { 882 // Templates. 883 UnresolvedSet<8> TemplDecls; 884 unsigned NumTemplates = Record.readInt(); 885 while (NumTemplates--) 886 TemplDecls.addDecl(ReadDeclAs<NamedDecl>()); 887 888 // Templates args. 889 TemplateArgumentListInfo TemplArgs; 890 unsigned NumArgs = Record.readInt(); 891 while (NumArgs--) 892 TemplArgs.addArgument(Record.readTemplateArgumentLoc()); 893 TemplArgs.setLAngleLoc(ReadSourceLocation()); 894 TemplArgs.setRAngleLoc(ReadSourceLocation()); 895 896 FD->setDependentTemplateSpecialization(Reader.getContext(), 897 TemplDecls, TemplArgs); 898 // These are not merged; we don't need to merge redeclarations of dependent 899 // template friends. 900 break; 901 } 902 } 903 904 // Read in the parameters. 905 unsigned NumParams = Record.readInt(); 906 SmallVector<ParmVarDecl *, 16> Params; 907 Params.reserve(NumParams); 908 for (unsigned I = 0; I != NumParams; ++I) 909 Params.push_back(ReadDeclAs<ParmVarDecl>()); 910 FD->setParams(Reader.getContext(), Params); 911 } 912 913 void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { 914 VisitNamedDecl(MD); 915 if (Record.readInt()) { 916 // Load the body on-demand. Most clients won't care, because method 917 // definitions rarely show up in headers. 918 Reader.PendingBodies[MD] = GetCurrentCursorOffset(); 919 HasPendingBody = true; 920 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>()); 921 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>()); 922 } 923 MD->setInstanceMethod(Record.readInt()); 924 MD->setVariadic(Record.readInt()); 925 MD->setPropertyAccessor(Record.readInt()); 926 MD->setDefined(Record.readInt()); 927 MD->IsOverriding = Record.readInt(); 928 MD->HasSkippedBody = Record.readInt(); 929 930 MD->IsRedeclaration = Record.readInt(); 931 MD->HasRedeclaration = Record.readInt(); 932 if (MD->HasRedeclaration) 933 Reader.getContext().setObjCMethodRedeclaration(MD, 934 ReadDeclAs<ObjCMethodDecl>()); 935 936 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt()); 937 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt()); 938 MD->SetRelatedResultType(Record.readInt()); 939 MD->setReturnType(Record.readType()); 940 MD->setReturnTypeSourceInfo(GetTypeSourceInfo()); 941 MD->DeclEndLoc = ReadSourceLocation(); 942 unsigned NumParams = Record.readInt(); 943 SmallVector<ParmVarDecl *, 16> Params; 944 Params.reserve(NumParams); 945 for (unsigned I = 0; I != NumParams; ++I) 946 Params.push_back(ReadDeclAs<ParmVarDecl>()); 947 948 MD->SelLocsKind = Record.readInt(); 949 unsigned NumStoredSelLocs = Record.readInt(); 950 SmallVector<SourceLocation, 16> SelLocs; 951 SelLocs.reserve(NumStoredSelLocs); 952 for (unsigned i = 0; i != NumStoredSelLocs; ++i) 953 SelLocs.push_back(ReadSourceLocation()); 954 955 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs); 956 } 957 958 void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { 959 VisitTypedefNameDecl(D); 960 961 D->Variance = Record.readInt(); 962 D->Index = Record.readInt(); 963 D->VarianceLoc = ReadSourceLocation(); 964 D->ColonLoc = ReadSourceLocation(); 965 } 966 967 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) { 968 VisitNamedDecl(CD); 969 CD->setAtStartLoc(ReadSourceLocation()); 970 CD->setAtEndRange(ReadSourceRange()); 971 } 972 973 ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() { 974 unsigned numParams = Record.readInt(); 975 if (numParams == 0) 976 return nullptr; 977 978 SmallVector<ObjCTypeParamDecl *, 4> typeParams; 979 typeParams.reserve(numParams); 980 for (unsigned i = 0; i != numParams; ++i) { 981 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>(); 982 if (!typeParam) 983 return nullptr; 984 985 typeParams.push_back(typeParam); 986 } 987 988 SourceLocation lAngleLoc = ReadSourceLocation(); 989 SourceLocation rAngleLoc = ReadSourceLocation(); 990 991 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc, 992 typeParams, rAngleLoc); 993 } 994 995 void ASTDeclReader::ReadObjCDefinitionData( 996 struct ObjCInterfaceDecl::DefinitionData &Data) { 997 // Read the superclass. 998 Data.SuperClassTInfo = GetTypeSourceInfo(); 999 1000 Data.EndLoc = ReadSourceLocation(); 1001 Data.HasDesignatedInitializers = Record.readInt(); 1002 1003 // Read the directly referenced protocols and their SourceLocations. 1004 unsigned NumProtocols = Record.readInt(); 1005 SmallVector<ObjCProtocolDecl *, 16> Protocols; 1006 Protocols.reserve(NumProtocols); 1007 for (unsigned I = 0; I != NumProtocols; ++I) 1008 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>()); 1009 SmallVector<SourceLocation, 16> ProtoLocs; 1010 ProtoLocs.reserve(NumProtocols); 1011 for (unsigned I = 0; I != NumProtocols; ++I) 1012 ProtoLocs.push_back(ReadSourceLocation()); 1013 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(), 1014 Reader.getContext()); 1015 1016 // Read the transitive closure of protocols referenced by this class. 1017 NumProtocols = Record.readInt(); 1018 Protocols.clear(); 1019 Protocols.reserve(NumProtocols); 1020 for (unsigned I = 0; I != NumProtocols; ++I) 1021 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>()); 1022 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols, 1023 Reader.getContext()); 1024 } 1025 1026 void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D, 1027 struct ObjCInterfaceDecl::DefinitionData &&NewDD) { 1028 // FIXME: odr checking? 1029 } 1030 1031 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) { 1032 RedeclarableResult Redecl = VisitRedeclarable(ID); 1033 VisitObjCContainerDecl(ID); 1034 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt()); 1035 mergeRedeclarable(ID, Redecl); 1036 1037 ID->TypeParamList = ReadObjCTypeParamList(); 1038 if (Record.readInt()) { 1039 // Read the definition. 1040 ID->allocateDefinitionData(); 1041 1042 ReadObjCDefinitionData(ID->data()); 1043 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl(); 1044 if (Canon->Data.getPointer()) { 1045 // If we already have a definition, keep the definition invariant and 1046 // merge the data. 1047 MergeDefinitionData(Canon, std::move(ID->data())); 1048 ID->Data = Canon->Data; 1049 } else { 1050 // Set the definition data of the canonical declaration, so other 1051 // redeclarations will see it. 1052 ID->getCanonicalDecl()->Data = ID->Data; 1053 1054 // We will rebuild this list lazily. 1055 ID->setIvarList(nullptr); 1056 } 1057 1058 // Note that we have deserialized a definition. 1059 Reader.PendingDefinitions.insert(ID); 1060 1061 // Note that we've loaded this Objective-C class. 1062 Reader.ObjCClassesLoaded.push_back(ID); 1063 } else { 1064 ID->Data = ID->getCanonicalDecl()->Data; 1065 } 1066 } 1067 1068 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) { 1069 VisitFieldDecl(IVD); 1070 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt()); 1071 // This field will be built lazily. 1072 IVD->setNextIvar(nullptr); 1073 bool synth = Record.readInt(); 1074 IVD->setSynthesize(synth); 1075 } 1076 1077 void ASTDeclReader::ReadObjCDefinitionData( 1078 struct ObjCProtocolDecl::DefinitionData &Data) { 1079 1080 unsigned NumProtoRefs = Record.readInt(); 1081 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; 1082 ProtoRefs.reserve(NumProtoRefs); 1083 for (unsigned I = 0; I != NumProtoRefs; ++I) 1084 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>()); 1085 SmallVector<SourceLocation, 16> ProtoLocs; 1086 ProtoLocs.reserve(NumProtoRefs); 1087 for (unsigned I = 0; I != NumProtoRefs; ++I) 1088 ProtoLocs.push_back(ReadSourceLocation()); 1089 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs, 1090 ProtoLocs.data(), Reader.getContext()); 1091 } 1092 1093 void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D, 1094 struct ObjCProtocolDecl::DefinitionData &&NewDD) { 1095 // FIXME: odr checking? 1096 } 1097 1098 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) { 1099 RedeclarableResult Redecl = VisitRedeclarable(PD); 1100 VisitObjCContainerDecl(PD); 1101 mergeRedeclarable(PD, Redecl); 1102 1103 if (Record.readInt()) { 1104 // Read the definition. 1105 PD->allocateDefinitionData(); 1106 1107 ReadObjCDefinitionData(PD->data()); 1108 1109 ObjCProtocolDecl *Canon = PD->getCanonicalDecl(); 1110 if (Canon->Data.getPointer()) { 1111 // If we already have a definition, keep the definition invariant and 1112 // merge the data. 1113 MergeDefinitionData(Canon, std::move(PD->data())); 1114 PD->Data = Canon->Data; 1115 } else { 1116 // Set the definition data of the canonical declaration, so other 1117 // redeclarations will see it. 1118 PD->getCanonicalDecl()->Data = PD->Data; 1119 } 1120 // Note that we have deserialized a definition. 1121 Reader.PendingDefinitions.insert(PD); 1122 } else { 1123 PD->Data = PD->getCanonicalDecl()->Data; 1124 } 1125 } 1126 1127 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) { 1128 VisitFieldDecl(FD); 1129 } 1130 1131 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) { 1132 VisitObjCContainerDecl(CD); 1133 CD->setCategoryNameLoc(ReadSourceLocation()); 1134 CD->setIvarLBraceLoc(ReadSourceLocation()); 1135 CD->setIvarRBraceLoc(ReadSourceLocation()); 1136 1137 // Note that this category has been deserialized. We do this before 1138 // deserializing the interface declaration, so that it will consider this 1139 /// category. 1140 Reader.CategoriesDeserialized.insert(CD); 1141 1142 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>(); 1143 CD->TypeParamList = ReadObjCTypeParamList(); 1144 unsigned NumProtoRefs = Record.readInt(); 1145 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; 1146 ProtoRefs.reserve(NumProtoRefs); 1147 for (unsigned I = 0; I != NumProtoRefs; ++I) 1148 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>()); 1149 SmallVector<SourceLocation, 16> ProtoLocs; 1150 ProtoLocs.reserve(NumProtoRefs); 1151 for (unsigned I = 0; I != NumProtoRefs; ++I) 1152 ProtoLocs.push_back(ReadSourceLocation()); 1153 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(), 1154 Reader.getContext()); 1155 } 1156 1157 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) { 1158 VisitNamedDecl(CAD); 1159 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>()); 1160 } 1161 1162 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 1163 VisitNamedDecl(D); 1164 D->setAtLoc(ReadSourceLocation()); 1165 D->setLParenLoc(ReadSourceLocation()); 1166 QualType T = Record.readType(); 1167 TypeSourceInfo *TSI = GetTypeSourceInfo(); 1168 D->setType(T, TSI); 1169 D->setPropertyAttributes( 1170 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt()); 1171 D->setPropertyAttributesAsWritten( 1172 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt()); 1173 D->setPropertyImplementation( 1174 (ObjCPropertyDecl::PropertyControl)Record.readInt()); 1175 DeclarationName GetterName = Record.readDeclarationName(); 1176 SourceLocation GetterLoc = ReadSourceLocation(); 1177 D->setGetterName(GetterName.getObjCSelector(), GetterLoc); 1178 DeclarationName SetterName = Record.readDeclarationName(); 1179 SourceLocation SetterLoc = ReadSourceLocation(); 1180 D->setSetterName(SetterName.getObjCSelector(), SetterLoc); 1181 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>()); 1182 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>()); 1183 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>()); 1184 } 1185 1186 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) { 1187 VisitObjCContainerDecl(D); 1188 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>()); 1189 } 1190 1191 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 1192 VisitObjCImplDecl(D); 1193 D->CategoryNameLoc = ReadSourceLocation(); 1194 } 1195 1196 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 1197 VisitObjCImplDecl(D); 1198 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>()); 1199 D->SuperLoc = ReadSourceLocation(); 1200 D->setIvarLBraceLoc(ReadSourceLocation()); 1201 D->setIvarRBraceLoc(ReadSourceLocation()); 1202 D->setHasNonZeroConstructors(Record.readInt()); 1203 D->setHasDestructors(Record.readInt()); 1204 D->NumIvarInitializers = Record.readInt(); 1205 if (D->NumIvarInitializers) 1206 D->IvarInitializers = ReadGlobalOffset(); 1207 } 1208 1209 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 1210 VisitDecl(D); 1211 D->setAtLoc(ReadSourceLocation()); 1212 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>()); 1213 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>(); 1214 D->IvarLoc = ReadSourceLocation(); 1215 D->setGetterCXXConstructor(Record.readExpr()); 1216 D->setSetterCXXAssignment(Record.readExpr()); 1217 } 1218 1219 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) { 1220 VisitDeclaratorDecl(FD); 1221 FD->Mutable = Record.readInt(); 1222 1223 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) { 1224 FD->InitStorage.setInt(ISK); 1225 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType 1226 ? Record.readType().getAsOpaquePtr() 1227 : Record.readExpr()); 1228 } 1229 1230 if (auto *BW = Record.readExpr()) 1231 FD->setBitWidth(BW); 1232 1233 if (!FD->getDeclName()) { 1234 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>()) 1235 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl); 1236 } 1237 mergeMergeable(FD); 1238 } 1239 1240 void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) { 1241 VisitDeclaratorDecl(PD); 1242 PD->GetterId = Record.getIdentifierInfo(); 1243 PD->SetterId = Record.getIdentifierInfo(); 1244 } 1245 1246 void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) { 1247 VisitValueDecl(FD); 1248 1249 FD->ChainingSize = Record.readInt(); 1250 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2"); 1251 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize]; 1252 1253 for (unsigned I = 0; I != FD->ChainingSize; ++I) 1254 FD->Chaining[I] = ReadDeclAs<NamedDecl>(); 1255 1256 mergeMergeable(FD); 1257 } 1258 1259 ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) { 1260 RedeclarableResult Redecl = VisitRedeclarable(VD); 1261 VisitDeclaratorDecl(VD); 1262 1263 VD->VarDeclBits.SClass = (StorageClass)Record.readInt(); 1264 VD->VarDeclBits.TSCSpec = Record.readInt(); 1265 VD->VarDeclBits.InitStyle = Record.readInt(); 1266 if (!isa<ParmVarDecl>(VD)) { 1267 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1268 Record.readInt(); 1269 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt(); 1270 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt(); 1271 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt(); 1272 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt(); 1273 VD->NonParmVarDeclBits.IsInline = Record.readInt(); 1274 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt(); 1275 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt(); 1276 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt(); 1277 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt(); 1278 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt(); 1279 } 1280 Linkage VarLinkage = Linkage(Record.readInt()); 1281 VD->setCachedLinkage(VarLinkage); 1282 1283 // Reconstruct the one piece of the IdentifierNamespace that we need. 1284 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage && 1285 VD->getLexicalDeclContext()->isFunctionOrMethod()) 1286 VD->setLocalExternDecl(); 1287 1288 if (uint64_t Val = Record.readInt()) { 1289 VD->setInit(Record.readExpr()); 1290 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3 1291 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt(); 1292 Eval->CheckedICE = true; 1293 Eval->IsICE = Val == 3; 1294 } 1295 } 1296 1297 if (VD->getStorageDuration() == SD_Static && Record.readInt()) 1298 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile; 1299 1300 enum VarKind { 1301 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization 1302 }; 1303 switch ((VarKind)Record.readInt()) { 1304 case VarNotTemplate: 1305 // Only true variables (not parameters or implicit parameters) can be 1306 // merged; the other kinds are not really redeclarable at all. 1307 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) && 1308 !isa<VarTemplateSpecializationDecl>(VD)) 1309 mergeRedeclarable(VD, Redecl); 1310 break; 1311 case VarTemplate: 1312 // Merged when we merge the template. 1313 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>()); 1314 break; 1315 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo. 1316 VarDecl *Tmpl = ReadDeclAs<VarDecl>(); 1317 TemplateSpecializationKind TSK = 1318 (TemplateSpecializationKind)Record.readInt(); 1319 SourceLocation POI = ReadSourceLocation(); 1320 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI); 1321 mergeRedeclarable(VD, Redecl); 1322 break; 1323 } 1324 } 1325 1326 return Redecl; 1327 } 1328 1329 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) { 1330 VisitVarDecl(PD); 1331 } 1332 1333 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) { 1334 VisitVarDecl(PD); 1335 unsigned isObjCMethodParam = Record.readInt(); 1336 unsigned scopeDepth = Record.readInt(); 1337 unsigned scopeIndex = Record.readInt(); 1338 unsigned declQualifier = Record.readInt(); 1339 if (isObjCMethodParam) { 1340 assert(scopeDepth == 0); 1341 PD->setObjCMethodScopeInfo(scopeIndex); 1342 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier; 1343 } else { 1344 PD->setScopeInfo(scopeDepth, scopeIndex); 1345 } 1346 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt(); 1347 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt(); 1348 if (Record.readInt()) // hasUninstantiatedDefaultArg. 1349 PD->setUninstantiatedDefaultArg(Record.readExpr()); 1350 1351 // FIXME: If this is a redeclaration of a function from another module, handle 1352 // inheritance of default arguments. 1353 } 1354 1355 void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) { 1356 VisitVarDecl(DD); 1357 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>(); 1358 for (unsigned I = 0; I != DD->NumBindings; ++I) 1359 BDs[I] = ReadDeclAs<BindingDecl>(); 1360 } 1361 1362 void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) { 1363 VisitValueDecl(BD); 1364 BD->Binding = Record.readExpr(); 1365 } 1366 1367 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) { 1368 VisitDecl(AD); 1369 AD->setAsmString(cast<StringLiteral>(Record.readExpr())); 1370 AD->setRParenLoc(ReadSourceLocation()); 1371 } 1372 1373 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) { 1374 VisitDecl(BD); 1375 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt())); 1376 BD->setSignatureAsWritten(GetTypeSourceInfo()); 1377 unsigned NumParams = Record.readInt(); 1378 SmallVector<ParmVarDecl *, 16> Params; 1379 Params.reserve(NumParams); 1380 for (unsigned I = 0; I != NumParams; ++I) 1381 Params.push_back(ReadDeclAs<ParmVarDecl>()); 1382 BD->setParams(Params); 1383 1384 BD->setIsVariadic(Record.readInt()); 1385 BD->setBlockMissingReturnType(Record.readInt()); 1386 BD->setIsConversionFromLambda(Record.readInt()); 1387 1388 bool capturesCXXThis = Record.readInt(); 1389 unsigned numCaptures = Record.readInt(); 1390 SmallVector<BlockDecl::Capture, 16> captures; 1391 captures.reserve(numCaptures); 1392 for (unsigned i = 0; i != numCaptures; ++i) { 1393 VarDecl *decl = ReadDeclAs<VarDecl>(); 1394 unsigned flags = Record.readInt(); 1395 bool byRef = (flags & 1); 1396 bool nested = (flags & 2); 1397 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr); 1398 1399 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr)); 1400 } 1401 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis); 1402 } 1403 1404 void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) { 1405 VisitDecl(CD); 1406 unsigned ContextParamPos = Record.readInt(); 1407 CD->setNothrow(Record.readInt() != 0); 1408 // Body is set by VisitCapturedStmt. 1409 for (unsigned I = 0; I < CD->NumParams; ++I) { 1410 if (I != ContextParamPos) 1411 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>()); 1412 else 1413 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>()); 1414 } 1415 } 1416 1417 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 1418 VisitDecl(D); 1419 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt()); 1420 D->setExternLoc(ReadSourceLocation()); 1421 D->setRBraceLoc(ReadSourceLocation()); 1422 } 1423 1424 void ASTDeclReader::VisitExportDecl(ExportDecl *D) { 1425 VisitDecl(D); 1426 D->RBraceLoc = ReadSourceLocation(); 1427 } 1428 1429 void ASTDeclReader::VisitLabelDecl(LabelDecl *D) { 1430 VisitNamedDecl(D); 1431 D->setLocStart(ReadSourceLocation()); 1432 } 1433 1434 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) { 1435 RedeclarableResult Redecl = VisitRedeclarable(D); 1436 VisitNamedDecl(D); 1437 D->setInline(Record.readInt()); 1438 D->LocStart = ReadSourceLocation(); 1439 D->RBraceLoc = ReadSourceLocation(); 1440 1441 // Defer loading the anonymous namespace until we've finished merging 1442 // this namespace; loading it might load a later declaration of the 1443 // same namespace, and we have an invariant that older declarations 1444 // get merged before newer ones try to merge. 1445 GlobalDeclID AnonNamespace = 0; 1446 if (Redecl.getFirstID() == ThisDeclID) { 1447 AnonNamespace = ReadDeclID(); 1448 } else { 1449 // Link this namespace back to the first declaration, which has already 1450 // been deserialized. 1451 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl()); 1452 } 1453 1454 mergeRedeclarable(D, Redecl); 1455 1456 if (AnonNamespace) { 1457 // Each module has its own anonymous namespace, which is disjoint from 1458 // any other module's anonymous namespaces, so don't attach the anonymous 1459 // namespace at all. 1460 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace)); 1461 if (!Record.isModule()) 1462 D->setAnonymousNamespace(Anon); 1463 } 1464 } 1465 1466 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 1467 RedeclarableResult Redecl = VisitRedeclarable(D); 1468 VisitNamedDecl(D); 1469 D->NamespaceLoc = ReadSourceLocation(); 1470 D->IdentLoc = ReadSourceLocation(); 1471 D->QualifierLoc = Record.readNestedNameSpecifierLoc(); 1472 D->Namespace = ReadDeclAs<NamedDecl>(); 1473 mergeRedeclarable(D, Redecl); 1474 } 1475 1476 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) { 1477 VisitNamedDecl(D); 1478 D->setUsingLoc(ReadSourceLocation()); 1479 D->QualifierLoc = Record.readNestedNameSpecifierLoc(); 1480 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName()); 1481 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>()); 1482 D->setTypename(Record.readInt()); 1483 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>()) 1484 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern); 1485 mergeMergeable(D); 1486 } 1487 1488 void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) { 1489 VisitNamedDecl(D); 1490 D->InstantiatedFrom = ReadDeclAs<NamedDecl>(); 1491 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>(); 1492 for (unsigned I = 0; I != D->NumExpansions; ++I) 1493 Expansions[I] = ReadDeclAs<NamedDecl>(); 1494 mergeMergeable(D); 1495 } 1496 1497 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) { 1498 RedeclarableResult Redecl = VisitRedeclarable(D); 1499 VisitNamedDecl(D); 1500 D->setTargetDecl(ReadDeclAs<NamedDecl>()); 1501 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(); 1502 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(); 1503 if (Pattern) 1504 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern); 1505 mergeRedeclarable(D, Redecl); 1506 } 1507 1508 void ASTDeclReader::VisitConstructorUsingShadowDecl( 1509 ConstructorUsingShadowDecl *D) { 1510 VisitUsingShadowDecl(D); 1511 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>(); 1512 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>(); 1513 D->IsVirtual = Record.readInt(); 1514 } 1515 1516 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 1517 VisitNamedDecl(D); 1518 D->UsingLoc = ReadSourceLocation(); 1519 D->NamespaceLoc = ReadSourceLocation(); 1520 D->QualifierLoc = Record.readNestedNameSpecifierLoc(); 1521 D->NominatedNamespace = ReadDeclAs<NamedDecl>(); 1522 D->CommonAncestor = ReadDeclAs<DeclContext>(); 1523 } 1524 1525 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1526 VisitValueDecl(D); 1527 D->setUsingLoc(ReadSourceLocation()); 1528 D->QualifierLoc = Record.readNestedNameSpecifierLoc(); 1529 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName()); 1530 D->EllipsisLoc = ReadSourceLocation(); 1531 mergeMergeable(D); 1532 } 1533 1534 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl( 1535 UnresolvedUsingTypenameDecl *D) { 1536 VisitTypeDecl(D); 1537 D->TypenameLocation = ReadSourceLocation(); 1538 D->QualifierLoc = Record.readNestedNameSpecifierLoc(); 1539 D->EllipsisLoc = ReadSourceLocation(); 1540 mergeMergeable(D); 1541 } 1542 1543 void ASTDeclReader::ReadCXXDefinitionData( 1544 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) { 1545 // Note: the caller has deserialized the IsLambda bit already. 1546 Data.UserDeclaredConstructor = Record.readInt(); 1547 Data.UserDeclaredSpecialMembers = Record.readInt(); 1548 Data.Aggregate = Record.readInt(); 1549 Data.PlainOldData = Record.readInt(); 1550 Data.Empty = Record.readInt(); 1551 Data.Polymorphic = Record.readInt(); 1552 Data.Abstract = Record.readInt(); 1553 Data.IsStandardLayout = Record.readInt(); 1554 Data.HasNoNonEmptyBases = Record.readInt(); 1555 Data.HasPrivateFields = Record.readInt(); 1556 Data.HasProtectedFields = Record.readInt(); 1557 Data.HasPublicFields = Record.readInt(); 1558 Data.HasMutableFields = Record.readInt(); 1559 Data.HasVariantMembers = Record.readInt(); 1560 Data.HasOnlyCMembers = Record.readInt(); 1561 Data.HasInClassInitializer = Record.readInt(); 1562 Data.HasUninitializedReferenceMember = Record.readInt(); 1563 Data.HasUninitializedFields = Record.readInt(); 1564 Data.HasInheritedConstructor = Record.readInt(); 1565 Data.HasInheritedAssignment = Record.readInt(); 1566 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt(); 1567 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt(); 1568 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt(); 1569 Data.NeedOverloadResolutionForDestructor = Record.readInt(); 1570 Data.DefaultedCopyConstructorIsDeleted = Record.readInt(); 1571 Data.DefaultedMoveConstructorIsDeleted = Record.readInt(); 1572 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt(); 1573 Data.DefaultedDestructorIsDeleted = Record.readInt(); 1574 Data.HasTrivialSpecialMembers = Record.readInt(); 1575 Data.DeclaredNonTrivialSpecialMembers = Record.readInt(); 1576 Data.HasIrrelevantDestructor = Record.readInt(); 1577 Data.HasConstexprNonCopyMoveConstructor = Record.readInt(); 1578 Data.HasDefaultedDefaultConstructor = Record.readInt(); 1579 Data.CanPassInRegisters = Record.readInt(); 1580 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt(); 1581 Data.HasConstexprDefaultConstructor = Record.readInt(); 1582 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt(); 1583 Data.ComputedVisibleConversions = Record.readInt(); 1584 Data.UserProvidedDefaultConstructor = Record.readInt(); 1585 Data.DeclaredSpecialMembers = Record.readInt(); 1586 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt(); 1587 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt(); 1588 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt(); 1589 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt(); 1590 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt(); 1591 Data.ODRHash = Record.readInt(); 1592 Data.HasODRHash = true; 1593 1594 if (Record.readInt()) 1595 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile; 1596 1597 Data.NumBases = Record.readInt(); 1598 if (Data.NumBases) 1599 Data.Bases = ReadGlobalOffset(); 1600 Data.NumVBases = Record.readInt(); 1601 if (Data.NumVBases) 1602 Data.VBases = ReadGlobalOffset(); 1603 1604 Record.readUnresolvedSet(Data.Conversions); 1605 Record.readUnresolvedSet(Data.VisibleConversions); 1606 assert(Data.Definition && "Data.Definition should be already set!"); 1607 Data.FirstFriend = ReadDeclID(); 1608 1609 if (Data.IsLambda) { 1610 typedef LambdaCapture Capture; 1611 CXXRecordDecl::LambdaDefinitionData &Lambda 1612 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data); 1613 Lambda.Dependent = Record.readInt(); 1614 Lambda.IsGenericLambda = Record.readInt(); 1615 Lambda.CaptureDefault = Record.readInt(); 1616 Lambda.NumCaptures = Record.readInt(); 1617 Lambda.NumExplicitCaptures = Record.readInt(); 1618 Lambda.ManglingNumber = Record.readInt(); 1619 Lambda.ContextDecl = ReadDeclID(); 1620 Lambda.Captures = (Capture *)Reader.getContext().Allocate( 1621 sizeof(Capture) * Lambda.NumCaptures); 1622 Capture *ToCapture = Lambda.Captures; 1623 Lambda.MethodTyInfo = GetTypeSourceInfo(); 1624 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { 1625 SourceLocation Loc = ReadSourceLocation(); 1626 bool IsImplicit = Record.readInt(); 1627 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt()); 1628 switch (Kind) { 1629 case LCK_StarThis: 1630 case LCK_This: 1631 case LCK_VLAType: 1632 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation()); 1633 break; 1634 case LCK_ByCopy: 1635 case LCK_ByRef: 1636 VarDecl *Var = ReadDeclAs<VarDecl>(); 1637 SourceLocation EllipsisLoc = ReadSourceLocation(); 1638 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc); 1639 break; 1640 } 1641 } 1642 } 1643 } 1644 1645 void ASTDeclReader::MergeDefinitionData( 1646 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) { 1647 assert(D->DefinitionData && 1648 "merging class definition into non-definition"); 1649 auto &DD = *D->DefinitionData; 1650 1651 if (DD.Definition != MergeDD.Definition) { 1652 // Track that we merged the definitions. 1653 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition, 1654 DD.Definition)); 1655 Reader.PendingDefinitions.erase(MergeDD.Definition); 1656 MergeDD.Definition->IsCompleteDefinition = false; 1657 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition); 1658 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() && 1659 "already loaded pending lookups for merged definition"); 1660 } 1661 1662 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD); 1663 if (PFDI != Reader.PendingFakeDefinitionData.end() && 1664 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) { 1665 // We faked up this definition data because we found a class for which we'd 1666 // not yet loaded the definition. Replace it with the real thing now. 1667 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?"); 1668 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded; 1669 1670 // Don't change which declaration is the definition; that is required 1671 // to be invariant once we select it. 1672 auto *Def = DD.Definition; 1673 DD = std::move(MergeDD); 1674 DD.Definition = Def; 1675 return; 1676 } 1677 1678 // FIXME: Move this out into a .def file? 1679 bool DetectedOdrViolation = false; 1680 #define OR_FIELD(Field) DD.Field |= MergeDD.Field; 1681 #define MATCH_FIELD(Field) \ 1682 DetectedOdrViolation |= DD.Field != MergeDD.Field; \ 1683 OR_FIELD(Field) 1684 MATCH_FIELD(UserDeclaredConstructor) 1685 MATCH_FIELD(UserDeclaredSpecialMembers) 1686 MATCH_FIELD(Aggregate) 1687 MATCH_FIELD(PlainOldData) 1688 MATCH_FIELD(Empty) 1689 MATCH_FIELD(Polymorphic) 1690 MATCH_FIELD(Abstract) 1691 MATCH_FIELD(IsStandardLayout) 1692 MATCH_FIELD(HasNoNonEmptyBases) 1693 MATCH_FIELD(HasPrivateFields) 1694 MATCH_FIELD(HasProtectedFields) 1695 MATCH_FIELD(HasPublicFields) 1696 MATCH_FIELD(HasMutableFields) 1697 MATCH_FIELD(HasVariantMembers) 1698 MATCH_FIELD(HasOnlyCMembers) 1699 MATCH_FIELD(HasInClassInitializer) 1700 MATCH_FIELD(HasUninitializedReferenceMember) 1701 MATCH_FIELD(HasUninitializedFields) 1702 MATCH_FIELD(HasInheritedConstructor) 1703 MATCH_FIELD(HasInheritedAssignment) 1704 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor) 1705 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor) 1706 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment) 1707 MATCH_FIELD(NeedOverloadResolutionForDestructor) 1708 MATCH_FIELD(DefaultedCopyConstructorIsDeleted) 1709 MATCH_FIELD(DefaultedMoveConstructorIsDeleted) 1710 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted) 1711 MATCH_FIELD(DefaultedDestructorIsDeleted) 1712 OR_FIELD(HasTrivialSpecialMembers) 1713 OR_FIELD(DeclaredNonTrivialSpecialMembers) 1714 MATCH_FIELD(HasIrrelevantDestructor) 1715 OR_FIELD(HasConstexprNonCopyMoveConstructor) 1716 OR_FIELD(HasDefaultedDefaultConstructor) 1717 MATCH_FIELD(CanPassInRegisters) 1718 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr) 1719 OR_FIELD(HasConstexprDefaultConstructor) 1720 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases) 1721 // ComputedVisibleConversions is handled below. 1722 MATCH_FIELD(UserProvidedDefaultConstructor) 1723 OR_FIELD(DeclaredSpecialMembers) 1724 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase) 1725 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase) 1726 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam) 1727 OR_FIELD(HasDeclaredCopyConstructorWithConstParam) 1728 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam) 1729 MATCH_FIELD(IsLambda) 1730 #undef OR_FIELD 1731 #undef MATCH_FIELD 1732 1733 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases) 1734 DetectedOdrViolation = true; 1735 // FIXME: Issue a diagnostic if the base classes don't match when we come 1736 // to lazily load them. 1737 1738 // FIXME: Issue a diagnostic if the list of conversion functions doesn't 1739 // match when we come to lazily load them. 1740 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) { 1741 DD.VisibleConversions = std::move(MergeDD.VisibleConversions); 1742 DD.ComputedVisibleConversions = true; 1743 } 1744 1745 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to 1746 // lazily load it. 1747 1748 if (DD.IsLambda) { 1749 // FIXME: ODR-checking for merging lambdas (this happens, for instance, 1750 // when they occur within the body of a function template specialization). 1751 } 1752 1753 if (D->getODRHash() != MergeDD.ODRHash) { 1754 DetectedOdrViolation = true; 1755 } 1756 1757 if (DetectedOdrViolation) 1758 Reader.PendingOdrMergeFailures[DD.Definition].push_back( 1759 {MergeDD.Definition, &MergeDD}); 1760 } 1761 1762 void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) { 1763 struct CXXRecordDecl::DefinitionData *DD; 1764 ASTContext &C = Reader.getContext(); 1765 1766 // Determine whether this is a lambda closure type, so that we can 1767 // allocate the appropriate DefinitionData structure. 1768 bool IsLambda = Record.readInt(); 1769 if (IsLambda) 1770 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false, 1771 LCD_None); 1772 else 1773 DD = new (C) struct CXXRecordDecl::DefinitionData(D); 1774 1775 ReadCXXDefinitionData(*DD, D); 1776 1777 // We might already have a definition for this record. This can happen either 1778 // because we're reading an update record, or because we've already done some 1779 // merging. Either way, just merge into it. 1780 CXXRecordDecl *Canon = D->getCanonicalDecl(); 1781 if (Canon->DefinitionData) { 1782 MergeDefinitionData(Canon, std::move(*DD)); 1783 D->DefinitionData = Canon->DefinitionData; 1784 return; 1785 } 1786 1787 // Mark this declaration as being a definition. 1788 D->IsCompleteDefinition = true; 1789 D->DefinitionData = DD; 1790 1791 // If this is not the first declaration or is an update record, we can have 1792 // other redeclarations already. Make a note that we need to propagate the 1793 // DefinitionData pointer onto them. 1794 if (Update || Canon != D) { 1795 Canon->DefinitionData = D->DefinitionData; 1796 Reader.PendingDefinitions.insert(D); 1797 } 1798 } 1799 1800 ASTDeclReader::RedeclarableResult 1801 ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) { 1802 RedeclarableResult Redecl = VisitRecordDeclImpl(D); 1803 1804 ASTContext &C = Reader.getContext(); 1805 1806 enum CXXRecKind { 1807 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization 1808 }; 1809 switch ((CXXRecKind)Record.readInt()) { 1810 case CXXRecNotTemplate: 1811 // Merged when we merge the folding set entry in the primary template. 1812 if (!isa<ClassTemplateSpecializationDecl>(D)) 1813 mergeRedeclarable(D, Redecl); 1814 break; 1815 case CXXRecTemplate: { 1816 // Merged when we merge the template. 1817 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>(); 1818 D->TemplateOrInstantiation = Template; 1819 if (!Template->getTemplatedDecl()) { 1820 // We've not actually loaded the ClassTemplateDecl yet, because we're 1821 // currently being loaded as its pattern. Rely on it to set up our 1822 // TypeForDecl (see VisitClassTemplateDecl). 1823 // 1824 // Beware: we do not yet know our canonical declaration, and may still 1825 // get merged once the surrounding class template has got off the ground. 1826 TypeIDForTypeDecl = 0; 1827 } 1828 break; 1829 } 1830 case CXXRecMemberSpecialization: { 1831 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(); 1832 TemplateSpecializationKind TSK = 1833 (TemplateSpecializationKind)Record.readInt(); 1834 SourceLocation POI = ReadSourceLocation(); 1835 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK); 1836 MSI->setPointOfInstantiation(POI); 1837 D->TemplateOrInstantiation = MSI; 1838 mergeRedeclarable(D, Redecl); 1839 break; 1840 } 1841 } 1842 1843 bool WasDefinition = Record.readInt(); 1844 if (WasDefinition) 1845 ReadCXXRecordDefinition(D, /*Update*/false); 1846 else 1847 // Propagate DefinitionData pointer from the canonical declaration. 1848 D->DefinitionData = D->getCanonicalDecl()->DefinitionData; 1849 1850 // Lazily load the key function to avoid deserializing every method so we can 1851 // compute it. 1852 if (WasDefinition) { 1853 DeclID KeyFn = ReadDeclID(); 1854 if (KeyFn && D->IsCompleteDefinition) 1855 // FIXME: This is wrong for the ARM ABI, where some other module may have 1856 // made this function no longer be a key function. We need an update 1857 // record or similar for that case. 1858 C.KeyFunctions[D] = KeyFn; 1859 } 1860 1861 return Redecl; 1862 } 1863 1864 void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { 1865 VisitFunctionDecl(D); 1866 } 1867 1868 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) { 1869 VisitFunctionDecl(D); 1870 1871 unsigned NumOverridenMethods = Record.readInt(); 1872 if (D->isCanonicalDecl()) { 1873 while (NumOverridenMethods--) { 1874 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod, 1875 // MD may be initializing. 1876 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>()) 1877 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl()); 1878 } 1879 } else { 1880 // We don't care about which declarations this used to override; we get 1881 // the relevant information from the canonical declaration. 1882 Record.skipInts(NumOverridenMethods); 1883 } 1884 } 1885 1886 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 1887 // We need the inherited constructor information to merge the declaration, 1888 // so we have to read it before we call VisitCXXMethodDecl. 1889 if (D->isInheritingConstructor()) { 1890 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>(); 1891 auto *Ctor = ReadDeclAs<CXXConstructorDecl>(); 1892 *D->getTrailingObjects<InheritedConstructor>() = 1893 InheritedConstructor(Shadow, Ctor); 1894 } 1895 1896 VisitCXXMethodDecl(D); 1897 } 1898 1899 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 1900 VisitCXXMethodDecl(D); 1901 1902 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) { 1903 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl()); 1904 auto *ThisArg = Record.readExpr(); 1905 // FIXME: Check consistency if we have an old and new operator delete. 1906 if (!Canon->OperatorDelete) { 1907 Canon->OperatorDelete = OperatorDelete; 1908 Canon->OperatorDeleteThisArg = ThisArg; 1909 } 1910 } 1911 } 1912 1913 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) { 1914 VisitCXXMethodDecl(D); 1915 } 1916 1917 void ASTDeclReader::VisitImportDecl(ImportDecl *D) { 1918 VisitDecl(D); 1919 D->ImportedAndComplete.setPointer(readModule()); 1920 D->ImportedAndComplete.setInt(Record.readInt()); 1921 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>(); 1922 for (unsigned I = 0, N = Record.back(); I != N; ++I) 1923 StoredLocs[I] = ReadSourceLocation(); 1924 Record.skipInts(1); // The number of stored source locations. 1925 } 1926 1927 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) { 1928 VisitDecl(D); 1929 D->setColonLoc(ReadSourceLocation()); 1930 } 1931 1932 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) { 1933 VisitDecl(D); 1934 if (Record.readInt()) // hasFriendDecl 1935 D->Friend = ReadDeclAs<NamedDecl>(); 1936 else 1937 D->Friend = GetTypeSourceInfo(); 1938 for (unsigned i = 0; i != D->NumTPLists; ++i) 1939 D->getTrailingObjects<TemplateParameterList *>()[i] = 1940 Record.readTemplateParameterList(); 1941 D->NextFriend = ReadDeclID(); 1942 D->UnsupportedFriend = (Record.readInt() != 0); 1943 D->FriendLoc = ReadSourceLocation(); 1944 } 1945 1946 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) { 1947 VisitDecl(D); 1948 unsigned NumParams = Record.readInt(); 1949 D->NumParams = NumParams; 1950 D->Params = new TemplateParameterList*[NumParams]; 1951 for (unsigned i = 0; i != NumParams; ++i) 1952 D->Params[i] = Record.readTemplateParameterList(); 1953 if (Record.readInt()) // HasFriendDecl 1954 D->Friend = ReadDeclAs<NamedDecl>(); 1955 else 1956 D->Friend = GetTypeSourceInfo(); 1957 D->FriendLoc = ReadSourceLocation(); 1958 } 1959 1960 DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) { 1961 VisitNamedDecl(D); 1962 1963 DeclID PatternID = ReadDeclID(); 1964 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID)); 1965 TemplateParameterList *TemplateParams = Record.readTemplateParameterList(); 1966 // FIXME handle associated constraints 1967 D->init(TemplatedDecl, TemplateParams); 1968 1969 return PatternID; 1970 } 1971 1972 ASTDeclReader::RedeclarableResult 1973 ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { 1974 RedeclarableResult Redecl = VisitRedeclarable(D); 1975 1976 // Make sure we've allocated the Common pointer first. We do this before 1977 // VisitTemplateDecl so that getCommonPtr() can be used during initialization. 1978 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl(); 1979 if (!CanonD->Common) { 1980 CanonD->Common = CanonD->newCommon(Reader.getContext()); 1981 Reader.PendingDefinitions.insert(CanonD); 1982 } 1983 D->Common = CanonD->Common; 1984 1985 // If this is the first declaration of the template, fill in the information 1986 // for the 'common' pointer. 1987 if (ThisDeclID == Redecl.getFirstID()) { 1988 if (RedeclarableTemplateDecl *RTD 1989 = ReadDeclAs<RedeclarableTemplateDecl>()) { 1990 assert(RTD->getKind() == D->getKind() && 1991 "InstantiatedFromMemberTemplate kind mismatch"); 1992 D->setInstantiatedFromMemberTemplate(RTD); 1993 if (Record.readInt()) 1994 D->setMemberSpecialization(); 1995 } 1996 } 1997 1998 DeclID PatternID = VisitTemplateDecl(D); 1999 D->IdentifierNamespace = Record.readInt(); 2000 2001 mergeRedeclarable(D, Redecl, PatternID); 2002 2003 // If we merged the template with a prior declaration chain, merge the common 2004 // pointer. 2005 // FIXME: Actually merge here, don't just overwrite. 2006 D->Common = D->getCanonicalDecl()->Common; 2007 2008 return Redecl; 2009 } 2010 2011 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { 2012 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 2013 2014 if (ThisDeclID == Redecl.getFirstID()) { 2015 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of 2016 // the specializations. 2017 SmallVector<serialization::DeclID, 32> SpecIDs; 2018 ReadDeclIDList(SpecIDs); 2019 ASTDeclReader::AddLazySpecializations(D, SpecIDs); 2020 } 2021 2022 if (D->getTemplatedDecl()->TemplateOrInstantiation) { 2023 // We were loaded before our templated declaration was. We've not set up 2024 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct 2025 // it now. 2026 Reader.getContext().getInjectedClassNameType( 2027 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization()); 2028 } 2029 } 2030 2031 void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { 2032 llvm_unreachable("BuiltinTemplates are not serialized"); 2033 } 2034 2035 /// TODO: Unify with ClassTemplateDecl version? 2036 /// May require unifying ClassTemplateDecl and 2037 /// VarTemplateDecl beyond TemplateDecl... 2038 void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) { 2039 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 2040 2041 if (ThisDeclID == Redecl.getFirstID()) { 2042 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of 2043 // the specializations. 2044 SmallVector<serialization::DeclID, 32> SpecIDs; 2045 ReadDeclIDList(SpecIDs); 2046 ASTDeclReader::AddLazySpecializations(D, SpecIDs); 2047 } 2048 } 2049 2050 ASTDeclReader::RedeclarableResult 2051 ASTDeclReader::VisitClassTemplateSpecializationDeclImpl( 2052 ClassTemplateSpecializationDecl *D) { 2053 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D); 2054 2055 ASTContext &C = Reader.getContext(); 2056 if (Decl *InstD = ReadDecl()) { 2057 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) { 2058 D->SpecializedTemplate = CTD; 2059 } else { 2060 SmallVector<TemplateArgument, 8> TemplArgs; 2061 Record.readTemplateArgumentList(TemplArgs); 2062 TemplateArgumentList *ArgList 2063 = TemplateArgumentList::CreateCopy(C, TemplArgs); 2064 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS 2065 = new (C) ClassTemplateSpecializationDecl:: 2066 SpecializedPartialSpecialization(); 2067 PS->PartialSpecialization 2068 = cast<ClassTemplatePartialSpecializationDecl>(InstD); 2069 PS->TemplateArgs = ArgList; 2070 D->SpecializedTemplate = PS; 2071 } 2072 } 2073 2074 SmallVector<TemplateArgument, 8> TemplArgs; 2075 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true); 2076 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs); 2077 D->PointOfInstantiation = ReadSourceLocation(); 2078 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt(); 2079 2080 bool writtenAsCanonicalDecl = Record.readInt(); 2081 if (writtenAsCanonicalDecl) { 2082 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(); 2083 if (D->isCanonicalDecl()) { // It's kept in the folding set. 2084 // Set this as, or find, the canonical declaration for this specialization 2085 ClassTemplateSpecializationDecl *CanonSpec; 2086 if (ClassTemplatePartialSpecializationDecl *Partial = 2087 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { 2088 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations 2089 .GetOrInsertNode(Partial); 2090 } else { 2091 CanonSpec = 2092 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D); 2093 } 2094 // If there was already a canonical specialization, merge into it. 2095 if (CanonSpec != D) { 2096 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl); 2097 2098 // This declaration might be a definition. Merge with any existing 2099 // definition. 2100 if (auto *DDD = D->DefinitionData) { 2101 if (CanonSpec->DefinitionData) 2102 MergeDefinitionData(CanonSpec, std::move(*DDD)); 2103 else 2104 CanonSpec->DefinitionData = D->DefinitionData; 2105 } 2106 D->DefinitionData = CanonSpec->DefinitionData; 2107 } 2108 } 2109 } 2110 2111 // Explicit info. 2112 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) { 2113 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo 2114 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo; 2115 ExplicitInfo->TypeAsWritten = TyInfo; 2116 ExplicitInfo->ExternLoc = ReadSourceLocation(); 2117 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(); 2118 D->ExplicitInfo = ExplicitInfo; 2119 } 2120 2121 return Redecl; 2122 } 2123 2124 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl( 2125 ClassTemplatePartialSpecializationDecl *D) { 2126 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D); 2127 2128 D->TemplateParams = Record.readTemplateParameterList(); 2129 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo(); 2130 2131 // These are read/set from/to the first declaration. 2132 if (ThisDeclID == Redecl.getFirstID()) { 2133 D->InstantiatedFromMember.setPointer( 2134 ReadDeclAs<ClassTemplatePartialSpecializationDecl>()); 2135 D->InstantiatedFromMember.setInt(Record.readInt()); 2136 } 2137 } 2138 2139 void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl( 2140 ClassScopeFunctionSpecializationDecl *D) { 2141 VisitDecl(D); 2142 D->Specialization = ReadDeclAs<CXXMethodDecl>(); 2143 } 2144 2145 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 2146 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 2147 2148 if (ThisDeclID == Redecl.getFirstID()) { 2149 // This FunctionTemplateDecl owns a CommonPtr; read it. 2150 SmallVector<serialization::DeclID, 32> SpecIDs; 2151 ReadDeclIDList(SpecIDs); 2152 ASTDeclReader::AddLazySpecializations(D, SpecIDs); 2153 } 2154 } 2155 2156 /// TODO: Unify with ClassTemplateSpecializationDecl version? 2157 /// May require unifying ClassTemplate(Partial)SpecializationDecl and 2158 /// VarTemplate(Partial)SpecializationDecl with a new data 2159 /// structure Template(Partial)SpecializationDecl, and 2160 /// using Template(Partial)SpecializationDecl as input type. 2161 ASTDeclReader::RedeclarableResult 2162 ASTDeclReader::VisitVarTemplateSpecializationDeclImpl( 2163 VarTemplateSpecializationDecl *D) { 2164 RedeclarableResult Redecl = VisitVarDeclImpl(D); 2165 2166 ASTContext &C = Reader.getContext(); 2167 if (Decl *InstD = ReadDecl()) { 2168 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) { 2169 D->SpecializedTemplate = VTD; 2170 } else { 2171 SmallVector<TemplateArgument, 8> TemplArgs; 2172 Record.readTemplateArgumentList(TemplArgs); 2173 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy( 2174 C, TemplArgs); 2175 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS = 2176 new (C) 2177 VarTemplateSpecializationDecl::SpecializedPartialSpecialization(); 2178 PS->PartialSpecialization = 2179 cast<VarTemplatePartialSpecializationDecl>(InstD); 2180 PS->TemplateArgs = ArgList; 2181 D->SpecializedTemplate = PS; 2182 } 2183 } 2184 2185 // Explicit info. 2186 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) { 2187 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo = 2188 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo; 2189 ExplicitInfo->TypeAsWritten = TyInfo; 2190 ExplicitInfo->ExternLoc = ReadSourceLocation(); 2191 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(); 2192 D->ExplicitInfo = ExplicitInfo; 2193 } 2194 2195 SmallVector<TemplateArgument, 8> TemplArgs; 2196 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true); 2197 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs); 2198 D->PointOfInstantiation = ReadSourceLocation(); 2199 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt(); 2200 2201 bool writtenAsCanonicalDecl = Record.readInt(); 2202 if (writtenAsCanonicalDecl) { 2203 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>(); 2204 if (D->isCanonicalDecl()) { // It's kept in the folding set. 2205 // FIXME: If it's already present, merge it. 2206 if (VarTemplatePartialSpecializationDecl *Partial = 2207 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) { 2208 CanonPattern->getCommonPtr()->PartialSpecializations 2209 .GetOrInsertNode(Partial); 2210 } else { 2211 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D); 2212 } 2213 } 2214 } 2215 2216 return Redecl; 2217 } 2218 2219 /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? 2220 /// May require unifying ClassTemplate(Partial)SpecializationDecl and 2221 /// VarTemplate(Partial)SpecializationDecl with a new data 2222 /// structure Template(Partial)SpecializationDecl, and 2223 /// using Template(Partial)SpecializationDecl as input type. 2224 void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl( 2225 VarTemplatePartialSpecializationDecl *D) { 2226 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D); 2227 2228 D->TemplateParams = Record.readTemplateParameterList(); 2229 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo(); 2230 2231 // These are read/set from/to the first declaration. 2232 if (ThisDeclID == Redecl.getFirstID()) { 2233 D->InstantiatedFromMember.setPointer( 2234 ReadDeclAs<VarTemplatePartialSpecializationDecl>()); 2235 D->InstantiatedFromMember.setInt(Record.readInt()); 2236 } 2237 } 2238 2239 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 2240 VisitTypeDecl(D); 2241 2242 D->setDeclaredWithTypename(Record.readInt()); 2243 2244 if (Record.readInt()) 2245 D->setDefaultArgument(GetTypeSourceInfo()); 2246 } 2247 2248 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 2249 VisitDeclaratorDecl(D); 2250 // TemplateParmPosition. 2251 D->setDepth(Record.readInt()); 2252 D->setPosition(Record.readInt()); 2253 if (D->isExpandedParameterPack()) { 2254 auto TypesAndInfos = 2255 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>(); 2256 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { 2257 new (&TypesAndInfos[I].first) QualType(Record.readType()); 2258 TypesAndInfos[I].second = GetTypeSourceInfo(); 2259 } 2260 } else { 2261 // Rest of NonTypeTemplateParmDecl. 2262 D->ParameterPack = Record.readInt(); 2263 if (Record.readInt()) 2264 D->setDefaultArgument(Record.readExpr()); 2265 } 2266 } 2267 2268 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 2269 VisitTemplateDecl(D); 2270 // TemplateParmPosition. 2271 D->setDepth(Record.readInt()); 2272 D->setPosition(Record.readInt()); 2273 if (D->isExpandedParameterPack()) { 2274 TemplateParameterList **Data = 2275 D->getTrailingObjects<TemplateParameterList *>(); 2276 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); 2277 I != N; ++I) 2278 Data[I] = Record.readTemplateParameterList(); 2279 } else { 2280 // Rest of TemplateTemplateParmDecl. 2281 D->ParameterPack = Record.readInt(); 2282 if (Record.readInt()) 2283 D->setDefaultArgument(Reader.getContext(), 2284 Record.readTemplateArgumentLoc()); 2285 } 2286 } 2287 2288 void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { 2289 VisitRedeclarableTemplateDecl(D); 2290 } 2291 2292 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) { 2293 VisitDecl(D); 2294 D->AssertExprAndFailed.setPointer(Record.readExpr()); 2295 D->AssertExprAndFailed.setInt(Record.readInt()); 2296 D->Message = cast_or_null<StringLiteral>(Record.readExpr()); 2297 D->RParenLoc = ReadSourceLocation(); 2298 } 2299 2300 void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) { 2301 VisitDecl(D); 2302 } 2303 2304 std::pair<uint64_t, uint64_t> 2305 ASTDeclReader::VisitDeclContext(DeclContext *DC) { 2306 uint64_t LexicalOffset = ReadLocalOffset(); 2307 uint64_t VisibleOffset = ReadLocalOffset(); 2308 return std::make_pair(LexicalOffset, VisibleOffset); 2309 } 2310 2311 template <typename T> 2312 ASTDeclReader::RedeclarableResult 2313 ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) { 2314 DeclID FirstDeclID = ReadDeclID(); 2315 Decl *MergeWith = nullptr; 2316 2317 bool IsKeyDecl = ThisDeclID == FirstDeclID; 2318 bool IsFirstLocalDecl = false; 2319 2320 uint64_t RedeclOffset = 0; 2321 2322 // 0 indicates that this declaration was the only declaration of its entity, 2323 // and is used for space optimization. 2324 if (FirstDeclID == 0) { 2325 FirstDeclID = ThisDeclID; 2326 IsKeyDecl = true; 2327 IsFirstLocalDecl = true; 2328 } else if (unsigned N = Record.readInt()) { 2329 // This declaration was the first local declaration, but may have imported 2330 // other declarations. 2331 IsKeyDecl = N == 1; 2332 IsFirstLocalDecl = true; 2333 2334 // We have some declarations that must be before us in our redeclaration 2335 // chain. Read them now, and remember that we ought to merge with one of 2336 // them. 2337 // FIXME: Provide a known merge target to the second and subsequent such 2338 // declaration. 2339 for (unsigned I = 0; I != N - 1; ++I) 2340 MergeWith = ReadDecl(); 2341 2342 RedeclOffset = ReadLocalOffset(); 2343 } else { 2344 // This declaration was not the first local declaration. Read the first 2345 // local declaration now, to trigger the import of other redeclarations. 2346 (void)ReadDecl(); 2347 } 2348 2349 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID)); 2350 if (FirstDecl != D) { 2351 // We delay loading of the redeclaration chain to avoid deeply nested calls. 2352 // We temporarily set the first (canonical) declaration as the previous one 2353 // which is the one that matters and mark the real previous DeclID to be 2354 // loaded & attached later on. 2355 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl); 2356 D->First = FirstDecl->getCanonicalDecl(); 2357 } 2358 2359 T *DAsT = static_cast<T*>(D); 2360 2361 // Note that we need to load local redeclarations of this decl and build a 2362 // decl chain for them. This must happen *after* we perform the preloading 2363 // above; this ensures that the redeclaration chain is built in the correct 2364 // order. 2365 if (IsFirstLocalDecl) 2366 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset)); 2367 2368 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl); 2369 } 2370 2371 /// \brief Attempts to merge the given declaration (D) with another declaration 2372 /// of the same entity. 2373 template<typename T> 2374 void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, 2375 RedeclarableResult &Redecl, 2376 DeclID TemplatePatternID) { 2377 // If modules are not available, there is no reason to perform this merge. 2378 if (!Reader.getContext().getLangOpts().Modules) 2379 return; 2380 2381 // If we're not the canonical declaration, we don't need to merge. 2382 if (!DBase->isFirstDecl()) 2383 return; 2384 2385 T *D = static_cast<T*>(DBase); 2386 2387 if (auto *Existing = Redecl.getKnownMergeTarget()) 2388 // We already know of an existing declaration we should merge with. 2389 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID); 2390 else if (FindExistingResult ExistingRes = findExisting(D)) 2391 if (T *Existing = ExistingRes) 2392 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID); 2393 } 2394 2395 /// \brief "Cast" to type T, asserting if we don't have an implicit conversion. 2396 /// We use this to put code in a template that will only be valid for certain 2397 /// instantiations. 2398 template<typename T> static T assert_cast(T t) { return t; } 2399 template<typename T> static T assert_cast(...) { 2400 llvm_unreachable("bad assert_cast"); 2401 } 2402 2403 /// \brief Merge together the pattern declarations from two template 2404 /// declarations. 2405 void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D, 2406 RedeclarableTemplateDecl *Existing, 2407 DeclID DsID, bool IsKeyDecl) { 2408 auto *DPattern = D->getTemplatedDecl(); 2409 auto *ExistingPattern = Existing->getTemplatedDecl(); 2410 RedeclarableResult Result(/*MergeWith*/ ExistingPattern, 2411 DPattern->getCanonicalDecl()->getGlobalID(), 2412 IsKeyDecl); 2413 2414 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) { 2415 // Merge with any existing definition. 2416 // FIXME: This is duplicated in several places. Refactor. 2417 auto *ExistingClass = 2418 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl(); 2419 if (auto *DDD = DClass->DefinitionData) { 2420 if (ExistingClass->DefinitionData) { 2421 MergeDefinitionData(ExistingClass, std::move(*DDD)); 2422 } else { 2423 ExistingClass->DefinitionData = DClass->DefinitionData; 2424 // We may have skipped this before because we thought that DClass 2425 // was the canonical declaration. 2426 Reader.PendingDefinitions.insert(DClass); 2427 } 2428 } 2429 DClass->DefinitionData = ExistingClass->DefinitionData; 2430 2431 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern), 2432 Result); 2433 } 2434 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern)) 2435 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern), 2436 Result); 2437 if (auto *DVar = dyn_cast<VarDecl>(DPattern)) 2438 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result); 2439 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern)) 2440 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern), 2441 Result); 2442 llvm_unreachable("merged an unknown kind of redeclarable template"); 2443 } 2444 2445 /// \brief Attempts to merge the given declaration (D) with another declaration 2446 /// of the same entity. 2447 template<typename T> 2448 void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing, 2449 RedeclarableResult &Redecl, 2450 DeclID TemplatePatternID) { 2451 T *D = static_cast<T*>(DBase); 2452 T *ExistingCanon = Existing->getCanonicalDecl(); 2453 T *DCanon = D->getCanonicalDecl(); 2454 if (ExistingCanon != DCanon) { 2455 assert(DCanon->getGlobalID() == Redecl.getFirstID() && 2456 "already merged this declaration"); 2457 2458 // Have our redeclaration link point back at the canonical declaration 2459 // of the existing declaration, so that this declaration has the 2460 // appropriate canonical declaration. 2461 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon); 2462 D->First = ExistingCanon; 2463 ExistingCanon->Used |= D->Used; 2464 D->Used = false; 2465 2466 // When we merge a namespace, update its pointer to the first namespace. 2467 // We cannot have loaded any redeclarations of this declaration yet, so 2468 // there's nothing else that needs to be updated. 2469 if (auto *Namespace = dyn_cast<NamespaceDecl>(D)) 2470 Namespace->AnonOrFirstNamespaceAndInline.setPointer( 2471 assert_cast<NamespaceDecl*>(ExistingCanon)); 2472 2473 // When we merge a template, merge its pattern. 2474 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D)) 2475 mergeTemplatePattern( 2476 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon), 2477 TemplatePatternID, Redecl.isKeyDecl()); 2478 2479 // If this declaration is a key declaration, make a note of that. 2480 if (Redecl.isKeyDecl()) 2481 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID()); 2482 } 2483 } 2484 2485 /// \brief Attempts to merge the given declaration (D) with another declaration 2486 /// of the same entity, for the case where the entity is not actually 2487 /// redeclarable. This happens, for instance, when merging the fields of 2488 /// identical class definitions from two different modules. 2489 template<typename T> 2490 void ASTDeclReader::mergeMergeable(Mergeable<T> *D) { 2491 // If modules are not available, there is no reason to perform this merge. 2492 if (!Reader.getContext().getLangOpts().Modules) 2493 return; 2494 2495 // ODR-based merging is only performed in C++. In C, identically-named things 2496 // in different translation units are not redeclarations (but may still have 2497 // compatible types). 2498 if (!Reader.getContext().getLangOpts().CPlusPlus) 2499 return; 2500 2501 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D))) 2502 if (T *Existing = ExistingRes) 2503 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D), 2504 Existing->getCanonicalDecl()); 2505 } 2506 2507 void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 2508 VisitDecl(D); 2509 unsigned NumVars = D->varlist_size(); 2510 SmallVector<Expr *, 16> Vars; 2511 Vars.reserve(NumVars); 2512 for (unsigned i = 0; i != NumVars; ++i) { 2513 Vars.push_back(Record.readExpr()); 2514 } 2515 D->setVars(Vars); 2516 } 2517 2518 void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { 2519 VisitValueDecl(D); 2520 D->setLocation(ReadSourceLocation()); 2521 D->setCombiner(Record.readExpr()); 2522 D->setInitializer( 2523 Record.readExpr(), 2524 static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt())); 2525 D->PrevDeclInScope = ReadDeclID(); 2526 } 2527 2528 void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { 2529 VisitVarDecl(D); 2530 } 2531 2532 //===----------------------------------------------------------------------===// 2533 // Attribute Reading 2534 //===----------------------------------------------------------------------===// 2535 2536 /// \brief Reads attributes from the current stream position. 2537 void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) { 2538 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) { 2539 Attr *New = nullptr; 2540 attr::Kind Kind = (attr::Kind)Record.readInt(); 2541 SourceRange Range = Record.readSourceRange(); 2542 ASTContext &Context = getContext(); 2543 2544 #include "clang/Serialization/AttrPCHRead.inc" 2545 2546 assert(New && "Unable to decode attribute?"); 2547 Attrs.push_back(New); 2548 } 2549 } 2550 2551 //===----------------------------------------------------------------------===// 2552 // ASTReader Implementation 2553 //===----------------------------------------------------------------------===// 2554 2555 /// \brief Note that we have loaded the declaration with the given 2556 /// Index. 2557 /// 2558 /// This routine notes that this declaration has already been loaded, 2559 /// so that future GetDecl calls will return this declaration rather 2560 /// than trying to load a new declaration. 2561 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) { 2562 assert(!DeclsLoaded[Index] && "Decl loaded twice?"); 2563 DeclsLoaded[Index] = D; 2564 } 2565 2566 2567 /// \brief Determine whether the consumer will be interested in seeing 2568 /// this declaration (via HandleTopLevelDecl). 2569 /// 2570 /// This routine should return true for anything that might affect 2571 /// code generation, e.g., inline function definitions, Objective-C 2572 /// declarations with metadata, etc. 2573 static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) { 2574 // An ObjCMethodDecl is never considered as "interesting" because its 2575 // implementation container always is. 2576 2577 // An ImportDecl or VarDecl imported from a module map module will get 2578 // emitted when we import the relevant module. 2579 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) { 2580 auto *M = D->getImportedOwningModule(); 2581 if (M && M->Kind == Module::ModuleMapModule && 2582 Ctx.DeclMustBeEmitted(D)) 2583 return false; 2584 } 2585 2586 if (isa<FileScopeAsmDecl>(D) || 2587 isa<ObjCProtocolDecl>(D) || 2588 isa<ObjCImplDecl>(D) || 2589 isa<ImportDecl>(D) || 2590 isa<PragmaCommentDecl>(D) || 2591 isa<PragmaDetectMismatchDecl>(D)) 2592 return true; 2593 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D)) 2594 return !D->getDeclContext()->isFunctionOrMethod(); 2595 if (VarDecl *Var = dyn_cast<VarDecl>(D)) 2596 return Var->isFileVarDecl() && 2597 Var->isThisDeclarationADefinition() == VarDecl::Definition; 2598 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) 2599 return Func->doesThisDeclarationHaveABody() || HasBody; 2600 2601 if (auto *ES = D->getASTContext().getExternalSource()) 2602 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) 2603 return true; 2604 2605 return false; 2606 } 2607 2608 /// \brief Get the correct cursor and offset for loading a declaration. 2609 ASTReader::RecordLocation 2610 ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) { 2611 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID); 2612 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 2613 ModuleFile *M = I->second; 2614 const DeclOffset &DOffs = 2615 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]; 2616 Loc = TranslateSourceLocation(*M, DOffs.getLocation()); 2617 return RecordLocation(M, DOffs.BitOffset); 2618 } 2619 2620 ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) { 2621 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I 2622 = GlobalBitOffsetsMap.find(GlobalOffset); 2623 2624 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map"); 2625 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset); 2626 } 2627 2628 uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) { 2629 return LocalOffset + M.GlobalBitOffset; 2630 } 2631 2632 static bool isSameTemplateParameterList(const TemplateParameterList *X, 2633 const TemplateParameterList *Y); 2634 2635 /// \brief Determine whether two template parameters are similar enough 2636 /// that they may be used in declarations of the same template. 2637 static bool isSameTemplateParameter(const NamedDecl *X, 2638 const NamedDecl *Y) { 2639 if (X->getKind() != Y->getKind()) 2640 return false; 2641 2642 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) { 2643 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y); 2644 return TX->isParameterPack() == TY->isParameterPack(); 2645 } 2646 2647 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) { 2648 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y); 2649 return TX->isParameterPack() == TY->isParameterPack() && 2650 TX->getASTContext().hasSameType(TX->getType(), TY->getType()); 2651 } 2652 2653 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X); 2654 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y); 2655 return TX->isParameterPack() == TY->isParameterPack() && 2656 isSameTemplateParameterList(TX->getTemplateParameters(), 2657 TY->getTemplateParameters()); 2658 } 2659 2660 static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) { 2661 if (auto *NS = X->getAsNamespace()) 2662 return NS; 2663 if (auto *NAS = X->getAsNamespaceAlias()) 2664 return NAS->getNamespace(); 2665 return nullptr; 2666 } 2667 2668 static bool isSameQualifier(const NestedNameSpecifier *X, 2669 const NestedNameSpecifier *Y) { 2670 if (auto *NSX = getNamespace(X)) { 2671 auto *NSY = getNamespace(Y); 2672 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl()) 2673 return false; 2674 } else if (X->getKind() != Y->getKind()) 2675 return false; 2676 2677 // FIXME: For namespaces and types, we're permitted to check that the entity 2678 // is named via the same tokens. We should probably do so. 2679 switch (X->getKind()) { 2680 case NestedNameSpecifier::Identifier: 2681 if (X->getAsIdentifier() != Y->getAsIdentifier()) 2682 return false; 2683 break; 2684 case NestedNameSpecifier::Namespace: 2685 case NestedNameSpecifier::NamespaceAlias: 2686 // We've already checked that we named the same namespace. 2687 break; 2688 case NestedNameSpecifier::TypeSpec: 2689 case NestedNameSpecifier::TypeSpecWithTemplate: 2690 if (X->getAsType()->getCanonicalTypeInternal() != 2691 Y->getAsType()->getCanonicalTypeInternal()) 2692 return false; 2693 break; 2694 case NestedNameSpecifier::Global: 2695 case NestedNameSpecifier::Super: 2696 return true; 2697 } 2698 2699 // Recurse into earlier portion of NNS, if any. 2700 auto *PX = X->getPrefix(); 2701 auto *PY = Y->getPrefix(); 2702 if (PX && PY) 2703 return isSameQualifier(PX, PY); 2704 return !PX && !PY; 2705 } 2706 2707 /// \brief Determine whether two template parameter lists are similar enough 2708 /// that they may be used in declarations of the same template. 2709 static bool isSameTemplateParameterList(const TemplateParameterList *X, 2710 const TemplateParameterList *Y) { 2711 if (X->size() != Y->size()) 2712 return false; 2713 2714 for (unsigned I = 0, N = X->size(); I != N; ++I) 2715 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I))) 2716 return false; 2717 2718 return true; 2719 } 2720 2721 /// Determine whether the attributes we can overload on are identical for A and 2722 /// B. Will ignore any overloadable attrs represented in the type of A and B. 2723 static bool hasSameOverloadableAttrs(const FunctionDecl *A, 2724 const FunctionDecl *B) { 2725 // Note that pass_object_size attributes are represented in the function's 2726 // ExtParameterInfo, so we don't need to check them here. 2727 2728 SmallVector<const EnableIfAttr *, 4> AEnableIfs; 2729 // Since this is an equality check, we can ignore that enable_if attrs show up 2730 // in reverse order. 2731 for (const auto *EIA : A->specific_attrs<EnableIfAttr>()) 2732 AEnableIfs.push_back(EIA); 2733 2734 SmallVector<const EnableIfAttr *, 4> BEnableIfs; 2735 for (const auto *EIA : B->specific_attrs<EnableIfAttr>()) 2736 BEnableIfs.push_back(EIA); 2737 2738 // Two very common cases: either we have 0 enable_if attrs, or we have an 2739 // unequal number of enable_if attrs. 2740 if (AEnableIfs.empty() && BEnableIfs.empty()) 2741 return true; 2742 2743 if (AEnableIfs.size() != BEnableIfs.size()) 2744 return false; 2745 2746 llvm::FoldingSetNodeID Cand1ID, Cand2ID; 2747 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) { 2748 Cand1ID.clear(); 2749 Cand2ID.clear(); 2750 2751 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true); 2752 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true); 2753 if (Cand1ID != Cand2ID) 2754 return false; 2755 } 2756 2757 return true; 2758 } 2759 2760 /// \brief Determine whether the two declarations refer to the same entity. 2761 static bool isSameEntity(NamedDecl *X, NamedDecl *Y) { 2762 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!"); 2763 2764 if (X == Y) 2765 return true; 2766 2767 // Must be in the same context. 2768 if (!X->getDeclContext()->getRedeclContext()->Equals( 2769 Y->getDeclContext()->getRedeclContext())) 2770 return false; 2771 2772 // Two typedefs refer to the same entity if they have the same underlying 2773 // type. 2774 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X)) 2775 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y)) 2776 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(), 2777 TypedefY->getUnderlyingType()); 2778 2779 // Must have the same kind. 2780 if (X->getKind() != Y->getKind()) 2781 return false; 2782 2783 // Objective-C classes and protocols with the same name always match. 2784 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X)) 2785 return true; 2786 2787 if (isa<ClassTemplateSpecializationDecl>(X)) { 2788 // No need to handle these here: we merge them when adding them to the 2789 // template. 2790 return false; 2791 } 2792 2793 // Compatible tags match. 2794 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) { 2795 TagDecl *TagY = cast<TagDecl>(Y); 2796 return (TagX->getTagKind() == TagY->getTagKind()) || 2797 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class || 2798 TagX->getTagKind() == TTK_Interface) && 2799 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class || 2800 TagY->getTagKind() == TTK_Interface)); 2801 } 2802 2803 // Functions with the same type and linkage match. 2804 // FIXME: This needs to cope with merging of prototyped/non-prototyped 2805 // functions, etc. 2806 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) { 2807 FunctionDecl *FuncY = cast<FunctionDecl>(Y); 2808 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) { 2809 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y); 2810 if (CtorX->getInheritedConstructor() && 2811 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(), 2812 CtorY->getInheritedConstructor().getConstructor())) 2813 return false; 2814 } 2815 ASTContext &C = FuncX->getASTContext(); 2816 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) { 2817 // We can get functions with different types on the redecl chain in C++17 2818 // if they have differing exception specifications and at least one of 2819 // the excpetion specs is unresolved. 2820 // FIXME: Do we need to check for C++14 deduced return types here too? 2821 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>(); 2822 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>(); 2823 if (C.getLangOpts().CPlusPlus1z && XFPT && YFPT && 2824 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) || 2825 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) && 2826 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(), 2827 FuncY->getType())) 2828 return true; 2829 return false; 2830 } 2831 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() && 2832 hasSameOverloadableAttrs(FuncX, FuncY); 2833 } 2834 2835 // Variables with the same type and linkage match. 2836 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) { 2837 VarDecl *VarY = cast<VarDecl>(Y); 2838 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) { 2839 ASTContext &C = VarX->getASTContext(); 2840 if (C.hasSameType(VarX->getType(), VarY->getType())) 2841 return true; 2842 2843 // We can get decls with different types on the redecl chain. Eg. 2844 // template <typename T> struct S { static T Var[]; }; // #1 2845 // template <typename T> T S<T>::Var[sizeof(T)]; // #2 2846 // Only? happens when completing an incomplete array type. In this case 2847 // when comparing #1 and #2 we should go through their element type. 2848 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType()); 2849 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType()); 2850 if (!VarXTy || !VarYTy) 2851 return false; 2852 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType()) 2853 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType()); 2854 } 2855 return false; 2856 } 2857 2858 // Namespaces with the same name and inlinedness match. 2859 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) { 2860 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y); 2861 return NamespaceX->isInline() == NamespaceY->isInline(); 2862 } 2863 2864 // Identical template names and kinds match if their template parameter lists 2865 // and patterns match. 2866 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) { 2867 TemplateDecl *TemplateY = cast<TemplateDecl>(Y); 2868 return isSameEntity(TemplateX->getTemplatedDecl(), 2869 TemplateY->getTemplatedDecl()) && 2870 isSameTemplateParameterList(TemplateX->getTemplateParameters(), 2871 TemplateY->getTemplateParameters()); 2872 } 2873 2874 // Fields with the same name and the same type match. 2875 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) { 2876 FieldDecl *FDY = cast<FieldDecl>(Y); 2877 // FIXME: Also check the bitwidth is odr-equivalent, if any. 2878 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType()); 2879 } 2880 2881 // Indirect fields with the same target field match. 2882 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) { 2883 auto *IFDY = cast<IndirectFieldDecl>(Y); 2884 return IFDX->getAnonField()->getCanonicalDecl() == 2885 IFDY->getAnonField()->getCanonicalDecl(); 2886 } 2887 2888 // Enumerators with the same name match. 2889 if (isa<EnumConstantDecl>(X)) 2890 // FIXME: Also check the value is odr-equivalent. 2891 return true; 2892 2893 // Using shadow declarations with the same target match. 2894 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) { 2895 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y); 2896 return USX->getTargetDecl() == USY->getTargetDecl(); 2897 } 2898 2899 // Using declarations with the same qualifier match. (We already know that 2900 // the name matches.) 2901 if (auto *UX = dyn_cast<UsingDecl>(X)) { 2902 auto *UY = cast<UsingDecl>(Y); 2903 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) && 2904 UX->hasTypename() == UY->hasTypename() && 2905 UX->isAccessDeclaration() == UY->isAccessDeclaration(); 2906 } 2907 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) { 2908 auto *UY = cast<UnresolvedUsingValueDecl>(Y); 2909 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) && 2910 UX->isAccessDeclaration() == UY->isAccessDeclaration(); 2911 } 2912 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X)) 2913 return isSameQualifier( 2914 UX->getQualifier(), 2915 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier()); 2916 2917 // Namespace alias definitions with the same target match. 2918 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) { 2919 auto *NAY = cast<NamespaceAliasDecl>(Y); 2920 return NAX->getNamespace()->Equals(NAY->getNamespace()); 2921 } 2922 2923 return false; 2924 } 2925 2926 /// Find the context in which we should search for previous declarations when 2927 /// looking for declarations to merge. 2928 DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader, 2929 DeclContext *DC) { 2930 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) 2931 return ND->getOriginalNamespace(); 2932 2933 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { 2934 // Try to dig out the definition. 2935 auto *DD = RD->DefinitionData; 2936 if (!DD) 2937 DD = RD->getCanonicalDecl()->DefinitionData; 2938 2939 // If there's no definition yet, then DC's definition is added by an update 2940 // record, but we've not yet loaded that update record. In this case, we 2941 // commit to DC being the canonical definition now, and will fix this when 2942 // we load the update record. 2943 if (!DD) { 2944 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD); 2945 RD->IsCompleteDefinition = true; 2946 RD->DefinitionData = DD; 2947 RD->getCanonicalDecl()->DefinitionData = DD; 2948 2949 // Track that we did this horrible thing so that we can fix it later. 2950 Reader.PendingFakeDefinitionData.insert( 2951 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake)); 2952 } 2953 2954 return DD->Definition; 2955 } 2956 2957 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC)) 2958 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition() 2959 : nullptr; 2960 2961 // We can see the TU here only if we have no Sema object. In that case, 2962 // there's no TU scope to look in, so using the DC alone is sufficient. 2963 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) 2964 return TU; 2965 2966 return nullptr; 2967 } 2968 2969 ASTDeclReader::FindExistingResult::~FindExistingResult() { 2970 // Record that we had a typedef name for linkage whether or not we merge 2971 // with that declaration. 2972 if (TypedefNameForLinkage) { 2973 DeclContext *DC = New->getDeclContext()->getRedeclContext(); 2974 Reader.ImportedTypedefNamesForLinkage.insert( 2975 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New)); 2976 return; 2977 } 2978 2979 if (!AddResult || Existing) 2980 return; 2981 2982 DeclarationName Name = New->getDeclName(); 2983 DeclContext *DC = New->getDeclContext()->getRedeclContext(); 2984 if (needsAnonymousDeclarationNumber(New)) { 2985 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(), 2986 AnonymousDeclNumber, New); 2987 } else if (DC->isTranslationUnit() && 2988 !Reader.getContext().getLangOpts().CPlusPlus) { 2989 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name)) 2990 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()] 2991 .push_back(New); 2992 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) { 2993 // Add the declaration to its redeclaration context so later merging 2994 // lookups will find it. 2995 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true); 2996 } 2997 } 2998 2999 /// Find the declaration that should be merged into, given the declaration found 3000 /// by name lookup. If we're merging an anonymous declaration within a typedef, 3001 /// we need a matching typedef, and we merge with the type inside it. 3002 static NamedDecl *getDeclForMerging(NamedDecl *Found, 3003 bool IsTypedefNameForLinkage) { 3004 if (!IsTypedefNameForLinkage) 3005 return Found; 3006 3007 // If we found a typedef declaration that gives a name to some other 3008 // declaration, then we want that inner declaration. Declarations from 3009 // AST files are handled via ImportedTypedefNamesForLinkage. 3010 if (Found->isFromASTFile()) 3011 return nullptr; 3012 3013 if (auto *TND = dyn_cast<TypedefNameDecl>(Found)) 3014 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true); 3015 3016 return nullptr; 3017 } 3018 3019 NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader, 3020 DeclContext *DC, 3021 unsigned Index) { 3022 // If the lexical context has been merged, look into the now-canonical 3023 // definition. 3024 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC)) 3025 DC = Merged; 3026 3027 // If we've seen this before, return the canonical declaration. 3028 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC]; 3029 if (Index < Previous.size() && Previous[Index]) 3030 return Previous[Index]; 3031 3032 // If this is the first time, but we have parsed a declaration of the context, 3033 // build the anonymous declaration list from the parsed declaration. 3034 if (!cast<Decl>(DC)->isFromASTFile()) { 3035 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) { 3036 if (Previous.size() == Number) 3037 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl())); 3038 else 3039 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl()); 3040 }); 3041 } 3042 3043 return Index < Previous.size() ? Previous[Index] : nullptr; 3044 } 3045 3046 void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader, 3047 DeclContext *DC, unsigned Index, 3048 NamedDecl *D) { 3049 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC)) 3050 DC = Merged; 3051 3052 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC]; 3053 if (Index >= Previous.size()) 3054 Previous.resize(Index + 1); 3055 if (!Previous[Index]) 3056 Previous[Index] = D; 3057 } 3058 3059 ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) { 3060 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage 3061 : D->getDeclName(); 3062 3063 if (!Name && !needsAnonymousDeclarationNumber(D)) { 3064 // Don't bother trying to find unnamed declarations that are in 3065 // unmergeable contexts. 3066 FindExistingResult Result(Reader, D, /*Existing=*/nullptr, 3067 AnonymousDeclNumber, TypedefNameForLinkage); 3068 Result.suppress(); 3069 return Result; 3070 } 3071 3072 DeclContext *DC = D->getDeclContext()->getRedeclContext(); 3073 if (TypedefNameForLinkage) { 3074 auto It = Reader.ImportedTypedefNamesForLinkage.find( 3075 std::make_pair(DC, TypedefNameForLinkage)); 3076 if (It != Reader.ImportedTypedefNamesForLinkage.end()) 3077 if (isSameEntity(It->second, D)) 3078 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber, 3079 TypedefNameForLinkage); 3080 // Go on to check in other places in case an existing typedef name 3081 // was not imported. 3082 } 3083 3084 if (needsAnonymousDeclarationNumber(D)) { 3085 // This is an anonymous declaration that we may need to merge. Look it up 3086 // in its context by number. 3087 if (auto *Existing = getAnonymousDeclForMerging( 3088 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber)) 3089 if (isSameEntity(Existing, D)) 3090 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, 3091 TypedefNameForLinkage); 3092 } else if (DC->isTranslationUnit() && 3093 !Reader.getContext().getLangOpts().CPlusPlus) { 3094 IdentifierResolver &IdResolver = Reader.getIdResolver(); 3095 3096 // Temporarily consider the identifier to be up-to-date. We don't want to 3097 // cause additional lookups here. 3098 class UpToDateIdentifierRAII { 3099 IdentifierInfo *II; 3100 bool WasOutToDate; 3101 3102 public: 3103 explicit UpToDateIdentifierRAII(IdentifierInfo *II) 3104 : II(II), WasOutToDate(false) 3105 { 3106 if (II) { 3107 WasOutToDate = II->isOutOfDate(); 3108 if (WasOutToDate) 3109 II->setOutOfDate(false); 3110 } 3111 } 3112 3113 ~UpToDateIdentifierRAII() { 3114 if (WasOutToDate) 3115 II->setOutOfDate(true); 3116 } 3117 } UpToDate(Name.getAsIdentifierInfo()); 3118 3119 for (IdentifierResolver::iterator I = IdResolver.begin(Name), 3120 IEnd = IdResolver.end(); 3121 I != IEnd; ++I) { 3122 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage)) 3123 if (isSameEntity(Existing, D)) 3124 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, 3125 TypedefNameForLinkage); 3126 } 3127 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) { 3128 DeclContext::lookup_result R = MergeDC->noload_lookup(Name); 3129 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { 3130 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage)) 3131 if (isSameEntity(Existing, D)) 3132 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, 3133 TypedefNameForLinkage); 3134 } 3135 } else { 3136 // Not in a mergeable context. 3137 return FindExistingResult(Reader); 3138 } 3139 3140 // If this declaration is from a merged context, make a note that we need to 3141 // check that the canonical definition of that context contains the decl. 3142 // 3143 // FIXME: We should do something similar if we merge two definitions of the 3144 // same template specialization into the same CXXRecordDecl. 3145 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext()); 3146 if (MergedDCIt != Reader.MergedDeclContexts.end() && 3147 MergedDCIt->second == D->getDeclContext()) 3148 Reader.PendingOdrMergeChecks.push_back(D); 3149 3150 return FindExistingResult(Reader, D, /*Existing=*/nullptr, 3151 AnonymousDeclNumber, TypedefNameForLinkage); 3152 } 3153 3154 template<typename DeclT> 3155 Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) { 3156 return D->RedeclLink.getLatestNotUpdated(); 3157 } 3158 Decl *ASTDeclReader::getMostRecentDeclImpl(...) { 3159 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration"); 3160 } 3161 3162 Decl *ASTDeclReader::getMostRecentDecl(Decl *D) { 3163 assert(D); 3164 3165 switch (D->getKind()) { 3166 #define ABSTRACT_DECL(TYPE) 3167 #define DECL(TYPE, BASE) \ 3168 case Decl::TYPE: \ 3169 return getMostRecentDeclImpl(cast<TYPE##Decl>(D)); 3170 #include "clang/AST/DeclNodes.inc" 3171 } 3172 llvm_unreachable("unknown decl kind"); 3173 } 3174 3175 Decl *ASTReader::getMostRecentExistingDecl(Decl *D) { 3176 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl()); 3177 } 3178 3179 template<typename DeclT> 3180 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, 3181 Redeclarable<DeclT> *D, 3182 Decl *Previous, Decl *Canon) { 3183 D->RedeclLink.setPrevious(cast<DeclT>(Previous)); 3184 D->First = cast<DeclT>(Previous)->First; 3185 } 3186 3187 namespace clang { 3188 template<> 3189 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, 3190 Redeclarable<VarDecl> *D, 3191 Decl *Previous, Decl *Canon) { 3192 VarDecl *VD = static_cast<VarDecl*>(D); 3193 VarDecl *PrevVD = cast<VarDecl>(Previous); 3194 D->RedeclLink.setPrevious(PrevVD); 3195 D->First = PrevVD->First; 3196 3197 // We should keep at most one definition on the chain. 3198 // FIXME: Cache the definition once we've found it. Building a chain with 3199 // N definitions currently takes O(N^2) time here. 3200 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) { 3201 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) { 3202 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) { 3203 Reader.mergeDefinitionVisibility(CurD, VD); 3204 VD->demoteThisDefinitionToDeclaration(); 3205 break; 3206 } 3207 } 3208 } 3209 } 3210 3211 template<> 3212 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, 3213 Redeclarable<FunctionDecl> *D, 3214 Decl *Previous, Decl *Canon) { 3215 FunctionDecl *FD = static_cast<FunctionDecl*>(D); 3216 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous); 3217 3218 FD->RedeclLink.setPrevious(PrevFD); 3219 FD->First = PrevFD->First; 3220 3221 // If the previous declaration is an inline function declaration, then this 3222 // declaration is too. 3223 if (PrevFD->IsInline != FD->IsInline) { 3224 // FIXME: [dcl.fct.spec]p4: 3225 // If a function with external linkage is declared inline in one 3226 // translation unit, it shall be declared inline in all translation 3227 // units in which it appears. 3228 // 3229 // Be careful of this case: 3230 // 3231 // module A: 3232 // template<typename T> struct X { void f(); }; 3233 // template<typename T> inline void X<T>::f() {} 3234 // 3235 // module B instantiates the declaration of X<int>::f 3236 // module C instantiates the definition of X<int>::f 3237 // 3238 // If module B and C are merged, we do not have a violation of this rule. 3239 FD->IsInline = true; 3240 } 3241 3242 // If we need to propagate an exception specification along the redecl 3243 // chain, make a note of that so that we can do so later. 3244 auto *FPT = FD->getType()->getAs<FunctionProtoType>(); 3245 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>(); 3246 if (FPT && PrevFPT) { 3247 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType()); 3248 bool WasUnresolved = 3249 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType()); 3250 if (IsUnresolved != WasUnresolved) 3251 Reader.PendingExceptionSpecUpdates.insert( 3252 std::make_pair(Canon, IsUnresolved ? PrevFD : FD)); 3253 } 3254 } 3255 } // end namespace clang 3256 3257 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) { 3258 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration"); 3259 } 3260 3261 /// Inherit the default template argument from \p From to \p To. Returns 3262 /// \c false if there is no default template for \p From. 3263 template <typename ParmDecl> 3264 static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From, 3265 Decl *ToD) { 3266 auto *To = cast<ParmDecl>(ToD); 3267 if (!From->hasDefaultArgument()) 3268 return false; 3269 To->setInheritedDefaultArgument(Context, From); 3270 return true; 3271 } 3272 3273 static void inheritDefaultTemplateArguments(ASTContext &Context, 3274 TemplateDecl *From, 3275 TemplateDecl *To) { 3276 auto *FromTP = From->getTemplateParameters(); 3277 auto *ToTP = To->getTemplateParameters(); 3278 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?"); 3279 3280 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) { 3281 NamedDecl *FromParam = FromTP->getParam(N - I - 1); 3282 if (FromParam->isParameterPack()) 3283 continue; 3284 NamedDecl *ToParam = ToTP->getParam(N - I - 1); 3285 3286 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) { 3287 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam)) 3288 break; 3289 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) { 3290 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam)) 3291 break; 3292 } else { 3293 if (!inheritDefaultTemplateArgument( 3294 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam)) 3295 break; 3296 } 3297 } 3298 } 3299 3300 void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D, 3301 Decl *Previous, Decl *Canon) { 3302 assert(D && Previous); 3303 3304 switch (D->getKind()) { 3305 #define ABSTRACT_DECL(TYPE) 3306 #define DECL(TYPE, BASE) \ 3307 case Decl::TYPE: \ 3308 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \ 3309 break; 3310 #include "clang/AST/DeclNodes.inc" 3311 } 3312 3313 // If the declaration was visible in one module, a redeclaration of it in 3314 // another module remains visible even if it wouldn't be visible by itself. 3315 // 3316 // FIXME: In this case, the declaration should only be visible if a module 3317 // that makes it visible has been imported. 3318 D->IdentifierNamespace |= 3319 Previous->IdentifierNamespace & 3320 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); 3321 3322 // If the declaration declares a template, it may inherit default arguments 3323 // from the previous declaration. 3324 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) 3325 inheritDefaultTemplateArguments(Reader.getContext(), 3326 cast<TemplateDecl>(Previous), TD); 3327 } 3328 3329 template<typename DeclT> 3330 void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) { 3331 D->RedeclLink.setLatest(cast<DeclT>(Latest)); 3332 } 3333 void ASTDeclReader::attachLatestDeclImpl(...) { 3334 llvm_unreachable("attachLatestDecl on non-redeclarable declaration"); 3335 } 3336 3337 void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) { 3338 assert(D && Latest); 3339 3340 switch (D->getKind()) { 3341 #define ABSTRACT_DECL(TYPE) 3342 #define DECL(TYPE, BASE) \ 3343 case Decl::TYPE: \ 3344 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \ 3345 break; 3346 #include "clang/AST/DeclNodes.inc" 3347 } 3348 } 3349 3350 template<typename DeclT> 3351 void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) { 3352 D->RedeclLink.markIncomplete(); 3353 } 3354 void ASTDeclReader::markIncompleteDeclChainImpl(...) { 3355 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration"); 3356 } 3357 3358 void ASTReader::markIncompleteDeclChain(Decl *D) { 3359 switch (D->getKind()) { 3360 #define ABSTRACT_DECL(TYPE) 3361 #define DECL(TYPE, BASE) \ 3362 case Decl::TYPE: \ 3363 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \ 3364 break; 3365 #include "clang/AST/DeclNodes.inc" 3366 } 3367 } 3368 3369 /// \brief Read the declaration at the given offset from the AST file. 3370 Decl *ASTReader::ReadDeclRecord(DeclID ID) { 3371 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 3372 SourceLocation DeclLoc; 3373 RecordLocation Loc = DeclCursorForID(ID, DeclLoc); 3374 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 3375 // Keep track of where we are in the stream, then jump back there 3376 // after reading this declaration. 3377 SavedStreamPosition SavedPosition(DeclsCursor); 3378 3379 ReadingKindTracker ReadingKind(Read_Decl, *this); 3380 3381 // Note that we are loading a declaration record. 3382 Deserializing ADecl(this); 3383 3384 DeclsCursor.JumpToBit(Loc.Offset); 3385 ASTRecordReader Record(*this, *Loc.F); 3386 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc); 3387 unsigned Code = DeclsCursor.ReadCode(); 3388 3389 ASTContext &Context = getContext(); 3390 Decl *D = nullptr; 3391 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) { 3392 case DECL_CONTEXT_LEXICAL: 3393 case DECL_CONTEXT_VISIBLE: 3394 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord"); 3395 case DECL_TYPEDEF: 3396 D = TypedefDecl::CreateDeserialized(Context, ID); 3397 break; 3398 case DECL_TYPEALIAS: 3399 D = TypeAliasDecl::CreateDeserialized(Context, ID); 3400 break; 3401 case DECL_ENUM: 3402 D = EnumDecl::CreateDeserialized(Context, ID); 3403 break; 3404 case DECL_RECORD: 3405 D = RecordDecl::CreateDeserialized(Context, ID); 3406 break; 3407 case DECL_ENUM_CONSTANT: 3408 D = EnumConstantDecl::CreateDeserialized(Context, ID); 3409 break; 3410 case DECL_FUNCTION: 3411 D = FunctionDecl::CreateDeserialized(Context, ID); 3412 break; 3413 case DECL_LINKAGE_SPEC: 3414 D = LinkageSpecDecl::CreateDeserialized(Context, ID); 3415 break; 3416 case DECL_EXPORT: 3417 D = ExportDecl::CreateDeserialized(Context, ID); 3418 break; 3419 case DECL_LABEL: 3420 D = LabelDecl::CreateDeserialized(Context, ID); 3421 break; 3422 case DECL_NAMESPACE: 3423 D = NamespaceDecl::CreateDeserialized(Context, ID); 3424 break; 3425 case DECL_NAMESPACE_ALIAS: 3426 D = NamespaceAliasDecl::CreateDeserialized(Context, ID); 3427 break; 3428 case DECL_USING: 3429 D = UsingDecl::CreateDeserialized(Context, ID); 3430 break; 3431 case DECL_USING_PACK: 3432 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt()); 3433 break; 3434 case DECL_USING_SHADOW: 3435 D = UsingShadowDecl::CreateDeserialized(Context, ID); 3436 break; 3437 case DECL_CONSTRUCTOR_USING_SHADOW: 3438 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID); 3439 break; 3440 case DECL_USING_DIRECTIVE: 3441 D = UsingDirectiveDecl::CreateDeserialized(Context, ID); 3442 break; 3443 case DECL_UNRESOLVED_USING_VALUE: 3444 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID); 3445 break; 3446 case DECL_UNRESOLVED_USING_TYPENAME: 3447 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID); 3448 break; 3449 case DECL_CXX_RECORD: 3450 D = CXXRecordDecl::CreateDeserialized(Context, ID); 3451 break; 3452 case DECL_CXX_DEDUCTION_GUIDE: 3453 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID); 3454 break; 3455 case DECL_CXX_METHOD: 3456 D = CXXMethodDecl::CreateDeserialized(Context, ID); 3457 break; 3458 case DECL_CXX_CONSTRUCTOR: 3459 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false); 3460 break; 3461 case DECL_CXX_INHERITED_CONSTRUCTOR: 3462 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true); 3463 break; 3464 case DECL_CXX_DESTRUCTOR: 3465 D = CXXDestructorDecl::CreateDeserialized(Context, ID); 3466 break; 3467 case DECL_CXX_CONVERSION: 3468 D = CXXConversionDecl::CreateDeserialized(Context, ID); 3469 break; 3470 case DECL_ACCESS_SPEC: 3471 D = AccessSpecDecl::CreateDeserialized(Context, ID); 3472 break; 3473 case DECL_FRIEND: 3474 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt()); 3475 break; 3476 case DECL_FRIEND_TEMPLATE: 3477 D = FriendTemplateDecl::CreateDeserialized(Context, ID); 3478 break; 3479 case DECL_CLASS_TEMPLATE: 3480 D = ClassTemplateDecl::CreateDeserialized(Context, ID); 3481 break; 3482 case DECL_CLASS_TEMPLATE_SPECIALIZATION: 3483 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID); 3484 break; 3485 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION: 3486 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); 3487 break; 3488 case DECL_VAR_TEMPLATE: 3489 D = VarTemplateDecl::CreateDeserialized(Context, ID); 3490 break; 3491 case DECL_VAR_TEMPLATE_SPECIALIZATION: 3492 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID); 3493 break; 3494 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION: 3495 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); 3496 break; 3497 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION: 3498 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID); 3499 break; 3500 case DECL_FUNCTION_TEMPLATE: 3501 D = FunctionTemplateDecl::CreateDeserialized(Context, ID); 3502 break; 3503 case DECL_TEMPLATE_TYPE_PARM: 3504 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID); 3505 break; 3506 case DECL_NON_TYPE_TEMPLATE_PARM: 3507 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID); 3508 break; 3509 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: 3510 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID, 3511 Record.readInt()); 3512 break; 3513 case DECL_TEMPLATE_TEMPLATE_PARM: 3514 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID); 3515 break; 3516 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK: 3517 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID, 3518 Record.readInt()); 3519 break; 3520 case DECL_TYPE_ALIAS_TEMPLATE: 3521 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID); 3522 break; 3523 case DECL_STATIC_ASSERT: 3524 D = StaticAssertDecl::CreateDeserialized(Context, ID); 3525 break; 3526 case DECL_OBJC_METHOD: 3527 D = ObjCMethodDecl::CreateDeserialized(Context, ID); 3528 break; 3529 case DECL_OBJC_INTERFACE: 3530 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID); 3531 break; 3532 case DECL_OBJC_IVAR: 3533 D = ObjCIvarDecl::CreateDeserialized(Context, ID); 3534 break; 3535 case DECL_OBJC_PROTOCOL: 3536 D = ObjCProtocolDecl::CreateDeserialized(Context, ID); 3537 break; 3538 case DECL_OBJC_AT_DEFS_FIELD: 3539 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID); 3540 break; 3541 case DECL_OBJC_CATEGORY: 3542 D = ObjCCategoryDecl::CreateDeserialized(Context, ID); 3543 break; 3544 case DECL_OBJC_CATEGORY_IMPL: 3545 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID); 3546 break; 3547 case DECL_OBJC_IMPLEMENTATION: 3548 D = ObjCImplementationDecl::CreateDeserialized(Context, ID); 3549 break; 3550 case DECL_OBJC_COMPATIBLE_ALIAS: 3551 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID); 3552 break; 3553 case DECL_OBJC_PROPERTY: 3554 D = ObjCPropertyDecl::CreateDeserialized(Context, ID); 3555 break; 3556 case DECL_OBJC_PROPERTY_IMPL: 3557 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID); 3558 break; 3559 case DECL_FIELD: 3560 D = FieldDecl::CreateDeserialized(Context, ID); 3561 break; 3562 case DECL_INDIRECTFIELD: 3563 D = IndirectFieldDecl::CreateDeserialized(Context, ID); 3564 break; 3565 case DECL_VAR: 3566 D = VarDecl::CreateDeserialized(Context, ID); 3567 break; 3568 case DECL_IMPLICIT_PARAM: 3569 D = ImplicitParamDecl::CreateDeserialized(Context, ID); 3570 break; 3571 case DECL_PARM_VAR: 3572 D = ParmVarDecl::CreateDeserialized(Context, ID); 3573 break; 3574 case DECL_DECOMPOSITION: 3575 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt()); 3576 break; 3577 case DECL_BINDING: 3578 D = BindingDecl::CreateDeserialized(Context, ID); 3579 break; 3580 case DECL_FILE_SCOPE_ASM: 3581 D = FileScopeAsmDecl::CreateDeserialized(Context, ID); 3582 break; 3583 case DECL_BLOCK: 3584 D = BlockDecl::CreateDeserialized(Context, ID); 3585 break; 3586 case DECL_MS_PROPERTY: 3587 D = MSPropertyDecl::CreateDeserialized(Context, ID); 3588 break; 3589 case DECL_CAPTURED: 3590 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt()); 3591 break; 3592 case DECL_CXX_BASE_SPECIFIERS: 3593 Error("attempt to read a C++ base-specifier record as a declaration"); 3594 return nullptr; 3595 case DECL_CXX_CTOR_INITIALIZERS: 3596 Error("attempt to read a C++ ctor initializer record as a declaration"); 3597 return nullptr; 3598 case DECL_IMPORT: 3599 // Note: last entry of the ImportDecl record is the number of stored source 3600 // locations. 3601 D = ImportDecl::CreateDeserialized(Context, ID, Record.back()); 3602 break; 3603 case DECL_OMP_THREADPRIVATE: 3604 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt()); 3605 break; 3606 case DECL_OMP_DECLARE_REDUCTION: 3607 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID); 3608 break; 3609 case DECL_OMP_CAPTUREDEXPR: 3610 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID); 3611 break; 3612 case DECL_PRAGMA_COMMENT: 3613 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt()); 3614 break; 3615 case DECL_PRAGMA_DETECT_MISMATCH: 3616 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID, 3617 Record.readInt()); 3618 break; 3619 case DECL_EMPTY: 3620 D = EmptyDecl::CreateDeserialized(Context, ID); 3621 break; 3622 case DECL_OBJC_TYPE_PARAM: 3623 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID); 3624 break; 3625 } 3626 3627 assert(D && "Unknown declaration reading AST file"); 3628 LoadedDecl(Index, D); 3629 // Set the DeclContext before doing any deserialization, to make sure internal 3630 // calls to Decl::getASTContext() by Decl's methods will find the 3631 // TranslationUnitDecl without crashing. 3632 D->setDeclContext(Context.getTranslationUnitDecl()); 3633 Reader.Visit(D); 3634 3635 // If this declaration is also a declaration context, get the 3636 // offsets for its tables of lexical and visible declarations. 3637 if (DeclContext *DC = dyn_cast<DeclContext>(D)) { 3638 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC); 3639 if (Offsets.first && 3640 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC)) 3641 return nullptr; 3642 if (Offsets.second && 3643 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID)) 3644 return nullptr; 3645 } 3646 assert(Record.getIdx() == Record.size()); 3647 3648 // Load any relevant update records. 3649 PendingUpdateRecords.push_back( 3650 PendingUpdateRecord(ID, D, /*JustLoaded=*/true)); 3651 3652 // Load the categories after recursive loading is finished. 3653 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) 3654 // If we already have a definition when deserializing the ObjCInterfaceDecl, 3655 // we put the Decl in PendingDefinitions so we can pull the categories here. 3656 if (Class->isThisDeclarationADefinition() || 3657 PendingDefinitions.count(Class)) 3658 loadObjCCategories(ID, Class); 3659 3660 // If we have deserialized a declaration that has a definition the 3661 // AST consumer might need to know about, queue it. 3662 // We don't pass it to the consumer immediately because we may be in recursive 3663 // loading, and some declarations may still be initializing. 3664 PotentiallyInterestingDecls.push_back( 3665 InterestingDecl(D, Reader.hasPendingBody())); 3666 3667 return D; 3668 } 3669 3670 void ASTReader::PassInterestingDeclsToConsumer() { 3671 assert(Consumer); 3672 3673 if (PassingDeclsToConsumer) 3674 return; 3675 3676 // Guard variable to avoid recursively redoing the process of passing 3677 // decls to consumer. 3678 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 3679 true); 3680 3681 // Ensure that we've loaded all potentially-interesting declarations 3682 // that need to be eagerly loaded. 3683 for (auto ID : EagerlyDeserializedDecls) 3684 GetDecl(ID); 3685 EagerlyDeserializedDecls.clear(); 3686 3687 while (!PotentiallyInterestingDecls.empty()) { 3688 InterestingDecl D = PotentiallyInterestingDecls.front(); 3689 PotentiallyInterestingDecls.pop_front(); 3690 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody())) 3691 PassInterestingDeclToConsumer(D.getDecl()); 3692 } 3693 } 3694 3695 void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) { 3696 // The declaration may have been modified by files later in the chain. 3697 // If this is the case, read the record containing the updates from each file 3698 // and pass it to ASTDeclReader to make the modifications. 3699 serialization::GlobalDeclID ID = Record.ID; 3700 Decl *D = Record.D; 3701 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 3702 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID); 3703 3704 llvm::SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs; 3705 3706 if (UpdI != DeclUpdateOffsets.end()) { 3707 auto UpdateOffsets = std::move(UpdI->second); 3708 DeclUpdateOffsets.erase(UpdI); 3709 3710 // Check if this decl was interesting to the consumer. If we just loaded 3711 // the declaration, then we know it was interesting and we skip the call 3712 // to isConsumerInterestedIn because it is unsafe to call in the 3713 // current ASTReader state. 3714 bool WasInteresting = 3715 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false); 3716 for (auto &FileAndOffset : UpdateOffsets) { 3717 ModuleFile *F = FileAndOffset.first; 3718 uint64_t Offset = FileAndOffset.second; 3719 llvm::BitstreamCursor &Cursor = F->DeclsCursor; 3720 SavedStreamPosition SavedPosition(Cursor); 3721 Cursor.JumpToBit(Offset); 3722 unsigned Code = Cursor.ReadCode(); 3723 ASTRecordReader Record(*this, *F); 3724 unsigned RecCode = Record.readRecord(Cursor, Code); 3725 (void)RecCode; 3726 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!"); 3727 3728 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID, 3729 SourceLocation()); 3730 Reader.UpdateDecl(D, PendingLazySpecializationIDs); 3731 3732 // We might have made this declaration interesting. If so, remember that 3733 // we need to hand it off to the consumer. 3734 if (!WasInteresting && 3735 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) { 3736 PotentiallyInterestingDecls.push_back( 3737 InterestingDecl(D, Reader.hasPendingBody())); 3738 WasInteresting = true; 3739 } 3740 } 3741 } 3742 // Add the lazy specializations to the template. 3743 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) || 3744 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) && 3745 "Must not have pending specializations"); 3746 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D)) 3747 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs); 3748 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 3749 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs); 3750 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D)) 3751 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs); 3752 PendingLazySpecializationIDs.clear(); 3753 3754 // Load the pending visible updates for this decl context, if it has any. 3755 auto I = PendingVisibleUpdates.find(ID); 3756 if (I != PendingVisibleUpdates.end()) { 3757 auto VisibleUpdates = std::move(I->second); 3758 PendingVisibleUpdates.erase(I); 3759 3760 auto *DC = cast<DeclContext>(D)->getPrimaryContext(); 3761 for (const PendingVisibleUpdate &Update : VisibleUpdates) 3762 Lookups[DC].Table.add( 3763 Update.Mod, Update.Data, 3764 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod)); 3765 DC->setHasExternalVisibleStorage(true); 3766 } 3767 } 3768 3769 void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) { 3770 // Attach FirstLocal to the end of the decl chain. 3771 Decl *CanonDecl = FirstLocal->getCanonicalDecl(); 3772 if (FirstLocal != CanonDecl) { 3773 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl); 3774 ASTDeclReader::attachPreviousDecl( 3775 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl, 3776 CanonDecl); 3777 } 3778 3779 if (!LocalOffset) { 3780 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal); 3781 return; 3782 } 3783 3784 // Load the list of other redeclarations from this module file. 3785 ModuleFile *M = getOwningModuleFile(FirstLocal); 3786 assert(M && "imported decl from no module file"); 3787 3788 llvm::BitstreamCursor &Cursor = M->DeclsCursor; 3789 SavedStreamPosition SavedPosition(Cursor); 3790 Cursor.JumpToBit(LocalOffset); 3791 3792 RecordData Record; 3793 unsigned Code = Cursor.ReadCode(); 3794 unsigned RecCode = Cursor.readRecord(Code, Record); 3795 (void)RecCode; 3796 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!"); 3797 3798 // FIXME: We have several different dispatches on decl kind here; maybe 3799 // we should instead generate one loop per kind and dispatch up-front? 3800 Decl *MostRecent = FirstLocal; 3801 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3802 auto *D = GetLocalDecl(*M, Record[N - I - 1]); 3803 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl); 3804 MostRecent = D; 3805 } 3806 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent); 3807 } 3808 3809 namespace { 3810 /// \brief Given an ObjC interface, goes through the modules and links to the 3811 /// interface all the categories for it. 3812 class ObjCCategoriesVisitor { 3813 ASTReader &Reader; 3814 ObjCInterfaceDecl *Interface; 3815 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized; 3816 ObjCCategoryDecl *Tail; 3817 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap; 3818 serialization::GlobalDeclID InterfaceID; 3819 unsigned PreviousGeneration; 3820 3821 void add(ObjCCategoryDecl *Cat) { 3822 // Only process each category once. 3823 if (!Deserialized.erase(Cat)) 3824 return; 3825 3826 // Check for duplicate categories. 3827 if (Cat->getDeclName()) { 3828 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()]; 3829 if (Existing && 3830 Reader.getOwningModuleFile(Existing) 3831 != Reader.getOwningModuleFile(Cat)) { 3832 // FIXME: We should not warn for duplicates in diamond: 3833 // 3834 // MT // 3835 // / \ // 3836 // ML MR // 3837 // \ / // 3838 // MB // 3839 // 3840 // If there are duplicates in ML/MR, there will be warning when 3841 // creating MB *and* when importing MB. We should not warn when 3842 // importing. 3843 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def) 3844 << Interface->getDeclName() << Cat->getDeclName(); 3845 Reader.Diag(Existing->getLocation(), diag::note_previous_definition); 3846 } else if (!Existing) { 3847 // Record this category. 3848 Existing = Cat; 3849 } 3850 } 3851 3852 // Add this category to the end of the chain. 3853 if (Tail) 3854 ASTDeclReader::setNextObjCCategory(Tail, Cat); 3855 else 3856 Interface->setCategoryListRaw(Cat); 3857 Tail = Cat; 3858 } 3859 3860 public: 3861 ObjCCategoriesVisitor(ASTReader &Reader, 3862 ObjCInterfaceDecl *Interface, 3863 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized, 3864 serialization::GlobalDeclID InterfaceID, 3865 unsigned PreviousGeneration) 3866 : Reader(Reader), Interface(Interface), Deserialized(Deserialized), 3867 Tail(nullptr), InterfaceID(InterfaceID), 3868 PreviousGeneration(PreviousGeneration) 3869 { 3870 // Populate the name -> category map with the set of known categories. 3871 for (auto *Cat : Interface->known_categories()) { 3872 if (Cat->getDeclName()) 3873 NameCategoryMap[Cat->getDeclName()] = Cat; 3874 3875 // Keep track of the tail of the category list. 3876 Tail = Cat; 3877 } 3878 } 3879 3880 bool operator()(ModuleFile &M) { 3881 // If we've loaded all of the category information we care about from 3882 // this module file, we're done. 3883 if (M.Generation <= PreviousGeneration) 3884 return true; 3885 3886 // Map global ID of the definition down to the local ID used in this 3887 // module file. If there is no such mapping, we'll find nothing here 3888 // (or in any module it imports). 3889 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID); 3890 if (!LocalID) 3891 return true; 3892 3893 // Perform a binary search to find the local redeclarations for this 3894 // declaration (if any). 3895 const ObjCCategoriesInfo Compare = { LocalID, 0 }; 3896 const ObjCCategoriesInfo *Result 3897 = std::lower_bound(M.ObjCCategoriesMap, 3898 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap, 3899 Compare); 3900 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap || 3901 Result->DefinitionID != LocalID) { 3902 // We didn't find anything. If the class definition is in this module 3903 // file, then the module files it depends on cannot have any categories, 3904 // so suppress further lookup. 3905 return Reader.isDeclIDFromModule(InterfaceID, M); 3906 } 3907 3908 // We found something. Dig out all of the categories. 3909 unsigned Offset = Result->Offset; 3910 unsigned N = M.ObjCCategories[Offset]; 3911 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again 3912 for (unsigned I = 0; I != N; ++I) 3913 add(cast_or_null<ObjCCategoryDecl>( 3914 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++]))); 3915 return true; 3916 } 3917 }; 3918 } // end anonymous namespace 3919 3920 void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID, 3921 ObjCInterfaceDecl *D, 3922 unsigned PreviousGeneration) { 3923 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID, 3924 PreviousGeneration); 3925 ModuleMgr.visit(Visitor); 3926 } 3927 3928 template<typename DeclT, typename Fn> 3929 static void forAllLaterRedecls(DeclT *D, Fn F) { 3930 F(D); 3931 3932 // Check whether we've already merged D into its redeclaration chain. 3933 // MostRecent may or may not be nullptr if D has not been merged. If 3934 // not, walk the merged redecl chain and see if it's there. 3935 auto *MostRecent = D->getMostRecentDecl(); 3936 bool Found = false; 3937 for (auto *Redecl = MostRecent; Redecl && !Found; 3938 Redecl = Redecl->getPreviousDecl()) 3939 Found = (Redecl == D); 3940 3941 // If this declaration is merged, apply the functor to all later decls. 3942 if (Found) { 3943 for (auto *Redecl = MostRecent; Redecl != D; 3944 Redecl = Redecl->getPreviousDecl()) 3945 F(Redecl); 3946 } 3947 } 3948 3949 void ASTDeclReader::UpdateDecl(Decl *D, 3950 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) { 3951 while (Record.getIdx() < Record.size()) { 3952 switch ((DeclUpdateKind)Record.readInt()) { 3953 case UPD_CXX_ADDED_IMPLICIT_MEMBER: { 3954 auto *RD = cast<CXXRecordDecl>(D); 3955 // FIXME: If we also have an update record for instantiating the 3956 // definition of D, we need that to happen before we get here. 3957 Decl *MD = Record.readDecl(); 3958 assert(MD && "couldn't read decl from update record"); 3959 // FIXME: We should call addHiddenDecl instead, to add the member 3960 // to its DeclContext. 3961 RD->addedMember(MD); 3962 break; 3963 } 3964 3965 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: 3966 // It will be added to the template's lazy specialization set. 3967 PendingLazySpecializationIDs.push_back(ReadDeclID()); 3968 break; 3969 3970 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: { 3971 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>(); 3972 3973 // Each module has its own anonymous namespace, which is disjoint from 3974 // any other module's anonymous namespaces, so don't attach the anonymous 3975 // namespace at all. 3976 if (!Record.isModule()) { 3977 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D)) 3978 TU->setAnonymousNamespace(Anon); 3979 else 3980 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon); 3981 } 3982 break; 3983 } 3984 3985 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: { 3986 VarDecl *VD = cast<VarDecl>(D); 3987 VD->getMemberSpecializationInfo()->setPointOfInstantiation( 3988 ReadSourceLocation()); 3989 uint64_t Val = Record.readInt(); 3990 if (Val && !VD->getInit()) { 3991 VD->setInit(Record.readExpr()); 3992 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3 3993 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt(); 3994 Eval->CheckedICE = true; 3995 Eval->IsICE = Val == 3; 3996 } 3997 } 3998 break; 3999 } 4000 4001 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: { 4002 auto Param = cast<ParmVarDecl>(D); 4003 4004 // We have to read the default argument regardless of whether we use it 4005 // so that hypothetical further update records aren't messed up. 4006 // TODO: Add a function to skip over the next expr record. 4007 auto DefaultArg = Record.readExpr(); 4008 4009 // Only apply the update if the parameter still has an uninstantiated 4010 // default argument. 4011 if (Param->hasUninstantiatedDefaultArg()) 4012 Param->setDefaultArg(DefaultArg); 4013 break; 4014 } 4015 4016 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: { 4017 auto FD = cast<FieldDecl>(D); 4018 auto DefaultInit = Record.readExpr(); 4019 4020 // Only apply the update if the field still has an uninstantiated 4021 // default member initializer. 4022 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) { 4023 if (DefaultInit) 4024 FD->setInClassInitializer(DefaultInit); 4025 else 4026 // Instantiation failed. We can get here if we serialized an AST for 4027 // an invalid program. 4028 FD->removeInClassInitializer(); 4029 } 4030 break; 4031 } 4032 4033 case UPD_CXX_ADDED_FUNCTION_DEFINITION: { 4034 FunctionDecl *FD = cast<FunctionDecl>(D); 4035 if (Reader.PendingBodies[FD]) { 4036 // FIXME: Maybe check for ODR violations. 4037 // It's safe to stop now because this update record is always last. 4038 return; 4039 } 4040 4041 if (Record.readInt()) { 4042 // Maintain AST consistency: any later redeclarations of this function 4043 // are inline if this one is. (We might have merged another declaration 4044 // into this one.) 4045 forAllLaterRedecls(FD, [](FunctionDecl *FD) { 4046 FD->setImplicitlyInline(); 4047 }); 4048 } 4049 FD->setInnerLocStart(ReadSourceLocation()); 4050 ReadFunctionDefinition(FD); 4051 assert(Record.getIdx() == Record.size() && "lazy body must be last"); 4052 break; 4053 } 4054 4055 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: { 4056 auto *RD = cast<CXXRecordDecl>(D); 4057 auto *OldDD = RD->getCanonicalDecl()->DefinitionData; 4058 bool HadRealDefinition = 4059 OldDD && (OldDD->Definition != RD || 4060 !Reader.PendingFakeDefinitionData.count(OldDD)); 4061 ReadCXXRecordDefinition(RD, /*Update*/true); 4062 4063 // Visible update is handled separately. 4064 uint64_t LexicalOffset = ReadLocalOffset(); 4065 if (!HadRealDefinition && LexicalOffset) { 4066 Record.readLexicalDeclContextStorage(LexicalOffset, RD); 4067 Reader.PendingFakeDefinitionData.erase(OldDD); 4068 } 4069 4070 auto TSK = (TemplateSpecializationKind)Record.readInt(); 4071 SourceLocation POI = ReadSourceLocation(); 4072 if (MemberSpecializationInfo *MSInfo = 4073 RD->getMemberSpecializationInfo()) { 4074 MSInfo->setTemplateSpecializationKind(TSK); 4075 MSInfo->setPointOfInstantiation(POI); 4076 } else { 4077 ClassTemplateSpecializationDecl *Spec = 4078 cast<ClassTemplateSpecializationDecl>(RD); 4079 Spec->setTemplateSpecializationKind(TSK); 4080 Spec->setPointOfInstantiation(POI); 4081 4082 if (Record.readInt()) { 4083 auto PartialSpec = 4084 ReadDeclAs<ClassTemplatePartialSpecializationDecl>(); 4085 SmallVector<TemplateArgument, 8> TemplArgs; 4086 Record.readTemplateArgumentList(TemplArgs); 4087 auto *TemplArgList = TemplateArgumentList::CreateCopy( 4088 Reader.getContext(), TemplArgs); 4089 4090 // FIXME: If we already have a partial specialization set, 4091 // check that it matches. 4092 if (!Spec->getSpecializedTemplateOrPartial() 4093 .is<ClassTemplatePartialSpecializationDecl *>()) 4094 Spec->setInstantiationOf(PartialSpec, TemplArgList); 4095 } 4096 } 4097 4098 RD->setTagKind((TagTypeKind)Record.readInt()); 4099 RD->setLocation(ReadSourceLocation()); 4100 RD->setLocStart(ReadSourceLocation()); 4101 RD->setBraceRange(ReadSourceRange()); 4102 4103 if (Record.readInt()) { 4104 AttrVec Attrs; 4105 Record.readAttributes(Attrs); 4106 // If the declaration already has attributes, we assume that some other 4107 // AST file already loaded them. 4108 if (!D->hasAttrs()) 4109 D->setAttrsImpl(Attrs, Reader.getContext()); 4110 } 4111 break; 4112 } 4113 4114 case UPD_CXX_RESOLVED_DTOR_DELETE: { 4115 // Set the 'operator delete' directly to avoid emitting another update 4116 // record. 4117 auto *Del = ReadDeclAs<FunctionDecl>(); 4118 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl()); 4119 auto *ThisArg = Record.readExpr(); 4120 // FIXME: Check consistency if we have an old and new operator delete. 4121 if (!First->OperatorDelete) { 4122 First->OperatorDelete = Del; 4123 First->OperatorDeleteThisArg = ThisArg; 4124 } 4125 break; 4126 } 4127 4128 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: { 4129 FunctionProtoType::ExceptionSpecInfo ESI; 4130 SmallVector<QualType, 8> ExceptionStorage; 4131 Record.readExceptionSpec(ExceptionStorage, ESI); 4132 4133 // Update this declaration's exception specification, if needed. 4134 auto *FD = cast<FunctionDecl>(D); 4135 auto *FPT = FD->getType()->castAs<FunctionProtoType>(); 4136 // FIXME: If the exception specification is already present, check that it 4137 // matches. 4138 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { 4139 FD->setType(Reader.getContext().getFunctionType( 4140 FPT->getReturnType(), FPT->getParamTypes(), 4141 FPT->getExtProtoInfo().withExceptionSpec(ESI))); 4142 4143 // When we get to the end of deserializing, see if there are other decls 4144 // that we need to propagate this exception specification onto. 4145 Reader.PendingExceptionSpecUpdates.insert( 4146 std::make_pair(FD->getCanonicalDecl(), FD)); 4147 } 4148 break; 4149 } 4150 4151 case UPD_CXX_DEDUCED_RETURN_TYPE: { 4152 // FIXME: Also do this when merging redecls. 4153 QualType DeducedResultType = Record.readType(); 4154 for (auto *Redecl : merged_redecls(D)) { 4155 // FIXME: If the return type is already deduced, check that it matches. 4156 FunctionDecl *FD = cast<FunctionDecl>(Redecl); 4157 Reader.getContext().adjustDeducedFunctionResultType(FD, 4158 DeducedResultType); 4159 } 4160 break; 4161 } 4162 4163 case UPD_DECL_MARKED_USED: { 4164 // Maintain AST consistency: any later redeclarations are used too. 4165 D->markUsed(Reader.getContext()); 4166 break; 4167 } 4168 4169 case UPD_MANGLING_NUMBER: 4170 Reader.getContext().setManglingNumber(cast<NamedDecl>(D), 4171 Record.readInt()); 4172 break; 4173 4174 case UPD_STATIC_LOCAL_NUMBER: 4175 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D), 4176 Record.readInt()); 4177 break; 4178 4179 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE: 4180 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(), 4181 ReadSourceRange())); 4182 break; 4183 4184 case UPD_DECL_EXPORTED: { 4185 unsigned SubmoduleID = readSubmoduleID(); 4186 auto *Exported = cast<NamedDecl>(D); 4187 if (auto *TD = dyn_cast<TagDecl>(Exported)) 4188 Exported = TD->getDefinition(); 4189 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr; 4190 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) { 4191 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported), 4192 Owner); 4193 Reader.PendingMergedDefinitionsToDeduplicate.insert( 4194 cast<NamedDecl>(Exported)); 4195 } else if (Owner && Owner->NameVisibility != Module::AllVisible) { 4196 // If Owner is made visible at some later point, make this declaration 4197 // visible too. 4198 Reader.HiddenNamesMap[Owner].push_back(Exported); 4199 } else { 4200 // The declaration is now visible. 4201 Exported->setVisibleDespiteOwningModule(); 4202 } 4203 break; 4204 } 4205 4206 case UPD_DECL_MARKED_OPENMP_DECLARETARGET: 4207 case UPD_ADDED_ATTR_TO_RECORD: 4208 AttrVec Attrs; 4209 Record.readAttributes(Attrs); 4210 assert(Attrs.size() == 1); 4211 D->addAttr(Attrs[0]); 4212 break; 4213 } 4214 } 4215 } 4216