1 //===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the ASTReader::ReadDeclRecord method, which is the 11 // entrypoint for loading a decl. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ASTCommon.h" 16 #include "clang/Serialization/ASTReader.h" 17 #include "clang/Sema/IdentifierResolver.h" 18 #include "clang/Sema/Sema.h" 19 #include "clang/Sema/SemaDiagnostic.h" 20 #include "clang/AST/ASTConsumer.h" 21 #include "clang/AST/ASTContext.h" 22 #include "clang/AST/DeclVisitor.h" 23 #include "clang/AST/DeclGroup.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/DeclTemplate.h" 26 #include "clang/AST/Expr.h" 27 using namespace clang; 28 using namespace clang::serialization; 29 30 //===----------------------------------------------------------------------===// 31 // Declaration deserialization 32 //===----------------------------------------------------------------------===// 33 34 namespace clang { 35 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> { 36 ASTReader &Reader; 37 ModuleFile &F; 38 llvm::BitstreamCursor &Cursor; 39 const DeclID ThisDeclID; 40 const unsigned RawLocation; 41 typedef ASTReader::RecordData RecordData; 42 const RecordData &Record; 43 unsigned &Idx; 44 TypeID TypeIDForTypeDecl; 45 46 DeclID DeclContextIDForTemplateParmDecl; 47 DeclID LexicalDeclContextIDForTemplateParmDecl; 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 ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data, 104 const RecordData &R, unsigned &I); 105 106 /// \brief RAII class used to capture the first ID within a redeclaration 107 /// chain and to introduce it into the list of pending redeclaration chains 108 /// on destruction. 109 /// 110 /// The caller can choose not to introduce this ID into the redeclaration 111 /// chain by calling \c suppress(). 112 class RedeclarableResult { 113 ASTReader &Reader; 114 GlobalDeclID FirstID; 115 mutable bool Owning; 116 117 RedeclarableResult &operator=(RedeclarableResult&); // DO NOT IMPLEMENT 118 119 public: 120 RedeclarableResult(ASTReader &Reader, GlobalDeclID FirstID) 121 : Reader(Reader), FirstID(FirstID), Owning(true) { } 122 123 RedeclarableResult(const RedeclarableResult &Other) 124 : Reader(Other.Reader), FirstID(Other.FirstID), Owning(Other.Owning) 125 { 126 Other.Owning = false; 127 } 128 129 ~RedeclarableResult() { 130 // FIXME: We want to suppress this when the declaration is local to 131 // a function, since there's no reason to search other AST files 132 // for redeclarations (they can't exist). However, this is hard to 133 // do locally because the declaration hasn't necessarily loaded its 134 // declaration context yet. Also, local externs still have the function 135 // as their (semantic) declaration context, which is wrong and would 136 // break this optimize. 137 138 if (FirstID && Owning && Reader.PendingDeclChainsKnown.insert(FirstID)) 139 Reader.PendingDeclChains.push_back(FirstID); 140 } 141 142 /// \brief Retrieve the first ID. 143 GlobalDeclID getFirstID() const { return FirstID; } 144 145 /// \brief Do not introduce this declaration ID into the set of pending 146 /// declaration chains. 147 void suppress() { 148 Owning = false; 149 } 150 }; 151 152 /// \brief Class used to capture the result of searching for an existing 153 /// declaration of a specific kind and name, along with the ability 154 /// to update the place where this result was found (the declaration 155 /// chain hanging off an identifier or the DeclContext we searched in) 156 /// if requested. 157 class FindExistingResult { 158 ASTReader &Reader; 159 NamedDecl *New; 160 NamedDecl *Existing; 161 mutable bool AddResult; 162 163 FindExistingResult &operator=(FindExistingResult&); // DO NOT IMPLEMENT 164 165 public: 166 FindExistingResult(ASTReader &Reader) 167 : Reader(Reader), New(0), Existing(0), AddResult(false) { } 168 169 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing) 170 : Reader(Reader), New(New), Existing(Existing), AddResult(true) { } 171 172 FindExistingResult(const FindExistingResult &Other) 173 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing), 174 AddResult(Other.AddResult) 175 { 176 Other.AddResult = false; 177 } 178 179 ~FindExistingResult(); 180 181 /// \brief Suppress the addition of this result into the known set of 182 /// names. 183 void suppress() { AddResult = false; } 184 185 operator NamedDecl*() const { return Existing; } 186 187 template<typename T> 188 operator T*() const { return dyn_cast_or_null<T>(Existing); } 189 }; 190 191 FindExistingResult findExisting(NamedDecl *D); 192 193 public: 194 ASTDeclReader(ASTReader &Reader, ModuleFile &F, 195 llvm::BitstreamCursor &Cursor, DeclID thisDeclID, 196 unsigned RawLocation, 197 const RecordData &Record, unsigned &Idx) 198 : Reader(Reader), F(F), Cursor(Cursor), ThisDeclID(thisDeclID), 199 RawLocation(RawLocation), Record(Record), Idx(Idx), 200 TypeIDForTypeDecl(0) { } 201 202 static void attachPreviousDecl(Decl *D, Decl *previous); 203 static void attachLatestDecl(Decl *D, Decl *latest); 204 205 void Visit(Decl *D); 206 207 void UpdateDecl(Decl *D, ModuleFile &ModuleFile, 208 const RecordData &Record); 209 210 static void setNextObjCCategory(ObjCCategoryDecl *Cat, 211 ObjCCategoryDecl *Next) { 212 Cat->NextClassCategory = Next; 213 } 214 215 void VisitDecl(Decl *D); 216 void VisitTranslationUnitDecl(TranslationUnitDecl *TU); 217 void VisitNamedDecl(NamedDecl *ND); 218 void VisitLabelDecl(LabelDecl *LD); 219 void VisitNamespaceDecl(NamespaceDecl *D); 220 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 221 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 222 void VisitTypeDecl(TypeDecl *TD); 223 void VisitTypedefNameDecl(TypedefNameDecl *TD); 224 void VisitTypedefDecl(TypedefDecl *TD); 225 void VisitTypeAliasDecl(TypeAliasDecl *TD); 226 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 227 void VisitTagDecl(TagDecl *TD); 228 void VisitEnumDecl(EnumDecl *ED); 229 void VisitRecordDecl(RecordDecl *RD); 230 void VisitCXXRecordDecl(CXXRecordDecl *D); 231 void VisitClassTemplateSpecializationDecl( 232 ClassTemplateSpecializationDecl *D); 233 void VisitClassTemplatePartialSpecializationDecl( 234 ClassTemplatePartialSpecializationDecl *D); 235 void VisitClassScopeFunctionSpecializationDecl( 236 ClassScopeFunctionSpecializationDecl *D); 237 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 238 void VisitValueDecl(ValueDecl *VD); 239 void VisitEnumConstantDecl(EnumConstantDecl *ECD); 240 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 241 void VisitDeclaratorDecl(DeclaratorDecl *DD); 242 void VisitFunctionDecl(FunctionDecl *FD); 243 void VisitCXXMethodDecl(CXXMethodDecl *D); 244 void VisitCXXConstructorDecl(CXXConstructorDecl *D); 245 void VisitCXXDestructorDecl(CXXDestructorDecl *D); 246 void VisitCXXConversionDecl(CXXConversionDecl *D); 247 void VisitFieldDecl(FieldDecl *FD); 248 void VisitIndirectFieldDecl(IndirectFieldDecl *FD); 249 void VisitVarDecl(VarDecl *VD); 250 void VisitImplicitParamDecl(ImplicitParamDecl *PD); 251 void VisitParmVarDecl(ParmVarDecl *PD); 252 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 253 void VisitTemplateDecl(TemplateDecl *D); 254 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D); 255 void VisitClassTemplateDecl(ClassTemplateDecl *D); 256 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 257 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 258 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); 259 void VisitUsingDecl(UsingDecl *D); 260 void VisitUsingShadowDecl(UsingShadowDecl *D); 261 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 262 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD); 263 void VisitImportDecl(ImportDecl *D); 264 void VisitAccessSpecDecl(AccessSpecDecl *D); 265 void VisitFriendDecl(FriendDecl *D); 266 void VisitFriendTemplateDecl(FriendTemplateDecl *D); 267 void VisitStaticAssertDecl(StaticAssertDecl *D); 268 void VisitBlockDecl(BlockDecl *BD); 269 270 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC); 271 272 template<typename T> 273 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D); 274 275 template<typename T> 276 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl); 277 278 // FIXME: Reorder according to DeclNodes.td? 279 void VisitObjCMethodDecl(ObjCMethodDecl *D); 280 void VisitObjCContainerDecl(ObjCContainerDecl *D); 281 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 282 void VisitObjCIvarDecl(ObjCIvarDecl *D); 283 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 284 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D); 285 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 286 void VisitObjCImplDecl(ObjCImplDecl *D); 287 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 288 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 289 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 290 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 291 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 292 }; 293 } 294 295 uint64_t ASTDeclReader::GetCurrentCursorOffset() { 296 return F.DeclsCursor.GetCurrentBitNo() + F.GlobalBitOffset; 297 } 298 299 void ASTDeclReader::Visit(Decl *D) { 300 DeclVisitor<ASTDeclReader, void>::Visit(D); 301 302 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 303 if (DD->DeclInfo) { 304 DeclaratorDecl::ExtInfo *Info = 305 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>(); 306 Info->TInfo = 307 GetTypeSourceInfo(Record, Idx); 308 } 309 else { 310 DD->DeclInfo = GetTypeSourceInfo(Record, Idx); 311 } 312 } 313 314 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { 315 // if we have a fully initialized TypeDecl, we can safely read its type now. 316 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull()); 317 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 318 // if we have a fully initialized TypeDecl, we can safely read its type now. 319 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull(); 320 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 321 // FunctionDecl's body was written last after all other Stmts/Exprs. 322 if (Record[Idx++]) 323 FD->setLazyBody(GetCurrentCursorOffset()); 324 } else if (D->isTemplateParameter()) { 325 // If we have a fully initialized template parameter, we can now 326 // set its DeclContext. 327 D->setDeclContext( 328 cast_or_null<DeclContext>( 329 Reader.GetDecl(DeclContextIDForTemplateParmDecl))); 330 D->setLexicalDeclContext( 331 cast_or_null<DeclContext>( 332 Reader.GetDecl(LexicalDeclContextIDForTemplateParmDecl))); 333 } 334 } 335 336 void ASTDeclReader::VisitDecl(Decl *D) { 337 if (D->isTemplateParameter()) { 338 // We don't want to deserialize the DeclContext of a template 339 // parameter immediately, because the template parameter might be 340 // used in the formulation of its DeclContext. Use the translation 341 // unit DeclContext as a placeholder. 342 DeclContextIDForTemplateParmDecl = ReadDeclID(Record, Idx); 343 LexicalDeclContextIDForTemplateParmDecl = ReadDeclID(Record, Idx); 344 D->setDeclContext(Reader.getContext().getTranslationUnitDecl()); 345 } else { 346 D->setDeclContext(ReadDeclAs<DeclContext>(Record, Idx)); 347 D->setLexicalDeclContext(ReadDeclAs<DeclContext>(Record, Idx)); 348 } 349 D->setLocation(Reader.ReadSourceLocation(F, RawLocation)); 350 D->setInvalidDecl(Record[Idx++]); 351 if (Record[Idx++]) { // hasAttrs 352 AttrVec Attrs; 353 Reader.ReadAttributes(F, Attrs, Record, Idx); 354 D->setAttrs(Attrs); 355 } 356 D->setImplicit(Record[Idx++]); 357 D->setUsed(Record[Idx++]); 358 D->setReferenced(Record[Idx++]); 359 D->setTopLevelDeclInObjCContainer(Record[Idx++]); 360 D->setAccess((AccessSpecifier)Record[Idx++]); 361 D->FromASTFile = true; 362 D->setModulePrivate(Record[Idx++]); 363 D->Hidden = D->isModulePrivate(); 364 365 // Determine whether this declaration is part of a (sub)module. If so, it 366 // may not yet be visible. 367 if (unsigned SubmoduleID = readSubmoduleID(Record, Idx)) { 368 // Store the owning submodule ID in the declaration. 369 D->setOwningModuleID(SubmoduleID); 370 371 // Module-private declarations are never visible, so there is no work to do. 372 if (!D->isModulePrivate()) { 373 if (Module *Owner = Reader.getSubmodule(SubmoduleID)) { 374 if (Owner->NameVisibility != Module::AllVisible) { 375 // The owning module is not visible. Mark this declaration as hidden. 376 D->Hidden = true; 377 378 // Note that this declaration was hidden because its owning module is 379 // not yet visible. 380 Reader.HiddenNamesMap[Owner].push_back(D); 381 } 382 } 383 } 384 } 385 } 386 387 void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) { 388 llvm_unreachable("Translation units are not serialized"); 389 } 390 391 void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) { 392 VisitDecl(ND); 393 ND->setDeclName(Reader.ReadDeclarationName(F, Record, Idx)); 394 } 395 396 void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) { 397 VisitNamedDecl(TD); 398 TD->setLocStart(ReadSourceLocation(Record, Idx)); 399 // Delay type reading until after we have fully initialized the decl. 400 TypeIDForTypeDecl = Reader.getGlobalTypeID(F, Record[Idx++]); 401 } 402 403 void ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) { 404 RedeclarableResult Redecl = VisitRedeclarable(TD); 405 VisitTypeDecl(TD); 406 407 TD->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx)); 408 mergeRedeclarable(TD, Redecl); 409 } 410 411 void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) { 412 VisitTypedefNameDecl(TD); 413 } 414 415 void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) { 416 VisitTypedefNameDecl(TD); 417 } 418 419 void ASTDeclReader::VisitTagDecl(TagDecl *TD) { 420 RedeclarableResult Redecl = VisitRedeclarable(TD); 421 VisitTypeDecl(TD); 422 423 TD->IdentifierNamespace = Record[Idx++]; 424 TD->setTagKind((TagDecl::TagKind)Record[Idx++]); 425 TD->setCompleteDefinition(Record[Idx++]); 426 TD->setEmbeddedInDeclarator(Record[Idx++]); 427 TD->setFreeStanding(Record[Idx++]); 428 TD->setRBraceLoc(ReadSourceLocation(Record, Idx)); 429 430 if (Record[Idx++]) { // hasExtInfo 431 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo(); 432 ReadQualifierInfo(*Info, Record, Idx); 433 TD->TypedefNameDeclOrQualifier = Info; 434 } else 435 TD->setTypedefNameForAnonDecl(ReadDeclAs<TypedefNameDecl>(Record, Idx)); 436 437 mergeRedeclarable(TD, Redecl); 438 } 439 440 void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) { 441 VisitTagDecl(ED); 442 if (TypeSourceInfo *TI = Reader.GetTypeSourceInfo(F, Record, Idx)) 443 ED->setIntegerTypeSourceInfo(TI); 444 else 445 ED->setIntegerType(Reader.readType(F, Record, Idx)); 446 ED->setPromotionType(Reader.readType(F, Record, Idx)); 447 ED->setNumPositiveBits(Record[Idx++]); 448 ED->setNumNegativeBits(Record[Idx++]); 449 ED->IsScoped = Record[Idx++]; 450 ED->IsScopedUsingClassTag = Record[Idx++]; 451 ED->IsFixed = Record[Idx++]; 452 ED->setInstantiationOfMemberEnum(ReadDeclAs<EnumDecl>(Record, Idx)); 453 } 454 455 void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) { 456 VisitTagDecl(RD); 457 RD->setHasFlexibleArrayMember(Record[Idx++]); 458 RD->setAnonymousStructOrUnion(Record[Idx++]); 459 RD->setHasObjectMember(Record[Idx++]); 460 } 461 462 void ASTDeclReader::VisitValueDecl(ValueDecl *VD) { 463 VisitNamedDecl(VD); 464 VD->setType(Reader.readType(F, Record, Idx)); 465 } 466 467 void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) { 468 VisitValueDecl(ECD); 469 if (Record[Idx++]) 470 ECD->setInitExpr(Reader.ReadExpr(F)); 471 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx)); 472 } 473 474 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) { 475 VisitValueDecl(DD); 476 DD->setInnerLocStart(ReadSourceLocation(Record, Idx)); 477 if (Record[Idx++]) { // hasExtInfo 478 DeclaratorDecl::ExtInfo *Info 479 = new (Reader.getContext()) DeclaratorDecl::ExtInfo(); 480 ReadQualifierInfo(*Info, Record, Idx); 481 DD->DeclInfo = Info; 482 } 483 } 484 485 void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) { 486 RedeclarableResult Redecl = VisitRedeclarable(FD); 487 VisitDeclaratorDecl(FD); 488 489 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName(), Record, Idx); 490 FD->IdentifierNamespace = Record[Idx++]; 491 492 // FunctionDecl's body is handled last at ASTDeclReader::Visit, 493 // after everything else is read. 494 495 FD->SClass = (StorageClass)Record[Idx++]; 496 FD->SClassAsWritten = (StorageClass)Record[Idx++]; 497 FD->IsInline = Record[Idx++]; 498 FD->IsInlineSpecified = Record[Idx++]; 499 FD->IsVirtualAsWritten = Record[Idx++]; 500 FD->IsPure = Record[Idx++]; 501 FD->HasInheritedPrototype = Record[Idx++]; 502 FD->HasWrittenPrototype = Record[Idx++]; 503 FD->IsDeleted = Record[Idx++]; 504 FD->IsTrivial = Record[Idx++]; 505 FD->IsDefaulted = Record[Idx++]; 506 FD->IsExplicitlyDefaulted = Record[Idx++]; 507 FD->HasImplicitReturnZero = Record[Idx++]; 508 FD->IsConstexpr = Record[Idx++]; 509 FD->EndRangeLoc = ReadSourceLocation(Record, Idx); 510 511 switch ((FunctionDecl::TemplatedKind)Record[Idx++]) { 512 default: llvm_unreachable("Unhandled TemplatedKind!"); 513 case FunctionDecl::TK_NonTemplate: 514 mergeRedeclarable(FD, Redecl); 515 break; 516 case FunctionDecl::TK_FunctionTemplate: 517 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>(Record, 518 Idx)); 519 break; 520 case FunctionDecl::TK_MemberSpecialization: { 521 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>(Record, Idx); 522 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; 523 SourceLocation POI = ReadSourceLocation(Record, Idx); 524 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK); 525 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); 526 break; 527 } 528 case FunctionDecl::TK_FunctionTemplateSpecialization: { 529 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>(Record, 530 Idx); 531 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; 532 533 // Template arguments. 534 SmallVector<TemplateArgument, 8> TemplArgs; 535 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 536 537 // Template args as written. 538 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs; 539 SourceLocation LAngleLoc, RAngleLoc; 540 bool HasTemplateArgumentsAsWritten = Record[Idx++]; 541 if (HasTemplateArgumentsAsWritten) { 542 unsigned NumTemplateArgLocs = Record[Idx++]; 543 TemplArgLocs.reserve(NumTemplateArgLocs); 544 for (unsigned i=0; i != NumTemplateArgLocs; ++i) 545 TemplArgLocs.push_back( 546 Reader.ReadTemplateArgumentLoc(F, Record, Idx)); 547 548 LAngleLoc = ReadSourceLocation(Record, Idx); 549 RAngleLoc = ReadSourceLocation(Record, Idx); 550 } 551 552 SourceLocation POI = ReadSourceLocation(Record, Idx); 553 554 ASTContext &C = Reader.getContext(); 555 TemplateArgumentList *TemplArgList 556 = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), TemplArgs.size()); 557 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 558 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i) 559 TemplArgsInfo.addArgument(TemplArgLocs[i]); 560 FunctionTemplateSpecializationInfo *FTInfo 561 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK, 562 TemplArgList, 563 HasTemplateArgumentsAsWritten ? &TemplArgsInfo : 0, 564 POI); 565 FD->TemplateOrSpecialization = FTInfo; 566 567 if (FD->isCanonicalDecl()) { // if canonical add to template's set. 568 // The template that contains the specializations set. It's not safe to 569 // use getCanonicalDecl on Template since it may still be initializing. 570 FunctionTemplateDecl *CanonTemplate 571 = ReadDeclAs<FunctionTemplateDecl>(Record, Idx); 572 // Get the InsertPos by FindNodeOrInsertPos() instead of calling 573 // InsertNode(FTInfo) directly to avoid the getASTContext() call in 574 // FunctionTemplateSpecializationInfo's Profile(). 575 // We avoid getASTContext because a decl in the parent hierarchy may 576 // be initializing. 577 llvm::FoldingSetNodeID ID; 578 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs.data(), 579 TemplArgs.size(), C); 580 void *InsertPos = 0; 581 CanonTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); 582 assert(InsertPos && "Another specialization already inserted!"); 583 CanonTemplate->getSpecializations().InsertNode(FTInfo, InsertPos); 584 } 585 break; 586 } 587 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { 588 // Templates. 589 UnresolvedSet<8> TemplDecls; 590 unsigned NumTemplates = Record[Idx++]; 591 while (NumTemplates--) 592 TemplDecls.addDecl(ReadDeclAs<NamedDecl>(Record, Idx)); 593 594 // Templates args. 595 TemplateArgumentListInfo TemplArgs; 596 unsigned NumArgs = Record[Idx++]; 597 while (NumArgs--) 598 TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(F, Record, Idx)); 599 TemplArgs.setLAngleLoc(ReadSourceLocation(Record, Idx)); 600 TemplArgs.setRAngleLoc(ReadSourceLocation(Record, Idx)); 601 602 FD->setDependentTemplateSpecialization(Reader.getContext(), 603 TemplDecls, TemplArgs); 604 break; 605 } 606 } 607 608 // Read in the parameters. 609 unsigned NumParams = Record[Idx++]; 610 SmallVector<ParmVarDecl *, 16> Params; 611 Params.reserve(NumParams); 612 for (unsigned I = 0; I != NumParams; ++I) 613 Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx)); 614 FD->setParams(Reader.getContext(), Params); 615 } 616 617 void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { 618 VisitNamedDecl(MD); 619 if (Record[Idx++]) { 620 // In practice, this won't be executed (since method definitions 621 // don't occur in header files). 622 MD->setBody(Reader.ReadStmt(F)); 623 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx)); 624 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx)); 625 } 626 MD->setInstanceMethod(Record[Idx++]); 627 MD->setVariadic(Record[Idx++]); 628 MD->setSynthesized(Record[Idx++]); 629 MD->setDefined(Record[Idx++]); 630 631 MD->IsRedeclaration = Record[Idx++]; 632 MD->HasRedeclaration = Record[Idx++]; 633 if (MD->HasRedeclaration) 634 Reader.getContext().setObjCMethodRedeclaration(MD, 635 ReadDeclAs<ObjCMethodDecl>(Record, Idx)); 636 637 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]); 638 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); 639 MD->SetRelatedResultType(Record[Idx++]); 640 MD->setResultType(Reader.readType(F, Record, Idx)); 641 MD->setResultTypeSourceInfo(GetTypeSourceInfo(Record, Idx)); 642 MD->setEndLoc(ReadSourceLocation(Record, Idx)); 643 unsigned NumParams = Record[Idx++]; 644 SmallVector<ParmVarDecl *, 16> Params; 645 Params.reserve(NumParams); 646 for (unsigned I = 0; I != NumParams; ++I) 647 Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx)); 648 649 MD->SelLocsKind = Record[Idx++]; 650 unsigned NumStoredSelLocs = Record[Idx++]; 651 SmallVector<SourceLocation, 16> SelLocs; 652 SelLocs.reserve(NumStoredSelLocs); 653 for (unsigned i = 0; i != NumStoredSelLocs; ++i) 654 SelLocs.push_back(ReadSourceLocation(Record, Idx)); 655 656 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs); 657 } 658 659 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) { 660 VisitNamedDecl(CD); 661 CD->setAtStartLoc(ReadSourceLocation(Record, Idx)); 662 CD->setAtEndRange(ReadSourceRange(Record, Idx)); 663 } 664 665 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) { 666 RedeclarableResult Redecl = VisitRedeclarable(ID); 667 VisitObjCContainerDecl(ID); 668 TypeIDForTypeDecl = Reader.getGlobalTypeID(F, Record[Idx++]); 669 mergeRedeclarable(ID, Redecl); 670 671 if (Record[Idx++]) { 672 // Read the definition. 673 ID->allocateDefinitionData(); 674 675 // Set the definition data of the canonical declaration, so other 676 // redeclarations will see it. 677 ID->getCanonicalDecl()->Data = ID->Data; 678 679 ObjCInterfaceDecl::DefinitionData &Data = ID->data(); 680 681 // Read the superclass. 682 Data.SuperClass = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx); 683 Data.SuperClassLoc = ReadSourceLocation(Record, Idx); 684 685 Data.EndLoc = ReadSourceLocation(Record, Idx); 686 687 // Read the directly referenced protocols and their SourceLocations. 688 unsigned NumProtocols = Record[Idx++]; 689 SmallVector<ObjCProtocolDecl *, 16> Protocols; 690 Protocols.reserve(NumProtocols); 691 for (unsigned I = 0; I != NumProtocols; ++I) 692 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx)); 693 SmallVector<SourceLocation, 16> ProtoLocs; 694 ProtoLocs.reserve(NumProtocols); 695 for (unsigned I = 0; I != NumProtocols; ++I) 696 ProtoLocs.push_back(ReadSourceLocation(Record, Idx)); 697 ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(), 698 Reader.getContext()); 699 700 // Read the transitive closure of protocols referenced by this class. 701 NumProtocols = Record[Idx++]; 702 Protocols.clear(); 703 Protocols.reserve(NumProtocols); 704 for (unsigned I = 0; I != NumProtocols; ++I) 705 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx)); 706 ID->data().AllReferencedProtocols.set(Protocols.data(), NumProtocols, 707 Reader.getContext()); 708 709 // Read the ivars. 710 unsigned NumIvars = Record[Idx++]; 711 SmallVector<ObjCIvarDecl *, 16> IVars; 712 IVars.reserve(NumIvars); 713 for (unsigned I = 0; I != NumIvars; ++I) 714 IVars.push_back(ReadDeclAs<ObjCIvarDecl>(Record, Idx)); 715 716 // Read the categories. 717 ID->setCategoryList(ReadDeclAs<ObjCCategoryDecl>(Record, Idx)); 718 719 // We will rebuild this list lazily. 720 ID->setIvarList(0); 721 722 // Note that we have deserialized a definition. 723 Reader.PendingDefinitions.insert(ID); 724 } else { 725 ID->Data = ID->getCanonicalDecl()->Data; 726 } 727 } 728 729 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) { 730 VisitFieldDecl(IVD); 731 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]); 732 // This field will be built lazily. 733 IVD->setNextIvar(0); 734 bool synth = Record[Idx++]; 735 IVD->setSynthesize(synth); 736 } 737 738 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) { 739 RedeclarableResult Redecl = VisitRedeclarable(PD); 740 VisitObjCContainerDecl(PD); 741 mergeRedeclarable(PD, Redecl); 742 743 if (Record[Idx++]) { 744 // Read the definition. 745 PD->allocateDefinitionData(); 746 747 // Set the definition data of the canonical declaration, so other 748 // redeclarations will see it. 749 PD->getCanonicalDecl()->Data = PD->Data; 750 751 unsigned NumProtoRefs = Record[Idx++]; 752 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; 753 ProtoRefs.reserve(NumProtoRefs); 754 for (unsigned I = 0; I != NumProtoRefs; ++I) 755 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx)); 756 SmallVector<SourceLocation, 16> ProtoLocs; 757 ProtoLocs.reserve(NumProtoRefs); 758 for (unsigned I = 0; I != NumProtoRefs; ++I) 759 ProtoLocs.push_back(ReadSourceLocation(Record, Idx)); 760 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(), 761 Reader.getContext()); 762 763 // Note that we have deserialized a definition. 764 Reader.PendingDefinitions.insert(PD); 765 } else { 766 PD->Data = PD->getCanonicalDecl()->Data; 767 } 768 } 769 770 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) { 771 VisitFieldDecl(FD); 772 } 773 774 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) { 775 VisitObjCContainerDecl(CD); 776 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx); 777 unsigned NumProtoRefs = Record[Idx++]; 778 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs; 779 ProtoRefs.reserve(NumProtoRefs); 780 for (unsigned I = 0; I != NumProtoRefs; ++I) 781 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx)); 782 SmallVector<SourceLocation, 16> ProtoLocs; 783 ProtoLocs.reserve(NumProtoRefs); 784 for (unsigned I = 0; I != NumProtoRefs; ++I) 785 ProtoLocs.push_back(ReadSourceLocation(Record, Idx)); 786 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(), 787 Reader.getContext()); 788 CD->NextClassCategory = ReadDeclAs<ObjCCategoryDecl>(Record, Idx); 789 CD->setHasSynthBitfield(Record[Idx++]); 790 CD->setCategoryNameLoc(ReadSourceLocation(Record, Idx)); 791 } 792 793 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) { 794 VisitNamedDecl(CAD); 795 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx)); 796 } 797 798 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 799 VisitNamedDecl(D); 800 D->setAtLoc(ReadSourceLocation(Record, Idx)); 801 D->setType(GetTypeSourceInfo(Record, Idx)); 802 // FIXME: stable encoding 803 D->setPropertyAttributes( 804 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]); 805 D->setPropertyAttributesAsWritten( 806 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]); 807 // FIXME: stable encoding 808 D->setPropertyImplementation( 809 (ObjCPropertyDecl::PropertyControl)Record[Idx++]); 810 D->setGetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector()); 811 D->setSetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector()); 812 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx)); 813 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx)); 814 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>(Record, Idx)); 815 } 816 817 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) { 818 VisitObjCContainerDecl(D); 819 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx)); 820 } 821 822 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 823 VisitObjCImplDecl(D); 824 D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx)); 825 D->CategoryNameLoc = ReadSourceLocation(Record, Idx); 826 } 827 828 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 829 VisitObjCImplDecl(D); 830 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx)); 831 llvm::tie(D->IvarInitializers, D->NumIvarInitializers) 832 = Reader.ReadCXXCtorInitializers(F, Record, Idx); 833 D->setHasSynthBitfield(Record[Idx++]); 834 } 835 836 837 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 838 VisitDecl(D); 839 D->setAtLoc(ReadSourceLocation(Record, Idx)); 840 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>(Record, Idx)); 841 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>(Record, Idx); 842 D->IvarLoc = ReadSourceLocation(Record, Idx); 843 D->setGetterCXXConstructor(Reader.ReadExpr(F)); 844 D->setSetterCXXAssignment(Reader.ReadExpr(F)); 845 } 846 847 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) { 848 VisitDeclaratorDecl(FD); 849 FD->setMutable(Record[Idx++]); 850 int BitWidthOrInitializer = Record[Idx++]; 851 if (BitWidthOrInitializer == 1) 852 FD->setBitWidth(Reader.ReadExpr(F)); 853 else if (BitWidthOrInitializer == 2) 854 FD->setInClassInitializer(Reader.ReadExpr(F)); 855 if (!FD->getDeclName()) { 856 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>(Record, Idx)) 857 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl); 858 } 859 } 860 861 void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) { 862 VisitValueDecl(FD); 863 864 FD->ChainingSize = Record[Idx++]; 865 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2"); 866 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize]; 867 868 for (unsigned I = 0; I != FD->ChainingSize; ++I) 869 FD->Chaining[I] = ReadDeclAs<NamedDecl>(Record, Idx); 870 } 871 872 void ASTDeclReader::VisitVarDecl(VarDecl *VD) { 873 RedeclarableResult Redecl = VisitRedeclarable(VD); 874 VisitDeclaratorDecl(VD); 875 876 VD->VarDeclBits.SClass = (StorageClass)Record[Idx++]; 877 VD->VarDeclBits.SClassAsWritten = (StorageClass)Record[Idx++]; 878 VD->VarDeclBits.ThreadSpecified = Record[Idx++]; 879 VD->VarDeclBits.HasCXXDirectInit = Record[Idx++]; 880 VD->VarDeclBits.ExceptionVar = Record[Idx++]; 881 VD->VarDeclBits.NRVOVariable = Record[Idx++]; 882 VD->VarDeclBits.CXXForRangeDecl = Record[Idx++]; 883 VD->VarDeclBits.ARCPseudoStrong = Record[Idx++]; 884 885 // Only true variables (not parameters or implicit parameters) can be merged. 886 if (VD->getKind() == Decl::Var) 887 mergeRedeclarable(VD, Redecl); 888 889 if (uint64_t Val = Record[Idx++]) { 890 VD->setInit(Reader.ReadExpr(F)); 891 if (Val > 1) { 892 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt(); 893 Eval->CheckedICE = true; 894 Eval->IsICE = Val == 3; 895 } 896 } 897 898 if (Record[Idx++]) { // HasMemberSpecializationInfo. 899 VarDecl *Tmpl = ReadDeclAs<VarDecl>(Record, Idx); 900 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; 901 SourceLocation POI = ReadSourceLocation(Record, Idx); 902 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI); 903 } 904 } 905 906 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) { 907 VisitVarDecl(PD); 908 } 909 910 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) { 911 VisitVarDecl(PD); 912 unsigned isObjCMethodParam = Record[Idx++]; 913 unsigned scopeDepth = Record[Idx++]; 914 unsigned scopeIndex = Record[Idx++]; 915 unsigned declQualifier = Record[Idx++]; 916 if (isObjCMethodParam) { 917 assert(scopeDepth == 0); 918 PD->setObjCMethodScopeInfo(scopeIndex); 919 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier; 920 } else { 921 PD->setScopeInfo(scopeDepth, scopeIndex); 922 } 923 PD->ParmVarDeclBits.IsKNRPromoted = Record[Idx++]; 924 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record[Idx++]; 925 if (Record[Idx++]) // hasUninstantiatedDefaultArg. 926 PD->setUninstantiatedDefaultArg(Reader.ReadExpr(F)); 927 } 928 929 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) { 930 VisitDecl(AD); 931 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(F))); 932 AD->setRParenLoc(ReadSourceLocation(Record, Idx)); 933 } 934 935 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) { 936 VisitDecl(BD); 937 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(F))); 938 BD->setSignatureAsWritten(GetTypeSourceInfo(Record, Idx)); 939 unsigned NumParams = Record[Idx++]; 940 SmallVector<ParmVarDecl *, 16> Params; 941 Params.reserve(NumParams); 942 for (unsigned I = 0; I != NumParams; ++I) 943 Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx)); 944 BD->setParams(Params); 945 946 bool capturesCXXThis = Record[Idx++]; 947 unsigned numCaptures = Record[Idx++]; 948 SmallVector<BlockDecl::Capture, 16> captures; 949 captures.reserve(numCaptures); 950 for (unsigned i = 0; i != numCaptures; ++i) { 951 VarDecl *decl = ReadDeclAs<VarDecl>(Record, Idx); 952 unsigned flags = Record[Idx++]; 953 bool byRef = (flags & 1); 954 bool nested = (flags & 2); 955 Expr *copyExpr = ((flags & 4) ? Reader.ReadExpr(F) : 0); 956 957 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr)); 958 } 959 BD->setCaptures(Reader.getContext(), captures.begin(), 960 captures.end(), capturesCXXThis); 961 } 962 963 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 964 VisitDecl(D); 965 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]); 966 D->setExternLoc(ReadSourceLocation(Record, Idx)); 967 D->setRBraceLoc(ReadSourceLocation(Record, Idx)); 968 } 969 970 void ASTDeclReader::VisitLabelDecl(LabelDecl *D) { 971 VisitNamedDecl(D); 972 D->setLocStart(ReadSourceLocation(Record, Idx)); 973 } 974 975 976 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) { 977 RedeclarableResult Redecl = VisitRedeclarable(D); 978 VisitNamedDecl(D); 979 D->setInline(Record[Idx++]); 980 D->LocStart = ReadSourceLocation(Record, Idx); 981 D->RBraceLoc = ReadSourceLocation(Record, Idx); 982 mergeRedeclarable(D, Redecl); 983 984 if (Redecl.getFirstID() == ThisDeclID) { 985 // Each module has its own anonymous namespace, which is disjoint from 986 // any other module's anonymous namespaces, so don't attach the anonymous 987 // namespace at all. 988 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>(Record, Idx); 989 if (F.Kind != MK_Module) 990 D->setAnonymousNamespace(Anon); 991 } else { 992 // Link this namespace back to the first declaration, which has already 993 // been deserialized. 994 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDeclaration()); 995 } 996 } 997 998 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 999 VisitNamedDecl(D); 1000 D->NamespaceLoc = ReadSourceLocation(Record, Idx); 1001 D->IdentLoc = ReadSourceLocation(Record, Idx); 1002 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1003 D->Namespace = ReadDeclAs<NamedDecl>(Record, Idx); 1004 } 1005 1006 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) { 1007 VisitNamedDecl(D); 1008 D->setUsingLocation(ReadSourceLocation(Record, Idx)); 1009 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1010 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx); 1011 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>(Record, Idx)); 1012 D->setTypeName(Record[Idx++]); 1013 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>(Record, Idx)) 1014 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern); 1015 } 1016 1017 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) { 1018 VisitNamedDecl(D); 1019 D->setTargetDecl(ReadDeclAs<NamedDecl>(Record, Idx)); 1020 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(Record, Idx); 1021 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(Record, Idx); 1022 if (Pattern) 1023 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern); 1024 } 1025 1026 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 1027 VisitNamedDecl(D); 1028 D->UsingLoc = ReadSourceLocation(Record, Idx); 1029 D->NamespaceLoc = ReadSourceLocation(Record, Idx); 1030 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1031 D->NominatedNamespace = ReadDeclAs<NamedDecl>(Record, Idx); 1032 D->CommonAncestor = ReadDeclAs<DeclContext>(Record, Idx); 1033 } 1034 1035 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1036 VisitValueDecl(D); 1037 D->setUsingLoc(ReadSourceLocation(Record, Idx)); 1038 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1039 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx); 1040 } 1041 1042 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl( 1043 UnresolvedUsingTypenameDecl *D) { 1044 VisitTypeDecl(D); 1045 D->TypenameLocation = ReadSourceLocation(Record, Idx); 1046 D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx); 1047 } 1048 1049 void ASTDeclReader::ReadCXXDefinitionData( 1050 struct CXXRecordDecl::DefinitionData &Data, 1051 const RecordData &Record, unsigned &Idx) { 1052 Data.UserDeclaredConstructor = Record[Idx++]; 1053 Data.UserDeclaredCopyConstructor = Record[Idx++]; 1054 Data.UserDeclaredMoveConstructor = Record[Idx++]; 1055 Data.UserDeclaredCopyAssignment = Record[Idx++]; 1056 Data.UserDeclaredMoveAssignment = Record[Idx++]; 1057 Data.UserDeclaredDestructor = Record[Idx++]; 1058 Data.Aggregate = Record[Idx++]; 1059 Data.PlainOldData = Record[Idx++]; 1060 Data.Empty = Record[Idx++]; 1061 Data.Polymorphic = Record[Idx++]; 1062 Data.Abstract = Record[Idx++]; 1063 Data.IsStandardLayout = Record[Idx++]; 1064 Data.HasNoNonEmptyBases = Record[Idx++]; 1065 Data.HasPrivateFields = Record[Idx++]; 1066 Data.HasProtectedFields = Record[Idx++]; 1067 Data.HasPublicFields = Record[Idx++]; 1068 Data.HasMutableFields = Record[Idx++]; 1069 Data.HasTrivialDefaultConstructor = Record[Idx++]; 1070 Data.HasConstexprNonCopyMoveConstructor = Record[Idx++]; 1071 Data.HasTrivialCopyConstructor = Record[Idx++]; 1072 Data.HasTrivialMoveConstructor = Record[Idx++]; 1073 Data.HasTrivialCopyAssignment = Record[Idx++]; 1074 Data.HasTrivialMoveAssignment = Record[Idx++]; 1075 Data.HasTrivialDestructor = Record[Idx++]; 1076 Data.HasNonLiteralTypeFieldsOrBases = Record[Idx++]; 1077 Data.ComputedVisibleConversions = Record[Idx++]; 1078 Data.UserProvidedDefaultConstructor = Record[Idx++]; 1079 Data.DeclaredDefaultConstructor = Record[Idx++]; 1080 Data.DeclaredCopyConstructor = Record[Idx++]; 1081 Data.DeclaredMoveConstructor = Record[Idx++]; 1082 Data.DeclaredCopyAssignment = Record[Idx++]; 1083 Data.DeclaredMoveAssignment = Record[Idx++]; 1084 Data.DeclaredDestructor = Record[Idx++]; 1085 Data.FailedImplicitMoveConstructor = Record[Idx++]; 1086 Data.FailedImplicitMoveAssignment = Record[Idx++]; 1087 1088 Data.NumBases = Record[Idx++]; 1089 if (Data.NumBases) 1090 Data.Bases = Reader.readCXXBaseSpecifiers(F, Record, Idx); 1091 Data.NumVBases = Record[Idx++]; 1092 if (Data.NumVBases) 1093 Data.VBases = Reader.readCXXBaseSpecifiers(F, Record, Idx); 1094 1095 Reader.ReadUnresolvedSet(F, Data.Conversions, Record, Idx); 1096 Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx); 1097 assert(Data.Definition && "Data.Definition should be already set!"); 1098 Data.FirstFriend = ReadDeclAs<FriendDecl>(Record, Idx); 1099 } 1100 1101 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) { 1102 VisitRecordDecl(D); 1103 1104 ASTContext &C = Reader.getContext(); 1105 if (Record[Idx++]) { 1106 D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D); 1107 1108 // Propagate the DefinitionData pointer to the canonical declaration, so 1109 // that all other deserialized declarations will see it. 1110 // FIXME: Complain if there already is a DefinitionData! 1111 D->getCanonicalDecl()->DefinitionData = D->DefinitionData; 1112 1113 ReadCXXDefinitionData(*D->DefinitionData, Record, Idx); 1114 1115 // Note that we have deserialized a definition. Any declarations 1116 // deserialized before this one will be be given the DefinitionData pointer 1117 // at the end. 1118 Reader.PendingDefinitions.insert(D); 1119 } else { 1120 // Propagate DefinitionData pointer from the canonical declaration. 1121 D->DefinitionData = D->getCanonicalDecl()->DefinitionData; 1122 } 1123 1124 enum CXXRecKind { 1125 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization 1126 }; 1127 switch ((CXXRecKind)Record[Idx++]) { 1128 default: 1129 llvm_unreachable("Out of sync with ASTDeclWriter::VisitCXXRecordDecl?"); 1130 case CXXRecNotTemplate: 1131 break; 1132 case CXXRecTemplate: 1133 D->TemplateOrInstantiation = ReadDeclAs<ClassTemplateDecl>(Record, Idx); 1134 break; 1135 case CXXRecMemberSpecialization: { 1136 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(Record, Idx); 1137 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; 1138 SourceLocation POI = ReadSourceLocation(Record, Idx); 1139 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK); 1140 MSI->setPointOfInstantiation(POI); 1141 D->TemplateOrInstantiation = MSI; 1142 break; 1143 } 1144 } 1145 1146 // Load the key function to avoid deserializing every method so we can 1147 // compute it. 1148 if (D->IsCompleteDefinition) { 1149 if (CXXMethodDecl *Key = ReadDeclAs<CXXMethodDecl>(Record, Idx)) 1150 C.KeyFunctions[D] = Key; 1151 } 1152 } 1153 1154 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) { 1155 VisitFunctionDecl(D); 1156 unsigned NumOverridenMethods = Record[Idx++]; 1157 while (NumOverridenMethods--) { 1158 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod, 1159 // MD may be initializing. 1160 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>(Record, Idx)) 1161 Reader.getContext().addOverriddenMethod(D, MD); 1162 } 1163 } 1164 1165 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 1166 VisitCXXMethodDecl(D); 1167 1168 D->IsExplicitSpecified = Record[Idx++]; 1169 D->ImplicitlyDefined = Record[Idx++]; 1170 llvm::tie(D->CtorInitializers, D->NumCtorInitializers) 1171 = Reader.ReadCXXCtorInitializers(F, Record, Idx); 1172 } 1173 1174 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 1175 VisitCXXMethodDecl(D); 1176 1177 D->ImplicitlyDefined = Record[Idx++]; 1178 D->OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx); 1179 } 1180 1181 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) { 1182 VisitCXXMethodDecl(D); 1183 D->IsExplicitSpecified = Record[Idx++]; 1184 } 1185 1186 void ASTDeclReader::VisitImportDecl(ImportDecl *D) { 1187 VisitDecl(D); 1188 D->ImportedAndComplete.setPointer(readModule(Record, Idx)); 1189 D->ImportedAndComplete.setInt(Record[Idx++]); 1190 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(D + 1); 1191 for (unsigned I = 0, N = Record.back(); I != N; ++I) 1192 StoredLocs[I] = ReadSourceLocation(Record, Idx); 1193 ++Idx; 1194 } 1195 1196 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) { 1197 VisitDecl(D); 1198 D->setColonLoc(ReadSourceLocation(Record, Idx)); 1199 } 1200 1201 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) { 1202 VisitDecl(D); 1203 if (Record[Idx++]) 1204 D->Friend = GetTypeSourceInfo(Record, Idx); 1205 else 1206 D->Friend = ReadDeclAs<NamedDecl>(Record, Idx); 1207 D->NextFriend = Record[Idx++]; 1208 D->UnsupportedFriend = (Record[Idx++] != 0); 1209 D->FriendLoc = ReadSourceLocation(Record, Idx); 1210 } 1211 1212 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) { 1213 VisitDecl(D); 1214 unsigned NumParams = Record[Idx++]; 1215 D->NumParams = NumParams; 1216 D->Params = new TemplateParameterList*[NumParams]; 1217 for (unsigned i = 0; i != NumParams; ++i) 1218 D->Params[i] = Reader.ReadTemplateParameterList(F, Record, Idx); 1219 if (Record[Idx++]) // HasFriendDecl 1220 D->Friend = ReadDeclAs<NamedDecl>(Record, Idx); 1221 else 1222 D->Friend = GetTypeSourceInfo(Record, Idx); 1223 D->FriendLoc = ReadSourceLocation(Record, Idx); 1224 } 1225 1226 void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) { 1227 VisitNamedDecl(D); 1228 1229 NamedDecl *TemplatedDecl = ReadDeclAs<NamedDecl>(Record, Idx); 1230 TemplateParameterList* TemplateParams 1231 = Reader.ReadTemplateParameterList(F, Record, Idx); 1232 D->init(TemplatedDecl, TemplateParams); 1233 } 1234 1235 ASTDeclReader::RedeclarableResult 1236 ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { 1237 RedeclarableResult Redecl = VisitRedeclarable(D); 1238 1239 // Make sure we've allocated the Common pointer first. We do this before 1240 // VisitTemplateDecl so that getCommonPtr() can be used during initialization. 1241 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl(); 1242 if (!CanonD->Common) { 1243 CanonD->Common = CanonD->newCommon(Reader.getContext()); 1244 Reader.PendingDefinitions.insert(CanonD); 1245 } 1246 D->Common = CanonD->Common; 1247 1248 // If this is the first declaration of the template, fill in the information 1249 // for the 'common' pointer. 1250 if (ThisDeclID == Redecl.getFirstID()) { 1251 if (RedeclarableTemplateDecl *RTD 1252 = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx)) { 1253 assert(RTD->getKind() == D->getKind() && 1254 "InstantiatedFromMemberTemplate kind mismatch"); 1255 D->setInstantiatedFromMemberTemplate(RTD); 1256 if (Record[Idx++]) 1257 D->setMemberSpecialization(); 1258 } 1259 } 1260 1261 VisitTemplateDecl(D); 1262 D->IdentifierNamespace = Record[Idx++]; 1263 1264 return Redecl; 1265 } 1266 1267 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { 1268 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 1269 1270 if (ThisDeclID == Redecl.getFirstID()) { 1271 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of 1272 // the specializations. 1273 SmallVector<serialization::DeclID, 2> SpecIDs; 1274 SpecIDs.push_back(0); 1275 1276 // Specializations. 1277 unsigned Size = Record[Idx++]; 1278 SpecIDs[0] += Size; 1279 for (unsigned I = 0; I != Size; ++I) 1280 SpecIDs.push_back(ReadDeclID(Record, Idx)); 1281 1282 // Partial specializations. 1283 Size = Record[Idx++]; 1284 SpecIDs[0] += Size; 1285 for (unsigned I = 0; I != Size; ++I) 1286 SpecIDs.push_back(ReadDeclID(Record, Idx)); 1287 1288 if (SpecIDs[0]) { 1289 typedef serialization::DeclID DeclID; 1290 1291 ClassTemplateDecl::Common *CommonPtr = D->getCommonPtr(); 1292 // FIXME: Append specializations! 1293 CommonPtr->LazySpecializations 1294 = new (Reader.getContext()) DeclID [SpecIDs.size()]; 1295 memcpy(CommonPtr->LazySpecializations, SpecIDs.data(), 1296 SpecIDs.size() * sizeof(DeclID)); 1297 } 1298 1299 // InjectedClassNameType is computed. 1300 } 1301 } 1302 1303 void ASTDeclReader::VisitClassTemplateSpecializationDecl( 1304 ClassTemplateSpecializationDecl *D) { 1305 VisitCXXRecordDecl(D); 1306 1307 ASTContext &C = Reader.getContext(); 1308 if (Decl *InstD = ReadDecl(Record, Idx)) { 1309 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) { 1310 D->SpecializedTemplate = CTD; 1311 } else { 1312 SmallVector<TemplateArgument, 8> TemplArgs; 1313 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 1314 TemplateArgumentList *ArgList 1315 = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), 1316 TemplArgs.size()); 1317 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS 1318 = new (C) ClassTemplateSpecializationDecl:: 1319 SpecializedPartialSpecialization(); 1320 PS->PartialSpecialization 1321 = cast<ClassTemplatePartialSpecializationDecl>(InstD); 1322 PS->TemplateArgs = ArgList; 1323 D->SpecializedTemplate = PS; 1324 } 1325 } 1326 1327 // Explicit info. 1328 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) { 1329 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo 1330 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo; 1331 ExplicitInfo->TypeAsWritten = TyInfo; 1332 ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx); 1333 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx); 1334 D->ExplicitInfo = ExplicitInfo; 1335 } 1336 1337 SmallVector<TemplateArgument, 8> TemplArgs; 1338 Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx); 1339 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), 1340 TemplArgs.size()); 1341 D->PointOfInstantiation = ReadSourceLocation(Record, Idx); 1342 D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++]; 1343 1344 if (D->isCanonicalDecl()) { // It's kept in the folding set. 1345 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(Record,Idx); 1346 if (ClassTemplatePartialSpecializationDecl *Partial 1347 = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { 1348 CanonPattern->getCommonPtr()->PartialSpecializations.InsertNode(Partial); 1349 } else { 1350 CanonPattern->getCommonPtr()->Specializations.InsertNode(D); 1351 } 1352 } 1353 } 1354 1355 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl( 1356 ClassTemplatePartialSpecializationDecl *D) { 1357 VisitClassTemplateSpecializationDecl(D); 1358 1359 ASTContext &C = Reader.getContext(); 1360 D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx); 1361 1362 unsigned NumArgs = Record[Idx++]; 1363 if (NumArgs) { 1364 D->NumArgsAsWritten = NumArgs; 1365 D->ArgsAsWritten = new (C) TemplateArgumentLoc[NumArgs]; 1366 for (unsigned i=0; i != NumArgs; ++i) 1367 D->ArgsAsWritten[i] = Reader.ReadTemplateArgumentLoc(F, Record, Idx); 1368 } 1369 1370 D->SequenceNumber = Record[Idx++]; 1371 1372 // These are read/set from/to the first declaration. 1373 if (D->getPreviousDecl() == 0) { 1374 D->InstantiatedFromMember.setPointer( 1375 ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx)); 1376 D->InstantiatedFromMember.setInt(Record[Idx++]); 1377 } 1378 } 1379 1380 void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl( 1381 ClassScopeFunctionSpecializationDecl *D) { 1382 VisitDecl(D); 1383 D->Specialization = ReadDeclAs<CXXMethodDecl>(Record, Idx); 1384 } 1385 1386 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 1387 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D); 1388 1389 if (ThisDeclID == Redecl.getFirstID()) { 1390 // This FunctionTemplateDecl owns a CommonPtr; read it. 1391 1392 // Read the function specialization declarations. 1393 // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled 1394 // when reading the specialized FunctionDecl. 1395 unsigned NumSpecs = Record[Idx++]; 1396 while (NumSpecs--) 1397 (void)ReadDecl(Record, Idx); 1398 } 1399 } 1400 1401 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 1402 VisitTypeDecl(D); 1403 1404 D->setDeclaredWithTypename(Record[Idx++]); 1405 1406 bool Inherited = Record[Idx++]; 1407 TypeSourceInfo *DefArg = GetTypeSourceInfo(Record, Idx); 1408 D->setDefaultArgument(DefArg, Inherited); 1409 } 1410 1411 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 1412 VisitDeclaratorDecl(D); 1413 // TemplateParmPosition. 1414 D->setDepth(Record[Idx++]); 1415 D->setPosition(Record[Idx++]); 1416 if (D->isExpandedParameterPack()) { 1417 void **Data = reinterpret_cast<void **>(D + 1); 1418 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { 1419 Data[2*I] = Reader.readType(F, Record, Idx).getAsOpaquePtr(); 1420 Data[2*I + 1] = GetTypeSourceInfo(Record, Idx); 1421 } 1422 } else { 1423 // Rest of NonTypeTemplateParmDecl. 1424 D->ParameterPack = Record[Idx++]; 1425 if (Record[Idx++]) { 1426 Expr *DefArg = Reader.ReadExpr(F); 1427 bool Inherited = Record[Idx++]; 1428 D->setDefaultArgument(DefArg, Inherited); 1429 } 1430 } 1431 } 1432 1433 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 1434 VisitTemplateDecl(D); 1435 // TemplateParmPosition. 1436 D->setDepth(Record[Idx++]); 1437 D->setPosition(Record[Idx++]); 1438 // Rest of TemplateTemplateParmDecl. 1439 TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(F, Record, Idx); 1440 bool IsInherited = Record[Idx++]; 1441 D->setDefaultArgument(Arg, IsInherited); 1442 D->ParameterPack = Record[Idx++]; 1443 } 1444 1445 void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { 1446 VisitRedeclarableTemplateDecl(D); 1447 } 1448 1449 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) { 1450 VisitDecl(D); 1451 D->AssertExpr = Reader.ReadExpr(F); 1452 D->Message = cast<StringLiteral>(Reader.ReadExpr(F)); 1453 D->RParenLoc = ReadSourceLocation(Record, Idx); 1454 } 1455 1456 std::pair<uint64_t, uint64_t> 1457 ASTDeclReader::VisitDeclContext(DeclContext *DC) { 1458 uint64_t LexicalOffset = Record[Idx++]; 1459 uint64_t VisibleOffset = Record[Idx++]; 1460 return std::make_pair(LexicalOffset, VisibleOffset); 1461 } 1462 1463 template <typename T> 1464 ASTDeclReader::RedeclarableResult 1465 ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) { 1466 DeclID FirstDeclID = ReadDeclID(Record, Idx); 1467 1468 // 0 indicates that this declaration was the only declaration of its entity, 1469 // and is used for space optimization. 1470 if (FirstDeclID == 0) 1471 FirstDeclID = ThisDeclID; 1472 1473 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID)); 1474 if (FirstDecl != D) { 1475 // We delay loading of the redeclaration chain to avoid deeply nested calls. 1476 // We temporarily set the first (canonical) declaration as the previous one 1477 // which is the one that matters and mark the real previous DeclID to be 1478 // loaded & attached later on. 1479 D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(FirstDecl); 1480 } 1481 1482 // Note that this declaration has been deserialized. 1483 Reader.RedeclsDeserialized.insert(static_cast<T *>(D)); 1484 1485 // The result structure takes care to note that we need to load the 1486 // other declaration chains for this ID. 1487 return RedeclarableResult(Reader, FirstDeclID); 1488 } 1489 1490 /// \brief Attempts to merge the given declaration (D) with another declaration 1491 /// of the same entity. 1492 template<typename T> 1493 void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *D, 1494 RedeclarableResult &Redecl) { 1495 // If modules are not available, there is no reason to perform this merge. 1496 if (!Reader.getContext().getLangOptions().Modules) 1497 return; 1498 1499 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D))) { 1500 if (T *Existing = ExistingRes) { 1501 T *ExistingCanon = Existing->getCanonicalDecl(); 1502 T *DCanon = static_cast<T*>(D)->getCanonicalDecl(); 1503 if (ExistingCanon != DCanon) { 1504 // Have our redeclaration link point back at the canonical declaration 1505 // of the existing declaration, so that this declaration has the 1506 // appropriate canonical declaration. 1507 D->RedeclLink 1508 = typename Redeclarable<T>::PreviousDeclLink(ExistingCanon); 1509 1510 // When we merge a namespace, update its pointer to the first namespace. 1511 if (NamespaceDecl *Namespace 1512 = dyn_cast<NamespaceDecl>(static_cast<T*>(D))) { 1513 Namespace->AnonOrFirstNamespaceAndInline.setPointer( 1514 static_cast<NamespaceDecl *>(static_cast<void*>(ExistingCanon))); 1515 } 1516 1517 // Don't introduce DCanon into the set of pending declaration chains. 1518 Redecl.suppress(); 1519 1520 // Introduce ExistingCanon into the set of pending declaration chains, 1521 // if in fact it came from a module file. 1522 if (ExistingCanon->isFromASTFile()) { 1523 GlobalDeclID ExistingCanonID = ExistingCanon->getGlobalID(); 1524 assert(ExistingCanonID && "Unrecorded canonical declaration ID?"); 1525 if (Reader.PendingDeclChainsKnown.insert(ExistingCanonID)) 1526 Reader.PendingDeclChains.push_back(ExistingCanonID); 1527 } 1528 1529 // If this declaration was the canonical declaration, make a note of 1530 // that. We accept the linear algorithm here because the number of 1531 // unique canonical declarations of an entity should always be tiny. 1532 if (DCanon == static_cast<T*>(D)) { 1533 SmallVectorImpl<DeclID> &Merged = Reader.MergedDecls[ExistingCanon]; 1534 if (std::find(Merged.begin(), Merged.end(), Redecl.getFirstID()) 1535 == Merged.end()) 1536 Merged.push_back(Redecl.getFirstID()); 1537 1538 // If ExistingCanon did not come from a module file, introduce the 1539 // first declaration that *does* come from a module file to the 1540 // set of pending declaration chains, so that we merge this 1541 // declaration. 1542 if (!ExistingCanon->isFromASTFile() && 1543 Reader.PendingDeclChainsKnown.insert(Redecl.getFirstID())) 1544 Reader.PendingDeclChains.push_back(Merged[0]); 1545 } 1546 } 1547 } 1548 } 1549 } 1550 1551 //===----------------------------------------------------------------------===// 1552 // Attribute Reading 1553 //===----------------------------------------------------------------------===// 1554 1555 /// \brief Reads attributes from the current stream position. 1556 void ASTReader::ReadAttributes(ModuleFile &F, AttrVec &Attrs, 1557 const RecordData &Record, unsigned &Idx) { 1558 for (unsigned i = 0, e = Record[Idx++]; i != e; ++i) { 1559 Attr *New = 0; 1560 attr::Kind Kind = (attr::Kind)Record[Idx++]; 1561 SourceRange Range = ReadSourceRange(F, Record, Idx); 1562 1563 #include "clang/Serialization/AttrPCHRead.inc" 1564 1565 assert(New && "Unable to decode attribute?"); 1566 Attrs.push_back(New); 1567 } 1568 } 1569 1570 //===----------------------------------------------------------------------===// 1571 // ASTReader Implementation 1572 //===----------------------------------------------------------------------===// 1573 1574 /// \brief Note that we have loaded the declaration with the given 1575 /// Index. 1576 /// 1577 /// This routine notes that this declaration has already been loaded, 1578 /// so that future GetDecl calls will return this declaration rather 1579 /// than trying to load a new declaration. 1580 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) { 1581 assert(!DeclsLoaded[Index] && "Decl loaded twice?"); 1582 DeclsLoaded[Index] = D; 1583 } 1584 1585 1586 /// \brief Determine whether the consumer will be interested in seeing 1587 /// this declaration (via HandleTopLevelDecl). 1588 /// 1589 /// This routine should return true for anything that might affect 1590 /// code generation, e.g., inline function definitions, Objective-C 1591 /// declarations with metadata, etc. 1592 static bool isConsumerInterestedIn(Decl *D) { 1593 // An ObjCMethodDecl is never considered as "interesting" because its 1594 // implementation container always is. 1595 1596 if (isa<FileScopeAsmDecl>(D) || 1597 isa<ObjCProtocolDecl>(D) || 1598 isa<ObjCImplDecl>(D)) 1599 return true; 1600 if (VarDecl *Var = dyn_cast<VarDecl>(D)) 1601 return Var->isFileVarDecl() && 1602 Var->isThisDeclarationADefinition() == VarDecl::Definition; 1603 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) 1604 return Func->doesThisDeclarationHaveABody(); 1605 1606 return false; 1607 } 1608 1609 /// \brief Get the correct cursor and offset for loading a declaration. 1610 ASTReader::RecordLocation 1611 ASTReader::DeclCursorForID(DeclID ID, unsigned &RawLocation) { 1612 // See if there's an override. 1613 DeclReplacementMap::iterator It = ReplacedDecls.find(ID); 1614 if (It != ReplacedDecls.end()) { 1615 RawLocation = It->second.RawLoc; 1616 return RecordLocation(It->second.Mod, It->second.Offset); 1617 } 1618 1619 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID); 1620 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 1621 ModuleFile *M = I->second; 1622 const DeclOffset & 1623 DOffs = M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]; 1624 RawLocation = DOffs.Loc; 1625 return RecordLocation(M, DOffs.BitOffset); 1626 } 1627 1628 ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) { 1629 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I 1630 = GlobalBitOffsetsMap.find(GlobalOffset); 1631 1632 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map"); 1633 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset); 1634 } 1635 1636 uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) { 1637 return LocalOffset + M.GlobalBitOffset; 1638 } 1639 1640 /// \brief Determine whether the two declarations refer to the same entity. 1641 static bool isSameEntity(NamedDecl *X, NamedDecl *Y) { 1642 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!"); 1643 1644 if (X == Y) 1645 return true; 1646 1647 // Must be in the same context. 1648 if (!X->getDeclContext()->getRedeclContext()->Equals( 1649 Y->getDeclContext()->getRedeclContext())) 1650 return false; 1651 1652 // Two typedefs refer to the same entity if they have the same underlying 1653 // type. 1654 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X)) 1655 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y)) 1656 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(), 1657 TypedefY->getUnderlyingType()); 1658 1659 // Must have the same kind. 1660 if (X->getKind() != Y->getKind()) 1661 return false; 1662 1663 // Objective-C classes and protocols with the same name always match. 1664 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X)) 1665 return true; 1666 1667 // Compatible tags match. 1668 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) { 1669 TagDecl *TagY = cast<TagDecl>(Y); 1670 return (TagX->getTagKind() == TagY->getTagKind()) || 1671 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class) && 1672 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class)); 1673 } 1674 1675 // Functions with the same type and linkage match. 1676 // FIXME: This needs to cope with function templates, merging of 1677 //prototyped/non-prototyped functions, etc. 1678 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) { 1679 FunctionDecl *FuncY = cast<FunctionDecl>(Y); 1680 return (FuncX->getLinkage() == FuncY->getLinkage()) && 1681 FuncX->getASTContext().hasSameType(FuncX->getType(), FuncY->getType()); 1682 } 1683 1684 // Variables with the same type and linkage match. 1685 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) { 1686 VarDecl *VarY = cast<VarDecl>(Y); 1687 return (VarX->getLinkage() == VarY->getLinkage()) && 1688 VarX->getASTContext().hasSameType(VarX->getType(), VarY->getType()); 1689 } 1690 1691 // Namespaces with the same name and inlinedness match. 1692 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) { 1693 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y); 1694 return NamespaceX->isInline() == NamespaceY->isInline(); 1695 } 1696 1697 // FIXME: Many other cases to implement. 1698 return false; 1699 } 1700 1701 ASTDeclReader::FindExistingResult::~FindExistingResult() { 1702 if (!AddResult || Existing) 1703 return; 1704 1705 DeclContext *DC = New->getDeclContext()->getRedeclContext(); 1706 if (DC->isTranslationUnit() && Reader.SemaObj) { 1707 Reader.SemaObj->IdResolver.tryAddTopLevelDecl(New, New->getDeclName()); 1708 } else if (DC->isNamespace()) { 1709 DC->addDecl(New); 1710 } 1711 } 1712 1713 ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) { 1714 DeclarationName Name = D->getDeclName(); 1715 if (!Name) { 1716 // Don't bother trying to find unnamed declarations. 1717 FindExistingResult Result(Reader, D, /*Existing=*/0); 1718 Result.suppress(); 1719 return Result; 1720 } 1721 1722 DeclContext *DC = D->getDeclContext()->getRedeclContext(); 1723 if (!DC->isFileContext()) 1724 return FindExistingResult(Reader); 1725 1726 if (DC->isTranslationUnit() && Reader.SemaObj) { 1727 IdentifierResolver &IdResolver = Reader.SemaObj->IdResolver; 1728 for (IdentifierResolver::iterator I = IdResolver.begin(Name), 1729 IEnd = IdResolver.end(); 1730 I != IEnd; ++I) { 1731 if (isSameEntity(*I, D)) 1732 return FindExistingResult(Reader, D, *I); 1733 } 1734 } 1735 1736 if (DC->isNamespace()) { 1737 for (DeclContext::lookup_result R = DC->lookup(Name); 1738 R.first != R.second; ++R.first) { 1739 if (isSameEntity(*R.first, D)) 1740 return FindExistingResult(Reader, D, *R.first); 1741 } 1742 } 1743 1744 return FindExistingResult(Reader, D, /*Existing=*/0); 1745 } 1746 1747 void ASTDeclReader::attachPreviousDecl(Decl *D, Decl *previous) { 1748 assert(D && previous); 1749 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 1750 TD->RedeclLink.setPointer(cast<TagDecl>(previous)); 1751 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1752 FD->RedeclLink.setPointer(cast<FunctionDecl>(previous)); 1753 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 1754 VD->RedeclLink.setPointer(cast<VarDecl>(previous)); 1755 } else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { 1756 TD->RedeclLink.setPointer(cast<TypedefNameDecl>(previous)); 1757 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 1758 ID->RedeclLink.setPointer(cast<ObjCInterfaceDecl>(previous)); 1759 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { 1760 PD->RedeclLink.setPointer(cast<ObjCProtocolDecl>(previous)); 1761 } else if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D)) { 1762 ND->RedeclLink.setPointer(cast<NamespaceDecl>(previous)); 1763 } else { 1764 RedeclarableTemplateDecl *TD = cast<RedeclarableTemplateDecl>(D); 1765 TD->RedeclLink.setPointer(cast<RedeclarableTemplateDecl>(previous)); 1766 } 1767 } 1768 1769 void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) { 1770 assert(D && Latest); 1771 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 1772 TD->RedeclLink 1773 = Redeclarable<TagDecl>::LatestDeclLink(cast<TagDecl>(Latest)); 1774 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1775 FD->RedeclLink 1776 = Redeclarable<FunctionDecl>::LatestDeclLink(cast<FunctionDecl>(Latest)); 1777 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 1778 VD->RedeclLink 1779 = Redeclarable<VarDecl>::LatestDeclLink(cast<VarDecl>(Latest)); 1780 } else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { 1781 TD->RedeclLink 1782 = Redeclarable<TypedefNameDecl>::LatestDeclLink( 1783 cast<TypedefNameDecl>(Latest)); 1784 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 1785 ID->RedeclLink 1786 = Redeclarable<ObjCInterfaceDecl>::LatestDeclLink( 1787 cast<ObjCInterfaceDecl>(Latest)); 1788 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { 1789 PD->RedeclLink 1790 = Redeclarable<ObjCProtocolDecl>::LatestDeclLink( 1791 cast<ObjCProtocolDecl>(Latest)); 1792 } else if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D)) { 1793 ND->RedeclLink 1794 = Redeclarable<NamespaceDecl>::LatestDeclLink( 1795 cast<NamespaceDecl>(Latest)); 1796 } else { 1797 RedeclarableTemplateDecl *TD = cast<RedeclarableTemplateDecl>(D); 1798 TD->RedeclLink 1799 = Redeclarable<RedeclarableTemplateDecl>::LatestDeclLink( 1800 cast<RedeclarableTemplateDecl>(Latest)); 1801 } 1802 } 1803 1804 ASTReader::MergedDeclsMap::iterator 1805 ASTReader::combineStoredMergedDecls(Decl *Canon, GlobalDeclID CanonID) { 1806 // If we don't have any stored merged declarations, just look in the 1807 // merged declarations set. 1808 StoredMergedDeclsMap::iterator StoredPos = StoredMergedDecls.find(CanonID); 1809 if (StoredPos == StoredMergedDecls.end()) 1810 return MergedDecls.find(Canon); 1811 1812 // Append the stored merged declarations to the merged declarations set. 1813 MergedDeclsMap::iterator Pos = MergedDecls.find(Canon); 1814 if (Pos == MergedDecls.end()) 1815 Pos = MergedDecls.insert(std::make_pair(Canon, 1816 SmallVector<DeclID, 2>())).first; 1817 Pos->second.append(StoredPos->second.begin(), StoredPos->second.end()); 1818 StoredMergedDecls.erase(StoredPos); 1819 1820 // Sort and uniquify the set of merged declarations. 1821 llvm::array_pod_sort(Pos->second.begin(), Pos->second.end()); 1822 Pos->second.erase(std::unique(Pos->second.begin(), Pos->second.end()), 1823 Pos->second.end()); 1824 return Pos; 1825 } 1826 1827 void ASTReader::loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID) { 1828 Decl *previous = GetDecl(ID); 1829 ASTDeclReader::attachPreviousDecl(D, previous); 1830 } 1831 1832 /// \brief Read the declaration at the given offset from the AST file. 1833 Decl *ASTReader::ReadDeclRecord(DeclID ID) { 1834 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 1835 unsigned RawLocation = 0; 1836 RecordLocation Loc = DeclCursorForID(ID, RawLocation); 1837 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 1838 // Keep track of where we are in the stream, then jump back there 1839 // after reading this declaration. 1840 SavedStreamPosition SavedPosition(DeclsCursor); 1841 1842 ReadingKindTracker ReadingKind(Read_Decl, *this); 1843 1844 // Note that we are loading a declaration record. 1845 Deserializing ADecl(this); 1846 1847 DeclsCursor.JumpToBit(Loc.Offset); 1848 RecordData Record; 1849 unsigned Code = DeclsCursor.ReadCode(); 1850 unsigned Idx = 0; 1851 ASTDeclReader Reader(*this, *Loc.F, DeclsCursor, ID, RawLocation, Record,Idx); 1852 1853 Decl *D = 0; 1854 switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) { 1855 case DECL_CONTEXT_LEXICAL: 1856 case DECL_CONTEXT_VISIBLE: 1857 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord"); 1858 case DECL_TYPEDEF: 1859 D = TypedefDecl::CreateDeserialized(Context, ID); 1860 break; 1861 case DECL_TYPEALIAS: 1862 D = TypeAliasDecl::CreateDeserialized(Context, ID); 1863 break; 1864 case DECL_ENUM: 1865 D = EnumDecl::CreateDeserialized(Context, ID); 1866 break; 1867 case DECL_RECORD: 1868 D = RecordDecl::CreateDeserialized(Context, ID); 1869 break; 1870 case DECL_ENUM_CONSTANT: 1871 D = EnumConstantDecl::CreateDeserialized(Context, ID); 1872 break; 1873 case DECL_FUNCTION: 1874 D = FunctionDecl::CreateDeserialized(Context, ID); 1875 break; 1876 case DECL_LINKAGE_SPEC: 1877 D = LinkageSpecDecl::CreateDeserialized(Context, ID); 1878 break; 1879 case DECL_LABEL: 1880 D = LabelDecl::CreateDeserialized(Context, ID); 1881 break; 1882 case DECL_NAMESPACE: 1883 D = NamespaceDecl::CreateDeserialized(Context, ID); 1884 break; 1885 case DECL_NAMESPACE_ALIAS: 1886 D = NamespaceAliasDecl::CreateDeserialized(Context, ID); 1887 break; 1888 case DECL_USING: 1889 D = UsingDecl::CreateDeserialized(Context, ID); 1890 break; 1891 case DECL_USING_SHADOW: 1892 D = UsingShadowDecl::CreateDeserialized(Context, ID); 1893 break; 1894 case DECL_USING_DIRECTIVE: 1895 D = UsingDirectiveDecl::CreateDeserialized(Context, ID); 1896 break; 1897 case DECL_UNRESOLVED_USING_VALUE: 1898 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID); 1899 break; 1900 case DECL_UNRESOLVED_USING_TYPENAME: 1901 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID); 1902 break; 1903 case DECL_CXX_RECORD: 1904 D = CXXRecordDecl::CreateDeserialized(Context, ID); 1905 break; 1906 case DECL_CXX_METHOD: 1907 D = CXXMethodDecl::CreateDeserialized(Context, ID); 1908 break; 1909 case DECL_CXX_CONSTRUCTOR: 1910 D = CXXConstructorDecl::CreateDeserialized(Context, ID); 1911 break; 1912 case DECL_CXX_DESTRUCTOR: 1913 D = CXXDestructorDecl::CreateDeserialized(Context, ID); 1914 break; 1915 case DECL_CXX_CONVERSION: 1916 D = CXXConversionDecl::CreateDeserialized(Context, ID); 1917 break; 1918 case DECL_ACCESS_SPEC: 1919 D = AccessSpecDecl::CreateDeserialized(Context, ID); 1920 break; 1921 case DECL_FRIEND: 1922 D = FriendDecl::CreateDeserialized(Context, ID); 1923 break; 1924 case DECL_FRIEND_TEMPLATE: 1925 D = FriendTemplateDecl::CreateDeserialized(Context, ID); 1926 break; 1927 case DECL_CLASS_TEMPLATE: 1928 D = ClassTemplateDecl::CreateDeserialized(Context, ID); 1929 break; 1930 case DECL_CLASS_TEMPLATE_SPECIALIZATION: 1931 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID); 1932 break; 1933 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION: 1934 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); 1935 break; 1936 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION: 1937 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID); 1938 break; 1939 case DECL_FUNCTION_TEMPLATE: 1940 D = FunctionTemplateDecl::CreateDeserialized(Context, ID); 1941 break; 1942 case DECL_TEMPLATE_TYPE_PARM: 1943 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID); 1944 break; 1945 case DECL_NON_TYPE_TEMPLATE_PARM: 1946 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID); 1947 break; 1948 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: 1949 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID, Record[Idx++]); 1950 break; 1951 case DECL_TEMPLATE_TEMPLATE_PARM: 1952 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID); 1953 break; 1954 case DECL_TYPE_ALIAS_TEMPLATE: 1955 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID); 1956 break; 1957 case DECL_STATIC_ASSERT: 1958 D = StaticAssertDecl::CreateDeserialized(Context, ID); 1959 break; 1960 case DECL_OBJC_METHOD: 1961 D = ObjCMethodDecl::CreateDeserialized(Context, ID); 1962 break; 1963 case DECL_OBJC_INTERFACE: 1964 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID); 1965 break; 1966 case DECL_OBJC_IVAR: 1967 D = ObjCIvarDecl::CreateDeserialized(Context, ID); 1968 break; 1969 case DECL_OBJC_PROTOCOL: 1970 D = ObjCProtocolDecl::CreateDeserialized(Context, ID); 1971 break; 1972 case DECL_OBJC_AT_DEFS_FIELD: 1973 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID); 1974 break; 1975 case DECL_OBJC_CATEGORY: 1976 D = ObjCCategoryDecl::CreateDeserialized(Context, ID); 1977 break; 1978 case DECL_OBJC_CATEGORY_IMPL: 1979 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID); 1980 break; 1981 case DECL_OBJC_IMPLEMENTATION: 1982 D = ObjCImplementationDecl::CreateDeserialized(Context, ID); 1983 break; 1984 case DECL_OBJC_COMPATIBLE_ALIAS: 1985 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID); 1986 break; 1987 case DECL_OBJC_PROPERTY: 1988 D = ObjCPropertyDecl::CreateDeserialized(Context, ID); 1989 break; 1990 case DECL_OBJC_PROPERTY_IMPL: 1991 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID); 1992 break; 1993 case DECL_FIELD: 1994 D = FieldDecl::CreateDeserialized(Context, ID); 1995 break; 1996 case DECL_INDIRECTFIELD: 1997 D = IndirectFieldDecl::CreateDeserialized(Context, ID); 1998 break; 1999 case DECL_VAR: 2000 D = VarDecl::CreateDeserialized(Context, ID); 2001 break; 2002 case DECL_IMPLICIT_PARAM: 2003 D = ImplicitParamDecl::CreateDeserialized(Context, ID); 2004 break; 2005 case DECL_PARM_VAR: 2006 D = ParmVarDecl::CreateDeserialized(Context, ID); 2007 break; 2008 case DECL_FILE_SCOPE_ASM: 2009 D = FileScopeAsmDecl::CreateDeserialized(Context, ID); 2010 break; 2011 case DECL_BLOCK: 2012 D = BlockDecl::CreateDeserialized(Context, ID); 2013 break; 2014 case DECL_CXX_BASE_SPECIFIERS: 2015 Error("attempt to read a C++ base-specifier record as a declaration"); 2016 return 0; 2017 case DECL_IMPORT: 2018 // Note: last entry of the ImportDecl record is the number of stored source 2019 // locations. 2020 D = ImportDecl::CreateDeserialized(Context, ID, Record.back()); 2021 break; 2022 } 2023 2024 assert(D && "Unknown declaration reading AST file"); 2025 LoadedDecl(Index, D); 2026 Reader.Visit(D); 2027 2028 // If this declaration is also a declaration context, get the 2029 // offsets for its tables of lexical and visible declarations. 2030 if (DeclContext *DC = dyn_cast<DeclContext>(D)) { 2031 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC); 2032 if (Offsets.first || Offsets.second) { 2033 if (Offsets.first != 0) 2034 DC->setHasExternalLexicalStorage(true); 2035 if (Offsets.second != 0) 2036 DC->setHasExternalVisibleStorage(true); 2037 if (ReadDeclContextStorage(*Loc.F, DeclsCursor, Offsets, 2038 Loc.F->DeclContextInfos[DC])) 2039 return 0; 2040 } 2041 2042 // Now add the pending visible updates for this decl context, if it has any. 2043 DeclContextVisibleUpdatesPending::iterator I = 2044 PendingVisibleUpdates.find(ID); 2045 if (I != PendingVisibleUpdates.end()) { 2046 // There are updates. This means the context has external visible 2047 // storage, even if the original stored version didn't. 2048 DC->setHasExternalVisibleStorage(true); 2049 DeclContextVisibleUpdates &U = I->second; 2050 for (DeclContextVisibleUpdates::iterator UI = U.begin(), UE = U.end(); 2051 UI != UE; ++UI) { 2052 UI->second->DeclContextInfos[DC].NameLookupTableData = UI->first; 2053 } 2054 PendingVisibleUpdates.erase(I); 2055 } 2056 } 2057 assert(Idx == Record.size()); 2058 2059 // Load any relevant update records. 2060 loadDeclUpdateRecords(ID, D); 2061 2062 // Load the category chain after recursive loading is finished. 2063 if (ObjCChainedCategoriesInterfaces.count(ID)) 2064 PendingChainedObjCCategories.push_back( 2065 std::make_pair(cast<ObjCInterfaceDecl>(D), ID)); 2066 2067 // If we have deserialized a declaration that has a definition the 2068 // AST consumer might need to know about, queue it. 2069 // We don't pass it to the consumer immediately because we may be in recursive 2070 // loading, and some declarations may still be initializing. 2071 if (isConsumerInterestedIn(D)) 2072 InterestingDecls.push_back(D); 2073 2074 return D; 2075 } 2076 2077 void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) { 2078 // The declaration may have been modified by files later in the chain. 2079 // If this is the case, read the record containing the updates from each file 2080 // and pass it to ASTDeclReader to make the modifications. 2081 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID); 2082 if (UpdI != DeclUpdateOffsets.end()) { 2083 FileOffsetsTy &UpdateOffsets = UpdI->second; 2084 for (FileOffsetsTy::iterator 2085 I = UpdateOffsets.begin(), E = UpdateOffsets.end(); I != E; ++I) { 2086 ModuleFile *F = I->first; 2087 uint64_t Offset = I->second; 2088 llvm::BitstreamCursor &Cursor = F->DeclsCursor; 2089 SavedStreamPosition SavedPosition(Cursor); 2090 Cursor.JumpToBit(Offset); 2091 RecordData Record; 2092 unsigned Code = Cursor.ReadCode(); 2093 unsigned RecCode = Cursor.ReadRecord(Code, Record); 2094 (void)RecCode; 2095 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!"); 2096 2097 unsigned Idx = 0; 2098 ASTDeclReader Reader(*this, *F, Cursor, ID, 0, Record, Idx); 2099 Reader.UpdateDecl(D, *F, Record); 2100 } 2101 } 2102 } 2103 2104 namespace { 2105 struct CompareLocalRedeclarationsInfoToID { 2106 bool operator()(const LocalRedeclarationsInfo &X, DeclID Y) { 2107 return X.FirstID < Y; 2108 } 2109 2110 bool operator()(DeclID X, const LocalRedeclarationsInfo &Y) { 2111 return X < Y.FirstID; 2112 } 2113 2114 bool operator()(const LocalRedeclarationsInfo &X, 2115 const LocalRedeclarationsInfo &Y) { 2116 return X.FirstID < Y.FirstID; 2117 } 2118 bool operator()(DeclID X, DeclID Y) { 2119 return X < Y; 2120 } 2121 }; 2122 2123 /// \brief Module visitor class that finds all of the redeclarations of a 2124 /// 2125 class RedeclChainVisitor { 2126 ASTReader &Reader; 2127 SmallVectorImpl<DeclID> &SearchDecls; 2128 llvm::SmallPtrSet<Decl *, 16> &Deserialized; 2129 GlobalDeclID CanonID; 2130 llvm::SmallVector<Decl *, 4> Chain; 2131 2132 public: 2133 RedeclChainVisitor(ASTReader &Reader, SmallVectorImpl<DeclID> &SearchDecls, 2134 llvm::SmallPtrSet<Decl *, 16> &Deserialized, 2135 GlobalDeclID CanonID) 2136 : Reader(Reader), SearchDecls(SearchDecls), Deserialized(Deserialized), 2137 CanonID(CanonID) { } 2138 2139 static bool visit(ModuleFile &M, bool Preorder, void *UserData) { 2140 if (Preorder) 2141 return false; 2142 2143 return static_cast<RedeclChainVisitor *>(UserData)->visit(M); 2144 } 2145 2146 void addToChain(Decl *D) { 2147 if (!D) 2148 return; 2149 2150 if (Deserialized.count(D)) { 2151 Deserialized.erase(D); 2152 Chain.push_back(D); 2153 } 2154 } 2155 2156 void searchForID(ModuleFile &M, GlobalDeclID GlobalID) { 2157 // Map global ID of the first declaration down to the local ID 2158 // used in this module file. 2159 DeclID ID = Reader.mapGlobalIDToModuleFileGlobalID(M, GlobalID); 2160 if (!ID) 2161 return; 2162 2163 // Perform a binary search to find the local redeclarations for this 2164 // declaration (if any). 2165 const LocalRedeclarationsInfo *Result 2166 = std::lower_bound(M.RedeclarationsMap, 2167 M.RedeclarationsMap + M.LocalNumRedeclarationsInMap, 2168 ID, CompareLocalRedeclarationsInfoToID()); 2169 if (Result == M.RedeclarationsMap + M.LocalNumRedeclarationsInMap || 2170 Result->FirstID != ID) { 2171 // If we have a previously-canonical singleton declaration that was 2172 // merged into another redeclaration chain, create a trivial chain 2173 // for this single declaration so that it will get wired into the 2174 // complete redeclaration chain. 2175 if (GlobalID != CanonID && 2176 GlobalID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 2177 GlobalID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls) { 2178 addToChain(Reader.GetDecl(GlobalID)); 2179 } 2180 2181 return; 2182 } 2183 2184 // Dig out the starting/ending declarations. 2185 unsigned Offset = Result->Offset; 2186 unsigned N = M.RedeclarationChains[Offset]; 2187 M.RedeclarationChains[Offset++] = 0; // Don't try to deserialize again 2188 for (unsigned I = 0; I != N; ++I) 2189 addToChain(Reader.GetLocalDecl(M, M.RedeclarationChains[Offset++])); 2190 } 2191 2192 bool visit(ModuleFile &M) { 2193 // Visit each of the declarations. 2194 for (unsigned I = 0, N = SearchDecls.size(); I != N; ++I) 2195 searchForID(M, SearchDecls[I]); 2196 return false; 2197 } 2198 2199 ArrayRef<Decl *> getChain() const { 2200 return Chain; 2201 } 2202 }; 2203 } 2204 2205 void ASTReader::loadPendingDeclChain(serialization::GlobalDeclID ID) { 2206 Decl *D = GetDecl(ID); 2207 Decl *CanonDecl = D->getCanonicalDecl(); 2208 2209 // Determine the set of declaration IDs we'll be searching for. 2210 llvm::SmallVector<DeclID, 1> SearchDecls; 2211 GlobalDeclID CanonID = 0; 2212 if (D == CanonDecl) { 2213 SearchDecls.push_back(ID); // Always first. 2214 CanonID = ID; 2215 } 2216 MergedDeclsMap::iterator MergedPos = combineStoredMergedDecls(CanonDecl, ID); 2217 if (MergedPos != MergedDecls.end()) 2218 SearchDecls.append(MergedPos->second.begin(), MergedPos->second.end()); 2219 2220 // Build up the list of redeclarations. 2221 RedeclChainVisitor Visitor(*this, SearchDecls, RedeclsDeserialized, CanonID); 2222 ModuleMgr.visitDepthFirst(&RedeclChainVisitor::visit, &Visitor); 2223 2224 // Retrieve the chains. 2225 ArrayRef<Decl *> Chain = Visitor.getChain(); 2226 if (Chain.empty()) 2227 return; 2228 2229 // Hook up the chains. 2230 Decl *MostRecent = CanonDecl->getMostRecentDecl(); 2231 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2232 if (Chain[I] == CanonDecl) 2233 continue; 2234 2235 ASTDeclReader::attachPreviousDecl(Chain[I], MostRecent); 2236 MostRecent = Chain[I]; 2237 } 2238 2239 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent); 2240 } 2241 2242 namespace { 2243 /// \brief Given an ObjC interface, goes through the modules and links to the 2244 /// interface all the categories for it. 2245 class ObjCChainedCategoriesVisitor { 2246 ASTReader &Reader; 2247 serialization::GlobalDeclID InterfaceID; 2248 ObjCInterfaceDecl *Interface; 2249 ObjCCategoryDecl *GlobHeadCat, *GlobTailCat; 2250 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap; 2251 2252 public: 2253 ObjCChainedCategoriesVisitor(ASTReader &Reader, 2254 serialization::GlobalDeclID InterfaceID, 2255 ObjCInterfaceDecl *Interface) 2256 : Reader(Reader), InterfaceID(InterfaceID), Interface(Interface), 2257 GlobHeadCat(0), GlobTailCat(0) { } 2258 2259 static bool visit(ModuleFile &M, void *UserData) { 2260 return static_cast<ObjCChainedCategoriesVisitor *>(UserData)->visit(M); 2261 } 2262 2263 bool visit(ModuleFile &M) { 2264 if (Reader.isDeclIDFromModule(InterfaceID, M)) 2265 return true; // We reached the module where the interface originated 2266 // from. Stop traversing the imported modules. 2267 2268 ModuleFile::ChainedObjCCategoriesMap::iterator 2269 I = M.ChainedObjCCategories.find(InterfaceID); 2270 if (I == M.ChainedObjCCategories.end()) 2271 return false; 2272 2273 ObjCCategoryDecl * 2274 HeadCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.first); 2275 ObjCCategoryDecl * 2276 TailCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.second); 2277 2278 addCategories(HeadCat, TailCat); 2279 return false; 2280 } 2281 2282 void addCategories(ObjCCategoryDecl *HeadCat, 2283 ObjCCategoryDecl *TailCat = 0) { 2284 if (!HeadCat) { 2285 assert(!TailCat); 2286 return; 2287 } 2288 2289 if (!TailCat) { 2290 TailCat = HeadCat; 2291 while (TailCat->getNextClassCategory()) 2292 TailCat = TailCat->getNextClassCategory(); 2293 } 2294 2295 if (!GlobHeadCat) { 2296 GlobHeadCat = HeadCat; 2297 GlobTailCat = TailCat; 2298 } else { 2299 ASTDeclReader::setNextObjCCategory(GlobTailCat, HeadCat); 2300 GlobTailCat = TailCat; 2301 } 2302 2303 llvm::DenseSet<DeclarationName> Checked; 2304 for (ObjCCategoryDecl *Cat = HeadCat, 2305 *CatEnd = TailCat->getNextClassCategory(); 2306 Cat != CatEnd; Cat = Cat->getNextClassCategory()) { 2307 if (Checked.count(Cat->getDeclName())) 2308 continue; 2309 Checked.insert(Cat->getDeclName()); 2310 checkForDuplicate(Cat); 2311 } 2312 } 2313 2314 /// \brief Warns for duplicate categories that come from different modules. 2315 void checkForDuplicate(ObjCCategoryDecl *Cat) { 2316 DeclarationName Name = Cat->getDeclName(); 2317 // Find the top category with the same name. We do not want to warn for 2318 // duplicates along the established chain because there were already 2319 // warnings for them when the module was created. We only want to warn for 2320 // duplicates between non-dependent modules: 2321 // 2322 // MT // 2323 // / \ // 2324 // ML MR // 2325 // 2326 // We want to warn for duplicates between ML and MR,not between ML and MT. 2327 // 2328 // FIXME: We should not warn for duplicates in diamond: 2329 // 2330 // MT // 2331 // / \ // 2332 // ML MR // 2333 // \ / // 2334 // MB // 2335 // 2336 // If there are duplicates in ML/MR, there will be warning when creating 2337 // MB *and* when importing MB. We should not warn when importing. 2338 for (ObjCCategoryDecl *Next = Cat->getNextClassCategory(); Next; 2339 Next = Next->getNextClassCategory()) { 2340 if (Next->getDeclName() == Name) 2341 Cat = Next; 2342 } 2343 2344 ObjCCategoryDecl *&PrevCat = NameCategoryMap[Name]; 2345 if (!PrevCat) 2346 PrevCat = Cat; 2347 2348 if (PrevCat != Cat) { 2349 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def) 2350 << Interface->getDeclName() << Name; 2351 Reader.Diag(PrevCat->getLocation(), diag::note_previous_definition); 2352 } 2353 } 2354 2355 ObjCCategoryDecl *getHeadCategory() const { return GlobHeadCat; } 2356 }; 2357 } 2358 2359 void ASTReader::loadObjCChainedCategories(serialization::GlobalDeclID ID, 2360 ObjCInterfaceDecl *D) { 2361 ObjCChainedCategoriesVisitor Visitor(*this, ID, D); 2362 ModuleMgr.visit(ObjCChainedCategoriesVisitor::visit, &Visitor); 2363 // Also add the categories that the interface already links to. 2364 Visitor.addCategories(D->getCategoryList()); 2365 D->setCategoryList(Visitor.getHeadCategory()); 2366 } 2367 2368 void ASTDeclReader::UpdateDecl(Decl *D, ModuleFile &ModuleFile, 2369 const RecordData &Record) { 2370 unsigned Idx = 0; 2371 while (Idx < Record.size()) { 2372 switch ((DeclUpdateKind)Record[Idx++]) { 2373 case UPD_CXX_ADDED_IMPLICIT_MEMBER: 2374 cast<CXXRecordDecl>(D)->addedMember(Reader.ReadDecl(ModuleFile, Record, Idx)); 2375 break; 2376 2377 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: 2378 // It will be added to the template's specializations set when loaded. 2379 (void)Reader.ReadDecl(ModuleFile, Record, Idx); 2380 break; 2381 2382 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: { 2383 NamespaceDecl *Anon 2384 = Reader.ReadDeclAs<NamespaceDecl>(ModuleFile, Record, Idx); 2385 2386 // Each module has its own anonymous namespace, which is disjoint from 2387 // any other module's anonymous namespaces, so don't attach the anonymous 2388 // namespace at all. 2389 if (ModuleFile.Kind != MK_Module) { 2390 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D)) 2391 TU->setAnonymousNamespace(Anon); 2392 else 2393 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon); 2394 } 2395 break; 2396 } 2397 2398 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: 2399 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation( 2400 Reader.ReadSourceLocation(ModuleFile, Record, Idx)); 2401 break; 2402 } 2403 } 2404 } 2405