198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===--- Module.cpp - Module description ------------------------*- C++ -*-===//
298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//                     The LLVM Compiler Infrastructure
498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// This file is distributed under the University of Illinois Open Source
698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// License. See LICENSE.TXT for details.
798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===----------------------------------------------------------------------===//
998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
1098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//  This file implements the Module class, which describes a module that has
1198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//  been loaded from an AST file.
1298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
1398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===----------------------------------------------------------------------===//
1498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "clang/Serialization/Module.h"
1598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "ASTReaderInternals.h"
1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/MemoryBuffer.h"
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
1898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
1998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorusing namespace clang;
2098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorusing namespace serialization;
2198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorusing namespace reader;
2298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
23057df20b3107cef764052d271c89b8591b98b3ceDouglas GregorModuleFile::ModuleFile(ModuleKind Kind, unsigned Generation)
24176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  : Kind(Kind), File(nullptr), Signature(0), DirectlyImported(false),
25d64c26f6676eef69d1713f353ca8a3c2fe963f17Argyrios Kyrtzidis    Generation(Generation), SizeInBits(0),
2698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    LocalNumSLocEntries(0), SLocEntryBaseID(0),
276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SLocEntryBaseOffset(0), SLocEntryOffsets(nullptr),
28745e6f168d0276c15fb72f3d90e3d93d60282b1bDouglas Gregor    LocalNumIdentifiers(0),
296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IdentifierOffsets(nullptr), BaseIdentifierID(0),
306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IdentifierTableData(nullptr), IdentifierLookupTable(nullptr),
316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LocalNumMacros(0), MacroOffsets(nullptr),
32a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor    BasePreprocessedEntityID(0),
336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    PreprocessedEntityOffsets(nullptr), NumPreprocessedEntities(0),
34e24692b30adbe8144597678a0e3354912e99c747Argyrios Kyrtzidis    LocalNumHeaderFileInfos(0),
356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    HeaderFileInfoTableData(nullptr), HeaderFileInfoTable(nullptr),
3636592b1fa381a978b1b6d4b97c66fb274915fe50Argyrios Kyrtzidis    LocalNumSubmodules(0), BaseSubmoduleID(0),
376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LocalNumSelectors(0), SelectorOffsets(nullptr), BaseSelectorID(0),
386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SelectorLookupTableData(nullptr), SelectorLookupTable(nullptr),
396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LocalNumDecls(0), DeclOffsets(nullptr), BaseDeclID(0),
406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(nullptr),
413ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar    LocalNumCXXCtorInitializers(0), CXXCtorInitializersOffsets(nullptr),
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FileSortedDecls(nullptr), NumFileSortedDecls(0),
436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    RedeclarationsMap(nullptr), LocalNumRedeclarationsInMap(0),
446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ObjCCategoriesMap(nullptr), LocalNumObjCCategoriesInMap(0),
456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LocalNumTypes(0), TypeOffsets(nullptr), BaseTypeIndex(0)
4698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor{}
4798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
481a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas GregorModuleFile::~ModuleFile() {
4998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  for (DeclContextInfosMap::iterator I = DeclContextInfos.begin(),
5098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor       E = DeclContextInfos.end();
5198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor       I != E; ++I) {
5298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    if (I->second.NameLookupTableData)
53b1758c662524e18d65d260188fdcbbdee6a9316bBenjamin Kramer      delete I->second.NameLookupTableData;
5498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  }
5598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
5698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
5798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
5898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}
6098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
6198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregortemplate<typename Key, typename Offset, unsigned InitialCapacity>
6298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorstatic void
6398339b96a8089a6da715487e432c5abfca0ca0dfDouglas GregordumpLocalRemap(StringRef Name,
6498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               const ContinuousRangeMap<Key, Offset, InitialCapacity> &Map) {
6598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  if (Map.begin() == Map.end())
6698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    return;
6798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
6898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  typedef ContinuousRangeMap<Key, Offset, InitialCapacity> MapType;
6998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  " << Name << ":\n";
7098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor       I != IEnd; ++I) {
7298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    llvm::errs() << "    " << I->first << " -> " << I->second << "\n";
7398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  }
7498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}
7598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
761a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregorvoid ModuleFile::dump() {
7798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "\nModule: " << FileName << "\n";
7898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  if (!Imports.empty()) {
7998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    llvm::errs() << "  Imports: ";
8098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
8198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor      if (I)
8298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor        llvm::errs() << ", ";
8398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor      llvm::errs() << Imports[I]->FileName;
8498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    }
8598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    llvm::errs() << "\n";
8698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  }
8798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
8898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  // Remapping tables.
8998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base source location offset: " << SLocEntryBaseOffset
9098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << '\n';
9198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Source location offset local -> global map", SLocRemap);
9298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
9398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base identifier ID: " << BaseIdentifierID << '\n'
9498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of identifiers: " << LocalNumIdentifiers << '\n';
9598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Identifier ID local -> global map", IdentifierRemap);
9626ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor
97a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor  llvm::errs() << "  Base macro ID: " << BaseMacroID << '\n'
98a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor               << "  Number of macros: " << LocalNumMacros << '\n';
99a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor  dumpLocalRemap("Macro ID local -> global map", MacroRemap);
100a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor
10126ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor  llvm::errs() << "  Base submodule ID: " << BaseSubmoduleID << '\n'
10226ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor               << "  Number of submodules: " << LocalNumSubmodules << '\n';
10326ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor  dumpLocalRemap("Submodule ID local -> global map", SubmoduleRemap);
10426ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor
10598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base selector ID: " << BaseSelectorID << '\n'
10698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of selectors: " << LocalNumSelectors << '\n';
10798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Selector ID local -> global map", SelectorRemap);
10898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
10998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base preprocessed entity ID: " << BasePreprocessedEntityID
11098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << '\n'
11198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of preprocessed entities: "
112e24692b30adbe8144597678a0e3354912e99c747Argyrios Kyrtzidis               << NumPreprocessedEntities << '\n';
11398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Preprocessed entity ID local -> global map",
11498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor                 PreprocessedEntityRemap);
11598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
11698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base type index: " << BaseTypeIndex << '\n'
11798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of types: " << LocalNumTypes << '\n';
11898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Type index local -> global map", TypeRemap);
11998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
12098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base decl ID: " << BaseDeclID << '\n'
12198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of decls: " << LocalNumDecls << '\n';
12298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Decl ID local -> global map", DeclRemap);
12398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}
124