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