ASTReader.cpp revision de4e0a8e57e643bbe78ad37ad6023c45a8a9f7e2
1af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===// 2af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// 3af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// The LLVM Compiler Infrastructure 4af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// 5d3df75b4472c9d5d4d2615aaea74d2adce4160f8machenbach@chromium.org// This file is distributed under the University of Illinois Open Source 6af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// License. See LICENSE.TXT for details. 7af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// 8af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org//===----------------------------------------------------------------------===// 9af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// 10af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// This file defines the ASTReader class, which reads AST files. 11af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// 12af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org//===----------------------------------------------------------------------===// 13af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org 14af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Serialization/ASTReader.h" 15af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Serialization/ASTDeserializationListener.h" 16af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Serialization/ModuleManager.h" 17af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "ASTCommon.h" 18af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "ASTReaderInternals.h" 19af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Frontend/FrontendDiagnostic.h" 20af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Frontend/Utils.h" 21af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Sema/Sema.h" 22af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Sema/Scope.h" 23af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/ASTConsumer.h" 24af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/ASTContext.h" 25af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/DeclTemplate.h" 26af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/Expr.h" 27af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/ExprCXX.h" 28af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/NestedNameSpecifier.h" 29af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/Type.h" 30af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/AST/TypeLocVisitor.h" 31af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Lex/MacroInfo.h" 32af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Lex/PreprocessingRecord.h" 33af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Lex/Preprocessor.h" 34af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Lex/HeaderSearch.h" 35af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/OnDiskHashTable.h" 36af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/SourceManager.h" 37af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/SourceManagerInternals.h" 38af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/FileManager.h" 39af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/FileSystemStatCache.h" 40af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/TargetInfo.h" 41af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/Version.h" 42af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "clang/Basic/VersionTuple.h" 43af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/ADT/StringExtras.h" 44af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/Bitcode/BitstreamReader.h" 45af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/Support/MemoryBuffer.h" 46af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/Support/ErrorHandling.h" 47af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/Support/FileSystem.h" 48af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/Support/Path.h" 49af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include "llvm/Support/system_error.h" 50af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include <algorithm> 51af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include <iterator> 52af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include <cstdio> 53af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org#include <sys/stat.h> 54af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org 55af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.orgusing namespace clang; 56af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.orgusing namespace clang::serialization; 57af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.orgusing namespace clang::serialization::reader; 58af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org 59af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org//===----------------------------------------------------------------------===// 60af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org// PCH validator implementation 61af6f699b0be532b73bc2f6c9e1cf40a57fa7e234machenbach@chromium.org//===----------------------------------------------------------------------===// 62 63ASTReaderListener::~ASTReaderListener() {} 64 65bool 66PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { 67 const LangOptions &PPLangOpts = PP.getLangOptions(); 68 69#define LANGOPT(Name, Bits, Default, Description) \ 70 if (PPLangOpts.Name != LangOpts.Name) { \ 71 Reader.Diag(diag::err_pch_langopt_mismatch) \ 72 << Description << LangOpts.Name << PPLangOpts.Name; \ 73 return true; \ 74 } 75 76#define VALUE_LANGOPT(Name, Bits, Default, Description) \ 77 if (PPLangOpts.Name != LangOpts.Name) { \ 78 Reader.Diag(diag::err_pch_langopt_value_mismatch) \ 79 << Description; \ 80 return true; \ 81} 82 83#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 84 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \ 85 Reader.Diag(diag::err_pch_langopt_value_mismatch) \ 86 << Description; \ 87 return true; \ 88 } 89 90#define BENIGN_LANGOPT(Name, Bits, Default, Description) 91#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 92#include "clang/Basic/LangOptions.def" 93 94 return false; 95} 96 97bool PCHValidator::ReadTargetTriple(StringRef Triple) { 98 if (Triple == PP.getTargetInfo().getTriple().str()) 99 return false; 100 101 Reader.Diag(diag::warn_pch_target_triple) 102 << Triple << PP.getTargetInfo().getTriple().str(); 103 return true; 104} 105 106namespace { 107 struct EmptyStringRef { 108 bool operator ()(StringRef r) const { return r.empty(); } 109 }; 110 struct EmptyBlock { 111 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();} 112 }; 113} 114 115static bool EqualConcatenations(SmallVector<StringRef, 2> L, 116 PCHPredefinesBlocks R) { 117 // First, sum up the lengths. 118 unsigned LL = 0, RL = 0; 119 for (unsigned I = 0, N = L.size(); I != N; ++I) { 120 LL += L[I].size(); 121 } 122 for (unsigned I = 0, N = R.size(); I != N; ++I) { 123 RL += R[I].Data.size(); 124 } 125 if (LL != RL) 126 return false; 127 if (LL == 0 && RL == 0) 128 return true; 129 130 // Kick out empty parts, they confuse the algorithm below. 131 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end()); 132 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end()); 133 134 // Do it the hard way. At this point, both vectors must be non-empty. 135 StringRef LR = L[0], RR = R[0].Data; 136 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size(); 137 (void) RN; 138 for (;;) { 139 // Compare the current pieces. 140 if (LR.size() == RR.size()) { 141 // If they're the same length, it's pretty easy. 142 if (LR != RR) 143 return false; 144 // Both pieces are done, advance. 145 ++LI; 146 ++RI; 147 // If either string is done, they're both done, since they're the same 148 // length. 149 if (LI == LN) { 150 assert(RI == RN && "Strings not the same length after all?"); 151 return true; 152 } 153 LR = L[LI]; 154 RR = R[RI].Data; 155 } else if (LR.size() < RR.size()) { 156 // Right piece is longer. 157 if (!RR.startswith(LR)) 158 return false; 159 ++LI; 160 assert(LI != LN && "Strings not the same length after all?"); 161 RR = RR.substr(LR.size()); 162 LR = L[LI]; 163 } else { 164 // Left piece is longer. 165 if (!LR.startswith(RR)) 166 return false; 167 ++RI; 168 assert(RI != RN && "Strings not the same length after all?"); 169 LR = LR.substr(RR.size()); 170 RR = R[RI].Data; 171 } 172 } 173} 174 175static std::pair<FileID, StringRef::size_type> 176FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) { 177 std::pair<FileID, StringRef::size_type> Res; 178 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) { 179 Res.second = Buffers[I].Data.find(MacroDef); 180 if (Res.second != StringRef::npos) { 181 Res.first = Buffers[I].BufferID; 182 break; 183 } 184 } 185 return Res; 186} 187 188bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 189 StringRef OriginalFileName, 190 std::string &SuggestedPredefines, 191 FileManager &FileMgr) { 192 // We are in the context of an implicit include, so the predefines buffer will 193 // have a #include entry for the PCH file itself (as normalized by the 194 // preprocessor initialization). Find it and skip over it in the checking 195 // below. 196 llvm::SmallString<256> PCHInclude; 197 PCHInclude += "#include \""; 198 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr); 199 PCHInclude += "\"\n"; 200 std::pair<StringRef,StringRef> Split = 201 StringRef(PP.getPredefines()).split(PCHInclude.str()); 202 StringRef Left = Split.first, Right = Split.second; 203 if (Left == PP.getPredefines()) { 204 Error("Missing PCH include entry!"); 205 return true; 206 } 207 208 // If the concatenation of all the PCH buffers is equal to the adjusted 209 // command line, we're done. 210 SmallVector<StringRef, 2> CommandLine; 211 CommandLine.push_back(Left); 212 CommandLine.push_back(Right); 213 if (EqualConcatenations(CommandLine, Buffers)) 214 return false; 215 216 SourceManager &SourceMgr = PP.getSourceManager(); 217 218 // The predefines buffers are different. Determine what the differences are, 219 // and whether they require us to reject the PCH file. 220 SmallVector<StringRef, 8> PCHLines; 221 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) 222 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 223 224 SmallVector<StringRef, 8> CmdLineLines; 225 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 226 227 // Pick out implicit #includes after the PCH and don't consider them for 228 // validation; we will insert them into SuggestedPredefines so that the 229 // preprocessor includes them. 230 std::string IncludesAfterPCH; 231 SmallVector<StringRef, 8> AfterPCHLines; 232 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 233 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) { 234 if (AfterPCHLines[i].startswith("#include ")) { 235 IncludesAfterPCH += AfterPCHLines[i]; 236 IncludesAfterPCH += '\n'; 237 } else { 238 CmdLineLines.push_back(AfterPCHLines[i]); 239 } 240 } 241 242 // Make sure we add the includes last into SuggestedPredefines before we 243 // exit this function. 244 struct AddIncludesRAII { 245 std::string &SuggestedPredefines; 246 std::string &IncludesAfterPCH; 247 248 AddIncludesRAII(std::string &SuggestedPredefines, 249 std::string &IncludesAfterPCH) 250 : SuggestedPredefines(SuggestedPredefines), 251 IncludesAfterPCH(IncludesAfterPCH) { } 252 ~AddIncludesRAII() { 253 SuggestedPredefines += IncludesAfterPCH; 254 } 255 } AddIncludes(SuggestedPredefines, IncludesAfterPCH); 256 257 // Sort both sets of predefined buffer lines, since we allow some extra 258 // definitions and they may appear at any point in the output. 259 std::sort(CmdLineLines.begin(), CmdLineLines.end()); 260 std::sort(PCHLines.begin(), PCHLines.end()); 261 262 // Determine which predefines that were used to build the PCH file are missing 263 // from the command line. 264 std::vector<StringRef> MissingPredefines; 265 std::set_difference(PCHLines.begin(), PCHLines.end(), 266 CmdLineLines.begin(), CmdLineLines.end(), 267 std::back_inserter(MissingPredefines)); 268 269 bool MissingDefines = false; 270 bool ConflictingDefines = false; 271 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { 272 StringRef Missing = MissingPredefines[I]; 273 if (Missing.startswith("#include ")) { 274 // An -include was specified when generating the PCH; it is included in 275 // the PCH, just ignore it. 276 continue; 277 } 278 if (!Missing.startswith("#define ")) { 279 Reader.Diag(diag::warn_pch_compiler_options_mismatch); 280 return true; 281 } 282 283 // This is a macro definition. Determine the name of the macro we're 284 // defining. 285 std::string::size_type StartOfMacroName = strlen("#define "); 286 std::string::size_type EndOfMacroName 287 = Missing.find_first_of("( \n\r", StartOfMacroName); 288 assert(EndOfMacroName != std::string::npos && 289 "Couldn't find the end of the macro name"); 290 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); 291 292 // Determine whether this macro was given a different definition on the 293 // command line. 294 std::string MacroDefStart = "#define " + MacroName.str(); 295 std::string::size_type MacroDefLen = MacroDefStart.size(); 296 SmallVector<StringRef, 8>::iterator ConflictPos 297 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), 298 MacroDefStart); 299 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) { 300 if (!ConflictPos->startswith(MacroDefStart)) { 301 // Different macro; we're done. 302 ConflictPos = CmdLineLines.end(); 303 break; 304 } 305 306 assert(ConflictPos->size() > MacroDefLen && 307 "Invalid #define in predefines buffer?"); 308 if ((*ConflictPos)[MacroDefLen] != ' ' && 309 (*ConflictPos)[MacroDefLen] != '(') 310 continue; // Longer macro name; keep trying. 311 312 // We found a conflicting macro definition. 313 break; 314 } 315 316 if (ConflictPos != CmdLineLines.end()) { 317 Reader.Diag(diag::warn_cmdline_conflicting_macro_def) 318 << MacroName; 319 320 // Show the definition of this macro within the PCH file. 321 std::pair<FileID, StringRef::size_type> MacroLoc = 322 FindMacro(Buffers, Missing); 323 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!"); 324 SourceLocation PCHMissingLoc = 325 SourceMgr.getLocForStartOfFile(MacroLoc.first) 326 .getLocWithOffset(MacroLoc.second); 327 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; 328 329 ConflictingDefines = true; 330 continue; 331 } 332 333 // If the macro doesn't conflict, then we'll just pick up the macro 334 // definition from the PCH file. Warn the user that they made a mistake. 335 if (ConflictingDefines) 336 continue; // Don't complain if there are already conflicting defs 337 338 if (!MissingDefines) { 339 Reader.Diag(diag::warn_cmdline_missing_macro_defs); 340 MissingDefines = true; 341 } 342 343 // Show the definition of this macro within the PCH file. 344 std::pair<FileID, StringRef::size_type> MacroLoc = 345 FindMacro(Buffers, Missing); 346 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!"); 347 SourceLocation PCHMissingLoc = 348 SourceMgr.getLocForStartOfFile(MacroLoc.first) 349 .getLocWithOffset(MacroLoc.second); 350 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); 351 } 352 353 if (ConflictingDefines) 354 return true; 355 356 // Determine what predefines were introduced based on command-line 357 // parameters that were not present when building the PCH 358 // file. Extra #defines are okay, so long as the identifiers being 359 // defined were not used within the precompiled header. 360 std::vector<StringRef> ExtraPredefines; 361 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), 362 PCHLines.begin(), PCHLines.end(), 363 std::back_inserter(ExtraPredefines)); 364 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { 365 StringRef &Extra = ExtraPredefines[I]; 366 if (!Extra.startswith("#define ")) { 367 Reader.Diag(diag::warn_pch_compiler_options_mismatch); 368 return true; 369 } 370 371 // This is an extra macro definition. Determine the name of the 372 // macro we're defining. 373 std::string::size_type StartOfMacroName = strlen("#define "); 374 std::string::size_type EndOfMacroName 375 = Extra.find_first_of("( \n\r", StartOfMacroName); 376 assert(EndOfMacroName != std::string::npos && 377 "Couldn't find the end of the macro name"); 378 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); 379 380 // Check whether this name was used somewhere in the PCH file. If 381 // so, defining it as a macro could change behavior, so we reject 382 // the PCH file. 383 if (IdentifierInfo *II = Reader.get(MacroName)) { 384 Reader.Diag(diag::warn_macro_name_used_in_pch) << II; 385 return true; 386 } 387 388 // Add this definition to the suggested predefines buffer. 389 SuggestedPredefines += Extra; 390 SuggestedPredefines += '\n'; 391 } 392 393 // If we get here, it's because the predefines buffer had compatible 394 // contents. Accept the PCH file. 395 return false; 396} 397 398void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, 399 unsigned ID) { 400 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); 401 ++NumHeaderInfos; 402} 403 404void PCHValidator::ReadCounter(unsigned Value) { 405 PP.setCounterValue(Value); 406} 407 408//===----------------------------------------------------------------------===// 409// AST reader implementation 410//===----------------------------------------------------------------------===// 411 412void 413ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) { 414 DeserializationListener = Listener; 415} 416 417 418 419unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 420 return serialization::ComputeHash(Sel); 421} 422 423 424std::pair<unsigned, unsigned> 425ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 426 using namespace clang::io; 427 unsigned KeyLen = ReadUnalignedLE16(d); 428 unsigned DataLen = ReadUnalignedLE16(d); 429 return std::make_pair(KeyLen, DataLen); 430} 431 432ASTSelectorLookupTrait::internal_key_type 433ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 434 using namespace clang::io; 435 SelectorTable &SelTable = Reader.getContext().Selectors; 436 unsigned N = ReadUnalignedLE16(d); 437 IdentifierInfo *FirstII 438 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 439 if (N == 0) 440 return SelTable.getNullarySelector(FirstII); 441 else if (N == 1) 442 return SelTable.getUnarySelector(FirstII); 443 444 SmallVector<IdentifierInfo *, 16> Args; 445 Args.push_back(FirstII); 446 for (unsigned I = 1; I != N; ++I) 447 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d))); 448 449 return SelTable.getSelector(N, Args.data()); 450} 451 452ASTSelectorLookupTrait::data_type 453ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 454 unsigned DataLen) { 455 using namespace clang::io; 456 457 data_type Result; 458 459 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d)); 460 unsigned NumInstanceMethods = ReadUnalignedLE16(d); 461 unsigned NumFactoryMethods = ReadUnalignedLE16(d); 462 463 // Load instance methods 464 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 465 if (ObjCMethodDecl *Method 466 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 467 Result.Instance.push_back(Method); 468 } 469 470 // Load factory methods 471 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 472 if (ObjCMethodDecl *Method 473 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d))) 474 Result.Factory.push_back(Method); 475 } 476 477 return Result; 478} 479 480unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) { 481 return llvm::HashString(StringRef(a.first, a.second)); 482} 483 484std::pair<unsigned, unsigned> 485ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 486 using namespace clang::io; 487 unsigned DataLen = ReadUnalignedLE16(d); 488 unsigned KeyLen = ReadUnalignedLE16(d); 489 return std::make_pair(KeyLen, DataLen); 490} 491 492std::pair<const char*, unsigned> 493ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) { 494 assert(n >= 2 && d[n-1] == '\0'); 495 return std::make_pair((const char*) d, n-1); 496} 497 498IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 499 const unsigned char* d, 500 unsigned DataLen) { 501 using namespace clang::io; 502 unsigned RawID = ReadUnalignedLE32(d); 503 bool IsInteresting = RawID & 0x01; 504 505 // Wipe out the "is interesting" bit. 506 RawID = RawID >> 1; 507 508 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 509 if (!IsInteresting) { 510 // For uninteresting identifiers, just build the IdentifierInfo 511 // and associate it with the persistent ID. 512 IdentifierInfo *II = KnownII; 513 if (!II) 514 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second)); 515 Reader.SetIdentifierInfo(ID, II); 516 II->setIsFromAST(); 517 return II; 518 } 519 520 unsigned Bits = ReadUnalignedLE16(d); 521 bool CPlusPlusOperatorKeyword = Bits & 0x01; 522 Bits >>= 1; 523 bool HasRevertedTokenIDToIdentifier = Bits & 0x01; 524 Bits >>= 1; 525 bool Poisoned = Bits & 0x01; 526 Bits >>= 1; 527 bool ExtensionToken = Bits & 0x01; 528 Bits >>= 1; 529 bool hasMacroDefinition = Bits & 0x01; 530 Bits >>= 1; 531 unsigned ObjCOrBuiltinID = Bits & 0x3FF; 532 Bits >>= 10; 533 534 assert(Bits == 0 && "Extra bits in the identifier?"); 535 DataLen -= 6; 536 537 // Build the IdentifierInfo itself and link the identifier ID with 538 // the new IdentifierInfo. 539 IdentifierInfo *II = KnownII; 540 if (!II) 541 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second)); 542 Reader.SetIdentifierInfo(ID, II); 543 544 // Set or check the various bits in the IdentifierInfo structure. 545 // Token IDs are read-only. 546 if (HasRevertedTokenIDToIdentifier) 547 II->RevertTokenIDToIdentifier(); 548 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 549 assert(II->isExtensionToken() == ExtensionToken && 550 "Incorrect extension token flag"); 551 (void)ExtensionToken; 552 if (Poisoned) 553 II->setIsPoisoned(true); 554 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 555 "Incorrect C++ operator keyword flag"); 556 (void)CPlusPlusOperatorKeyword; 557 558 // If this identifier is a macro, deserialize the macro 559 // definition. 560 if (hasMacroDefinition) { 561 // FIXME: Check for conflicts? 562 uint32_t Offset = ReadUnalignedLE32(d); 563 Reader.SetIdentifierIsMacro(II, F, Offset); 564 DataLen -= 4; 565 } 566 567 // Read all of the declarations visible at global scope with this 568 // name. 569 if (DataLen > 0) { 570 SmallVector<uint32_t, 4> DeclIDs; 571 for (; DataLen > 0; DataLen -= 4) 572 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d))); 573 Reader.SetGloballyVisibleDecls(II, DeclIDs); 574 } 575 576 II->setIsFromAST(); 577 return II; 578} 579 580unsigned 581ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const { 582 llvm::FoldingSetNodeID ID; 583 ID.AddInteger(Key.Kind); 584 585 switch (Key.Kind) { 586 case DeclarationName::Identifier: 587 case DeclarationName::CXXLiteralOperatorName: 588 ID.AddString(((IdentifierInfo*)Key.Data)->getName()); 589 break; 590 case DeclarationName::ObjCZeroArgSelector: 591 case DeclarationName::ObjCOneArgSelector: 592 case DeclarationName::ObjCMultiArgSelector: 593 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data))); 594 break; 595 case DeclarationName::CXXOperatorName: 596 ID.AddInteger((OverloadedOperatorKind)Key.Data); 597 break; 598 case DeclarationName::CXXConstructorName: 599 case DeclarationName::CXXDestructorName: 600 case DeclarationName::CXXConversionFunctionName: 601 case DeclarationName::CXXUsingDirective: 602 break; 603 } 604 605 return ID.ComputeHash(); 606} 607 608ASTDeclContextNameLookupTrait::internal_key_type 609ASTDeclContextNameLookupTrait::GetInternalKey( 610 const external_key_type& Name) const { 611 DeclNameKey Key; 612 Key.Kind = Name.getNameKind(); 613 switch (Name.getNameKind()) { 614 case DeclarationName::Identifier: 615 Key.Data = (uint64_t)Name.getAsIdentifierInfo(); 616 break; 617 case DeclarationName::ObjCZeroArgSelector: 618 case DeclarationName::ObjCOneArgSelector: 619 case DeclarationName::ObjCMultiArgSelector: 620 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 621 break; 622 case DeclarationName::CXXOperatorName: 623 Key.Data = Name.getCXXOverloadedOperator(); 624 break; 625 case DeclarationName::CXXLiteralOperatorName: 626 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier(); 627 break; 628 case DeclarationName::CXXConstructorName: 629 case DeclarationName::CXXDestructorName: 630 case DeclarationName::CXXConversionFunctionName: 631 case DeclarationName::CXXUsingDirective: 632 Key.Data = 0; 633 break; 634 } 635 636 return Key; 637} 638 639ASTDeclContextNameLookupTrait::external_key_type 640ASTDeclContextNameLookupTrait::GetExternalKey( 641 const internal_key_type& Key) const { 642 ASTContext &Context = Reader.getContext(); 643 switch (Key.Kind) { 644 case DeclarationName::Identifier: 645 return DeclarationName((IdentifierInfo*)Key.Data); 646 647 case DeclarationName::ObjCZeroArgSelector: 648 case DeclarationName::ObjCOneArgSelector: 649 case DeclarationName::ObjCMultiArgSelector: 650 return DeclarationName(Selector(Key.Data)); 651 652 case DeclarationName::CXXConstructorName: 653 return Context.DeclarationNames.getCXXConstructorName( 654 Context.getCanonicalType(Reader.getLocalType(F, Key.Data))); 655 656 case DeclarationName::CXXDestructorName: 657 return Context.DeclarationNames.getCXXDestructorName( 658 Context.getCanonicalType(Reader.getLocalType(F, Key.Data))); 659 660 case DeclarationName::CXXConversionFunctionName: 661 return Context.DeclarationNames.getCXXConversionFunctionName( 662 Context.getCanonicalType(Reader.getLocalType(F, Key.Data))); 663 664 case DeclarationName::CXXOperatorName: 665 return Context.DeclarationNames.getCXXOperatorName( 666 (OverloadedOperatorKind)Key.Data); 667 668 case DeclarationName::CXXLiteralOperatorName: 669 return Context.DeclarationNames.getCXXLiteralOperatorName( 670 (IdentifierInfo*)Key.Data); 671 672 case DeclarationName::CXXUsingDirective: 673 return DeclarationName::getUsingDirectiveName(); 674 } 675 676 llvm_unreachable("Invalid Name Kind ?"); 677} 678 679std::pair<unsigned, unsigned> 680ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 681 using namespace clang::io; 682 unsigned KeyLen = ReadUnalignedLE16(d); 683 unsigned DataLen = ReadUnalignedLE16(d); 684 return std::make_pair(KeyLen, DataLen); 685} 686 687ASTDeclContextNameLookupTrait::internal_key_type 688ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) { 689 using namespace clang::io; 690 691 DeclNameKey Key; 692 Key.Kind = (DeclarationName::NameKind)*d++; 693 switch (Key.Kind) { 694 case DeclarationName::Identifier: 695 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 696 break; 697 case DeclarationName::ObjCZeroArgSelector: 698 case DeclarationName::ObjCOneArgSelector: 699 case DeclarationName::ObjCMultiArgSelector: 700 Key.Data = 701 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d)) 702 .getAsOpaquePtr(); 703 break; 704 case DeclarationName::CXXOperatorName: 705 Key.Data = *d++; // OverloadedOperatorKind 706 break; 707 case DeclarationName::CXXLiteralOperatorName: 708 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); 709 break; 710 case DeclarationName::CXXConstructorName: 711 case DeclarationName::CXXDestructorName: 712 case DeclarationName::CXXConversionFunctionName: 713 case DeclarationName::CXXUsingDirective: 714 Key.Data = 0; 715 break; 716 } 717 718 return Key; 719} 720 721ASTDeclContextNameLookupTrait::data_type 722ASTDeclContextNameLookupTrait::ReadData(internal_key_type, 723 const unsigned char* d, 724 unsigned DataLen) { 725 using namespace clang::io; 726 unsigned NumDecls = ReadUnalignedLE16(d); 727 DeclID *Start = (DeclID *)d; 728 return std::make_pair(Start, Start + NumDecls); 729} 730 731bool ASTReader::ReadDeclContextStorage(Module &M, 732 llvm::BitstreamCursor &Cursor, 733 const std::pair<uint64_t, uint64_t> &Offsets, 734 DeclContextInfo &Info) { 735 SavedStreamPosition SavedPosition(Cursor); 736 // First the lexical decls. 737 if (Offsets.first != 0) { 738 Cursor.JumpToBit(Offsets.first); 739 740 RecordData Record; 741 const char *Blob; 742 unsigned BlobLen; 743 unsigned Code = Cursor.ReadCode(); 744 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen); 745 if (RecCode != DECL_CONTEXT_LEXICAL) { 746 Error("Expected lexical block"); 747 return true; 748 } 749 750 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob); 751 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair); 752 } 753 754 // Now the lookup table. 755 if (Offsets.second != 0) { 756 Cursor.JumpToBit(Offsets.second); 757 758 RecordData Record; 759 const char *Blob; 760 unsigned BlobLen; 761 unsigned Code = Cursor.ReadCode(); 762 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen); 763 if (RecCode != DECL_CONTEXT_VISIBLE) { 764 Error("Expected visible lookup table block"); 765 return true; 766 } 767 Info.NameLookupTableData 768 = ASTDeclContextNameLookupTable::Create( 769 (const unsigned char *)Blob + Record[0], 770 (const unsigned char *)Blob, 771 ASTDeclContextNameLookupTrait(*this, M)); 772 } 773 774 return false; 775} 776 777void ASTReader::Error(StringRef Msg) { 778 Error(diag::err_fe_pch_malformed, Msg); 779} 780 781void ASTReader::Error(unsigned DiagID, 782 StringRef Arg1, StringRef Arg2) { 783 if (Diags.isDiagnosticInFlight()) 784 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 785 else 786 Diag(DiagID) << Arg1 << Arg2; 787} 788 789/// \brief Tell the AST listener about the predefines buffers in the chain. 790bool ASTReader::CheckPredefinesBuffers() { 791 if (Listener) 792 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers, 793 ActualOriginalFileName, 794 SuggestedPredefines, 795 FileMgr); 796 return false; 797} 798 799//===----------------------------------------------------------------------===// 800// Source Manager Deserialization 801//===----------------------------------------------------------------------===// 802 803/// \brief Read the line table in the source manager block. 804/// \returns true if there was an error. 805bool ASTReader::ParseLineTable(Module &F, 806 SmallVectorImpl<uint64_t> &Record) { 807 unsigned Idx = 0; 808 LineTableInfo &LineTable = SourceMgr.getLineTable(); 809 810 // Parse the file names 811 std::map<int, int> FileIDs; 812 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 813 // Extract the file name 814 unsigned FilenameLen = Record[Idx++]; 815 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 816 Idx += FilenameLen; 817 MaybeAddSystemRootToFilename(Filename); 818 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 819 } 820 821 // Parse the line entries 822 std::vector<LineEntry> Entries; 823 while (Idx < Record.size()) { 824 int FID = Record[Idx++]; 825 assert(FID >= 0 && "Serialized line entries for non-local file."); 826 // Remap FileID from 1-based old view. 827 FID += F.SLocEntryBaseID - 1; 828 829 // Extract the line entries 830 unsigned NumEntries = Record[Idx++]; 831 assert(NumEntries && "Numentries is 00000"); 832 Entries.clear(); 833 Entries.reserve(NumEntries); 834 for (unsigned I = 0; I != NumEntries; ++I) { 835 unsigned FileOffset = Record[Idx++]; 836 unsigned LineNo = Record[Idx++]; 837 int FilenameID = FileIDs[Record[Idx++]]; 838 SrcMgr::CharacteristicKind FileKind 839 = (SrcMgr::CharacteristicKind)Record[Idx++]; 840 unsigned IncludeOffset = Record[Idx++]; 841 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 842 FileKind, IncludeOffset)); 843 } 844 LineTable.AddEntry(FID, Entries); 845 } 846 847 return false; 848} 849 850namespace { 851 852class ASTStatData { 853public: 854 const ino_t ino; 855 const dev_t dev; 856 const mode_t mode; 857 const time_t mtime; 858 const off_t size; 859 860 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) 861 : ino(i), dev(d), mode(mo), mtime(m), size(s) {} 862}; 863 864class ASTStatLookupTrait { 865 public: 866 typedef const char *external_key_type; 867 typedef const char *internal_key_type; 868 869 typedef ASTStatData data_type; 870 871 static unsigned ComputeHash(const char *path) { 872 return llvm::HashString(path); 873 } 874 875 static internal_key_type GetInternalKey(const char *path) { return path; } 876 877 static bool EqualKey(internal_key_type a, internal_key_type b) { 878 return strcmp(a, b) == 0; 879 } 880 881 static std::pair<unsigned, unsigned> 882 ReadKeyDataLength(const unsigned char*& d) { 883 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 884 unsigned DataLen = (unsigned) *d++; 885 return std::make_pair(KeyLen + 1, DataLen); 886 } 887 888 static internal_key_type ReadKey(const unsigned char *d, unsigned) { 889 return (const char *)d; 890 } 891 892 static data_type ReadData(const internal_key_type, const unsigned char *d, 893 unsigned /*DataLen*/) { 894 using namespace clang::io; 895 896 ino_t ino = (ino_t) ReadUnalignedLE32(d); 897 dev_t dev = (dev_t) ReadUnalignedLE32(d); 898 mode_t mode = (mode_t) ReadUnalignedLE16(d); 899 time_t mtime = (time_t) ReadUnalignedLE64(d); 900 off_t size = (off_t) ReadUnalignedLE64(d); 901 return data_type(ino, dev, mode, mtime, size); 902 } 903}; 904 905/// \brief stat() cache for precompiled headers. 906/// 907/// This cache is very similar to the stat cache used by pretokenized 908/// headers. 909class ASTStatCache : public FileSystemStatCache { 910 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy; 911 CacheTy *Cache; 912 913 unsigned &NumStatHits, &NumStatMisses; 914public: 915 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base, 916 unsigned &NumStatHits, unsigned &NumStatMisses) 917 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { 918 Cache = CacheTy::Create(Buckets, Base); 919 } 920 921 ~ASTStatCache() { delete Cache; } 922 923 LookupResult getStat(const char *Path, struct stat &StatBuf, 924 int *FileDescriptor) { 925 // Do the lookup for the file's data in the AST file. 926 CacheTy::iterator I = Cache->find(Path); 927 928 // If we don't get a hit in the AST file just forward to 'stat'. 929 if (I == Cache->end()) { 930 ++NumStatMisses; 931 return statChained(Path, StatBuf, FileDescriptor); 932 } 933 934 ++NumStatHits; 935 ASTStatData Data = *I; 936 937 StatBuf.st_ino = Data.ino; 938 StatBuf.st_dev = Data.dev; 939 StatBuf.st_mtime = Data.mtime; 940 StatBuf.st_mode = Data.mode; 941 StatBuf.st_size = Data.size; 942 return CacheExists; 943 } 944}; 945} // end anonymous namespace 946 947 948/// \brief Read a source manager block 949ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) { 950 using namespace SrcMgr; 951 952 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 953 954 // Set the source-location entry cursor to the current position in 955 // the stream. This cursor will be used to read the contents of the 956 // source manager block initially, and then lazily read 957 // source-location entries as needed. 958 SLocEntryCursor = F.Stream; 959 960 // The stream itself is going to skip over the source manager block. 961 if (F.Stream.SkipBlock()) { 962 Error("malformed block record in AST file"); 963 return Failure; 964 } 965 966 // Enter the source manager block. 967 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 968 Error("malformed source manager block record in AST file"); 969 return Failure; 970 } 971 972 RecordData Record; 973 while (true) { 974 unsigned Code = SLocEntryCursor.ReadCode(); 975 if (Code == llvm::bitc::END_BLOCK) { 976 if (SLocEntryCursor.ReadBlockEnd()) { 977 Error("error at end of Source Manager block in AST file"); 978 return Failure; 979 } 980 return Success; 981 } 982 983 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 984 // No known subblocks, always skip them. 985 SLocEntryCursor.ReadSubBlockID(); 986 if (SLocEntryCursor.SkipBlock()) { 987 Error("malformed block record in AST file"); 988 return Failure; 989 } 990 continue; 991 } 992 993 if (Code == llvm::bitc::DEFINE_ABBREV) { 994 SLocEntryCursor.ReadAbbrevRecord(); 995 continue; 996 } 997 998 // Read a record. 999 const char *BlobStart; 1000 unsigned BlobLen; 1001 Record.clear(); 1002 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1003 default: // Default behavior: ignore. 1004 break; 1005 1006 case SM_SLOC_FILE_ENTRY: 1007 case SM_SLOC_BUFFER_ENTRY: 1008 case SM_SLOC_EXPANSION_ENTRY: 1009 // Once we hit one of the source location entries, we're done. 1010 return Success; 1011 } 1012 } 1013} 1014 1015/// \brief If a header file is not found at the path that we expect it to be 1016/// and the PCH file was moved from its original location, try to resolve the 1017/// file by assuming that header+PCH were moved together and the header is in 1018/// the same place relative to the PCH. 1019static std::string 1020resolveFileRelativeToOriginalDir(const std::string &Filename, 1021 const std::string &OriginalDir, 1022 const std::string &CurrDir) { 1023 assert(OriginalDir != CurrDir && 1024 "No point trying to resolve the file if the PCH dir didn't change"); 1025 using namespace llvm::sys; 1026 llvm::SmallString<128> filePath(Filename); 1027 fs::make_absolute(filePath); 1028 assert(path::is_absolute(OriginalDir)); 1029 llvm::SmallString<128> currPCHPath(CurrDir); 1030 1031 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1032 fileDirE = path::end(path::parent_path(filePath)); 1033 path::const_iterator origDirI = path::begin(OriginalDir), 1034 origDirE = path::end(OriginalDir); 1035 // Skip the common path components from filePath and OriginalDir. 1036 while (fileDirI != fileDirE && origDirI != origDirE && 1037 *fileDirI == *origDirI) { 1038 ++fileDirI; 1039 ++origDirI; 1040 } 1041 for (; origDirI != origDirE; ++origDirI) 1042 path::append(currPCHPath, ".."); 1043 path::append(currPCHPath, fileDirI, fileDirE); 1044 path::append(currPCHPath, path::filename(Filename)); 1045 return currPCHPath.str(); 1046} 1047 1048/// \brief Read in the source location entry with the given ID. 1049ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) { 1050 if (ID == 0) 1051 return Success; 1052 1053 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1054 Error("source location entry ID out-of-range for AST file"); 1055 return Failure; 1056 } 1057 1058 Module *F = GlobalSLocEntryMap.find(-ID)->second; 1059 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 1060 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1061 unsigned BaseOffset = F->SLocEntryBaseOffset; 1062 1063 ++NumSLocEntriesRead; 1064 unsigned Code = SLocEntryCursor.ReadCode(); 1065 if (Code == llvm::bitc::END_BLOCK || 1066 Code == llvm::bitc::ENTER_SUBBLOCK || 1067 Code == llvm::bitc::DEFINE_ABBREV) { 1068 Error("incorrectly-formatted source location entry in AST file"); 1069 return Failure; 1070 } 1071 1072 RecordData Record; 1073 const char *BlobStart; 1074 unsigned BlobLen; 1075 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1076 default: 1077 Error("incorrectly-formatted source location entry in AST file"); 1078 return Failure; 1079 1080 case SM_SLOC_FILE_ENTRY: { 1081 std::string Filename(BlobStart, BlobStart + BlobLen); 1082 MaybeAddSystemRootToFilename(Filename); 1083 const FileEntry *File = FileMgr.getFile(Filename); 1084 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() && 1085 OriginalDir != CurrentDir) { 1086 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 1087 OriginalDir, 1088 CurrentDir); 1089 if (!resolved.empty()) 1090 File = FileMgr.getFile(resolved); 1091 } 1092 if (File == 0) 1093 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4], 1094 (time_t)Record[5]); 1095 if (File == 0) { 1096 std::string ErrorStr = "could not find file '"; 1097 ErrorStr += Filename; 1098 ErrorStr += "' referenced by AST file"; 1099 Error(ErrorStr.c_str()); 1100 return Failure; 1101 } 1102 1103 if (Record.size() < 6) { 1104 Error("source location entry is incorrect"); 1105 return Failure; 1106 } 1107 1108 if (!DisableValidation && 1109 ((off_t)Record[4] != File->getSize() 1110#if !defined(LLVM_ON_WIN32) 1111 // In our regression testing, the Windows file system seems to 1112 // have inconsistent modification times that sometimes 1113 // erroneously trigger this error-handling path. 1114 || (time_t)Record[5] != File->getModificationTime() 1115#endif 1116 )) { 1117 Error(diag::err_fe_pch_file_modified, Filename); 1118 return Failure; 1119 } 1120 1121 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1122 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1123 // This is the module's main file. 1124 IncludeLoc = getImportLocation(F); 1125 } 1126 FileID FID = SourceMgr.createFileID(File, IncludeLoc, 1127 (SrcMgr::CharacteristicKind)Record[2], 1128 ID, BaseOffset + Record[0]); 1129 SrcMgr::FileInfo &FileInfo = 1130 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1131 FileInfo.NumCreatedFIDs = Record[6]; 1132 if (Record[3]) 1133 FileInfo.setHasLineDirectives(); 1134 1135 break; 1136 } 1137 1138 case SM_SLOC_BUFFER_ENTRY: { 1139 const char *Name = BlobStart; 1140 unsigned Offset = Record[0]; 1141 unsigned Code = SLocEntryCursor.ReadCode(); 1142 Record.clear(); 1143 unsigned RecCode 1144 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); 1145 1146 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1147 Error("AST record has invalid code"); 1148 return Failure; 1149 } 1150 1151 llvm::MemoryBuffer *Buffer 1152 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1), 1153 Name); 1154 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, 1155 BaseOffset + Offset); 1156 1157 if (strcmp(Name, "<built-in>") == 0) { 1158 PCHPredefinesBlock Block = { 1159 BufferID, 1160 StringRef(BlobStart, BlobLen - 1) 1161 }; 1162 PCHPredefinesBuffers.push_back(Block); 1163 } 1164 1165 break; 1166 } 1167 1168 case SM_SLOC_EXPANSION_ENTRY: { 1169 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1170 SourceMgr.createExpansionLoc(SpellingLoc, 1171 ReadSourceLocation(*F, Record[2]), 1172 ReadSourceLocation(*F, Record[3]), 1173 Record[4], 1174 ID, 1175 BaseOffset + Record[0]); 1176 break; 1177 } 1178 } 1179 1180 return Success; 1181} 1182 1183/// \brief Find the location where the module F is imported. 1184SourceLocation ASTReader::getImportLocation(Module *F) { 1185 if (F->ImportLoc.isValid()) 1186 return F->ImportLoc; 1187 1188 // Otherwise we have a PCH. It's considered to be "imported" at the first 1189 // location of its includer. 1190 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1191 // Main file is the importer. We assume that it is the first entry in the 1192 // entry table. We can't ask the manager, because at the time of PCH loading 1193 // the main file entry doesn't exist yet. 1194 // The very first entry is the invalid instantiation loc, which takes up 1195 // offsets 0 and 1. 1196 return SourceLocation::getFromRawEncoding(2U); 1197 } 1198 //return F->Loaders[0]->FirstLoc; 1199 return F->ImportedBy[0]->FirstLoc; 1200} 1201 1202/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1203/// specified cursor. Read the abbreviations that are at the top of the block 1204/// and then leave the cursor pointing into the block. 1205bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, 1206 unsigned BlockID) { 1207 if (Cursor.EnterSubBlock(BlockID)) { 1208 Error("malformed block record in AST file"); 1209 return Failure; 1210 } 1211 1212 while (true) { 1213 uint64_t Offset = Cursor.GetCurrentBitNo(); 1214 unsigned Code = Cursor.ReadCode(); 1215 1216 // We expect all abbrevs to be at the start of the block. 1217 if (Code != llvm::bitc::DEFINE_ABBREV) { 1218 Cursor.JumpToBit(Offset); 1219 return false; 1220 } 1221 Cursor.ReadAbbrevRecord(); 1222 } 1223} 1224 1225void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) { 1226 llvm::BitstreamCursor &Stream = F.MacroCursor; 1227 1228 // Keep track of where we are in the stream, then jump back there 1229 // after reading this macro. 1230 SavedStreamPosition SavedPosition(Stream); 1231 1232 Stream.JumpToBit(Offset); 1233 RecordData Record; 1234 SmallVector<IdentifierInfo*, 16> MacroArgs; 1235 MacroInfo *Macro = 0; 1236 1237 while (true) { 1238 unsigned Code = Stream.ReadCode(); 1239 switch (Code) { 1240 case llvm::bitc::END_BLOCK: 1241 return; 1242 1243 case llvm::bitc::ENTER_SUBBLOCK: 1244 // No known subblocks, always skip them. 1245 Stream.ReadSubBlockID(); 1246 if (Stream.SkipBlock()) { 1247 Error("malformed block record in AST file"); 1248 return; 1249 } 1250 continue; 1251 1252 case llvm::bitc::DEFINE_ABBREV: 1253 Stream.ReadAbbrevRecord(); 1254 continue; 1255 default: break; 1256 } 1257 1258 // Read a record. 1259 const char *BlobStart = 0; 1260 unsigned BlobLen = 0; 1261 Record.clear(); 1262 PreprocessorRecordTypes RecType = 1263 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart, 1264 BlobLen); 1265 switch (RecType) { 1266 case PP_MACRO_OBJECT_LIKE: 1267 case PP_MACRO_FUNCTION_LIKE: { 1268 // If we already have a macro, that means that we've hit the end 1269 // of the definition of the macro we were looking for. We're 1270 // done. 1271 if (Macro) 1272 return; 1273 1274 IdentifierInfo *II = getLocalIdentifier(F, Record[0]); 1275 if (II == 0) { 1276 Error("macro must have a name in AST file"); 1277 return; 1278 } 1279 SourceLocation Loc = ReadSourceLocation(F, Record[1]); 1280 bool isUsed = Record[2]; 1281 1282 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1283 MI->setIsUsed(isUsed); 1284 MI->setIsFromAST(); 1285 1286 unsigned NextIndex = 3; 1287 MI->setExportLocation(ReadSourceLocation(F, Record, NextIndex)); 1288 1289 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1290 // Decode function-like macro info. 1291 bool isC99VarArgs = Record[NextIndex++]; 1292 bool isGNUVarArgs = Record[NextIndex++]; 1293 MacroArgs.clear(); 1294 unsigned NumArgs = Record[NextIndex++]; 1295 for (unsigned i = 0; i != NumArgs; ++i) 1296 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1297 1298 // Install function-like macro info. 1299 MI->setIsFunctionLike(); 1300 if (isC99VarArgs) MI->setIsC99Varargs(); 1301 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1302 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1303 PP.getPreprocessorAllocator()); 1304 } 1305 1306 // Finally, install the macro. 1307 PP.setMacroInfo(II, MI); 1308 1309 // Remember that we saw this macro last so that we add the tokens that 1310 // form its body to it. 1311 Macro = MI; 1312 1313 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1314 Record[NextIndex]) { 1315 // We have a macro definition. Register the association 1316 PreprocessedEntityID 1317 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1318 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1319 PPRec.RegisterMacroDefinition(Macro, 1320 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true)); 1321 } 1322 1323 ++NumMacrosRead; 1324 break; 1325 } 1326 1327 case PP_TOKEN: { 1328 // If we see a TOKEN before a PP_MACRO_*, then the file is 1329 // erroneous, just pretend we didn't see this. 1330 if (Macro == 0) break; 1331 1332 Token Tok; 1333 Tok.startToken(); 1334 Tok.setLocation(ReadSourceLocation(F, Record[0])); 1335 Tok.setLength(Record[1]); 1336 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2])) 1337 Tok.setIdentifierInfo(II); 1338 Tok.setKind((tok::TokenKind)Record[3]); 1339 Tok.setFlag((Token::TokenFlags)Record[4]); 1340 Macro->AddTokenToBody(Tok); 1341 break; 1342 } 1343 } 1344 } 1345 1346 return; 1347} 1348 1349PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) { 1350 unsigned Code = F.PreprocessorDetailCursor.ReadCode(); 1351 switch (Code) { 1352 case llvm::bitc::END_BLOCK: 1353 return 0; 1354 1355 case llvm::bitc::ENTER_SUBBLOCK: 1356 Error("unexpected subblock record in preprocessor detail block"); 1357 return 0; 1358 1359 case llvm::bitc::DEFINE_ABBREV: 1360 Error("unexpected abbrevation record in preprocessor detail block"); 1361 return 0; 1362 1363 default: 1364 break; 1365 } 1366 1367 if (!PP.getPreprocessingRecord()) { 1368 Error("no preprocessing record"); 1369 return 0; 1370 } 1371 1372 // Read the record. 1373 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1374 const char *BlobStart = 0; 1375 unsigned BlobLen = 0; 1376 RecordData Record; 1377 PreprocessorDetailRecordTypes RecType = 1378 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord( 1379 Code, Record, BlobStart, BlobLen); 1380 switch (RecType) { 1381 case PPD_MACRO_EXPANSION: { 1382 bool isBuiltin = Record[3]; 1383 MacroExpansion *ME; 1384 if (isBuiltin) { 1385 ME = new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[4]), 1386 SourceRange(ReadSourceLocation(F, Record[1]), 1387 ReadSourceLocation(F, Record[2]))); 1388 } else { 1389 PreprocessedEntityID 1390 GlobalID = getGlobalPreprocessedEntityID(F, Record[4]); 1391 ME = new (PPRec) MacroExpansion( 1392 cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)), 1393 SourceRange(ReadSourceLocation(F, Record[1]), 1394 ReadSourceLocation(F, Record[2]))); 1395 } 1396 return ME; 1397 } 1398 1399 case PPD_MACRO_DEFINITION: { 1400 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]); 1401 1402 // Decode the identifier info and then check again; if the macro is 1403 // still defined and associated with the identifier, 1404 IdentifierInfo *II = getLocalIdentifier(F, Record[3]); 1405 MacroDefinition *MD 1406 = new (PPRec) MacroDefinition(II, 1407 SourceRange( 1408 ReadSourceLocation(F, Record[1]), 1409 ReadSourceLocation(F, Record[2]))); 1410 1411 if (DeserializationListener) 1412 DeserializationListener->MacroDefinitionRead(GlobalID, MD); 1413 1414 return MD; 1415 } 1416 1417 case PPD_INCLUSION_DIRECTIVE: { 1418 const char *FullFileNameStart = BlobStart + Record[3]; 1419 const FileEntry *File 1420 = PP.getFileManager().getFile(StringRef(FullFileNameStart, 1421 BlobLen - Record[3])); 1422 1423 // FIXME: Stable encoding 1424 InclusionDirective::InclusionKind Kind 1425 = static_cast<InclusionDirective::InclusionKind>(Record[5]); 1426 InclusionDirective *ID 1427 = new (PPRec) InclusionDirective(PPRec, Kind, 1428 StringRef(BlobStart, Record[3]), 1429 Record[4], 1430 File, 1431 SourceRange(ReadSourceLocation(F, Record[1]), 1432 ReadSourceLocation(F, Record[2]))); 1433 return ID; 1434 } 1435 } 1436 1437 Error("invalid offset in preprocessor detail block"); 1438 return 0; 1439} 1440 1441PreprocessedEntityID 1442ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) const { 1443 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1444 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1445 assert(I != M.PreprocessedEntityRemap.end() 1446 && "Invalid index into preprocessed entity index remap"); 1447 1448 return LocalID + I->second; 1449} 1450 1451unsigned HeaderFileInfoTrait::ComputeHash(const char *path) { 1452 return llvm::HashString(llvm::sys::path::filename(path)); 1453} 1454 1455HeaderFileInfoTrait::internal_key_type 1456HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; } 1457 1458bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) { 1459 if (strcmp(a, b) == 0) 1460 return true; 1461 1462 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b)) 1463 return false; 1464 1465 // The file names match, but the path names don't. stat() the files to 1466 // see if they are the same. 1467 struct stat StatBufA, StatBufB; 1468 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB)) 1469 return false; 1470 1471 return StatBufA.st_ino == StatBufB.st_ino; 1472} 1473 1474std::pair<unsigned, unsigned> 1475HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1476 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1477 unsigned DataLen = (unsigned) *d++; 1478 return std::make_pair(KeyLen + 1, DataLen); 1479} 1480 1481HeaderFileInfoTrait::data_type 1482HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d, 1483 unsigned DataLen) { 1484 const unsigned char *End = d + DataLen; 1485 using namespace clang::io; 1486 HeaderFileInfo HFI; 1487 unsigned Flags = *d++; 1488 HFI.isImport = (Flags >> 5) & 0x01; 1489 HFI.isPragmaOnce = (Flags >> 4) & 0x01; 1490 HFI.DirInfo = (Flags >> 2) & 0x03; 1491 HFI.Resolved = (Flags >> 1) & 0x01; 1492 HFI.IndexHeaderMapHeader = Flags & 0x01; 1493 HFI.NumIncludes = ReadUnalignedLE16(d); 1494 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d)); 1495 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) { 1496 // The framework offset is 1 greater than the actual offset, 1497 // since 0 is used as an indicator for "no framework name". 1498 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1499 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1500 } 1501 1502 assert(End == d && "Wrong data length in HeaderFileInfo deserialization"); 1503 (void)End; 1504 1505 // This HeaderFileInfo was externally loaded. 1506 HFI.External = true; 1507 return HFI; 1508} 1509 1510void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F, 1511 uint64_t LocalOffset) { 1512 // Note that this identifier has a macro definition. 1513 II->setHasMacroDefinition(true); 1514 1515 // Adjust the offset to a global offset. 1516 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset; 1517} 1518 1519void ASTReader::ReadDefinedMacros() { 1520 for (ModuleReverseIterator I = ModuleMgr.rbegin(), 1521 E = ModuleMgr.rend(); I != E; ++I) { 1522 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor; 1523 1524 // If there was no preprocessor block, skip this file. 1525 if (!MacroCursor.getBitStreamReader()) 1526 continue; 1527 1528 llvm::BitstreamCursor Cursor = MacroCursor; 1529 Cursor.JumpToBit((*I)->MacroStartOffset); 1530 1531 RecordData Record; 1532 while (true) { 1533 unsigned Code = Cursor.ReadCode(); 1534 if (Code == llvm::bitc::END_BLOCK) 1535 break; 1536 1537 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1538 // No known subblocks, always skip them. 1539 Cursor.ReadSubBlockID(); 1540 if (Cursor.SkipBlock()) { 1541 Error("malformed block record in AST file"); 1542 return; 1543 } 1544 continue; 1545 } 1546 1547 if (Code == llvm::bitc::DEFINE_ABBREV) { 1548 Cursor.ReadAbbrevRecord(); 1549 continue; 1550 } 1551 1552 // Read a record. 1553 const char *BlobStart; 1554 unsigned BlobLen; 1555 Record.clear(); 1556 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1557 default: // Default behavior: ignore. 1558 break; 1559 1560 case PP_MACRO_OBJECT_LIKE: 1561 case PP_MACRO_FUNCTION_LIKE: 1562 getLocalIdentifier(**I, Record[0]); 1563 break; 1564 1565 case PP_TOKEN: 1566 // Ignore tokens. 1567 break; 1568 } 1569 } 1570 } 1571 1572 // Drain the unread macro-record offsets map. 1573 while (!UnreadMacroRecordOffsets.empty()) 1574 LoadMacroDefinition(UnreadMacroRecordOffsets.begin()); 1575} 1576 1577void ASTReader::LoadMacroDefinition( 1578 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) { 1579 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition"); 1580 uint64_t Offset = Pos->second; 1581 UnreadMacroRecordOffsets.erase(Pos); 1582 1583 RecordLocation Loc = getLocalBitOffset(Offset); 1584 ReadMacroRecord(*Loc.F, Loc.Offset); 1585} 1586 1587void ASTReader::LoadMacroDefinition(IdentifierInfo *II) { 1588 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos 1589 = UnreadMacroRecordOffsets.find(II); 1590 LoadMacroDefinition(Pos); 1591} 1592 1593const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) { 1594 std::string Filename = filenameStrRef; 1595 MaybeAddSystemRootToFilename(Filename); 1596 const FileEntry *File = FileMgr.getFile(Filename); 1597 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() && 1598 OriginalDir != CurrentDir) { 1599 std::string resolved = resolveFileRelativeToOriginalDir(Filename, 1600 OriginalDir, 1601 CurrentDir); 1602 if (!resolved.empty()) 1603 File = FileMgr.getFile(resolved); 1604 } 1605 1606 return File; 1607} 1608 1609/// \brief If we are loading a relocatable PCH file, and the filename is 1610/// not an absolute path, add the system root to the beginning of the file 1611/// name. 1612void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) { 1613 // If this is not a relocatable PCH file, there's nothing to do. 1614 if (!RelocatablePCH) 1615 return; 1616 1617 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1618 return; 1619 1620 if (isysroot.empty()) { 1621 // If no system root was given, default to '/' 1622 Filename.insert(Filename.begin(), '/'); 1623 return; 1624 } 1625 1626 unsigned Length = isysroot.size(); 1627 if (isysroot[Length - 1] != '/') 1628 Filename.insert(Filename.begin(), '/'); 1629 1630 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end()); 1631} 1632 1633ASTReader::ASTReadResult 1634ASTReader::ReadASTBlock(Module &F) { 1635 llvm::BitstreamCursor &Stream = F.Stream; 1636 1637 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 1638 Error("malformed block record in AST file"); 1639 return Failure; 1640 } 1641 1642 // Read all of the records and blocks for the ASt file. 1643 RecordData Record; 1644 while (!Stream.AtEndOfStream()) { 1645 unsigned Code = Stream.ReadCode(); 1646 if (Code == llvm::bitc::END_BLOCK) { 1647 if (Stream.ReadBlockEnd()) { 1648 Error("error at end of module block in AST file"); 1649 return Failure; 1650 } 1651 1652 return Success; 1653 } 1654 1655 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1656 switch (Stream.ReadSubBlockID()) { 1657 case DECLTYPES_BLOCK_ID: 1658 // We lazily load the decls block, but we want to set up the 1659 // DeclsCursor cursor to point into it. Clone our current bitcode 1660 // cursor to it, enter the block and read the abbrevs in that block. 1661 // With the main cursor, we just skip over it. 1662 F.DeclsCursor = Stream; 1663 if (Stream.SkipBlock() || // Skip with the main cursor. 1664 // Read the abbrevs. 1665 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 1666 Error("malformed block record in AST file"); 1667 return Failure; 1668 } 1669 break; 1670 1671 case DECL_UPDATES_BLOCK_ID: 1672 if (Stream.SkipBlock()) { 1673 Error("malformed block record in AST file"); 1674 return Failure; 1675 } 1676 break; 1677 1678 case PREPROCESSOR_BLOCK_ID: 1679 F.MacroCursor = Stream; 1680 if (!PP.getExternalSource()) 1681 PP.setExternalSource(this); 1682 1683 if (Stream.SkipBlock() || 1684 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 1685 Error("malformed block record in AST file"); 1686 return Failure; 1687 } 1688 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 1689 break; 1690 1691 case PREPROCESSOR_DETAIL_BLOCK_ID: 1692 F.PreprocessorDetailCursor = Stream; 1693 if (Stream.SkipBlock() || 1694 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 1695 PREPROCESSOR_DETAIL_BLOCK_ID)) { 1696 Error("malformed preprocessor detail record in AST file"); 1697 return Failure; 1698 } 1699 F.PreprocessorDetailStartOffset 1700 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 1701 1702 if (!PP.getPreprocessingRecord()) 1703 PP.createPreprocessingRecord(true); 1704 if (!PP.getPreprocessingRecord()->getExternalSource()) 1705 PP.getPreprocessingRecord()->SetExternalSource(*this); 1706 break; 1707 1708 case SOURCE_MANAGER_BLOCK_ID: 1709 switch (ReadSourceManagerBlock(F)) { 1710 case Success: 1711 break; 1712 1713 case Failure: 1714 Error("malformed source manager block in AST file"); 1715 return Failure; 1716 1717 case IgnorePCH: 1718 return IgnorePCH; 1719 } 1720 break; 1721 } 1722 continue; 1723 } 1724 1725 if (Code == llvm::bitc::DEFINE_ABBREV) { 1726 Stream.ReadAbbrevRecord(); 1727 continue; 1728 } 1729 1730 // Read and process a record. 1731 Record.clear(); 1732 const char *BlobStart = 0; 1733 unsigned BlobLen = 0; 1734 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record, 1735 &BlobStart, &BlobLen)) { 1736 default: // Default behavior: ignore. 1737 break; 1738 1739 case METADATA: { 1740 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1741 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1742 : diag::warn_pch_version_too_new); 1743 return IgnorePCH; 1744 } 1745 1746 RelocatablePCH = Record[4]; 1747 if (Listener) { 1748 std::string TargetTriple(BlobStart, BlobLen); 1749 if (Listener->ReadTargetTriple(TargetTriple)) 1750 return IgnorePCH; 1751 } 1752 break; 1753 } 1754 1755 case IMPORTS: { 1756 // Load each of the imported PCH files. 1757 unsigned Idx = 0, N = Record.size(); 1758 while (Idx < N) { 1759 // Read information about the AST file. 1760 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 1761 unsigned Length = Record[Idx++]; 1762 llvm::SmallString<128> ImportedFile(Record.begin() + Idx, 1763 Record.begin() + Idx + Length); 1764 Idx += Length; 1765 1766 // Load the AST file. 1767 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) { 1768 case Failure: return Failure; 1769 // If we have to ignore the dependency, we'll have to ignore this too. 1770 case IgnorePCH: return IgnorePCH; 1771 case Success: break; 1772 } 1773 } 1774 break; 1775 } 1776 1777 case TYPE_OFFSET: { 1778 if (F.LocalNumTypes != 0) { 1779 Error("duplicate TYPE_OFFSET record in AST file"); 1780 return Failure; 1781 } 1782 F.TypeOffsets = (const uint32_t *)BlobStart; 1783 F.LocalNumTypes = Record[0]; 1784 unsigned LocalBaseTypeIndex = Record[1]; 1785 F.BaseTypeIndex = getTotalNumTypes(); 1786 1787 if (F.LocalNumTypes > 0) { 1788 // Introduce the global -> local mapping for types within this module. 1789 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 1790 1791 // Introduce the local -> global mapping for types within this module. 1792 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex, 1793 F.BaseTypeIndex - LocalBaseTypeIndex)); 1794 1795 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 1796 } 1797 break; 1798 } 1799 1800 case DECL_OFFSET: { 1801 if (F.LocalNumDecls != 0) { 1802 Error("duplicate DECL_OFFSET record in AST file"); 1803 return Failure; 1804 } 1805 F.DeclOffsets = (const uint32_t *)BlobStart; 1806 F.LocalNumDecls = Record[0]; 1807 unsigned LocalBaseDeclID = Record[1]; 1808 F.BaseDeclID = getTotalNumDecls(); 1809 1810 if (F.LocalNumDecls > 0) { 1811 // Introduce the global -> local mapping for declarations within this 1812 // module. 1813 GlobalDeclMap.insert( 1814 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 1815 1816 // Introduce the local -> global mapping for declarations within this 1817 // module. 1818 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID, 1819 F.BaseDeclID - LocalBaseDeclID)); 1820 1821 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 1822 } 1823 break; 1824 } 1825 1826 case TU_UPDATE_LEXICAL: { 1827 DeclContext *TU = Context.getTranslationUnitDecl(); 1828 DeclContextInfo &Info = F.DeclContextInfos[TU]; 1829 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart); 1830 Info.NumLexicalDecls 1831 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair)); 1832 TU->setHasExternalLexicalStorage(true); 1833 break; 1834 } 1835 1836 case UPDATE_VISIBLE: { 1837 unsigned Idx = 0; 1838 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 1839 void *Table = ASTDeclContextNameLookupTable::Create( 1840 (const unsigned char *)BlobStart + Record[Idx++], 1841 (const unsigned char *)BlobStart, 1842 ASTDeclContextNameLookupTrait(*this, F)); 1843 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU? 1844 DeclContext *TU = Context.getTranslationUnitDecl(); 1845 F.DeclContextInfos[TU].NameLookupTableData = Table; 1846 TU->setHasExternalVisibleStorage(true); 1847 } else 1848 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F)); 1849 break; 1850 } 1851 1852 case REDECLS_UPDATE_LATEST: { 1853 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs"); 1854 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) { 1855 DeclID First = ReadDeclID(F, Record, i); 1856 DeclID Latest = ReadDeclID(F, Record, i); 1857 FirstLatestDeclIDs[First] = Latest; 1858 } 1859 break; 1860 } 1861 1862 case LANGUAGE_OPTIONS: 1863 if (ParseLanguageOptions(Record) && !DisableValidation) 1864 return IgnorePCH; 1865 break; 1866 1867 case IDENTIFIER_TABLE: 1868 F.IdentifierTableData = BlobStart; 1869 if (Record[0]) { 1870 F.IdentifierLookupTable 1871 = ASTIdentifierLookupTable::Create( 1872 (const unsigned char *)F.IdentifierTableData + Record[0], 1873 (const unsigned char *)F.IdentifierTableData, 1874 ASTIdentifierLookupTrait(*this, F)); 1875 1876 PP.getIdentifierTable().setExternalIdentifierLookup(this); 1877 } 1878 break; 1879 1880 case IDENTIFIER_OFFSET: { 1881 if (F.LocalNumIdentifiers != 0) { 1882 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 1883 return Failure; 1884 } 1885 F.IdentifierOffsets = (const uint32_t *)BlobStart; 1886 F.LocalNumIdentifiers = Record[0]; 1887 unsigned LocalBaseIdentifierID = Record[1]; 1888 F.BaseIdentifierID = getTotalNumIdentifiers(); 1889 1890 if (F.LocalNumIdentifiers > 0) { 1891 // Introduce the global -> local mapping for identifiers within this 1892 // module. 1893 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 1894 &F)); 1895 1896 // Introduce the local -> global mapping for identifiers within this 1897 // module. 1898 F.IdentifierRemap.insert( 1899 std::make_pair(LocalBaseIdentifierID, 1900 F.BaseIdentifierID - LocalBaseIdentifierID)); 1901 1902 IdentifiersLoaded.resize(IdentifiersLoaded.size() 1903 + F.LocalNumIdentifiers); 1904 } 1905 break; 1906 } 1907 1908 case EXTERNAL_DEFINITIONS: 1909 for (unsigned I = 0, N = Record.size(); I != N; ++I) 1910 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I])); 1911 break; 1912 1913 case SPECIAL_TYPES: 1914 for (unsigned I = 0, N = Record.size(); I != N; ++I) 1915 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 1916 break; 1917 1918 case STATISTICS: 1919 TotalNumStatements += Record[0]; 1920 TotalNumMacros += Record[1]; 1921 TotalLexicalDeclContexts += Record[2]; 1922 TotalVisibleDeclContexts += Record[3]; 1923 break; 1924 1925 case UNUSED_FILESCOPED_DECLS: 1926 for (unsigned I = 0, N = Record.size(); I != N; ++I) 1927 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 1928 break; 1929 1930 case DELEGATING_CTORS: 1931 for (unsigned I = 0, N = Record.size(); I != N; ++I) 1932 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 1933 break; 1934 1935 case WEAK_UNDECLARED_IDENTIFIERS: 1936 if (Record.size() % 4 != 0) { 1937 Error("invalid weak identifiers record"); 1938 return Failure; 1939 } 1940 1941 // FIXME: Ignore weak undeclared identifiers from non-original PCH 1942 // files. This isn't the way to do it :) 1943 WeakUndeclaredIdentifiers.clear(); 1944 1945 // Translate the weak, undeclared identifiers into global IDs. 1946 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 1947 WeakUndeclaredIdentifiers.push_back( 1948 getGlobalIdentifierID(F, Record[I++])); 1949 WeakUndeclaredIdentifiers.push_back( 1950 getGlobalIdentifierID(F, Record[I++])); 1951 WeakUndeclaredIdentifiers.push_back( 1952 ReadSourceLocation(F, Record, I).getRawEncoding()); 1953 WeakUndeclaredIdentifiers.push_back(Record[I++]); 1954 } 1955 break; 1956 1957 case LOCALLY_SCOPED_EXTERNAL_DECLS: 1958 for (unsigned I = 0, N = Record.size(); I != N; ++I) 1959 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I])); 1960 break; 1961 1962 case SELECTOR_OFFSETS: { 1963 F.SelectorOffsets = (const uint32_t *)BlobStart; 1964 F.LocalNumSelectors = Record[0]; 1965 unsigned LocalBaseSelectorID = Record[1]; 1966 F.BaseSelectorID = getTotalNumSelectors(); 1967 1968 if (F.LocalNumSelectors > 0) { 1969 // Introduce the global -> local mapping for selectors within this 1970 // module. 1971 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 1972 1973 // Introduce the local -> global mapping for selectors within this 1974 // module. 1975 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID, 1976 F.BaseSelectorID - LocalBaseSelectorID)); 1977 1978 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 1979 } 1980 break; 1981 } 1982 1983 case METHOD_POOL: 1984 F.SelectorLookupTableData = (const unsigned char *)BlobStart; 1985 if (Record[0]) 1986 F.SelectorLookupTable 1987 = ASTSelectorLookupTable::Create( 1988 F.SelectorLookupTableData + Record[0], 1989 F.SelectorLookupTableData, 1990 ASTSelectorLookupTrait(*this, F)); 1991 TotalNumMethodPoolEntries += Record[1]; 1992 break; 1993 1994 case REFERENCED_SELECTOR_POOL: 1995 if (!Record.empty()) { 1996 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 1997 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 1998 Record[Idx++])); 1999 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2000 getRawEncoding()); 2001 } 2002 } 2003 break; 2004 2005 case PP_COUNTER_VALUE: 2006 if (!Record.empty() && Listener) 2007 Listener->ReadCounter(Record[0]); 2008 break; 2009 2010 case SOURCE_LOCATION_OFFSETS: { 2011 F.SLocEntryOffsets = (const uint32_t *)BlobStart; 2012 F.LocalNumSLocEntries = Record[0]; 2013 unsigned SLocSpaceSize = Record[1]; 2014 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2015 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2016 SLocSpaceSize); 2017 // Make our entry in the range map. BaseID is negative and growing, so 2018 // we invert it. Because we invert it, though, we need the other end of 2019 // the range. 2020 unsigned RangeStart = 2021 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2022 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2023 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2024 2025 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2026 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2027 GlobalSLocOffsetMap.insert( 2028 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2029 - SLocSpaceSize,&F)); 2030 2031 // Initialize the remapping table. 2032 // Invalid stays invalid. 2033 F.SLocRemap.insert(std::make_pair(0U, 0)); 2034 // This module. Base was 2 when being compiled. 2035 F.SLocRemap.insert(std::make_pair(2U, 2036 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2037 2038 TotalNumSLocEntries += F.LocalNumSLocEntries; 2039 break; 2040 } 2041 2042 case MODULE_OFFSET_MAP: { 2043 // Additional remapping information. 2044 const unsigned char *Data = (const unsigned char*)BlobStart; 2045 const unsigned char *DataEnd = Data + BlobLen; 2046 2047 // Continuous range maps we may be updating in our module. 2048 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap); 2049 ContinuousRangeMap<uint32_t, int, 2>::Builder 2050 IdentifierRemap(F.IdentifierRemap); 2051 ContinuousRangeMap<uint32_t, int, 2>::Builder 2052 PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2053 ContinuousRangeMap<uint32_t, int, 2>::Builder 2054 SelectorRemap(F.SelectorRemap); 2055 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap); 2056 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap); 2057 2058 while(Data < DataEnd) { 2059 uint16_t Len = io::ReadUnalignedLE16(Data); 2060 StringRef Name = StringRef((const char*)Data, Len); 2061 Data += Len; 2062 Module *OM = ModuleMgr.lookup(Name); 2063 if (!OM) { 2064 Error("SourceLocation remap refers to unknown module"); 2065 return Failure; 2066 } 2067 2068 uint32_t SLocOffset = io::ReadUnalignedLE32(Data); 2069 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data); 2070 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data); 2071 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data); 2072 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data); 2073 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data); 2074 2075 // Source location offset is mapped to OM->SLocEntryBaseOffset. 2076 SLocRemap.insert(std::make_pair(SLocOffset, 2077 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset))); 2078 IdentifierRemap.insert( 2079 std::make_pair(IdentifierIDOffset, 2080 OM->BaseIdentifierID - IdentifierIDOffset)); 2081 PreprocessedEntityRemap.insert( 2082 std::make_pair(PreprocessedEntityIDOffset, 2083 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset)); 2084 SelectorRemap.insert(std::make_pair(SelectorIDOffset, 2085 OM->BaseSelectorID - SelectorIDOffset)); 2086 DeclRemap.insert(std::make_pair(DeclIDOffset, 2087 OM->BaseDeclID - DeclIDOffset)); 2088 2089 TypeRemap.insert(std::make_pair(TypeIndexOffset, 2090 OM->BaseTypeIndex - TypeIndexOffset)); 2091 } 2092 break; 2093 } 2094 2095 case SOURCE_MANAGER_LINE_TABLE: 2096 if (ParseLineTable(F, Record)) 2097 return Failure; 2098 break; 2099 2100 case FILE_SOURCE_LOCATION_OFFSETS: 2101 F.SLocFileOffsets = (const uint32_t *)BlobStart; 2102 F.LocalNumSLocFileEntries = Record[0]; 2103 break; 2104 2105 case SOURCE_LOCATION_PRELOADS: { 2106 // Need to transform from the local view (1-based IDs) to the global view, 2107 // which is based off F.SLocEntryBaseID. 2108 if (!F.PreloadSLocEntries.empty()) { 2109 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2110 return Failure; 2111 } 2112 2113 F.PreloadSLocEntries.swap(Record); 2114 break; 2115 } 2116 2117 case STAT_CACHE: { 2118 if (!DisableStatCache) { 2119 ASTStatCache *MyStatCache = 2120 new ASTStatCache((const unsigned char *)BlobStart + Record[0], 2121 (const unsigned char *)BlobStart, 2122 NumStatHits, NumStatMisses); 2123 FileMgr.addStatCache(MyStatCache); 2124 F.StatCache = MyStatCache; 2125 } 2126 break; 2127 } 2128 2129 case EXT_VECTOR_DECLS: 2130 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2131 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2132 break; 2133 2134 case VTABLE_USES: 2135 if (Record.size() % 3 != 0) { 2136 Error("Invalid VTABLE_USES record"); 2137 return Failure; 2138 } 2139 2140 // Later tables overwrite earlier ones. 2141 // FIXME: Modules will have some trouble with this. This is clearly not 2142 // the right way to do this. 2143 VTableUses.clear(); 2144 2145 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2146 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2147 VTableUses.push_back( 2148 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2149 VTableUses.push_back(Record[Idx++]); 2150 } 2151 break; 2152 2153 case DYNAMIC_CLASSES: 2154 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2155 DynamicClasses.push_back(getGlobalDeclID(F, Record[I])); 2156 break; 2157 2158 case PENDING_IMPLICIT_INSTANTIATIONS: 2159 if (PendingInstantiations.size() % 2 != 0) { 2160 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2161 return Failure; 2162 } 2163 2164 // Later lists of pending instantiations overwrite earlier ones. 2165 // FIXME: This is most certainly wrong for modules. 2166 PendingInstantiations.clear(); 2167 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2168 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2169 PendingInstantiations.push_back( 2170 ReadSourceLocation(F, Record, I).getRawEncoding()); 2171 } 2172 break; 2173 2174 case SEMA_DECL_REFS: 2175 // Later tables overwrite earlier ones. 2176 // FIXME: Modules will have some trouble with this. 2177 SemaDeclRefs.clear(); 2178 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2179 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2180 break; 2181 2182 case ORIGINAL_FILE_NAME: 2183 // The primary AST will be the last to get here, so it will be the one 2184 // that's used. 2185 ActualOriginalFileName.assign(BlobStart, BlobLen); 2186 OriginalFileName = ActualOriginalFileName; 2187 MaybeAddSystemRootToFilename(OriginalFileName); 2188 break; 2189 2190 case ORIGINAL_FILE_ID: 2191 OriginalFileID = FileID::get(Record[0]); 2192 break; 2193 2194 case ORIGINAL_PCH_DIR: 2195 // The primary AST will be the last to get here, so it will be the one 2196 // that's used. 2197 OriginalDir.assign(BlobStart, BlobLen); 2198 break; 2199 2200 case VERSION_CONTROL_BRANCH_REVISION: { 2201 const std::string &CurBranch = getClangFullRepositoryVersion(); 2202 StringRef ASTBranch(BlobStart, BlobLen); 2203 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2204 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 2205 return IgnorePCH; 2206 } 2207 break; 2208 } 2209 2210 case PPD_ENTITIES_OFFSETS: { 2211 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart; 2212 assert(BlobLen % sizeof(PPEntityOffset) == 0); 2213 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset); 2214 2215 unsigned LocalBasePreprocessedEntityID = Record[0]; 2216 2217 unsigned StartingID; 2218 if (!PP.getPreprocessingRecord()) 2219 PP.createPreprocessingRecord(true); 2220 if (!PP.getPreprocessingRecord()->getExternalSource()) 2221 PP.getPreprocessingRecord()->SetExternalSource(*this); 2222 StartingID 2223 = PP.getPreprocessingRecord() 2224 ->allocateLoadedEntities(F.NumPreprocessedEntities); 2225 F.BasePreprocessedEntityID = StartingID; 2226 2227 if (F.NumPreprocessedEntities > 0) { 2228 // Introduce the global -> local mapping for preprocessed entities in 2229 // this module. 2230 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 2231 2232 // Introduce the local -> global mapping for preprocessed entities in 2233 // this module. 2234 F.PreprocessedEntityRemap.insert( 2235 std::make_pair(LocalBasePreprocessedEntityID, 2236 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 2237 } 2238 2239 break; 2240 } 2241 2242 case DECL_UPDATE_OFFSETS: { 2243 if (Record.size() % 2 != 0) { 2244 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2245 return Failure; 2246 } 2247 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2248 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])] 2249 .push_back(std::make_pair(&F, Record[I+1])); 2250 break; 2251 } 2252 2253 case DECL_REPLACEMENTS: { 2254 if (Record.size() % 2 != 0) { 2255 Error("invalid DECL_REPLACEMENTS block in AST file"); 2256 return Failure; 2257 } 2258 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2259 ReplacedDecls[getGlobalDeclID(F, Record[I])] 2260 = std::make_pair(&F, Record[I+1]); 2261 break; 2262 } 2263 2264 case OBJC_CHAINED_CATEGORIES: { 2265 if (Record.size() % 3 != 0) { 2266 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file"); 2267 return Failure; 2268 } 2269 for (unsigned I = 0, N = Record.size(); I != N; I += 3) { 2270 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]); 2271 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1], 2272 Record[I+2]); 2273 ObjCChainedCategoriesInterfaces.insert(GlobID); 2274 } 2275 break; 2276 } 2277 2278 case CXX_BASE_SPECIFIER_OFFSETS: { 2279 if (F.LocalNumCXXBaseSpecifiers != 0) { 2280 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2281 return Failure; 2282 } 2283 2284 F.LocalNumCXXBaseSpecifiers = Record[0]; 2285 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart; 2286 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers; 2287 break; 2288 } 2289 2290 case DIAG_PRAGMA_MAPPINGS: 2291 if (Record.size() % 2 != 0) { 2292 Error("invalid DIAG_USER_MAPPINGS block in AST file"); 2293 return Failure; 2294 } 2295 2296 if (F.PragmaDiagMappings.empty()) 2297 F.PragmaDiagMappings.swap(Record); 2298 else 2299 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 2300 Record.begin(), Record.end()); 2301 break; 2302 2303 case CUDA_SPECIAL_DECL_REFS: 2304 // Later tables overwrite earlier ones. 2305 // FIXME: Modules will have trouble with this. 2306 CUDASpecialDeclRefs.clear(); 2307 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2308 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2309 break; 2310 2311 case HEADER_SEARCH_TABLE: { 2312 F.HeaderFileInfoTableData = BlobStart; 2313 F.LocalNumHeaderFileInfos = Record[1]; 2314 F.HeaderFileFrameworkStrings = BlobStart + Record[2]; 2315 if (Record[0]) { 2316 F.HeaderFileInfoTable 2317 = HeaderFileInfoLookupTable::Create( 2318 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 2319 (const unsigned char *)F.HeaderFileInfoTableData, 2320 HeaderFileInfoTrait(*this, F, 2321 &PP.getHeaderSearchInfo(), 2322 BlobStart + Record[2])); 2323 2324 PP.getHeaderSearchInfo().SetExternalSource(this); 2325 if (!PP.getHeaderSearchInfo().getExternalLookup()) 2326 PP.getHeaderSearchInfo().SetExternalLookup(this); 2327 } 2328 break; 2329 } 2330 2331 case FP_PRAGMA_OPTIONS: 2332 // Later tables overwrite earlier ones. 2333 FPPragmaOptions.swap(Record); 2334 break; 2335 2336 case OPENCL_EXTENSIONS: 2337 // Later tables overwrite earlier ones. 2338 OpenCLExtensions.swap(Record); 2339 break; 2340 2341 case TENTATIVE_DEFINITIONS: 2342 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2343 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 2344 break; 2345 2346 case KNOWN_NAMESPACES: 2347 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2348 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 2349 break; 2350 } 2351 } 2352 Error("premature end of bitstream in AST file"); 2353 return Failure; 2354} 2355 2356ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) { 2357 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor; 2358 2359 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) { 2360 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]); 2361 unsigned Code = SLocEntryCursor.ReadCode(); 2362 if (Code == llvm::bitc::END_BLOCK || 2363 Code == llvm::bitc::ENTER_SUBBLOCK || 2364 Code == llvm::bitc::DEFINE_ABBREV) { 2365 Error("incorrectly-formatted source location entry in AST file"); 2366 return Failure; 2367 } 2368 2369 RecordData Record; 2370 const char *BlobStart; 2371 unsigned BlobLen; 2372 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 2373 default: 2374 Error("incorrectly-formatted source location entry in AST file"); 2375 return Failure; 2376 2377 case SM_SLOC_FILE_ENTRY: { 2378 StringRef Filename(BlobStart, BlobLen); 2379 const FileEntry *File = getFileEntry(Filename); 2380 2381 if (File == 0) { 2382 std::string ErrorStr = "could not find file '"; 2383 ErrorStr += Filename; 2384 ErrorStr += "' referenced by AST file"; 2385 Error(ErrorStr.c_str()); 2386 return IgnorePCH; 2387 } 2388 2389 if (Record.size() < 6) { 2390 Error("source location entry is incorrect"); 2391 return Failure; 2392 } 2393 2394 // The stat info from the FileEntry came from the cached stat 2395 // info of the PCH, so we cannot trust it. 2396 struct stat StatBuf; 2397 if (::stat(File->getName(), &StatBuf) != 0) { 2398 StatBuf.st_size = File->getSize(); 2399 StatBuf.st_mtime = File->getModificationTime(); 2400 } 2401 2402 if (((off_t)Record[4] != StatBuf.st_size 2403#if !defined(LLVM_ON_WIN32) 2404 // In our regression testing, the Windows file system seems to 2405 // have inconsistent modification times that sometimes 2406 // erroneously trigger this error-handling path. 2407 || (time_t)Record[5] != StatBuf.st_mtime 2408#endif 2409 )) { 2410 Error(diag::err_fe_pch_file_modified, Filename); 2411 return IgnorePCH; 2412 } 2413 2414 break; 2415 } 2416 } 2417 } 2418 2419 return Success; 2420} 2421 2422namespace { 2423 /// \brief Visitor class used to look up identifirs in an AST file. 2424 class IdentifierLookupVisitor { 2425 StringRef Name; 2426 IdentifierInfo *Found; 2427 public: 2428 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { } 2429 2430 static bool visit(Module &M, void *UserData) { 2431 IdentifierLookupVisitor *This 2432 = static_cast<IdentifierLookupVisitor *>(UserData); 2433 2434 ASTIdentifierLookupTable *IdTable 2435 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2436 if (!IdTable) 2437 return false; 2438 2439 std::pair<const char*, unsigned> Key(This->Name.begin(), 2440 This->Name.size()); 2441 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key); 2442 if (Pos == IdTable->end()) 2443 return false; 2444 2445 // Dereferencing the iterator has the effect of building the 2446 // IdentifierInfo node and populating it with the various 2447 // declarations it needs. 2448 This->Found = *Pos; 2449 return true; 2450 } 2451 2452 // \brief Retrieve the identifier info found within the module 2453 // files. 2454 IdentifierInfo *getIdentifierInfo() const { return Found; } 2455 }; 2456} 2457 2458 2459ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2460 ModuleKind Type) { 2461 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) { 2462 case Failure: return Failure; 2463 case IgnorePCH: return IgnorePCH; 2464 case Success: break; 2465 } 2466 2467 // Here comes stuff that we only do once the entire chain is loaded. 2468 2469 // Check the predefines buffers. 2470 if (!DisableValidation && Type != MK_Module && Type != MK_Preamble && 2471 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines; 2472 // if DisableValidation is true, defines that were set on command-line 2473 // but not in the PCH file will not be added to SuggestedPredefines. 2474 CheckPredefinesBuffers()) 2475 return IgnorePCH; 2476 2477 // Initialization of keywords and pragmas occurs before the 2478 // AST file is read, so there may be some identifiers that were 2479 // loaded into the IdentifierTable before we intercepted the 2480 // creation of identifiers. Iterate through the list of known 2481 // identifiers and determine whether we have to establish 2482 // preprocessor definitions or top-level identifier declaration 2483 // chains for those identifiers. 2484 // 2485 // We copy the IdentifierInfo pointers to a small vector first, 2486 // since de-serializing declarations or macro definitions can add 2487 // new entries into the identifier table, invalidating the 2488 // iterators. 2489 // 2490 // FIXME: We need a lazier way to load this information, e.g., by marking 2491 // the identifier data as 'dirty', so that it will be looked up in the 2492 // AST file(s) if it is uttered in the source. This could save us some 2493 // module load time. 2494 SmallVector<IdentifierInfo *, 128> Identifiers; 2495 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 2496 IdEnd = PP.getIdentifierTable().end(); 2497 Id != IdEnd; ++Id) 2498 Identifiers.push_back(Id->second); 2499 2500 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { 2501 IdentifierLookupVisitor Visitor(Identifiers[I]->getName()); 2502 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); 2503 } 2504 2505 InitializeContext(); 2506 2507 if (DeserializationListener) 2508 DeserializationListener->ReaderInitialized(this); 2509 2510 // If this AST file is a precompiled preamble, then set the preamble file ID 2511 // of the source manager to the file source file from which the preamble was 2512 // built. 2513 if (Type == MK_Preamble) { 2514 if (!OriginalFileID.isInvalid()) { 2515 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID 2516 + OriginalFileID.getOpaqueValue() - 1); 2517 SourceMgr.setPreambleFileID(OriginalFileID); 2518 } 2519 } 2520 2521 return Success; 2522} 2523 2524ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName, 2525 ModuleKind Type, 2526 Module *ImportedBy) { 2527 Module *M; 2528 bool NewModule; 2529 std::string ErrorStr; 2530 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy, 2531 ErrorStr); 2532 2533 if (!M) { 2534 // We couldn't load the module. 2535 std::string Msg = "Unable to load module \"" + FileName.str() + "\": " 2536 + ErrorStr; 2537 Error(Msg); 2538 return Failure; 2539 } 2540 2541 if (!NewModule) { 2542 // We've already loaded this module. 2543 return Success; 2544 } 2545 2546 // FIXME: This seems rather a hack. Should CurrentDir be part of the 2547 // module? 2548 if (FileName != "-") { 2549 CurrentDir = llvm::sys::path::parent_path(FileName); 2550 if (CurrentDir.empty()) CurrentDir = "."; 2551 } 2552 2553 Module &F = *M; 2554 llvm::BitstreamCursor &Stream = F.Stream; 2555 Stream.init(F.StreamFile); 2556 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2557 2558 // Sniff for the signature. 2559 if (Stream.Read(8) != 'C' || 2560 Stream.Read(8) != 'P' || 2561 Stream.Read(8) != 'C' || 2562 Stream.Read(8) != 'H') { 2563 Diag(diag::err_not_a_pch_file) << FileName; 2564 return Failure; 2565 } 2566 2567 while (!Stream.AtEndOfStream()) { 2568 unsigned Code = Stream.ReadCode(); 2569 2570 if (Code != llvm::bitc::ENTER_SUBBLOCK) { 2571 Error("invalid record at top-level of AST file"); 2572 return Failure; 2573 } 2574 2575 unsigned BlockID = Stream.ReadSubBlockID(); 2576 2577 // We only know the AST subblock ID. 2578 switch (BlockID) { 2579 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2580 if (Stream.ReadBlockInfoBlock()) { 2581 Error("malformed BlockInfoBlock in AST file"); 2582 return Failure; 2583 } 2584 break; 2585 case AST_BLOCK_ID: 2586 switch (ReadASTBlock(F)) { 2587 case Success: 2588 break; 2589 2590 case Failure: 2591 return Failure; 2592 2593 case IgnorePCH: 2594 // FIXME: We could consider reading through to the end of this 2595 // AST block, skipping subblocks, to see if there are other 2596 // AST blocks elsewhere. 2597 2598 // FIXME: We can't clear loaded slocentries anymore. 2599 //SourceMgr.ClearPreallocatedSLocEntries(); 2600 2601 // Remove the stat cache. 2602 if (F.StatCache) 2603 FileMgr.removeStatCache((ASTStatCache*)F.StatCache); 2604 2605 return IgnorePCH; 2606 } 2607 break; 2608 default: 2609 if (Stream.SkipBlock()) { 2610 Error("malformed block record in AST file"); 2611 return Failure; 2612 } 2613 break; 2614 } 2615 } 2616 2617 // Once read, set the Module bit base offset and update the size in 2618 // bits of all files we've seen. 2619 F.GlobalBitOffset = TotalModulesSizeInBits; 2620 TotalModulesSizeInBits += F.SizeInBits; 2621 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 2622 2623 // Make sure that the files this module was built against are still available. 2624 if (!DisableValidation) { 2625 switch(validateFileEntries(*M)) { 2626 case Failure: return Failure; 2627 case IgnorePCH: return IgnorePCH; 2628 case Success: break; 2629 } 2630 } 2631 2632 // Preload SLocEntries. 2633 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) { 2634 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 2635 // Load it through the SourceManager and don't call ReadSLocEntryRecord() 2636 // directly because the entry may have already been loaded in which case 2637 // calling ReadSLocEntryRecord() directly would trigger an assertion in 2638 // SourceManager. 2639 SourceMgr.getLoadedSLocEntryByID(Index); 2640 } 2641 2642 2643 return Success; 2644} 2645 2646void ASTReader::InitializeContext() { 2647 // If there's a listener, notify them that we "read" the translation unit. 2648 if (DeserializationListener) 2649 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 2650 Context.getTranslationUnitDecl()); 2651 2652 // Make sure we load the declaration update records for the translation unit, 2653 // if there are any. 2654 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, 2655 Context.getTranslationUnitDecl()); 2656 2657 // FIXME: Find a better way to deal with collisions between these 2658 // built-in types. Right now, we just ignore the problem. 2659 2660 // Load the special types. 2661 if (SpecialTypes.size() > NumSpecialTypeIDs) { 2662 if (Context.getBuiltinVaListType().isNull()) { 2663 Context.setBuiltinVaListType( 2664 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST])); 2665 } 2666 2667 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) { 2668 if (Context.ObjCProtoType.isNull()) 2669 Context.ObjCProtoType = GetType(Proto); 2670 } 2671 2672 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 2673 if (!Context.CFConstantStringTypeDecl) 2674 Context.setCFConstantStringType(GetType(String)); 2675 } 2676 2677 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 2678 QualType FileType = GetType(File); 2679 if (FileType.isNull()) { 2680 Error("FILE type is NULL"); 2681 return; 2682 } 2683 2684 if (!Context.FILEDecl) { 2685 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 2686 Context.setFILEDecl(Typedef->getDecl()); 2687 else { 2688 const TagType *Tag = FileType->getAs<TagType>(); 2689 if (!Tag) { 2690 Error("Invalid FILE type in AST file"); 2691 return; 2692 } 2693 Context.setFILEDecl(Tag->getDecl()); 2694 } 2695 } 2696 } 2697 2698 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) { 2699 QualType Jmp_bufType = GetType(Jmp_buf); 2700 if (Jmp_bufType.isNull()) { 2701 Error("jmp_buf type is NULL"); 2702 return; 2703 } 2704 2705 if (!Context.jmp_bufDecl) { 2706 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 2707 Context.setjmp_bufDecl(Typedef->getDecl()); 2708 else { 2709 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 2710 if (!Tag) { 2711 Error("Invalid jmp_buf type in AST file"); 2712 return; 2713 } 2714 Context.setjmp_bufDecl(Tag->getDecl()); 2715 } 2716 } 2717 } 2718 2719 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) { 2720 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 2721 if (Sigjmp_bufType.isNull()) { 2722 Error("sigjmp_buf type is NULL"); 2723 return; 2724 } 2725 2726 if (!Context.sigjmp_bufDecl) { 2727 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 2728 Context.setsigjmp_bufDecl(Typedef->getDecl()); 2729 else { 2730 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 2731 assert(Tag && "Invalid sigjmp_buf type in AST file"); 2732 Context.setsigjmp_bufDecl(Tag->getDecl()); 2733 } 2734 } 2735 } 2736 2737 if (unsigned ObjCIdRedef 2738 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 2739 if (Context.ObjCIdRedefinitionType.isNull()) 2740 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 2741 } 2742 2743 if (unsigned ObjCClassRedef 2744 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 2745 if (Context.ObjCClassRedefinitionType.isNull()) 2746 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 2747 } 2748 2749 if (unsigned ObjCSelRedef 2750 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 2751 if (Context.ObjCSelRedefinitionType.isNull()) 2752 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 2753 } 2754 } 2755 2756 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 2757 2758 // If there were any CUDA special declarations, deserialize them. 2759 if (!CUDASpecialDeclRefs.empty()) { 2760 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 2761 Context.setcudaConfigureCallDecl( 2762 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 2763 } 2764} 2765 2766/// \brief Retrieve the name of the original source file name 2767/// directly from the AST file, without actually loading the AST 2768/// file. 2769std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 2770 FileManager &FileMgr, 2771 Diagnostic &Diags) { 2772 // Open the AST file. 2773 std::string ErrStr; 2774 llvm::OwningPtr<llvm::MemoryBuffer> Buffer; 2775 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 2776 if (!Buffer) { 2777 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; 2778 return std::string(); 2779 } 2780 2781 // Initialize the stream 2782 llvm::BitstreamReader StreamFile; 2783 llvm::BitstreamCursor Stream; 2784 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 2785 (const unsigned char *)Buffer->getBufferEnd()); 2786 Stream.init(StreamFile); 2787 2788 // Sniff for the signature. 2789 if (Stream.Read(8) != 'C' || 2790 Stream.Read(8) != 'P' || 2791 Stream.Read(8) != 'C' || 2792 Stream.Read(8) != 'H') { 2793 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 2794 return std::string(); 2795 } 2796 2797 RecordData Record; 2798 while (!Stream.AtEndOfStream()) { 2799 unsigned Code = Stream.ReadCode(); 2800 2801 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 2802 unsigned BlockID = Stream.ReadSubBlockID(); 2803 2804 // We only know the AST subblock ID. 2805 switch (BlockID) { 2806 case AST_BLOCK_ID: 2807 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2808 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2809 return std::string(); 2810 } 2811 break; 2812 2813 default: 2814 if (Stream.SkipBlock()) { 2815 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2816 return std::string(); 2817 } 2818 break; 2819 } 2820 continue; 2821 } 2822 2823 if (Code == llvm::bitc::END_BLOCK) { 2824 if (Stream.ReadBlockEnd()) { 2825 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 2826 return std::string(); 2827 } 2828 continue; 2829 } 2830 2831 if (Code == llvm::bitc::DEFINE_ABBREV) { 2832 Stream.ReadAbbrevRecord(); 2833 continue; 2834 } 2835 2836 Record.clear(); 2837 const char *BlobStart = 0; 2838 unsigned BlobLen = 0; 2839 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) 2840 == ORIGINAL_FILE_NAME) 2841 return std::string(BlobStart, BlobLen); 2842 } 2843 2844 return std::string(); 2845} 2846 2847/// \brief Parse the record that corresponds to a LangOptions data 2848/// structure. 2849/// 2850/// This routine parses the language options from the AST file and then gives 2851/// them to the AST listener if one is set. 2852/// 2853/// \returns true if the listener deems the file unacceptable, false otherwise. 2854bool ASTReader::ParseLanguageOptions( 2855 const SmallVectorImpl<uint64_t> &Record) { 2856 if (Listener) { 2857 LangOptions LangOpts; 2858 unsigned Idx = 0; 2859#define LANGOPT(Name, Bits, Default, Description) \ 2860 LangOpts.Name = Record[Idx++]; 2861#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 2862 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 2863#include "clang/Basic/LangOptions.def" 2864 2865 return Listener->ReadLanguageOptions(LangOpts); 2866 } 2867 2868 return false; 2869} 2870 2871PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 2872 GlobalPreprocessedEntityMapType::iterator 2873 I = GlobalPreprocessedEntityMap.find(Index); 2874 assert(I != GlobalPreprocessedEntityMap.end() && 2875 "Corrupted global preprocessed entity map"); 2876 Module &M = *I->second; 2877 unsigned LocalIndex = Index - M.BasePreprocessedEntityID; 2878 2879 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 2880 M.PreprocessorDetailCursor.JumpToBit( 2881 M.PreprocessedEntityOffsets[LocalIndex].BitOffset); 2882 return LoadPreprocessedEntity(M); 2883} 2884 2885/// \brief \arg SLocMapI points at a chunk of a module that contains no 2886/// preprocessed entities or the entities it contains are not the ones we are 2887/// looking for. Find the next module that contains entities and return the ID 2888/// of the first entry. 2889PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 2890 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 2891 ++SLocMapI; 2892 for (GlobalSLocOffsetMapType::const_iterator 2893 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 2894 Module &M = *SLocMapI->second; 2895 if (M.NumPreprocessedEntities) 2896 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID); 2897 } 2898 2899 return getTotalNumPreprocessedEntities(); 2900} 2901 2902namespace { 2903 2904template <unsigned PPEntityOffset::*PPLoc> 2905struct PPEntityComp { 2906 const ASTReader &Reader; 2907 Module &M; 2908 2909 PPEntityComp(const ASTReader &Reader, Module &M) : Reader(Reader), M(M) { } 2910 2911 bool operator()(const PPEntityOffset &L, SourceLocation RHS) { 2912 SourceLocation LHS = getLoc(L); 2913 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 2914 } 2915 2916 bool operator()(SourceLocation LHS, const PPEntityOffset &R) { 2917 SourceLocation RHS = getLoc(R); 2918 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 2919 } 2920 2921 SourceLocation getLoc(const PPEntityOffset &PPE) const { 2922 return Reader.ReadSourceLocation(M, PPE.*PPLoc); 2923 } 2924}; 2925 2926} 2927 2928/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc. 2929PreprocessedEntityID 2930ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const { 2931 if (SourceMgr.isLocalSourceLocation(BLoc)) 2932 return getTotalNumPreprocessedEntities(); 2933 2934 GlobalSLocOffsetMapType::const_iterator 2935 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 2936 BLoc.getOffset()); 2937 assert(SLocMapI != GlobalSLocOffsetMap.end() && 2938 "Corrupted global sloc offset map"); 2939 2940 if (SLocMapI->second->NumPreprocessedEntities == 0) 2941 return findNextPreprocessedEntity(SLocMapI); 2942 2943 Module &M = *SLocMapI->second; 2944 typedef const PPEntityOffset *pp_iterator; 2945 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 2946 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 2947 pp_iterator PPI = 2948 std::lower_bound(pp_begin, pp_end, BLoc, 2949 PPEntityComp<&PPEntityOffset::End>(*this, M)); 2950 2951 if (PPI == pp_end) 2952 return findNextPreprocessedEntity(SLocMapI); 2953 2954 return getGlobalPreprocessedEntityID(M, 2955 M.BasePreprocessedEntityID + (PPI - pp_begin)); 2956} 2957 2958/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc. 2959PreprocessedEntityID 2960ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const { 2961 if (SourceMgr.isLocalSourceLocation(ELoc)) 2962 return getTotalNumPreprocessedEntities(); 2963 2964 GlobalSLocOffsetMapType::const_iterator 2965 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - 2966 ELoc.getOffset()); 2967 assert(SLocMapI != GlobalSLocOffsetMap.end() && 2968 "Corrupted global sloc offset map"); 2969 2970 if (SLocMapI->second->NumPreprocessedEntities == 0) 2971 return findNextPreprocessedEntity(SLocMapI); 2972 2973 Module &M = *SLocMapI->second; 2974 typedef const PPEntityOffset *pp_iterator; 2975 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 2976 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 2977 pp_iterator PPI = 2978 std::upper_bound(pp_begin, pp_end, ELoc, 2979 PPEntityComp<&PPEntityOffset::Begin>(*this, M)); 2980 2981 if (PPI == pp_end) 2982 return findNextPreprocessedEntity(SLocMapI); 2983 2984 return getGlobalPreprocessedEntityID(M, 2985 M.BasePreprocessedEntityID + (PPI - pp_begin)); 2986} 2987 2988/// \brief Returns a pair of [Begin, End) indices of preallocated 2989/// preprocessed entities that \arg Range encompasses. 2990std::pair<unsigned, unsigned> 2991 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 2992 if (Range.isInvalid()) 2993 return std::make_pair(0,0); 2994 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 2995 2996 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin()); 2997 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd()); 2998 return std::make_pair(BeginID, EndID); 2999} 3000 3001namespace { 3002 /// \brief Visitor used to search for information about a header file. 3003 class HeaderFileInfoVisitor { 3004 ASTReader &Reader; 3005 const FileEntry *FE; 3006 3007 llvm::Optional<HeaderFileInfo> HFI; 3008 3009 public: 3010 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE) 3011 : Reader(Reader), FE(FE) { } 3012 3013 static bool visit(Module &M, void *UserData) { 3014 HeaderFileInfoVisitor *This 3015 = static_cast<HeaderFileInfoVisitor *>(UserData); 3016 3017 HeaderFileInfoTrait Trait(This->Reader, M, 3018 &This->Reader.getPreprocessor().getHeaderSearchInfo(), 3019 M.HeaderFileFrameworkStrings, 3020 This->FE->getName()); 3021 3022 HeaderFileInfoLookupTable *Table 3023 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 3024 if (!Table) 3025 return false; 3026 3027 // Look in the on-disk hash table for an entry for this file name. 3028 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(), 3029 &Trait); 3030 if (Pos == Table->end()) 3031 return false; 3032 3033 This->HFI = *Pos; 3034 return true; 3035 } 3036 3037 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 3038 }; 3039} 3040 3041HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 3042 HeaderFileInfoVisitor Visitor(*this, FE); 3043 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); 3044 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) { 3045 if (Listener) 3046 Listener->ReadHeaderFileInfo(*HFI, FE->getUID()); 3047 return *HFI; 3048 } 3049 3050 return HeaderFileInfo(); 3051} 3052 3053void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) { 3054 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 3055 Module &F = *(*I); 3056 unsigned Idx = 0; 3057 while (Idx < F.PragmaDiagMappings.size()) { 3058 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 3059 while (1) { 3060 assert(Idx < F.PragmaDiagMappings.size() && 3061 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 3062 if (Idx >= F.PragmaDiagMappings.size()) { 3063 break; // Something is messed up but at least avoid infinite loop in 3064 // release build. 3065 } 3066 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 3067 if (DiagID == (unsigned)-1) { 3068 break; // no more diag/map pairs for this location. 3069 } 3070 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++]; 3071 Diag.setDiagnosticMapping(DiagID, Map, Loc); 3072 } 3073 } 3074 } 3075} 3076 3077/// \brief Get the correct cursor and offset for loading a type. 3078ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 3079 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 3080 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 3081 Module *M = I->second; 3082 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 3083} 3084 3085/// \brief Read and return the type with the given index.. 3086/// 3087/// The index is the type ID, shifted and minus the number of predefs. This 3088/// routine actually reads the record corresponding to the type at the given 3089/// location. It is a helper routine for GetType, which deals with reading type 3090/// IDs. 3091QualType ASTReader::readTypeRecord(unsigned Index) { 3092 RecordLocation Loc = TypeCursorForIndex(Index); 3093 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 3094 3095 // Keep track of where we are in the stream, then jump back there 3096 // after reading this type. 3097 SavedStreamPosition SavedPosition(DeclsCursor); 3098 3099 ReadingKindTracker ReadingKind(Read_Type, *this); 3100 3101 // Note that we are loading a type record. 3102 Deserializing AType(this); 3103 3104 unsigned Idx = 0; 3105 DeclsCursor.JumpToBit(Loc.Offset); 3106 RecordData Record; 3107 unsigned Code = DeclsCursor.ReadCode(); 3108 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) { 3109 case TYPE_EXT_QUAL: { 3110 if (Record.size() != 2) { 3111 Error("Incorrect encoding of extended qualifier type"); 3112 return QualType(); 3113 } 3114 QualType Base = readType(*Loc.F, Record, Idx); 3115 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 3116 return Context.getQualifiedType(Base, Quals); 3117 } 3118 3119 case TYPE_COMPLEX: { 3120 if (Record.size() != 1) { 3121 Error("Incorrect encoding of complex type"); 3122 return QualType(); 3123 } 3124 QualType ElemType = readType(*Loc.F, Record, Idx); 3125 return Context.getComplexType(ElemType); 3126 } 3127 3128 case TYPE_POINTER: { 3129 if (Record.size() != 1) { 3130 Error("Incorrect encoding of pointer type"); 3131 return QualType(); 3132 } 3133 QualType PointeeType = readType(*Loc.F, Record, Idx); 3134 return Context.getPointerType(PointeeType); 3135 } 3136 3137 case TYPE_BLOCK_POINTER: { 3138 if (Record.size() != 1) { 3139 Error("Incorrect encoding of block pointer type"); 3140 return QualType(); 3141 } 3142 QualType PointeeType = readType(*Loc.F, Record, Idx); 3143 return Context.getBlockPointerType(PointeeType); 3144 } 3145 3146 case TYPE_LVALUE_REFERENCE: { 3147 if (Record.size() != 2) { 3148 Error("Incorrect encoding of lvalue reference type"); 3149 return QualType(); 3150 } 3151 QualType PointeeType = readType(*Loc.F, Record, Idx); 3152 return Context.getLValueReferenceType(PointeeType, Record[1]); 3153 } 3154 3155 case TYPE_RVALUE_REFERENCE: { 3156 if (Record.size() != 1) { 3157 Error("Incorrect encoding of rvalue reference type"); 3158 return QualType(); 3159 } 3160 QualType PointeeType = readType(*Loc.F, Record, Idx); 3161 return Context.getRValueReferenceType(PointeeType); 3162 } 3163 3164 case TYPE_MEMBER_POINTER: { 3165 if (Record.size() != 2) { 3166 Error("Incorrect encoding of member pointer type"); 3167 return QualType(); 3168 } 3169 QualType PointeeType = readType(*Loc.F, Record, Idx); 3170 QualType ClassType = readType(*Loc.F, Record, Idx); 3171 if (PointeeType.isNull() || ClassType.isNull()) 3172 return QualType(); 3173 3174 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 3175 } 3176 3177 case TYPE_CONSTANT_ARRAY: { 3178 QualType ElementType = readType(*Loc.F, Record, Idx); 3179 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3180 unsigned IndexTypeQuals = Record[2]; 3181 unsigned Idx = 3; 3182 llvm::APInt Size = ReadAPInt(Record, Idx); 3183 return Context.getConstantArrayType(ElementType, Size, 3184 ASM, IndexTypeQuals); 3185 } 3186 3187 case TYPE_INCOMPLETE_ARRAY: { 3188 QualType ElementType = readType(*Loc.F, Record, Idx); 3189 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3190 unsigned IndexTypeQuals = Record[2]; 3191 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 3192 } 3193 3194 case TYPE_VARIABLE_ARRAY: { 3195 QualType ElementType = readType(*Loc.F, Record, Idx); 3196 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 3197 unsigned IndexTypeQuals = Record[2]; 3198 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 3199 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 3200 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 3201 ASM, IndexTypeQuals, 3202 SourceRange(LBLoc, RBLoc)); 3203 } 3204 3205 case TYPE_VECTOR: { 3206 if (Record.size() != 3) { 3207 Error("incorrect encoding of vector type in AST file"); 3208 return QualType(); 3209 } 3210 3211 QualType ElementType = readType(*Loc.F, Record, Idx); 3212 unsigned NumElements = Record[1]; 3213 unsigned VecKind = Record[2]; 3214 return Context.getVectorType(ElementType, NumElements, 3215 (VectorType::VectorKind)VecKind); 3216 } 3217 3218 case TYPE_EXT_VECTOR: { 3219 if (Record.size() != 3) { 3220 Error("incorrect encoding of extended vector type in AST file"); 3221 return QualType(); 3222 } 3223 3224 QualType ElementType = readType(*Loc.F, Record, Idx); 3225 unsigned NumElements = Record[1]; 3226 return Context.getExtVectorType(ElementType, NumElements); 3227 } 3228 3229 case TYPE_FUNCTION_NO_PROTO: { 3230 if (Record.size() != 6) { 3231 Error("incorrect encoding of no-proto function type"); 3232 return QualType(); 3233 } 3234 QualType ResultType = readType(*Loc.F, Record, Idx); 3235 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 3236 (CallingConv)Record[4], Record[5]); 3237 return Context.getFunctionNoProtoType(ResultType, Info); 3238 } 3239 3240 case TYPE_FUNCTION_PROTO: { 3241 QualType ResultType = readType(*Loc.F, Record, Idx); 3242 3243 FunctionProtoType::ExtProtoInfo EPI; 3244 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 3245 /*hasregparm*/ Record[2], 3246 /*regparm*/ Record[3], 3247 static_cast<CallingConv>(Record[4]), 3248 /*produces*/ Record[5]); 3249 3250 unsigned Idx = 6; 3251 unsigned NumParams = Record[Idx++]; 3252 SmallVector<QualType, 16> ParamTypes; 3253 for (unsigned I = 0; I != NumParams; ++I) 3254 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 3255 3256 EPI.Variadic = Record[Idx++]; 3257 EPI.TypeQuals = Record[Idx++]; 3258 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 3259 ExceptionSpecificationType EST = 3260 static_cast<ExceptionSpecificationType>(Record[Idx++]); 3261 EPI.ExceptionSpecType = EST; 3262 if (EST == EST_Dynamic) { 3263 EPI.NumExceptions = Record[Idx++]; 3264 SmallVector<QualType, 2> Exceptions; 3265 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 3266 Exceptions.push_back(readType(*Loc.F, Record, Idx)); 3267 EPI.Exceptions = Exceptions.data(); 3268 } else if (EST == EST_ComputedNoexcept) { 3269 EPI.NoexceptExpr = ReadExpr(*Loc.F); 3270 } 3271 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams, 3272 EPI); 3273 } 3274 3275 case TYPE_UNRESOLVED_USING: { 3276 unsigned Idx = 0; 3277 return Context.getTypeDeclType( 3278 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 3279 } 3280 3281 case TYPE_TYPEDEF: { 3282 if (Record.size() != 2) { 3283 Error("incorrect encoding of typedef type"); 3284 return QualType(); 3285 } 3286 unsigned Idx = 0; 3287 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 3288 QualType Canonical = readType(*Loc.F, Record, Idx); 3289 if (!Canonical.isNull()) 3290 Canonical = Context.getCanonicalType(Canonical); 3291 return Context.getTypedefType(Decl, Canonical); 3292 } 3293 3294 case TYPE_TYPEOF_EXPR: 3295 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 3296 3297 case TYPE_TYPEOF: { 3298 if (Record.size() != 1) { 3299 Error("incorrect encoding of typeof(type) in AST file"); 3300 return QualType(); 3301 } 3302 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 3303 return Context.getTypeOfType(UnderlyingType); 3304 } 3305 3306 case TYPE_DECLTYPE: 3307 return Context.getDecltypeType(ReadExpr(*Loc.F)); 3308 3309 case TYPE_UNARY_TRANSFORM: { 3310 QualType BaseType = readType(*Loc.F, Record, Idx); 3311 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 3312 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 3313 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 3314 } 3315 3316 case TYPE_AUTO: 3317 return Context.getAutoType(readType(*Loc.F, Record, Idx)); 3318 3319 case TYPE_RECORD: { 3320 if (Record.size() != 2) { 3321 Error("incorrect encoding of record type"); 3322 return QualType(); 3323 } 3324 unsigned Idx = 0; 3325 bool IsDependent = Record[Idx++]; 3326 QualType T 3327 = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx)); 3328 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 3329 return T; 3330 } 3331 3332 case TYPE_ENUM: { 3333 if (Record.size() != 2) { 3334 Error("incorrect encoding of enum type"); 3335 return QualType(); 3336 } 3337 unsigned Idx = 0; 3338 bool IsDependent = Record[Idx++]; 3339 QualType T 3340 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 3341 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 3342 return T; 3343 } 3344 3345 case TYPE_ATTRIBUTED: { 3346 if (Record.size() != 3) { 3347 Error("incorrect encoding of attributed type"); 3348 return QualType(); 3349 } 3350 QualType modifiedType = readType(*Loc.F, Record, Idx); 3351 QualType equivalentType = readType(*Loc.F, Record, Idx); 3352 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 3353 return Context.getAttributedType(kind, modifiedType, equivalentType); 3354 } 3355 3356 case TYPE_PAREN: { 3357 if (Record.size() != 1) { 3358 Error("incorrect encoding of paren type"); 3359 return QualType(); 3360 } 3361 QualType InnerType = readType(*Loc.F, Record, Idx); 3362 return Context.getParenType(InnerType); 3363 } 3364 3365 case TYPE_PACK_EXPANSION: { 3366 if (Record.size() != 2) { 3367 Error("incorrect encoding of pack expansion type"); 3368 return QualType(); 3369 } 3370 QualType Pattern = readType(*Loc.F, Record, Idx); 3371 if (Pattern.isNull()) 3372 return QualType(); 3373 llvm::Optional<unsigned> NumExpansions; 3374 if (Record[1]) 3375 NumExpansions = Record[1] - 1; 3376 return Context.getPackExpansionType(Pattern, NumExpansions); 3377 } 3378 3379 case TYPE_ELABORATED: { 3380 unsigned Idx = 0; 3381 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3382 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 3383 QualType NamedType = readType(*Loc.F, Record, Idx); 3384 return Context.getElaboratedType(Keyword, NNS, NamedType); 3385 } 3386 3387 case TYPE_OBJC_INTERFACE: { 3388 unsigned Idx = 0; 3389 ObjCInterfaceDecl *ItfD 3390 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 3391 return Context.getObjCInterfaceType(ItfD); 3392 } 3393 3394 case TYPE_OBJC_OBJECT: { 3395 unsigned Idx = 0; 3396 QualType Base = readType(*Loc.F, Record, Idx); 3397 unsigned NumProtos = Record[Idx++]; 3398 SmallVector<ObjCProtocolDecl*, 4> Protos; 3399 for (unsigned I = 0; I != NumProtos; ++I) 3400 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 3401 return Context.getObjCObjectType(Base, Protos.data(), NumProtos); 3402 } 3403 3404 case TYPE_OBJC_OBJECT_POINTER: { 3405 unsigned Idx = 0; 3406 QualType Pointee = readType(*Loc.F, Record, Idx); 3407 return Context.getObjCObjectPointerType(Pointee); 3408 } 3409 3410 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 3411 unsigned Idx = 0; 3412 QualType Parm = readType(*Loc.F, Record, Idx); 3413 QualType Replacement = readType(*Loc.F, Record, Idx); 3414 return 3415 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 3416 Replacement); 3417 } 3418 3419 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 3420 unsigned Idx = 0; 3421 QualType Parm = readType(*Loc.F, Record, Idx); 3422 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 3423 return Context.getSubstTemplateTypeParmPackType( 3424 cast<TemplateTypeParmType>(Parm), 3425 ArgPack); 3426 } 3427 3428 case TYPE_INJECTED_CLASS_NAME: { 3429 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 3430 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 3431 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 3432 // for AST reading, too much interdependencies. 3433 return 3434 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 3435 } 3436 3437 case TYPE_TEMPLATE_TYPE_PARM: { 3438 unsigned Idx = 0; 3439 unsigned Depth = Record[Idx++]; 3440 unsigned Index = Record[Idx++]; 3441 bool Pack = Record[Idx++]; 3442 TemplateTypeParmDecl *D 3443 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 3444 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 3445 } 3446 3447 case TYPE_DEPENDENT_NAME: { 3448 unsigned Idx = 0; 3449 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3450 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 3451 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 3452 QualType Canon = readType(*Loc.F, Record, Idx); 3453 if (!Canon.isNull()) 3454 Canon = Context.getCanonicalType(Canon); 3455 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 3456 } 3457 3458 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 3459 unsigned Idx = 0; 3460 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3461 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 3462 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); 3463 unsigned NumArgs = Record[Idx++]; 3464 SmallVector<TemplateArgument, 8> Args; 3465 Args.reserve(NumArgs); 3466 while (NumArgs--) 3467 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 3468 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 3469 Args.size(), Args.data()); 3470 } 3471 3472 case TYPE_DEPENDENT_SIZED_ARRAY: { 3473 unsigned Idx = 0; 3474 3475 // ArrayType 3476 QualType ElementType = readType(*Loc.F, Record, Idx); 3477 ArrayType::ArraySizeModifier ASM 3478 = (ArrayType::ArraySizeModifier)Record[Idx++]; 3479 unsigned IndexTypeQuals = Record[Idx++]; 3480 3481 // DependentSizedArrayType 3482 Expr *NumElts = ReadExpr(*Loc.F); 3483 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 3484 3485 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 3486 IndexTypeQuals, Brackets); 3487 } 3488 3489 case TYPE_TEMPLATE_SPECIALIZATION: { 3490 unsigned Idx = 0; 3491 bool IsDependent = Record[Idx++]; 3492 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 3493 SmallVector<TemplateArgument, 8> Args; 3494 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 3495 QualType Underlying = readType(*Loc.F, Record, Idx); 3496 QualType T; 3497 if (Underlying.isNull()) 3498 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(), 3499 Args.size()); 3500 else 3501 T = Context.getTemplateSpecializationType(Name, Args.data(), 3502 Args.size(), Underlying); 3503 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 3504 return T; 3505 } 3506 } 3507 // Suppress a GCC warning 3508 return QualType(); 3509} 3510 3511class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 3512 ASTReader &Reader; 3513 Module &F; 3514 llvm::BitstreamCursor &DeclsCursor; 3515 const ASTReader::RecordData &Record; 3516 unsigned &Idx; 3517 3518 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 3519 unsigned &I) { 3520 return Reader.ReadSourceLocation(F, R, I); 3521 } 3522 3523 template<typename T> 3524 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 3525 return Reader.ReadDeclAs<T>(F, Record, Idx); 3526 } 3527 3528public: 3529 TypeLocReader(ASTReader &Reader, Module &F, 3530 const ASTReader::RecordData &Record, unsigned &Idx) 3531 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx) 3532 { } 3533 3534 // We want compile-time assurance that we've enumerated all of 3535 // these, so unfortunately we have to declare them first, then 3536 // define them out-of-line. 3537#define ABSTRACT_TYPELOC(CLASS, PARENT) 3538#define TYPELOC(CLASS, PARENT) \ 3539 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 3540#include "clang/AST/TypeLocNodes.def" 3541 3542 void VisitFunctionTypeLoc(FunctionTypeLoc); 3543 void VisitArrayTypeLoc(ArrayTypeLoc); 3544}; 3545 3546void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 3547 // nothing to do 3548} 3549void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 3550 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 3551 if (TL.needsExtraLocalData()) { 3552 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 3553 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 3554 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 3555 TL.setModeAttr(Record[Idx++]); 3556 } 3557} 3558void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 3559 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3560} 3561void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 3562 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3563} 3564void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 3565 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 3566} 3567void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 3568 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 3569} 3570void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 3571 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 3572} 3573void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 3574 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3575 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 3576} 3577void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 3578 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 3579 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 3580 if (Record[Idx++]) 3581 TL.setSizeExpr(Reader.ReadExpr(F)); 3582 else 3583 TL.setSizeExpr(0); 3584} 3585void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 3586 VisitArrayTypeLoc(TL); 3587} 3588void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 3589 VisitArrayTypeLoc(TL); 3590} 3591void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 3592 VisitArrayTypeLoc(TL); 3593} 3594void TypeLocReader::VisitDependentSizedArrayTypeLoc( 3595 DependentSizedArrayTypeLoc TL) { 3596 VisitArrayTypeLoc(TL); 3597} 3598void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 3599 DependentSizedExtVectorTypeLoc TL) { 3600 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3601} 3602void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 3603 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3604} 3605void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 3606 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3607} 3608void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 3609 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 3610 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 3611 TL.setTrailingReturn(Record[Idx++]); 3612 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 3613 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 3614 } 3615} 3616void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 3617 VisitFunctionTypeLoc(TL); 3618} 3619void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 3620 VisitFunctionTypeLoc(TL); 3621} 3622void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 3623 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3624} 3625void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 3626 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3627} 3628void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 3629 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 3630 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3631 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3632} 3633void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 3634 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 3635 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3636 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3637 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 3638} 3639void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 3640 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3641} 3642void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 3643 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 3644 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3645 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3646 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 3647} 3648void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 3649 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3650} 3651void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 3652 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3653} 3654void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 3655 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3656} 3657void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 3658 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 3659 if (TL.hasAttrOperand()) { 3660 SourceRange range; 3661 range.setBegin(ReadSourceLocation(Record, Idx)); 3662 range.setEnd(ReadSourceLocation(Record, Idx)); 3663 TL.setAttrOperandParensRange(range); 3664 } 3665 if (TL.hasAttrExprOperand()) { 3666 if (Record[Idx++]) 3667 TL.setAttrExprOperand(Reader.ReadExpr(F)); 3668 else 3669 TL.setAttrExprOperand(0); 3670 } else if (TL.hasAttrEnumOperand()) 3671 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 3672} 3673void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 3674 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3675} 3676void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 3677 SubstTemplateTypeParmTypeLoc TL) { 3678 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3679} 3680void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 3681 SubstTemplateTypeParmPackTypeLoc TL) { 3682 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3683} 3684void TypeLocReader::VisitTemplateSpecializationTypeLoc( 3685 TemplateSpecializationTypeLoc TL) { 3686 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 3687 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3688 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3689 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 3690 TL.setArgLocInfo(i, 3691 Reader.GetTemplateArgumentLocInfo(F, 3692 TL.getTypePtr()->getArg(i).getKind(), 3693 Record, Idx)); 3694} 3695void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 3696 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3697 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3698} 3699void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 3700 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3701 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 3702} 3703void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 3704 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3705} 3706void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 3707 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3708 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 3709 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3710} 3711void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 3712 DependentTemplateSpecializationTypeLoc TL) { 3713 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3714 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 3715 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3716 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3717 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3718 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 3719 TL.setArgLocInfo(I, 3720 Reader.GetTemplateArgumentLocInfo(F, 3721 TL.getTypePtr()->getArg(I).getKind(), 3722 Record, Idx)); 3723} 3724void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 3725 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 3726} 3727void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 3728 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3729} 3730void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 3731 TL.setHasBaseTypeAsWritten(Record[Idx++]); 3732 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3733 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3734 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 3735 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 3736} 3737void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 3738 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3739} 3740 3741TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F, 3742 const RecordData &Record, 3743 unsigned &Idx) { 3744 QualType InfoTy = readType(F, Record, Idx); 3745 if (InfoTy.isNull()) 3746 return 0; 3747 3748 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 3749 TypeLocReader TLR(*this, F, Record, Idx); 3750 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 3751 TLR.Visit(TL); 3752 return TInfo; 3753} 3754 3755QualType ASTReader::GetType(TypeID ID) { 3756 unsigned FastQuals = ID & Qualifiers::FastMask; 3757 unsigned Index = ID >> Qualifiers::FastWidth; 3758 3759 if (Index < NUM_PREDEF_TYPE_IDS) { 3760 QualType T; 3761 switch ((PredefinedTypeIDs)Index) { 3762 case PREDEF_TYPE_NULL_ID: return QualType(); 3763 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break; 3764 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break; 3765 3766 case PREDEF_TYPE_CHAR_U_ID: 3767 case PREDEF_TYPE_CHAR_S_ID: 3768 // FIXME: Check that the signedness of CharTy is correct! 3769 T = Context.CharTy; 3770 break; 3771 3772 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break; 3773 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break; 3774 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break; 3775 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break; 3776 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break; 3777 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break; 3778 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break; 3779 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break; 3780 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break; 3781 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break; 3782 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break; 3783 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break; 3784 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break; 3785 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break; 3786 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break; 3787 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break; 3788 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break; 3789 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break; 3790 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break; 3791 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break; 3792 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break; 3793 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break; 3794 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break; 3795 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break; 3796 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break; 3797 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break; 3798 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break; 3799 3800 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 3801 T = Context.getAutoRRefDeductType(); 3802 break; 3803 } 3804 3805 assert(!T.isNull() && "Unknown predefined type"); 3806 return T.withFastQualifiers(FastQuals); 3807 } 3808 3809 Index -= NUM_PREDEF_TYPE_IDS; 3810 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 3811 if (TypesLoaded[Index].isNull()) { 3812 TypesLoaded[Index] = readTypeRecord(Index); 3813 if (TypesLoaded[Index].isNull()) 3814 return QualType(); 3815 3816 TypesLoaded[Index]->setFromAST(); 3817 if (DeserializationListener) 3818 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 3819 TypesLoaded[Index]); 3820 } 3821 3822 return TypesLoaded[Index].withFastQualifiers(FastQuals); 3823} 3824 3825QualType ASTReader::getLocalType(Module &F, unsigned LocalID) { 3826 return GetType(getGlobalTypeID(F, LocalID)); 3827} 3828 3829serialization::TypeID 3830ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const { 3831 unsigned FastQuals = LocalID & Qualifiers::FastMask; 3832 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 3833 3834 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 3835 return LocalID; 3836 3837 ContinuousRangeMap<uint32_t, int, 2>::iterator I 3838 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 3839 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 3840 3841 unsigned GlobalIndex = LocalIndex + I->second; 3842 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 3843} 3844 3845TemplateArgumentLocInfo 3846ASTReader::GetTemplateArgumentLocInfo(Module &F, 3847 TemplateArgument::ArgKind Kind, 3848 const RecordData &Record, 3849 unsigned &Index) { 3850 switch (Kind) { 3851 case TemplateArgument::Expression: 3852 return ReadExpr(F); 3853 case TemplateArgument::Type: 3854 return GetTypeSourceInfo(F, Record, Index); 3855 case TemplateArgument::Template: { 3856 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 3857 Index); 3858 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 3859 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 3860 SourceLocation()); 3861 } 3862 case TemplateArgument::TemplateExpansion: { 3863 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 3864 Index); 3865 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 3866 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 3867 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 3868 EllipsisLoc); 3869 } 3870 case TemplateArgument::Null: 3871 case TemplateArgument::Integral: 3872 case TemplateArgument::Declaration: 3873 case TemplateArgument::Pack: 3874 return TemplateArgumentLocInfo(); 3875 } 3876 llvm_unreachable("unexpected template argument loc"); 3877 return TemplateArgumentLocInfo(); 3878} 3879 3880TemplateArgumentLoc 3881ASTReader::ReadTemplateArgumentLoc(Module &F, 3882 const RecordData &Record, unsigned &Index) { 3883 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 3884 3885 if (Arg.getKind() == TemplateArgument::Expression) { 3886 if (Record[Index++]) // bool InfoHasSameExpr. 3887 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 3888 } 3889 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 3890 Record, Index)); 3891} 3892 3893Decl *ASTReader::GetExternalDecl(uint32_t ID) { 3894 return GetDecl(ID); 3895} 3896 3897uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record, 3898 unsigned &Idx){ 3899 if (Idx >= Record.size()) 3900 return 0; 3901 3902 unsigned LocalID = Record[Idx++]; 3903 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]); 3904} 3905 3906CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 3907 RecordLocation Loc = getLocalBitOffset(Offset); 3908 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor; 3909 SavedStreamPosition SavedPosition(Cursor); 3910 Cursor.JumpToBit(Loc.Offset); 3911 ReadingKindTracker ReadingKind(Read_Decl, *this); 3912 RecordData Record; 3913 unsigned Code = Cursor.ReadCode(); 3914 unsigned RecCode = Cursor.ReadRecord(Code, Record); 3915 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 3916 Error("Malformed AST file: missing C++ base specifiers"); 3917 return 0; 3918 } 3919 3920 unsigned Idx = 0; 3921 unsigned NumBases = Record[Idx++]; 3922 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 3923 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 3924 for (unsigned I = 0; I != NumBases; ++I) 3925 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 3926 return Bases; 3927} 3928 3929serialization::DeclID 3930ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const { 3931 if (LocalID < NUM_PREDEF_DECL_IDS) 3932 return LocalID; 3933 3934 ContinuousRangeMap<uint32_t, int, 2>::iterator I 3935 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 3936 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 3937 3938 return LocalID + I->second; 3939} 3940 3941bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 3942 Module &M) const { 3943 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID); 3944 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 3945 return &M == I->second; 3946} 3947 3948Decl *ASTReader::GetDecl(DeclID ID) { 3949 if (ID < NUM_PREDEF_DECL_IDS) { 3950 switch ((PredefinedDeclIDs)ID) { 3951 case PREDEF_DECL_NULL_ID: 3952 return 0; 3953 3954 case PREDEF_DECL_TRANSLATION_UNIT_ID: 3955 return Context.getTranslationUnitDecl(); 3956 3957 case PREDEF_DECL_OBJC_ID_ID: 3958 return Context.getObjCIdDecl(); 3959 3960 case PREDEF_DECL_OBJC_SEL_ID: 3961 return Context.getObjCSelDecl(); 3962 3963 case PREDEF_DECL_OBJC_CLASS_ID: 3964 return Context.getObjCClassDecl(); 3965 3966 case PREDEF_DECL_INT_128_ID: 3967 return Context.getInt128Decl(); 3968 3969 case PREDEF_DECL_UNSIGNED_INT_128_ID: 3970 return Context.getUInt128Decl(); 3971 3972 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 3973 return Context.getObjCInstanceTypeDecl(); 3974 } 3975 3976 return 0; 3977 } 3978 3979 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 3980 3981 if (Index > DeclsLoaded.size()) { 3982 Error("declaration ID out-of-range for AST file"); 3983 return 0; 3984 } 3985 3986if (!DeclsLoaded[Index]) { 3987 ReadDeclRecord(ID); 3988 if (DeserializationListener) 3989 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 3990 } 3991 3992 return DeclsLoaded[Index]; 3993} 3994 3995serialization::DeclID ASTReader::ReadDeclID(Module &F, 3996 const RecordData &Record, 3997 unsigned &Idx) { 3998 if (Idx >= Record.size()) { 3999 Error("Corrupted AST file"); 4000 return 0; 4001 } 4002 4003 return getGlobalDeclID(F, Record[Idx++]); 4004} 4005 4006/// \brief Resolve the offset of a statement into a statement. 4007/// 4008/// This operation will read a new statement from the external 4009/// source each time it is called, and is meant to be used via a 4010/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 4011Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 4012 // Switch case IDs are per Decl. 4013 ClearSwitchCaseIDs(); 4014 4015 // Offset here is a global offset across the entire chain. 4016 RecordLocation Loc = getLocalBitOffset(Offset); 4017 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 4018 return ReadStmtFromStream(*Loc.F); 4019} 4020 4021namespace { 4022 class FindExternalLexicalDeclsVisitor { 4023 ASTReader &Reader; 4024 const DeclContext *DC; 4025 bool (*isKindWeWant)(Decl::Kind); 4026 4027 SmallVectorImpl<Decl*> &Decls; 4028 bool PredefsVisited[NUM_PREDEF_DECL_IDS]; 4029 4030 public: 4031 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC, 4032 bool (*isKindWeWant)(Decl::Kind), 4033 SmallVectorImpl<Decl*> &Decls) 4034 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls) 4035 { 4036 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I) 4037 PredefsVisited[I] = false; 4038 } 4039 4040 static bool visit(Module &M, bool Preorder, void *UserData) { 4041 if (Preorder) 4042 return false; 4043 4044 FindExternalLexicalDeclsVisitor *This 4045 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData); 4046 4047 Module::DeclContextInfosMap::iterator Info 4048 = M.DeclContextInfos.find(This->DC); 4049 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls) 4050 return false; 4051 4052 // Load all of the declaration IDs 4053 for (const KindDeclIDPair *ID = Info->second.LexicalDecls, 4054 *IDE = ID + Info->second.NumLexicalDecls; 4055 ID != IDE; ++ID) { 4056 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first)) 4057 continue; 4058 4059 // Don't add predefined declarations to the lexical context more 4060 // than once. 4061 if (ID->second < NUM_PREDEF_DECL_IDS) { 4062 if (This->PredefsVisited[ID->second]) 4063 continue; 4064 4065 This->PredefsVisited[ID->second] = true; 4066 } 4067 4068 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) { 4069 if (!This->DC->isDeclInLexicalTraversal(D)) 4070 This->Decls.push_back(D); 4071 } 4072 } 4073 4074 return false; 4075 } 4076 }; 4077} 4078 4079ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 4080 bool (*isKindWeWant)(Decl::Kind), 4081 SmallVectorImpl<Decl*> &Decls) { 4082 // There might be lexical decls in multiple modules, for the TU at 4083 // least. Walk all of the modules in the order they were loaded. 4084 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls); 4085 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor); 4086 ++NumLexicalDeclContextsRead; 4087 return ELR_Success; 4088} 4089 4090namespace { 4091 /// \brief Module visitor used to perform name lookup into a 4092 /// declaration context. 4093 class DeclContextNameLookupVisitor { 4094 ASTReader &Reader; 4095 const DeclContext *DC; 4096 DeclarationName Name; 4097 SmallVectorImpl<NamedDecl *> &Decls; 4098 4099 public: 4100 DeclContextNameLookupVisitor(ASTReader &Reader, 4101 const DeclContext *DC, DeclarationName Name, 4102 SmallVectorImpl<NamedDecl *> &Decls) 4103 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { } 4104 4105 static bool visit(Module &M, void *UserData) { 4106 DeclContextNameLookupVisitor *This 4107 = static_cast<DeclContextNameLookupVisitor *>(UserData); 4108 4109 // Check whether we have any visible declaration information for 4110 // this context in this module. 4111 Module::DeclContextInfosMap::iterator Info 4112 = M.DeclContextInfos.find(This->DC); 4113 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData) 4114 return false; 4115 4116 // Look for this name within this module. 4117 ASTDeclContextNameLookupTable *LookupTable = 4118 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData; 4119 ASTDeclContextNameLookupTable::iterator Pos 4120 = LookupTable->find(This->Name); 4121 if (Pos == LookupTable->end()) 4122 return false; 4123 4124 bool FoundAnything = false; 4125 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 4126 for (; Data.first != Data.second; ++Data.first) { 4127 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first); 4128 if (!ND) 4129 continue; 4130 4131 if (ND->getDeclName() != This->Name) { 4132 assert(!This->Name.getCXXNameType().isNull() && 4133 "Name mismatch without a type"); 4134 continue; 4135 } 4136 4137 // Record this declaration. 4138 FoundAnything = true; 4139 This->Decls.push_back(ND); 4140 } 4141 4142 return FoundAnything; 4143 } 4144 }; 4145} 4146 4147DeclContext::lookup_result 4148ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 4149 DeclarationName Name) { 4150 assert(DC->hasExternalVisibleStorage() && 4151 "DeclContext has no visible decls in storage"); 4152 if (!Name) 4153 return DeclContext::lookup_result(DeclContext::lookup_iterator(0), 4154 DeclContext::lookup_iterator(0)); 4155 4156 SmallVector<NamedDecl *, 64> Decls; 4157 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls); 4158 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor); 4159 ++NumVisibleDeclContextsRead; 4160 SetExternalVisibleDeclsForName(DC, Name, Decls); 4161 return const_cast<DeclContext*>(DC)->lookup(Name); 4162} 4163 4164/// \brief Under non-PCH compilation the consumer receives the objc methods 4165/// before receiving the implementation, and codegen depends on this. 4166/// We simulate this by deserializing and passing to consumer the methods of the 4167/// implementation before passing the deserialized implementation decl. 4168static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 4169 ASTConsumer *Consumer) { 4170 assert(ImplD && Consumer); 4171 4172 for (ObjCImplDecl::method_iterator 4173 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I) 4174 Consumer->HandleInterestingDecl(DeclGroupRef(*I)); 4175 4176 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 4177} 4178 4179void ASTReader::PassInterestingDeclsToConsumer() { 4180 assert(Consumer); 4181 while (!InterestingDecls.empty()) { 4182 Decl *D = InterestingDecls.front(); 4183 InterestingDecls.pop_front(); 4184 4185 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 4186 PassObjCImplDeclToConsumer(ImplD, Consumer); 4187 else 4188 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 4189 } 4190} 4191 4192void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 4193 this->Consumer = Consumer; 4194 4195 if (!Consumer) 4196 return; 4197 4198 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 4199 // Force deserialization of this decl, which will cause it to be queued for 4200 // passing to the consumer. 4201 GetDecl(ExternalDefinitions[I]); 4202 } 4203 ExternalDefinitions.clear(); 4204 4205 PassInterestingDeclsToConsumer(); 4206} 4207 4208void ASTReader::PrintStats() { 4209 std::fprintf(stderr, "*** AST File Statistics:\n"); 4210 4211 unsigned NumTypesLoaded 4212 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 4213 QualType()); 4214 unsigned NumDeclsLoaded 4215 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 4216 (Decl *)0); 4217 unsigned NumIdentifiersLoaded 4218 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 4219 IdentifiersLoaded.end(), 4220 (IdentifierInfo *)0); 4221 unsigned NumSelectorsLoaded 4222 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 4223 SelectorsLoaded.end(), 4224 Selector()); 4225 4226 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); 4227 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); 4228 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 4229 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 4230 NumSLocEntriesRead, TotalNumSLocEntries, 4231 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 4232 if (!TypesLoaded.empty()) 4233 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 4234 NumTypesLoaded, (unsigned)TypesLoaded.size(), 4235 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 4236 if (!DeclsLoaded.empty()) 4237 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 4238 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 4239 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 4240 if (!IdentifiersLoaded.empty()) 4241 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 4242 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 4243 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 4244 if (!SelectorsLoaded.empty()) 4245 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 4246 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 4247 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 4248 if (TotalNumStatements) 4249 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 4250 NumStatementsRead, TotalNumStatements, 4251 ((float)NumStatementsRead/TotalNumStatements * 100)); 4252 if (TotalNumMacros) 4253 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 4254 NumMacrosRead, TotalNumMacros, 4255 ((float)NumMacrosRead/TotalNumMacros * 100)); 4256 if (TotalLexicalDeclContexts) 4257 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 4258 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 4259 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 4260 * 100)); 4261 if (TotalVisibleDeclContexts) 4262 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 4263 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 4264 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 4265 * 100)); 4266 if (TotalNumMethodPoolEntries) { 4267 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 4268 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 4269 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 4270 * 100)); 4271 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); 4272 } 4273 std::fprintf(stderr, "\n"); 4274 dump(); 4275 std::fprintf(stderr, "\n"); 4276} 4277 4278template<typename Key, typename Module, unsigned InitialCapacity> 4279static void 4280dumpModuleIDMap(StringRef Name, 4281 const ContinuousRangeMap<Key, Module *, 4282 InitialCapacity> &Map) { 4283 if (Map.begin() == Map.end()) 4284 return; 4285 4286 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType; 4287 llvm::errs() << Name << ":\n"; 4288 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 4289 I != IEnd; ++I) { 4290 llvm::errs() << " " << I->first << " -> " << I->second->FileName 4291 << "\n"; 4292 } 4293} 4294 4295void ASTReader::dump() { 4296 llvm::errs() << "*** PCH/Module Remappings:\n"; 4297 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 4298 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 4299 dumpModuleIDMap("Global type map", GlobalTypeMap); 4300 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 4301 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 4302 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 4303 dumpModuleIDMap("Global preprocessed entity map", 4304 GlobalPreprocessedEntityMap); 4305 4306 llvm::errs() << "\n*** PCH/Modules Loaded:"; 4307 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 4308 MEnd = ModuleMgr.end(); 4309 M != MEnd; ++M) 4310 (*M)->dump(); 4311} 4312 4313/// Return the amount of memory used by memory buffers, breaking down 4314/// by heap-backed versus mmap'ed memory. 4315void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 4316 for (ModuleConstIterator I = ModuleMgr.begin(), 4317 E = ModuleMgr.end(); I != E; ++I) { 4318 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 4319 size_t bytes = buf->getBufferSize(); 4320 switch (buf->getBufferKind()) { 4321 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 4322 sizes.malloc_bytes += bytes; 4323 break; 4324 case llvm::MemoryBuffer::MemoryBuffer_MMap: 4325 sizes.mmap_bytes += bytes; 4326 break; 4327 } 4328 } 4329 } 4330} 4331 4332void ASTReader::InitializeSema(Sema &S) { 4333 SemaObj = &S; 4334 S.ExternalSource = this; 4335 4336 // Makes sure any declarations that were deserialized "too early" 4337 // still get added to the identifier's declaration chains. 4338 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 4339 if (SemaObj->TUScope) 4340 SemaObj->TUScope->AddDecl(PreloadedDecls[I]); 4341 4342 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); 4343 } 4344 PreloadedDecls.clear(); 4345 4346 // Load the offsets of the declarations that Sema references. 4347 // They will be lazily deserialized when needed. 4348 if (!SemaDeclRefs.empty()) { 4349 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 4350 if (!SemaObj->StdNamespace) 4351 SemaObj->StdNamespace = SemaDeclRefs[0]; 4352 if (!SemaObj->StdBadAlloc) 4353 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 4354 } 4355 4356 if (!FPPragmaOptions.empty()) { 4357 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 4358 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 4359 } 4360 4361 if (!OpenCLExtensions.empty()) { 4362 unsigned I = 0; 4363#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 4364#include "clang/Basic/OpenCLExtensions.def" 4365 4366 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 4367 } 4368} 4369 4370IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 4371 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart)); 4372 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); 4373 return Visitor.getIdentifierInfo(); 4374} 4375 4376namespace clang { 4377 /// \brief An identifier-lookup iterator that enumerates all of the 4378 /// identifiers stored within a set of AST files. 4379 class ASTIdentifierIterator : public IdentifierIterator { 4380 /// \brief The AST reader whose identifiers are being enumerated. 4381 const ASTReader &Reader; 4382 4383 /// \brief The current index into the chain of AST files stored in 4384 /// the AST reader. 4385 unsigned Index; 4386 4387 /// \brief The current position within the identifier lookup table 4388 /// of the current AST file. 4389 ASTIdentifierLookupTable::key_iterator Current; 4390 4391 /// \brief The end position within the identifier lookup table of 4392 /// the current AST file. 4393 ASTIdentifierLookupTable::key_iterator End; 4394 4395 public: 4396 explicit ASTIdentifierIterator(const ASTReader &Reader); 4397 4398 virtual StringRef Next(); 4399 }; 4400} 4401 4402ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 4403 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) { 4404 ASTIdentifierLookupTable *IdTable 4405 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable; 4406 Current = IdTable->key_begin(); 4407 End = IdTable->key_end(); 4408} 4409 4410StringRef ASTIdentifierIterator::Next() { 4411 while (Current == End) { 4412 // If we have exhausted all of our AST files, we're done. 4413 if (Index == 0) 4414 return StringRef(); 4415 4416 --Index; 4417 ASTIdentifierLookupTable *IdTable 4418 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index]. 4419 IdentifierLookupTable; 4420 Current = IdTable->key_begin(); 4421 End = IdTable->key_end(); 4422 } 4423 4424 // We have any identifiers remaining in the current AST file; return 4425 // the next one. 4426 std::pair<const char*, unsigned> Key = *Current; 4427 ++Current; 4428 return StringRef(Key.first, Key.second); 4429} 4430 4431IdentifierIterator *ASTReader::getIdentifiers() const { 4432 return new ASTIdentifierIterator(*this); 4433} 4434 4435namespace clang { namespace serialization { 4436 class ReadMethodPoolVisitor { 4437 ASTReader &Reader; 4438 Selector Sel; 4439 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 4440 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 4441 4442 /// \brief Build an ObjCMethodList from a vector of Objective-C method 4443 /// declarations. 4444 ObjCMethodList 4445 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const 4446 { 4447 ObjCMethodList List; 4448 ObjCMethodList *Prev = 0; 4449 for (unsigned I = 0, N = Vec.size(); I != N; ++I) { 4450 if (!List.Method) { 4451 // This is the first method, which is the easy case. 4452 List.Method = Vec[I]; 4453 Prev = &List; 4454 continue; 4455 } 4456 4457 ObjCMethodList *Mem = 4458 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); 4459 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0); 4460 Prev = Prev->Next; 4461 } 4462 4463 return List; 4464 } 4465 4466 public: 4467 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel) 4468 : Reader(Reader), Sel(Sel) { } 4469 4470 static bool visit(Module &M, void *UserData) { 4471 ReadMethodPoolVisitor *This 4472 = static_cast<ReadMethodPoolVisitor *>(UserData); 4473 4474 if (!M.SelectorLookupTable) 4475 return false; 4476 4477 ASTSelectorLookupTable *PoolTable 4478 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 4479 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); 4480 if (Pos == PoolTable->end()) 4481 return false; 4482 4483 ++This->Reader.NumSelectorsRead; 4484 // FIXME: Not quite happy with the statistics here. We probably should 4485 // disable this tracking when called via LoadSelector. 4486 // Also, should entries without methods count as misses? 4487 ++This->Reader.NumMethodPoolEntriesRead; 4488 ASTSelectorLookupTrait::data_type Data = *Pos; 4489 if (This->Reader.DeserializationListener) 4490 This->Reader.DeserializationListener->SelectorRead(Data.ID, 4491 This->Sel); 4492 4493 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 4494 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 4495 return true; 4496 } 4497 4498 /// \brief Retrieve the instance methods found by this visitor. 4499 ObjCMethodList getInstanceMethods() const { 4500 return buildObjCMethodList(InstanceMethods); 4501 } 4502 4503 /// \brief Retrieve the instance methods found by this visitor. 4504 ObjCMethodList getFactoryMethods() const { 4505 return buildObjCMethodList(FactoryMethods); 4506 } 4507 }; 4508} } // end namespace clang::serialization 4509 4510std::pair<ObjCMethodList, ObjCMethodList> 4511ASTReader::ReadMethodPool(Selector Sel) { 4512 ReadMethodPoolVisitor Visitor(*this, Sel); 4513 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); 4514 std::pair<ObjCMethodList, ObjCMethodList> Result; 4515 Result.first = Visitor.getInstanceMethods(); 4516 Result.second = Visitor.getFactoryMethods(); 4517 4518 if (!Result.first.Method && !Result.second.Method) 4519 ++NumMethodPoolMisses; 4520 return Result; 4521} 4522 4523void ASTReader::ReadKnownNamespaces( 4524 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 4525 Namespaces.clear(); 4526 4527 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 4528 if (NamespaceDecl *Namespace 4529 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 4530 Namespaces.push_back(Namespace); 4531 } 4532} 4533 4534void ASTReader::ReadTentativeDefinitions( 4535 SmallVectorImpl<VarDecl *> &TentativeDefs) { 4536 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 4537 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 4538 if (Var) 4539 TentativeDefs.push_back(Var); 4540 } 4541 TentativeDefinitions.clear(); 4542} 4543 4544void ASTReader::ReadUnusedFileScopedDecls( 4545 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 4546 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 4547 DeclaratorDecl *D 4548 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 4549 if (D) 4550 Decls.push_back(D); 4551 } 4552 UnusedFileScopedDecls.clear(); 4553} 4554 4555void ASTReader::ReadDelegatingConstructors( 4556 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 4557 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 4558 CXXConstructorDecl *D 4559 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 4560 if (D) 4561 Decls.push_back(D); 4562 } 4563 DelegatingCtorDecls.clear(); 4564} 4565 4566void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 4567 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 4568 TypedefNameDecl *D 4569 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 4570 if (D) 4571 Decls.push_back(D); 4572 } 4573 ExtVectorDecls.clear(); 4574} 4575 4576void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) { 4577 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 4578 CXXRecordDecl *D 4579 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I])); 4580 if (D) 4581 Decls.push_back(D); 4582 } 4583 DynamicClasses.clear(); 4584} 4585 4586void 4587ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) { 4588 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { 4589 NamedDecl *D 4590 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); 4591 if (D) 4592 Decls.push_back(D); 4593 } 4594 LocallyScopedExternalDecls.clear(); 4595} 4596 4597void ASTReader::ReadReferencedSelectors( 4598 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 4599 if (ReferencedSelectorsData.empty()) 4600 return; 4601 4602 // If there are @selector references added them to its pool. This is for 4603 // implementation of -Wselector. 4604 unsigned int DataSize = ReferencedSelectorsData.size()-1; 4605 unsigned I = 0; 4606 while (I < DataSize) { 4607 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 4608 SourceLocation SelLoc 4609 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 4610 Sels.push_back(std::make_pair(Sel, SelLoc)); 4611 } 4612 ReferencedSelectorsData.clear(); 4613} 4614 4615void ASTReader::ReadWeakUndeclaredIdentifiers( 4616 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 4617 if (WeakUndeclaredIdentifiers.empty()) 4618 return; 4619 4620 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 4621 IdentifierInfo *WeakId 4622 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 4623 IdentifierInfo *AliasId 4624 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 4625 SourceLocation Loc 4626 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 4627 bool Used = WeakUndeclaredIdentifiers[I++]; 4628 WeakInfo WI(AliasId, Loc); 4629 WI.setUsed(Used); 4630 WeakIDs.push_back(std::make_pair(WeakId, WI)); 4631 } 4632 WeakUndeclaredIdentifiers.clear(); 4633} 4634 4635void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 4636 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 4637 ExternalVTableUse VT; 4638 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 4639 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 4640 VT.DefinitionRequired = VTableUses[Idx++]; 4641 VTables.push_back(VT); 4642 } 4643 4644 VTableUses.clear(); 4645} 4646 4647void ASTReader::ReadPendingInstantiations( 4648 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 4649 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 4650 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 4651 SourceLocation Loc 4652 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 4653 Pending.push_back(std::make_pair(D, Loc)); 4654 } 4655 PendingInstantiations.clear(); 4656} 4657 4658void ASTReader::LoadSelector(Selector Sel) { 4659 // It would be complicated to avoid reading the methods anyway. So don't. 4660 ReadMethodPool(Sel); 4661} 4662 4663void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 4664 assert(ID && "Non-zero identifier ID required"); 4665 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 4666 IdentifiersLoaded[ID - 1] = II; 4667 if (DeserializationListener) 4668 DeserializationListener->IdentifierRead(ID, II); 4669} 4670 4671/// \brief Set the globally-visible declarations associated with the given 4672/// identifier. 4673/// 4674/// If the AST reader is currently in a state where the given declaration IDs 4675/// cannot safely be resolved, they are queued until it is safe to resolve 4676/// them. 4677/// 4678/// \param II an IdentifierInfo that refers to one or more globally-visible 4679/// declarations. 4680/// 4681/// \param DeclIDs the set of declaration IDs with the name @p II that are 4682/// visible at global scope. 4683/// 4684/// \param Nonrecursive should be true to indicate that the caller knows that 4685/// this call is non-recursive, and therefore the globally-visible declarations 4686/// will not be placed onto the pending queue. 4687void 4688ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 4689 const SmallVectorImpl<uint32_t> &DeclIDs, 4690 bool Nonrecursive) { 4691 if (NumCurrentElementsDeserializing && !Nonrecursive) { 4692 PendingIdentifierInfos.push_back(PendingIdentifierInfo()); 4693 PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); 4694 PII.II = II; 4695 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end()); 4696 return; 4697 } 4698 4699 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 4700 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 4701 if (SemaObj) { 4702 if (SemaObj->TUScope) { 4703 // Introduce this declaration into the translation-unit scope 4704 // and add it to the declaration chain for this identifier, so 4705 // that (unqualified) name lookup will find it. 4706 SemaObj->TUScope->AddDecl(D); 4707 } 4708 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); 4709 } else { 4710 // Queue this declaration so that it will be added to the 4711 // translation unit scope and identifier's declaration chain 4712 // once a Sema object is known. 4713 PreloadedDecls.push_back(D); 4714 } 4715 } 4716} 4717 4718IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 4719 if (ID == 0) 4720 return 0; 4721 4722 if (IdentifiersLoaded.empty()) { 4723 Error("no identifier table in AST file"); 4724 return 0; 4725 } 4726 4727 ID -= 1; 4728 if (!IdentifiersLoaded[ID]) { 4729 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 4730 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 4731 Module *M = I->second; 4732 unsigned Index = ID - M->BaseIdentifierID; 4733 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 4734 4735 // All of the strings in the AST file are preceded by a 16-bit length. 4736 // Extract that 16-bit length to avoid having to execute strlen(). 4737 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 4738 // unsigned integers. This is important to avoid integer overflow when 4739 // we cast them to 'unsigned'. 4740 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 4741 unsigned StrLen = (((unsigned) StrLenPtr[0]) 4742 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 4743 IdentifiersLoaded[ID] 4744 = &PP.getIdentifierTable().get(StringRef(Str, StrLen)); 4745 if (DeserializationListener) 4746 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 4747 } 4748 4749 return IdentifiersLoaded[ID]; 4750} 4751 4752IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) { 4753 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 4754} 4755 4756IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) { 4757 if (LocalID < NUM_PREDEF_IDENT_IDS) 4758 return LocalID; 4759 4760 ContinuousRangeMap<uint32_t, int, 2>::iterator I 4761 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 4762 assert(I != M.IdentifierRemap.end() 4763 && "Invalid index into identifier index remap"); 4764 4765 return LocalID + I->second; 4766} 4767 4768bool ASTReader::ReadSLocEntry(int ID) { 4769 return ReadSLocEntryRecord(ID) != Success; 4770} 4771 4772Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) { 4773 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 4774} 4775 4776Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 4777 if (ID == 0) 4778 return Selector(); 4779 4780 if (ID > SelectorsLoaded.size()) { 4781 Error("selector ID out of range in AST file"); 4782 return Selector(); 4783 } 4784 4785 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 4786 // Load this selector from the selector table. 4787 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 4788 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 4789 Module &M = *I->second; 4790 ASTSelectorLookupTrait Trait(*this, M); 4791 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 4792 SelectorsLoaded[ID - 1] = 4793 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 4794 if (DeserializationListener) 4795 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 4796 } 4797 4798 return SelectorsLoaded[ID - 1]; 4799} 4800 4801Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 4802 return DecodeSelector(ID); 4803} 4804 4805uint32_t ASTReader::GetNumExternalSelectors() { 4806 // ID 0 (the null selector) is considered an external selector. 4807 return getTotalNumSelectors() + 1; 4808} 4809 4810serialization::SelectorID 4811ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const { 4812 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 4813 return LocalID; 4814 4815 ContinuousRangeMap<uint32_t, int, 2>::iterator I 4816 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 4817 assert(I != M.SelectorRemap.end() 4818 && "Invalid index into identifier index remap"); 4819 4820 return LocalID + I->second; 4821} 4822 4823DeclarationName 4824ASTReader::ReadDeclarationName(Module &F, 4825 const RecordData &Record, unsigned &Idx) { 4826 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 4827 switch (Kind) { 4828 case DeclarationName::Identifier: 4829 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 4830 4831 case DeclarationName::ObjCZeroArgSelector: 4832 case DeclarationName::ObjCOneArgSelector: 4833 case DeclarationName::ObjCMultiArgSelector: 4834 return DeclarationName(ReadSelector(F, Record, Idx)); 4835 4836 case DeclarationName::CXXConstructorName: 4837 return Context.DeclarationNames.getCXXConstructorName( 4838 Context.getCanonicalType(readType(F, Record, Idx))); 4839 4840 case DeclarationName::CXXDestructorName: 4841 return Context.DeclarationNames.getCXXDestructorName( 4842 Context.getCanonicalType(readType(F, Record, Idx))); 4843 4844 case DeclarationName::CXXConversionFunctionName: 4845 return Context.DeclarationNames.getCXXConversionFunctionName( 4846 Context.getCanonicalType(readType(F, Record, Idx))); 4847 4848 case DeclarationName::CXXOperatorName: 4849 return Context.DeclarationNames.getCXXOperatorName( 4850 (OverloadedOperatorKind)Record[Idx++]); 4851 4852 case DeclarationName::CXXLiteralOperatorName: 4853 return Context.DeclarationNames.getCXXLiteralOperatorName( 4854 GetIdentifierInfo(F, Record, Idx)); 4855 4856 case DeclarationName::CXXUsingDirective: 4857 return DeclarationName::getUsingDirectiveName(); 4858 } 4859 4860 // Required to silence GCC warning 4861 return DeclarationName(); 4862} 4863 4864void ASTReader::ReadDeclarationNameLoc(Module &F, 4865 DeclarationNameLoc &DNLoc, 4866 DeclarationName Name, 4867 const RecordData &Record, unsigned &Idx) { 4868 switch (Name.getNameKind()) { 4869 case DeclarationName::CXXConstructorName: 4870 case DeclarationName::CXXDestructorName: 4871 case DeclarationName::CXXConversionFunctionName: 4872 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 4873 break; 4874 4875 case DeclarationName::CXXOperatorName: 4876 DNLoc.CXXOperatorName.BeginOpNameLoc 4877 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4878 DNLoc.CXXOperatorName.EndOpNameLoc 4879 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4880 break; 4881 4882 case DeclarationName::CXXLiteralOperatorName: 4883 DNLoc.CXXLiteralOperatorName.OpNameLoc 4884 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4885 break; 4886 4887 case DeclarationName::Identifier: 4888 case DeclarationName::ObjCZeroArgSelector: 4889 case DeclarationName::ObjCOneArgSelector: 4890 case DeclarationName::ObjCMultiArgSelector: 4891 case DeclarationName::CXXUsingDirective: 4892 break; 4893 } 4894} 4895 4896void ASTReader::ReadDeclarationNameInfo(Module &F, 4897 DeclarationNameInfo &NameInfo, 4898 const RecordData &Record, unsigned &Idx) { 4899 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 4900 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 4901 DeclarationNameLoc DNLoc; 4902 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 4903 NameInfo.setInfo(DNLoc); 4904} 4905 4906void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info, 4907 const RecordData &Record, unsigned &Idx) { 4908 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 4909 unsigned NumTPLists = Record[Idx++]; 4910 Info.NumTemplParamLists = NumTPLists; 4911 if (NumTPLists) { 4912 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 4913 for (unsigned i=0; i != NumTPLists; ++i) 4914 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 4915 } 4916} 4917 4918TemplateName 4919ASTReader::ReadTemplateName(Module &F, const RecordData &Record, 4920 unsigned &Idx) { 4921 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 4922 switch (Kind) { 4923 case TemplateName::Template: 4924 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 4925 4926 case TemplateName::OverloadedTemplate: { 4927 unsigned size = Record[Idx++]; 4928 UnresolvedSet<8> Decls; 4929 while (size--) 4930 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 4931 4932 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 4933 } 4934 4935 case TemplateName::QualifiedTemplate: { 4936 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 4937 bool hasTemplKeyword = Record[Idx++]; 4938 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 4939 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 4940 } 4941 4942 case TemplateName::DependentTemplate: { 4943 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 4944 if (Record[Idx++]) // isIdentifier 4945 return Context.getDependentTemplateName(NNS, 4946 GetIdentifierInfo(F, Record, 4947 Idx)); 4948 return Context.getDependentTemplateName(NNS, 4949 (OverloadedOperatorKind)Record[Idx++]); 4950 } 4951 4952 case TemplateName::SubstTemplateTemplateParm: { 4953 TemplateTemplateParmDecl *param 4954 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 4955 if (!param) return TemplateName(); 4956 TemplateName replacement = ReadTemplateName(F, Record, Idx); 4957 return Context.getSubstTemplateTemplateParm(param, replacement); 4958 } 4959 4960 case TemplateName::SubstTemplateTemplateParmPack: { 4961 TemplateTemplateParmDecl *Param 4962 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 4963 if (!Param) 4964 return TemplateName(); 4965 4966 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 4967 if (ArgPack.getKind() != TemplateArgument::Pack) 4968 return TemplateName(); 4969 4970 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 4971 } 4972 } 4973 4974 assert(0 && "Unhandled template name kind!"); 4975 return TemplateName(); 4976} 4977 4978TemplateArgument 4979ASTReader::ReadTemplateArgument(Module &F, 4980 const RecordData &Record, unsigned &Idx) { 4981 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 4982 switch (Kind) { 4983 case TemplateArgument::Null: 4984 return TemplateArgument(); 4985 case TemplateArgument::Type: 4986 return TemplateArgument(readType(F, Record, Idx)); 4987 case TemplateArgument::Declaration: 4988 return TemplateArgument(ReadDecl(F, Record, Idx)); 4989 case TemplateArgument::Integral: { 4990 llvm::APSInt Value = ReadAPSInt(Record, Idx); 4991 QualType T = readType(F, Record, Idx); 4992 return TemplateArgument(Value, T); 4993 } 4994 case TemplateArgument::Template: 4995 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 4996 case TemplateArgument::TemplateExpansion: { 4997 TemplateName Name = ReadTemplateName(F, Record, Idx); 4998 llvm::Optional<unsigned> NumTemplateExpansions; 4999 if (unsigned NumExpansions = Record[Idx++]) 5000 NumTemplateExpansions = NumExpansions - 1; 5001 return TemplateArgument(Name, NumTemplateExpansions); 5002 } 5003 case TemplateArgument::Expression: 5004 return TemplateArgument(ReadExpr(F)); 5005 case TemplateArgument::Pack: { 5006 unsigned NumArgs = Record[Idx++]; 5007 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 5008 for (unsigned I = 0; I != NumArgs; ++I) 5009 Args[I] = ReadTemplateArgument(F, Record, Idx); 5010 return TemplateArgument(Args, NumArgs); 5011 } 5012 } 5013 5014 assert(0 && "Unhandled template argument kind!"); 5015 return TemplateArgument(); 5016} 5017 5018TemplateParameterList * 5019ASTReader::ReadTemplateParameterList(Module &F, 5020 const RecordData &Record, unsigned &Idx) { 5021 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 5022 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 5023 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 5024 5025 unsigned NumParams = Record[Idx++]; 5026 SmallVector<NamedDecl *, 16> Params; 5027 Params.reserve(NumParams); 5028 while (NumParams--) 5029 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 5030 5031 TemplateParameterList* TemplateParams = 5032 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 5033 Params.data(), Params.size(), RAngleLoc); 5034 return TemplateParams; 5035} 5036 5037void 5038ASTReader:: 5039ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 5040 Module &F, const RecordData &Record, 5041 unsigned &Idx) { 5042 unsigned NumTemplateArgs = Record[Idx++]; 5043 TemplArgs.reserve(NumTemplateArgs); 5044 while (NumTemplateArgs--) 5045 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 5046} 5047 5048/// \brief Read a UnresolvedSet structure. 5049void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set, 5050 const RecordData &Record, unsigned &Idx) { 5051 unsigned NumDecls = Record[Idx++]; 5052 while (NumDecls--) { 5053 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx); 5054 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 5055 Set.addDecl(D, AS); 5056 } 5057} 5058 5059CXXBaseSpecifier 5060ASTReader::ReadCXXBaseSpecifier(Module &F, 5061 const RecordData &Record, unsigned &Idx) { 5062 bool isVirtual = static_cast<bool>(Record[Idx++]); 5063 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 5064 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 5065 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 5066 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 5067 SourceRange Range = ReadSourceRange(F, Record, Idx); 5068 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 5069 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 5070 EllipsisLoc); 5071 Result.setInheritConstructors(inheritConstructors); 5072 return Result; 5073} 5074 5075std::pair<CXXCtorInitializer **, unsigned> 5076ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record, 5077 unsigned &Idx) { 5078 CXXCtorInitializer **CtorInitializers = 0; 5079 unsigned NumInitializers = Record[Idx++]; 5080 if (NumInitializers) { 5081 CtorInitializers 5082 = new (Context) CXXCtorInitializer*[NumInitializers]; 5083 for (unsigned i=0; i != NumInitializers; ++i) { 5084 TypeSourceInfo *BaseClassInfo = 0; 5085 bool IsBaseVirtual = false; 5086 FieldDecl *Member = 0; 5087 IndirectFieldDecl *IndirectMember = 0; 5088 CXXConstructorDecl *Target = 0; 5089 5090 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 5091 switch (Type) { 5092 case CTOR_INITIALIZER_BASE: 5093 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx); 5094 IsBaseVirtual = Record[Idx++]; 5095 break; 5096 5097 case CTOR_INITIALIZER_DELEGATING: 5098 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx); 5099 break; 5100 5101 case CTOR_INITIALIZER_MEMBER: 5102 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 5103 break; 5104 5105 case CTOR_INITIALIZER_INDIRECT_MEMBER: 5106 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 5107 break; 5108 } 5109 5110 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 5111 Expr *Init = ReadExpr(F); 5112 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 5113 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 5114 bool IsWritten = Record[Idx++]; 5115 unsigned SourceOrderOrNumArrayIndices; 5116 SmallVector<VarDecl *, 8> Indices; 5117 if (IsWritten) { 5118 SourceOrderOrNumArrayIndices = Record[Idx++]; 5119 } else { 5120 SourceOrderOrNumArrayIndices = Record[Idx++]; 5121 Indices.reserve(SourceOrderOrNumArrayIndices); 5122 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 5123 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 5124 } 5125 5126 CXXCtorInitializer *BOMInit; 5127 if (Type == CTOR_INITIALIZER_BASE) { 5128 BOMInit = new (Context) CXXCtorInitializer(Context, BaseClassInfo, IsBaseVirtual, 5129 LParenLoc, Init, RParenLoc, 5130 MemberOrEllipsisLoc); 5131 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 5132 BOMInit = new (Context) CXXCtorInitializer(Context, MemberOrEllipsisLoc, LParenLoc, 5133 Target, Init, RParenLoc); 5134 } else if (IsWritten) { 5135 if (Member) 5136 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, 5137 LParenLoc, Init, RParenLoc); 5138 else 5139 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember, 5140 MemberOrEllipsisLoc, LParenLoc, 5141 Init, RParenLoc); 5142 } else { 5143 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc, 5144 LParenLoc, Init, RParenLoc, 5145 Indices.data(), Indices.size()); 5146 } 5147 5148 if (IsWritten) 5149 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 5150 CtorInitializers[i] = BOMInit; 5151 } 5152 } 5153 5154 return std::make_pair(CtorInitializers, NumInitializers); 5155} 5156 5157NestedNameSpecifier * 5158ASTReader::ReadNestedNameSpecifier(Module &F, 5159 const RecordData &Record, unsigned &Idx) { 5160 unsigned N = Record[Idx++]; 5161 NestedNameSpecifier *NNS = 0, *Prev = 0; 5162 for (unsigned I = 0; I != N; ++I) { 5163 NestedNameSpecifier::SpecifierKind Kind 5164 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 5165 switch (Kind) { 5166 case NestedNameSpecifier::Identifier: { 5167 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 5168 NNS = NestedNameSpecifier::Create(Context, Prev, II); 5169 break; 5170 } 5171 5172 case NestedNameSpecifier::Namespace: { 5173 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 5174 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 5175 break; 5176 } 5177 5178 case NestedNameSpecifier::NamespaceAlias: { 5179 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 5180 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 5181 break; 5182 } 5183 5184 case NestedNameSpecifier::TypeSpec: 5185 case NestedNameSpecifier::TypeSpecWithTemplate: { 5186 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 5187 if (!T) 5188 return 0; 5189 5190 bool Template = Record[Idx++]; 5191 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 5192 break; 5193 } 5194 5195 case NestedNameSpecifier::Global: { 5196 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 5197 // No associated value, and there can't be a prefix. 5198 break; 5199 } 5200 } 5201 Prev = NNS; 5202 } 5203 return NNS; 5204} 5205 5206NestedNameSpecifierLoc 5207ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record, 5208 unsigned &Idx) { 5209 unsigned N = Record[Idx++]; 5210 NestedNameSpecifierLocBuilder Builder; 5211 for (unsigned I = 0; I != N; ++I) { 5212 NestedNameSpecifier::SpecifierKind Kind 5213 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 5214 switch (Kind) { 5215 case NestedNameSpecifier::Identifier: { 5216 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 5217 SourceRange Range = ReadSourceRange(F, Record, Idx); 5218 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 5219 break; 5220 } 5221 5222 case NestedNameSpecifier::Namespace: { 5223 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 5224 SourceRange Range = ReadSourceRange(F, Record, Idx); 5225 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 5226 break; 5227 } 5228 5229 case NestedNameSpecifier::NamespaceAlias: { 5230 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 5231 SourceRange Range = ReadSourceRange(F, Record, Idx); 5232 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 5233 break; 5234 } 5235 5236 case NestedNameSpecifier::TypeSpec: 5237 case NestedNameSpecifier::TypeSpecWithTemplate: { 5238 bool Template = Record[Idx++]; 5239 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 5240 if (!T) 5241 return NestedNameSpecifierLoc(); 5242 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 5243 5244 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 5245 Builder.Extend(Context, 5246 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 5247 T->getTypeLoc(), ColonColonLoc); 5248 break; 5249 } 5250 5251 case NestedNameSpecifier::Global: { 5252 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 5253 Builder.MakeGlobal(Context, ColonColonLoc); 5254 break; 5255 } 5256 } 5257 } 5258 5259 return Builder.getWithLocInContext(Context); 5260} 5261 5262SourceRange 5263ASTReader::ReadSourceRange(Module &F, const RecordData &Record, 5264 unsigned &Idx) { 5265 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 5266 SourceLocation end = ReadSourceLocation(F, Record, Idx); 5267 return SourceRange(beg, end); 5268} 5269 5270/// \brief Read an integral value 5271llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 5272 unsigned BitWidth = Record[Idx++]; 5273 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 5274 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 5275 Idx += NumWords; 5276 return Result; 5277} 5278 5279/// \brief Read a signed integral value 5280llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 5281 bool isUnsigned = Record[Idx++]; 5282 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 5283} 5284 5285/// \brief Read a floating-point value 5286llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 5287 return llvm::APFloat(ReadAPInt(Record, Idx)); 5288} 5289 5290// \brief Read a string 5291std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 5292 unsigned Len = Record[Idx++]; 5293 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 5294 Idx += Len; 5295 return Result; 5296} 5297 5298VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 5299 unsigned &Idx) { 5300 unsigned Major = Record[Idx++]; 5301 unsigned Minor = Record[Idx++]; 5302 unsigned Subminor = Record[Idx++]; 5303 if (Minor == 0) 5304 return VersionTuple(Major); 5305 if (Subminor == 0) 5306 return VersionTuple(Major, Minor - 1); 5307 return VersionTuple(Major, Minor - 1, Subminor - 1); 5308} 5309 5310CXXTemporary *ASTReader::ReadCXXTemporary(Module &F, 5311 const RecordData &Record, 5312 unsigned &Idx) { 5313 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 5314 return CXXTemporary::Create(Context, Decl); 5315} 5316 5317DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 5318 return Diag(SourceLocation(), DiagID); 5319} 5320 5321DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 5322 return Diags.Report(Loc, DiagID); 5323} 5324 5325/// \brief Retrieve the identifier table associated with the 5326/// preprocessor. 5327IdentifierTable &ASTReader::getIdentifierTable() { 5328 return PP.getIdentifierTable(); 5329} 5330 5331/// \brief Record that the given ID maps to the given switch-case 5332/// statement. 5333void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 5334 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); 5335 SwitchCaseStmts[ID] = SC; 5336} 5337 5338/// \brief Retrieve the switch-case statement with the given ID. 5339SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 5340 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); 5341 return SwitchCaseStmts[ID]; 5342} 5343 5344void ASTReader::ClearSwitchCaseIDs() { 5345 SwitchCaseStmts.clear(); 5346} 5347 5348void ASTReader::FinishedDeserializing() { 5349 assert(NumCurrentElementsDeserializing && 5350 "FinishedDeserializing not paired with StartedDeserializing"); 5351 if (NumCurrentElementsDeserializing == 1) { 5352 // If any identifiers with corresponding top-level declarations have 5353 // been loaded, load those declarations now. 5354 while (!PendingIdentifierInfos.empty()) { 5355 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 5356 PendingIdentifierInfos.front().DeclIDs, true); 5357 PendingIdentifierInfos.pop_front(); 5358 } 5359 5360 // Ready to load previous declarations of Decls that were delayed. 5361 while (!PendingPreviousDecls.empty()) { 5362 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first, 5363 PendingPreviousDecls.front().second); 5364 PendingPreviousDecls.pop_front(); 5365 } 5366 5367 // We are not in recursive loading, so it's safe to pass the "interesting" 5368 // decls to the consumer. 5369 if (Consumer) 5370 PassInterestingDeclsToConsumer(); 5371 5372 assert(PendingForwardRefs.size() == 0 && 5373 "Some forward refs did not get linked to the definition!"); 5374 } 5375 --NumCurrentElementsDeserializing; 5376} 5377 5378ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, 5379 StringRef isysroot, bool DisableValidation, 5380 bool DisableStatCache) 5381 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 5382 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 5383 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context), 5384 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()), 5385 RelocatablePCH(false), isysroot(isysroot), 5386 DisableValidation(DisableValidation), 5387 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0), 5388 NumSLocEntriesRead(0), TotalNumSLocEntries(0), 5389 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), 5390 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0), 5391 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0), 5392 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 5393 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 5394 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 5395 NumCXXBaseSpecifiersLoaded(0) 5396{ 5397 SourceMgr.setExternalSLocEntrySource(this); 5398} 5399 5400ASTReader::~ASTReader() { 5401 for (DeclContextVisibleUpdatesPending::iterator 5402 I = PendingVisibleUpdates.begin(), 5403 E = PendingVisibleUpdates.end(); 5404 I != E; ++I) { 5405 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 5406 F = I->second.end(); 5407 J != F; ++J) 5408 delete static_cast<ASTDeclContextNameLookupTable*>(J->first); 5409 } 5410} 5411