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