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 if (Redecl.getFirstID() == ThisDeclID) { 1202 // Each module has its own anonymous namespace, which is disjoint from 1203 // any other module's anonymous namespaces, so don't attach the anonymous 1204 // namespace at all. 1205 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>(Record, Idx); 1206 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) 1207 D->setAnonymousNamespace(Anon); 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 1217 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 1218 RedeclarableResult Redecl = VisitRedeclarable(D); 1219 VisitNamedDecl(D); 1220 D->NamespaceLoc = ReadSourceLocation(Record, Idx); 1221 D->IdentLoc = ReadSourceLocation(Record, Idx); 1222 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1223 D->Namespace = ReadDeclAs<NamedDecl>(Record, Idx); 1224 mergeRedeclarable(D, Redecl); 1225 } 1226 1227 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) { 1228 VisitNamedDecl(D); 1229 D->setUsingLoc(ReadSourceLocation(Record, Idx)); 1230 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1231 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx); 1232 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>(Record, Idx)); 1233 D->setTypename(Record[Idx++]); 1234 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>(Record, Idx)) 1235 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern); 1236 mergeMergeable(D); 1237 } 1238 1239 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) { 1240 RedeclarableResult Redecl = VisitRedeclarable(D); 1241 VisitNamedDecl(D); 1242 D->setTargetDecl(ReadDeclAs<NamedDecl>(Record, Idx)); 1243 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(Record, Idx); 1244 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(Record, Idx); 1245 if (Pattern) 1246 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern); 1247 mergeRedeclarable(D, Redecl); 1248 } 1249 1250 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 1251 VisitNamedDecl(D); 1252 D->UsingLoc = ReadSourceLocation(Record, Idx); 1253 D->NamespaceLoc = ReadSourceLocation(Record, Idx); 1254 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1255 D->NominatedNamespace = ReadDeclAs<NamedDecl>(Record, Idx); 1256 D->CommonAncestor = ReadDeclAs<DeclContext>(Record, Idx); 1257 } 1258 1259 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1260 VisitValueDecl(D); 1261 D->setUsingLoc(ReadSourceLocation(Record, Idx)); 1262 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1263 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx); 1264 mergeMergeable(D); 1265 } 1266 1267 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl( 1268 UnresolvedUsingTypenameDecl *D) { 1269 VisitTypeDecl(D); 1270 D->TypenameLocation = ReadSourceLocation(Record, Idx); 1271 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1272 mergeMergeable(D); 1273 } 1274 1275 void ASTDeclReader::ReadCXXDefinitionData( 1276 struct CXXRecordDecl::DefinitionData &Data, 1277 const RecordData &Record, unsigned &Idx) { 1278 // Note: the caller has deserialized the IsLambda bit already. 1279 Data.UserDeclaredConstructor = Record[Idx++]; 1280 Data.UserDeclaredSpecialMembers = Record[Idx++]; 1281 Data.Aggregate = Record[Idx++]; 1282 Data.PlainOldData = Record[Idx++]; 1283 Data.Empty = Record[Idx++]; 1284 Data.Polymorphic = Record[Idx++]; 1285 Data.Abstract = Record[Idx++]; 1286 Data.IsStandardLayout = Record[Idx++]; 1287 Data.HasNoNonEmptyBases = Record[Idx++]; 1288 Data.HasPrivateFields = Record[Idx++]; 1289 Data.HasProtectedFields = Record[Idx++]; 1290 Data.HasPublicFields = Record[Idx++]; 1291 Data.HasMutableFields = Record[Idx++]; 1292 Data.HasVariantMembers = Record[Idx++]; 1293 Data.HasOnlyCMembers = Record[Idx++]; 1294 Data.HasInClassInitializer = Record[Idx++]; 1295 Data.HasUninitializedReferenceMember = Record[Idx++]; 1296 Data.NeedOverloadResolutionForMoveConstructor = Record[Idx++]; 1297 Data.NeedOverloadResolutionForMoveAssignment = Record[Idx++]; 1298 Data.NeedOverloadResolutionForDestructor = Record[Idx++]; 1299 Data.DefaultedMoveConstructorIsDeleted = Record[Idx++]; 1300 Data.DefaultedMoveAssignmentIsDeleted = Record[Idx++]; 1301 Data.DefaultedDestructorIsDeleted = Record[Idx++]; 1302 Data.HasTrivialSpecialMembers = Record[Idx++]; 1303 Data.DeclaredNonTrivialSpecialMembers = Record[Idx++]; 1304 Data.HasIrrelevantDestructor = Record[Idx++]; 1305 Data.HasConstexprNonCopyMoveConstructor = Record[Idx++]; 1306 Data.DefaultedDefaultConstructorIsConstexpr = Record[Idx++]; 1307 Data.HasConstexprDefaultConstructor = Record[Idx++]; 1308 Data.HasNonLiteralTypeFieldsOrBases = Record[Idx++]; 1309 Data.ComputedVisibleConversions = Record[Idx++]; 1310 Data.UserProvidedDefaultConstructor = Record[Idx++]; 1311 Data.DeclaredSpecialMembers = Record[Idx++]; 1312 Data.ImplicitCopyConstructorHasConstParam = Record[Idx++]; 1313 Data.ImplicitCopyAssignmentHasConstParam = Record[Idx++]; 1314 Data.HasDeclaredCopyConstructorWithConstParam = Record[Idx++]; 1315 Data.HasDeclaredCopyAssignmentWithConstParam = Record[Idx++]; 1316 1317 Data.NumBases = Record[Idx++]; 1318 if (Data.NumBases) 1319 Data.Bases = Reader.readCXXBaseSpecifiers(F, Record, Idx); 1320 Data.NumVBases = Record[Idx++]; 1321 if (Data.NumVBases) 1322 Data.VBases = Reader.readCXXBaseSpecifiers(F, Record, Idx); 1323 1324 Reader.ReadUnresolvedSet(F, Data.Conversions, Record, Idx); 1325 Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx); 1326 assert(Data.Definition && "Data.Definition should be already set!"); 1327 Data.FirstFriend = ReadDeclID(Record, Idx); 1328 1329 if (Data.IsLambda) { 1330 typedef LambdaCapture Capture; 1331 CXXRecordDecl::LambdaDefinitionData &Lambda 1332 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data); 1333 Lambda.Dependent = Record[Idx++]; 1334 Lambda.IsGenericLambda = Record[Idx++]; 1335 Lambda.CaptureDefault = Record[Idx++]; 1336 Lambda.NumCaptures = Record[Idx++]; 1337 Lambda.NumExplicitCaptures = Record[Idx++]; 1338 Lambda.ManglingNumber = Record[Idx++]; 1339 Lambda.ContextDecl = ReadDecl(Record, Idx); 1340 Lambda.Captures 1341 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures); 1342 Capture *ToCapture = Lambda.Captures; 1343 Lambda.MethodTyInfo = GetTypeSourceInfo(Record, Idx); 1344 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { 1345 SourceLocation Loc = ReadSourceLocation(Record, Idx); 1346 bool IsImplicit = Record[Idx++]; 1347 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record[Idx++]); 1348 switch (Kind) { 1349 case LCK_This: 1350 case LCK_VLAType: 1351 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation()); 1352 break; 1353 case LCK_ByCopy: 1354 case LCK_ByRef: 1355 VarDecl *Var = ReadDeclAs<VarDecl>(Record, Idx); 1356 SourceLocation EllipsisLoc = ReadSourceLocation(Record, Idx); 1357 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc); 1358 break; 1359 } 1360 } 1361 } 1362 } 1363 1364 void ASTDeclReader::MergeDefinitionData( 1365 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) { 1366 assert(D->DefinitionData.getNotUpdated() && 1367 "merging class definition into non-definition"); 1368 auto &DD = *D->DefinitionData.getNotUpdated(); 1369 1370 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD); 1371 if (PFDI != Reader.PendingFakeDefinitionData.end() && 1372 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) { 1373 // We faked up this definition data because we found a class for which we'd 1374 // not yet loaded the definition. Replace it with the real thing now. 1375 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?"); 1376 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded; 1377 1378 // Don't change which declaration is the definition; that is required 1379 // to be invariant once we select it. 1380 auto *Def = DD.Definition; 1381 DD = std::move(MergeDD); 1382 DD.Definition = Def; 1383 return; 1384 } 1385 1386 // If the new definition has new special members, let the name lookup 1387 // code know that it needs to look in the new definition too. 1388 // 1389 // FIXME: We only need to do this if the merged definition declares members 1390 // that this definition did not declare, or if it defines members that this 1391 // definition did not define. 1392 if (MergeDD.DeclaredSpecialMembers && DD.Definition != MergeDD.Definition) { 1393 Reader.MergedLookups[DD.Definition].push_back(MergeDD.Definition); 1394 DD.Definition->setHasExternalVisibleStorage(); 1395 } 1396 1397 // FIXME: Move this out into a .def file? 1398 bool DetectedOdrViolation = false; 1399 #define OR_FIELD(Field) DD.Field |= MergeDD.Field; 1400 #define MATCH_FIELD(Field) \ 1401 DetectedOdrViolation |= DD.Field != MergeDD.Field; \ 1402 OR_FIELD(Field) 1403 MATCH_FIELD(UserDeclaredConstructor) 1404 MATCH_FIELD(UserDeclaredSpecialMembers) 1405 MATCH_FIELD(Aggregate) 1406 MATCH_FIELD(PlainOldData) 1407 MATCH_FIELD(Empty) 1408 MATCH_FIELD(Polymorphic) 1409 MATCH_FIELD(Abstract) 1410 MATCH_FIELD(IsStandardLayout) 1411 MATCH_FIELD(HasNoNonEmptyBases) 1412 MATCH_FIELD(HasPrivateFields) 1413 MATCH_FIELD(HasProtectedFields) 1414 MATCH_FIELD(HasPublicFields) 1415 MATCH_FIELD(HasMutableFields) 1416 MATCH_FIELD(HasVariantMembers) 1417 MATCH_FIELD(HasOnlyCMembers) 1418 MATCH_FIELD(HasInClassInitializer) 1419 MATCH_FIELD(HasUninitializedReferenceMember) 1420 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor) 1421 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment) 1422 MATCH_FIELD(NeedOverloadResolutionForDestructor) 1423 MATCH_FIELD(DefaultedMoveConstructorIsDeleted) 1424 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted) 1425 MATCH_FIELD(DefaultedDestructorIsDeleted) 1426 OR_FIELD(HasTrivialSpecialMembers) 1427 OR_FIELD(DeclaredNonTrivialSpecialMembers) 1428 MATCH_FIELD(HasIrrelevantDestructor) 1429 OR_FIELD(HasConstexprNonCopyMoveConstructor) 1430 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr) 1431 OR_FIELD(HasConstexprDefaultConstructor) 1432 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases) 1433 // ComputedVisibleConversions is handled below. 1434 MATCH_FIELD(UserProvidedDefaultConstructor) 1435 OR_FIELD(DeclaredSpecialMembers) 1436 MATCH_FIELD(ImplicitCopyConstructorHasConstParam) 1437 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam) 1438 OR_FIELD(HasDeclaredCopyConstructorWithConstParam) 1439 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam) 1440 MATCH_FIELD(IsLambda) 1441 #undef OR_FIELD 1442 #undef MATCH_FIELD 1443 1444 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases) 1445 DetectedOdrViolation = true; 1446 // FIXME: Issue a diagnostic if the base classes don't match when we come 1447 // to lazily load them. 1448 1449 // FIXME: Issue a diagnostic if the list of conversion functions doesn't 1450 // match when we come to lazily load them. 1451 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) { 1452 DD.VisibleConversions = std::move(MergeDD.VisibleConversions); 1453 DD.ComputedVisibleConversions = true; 1454 } 1455 1456 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to 1457 // lazily load it. 1458 1459 if (DD.IsLambda) { 1460 // FIXME: ODR-checking for merging lambdas (this happens, for instance, 1461 // when they occur within the body of a function template specialization). 1462 } 1463 1464 if (DetectedOdrViolation) 1465 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition); 1466 } 1467 1468 void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) { 1469 struct CXXRecordDecl::DefinitionData *DD; 1470 ASTContext &C = Reader.getContext(); 1471 1472 // Determine whether this is a lambda closure type, so that we can 1473 // allocate the appropriate DefinitionData structure. 1474 bool IsLambda = Record[Idx++]; 1475 if (IsLambda) 1476 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false, 1477 LCD_None); 1478 else 1479 DD = new (C) struct CXXRecordDecl::DefinitionData(D); 1480 1481 ReadCXXDefinitionData(*DD, Record, Idx); 1482 1483 // We might already have a definition for this record. This can happen either 1484 // because we're reading an update record, or because we've already done some 1485 // merging. Either way, just merge into it. 1486 CXXRecordDecl *Canon = D->getCanonicalDecl(); 1487 if (auto *CanonDD = Canon->DefinitionData.getNotUpdated()) { 1488 if (CanonDD->Definition != DD->Definition) 1489 Reader.MergedDeclContexts.insert( 1490 std::make_pair(DD->Definition, CanonDD->Definition)); 1491 MergeDefinitionData(Canon, std::move(*DD)); 1492 D->DefinitionData = Canon->DefinitionData; 1493 return; 1494 } 1495 1496 // Propagate the DefinitionData pointer to the canonical declaration, so 1497 // that all other deserialized declarations will see it. 1498 if (Canon == D) { 1499 D->DefinitionData = DD; 1500 D->IsCompleteDefinition = true; 1501 1502 // If this is an update record, we can have redeclarations already. Make a 1503 // note that we need to propagate the DefinitionData pointer onto them. 1504 if (Update) 1505 Reader.PendingDefinitions.insert(D); 1506 } else if (auto *CanonDD = Canon->DefinitionData.getNotUpdated()) { 1507 // We have already deserialized a definition of this record. This 1508 // definition is no longer really a definition. Note that the pre-existing 1509 // definition is the *real* definition. 1510 Reader.MergedDeclContexts.insert( 1511 std::make_pair(D, CanonDD->Definition)); 1512 D->DefinitionData = Canon->DefinitionData; 1513 D->IsCompleteDefinition = false; 1514 MergeDefinitionData(D, std::move(*DD)); 1515 } else { 1516 Canon->DefinitionData = DD; 1517 D->DefinitionData = Canon->DefinitionData; 1518 D->IsCompleteDefinition = true; 1519 1520 // Note that we have deserialized a definition. Any declarations 1521 // deserialized before this one will be be given the DefinitionData 1522 // pointer at the end. 1523 Reader.PendingDefinitions.insert(D); 1524 } 1525 } 1526 1527 ASTDeclReader::RedeclarableResult 1528 ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) { 1529 RedeclarableResult Redecl = VisitRecordDeclImpl(D); 1530 1531 ASTContext &C = Reader.getContext(); 1532 1533 enum CXXRecKind { 1534 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization 1535 }; 1536 switch ((CXXRecKind)Record[Idx++]) { 1537 case CXXRecNotTemplate: 1538 // Merged when we merge the folding set entry in the primary template. 1539 if (!isa<ClassTemplateSpecializationDecl>(D)) 1540 mergeRedeclarable(D, Redecl); 1541 break; 1542 case CXXRecTemplate: { 1543 // Merged when we merge the template. 1544 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>(Record, Idx); 1545 D->TemplateOrInstantiation = Template; 1546 if (!Template->getTemplatedDecl()) { 1547 // We've not actually loaded the ClassTemplateDecl yet, because we're 1548 // currently being loaded as its pattern. Rely on it to set up our 1549 // TypeForDecl (see VisitClassTemplateDecl). 1550 // 1551 // Beware: we do not yet know our canonical declaration, and may still 1552 // get merged once the surrounding class template has got off the ground. 1553 TypeIDForTypeDecl = 0; 1554 } 1555 break; 1556 } 1557 case CXXRecMemberSpecialization: { 1558 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(Record, Idx); 1559 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; 1560 SourceLocation POI = ReadSourceLocation(Record, Idx); 1561 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK); 1562 MSI->setPointOfInstantiation(POI); 1563 D->TemplateOrInstantiation = MSI; 1564 mergeRedeclarable(D, Redecl); 1565 break; 1566 } 1567 } 1568 1569 bool WasDefinition = Record[Idx++]; 1570 if (WasDefinition) 1571 ReadCXXRecordDefinition(D, /*Update*/false); 1572 else 1573 // Propagate DefinitionData pointer from the canonical declaration. 1574 D->DefinitionData = D->getCanonicalDecl()->DefinitionData; 1575 1576 // Lazily load the key function to avoid deserializing every method so we can 1577 // compute it. 1578 if (WasDefinition) { 1579 DeclID KeyFn = ReadDeclID(Record, Idx); 1580 if (KeyFn && D->IsCompleteDefinition) 1581 // FIXME: This is wrong for the ARM ABI, where some other module may have 1582 // made this function no longer be a key function. We need an update 1583 // record or similar for that case. 1584 C.KeyFunctions[D] = KeyFn; 1585 } 1586 1587 return Redecl; 1588 } 1589 1590 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) { 1591 VisitFunctionDecl(D); 1592 1593 unsigned NumOverridenMethods = Record[Idx++]; 1594 if (D->isCanonicalDecl()) { 1595 while (NumOverridenMethods--) { 1596 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod, 1597 // MD may be initializing. 1598 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>(Record, Idx)) 1599 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl()); 1600 } 1601 } else { 1602 // We don't care about which declarations this used to override; we get 1603 // the relevant information from the canonical declaration. 1604 Idx += NumOverridenMethods; 1605 } 1606 } 1607 1608 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 1609 VisitCXXMethodDecl(D); 1610 1611 if (auto *CD = ReadDeclAs<CXXConstructorDecl>(Record, Idx)) 1612 if (D->isCanonicalDecl()) 1613 D->setInheritedConstructor(CD); 1614 D->IsExplicitSpecified = Record[Idx++]; 1615 // FIXME: We should defer loading this until we need the constructor's body. 1616 std::tie(D->CtorInitializers, D->NumCtorInitializers) = 1617 Reader.ReadCXXCtorInitializers(F, Record, Idx); 1618 } 1619 1620 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 1621 VisitCXXMethodDecl(D); 1622 1623 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx)) { 1624 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl()); 1625 // FIXME: Check consistency if we have an old and new operator delete. 1626 if (!Canon->OperatorDelete) 1627 Canon->OperatorDelete = OperatorDelete; 1628 } 1629 } 1630 1631 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) { 1632 VisitCXXMethodDecl(D); 1633 D->IsExplicitSpecified = Record[Idx++]; 1634 } 1635 1636 void ASTDeclReader::VisitImportDecl(ImportDecl *D) { 1637 VisitDecl(D); 1638 D->ImportedAndComplete.setPointer(readModule(Record, Idx)); 1639 D->ImportedAndComplete.setInt(Record[Idx++]); 1640 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(D + 1); 1641 for (unsigned I = 0, N = Record.back(); I != N; ++I) 1642 StoredLocs[I] = ReadSourceLocation(Record, Idx); 1643 ++Idx; // The number of stored source locations. 1644 } 1645 1646 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) { 1647 VisitDecl(D); 1648 D->setColonLoc(ReadSourceLocation(Record, Idx)); 1649 } 1650 1651 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) { 1652 VisitDecl(D); 1653 if (Record[Idx++]) // hasFriendDecl 1654 D->Friend = ReadDeclAs<NamedDecl>(Record, Idx); 1655 else 1656 D->Friend = GetTypeSourceInfo(Record, Idx); 1657 for (unsigned i = 0; i != D->NumTPLists; ++i) 1658 D->getTPLists()[i] = Reader.ReadTemplateParameterList(F, Record, Idx); 1659 D->NextFriend = ReadDeclID(Record, Idx); 1660 D->UnsupportedFriend = (Record[Idx++] != 0); 1661 D->FriendLoc = ReadSourceLocation(Record, Idx); 1662 } 1663 1664 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) { 1665 VisitDecl(D); 1666 unsigned NumParams = Record[Idx++]; 1667 D->NumParams = NumParams; 1668 D->Params = new TemplateParameterList*[NumParams]; 1669 for (unsigned i = 0; i != NumParams; ++i) 1670 D->Params[i] = Reader.ReadTemplateParameterList(F, Record, Idx); 1671 if (Record[Idx++]) // HasFriendDecl 1672 D->Friend = ReadDeclAs<NamedDecl>(Record, Idx); 1673 else 1674 D->Friend = GetTypeSourceInfo(Record, Idx); 1675 D->FriendLoc = ReadSourceLocation(Record, Idx); 1676 } 1677 1678 DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) { 1679 VisitNamedDecl(D); 1680 1681 DeclID PatternID = ReadDeclID(Record, Idx); 1682 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID)); 1683 TemplateParameterList* TemplateParams 1684 = Reader.ReadTemplateParameterList(F, Record, Idx); 1685 D->init(TemplatedDecl, TemplateParams); 1686 1687 // FIXME: If this is a redeclaration of a template from another module, handle 1688 // inheritance of default template arguments. 1689 1690 return PatternID; 1691 } 1692 1693 ASTDeclReader::RedeclarableResult 1694 ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { 1695 RedeclarableResult Redecl = VisitRedeclarable(D); 1696 1697 // Make sure we've allocated the Common pointer first. We do this before 1698 // VisitTemplateDecl so that getCommonPtr() can be used during initialization. 1699 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl(); 1700 if (!CanonD->Common) { 1701 CanonD->Common = CanonD->newCommon(Reader.getContext()); 1702 Reader.PendingDefinitions.insert(CanonD); 1703 } 1704 D->Common = CanonD->Common; 1705 1706 // If this is the first declaration of the template, fill in the information 1707 // for the 'common' pointer. 1708 if (ThisDeclID == Redecl.getFirstID()) { 1709 if (RedeclarableTemplateDecl *RTD 1710 = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx)) { 1711 assert(RTD->getKind() == D->getKind() && 1712 "InstantiatedFromMemberTemplate kind mismatch"); 1713 D->setInstantiatedFromMemberTemplate(RTD); 1714 if (Record[Idx++]) 1715 D->setMemberSpecialization(); 1716 } 1717 } 1718 1719 DeclID PatternID = VisitTemplateDecl(D); 1720 D->IdentifierNamespace = Record[Idx++]; 1721 1722 mergeRedeclarable(D, Redecl, PatternID); 1723 1724 // If we merged the template with a prior declaration chain, merge the common 1725 // pointer. 1726 // FIXME: Actually merge here, don't just overwrite. 1727 D->Common = D->getCanonicalDecl()->Common; 1728 1729 return Redecl; 1730 } 1731 1732 static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old, 1733 SmallVectorImpl<DeclID> &IDs) { 1734 assert(!IDs.empty() && "no IDs to add to list"); 1735 if (Old) { 1736 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]); 1737 std::sort(IDs.begin(), IDs.end()); 1738 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end()); 1739 } 1740 1741 auto *Result = new (Context) DeclID[1 + IDs.size()]; 1742 *Result = IDs.size(); 1743 std::copy(IDs.begin(), IDs.end(), Result + 1); 1744 return Result; 1745 } 1746 1747 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { 1748 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 1749 1750 if (ThisDeclID == Redecl.getFirstID()) { 1751 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of 1752 // the specializations. 1753 SmallVector<serialization::DeclID, 32> SpecIDs; 1754 ReadDeclIDList(SpecIDs); 1755 1756 if (!SpecIDs.empty()) { 1757 auto *CommonPtr = D->getCommonPtr(); 1758 CommonPtr->LazySpecializations = newDeclIDList( 1759 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs); 1760 } 1761 } 1762 1763 if (D->getTemplatedDecl()->TemplateOrInstantiation) { 1764 // We were loaded before our templated declaration was. We've not set up 1765 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct 1766 // it now. 1767 Reader.Context.getInjectedClassNameType( 1768 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization()); 1769 } 1770 } 1771 1772 /// TODO: Unify with ClassTemplateDecl version? 1773 /// May require unifying ClassTemplateDecl and 1774 /// VarTemplateDecl beyond TemplateDecl... 1775 void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) { 1776 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 1777 1778 if (ThisDeclID == Redecl.getFirstID()) { 1779 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of 1780 // the specializations. 1781 SmallVector<serialization::DeclID, 32> SpecIDs; 1782 ReadDeclIDList(SpecIDs); 1783 1784 if (!SpecIDs.empty()) { 1785 auto *CommonPtr = D->getCommonPtr(); 1786 CommonPtr->LazySpecializations = newDeclIDList( 1787 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs); 1788 } 1789 } 1790 } 1791 1792 ASTDeclReader::RedeclarableResult 1793 ASTDeclReader::VisitClassTemplateSpecializationDeclImpl( 1794 ClassTemplateSpecializationDecl *D) { 1795 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D); 1796 1797 ASTContext &C = Reader.getContext(); 1798 if (Decl *InstD = ReadDecl(Record, Idx)) { 1799 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) { 1800 D->SpecializedTemplate = CTD; 1801 } else { 1802 SmallVector<TemplateArgument, 8> TemplArgs; 1803 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 1804 TemplateArgumentList *ArgList 1805 = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), 1806 TemplArgs.size()); 1807 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS 1808 = new (C) ClassTemplateSpecializationDecl:: 1809 SpecializedPartialSpecialization(); 1810 PS->PartialSpecialization 1811 = cast<ClassTemplatePartialSpecializationDecl>(InstD); 1812 PS->TemplateArgs = ArgList; 1813 D->SpecializedTemplate = PS; 1814 } 1815 } 1816 1817 SmallVector<TemplateArgument, 8> TemplArgs; 1818 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 1819 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), 1820 TemplArgs.size()); 1821 D->PointOfInstantiation = ReadSourceLocation(Record, Idx); 1822 D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++]; 1823 1824 bool writtenAsCanonicalDecl = Record[Idx++]; 1825 if (writtenAsCanonicalDecl) { 1826 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(Record,Idx); 1827 if (D->isCanonicalDecl()) { // It's kept in the folding set. 1828 // Set this as, or find, the canonical declaration for this specialization 1829 ClassTemplateSpecializationDecl *CanonSpec; 1830 if (ClassTemplatePartialSpecializationDecl *Partial = 1831 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { 1832 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations 1833 .GetOrInsertNode(Partial); 1834 } else { 1835 CanonSpec = 1836 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D); 1837 } 1838 // If there was already a canonical specialization, merge into it. 1839 if (CanonSpec != D) { 1840 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl); 1841 1842 // This declaration might be a definition. Merge with any existing 1843 // definition. 1844 if (auto *DDD = D->DefinitionData.getNotUpdated()) { 1845 if (auto *CanonDD = CanonSpec->DefinitionData.getNotUpdated()) { 1846 MergeDefinitionData(CanonSpec, std::move(*DDD)); 1847 Reader.PendingDefinitions.erase(D); 1848 Reader.MergedDeclContexts.insert( 1849 std::make_pair(D, CanonDD->Definition)); 1850 D->IsCompleteDefinition = false; 1851 } else { 1852 CanonSpec->DefinitionData = D->DefinitionData; 1853 } 1854 } 1855 D->DefinitionData = CanonSpec->DefinitionData; 1856 } 1857 } 1858 } 1859 1860 // Explicit info. 1861 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) { 1862 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo 1863 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo; 1864 ExplicitInfo->TypeAsWritten = TyInfo; 1865 ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx); 1866 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx); 1867 D->ExplicitInfo = ExplicitInfo; 1868 } 1869 1870 return Redecl; 1871 } 1872 1873 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl( 1874 ClassTemplatePartialSpecializationDecl *D) { 1875 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D); 1876 1877 D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx); 1878 D->ArgsAsWritten = Reader.ReadASTTemplateArgumentListInfo(F, Record, Idx); 1879 1880 // These are read/set from/to the first declaration. 1881 if (ThisDeclID == Redecl.getFirstID()) { 1882 D->InstantiatedFromMember.setPointer( 1883 ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx)); 1884 D->InstantiatedFromMember.setInt(Record[Idx++]); 1885 } 1886 } 1887 1888 void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl( 1889 ClassScopeFunctionSpecializationDecl *D) { 1890 VisitDecl(D); 1891 D->Specialization = ReadDeclAs<CXXMethodDecl>(Record, Idx); 1892 } 1893 1894 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 1895 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 1896 1897 if (ThisDeclID == Redecl.getFirstID()) { 1898 // This FunctionTemplateDecl owns a CommonPtr; read it. 1899 SmallVector<serialization::DeclID, 32> SpecIDs; 1900 ReadDeclIDList(SpecIDs); 1901 1902 if (!SpecIDs.empty()) { 1903 auto *CommonPtr = D->getCommonPtr(); 1904 CommonPtr->LazySpecializations = newDeclIDList( 1905 Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs); 1906 } 1907 } 1908 } 1909 1910 /// TODO: Unify with ClassTemplateSpecializationDecl version? 1911 /// May require unifying ClassTemplate(Partial)SpecializationDecl and 1912 /// VarTemplate(Partial)SpecializationDecl with a new data 1913 /// structure Template(Partial)SpecializationDecl, and 1914 /// using Template(Partial)SpecializationDecl as input type. 1915 ASTDeclReader::RedeclarableResult 1916 ASTDeclReader::VisitVarTemplateSpecializationDeclImpl( 1917 VarTemplateSpecializationDecl *D) { 1918 RedeclarableResult Redecl = VisitVarDeclImpl(D); 1919 1920 ASTContext &C = Reader.getContext(); 1921 if (Decl *InstD = ReadDecl(Record, Idx)) { 1922 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) { 1923 D->SpecializedTemplate = VTD; 1924 } else { 1925 SmallVector<TemplateArgument, 8> TemplArgs; 1926 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 1927 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy( 1928 C, TemplArgs.data(), TemplArgs.size()); 1929 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS = 1930 new (C) 1931 VarTemplateSpecializationDecl::SpecializedPartialSpecialization(); 1932 PS->PartialSpecialization = 1933 cast<VarTemplatePartialSpecializationDecl>(InstD); 1934 PS->TemplateArgs = ArgList; 1935 D->SpecializedTemplate = PS; 1936 } 1937 } 1938 1939 // Explicit info. 1940 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) { 1941 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo = 1942 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo; 1943 ExplicitInfo->TypeAsWritten = TyInfo; 1944 ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx); 1945 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx); 1946 D->ExplicitInfo = ExplicitInfo; 1947 } 1948 1949 SmallVector<TemplateArgument, 8> TemplArgs; 1950 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 1951 D->TemplateArgs = 1952 TemplateArgumentList::CreateCopy(C, TemplArgs.data(), TemplArgs.size()); 1953 D->PointOfInstantiation = ReadSourceLocation(Record, Idx); 1954 D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++]; 1955 1956 bool writtenAsCanonicalDecl = Record[Idx++]; 1957 if (writtenAsCanonicalDecl) { 1958 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>(Record, Idx); 1959 if (D->isCanonicalDecl()) { // It's kept in the folding set. 1960 if (VarTemplatePartialSpecializationDecl *Partial = 1961 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) { 1962 CanonPattern->getCommonPtr()->PartialSpecializations 1963 .GetOrInsertNode(Partial); 1964 } else { 1965 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D); 1966 } 1967 } 1968 } 1969 1970 return Redecl; 1971 } 1972 1973 /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? 1974 /// May require unifying ClassTemplate(Partial)SpecializationDecl and 1975 /// VarTemplate(Partial)SpecializationDecl with a new data 1976 /// structure Template(Partial)SpecializationDecl, and 1977 /// using Template(Partial)SpecializationDecl as input type. 1978 void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl( 1979 VarTemplatePartialSpecializationDecl *D) { 1980 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D); 1981 1982 D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx); 1983 D->ArgsAsWritten = Reader.ReadASTTemplateArgumentListInfo(F, Record, Idx); 1984 1985 // These are read/set from/to the first declaration. 1986 if (ThisDeclID == Redecl.getFirstID()) { 1987 D->InstantiatedFromMember.setPointer( 1988 ReadDeclAs<VarTemplatePartialSpecializationDecl>(Record, Idx)); 1989 D->InstantiatedFromMember.setInt(Record[Idx++]); 1990 } 1991 } 1992 1993 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 1994 VisitTypeDecl(D); 1995 1996 D->setDeclaredWithTypename(Record[Idx++]); 1997 1998 bool Inherited = Record[Idx++]; 1999 TypeSourceInfo *DefArg = GetTypeSourceInfo(Record, Idx); 2000 D->setDefaultArgument(DefArg, Inherited); 2001 } 2002 2003 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 2004 VisitDeclaratorDecl(D); 2005 // TemplateParmPosition. 2006 D->setDepth(Record[Idx++]); 2007 D->setPosition(Record[Idx++]); 2008 if (D->isExpandedParameterPack()) { 2009 void **Data = reinterpret_cast<void **>(D + 1); 2010 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { 2011 Data[2*I] = Reader.readType(F, Record, Idx).getAsOpaquePtr(); 2012 Data[2*I + 1] = GetTypeSourceInfo(Record, Idx); 2013 } 2014 } else { 2015 // Rest of NonTypeTemplateParmDecl. 2016 D->ParameterPack = Record[Idx++]; 2017 if (Record[Idx++]) { 2018 Expr *DefArg = Reader.ReadExpr(F); 2019 bool Inherited = Record[Idx++]; 2020 D->setDefaultArgument(DefArg, Inherited); 2021 } 2022 } 2023 } 2024 2025 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 2026 VisitTemplateDecl(D); 2027 // TemplateParmPosition. 2028 D->setDepth(Record[Idx++]); 2029 D->setPosition(Record[Idx++]); 2030 if (D->isExpandedParameterPack()) { 2031 void **Data = reinterpret_cast<void **>(D + 1); 2032 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); 2033 I != N; ++I) 2034 Data[I] = Reader.ReadTemplateParameterList(F, Record, Idx); 2035 } else { 2036 // Rest of TemplateTemplateParmDecl. 2037 TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(F, Record, Idx); 2038 bool IsInherited = Record[Idx++]; 2039 D->setDefaultArgument(Arg, IsInherited); 2040 D->ParameterPack = Record[Idx++]; 2041 } 2042 } 2043 2044 void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { 2045 VisitRedeclarableTemplateDecl(D); 2046 } 2047 2048 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) { 2049 VisitDecl(D); 2050 D->AssertExprAndFailed.setPointer(Reader.ReadExpr(F)); 2051 D->AssertExprAndFailed.setInt(Record[Idx++]); 2052 D->Message = cast<StringLiteral>(Reader.ReadExpr(F)); 2053 D->RParenLoc = ReadSourceLocation(Record, Idx); 2054 } 2055 2056 void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) { 2057 VisitDecl(D); 2058 } 2059 2060 std::pair<uint64_t, uint64_t> 2061 ASTDeclReader::VisitDeclContext(DeclContext *DC) { 2062 uint64_t LexicalOffset = Record[Idx++]; 2063 uint64_t VisibleOffset = Record[Idx++]; 2064 return std::make_pair(LexicalOffset, VisibleOffset); 2065 } 2066 2067 template <typename T> 2068 ASTDeclReader::RedeclarableResult 2069 ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) { 2070 DeclID FirstDeclID = ReadDeclID(Record, Idx); 2071 Decl *MergeWith = nullptr; 2072 2073 // 0 indicates that this declaration was the only declaration of its entity, 2074 // and is used for space optimization. 2075 if (FirstDeclID == 0) 2076 FirstDeclID = ThisDeclID; 2077 else if (unsigned N = Record[Idx++]) { 2078 // We have some declarations that must be before us in our redeclaration 2079 // chain. Read them now, and remember that we ought to merge with one of 2080 // them. 2081 // FIXME: Provide a known merge target to the second and subsequent such 2082 // declaration. 2083 for (unsigned I = 0; I != N; ++I) 2084 MergeWith = ReadDecl(Record, Idx/*, MergeWith*/); 2085 } 2086 2087 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID)); 2088 if (FirstDecl != D) { 2089 // We delay loading of the redeclaration chain to avoid deeply nested calls. 2090 // We temporarily set the first (canonical) declaration as the previous one 2091 // which is the one that matters and mark the real previous DeclID to be 2092 // loaded & attached later on. 2093 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl); 2094 } 2095 2096 // Note that this declaration has been deserialized. 2097 Reader.RedeclsDeserialized.insert(static_cast<T *>(D)); 2098 2099 // The result structure takes care to note that we need to load the 2100 // other declaration chains for this ID. 2101 return RedeclarableResult(Reader, FirstDeclID, MergeWith, 2102 static_cast<T *>(D)->getKind()); 2103 } 2104 2105 /// \brief Attempts to merge the given declaration (D) with another declaration 2106 /// of the same entity. 2107 template<typename T> 2108 void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, 2109 RedeclarableResult &Redecl, 2110 DeclID TemplatePatternID) { 2111 T *D = static_cast<T*>(DBase); 2112 2113 // If modules are not available, there is no reason to perform this merge. 2114 if (!Reader.getContext().getLangOpts().Modules) 2115 return; 2116 2117 if (auto *Existing = Redecl.getKnownMergeTarget()) 2118 // We already know of an existing declaration we should merge with. 2119 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID); 2120 else if (FindExistingResult ExistingRes = findExisting(D)) 2121 if (T *Existing = ExistingRes) 2122 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID); 2123 } 2124 2125 /// \brief "Cast" to type T, asserting if we don't have an implicit conversion. 2126 /// We use this to put code in a template that will only be valid for certain 2127 /// instantiations. 2128 template<typename T> static T assert_cast(T t) { return t; } 2129 template<typename T> static T assert_cast(...) { 2130 llvm_unreachable("bad assert_cast"); 2131 } 2132 2133 /// \brief Merge together the pattern declarations from two template 2134 /// declarations. 2135 void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D, 2136 RedeclarableTemplateDecl *Existing, 2137 DeclID DsID) { 2138 auto *DPattern = D->getTemplatedDecl(); 2139 auto *ExistingPattern = Existing->getTemplatedDecl(); 2140 RedeclarableResult Result(Reader, DPattern->getCanonicalDecl()->getGlobalID(), 2141 /*MergeWith*/ExistingPattern, DPattern->getKind()); 2142 2143 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) { 2144 // Merge with any existing definition. 2145 // FIXME: This is duplicated in several places. Refactor. 2146 auto *ExistingClass = 2147 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl(); 2148 if (auto *DDD = DClass->DefinitionData.getNotUpdated()) { 2149 if (auto *ExistingDD = ExistingClass->DefinitionData.getNotUpdated()) { 2150 MergeDefinitionData(ExistingClass, std::move(*DDD)); 2151 Reader.PendingDefinitions.erase(DClass); 2152 Reader.MergedDeclContexts.insert( 2153 std::make_pair(DClass, ExistingDD->Definition)); 2154 DClass->IsCompleteDefinition = false; 2155 } else { 2156 ExistingClass->DefinitionData = DClass->DefinitionData; 2157 } 2158 } 2159 DClass->DefinitionData = ExistingClass->DefinitionData; 2160 2161 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern), 2162 Result); 2163 } 2164 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern)) 2165 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern), 2166 Result); 2167 if (auto *DVar = dyn_cast<VarDecl>(DPattern)) 2168 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result); 2169 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern)) 2170 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern), 2171 Result); 2172 llvm_unreachable("merged an unknown kind of redeclarable template"); 2173 } 2174 2175 /// \brief Attempts to merge the given declaration (D) with another declaration 2176 /// of the same entity. 2177 template<typename T> 2178 void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing, 2179 RedeclarableResult &Redecl, 2180 DeclID TemplatePatternID) { 2181 T *D = static_cast<T*>(DBase); 2182 T *ExistingCanon = Existing->getCanonicalDecl(); 2183 T *DCanon = D->getCanonicalDecl(); 2184 if (ExistingCanon != DCanon) { 2185 assert(DCanon->getGlobalID() == Redecl.getFirstID()); 2186 2187 // Have our redeclaration link point back at the canonical declaration 2188 // of the existing declaration, so that this declaration has the 2189 // appropriate canonical declaration. 2190 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon); 2191 2192 // When we merge a namespace, update its pointer to the first namespace. 2193 if (auto *Namespace = dyn_cast<NamespaceDecl>(D)) 2194 Namespace->AnonOrFirstNamespaceAndInline.setPointer( 2195 assert_cast<NamespaceDecl*>(ExistingCanon)); 2196 2197 // When we merge a template, merge its pattern. 2198 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D)) 2199 mergeTemplatePattern( 2200 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon), 2201 TemplatePatternID); 2202 2203 // If this declaration was the canonical declaration, make a note of 2204 // that. We accept the linear algorithm here because the number of 2205 // unique canonical declarations of an entity should always be tiny. 2206 if (DCanon == D) { 2207 Reader.MergedDecls[ExistingCanon].push_back(Redecl.getFirstID()); 2208 if (Reader.PendingDeclChainsKnown.insert(ExistingCanon).second) 2209 Reader.PendingDeclChains.push_back(ExistingCanon); 2210 } 2211 } 2212 } 2213 2214 /// \brief Attempts to merge the given declaration (D) with another declaration 2215 /// of the same entity, for the case where the entity is not actually 2216 /// redeclarable. This happens, for instance, when merging the fields of 2217 /// identical class definitions from two different modules. 2218 template<typename T> 2219 void ASTDeclReader::mergeMergeable(Mergeable<T> *D) { 2220 // If modules are not available, there is no reason to perform this merge. 2221 if (!Reader.getContext().getLangOpts().Modules) 2222 return; 2223 2224 // ODR-based merging is only performed in C++. In C, identically-named things 2225 // in different translation units are not redeclarations (but may still have 2226 // compatible types). 2227 if (!Reader.getContext().getLangOpts().CPlusPlus) 2228 return; 2229 2230 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D))) 2231 if (T *Existing = ExistingRes) 2232 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D), 2233 Existing->getCanonicalDecl()); 2234 } 2235 2236 void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 2237 VisitDecl(D); 2238 unsigned NumVars = D->varlist_size(); 2239 SmallVector<Expr *, 16> Vars; 2240 Vars.reserve(NumVars); 2241 for (unsigned i = 0; i != NumVars; ++i) { 2242 Vars.push_back(Reader.ReadExpr(F)); 2243 } 2244 D->setVars(Vars); 2245 } 2246 2247 //===----------------------------------------------------------------------===// 2248 // Attribute Reading 2249 //===----------------------------------------------------------------------===// 2250 2251 /// \brief Reads attributes from the current stream position. 2252 void ASTReader::ReadAttributes(ModuleFile &F, AttrVec &Attrs, 2253 const RecordData &Record, unsigned &Idx) { 2254 for (unsigned i = 0, e = Record[Idx++]; i != e; ++i) { 2255 Attr *New = nullptr; 2256 attr::Kind Kind = (attr::Kind)Record[Idx++]; 2257 SourceRange Range = ReadSourceRange(F, Record, Idx); 2258 2259 #include "clang/Serialization/AttrPCHRead.inc" 2260 2261 assert(New && "Unable to decode attribute?"); 2262 Attrs.push_back(New); 2263 } 2264 } 2265 2266 //===----------------------------------------------------------------------===// 2267 // ASTReader Implementation 2268 //===----------------------------------------------------------------------===// 2269 2270 /// \brief Note that we have loaded the declaration with the given 2271 /// Index. 2272 /// 2273 /// This routine notes that this declaration has already been loaded, 2274 /// so that future GetDecl calls will return this declaration rather 2275 /// than trying to load a new declaration. 2276 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) { 2277 assert(!DeclsLoaded[Index] && "Decl loaded twice?"); 2278 DeclsLoaded[Index] = D; 2279 } 2280 2281 2282 /// \brief Determine whether the consumer will be interested in seeing 2283 /// this declaration (via HandleTopLevelDecl). 2284 /// 2285 /// This routine should return true for anything that might affect 2286 /// code generation, e.g., inline function definitions, Objective-C 2287 /// declarations with metadata, etc. 2288 static bool isConsumerInterestedIn(Decl *D, bool HasBody) { 2289 // An ObjCMethodDecl is never considered as "interesting" because its 2290 // implementation container always is. 2291 2292 if (isa<FileScopeAsmDecl>(D) || 2293 isa<ObjCProtocolDecl>(D) || 2294 isa<ObjCImplDecl>(D) || 2295 isa<ImportDecl>(D) || 2296 isa<OMPThreadPrivateDecl>(D)) 2297 return true; 2298 if (VarDecl *Var = dyn_cast<VarDecl>(D)) 2299 return Var->isFileVarDecl() && 2300 Var->isThisDeclarationADefinition() == VarDecl::Definition; 2301 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) 2302 return Func->doesThisDeclarationHaveABody() || HasBody; 2303 2304 return false; 2305 } 2306 2307 /// \brief Get the correct cursor and offset for loading a declaration. 2308 ASTReader::RecordLocation 2309 ASTReader::DeclCursorForID(DeclID ID, unsigned &RawLocation) { 2310 // See if there's an override. 2311 DeclReplacementMap::iterator It = ReplacedDecls.find(ID); 2312 if (It != ReplacedDecls.end()) { 2313 RawLocation = It->second.RawLoc; 2314 return RecordLocation(It->second.Mod, It->second.Offset); 2315 } 2316 2317 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID); 2318 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 2319 ModuleFile *M = I->second; 2320 const DeclOffset & 2321 DOffs = M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]; 2322 RawLocation = DOffs.Loc; 2323 return RecordLocation(M, DOffs.BitOffset); 2324 } 2325 2326 ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) { 2327 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I 2328 = GlobalBitOffsetsMap.find(GlobalOffset); 2329 2330 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map"); 2331 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset); 2332 } 2333 2334 uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) { 2335 return LocalOffset + M.GlobalBitOffset; 2336 } 2337 2338 static bool isSameTemplateParameterList(const TemplateParameterList *X, 2339 const TemplateParameterList *Y); 2340 2341 /// \brief Determine whether two template parameters are similar enough 2342 /// that they may be used in declarations of the same template. 2343 static bool isSameTemplateParameter(const NamedDecl *X, 2344 const NamedDecl *Y) { 2345 if (X->getKind() != Y->getKind()) 2346 return false; 2347 2348 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) { 2349 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y); 2350 return TX->isParameterPack() == TY->isParameterPack(); 2351 } 2352 2353 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) { 2354 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y); 2355 return TX->isParameterPack() == TY->isParameterPack() && 2356 TX->getASTContext().hasSameType(TX->getType(), TY->getType()); 2357 } 2358 2359 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X); 2360 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y); 2361 return TX->isParameterPack() == TY->isParameterPack() && 2362 isSameTemplateParameterList(TX->getTemplateParameters(), 2363 TY->getTemplateParameters()); 2364 } 2365 2366 static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) { 2367 if (auto *NS = X->getAsNamespace()) 2368 return NS; 2369 if (auto *NAS = X->getAsNamespaceAlias()) 2370 return NAS->getNamespace(); 2371 return nullptr; 2372 } 2373 2374 static bool isSameQualifier(const NestedNameSpecifier *X, 2375 const NestedNameSpecifier *Y) { 2376 if (auto *NSX = getNamespace(X)) { 2377 auto *NSY = getNamespace(Y); 2378 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl()) 2379 return false; 2380 } else if (X->getKind() != Y->getKind()) 2381 return false; 2382 2383 // FIXME: For namespaces and types, we're permitted to check that the entity 2384 // is named via the same tokens. We should probably do so. 2385 switch (X->getKind()) { 2386 case NestedNameSpecifier::Identifier: 2387 if (X->getAsIdentifier() != Y->getAsIdentifier()) 2388 return false; 2389 break; 2390 case NestedNameSpecifier::Namespace: 2391 case NestedNameSpecifier::NamespaceAlias: 2392 // We've already checked that we named the same namespace. 2393 break; 2394 case NestedNameSpecifier::TypeSpec: 2395 case NestedNameSpecifier::TypeSpecWithTemplate: 2396 if (X->getAsType()->getCanonicalTypeInternal() != 2397 Y->getAsType()->getCanonicalTypeInternal()) 2398 return false; 2399 break; 2400 case NestedNameSpecifier::Global: 2401 case NestedNameSpecifier::Super: 2402 return true; 2403 } 2404 2405 // Recurse into earlier portion of NNS, if any. 2406 auto *PX = X->getPrefix(); 2407 auto *PY = Y->getPrefix(); 2408 if (PX && PY) 2409 return isSameQualifier(PX, PY); 2410 return !PX && !PY; 2411 } 2412 2413 /// \brief Determine whether two template parameter lists are similar enough 2414 /// that they may be used in declarations of the same template. 2415 static bool isSameTemplateParameterList(const TemplateParameterList *X, 2416 const TemplateParameterList *Y) { 2417 if (X->size() != Y->size()) 2418 return false; 2419 2420 for (unsigned I = 0, N = X->size(); I != N; ++I) 2421 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I))) 2422 return false; 2423 2424 return true; 2425 } 2426 2427 /// \brief Determine whether the two declarations refer to the same entity. 2428 static bool isSameEntity(NamedDecl *X, NamedDecl *Y) { 2429 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!"); 2430 2431 if (X == Y) 2432 return true; 2433 2434 // Must be in the same context. 2435 if (!X->getDeclContext()->getRedeclContext()->Equals( 2436 Y->getDeclContext()->getRedeclContext())) 2437 return false; 2438 2439 // Two typedefs refer to the same entity if they have the same underlying 2440 // type. 2441 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X)) 2442 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y)) 2443 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(), 2444 TypedefY->getUnderlyingType()); 2445 2446 // Must have the same kind. 2447 if (X->getKind() != Y->getKind()) 2448 return false; 2449 2450 // Objective-C classes and protocols with the same name always match. 2451 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X)) 2452 return true; 2453 2454 if (isa<ClassTemplateSpecializationDecl>(X)) { 2455 // No need to handle these here: we merge them when adding them to the 2456 // template. 2457 return false; 2458 } 2459 2460 // Compatible tags match. 2461 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) { 2462 TagDecl *TagY = cast<TagDecl>(Y); 2463 return (TagX->getTagKind() == TagY->getTagKind()) || 2464 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class || 2465 TagX->getTagKind() == TTK_Interface) && 2466 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class || 2467 TagY->getTagKind() == TTK_Interface)); 2468 } 2469 2470 // Functions with the same type and linkage match. 2471 // FIXME: This needs to cope with merging of prototyped/non-prototyped 2472 // functions, etc. 2473 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) { 2474 FunctionDecl *FuncY = cast<FunctionDecl>(Y); 2475 return (FuncX->getLinkageInternal() == FuncY->getLinkageInternal()) && 2476 FuncX->getASTContext().hasSameType(FuncX->getType(), FuncY->getType()); 2477 } 2478 2479 // Variables with the same type and linkage match. 2480 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) { 2481 VarDecl *VarY = cast<VarDecl>(Y); 2482 return (VarX->getLinkageInternal() == VarY->getLinkageInternal()) && 2483 VarX->getASTContext().hasSameType(VarX->getType(), VarY->getType()); 2484 } 2485 2486 // Namespaces with the same name and inlinedness match. 2487 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) { 2488 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y); 2489 return NamespaceX->isInline() == NamespaceY->isInline(); 2490 } 2491 2492 // Identical template names and kinds match if their template parameter lists 2493 // and patterns match. 2494 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) { 2495 TemplateDecl *TemplateY = cast<TemplateDecl>(Y); 2496 return isSameEntity(TemplateX->getTemplatedDecl(), 2497 TemplateY->getTemplatedDecl()) && 2498 isSameTemplateParameterList(TemplateX->getTemplateParameters(), 2499 TemplateY->getTemplateParameters()); 2500 } 2501 2502 // Fields with the same name and the same type match. 2503 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) { 2504 FieldDecl *FDY = cast<FieldDecl>(Y); 2505 // FIXME: Also check the bitwidth is odr-equivalent, if any. 2506 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType()); 2507 } 2508 2509 // Enumerators with the same name match. 2510 if (isa<EnumConstantDecl>(X)) 2511 // FIXME: Also check the value is odr-equivalent. 2512 return true; 2513 2514 // Using shadow declarations with the same target match. 2515 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) { 2516 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y); 2517 return USX->getTargetDecl() == USY->getTargetDecl(); 2518 } 2519 2520 // Using declarations with the same qualifier match. (We already know that 2521 // the name matches.) 2522 if (auto *UX = dyn_cast<UsingDecl>(X)) { 2523 auto *UY = cast<UsingDecl>(Y); 2524 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) && 2525 UX->hasTypename() == UY->hasTypename() && 2526 UX->isAccessDeclaration() == UY->isAccessDeclaration(); 2527 } 2528 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) { 2529 auto *UY = cast<UnresolvedUsingValueDecl>(Y); 2530 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) && 2531 UX->isAccessDeclaration() == UY->isAccessDeclaration(); 2532 } 2533 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X)) 2534 return isSameQualifier( 2535 UX->getQualifier(), 2536 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier()); 2537 2538 // Namespace alias definitions with the same target match. 2539 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) { 2540 auto *NAY = cast<NamespaceAliasDecl>(Y); 2541 return NAX->getNamespace()->Equals(NAY->getNamespace()); 2542 } 2543 2544 // FIXME: Many other cases to implement. 2545 return false; 2546 } 2547 2548 /// Find the context in which we should search for previous declarations when 2549 /// looking for declarations to merge. 2550 DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader, 2551 DeclContext *DC) { 2552 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) 2553 return ND->getOriginalNamespace(); 2554 2555 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { 2556 // Try to dig out the definition. 2557 auto *DD = RD->DefinitionData.getNotUpdated(); 2558 if (!DD) 2559 DD = RD->getCanonicalDecl()->DefinitionData.getNotUpdated(); 2560 2561 // If there's no definition yet, then DC's definition is added by an update 2562 // record, but we've not yet loaded that update record. In this case, we 2563 // commit to DC being the canonical definition now, and will fix this when 2564 // we load the update record. 2565 if (!DD) { 2566 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD); 2567 RD->IsCompleteDefinition = true; 2568 RD->DefinitionData = DD; 2569 RD->getCanonicalDecl()->DefinitionData = DD; 2570 2571 // Track that we did this horrible thing so that we can fix it later. 2572 Reader.PendingFakeDefinitionData.insert( 2573 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake)); 2574 } 2575 2576 return DD->Definition; 2577 } 2578 2579 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC)) 2580 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition() 2581 : nullptr; 2582 2583 // We can see the TU here only if we have no Sema object. In that case, 2584 // there's no TU scope to look in, so using the DC alone is sufficient. 2585 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) 2586 return TU; 2587 2588 return nullptr; 2589 } 2590 2591 ASTDeclReader::FindExistingResult::~FindExistingResult() { 2592 // Record that we had a typedef name for linkage whether or not we merge 2593 // with that declaration. 2594 if (TypedefNameForLinkage) { 2595 DeclContext *DC = New->getDeclContext()->getRedeclContext(); 2596 Reader.ImportedTypedefNamesForLinkage.insert( 2597 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New)); 2598 return; 2599 } 2600 2601 if (!AddResult || Existing) 2602 return; 2603 2604 DeclarationName Name = New->getDeclName(); 2605 DeclContext *DC = New->getDeclContext()->getRedeclContext(); 2606 if (needsAnonymousDeclarationNumber(New)) { 2607 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(), 2608 AnonymousDeclNumber, New); 2609 } else if (DC->isTranslationUnit() && Reader.SemaObj) { 2610 Reader.SemaObj->IdResolver.tryAddTopLevelDecl(New, Name); 2611 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) { 2612 // Add the declaration to its redeclaration context so later merging 2613 // lookups will find it. 2614 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true); 2615 } 2616 } 2617 2618 /// Find the declaration that should be merged into, given the declaration found 2619 /// by name lookup. If we're merging an anonymous declaration within a typedef, 2620 /// we need a matching typedef, and we merge with the type inside it. 2621 static NamedDecl *getDeclForMerging(NamedDecl *Found, 2622 bool IsTypedefNameForLinkage) { 2623 if (!IsTypedefNameForLinkage) 2624 return Found; 2625 2626 // If we found a typedef declaration that gives a name to some other 2627 // declaration, then we want that inner declaration. Declarations from 2628 // AST files are handled via ImportedTypedefNamesForLinkage. 2629 if (Found->isFromASTFile()) return 0; 2630 if (auto *TND = dyn_cast<TypedefNameDecl>(Found)) { 2631 if (auto *TT = TND->getTypeSourceInfo()->getType()->getAs<TagType>()) 2632 if (TT->getDecl()->getTypedefNameForAnonDecl() == TND) 2633 return TT->getDecl(); 2634 } 2635 2636 return 0; 2637 } 2638 2639 NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader, 2640 DeclContext *DC, 2641 unsigned Index) { 2642 // If the lexical context has been merged, look into the now-canonical 2643 // definition. 2644 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC)) 2645 DC = Merged; 2646 2647 // If we've seen this before, return the canonical declaration. 2648 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC]; 2649 if (Index < Previous.size() && Previous[Index]) 2650 return Previous[Index]; 2651 2652 // If this is the first time, but we have parsed a declaration of the context, 2653 // build the anonymous declaration list from the parsed declaration. 2654 if (!cast<Decl>(DC)->isFromASTFile()) { 2655 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) { 2656 if (Previous.size() == Number) 2657 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl())); 2658 else 2659 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl()); 2660 }); 2661 } 2662 2663 return Index < Previous.size() ? Previous[Index] : nullptr; 2664 } 2665 2666 void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader, 2667 DeclContext *DC, unsigned Index, 2668 NamedDecl *D) { 2669 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC)) 2670 DC = Merged; 2671 2672 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC]; 2673 if (Index >= Previous.size()) 2674 Previous.resize(Index + 1); 2675 if (!Previous[Index]) 2676 Previous[Index] = D; 2677 } 2678 2679 ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) { 2680 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage 2681 : D->getDeclName(); 2682 2683 if (!Name && !needsAnonymousDeclarationNumber(D)) { 2684 // Don't bother trying to find unnamed declarations that are in 2685 // unmergeable contexts. 2686 FindExistingResult Result(Reader, D, /*Existing=*/nullptr, 2687 AnonymousDeclNumber, TypedefNameForLinkage); 2688 Result.suppress(); 2689 return Result; 2690 } 2691 2692 // FIXME: Bail out for non-canonical declarations. We will have performed any 2693 // necessary merging already. 2694 2695 DeclContext *DC = D->getDeclContext()->getRedeclContext(); 2696 if (TypedefNameForLinkage) { 2697 auto It = Reader.ImportedTypedefNamesForLinkage.find( 2698 std::make_pair(DC, TypedefNameForLinkage)); 2699 if (It != Reader.ImportedTypedefNamesForLinkage.end()) 2700 if (isSameEntity(It->second, D)) 2701 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber, 2702 TypedefNameForLinkage); 2703 // Go on to check in other places in case an existing typedef name 2704 // was not imported. 2705 } 2706 2707 if (needsAnonymousDeclarationNumber(D)) { 2708 // This is an anonymous declaration that we may need to merge. Look it up 2709 // in its context by number. 2710 if (auto *Existing = getAnonymousDeclForMerging( 2711 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber)) 2712 if (isSameEntity(Existing, D)) 2713 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, 2714 TypedefNameForLinkage); 2715 } else if (DC->isTranslationUnit() && Reader.SemaObj) { 2716 IdentifierResolver &IdResolver = Reader.SemaObj->IdResolver; 2717 2718 // Temporarily consider the identifier to be up-to-date. We don't want to 2719 // cause additional lookups here. 2720 class UpToDateIdentifierRAII { 2721 IdentifierInfo *II; 2722 bool WasOutToDate; 2723 2724 public: 2725 explicit UpToDateIdentifierRAII(IdentifierInfo *II) 2726 : II(II), WasOutToDate(false) 2727 { 2728 if (II) { 2729 WasOutToDate = II->isOutOfDate(); 2730 if (WasOutToDate) 2731 II->setOutOfDate(false); 2732 } 2733 } 2734 2735 ~UpToDateIdentifierRAII() { 2736 if (WasOutToDate) 2737 II->setOutOfDate(true); 2738 } 2739 } UpToDate(Name.getAsIdentifierInfo()); 2740 2741 for (IdentifierResolver::iterator I = IdResolver.begin(Name), 2742 IEnd = IdResolver.end(); 2743 I != IEnd; ++I) { 2744 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage)) 2745 if (isSameEntity(Existing, D)) 2746 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, 2747 TypedefNameForLinkage); 2748 } 2749 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) { 2750 DeclContext::lookup_result R = MergeDC->noload_lookup(Name); 2751 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { 2752 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage)) 2753 if (isSameEntity(Existing, D)) 2754 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, 2755 TypedefNameForLinkage); 2756 } 2757 } else { 2758 // Not in a mergeable context. 2759 return FindExistingResult(Reader); 2760 } 2761 2762 // If this declaration is from a merged context, make a note that we need to 2763 // check that the canonical definition of that context contains the decl. 2764 // 2765 // FIXME: We should do something similar if we merge two definitions of the 2766 // same template specialization into the same CXXRecordDecl. 2767 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext()); 2768 if (MergedDCIt != Reader.MergedDeclContexts.end() && 2769 MergedDCIt->second == D->getDeclContext()) 2770 Reader.PendingOdrMergeChecks.push_back(D); 2771 2772 return FindExistingResult(Reader, D, /*Existing=*/nullptr, 2773 AnonymousDeclNumber, TypedefNameForLinkage); 2774 } 2775 2776 template<typename DeclT> 2777 Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) { 2778 return D->RedeclLink.getLatestNotUpdated(); 2779 } 2780 Decl *ASTDeclReader::getMostRecentDeclImpl(...) { 2781 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration"); 2782 } 2783 2784 Decl *ASTDeclReader::getMostRecentDecl(Decl *D) { 2785 assert(D); 2786 2787 switch (D->getKind()) { 2788 #define ABSTRACT_DECL(TYPE) 2789 #define DECL(TYPE, BASE) \ 2790 case Decl::TYPE: \ 2791 return getMostRecentDeclImpl(cast<TYPE##Decl>(D)); 2792 #include "clang/AST/DeclNodes.inc" 2793 } 2794 llvm_unreachable("unknown decl kind"); 2795 } 2796 2797 template<typename DeclT> 2798 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, 2799 Redeclarable<DeclT> *D, 2800 Decl *Previous) { 2801 D->RedeclLink.setPrevious(cast<DeclT>(Previous)); 2802 } 2803 namespace clang { 2804 template<> 2805 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, 2806 Redeclarable<FunctionDecl> *D, 2807 Decl *Previous) { 2808 FunctionDecl *FD = static_cast<FunctionDecl*>(D); 2809 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous); 2810 2811 FD->RedeclLink.setPrevious(PrevFD); 2812 2813 // If the previous declaration is an inline function declaration, then this 2814 // declaration is too. 2815 if (PrevFD->IsInline != FD->IsInline) { 2816 // FIXME: [dcl.fct.spec]p4: 2817 // If a function with external linkage is declared inline in one 2818 // translation unit, it shall be declared inline in all translation 2819 // units in which it appears. 2820 // 2821 // Be careful of this case: 2822 // 2823 // module A: 2824 // template<typename T> struct X { void f(); }; 2825 // template<typename T> inline void X<T>::f() {} 2826 // 2827 // module B instantiates the declaration of X<int>::f 2828 // module C instantiates the definition of X<int>::f 2829 // 2830 // If module B and C are merged, we do not have a violation of this rule. 2831 FD->IsInline = true; 2832 } 2833 2834 // If this declaration has an unresolved exception specification but the 2835 // previous declaration had a resolved one, resolve the exception 2836 // specification now. If this declaration has a resolved exception 2837 // specification but the previous declarations did not, apply our exception 2838 // specification to all prior ones now. 2839 auto *FPT = FD->getType()->getAs<FunctionProtoType>(); 2840 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>(); 2841 if (FPT && PrevFPT) { 2842 bool WasUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType()); 2843 bool IsUnresolved = isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType()); 2844 if (WasUnresolved && !IsUnresolved) { 2845 Reader.Context.adjustExceptionSpec( 2846 FD, PrevFPT->getExtProtoInfo().ExceptionSpec); 2847 } else if (!WasUnresolved && IsUnresolved) { 2848 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); 2849 for (FunctionDecl *PrevFDToUpdate = PrevFD; PrevFDToUpdate; 2850 PrevFDToUpdate = PrevFDToUpdate->getPreviousDecl()) 2851 Reader.Context.adjustExceptionSpec(PrevFDToUpdate, EPI.ExceptionSpec); 2852 } 2853 } 2854 } 2855 } 2856 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) { 2857 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration"); 2858 } 2859 2860 void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D, 2861 Decl *Previous) { 2862 assert(D && Previous); 2863 2864 switch (D->getKind()) { 2865 #define ABSTRACT_DECL(TYPE) 2866 #define DECL(TYPE, BASE) \ 2867 case Decl::TYPE: \ 2868 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous); \ 2869 break; 2870 #include "clang/AST/DeclNodes.inc" 2871 } 2872 2873 // If the declaration was visible in one module, a redeclaration of it in 2874 // another module remains visible even if it wouldn't be visible by itself. 2875 // 2876 // FIXME: In this case, the declaration should only be visible if a module 2877 // that makes it visible has been imported. 2878 D->IdentifierNamespace |= 2879 Previous->IdentifierNamespace & 2880 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); 2881 2882 // If the previous declaration is marked as used, then this declaration should 2883 // be too. 2884 if (Previous->Used) 2885 D->Used = true; 2886 } 2887 2888 template<typename DeclT> 2889 void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) { 2890 D->RedeclLink.setLatest(cast<DeclT>(Latest)); 2891 } 2892 void ASTDeclReader::attachLatestDeclImpl(...) { 2893 llvm_unreachable("attachLatestDecl on non-redeclarable declaration"); 2894 } 2895 2896 void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) { 2897 assert(D && Latest); 2898 2899 switch (D->getKind()) { 2900 #define ABSTRACT_DECL(TYPE) 2901 #define DECL(TYPE, BASE) \ 2902 case Decl::TYPE: \ 2903 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \ 2904 break; 2905 #include "clang/AST/DeclNodes.inc" 2906 } 2907 } 2908 2909 template<typename DeclT> 2910 void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) { 2911 D->RedeclLink.markIncomplete(); 2912 } 2913 void ASTDeclReader::markIncompleteDeclChainImpl(...) { 2914 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration"); 2915 } 2916 2917 void ASTReader::markIncompleteDeclChain(Decl *D) { 2918 switch (D->getKind()) { 2919 #define ABSTRACT_DECL(TYPE) 2920 #define DECL(TYPE, BASE) \ 2921 case Decl::TYPE: \ 2922 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \ 2923 break; 2924 #include "clang/AST/DeclNodes.inc" 2925 } 2926 } 2927 2928 /// \brief Read the declaration at the given offset from the AST file. 2929 Decl *ASTReader::ReadDeclRecord(DeclID ID) { 2930 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 2931 unsigned RawLocation = 0; 2932 RecordLocation Loc = DeclCursorForID(ID, RawLocation); 2933 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 2934 // Keep track of where we are in the stream, then jump back there 2935 // after reading this declaration. 2936 SavedStreamPosition SavedPosition(DeclsCursor); 2937 2938 ReadingKindTracker ReadingKind(Read_Decl, *this); 2939 2940 // Note that we are loading a declaration record. 2941 Deserializing ADecl(this); 2942 2943 DeclsCursor.JumpToBit(Loc.Offset); 2944 RecordData Record; 2945 unsigned Code = DeclsCursor.ReadCode(); 2946 unsigned Idx = 0; 2947 ASTDeclReader Reader(*this, *Loc.F, ID, RawLocation, Record,Idx); 2948 2949 Decl *D = nullptr; 2950 switch ((DeclCode)DeclsCursor.readRecord(Code, Record)) { 2951 case DECL_CONTEXT_LEXICAL: 2952 case DECL_CONTEXT_VISIBLE: 2953 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord"); 2954 case DECL_TYPEDEF: 2955 D = TypedefDecl::CreateDeserialized(Context, ID); 2956 break; 2957 case DECL_TYPEALIAS: 2958 D = TypeAliasDecl::CreateDeserialized(Context, ID); 2959 break; 2960 case DECL_ENUM: 2961 D = EnumDecl::CreateDeserialized(Context, ID); 2962 break; 2963 case DECL_RECORD: 2964 D = RecordDecl::CreateDeserialized(Context, ID); 2965 break; 2966 case DECL_ENUM_CONSTANT: 2967 D = EnumConstantDecl::CreateDeserialized(Context, ID); 2968 break; 2969 case DECL_FUNCTION: 2970 D = FunctionDecl::CreateDeserialized(Context, ID); 2971 break; 2972 case DECL_LINKAGE_SPEC: 2973 D = LinkageSpecDecl::CreateDeserialized(Context, ID); 2974 break; 2975 case DECL_LABEL: 2976 D = LabelDecl::CreateDeserialized(Context, ID); 2977 break; 2978 case DECL_NAMESPACE: 2979 D = NamespaceDecl::CreateDeserialized(Context, ID); 2980 break; 2981 case DECL_NAMESPACE_ALIAS: 2982 D = NamespaceAliasDecl::CreateDeserialized(Context, ID); 2983 break; 2984 case DECL_USING: 2985 D = UsingDecl::CreateDeserialized(Context, ID); 2986 break; 2987 case DECL_USING_SHADOW: 2988 D = UsingShadowDecl::CreateDeserialized(Context, ID); 2989 break; 2990 case DECL_USING_DIRECTIVE: 2991 D = UsingDirectiveDecl::CreateDeserialized(Context, ID); 2992 break; 2993 case DECL_UNRESOLVED_USING_VALUE: 2994 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID); 2995 break; 2996 case DECL_UNRESOLVED_USING_TYPENAME: 2997 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID); 2998 break; 2999 case DECL_CXX_RECORD: 3000 D = CXXRecordDecl::CreateDeserialized(Context, ID); 3001 break; 3002 case DECL_CXX_METHOD: 3003 D = CXXMethodDecl::CreateDeserialized(Context, ID); 3004 break; 3005 case DECL_CXX_CONSTRUCTOR: 3006 D = CXXConstructorDecl::CreateDeserialized(Context, ID); 3007 break; 3008 case DECL_CXX_DESTRUCTOR: 3009 D = CXXDestructorDecl::CreateDeserialized(Context, ID); 3010 break; 3011 case DECL_CXX_CONVERSION: 3012 D = CXXConversionDecl::CreateDeserialized(Context, ID); 3013 break; 3014 case DECL_ACCESS_SPEC: 3015 D = AccessSpecDecl::CreateDeserialized(Context, ID); 3016 break; 3017 case DECL_FRIEND: 3018 D = FriendDecl::CreateDeserialized(Context, ID, Record[Idx++]); 3019 break; 3020 case DECL_FRIEND_TEMPLATE: 3021 D = FriendTemplateDecl::CreateDeserialized(Context, ID); 3022 break; 3023 case DECL_CLASS_TEMPLATE: 3024 D = ClassTemplateDecl::CreateDeserialized(Context, ID); 3025 break; 3026 case DECL_CLASS_TEMPLATE_SPECIALIZATION: 3027 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID); 3028 break; 3029 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION: 3030 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); 3031 break; 3032 case DECL_VAR_TEMPLATE: 3033 D = VarTemplateDecl::CreateDeserialized(Context, ID); 3034 break; 3035 case DECL_VAR_TEMPLATE_SPECIALIZATION: 3036 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID); 3037 break; 3038 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION: 3039 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); 3040 break; 3041 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION: 3042 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID); 3043 break; 3044 case DECL_FUNCTION_TEMPLATE: 3045 D = FunctionTemplateDecl::CreateDeserialized(Context, ID); 3046 break; 3047 case DECL_TEMPLATE_TYPE_PARM: 3048 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID); 3049 break; 3050 case DECL_NON_TYPE_TEMPLATE_PARM: 3051 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID); 3052 break; 3053 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: 3054 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID, Record[Idx++]); 3055 break; 3056 case DECL_TEMPLATE_TEMPLATE_PARM: 3057 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID); 3058 break; 3059 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK: 3060 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID, 3061 Record[Idx++]); 3062 break; 3063 case DECL_TYPE_ALIAS_TEMPLATE: 3064 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID); 3065 break; 3066 case DECL_STATIC_ASSERT: 3067 D = StaticAssertDecl::CreateDeserialized(Context, ID); 3068 break; 3069 case DECL_OBJC_METHOD: 3070 D = ObjCMethodDecl::CreateDeserialized(Context, ID); 3071 break; 3072 case DECL_OBJC_INTERFACE: 3073 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID); 3074 break; 3075 case DECL_OBJC_IVAR: 3076 D = ObjCIvarDecl::CreateDeserialized(Context, ID); 3077 break; 3078 case DECL_OBJC_PROTOCOL: 3079 D = ObjCProtocolDecl::CreateDeserialized(Context, ID); 3080 break; 3081 case DECL_OBJC_AT_DEFS_FIELD: 3082 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID); 3083 break; 3084 case DECL_OBJC_CATEGORY: 3085 D = ObjCCategoryDecl::CreateDeserialized(Context, ID); 3086 break; 3087 case DECL_OBJC_CATEGORY_IMPL: 3088 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID); 3089 break; 3090 case DECL_OBJC_IMPLEMENTATION: 3091 D = ObjCImplementationDecl::CreateDeserialized(Context, ID); 3092 break; 3093 case DECL_OBJC_COMPATIBLE_ALIAS: 3094 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID); 3095 break; 3096 case DECL_OBJC_PROPERTY: 3097 D = ObjCPropertyDecl::CreateDeserialized(Context, ID); 3098 break; 3099 case DECL_OBJC_PROPERTY_IMPL: 3100 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID); 3101 break; 3102 case DECL_FIELD: 3103 D = FieldDecl::CreateDeserialized(Context, ID); 3104 break; 3105 case DECL_INDIRECTFIELD: 3106 D = IndirectFieldDecl::CreateDeserialized(Context, ID); 3107 break; 3108 case DECL_VAR: 3109 D = VarDecl::CreateDeserialized(Context, ID); 3110 break; 3111 case DECL_IMPLICIT_PARAM: 3112 D = ImplicitParamDecl::CreateDeserialized(Context, ID); 3113 break; 3114 case DECL_PARM_VAR: 3115 D = ParmVarDecl::CreateDeserialized(Context, ID); 3116 break; 3117 case DECL_FILE_SCOPE_ASM: 3118 D = FileScopeAsmDecl::CreateDeserialized(Context, ID); 3119 break; 3120 case DECL_BLOCK: 3121 D = BlockDecl::CreateDeserialized(Context, ID); 3122 break; 3123 case DECL_MS_PROPERTY: 3124 D = MSPropertyDecl::CreateDeserialized(Context, ID); 3125 break; 3126 case DECL_CAPTURED: 3127 D = CapturedDecl::CreateDeserialized(Context, ID, Record[Idx++]); 3128 break; 3129 case DECL_CXX_BASE_SPECIFIERS: 3130 Error("attempt to read a C++ base-specifier record as a declaration"); 3131 return nullptr; 3132 case DECL_IMPORT: 3133 // Note: last entry of the ImportDecl record is the number of stored source 3134 // locations. 3135 D = ImportDecl::CreateDeserialized(Context, ID, Record.back()); 3136 break; 3137 case DECL_OMP_THREADPRIVATE: 3138 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record[Idx++]); 3139 break; 3140 case DECL_EMPTY: 3141 D = EmptyDecl::CreateDeserialized(Context, ID); 3142 break; 3143 } 3144 3145 assert(D && "Unknown declaration reading AST file"); 3146 LoadedDecl(Index, D); 3147 // Set the DeclContext before doing any deserialization, to make sure internal 3148 // calls to Decl::getASTContext() by Decl's methods will find the 3149 // TranslationUnitDecl without crashing. 3150 D->setDeclContext(Context.getTranslationUnitDecl()); 3151 Reader.Visit(D); 3152 3153 // If this declaration is also a declaration context, get the 3154 // offsets for its tables of lexical and visible declarations. 3155 if (DeclContext *DC = dyn_cast<DeclContext>(D)) { 3156 // FIXME: This should really be 3157 // DeclContext *LookupDC = DC->getPrimaryContext(); 3158 // but that can walk the redeclaration chain, which might not work yet. 3159 DeclContext *LookupDC = DC; 3160 if (isa<NamespaceDecl>(DC)) 3161 LookupDC = DC->getPrimaryContext(); 3162 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC); 3163 if (Offsets.first || Offsets.second) { 3164 if (Offsets.first != 0) 3165 DC->setHasExternalLexicalStorage(true); 3166 if (Offsets.second != 0) 3167 LookupDC->setHasExternalVisibleStorage(true); 3168 if (ReadDeclContextStorage(*Loc.F, DeclsCursor, Offsets, 3169 Loc.F->DeclContextInfos[DC])) 3170 return nullptr; 3171 } 3172 3173 // Now add the pending visible updates for this decl context, if it has any. 3174 DeclContextVisibleUpdatesPending::iterator I = 3175 PendingVisibleUpdates.find(ID); 3176 if (I != PendingVisibleUpdates.end()) { 3177 // There are updates. This means the context has external visible 3178 // storage, even if the original stored version didn't. 3179 LookupDC->setHasExternalVisibleStorage(true); 3180 for (const auto &Update : I->second) { 3181 DeclContextInfo &Info = Update.second->DeclContextInfos[DC]; 3182 delete Info.NameLookupTableData; 3183 Info.NameLookupTableData = Update.first; 3184 } 3185 PendingVisibleUpdates.erase(I); 3186 } 3187 } 3188 assert(Idx == Record.size()); 3189 3190 // Load any relevant update records. 3191 PendingUpdateRecords.push_back(std::make_pair(ID, D)); 3192 3193 // Load the categories after recursive loading is finished. 3194 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) 3195 if (Class->isThisDeclarationADefinition()) 3196 loadObjCCategories(ID, Class); 3197 3198 // If we have deserialized a declaration that has a definition the 3199 // AST consumer might need to know about, queue it. 3200 // We don't pass it to the consumer immediately because we may be in recursive 3201 // loading, and some declarations may still be initializing. 3202 if (isConsumerInterestedIn(D, Reader.hasPendingBody())) 3203 InterestingDecls.push_back(D); 3204 3205 return D; 3206 } 3207 3208 void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) { 3209 // The declaration may have been modified by files later in the chain. 3210 // If this is the case, read the record containing the updates from each file 3211 // and pass it to ASTDeclReader to make the modifications. 3212 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID); 3213 if (UpdI != DeclUpdateOffsets.end()) { 3214 FileOffsetsTy &UpdateOffsets = UpdI->second; 3215 bool WasInteresting = isConsumerInterestedIn(D, false); 3216 for (FileOffsetsTy::iterator 3217 I = UpdateOffsets.begin(), E = UpdateOffsets.end(); I != E; ++I) { 3218 ModuleFile *F = I->first; 3219 uint64_t Offset = I->second; 3220 llvm::BitstreamCursor &Cursor = F->DeclsCursor; 3221 SavedStreamPosition SavedPosition(Cursor); 3222 Cursor.JumpToBit(Offset); 3223 RecordData Record; 3224 unsigned Code = Cursor.ReadCode(); 3225 unsigned RecCode = Cursor.readRecord(Code, Record); 3226 (void)RecCode; 3227 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!"); 3228 3229 unsigned Idx = 0; 3230 ASTDeclReader Reader(*this, *F, ID, 0, Record, Idx); 3231 Reader.UpdateDecl(D, *F, Record); 3232 3233 // We might have made this declaration interesting. If so, remember that 3234 // we need to hand it off to the consumer. 3235 if (!WasInteresting && 3236 isConsumerInterestedIn(D, Reader.hasPendingBody())) { 3237 InterestingDecls.push_back(D); 3238 WasInteresting = true; 3239 } 3240 } 3241 } 3242 } 3243 3244 namespace { 3245 /// \brief Module visitor class that finds all of the redeclarations of a 3246 /// redeclarable declaration. 3247 class RedeclChainVisitor { 3248 ASTReader &Reader; 3249 SmallVectorImpl<DeclID> &SearchDecls; 3250 llvm::SmallPtrSetImpl<Decl *> &Deserialized; 3251 GlobalDeclID CanonID; 3252 SmallVector<Decl *, 4> Chain; 3253 3254 public: 3255 RedeclChainVisitor(ASTReader &Reader, SmallVectorImpl<DeclID> &SearchDecls, 3256 llvm::SmallPtrSetImpl<Decl *> &Deserialized, 3257 GlobalDeclID CanonID) 3258 : Reader(Reader), SearchDecls(SearchDecls), Deserialized(Deserialized), 3259 CanonID(CanonID) { 3260 // Ensure that the canonical ID goes at the start of the chain. 3261 addToChain(Reader.GetDecl(CanonID)); 3262 } 3263 3264 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 3265 if (Preorder) 3266 return false; 3267 3268 return static_cast<RedeclChainVisitor *>(UserData)->visit(M); 3269 } 3270 3271 void addToChain(Decl *D) { 3272 if (!D) 3273 return; 3274 3275 if (Deserialized.erase(D)) 3276 Chain.push_back(D); 3277 } 3278 3279 void searchForID(ModuleFile &M, GlobalDeclID GlobalID) { 3280 // Map global ID of the first declaration down to the local ID 3281 // used in this module file. 3282 DeclID ID = Reader.mapGlobalIDToModuleFileGlobalID(M, GlobalID); 3283 if (!ID) 3284 return; 3285 3286 // If the search decl was from this module, add it to the chain before any 3287 // of its redeclarations in this module or users of it, and after any from 3288 // imported modules. 3289 if (CanonID != GlobalID && Reader.isDeclIDFromModule(GlobalID, M)) 3290 addToChain(Reader.GetDecl(GlobalID)); 3291 3292 // Perform a binary search to find the local redeclarations for this 3293 // declaration (if any). 3294 const LocalRedeclarationsInfo Compare = { ID, 0 }; 3295 const LocalRedeclarationsInfo *Result 3296 = std::lower_bound(M.RedeclarationsMap, 3297 M.RedeclarationsMap + M.LocalNumRedeclarationsInMap, 3298 Compare); 3299 if (Result == M.RedeclarationsMap + M.LocalNumRedeclarationsInMap || 3300 Result->FirstID != ID) { 3301 // If we have a previously-canonical singleton declaration that was 3302 // merged into another redeclaration chain, create a trivial chain 3303 // for this single declaration so that it will get wired into the 3304 // complete redeclaration chain. 3305 if (GlobalID != CanonID && 3306 GlobalID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 3307 GlobalID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls) { 3308 addToChain(Reader.GetDecl(GlobalID)); 3309 } 3310 3311 return; 3312 } 3313 3314 // Dig out all of the redeclarations. 3315 unsigned Offset = Result->Offset; 3316 unsigned N = M.RedeclarationChains[Offset]; 3317 M.RedeclarationChains[Offset++] = 0; // Don't try to deserialize again 3318 for (unsigned I = 0; I != N; ++I) 3319 addToChain(Reader.GetLocalDecl(M, M.RedeclarationChains[Offset++])); 3320 } 3321 3322 bool visit(ModuleFile &M) { 3323 // Visit each of the declarations. 3324 for (unsigned I = 0, N = SearchDecls.size(); I != N; ++I) 3325 searchForID(M, SearchDecls[I]); 3326 // FIXME: If none of the SearchDecls had local IDs in this module, can 3327 // we avoid searching any ancestor module files? 3328 return false; 3329 } 3330 3331 ArrayRef<Decl *> getChain() const { 3332 return Chain; 3333 } 3334 }; 3335 } 3336 3337 void ASTReader::loadPendingDeclChain(Decl *CanonDecl) { 3338 // The decl might have been merged into something else after being added to 3339 // our list. If it was, just skip it. 3340 if (!CanonDecl->isCanonicalDecl()) 3341 return; 3342 3343 // Determine the set of declaration IDs we'll be searching for. 3344 SmallVector<DeclID, 16> SearchDecls; 3345 GlobalDeclID CanonID = CanonDecl->getGlobalID(); 3346 if (CanonID) 3347 SearchDecls.push_back(CanonDecl->getGlobalID()); // Always first. 3348 MergedDeclsMap::iterator MergedPos = MergedDecls.find(CanonDecl); 3349 if (MergedPos != MergedDecls.end()) 3350 SearchDecls.append(MergedPos->second.begin(), MergedPos->second.end()); 3351 3352 // Build up the list of redeclarations. 3353 RedeclChainVisitor Visitor(*this, SearchDecls, RedeclsDeserialized, CanonID); 3354 ModuleMgr.visitDepthFirst(&RedeclChainVisitor::visit, &Visitor); 3355 3356 // Retrieve the chains. 3357 ArrayRef<Decl *> Chain = Visitor.getChain(); 3358 if (Chain.empty()) 3359 return; 3360 3361 // Hook up the chains. 3362 // 3363 // FIXME: We have three different dispatches on decl kind here; maybe 3364 // we should instead generate one loop per kind and dispatch up-front? 3365 Decl *MostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl); 3366 if (!MostRecent) 3367 MostRecent = CanonDecl; 3368 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3369 if (Chain[I] == CanonDecl) 3370 continue; 3371 3372 ASTDeclReader::attachPreviousDecl(*this, Chain[I], MostRecent); 3373 MostRecent = Chain[I]; 3374 } 3375 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent); 3376 } 3377 3378 namespace { 3379 /// \brief Given an ObjC interface, goes through the modules and links to the 3380 /// interface all the categories for it. 3381 class ObjCCategoriesVisitor { 3382 ASTReader &Reader; 3383 serialization::GlobalDeclID InterfaceID; 3384 ObjCInterfaceDecl *Interface; 3385 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized; 3386 unsigned PreviousGeneration; 3387 ObjCCategoryDecl *Tail; 3388 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap; 3389 3390 void add(ObjCCategoryDecl *Cat) { 3391 // Only process each category once. 3392 if (!Deserialized.erase(Cat)) 3393 return; 3394 3395 // Check for duplicate categories. 3396 if (Cat->getDeclName()) { 3397 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()]; 3398 if (Existing && 3399 Reader.getOwningModuleFile(Existing) 3400 != Reader.getOwningModuleFile(Cat)) { 3401 // FIXME: We should not warn for duplicates in diamond: 3402 // 3403 // MT // 3404 // / \ // 3405 // ML MR // 3406 // \ / // 3407 // MB // 3408 // 3409 // If there are duplicates in ML/MR, there will be warning when 3410 // creating MB *and* when importing MB. We should not warn when 3411 // importing. 3412 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def) 3413 << Interface->getDeclName() << Cat->getDeclName(); 3414 Reader.Diag(Existing->getLocation(), diag::note_previous_definition); 3415 } else if (!Existing) { 3416 // Record this category. 3417 Existing = Cat; 3418 } 3419 } 3420 3421 // Add this category to the end of the chain. 3422 if (Tail) 3423 ASTDeclReader::setNextObjCCategory(Tail, Cat); 3424 else 3425 Interface->setCategoryListRaw(Cat); 3426 Tail = Cat; 3427 } 3428 3429 public: 3430 ObjCCategoriesVisitor(ASTReader &Reader, 3431 serialization::GlobalDeclID InterfaceID, 3432 ObjCInterfaceDecl *Interface, 3433 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized, 3434 unsigned PreviousGeneration) 3435 : Reader(Reader), InterfaceID(InterfaceID), Interface(Interface), 3436 Deserialized(Deserialized), PreviousGeneration(PreviousGeneration), 3437 Tail(nullptr) 3438 { 3439 // Populate the name -> category map with the set of known categories. 3440 for (auto *Cat : Interface->known_categories()) { 3441 if (Cat->getDeclName()) 3442 NameCategoryMap[Cat->getDeclName()] = Cat; 3443 3444 // Keep track of the tail of the category list. 3445 Tail = Cat; 3446 } 3447 } 3448 3449 static bool visit(ModuleFile &M, void *UserData) { 3450 return static_cast<ObjCCategoriesVisitor *>(UserData)->visit(M); 3451 } 3452 3453 bool visit(ModuleFile &M) { 3454 // If we've loaded all of the category information we care about from 3455 // this module file, we're done. 3456 if (M.Generation <= PreviousGeneration) 3457 return true; 3458 3459 // Map global ID of the definition down to the local ID used in this 3460 // module file. If there is no such mapping, we'll find nothing here 3461 // (or in any module it imports). 3462 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID); 3463 if (!LocalID) 3464 return true; 3465 3466 // Perform a binary search to find the local redeclarations for this 3467 // declaration (if any). 3468 const ObjCCategoriesInfo Compare = { LocalID, 0 }; 3469 const ObjCCategoriesInfo *Result 3470 = std::lower_bound(M.ObjCCategoriesMap, 3471 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap, 3472 Compare); 3473 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap || 3474 Result->DefinitionID != LocalID) { 3475 // We didn't find anything. If the class definition is in this module 3476 // file, then the module files it depends on cannot have any categories, 3477 // so suppress further lookup. 3478 return Reader.isDeclIDFromModule(InterfaceID, M); 3479 } 3480 3481 // We found something. Dig out all of the categories. 3482 unsigned Offset = Result->Offset; 3483 unsigned N = M.ObjCCategories[Offset]; 3484 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again 3485 for (unsigned I = 0; I != N; ++I) 3486 add(cast_or_null<ObjCCategoryDecl>( 3487 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++]))); 3488 return true; 3489 } 3490 }; 3491 } 3492 3493 void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID, 3494 ObjCInterfaceDecl *D, 3495 unsigned PreviousGeneration) { 3496 ObjCCategoriesVisitor Visitor(*this, ID, D, CategoriesDeserialized, 3497 PreviousGeneration); 3498 ModuleMgr.visit(ObjCCategoriesVisitor::visit, &Visitor); 3499 } 3500 3501 namespace { 3502 /// Iterator over the redeclarations of a declaration that have already 3503 /// been merged into the same redeclaration chain. 3504 template<typename DeclT> 3505 class MergedRedeclIterator { 3506 DeclT *Start, *Canonical, *Current; 3507 public: 3508 MergedRedeclIterator() : Current(nullptr) {} 3509 MergedRedeclIterator(DeclT *Start) 3510 : Start(Start), Canonical(nullptr), Current(Start) {} 3511 3512 DeclT *operator*() { return Current; } 3513 3514 MergedRedeclIterator &operator++() { 3515 if (Current->isFirstDecl()) { 3516 Canonical = Current; 3517 Current = Current->getMostRecentDecl(); 3518 } else 3519 Current = Current->getPreviousDecl(); 3520 3521 // If we started in the merged portion, we'll reach our start position 3522 // eventually. Otherwise, we'll never reach it, but the second declaration 3523 // we reached was the canonical declaration, so stop when we see that one 3524 // again. 3525 if (Current == Start || Current == Canonical) 3526 Current = nullptr; 3527 return *this; 3528 } 3529 3530 friend bool operator!=(const MergedRedeclIterator &A, 3531 const MergedRedeclIterator &B) { 3532 return A.Current != B.Current; 3533 } 3534 }; 3535 } 3536 template<typename DeclT> 3537 llvm::iterator_range<MergedRedeclIterator<DeclT>> merged_redecls(DeclT *D) { 3538 return llvm::iterator_range<MergedRedeclIterator<DeclT>>( 3539 MergedRedeclIterator<DeclT>(D), 3540 MergedRedeclIterator<DeclT>()); 3541 } 3542 3543 template<typename DeclT, typename Fn> 3544 static void forAllLaterRedecls(DeclT *D, Fn F) { 3545 F(D); 3546 3547 // Check whether we've already merged D into its redeclaration chain. 3548 // MostRecent may or may not be nullptr if D has not been merged. If 3549 // not, walk the merged redecl chain and see if it's there. 3550 auto *MostRecent = D->getMostRecentDecl(); 3551 bool Found = false; 3552 for (auto *Redecl = MostRecent; Redecl && !Found; 3553 Redecl = Redecl->getPreviousDecl()) 3554 Found = (Redecl == D); 3555 3556 // If this declaration is merged, apply the functor to all later decls. 3557 if (Found) { 3558 for (auto *Redecl = MostRecent; Redecl != D; 3559 Redecl = Redecl->getPreviousDecl()) 3560 F(Redecl); 3561 } 3562 } 3563 3564 void ASTDeclReader::UpdateDecl(Decl *D, ModuleFile &ModuleFile, 3565 const RecordData &Record) { 3566 while (Idx < Record.size()) { 3567 switch ((DeclUpdateKind)Record[Idx++]) { 3568 case UPD_CXX_ADDED_IMPLICIT_MEMBER: { 3569 auto *RD = cast<CXXRecordDecl>(D); 3570 // FIXME: If we also have an update record for instantiating the 3571 // definition of D, we need that to happen before we get here. 3572 Decl *MD = Reader.ReadDecl(ModuleFile, Record, Idx); 3573 assert(MD && "couldn't read decl from update record"); 3574 // FIXME: We should call addHiddenDecl instead, to add the member 3575 // to its DeclContext. 3576 RD->addedMember(MD); 3577 3578 // If we've added a new special member to a class definition that is not 3579 // the canonical definition, then we need special member lookups in the 3580 // canonical definition to also look into our class. 3581 auto *DD = RD->DefinitionData.getNotUpdated(); 3582 if (DD && DD->Definition != RD) { 3583 auto &Merged = Reader.MergedLookups[DD->Definition]; 3584 // FIXME: Avoid the linear-time scan here. 3585 if (std::find(Merged.begin(), Merged.end(), RD) == Merged.end()) 3586 Merged.push_back(RD); 3587 } 3588 break; 3589 } 3590 3591 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: 3592 // It will be added to the template's specializations set when loaded. 3593 (void)Reader.ReadDecl(ModuleFile, Record, Idx); 3594 break; 3595 3596 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: { 3597 NamespaceDecl *Anon 3598 = Reader.ReadDeclAs<NamespaceDecl>(ModuleFile, Record, Idx); 3599 3600 // Each module has its own anonymous namespace, which is disjoint from 3601 // any other module's anonymous namespaces, so don't attach the anonymous 3602 // namespace at all. 3603 if (ModuleFile.Kind != MK_ImplicitModule && 3604 ModuleFile.Kind != MK_ExplicitModule) { 3605 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D)) 3606 TU->setAnonymousNamespace(Anon); 3607 else 3608 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon); 3609 } 3610 break; 3611 } 3612 3613 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: 3614 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation( 3615 Reader.ReadSourceLocation(ModuleFile, Record, Idx)); 3616 break; 3617 3618 case UPD_CXX_ADDED_FUNCTION_DEFINITION: { 3619 FunctionDecl *FD = cast<FunctionDecl>(D); 3620 if (Reader.PendingBodies[FD]) { 3621 // FIXME: Maybe check for ODR violations. 3622 // It's safe to stop now because this update record is always last. 3623 return; 3624 } 3625 3626 if (Record[Idx++]) { 3627 // Maintain AST consistency: any later redeclarations of this function 3628 // are inline if this one is. (We might have merged another declaration 3629 // into this one.) 3630 forAllLaterRedecls(FD, [](FunctionDecl *FD) { 3631 FD->setImplicitlyInline(); 3632 }); 3633 } 3634 FD->setInnerLocStart(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); 3635 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) 3636 std::tie(CD->CtorInitializers, CD->NumCtorInitializers) = 3637 Reader.ReadCXXCtorInitializers(ModuleFile, Record, Idx); 3638 // Store the offset of the body so we can lazily load it later. 3639 Reader.PendingBodies[FD] = GetCurrentCursorOffset(); 3640 HasPendingBody = true; 3641 assert(Idx == Record.size() && "lazy body must be last"); 3642 break; 3643 } 3644 3645 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: { 3646 auto *RD = cast<CXXRecordDecl>(D); 3647 auto *OldDD = RD->DefinitionData.getNotUpdated(); 3648 bool HadRealDefinition = 3649 OldDD && (OldDD->Definition != RD || 3650 !Reader.PendingFakeDefinitionData.count(OldDD)); 3651 ReadCXXRecordDefinition(RD, /*Update*/true); 3652 3653 // Visible update is handled separately. 3654 uint64_t LexicalOffset = Record[Idx++]; 3655 if (!HadRealDefinition && LexicalOffset) { 3656 RD->setHasExternalLexicalStorage(true); 3657 Reader.ReadDeclContextStorage(ModuleFile, ModuleFile.DeclsCursor, 3658 std::make_pair(LexicalOffset, 0), 3659 ModuleFile.DeclContextInfos[RD]); 3660 Reader.PendingFakeDefinitionData.erase(OldDD); 3661 } 3662 3663 auto TSK = (TemplateSpecializationKind)Record[Idx++]; 3664 SourceLocation POI = Reader.ReadSourceLocation(ModuleFile, Record, Idx); 3665 if (MemberSpecializationInfo *MSInfo = 3666 RD->getMemberSpecializationInfo()) { 3667 MSInfo->setTemplateSpecializationKind(TSK); 3668 MSInfo->setPointOfInstantiation(POI); 3669 } else { 3670 ClassTemplateSpecializationDecl *Spec = 3671 cast<ClassTemplateSpecializationDecl>(RD); 3672 Spec->setTemplateSpecializationKind(TSK); 3673 Spec->setPointOfInstantiation(POI); 3674 3675 if (Record[Idx++]) { 3676 auto PartialSpec = 3677 ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx); 3678 SmallVector<TemplateArgument, 8> TemplArgs; 3679 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 3680 auto *TemplArgList = TemplateArgumentList::CreateCopy( 3681 Reader.getContext(), TemplArgs.data(), TemplArgs.size()); 3682 3683 // FIXME: If we already have a partial specialization set, 3684 // check that it matches. 3685 if (!Spec->getSpecializedTemplateOrPartial() 3686 .is<ClassTemplatePartialSpecializationDecl *>()) 3687 Spec->setInstantiationOf(PartialSpec, TemplArgList); 3688 } 3689 } 3690 3691 RD->setTagKind((TagTypeKind)Record[Idx++]); 3692 RD->setLocation(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); 3693 RD->setLocStart(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); 3694 RD->setRBraceLoc(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); 3695 3696 if (Record[Idx++]) { 3697 AttrVec Attrs; 3698 Reader.ReadAttributes(F, Attrs, Record, Idx); 3699 D->setAttrsImpl(Attrs, Reader.getContext()); 3700 } 3701 break; 3702 } 3703 3704 case UPD_CXX_RESOLVED_DTOR_DELETE: { 3705 // Set the 'operator delete' directly to avoid emitting another update 3706 // record. 3707 auto *Del = Reader.ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 3708 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl()); 3709 // FIXME: Check consistency if we have an old and new operator delete. 3710 if (!First->OperatorDelete) 3711 First->OperatorDelete = Del; 3712 break; 3713 } 3714 3715 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: { 3716 // FIXME: This doesn't send the right notifications if there are 3717 // ASTMutationListeners other than an ASTWriter. 3718 FunctionProtoType::ExceptionSpecInfo ESI; 3719 SmallVector<QualType, 8> ExceptionStorage; 3720 Reader.readExceptionSpec(ModuleFile, ExceptionStorage, ESI, Record, Idx); 3721 for (auto *Redecl : merged_redecls(D)) { 3722 auto *FD = cast<FunctionDecl>(Redecl); 3723 auto *FPT = FD->getType()->castAs<FunctionProtoType>(); 3724 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { 3725 // AST invariant: if any exception spec in the redecl chain is 3726 // resolved, all are resolved. We don't need to go any further. 3727 // FIXME: If the exception spec is resolved, check that it matches. 3728 break; 3729 } 3730 FD->setType(Reader.Context.getFunctionType( 3731 FPT->getReturnType(), FPT->getParamTypes(), 3732 FPT->getExtProtoInfo().withExceptionSpec(ESI))); 3733 } 3734 break; 3735 } 3736 3737 case UPD_CXX_DEDUCED_RETURN_TYPE: { 3738 // FIXME: Also do this when merging redecls. 3739 QualType DeducedResultType = Reader.readType(ModuleFile, Record, Idx); 3740 for (auto *Redecl : merged_redecls(D)) { 3741 // FIXME: If the return type is already deduced, check that it matches. 3742 FunctionDecl *FD = cast<FunctionDecl>(Redecl); 3743 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType); 3744 } 3745 break; 3746 } 3747 3748 case UPD_DECL_MARKED_USED: { 3749 // FIXME: This doesn't send the right notifications if there are 3750 // ASTMutationListeners other than an ASTWriter. 3751 3752 // Maintain AST consistency: any later redeclarations are used too. 3753 forAllLaterRedecls(D, [](Decl *D) { D->Used = true; }); 3754 break; 3755 } 3756 3757 case UPD_MANGLING_NUMBER: 3758 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record[Idx++]); 3759 break; 3760 3761 case UPD_STATIC_LOCAL_NUMBER: 3762 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record[Idx++]); 3763 break; 3764 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE: 3765 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3766 Reader.Context, ReadSourceRange(Record, Idx))); 3767 break; 3768 } 3769 } 3770 } 3771