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