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