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