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)
246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  : Kind(Kind), File(nullptr), 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),
416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FileSortedDecls(nullptr), NumFileSortedDecls(0),
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    RedeclarationsMap(nullptr), LocalNumRedeclarationsInMap(0),
436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ObjCCategoriesMap(nullptr), LocalNumObjCCategoriesInMap(0),
446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LocalNumTypes(0), TypeOffsets(nullptr), BaseTypeIndex(0)
4598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor{}
4698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
471a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas GregorModuleFile::~ModuleFile() {
4898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  for (DeclContextInfosMap::iterator I = DeclContextInfos.begin(),
4998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor       E = DeclContextInfos.end();
5098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor       I != E; ++I) {
5198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    if (I->second.NameLookupTableData)
52b1758c662524e18d65d260188fdcbbdee6a9316bBenjamin Kramer      delete I->second.NameLookupTableData;
5398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  }
5498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
5598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
5698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
5798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}
5998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
6098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregortemplate<typename Key, typename Offset, unsigned InitialCapacity>
6198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorstatic void
6298339b96a8089a6da715487e432c5abfca0ca0dfDouglas GregordumpLocalRemap(StringRef Name,
6398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               const ContinuousRangeMap<Key, Offset, InitialCapacity> &Map) {
6498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  if (Map.begin() == Map.end())
6598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    return;
6698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
6798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  typedef ContinuousRangeMap<Key, Offset, InitialCapacity> MapType;
6898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  " << Name << ":\n";
6998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor       I != IEnd; ++I) {
7198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    llvm::errs() << "    " << I->first << " -> " << I->second << "\n";
7298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  }
7398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}
7498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
751a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregorvoid ModuleFile::dump() {
7698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "\nModule: " << FileName << "\n";
7798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  if (!Imports.empty()) {
7898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    llvm::errs() << "  Imports: ";
7998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
8098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor      if (I)
8198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor        llvm::errs() << ", ";
8298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor      llvm::errs() << Imports[I]->FileName;
8398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    }
8498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor    llvm::errs() << "\n";
8598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  }
8698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
8798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  // Remapping tables.
8898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base source location offset: " << SLocEntryBaseOffset
8998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << '\n';
9098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Source location offset local -> global map", SLocRemap);
9198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
9298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base identifier ID: " << BaseIdentifierID << '\n'
9398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of identifiers: " << LocalNumIdentifiers << '\n';
9498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Identifier ID local -> global map", IdentifierRemap);
9526ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor
96a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor  llvm::errs() << "  Base macro ID: " << BaseMacroID << '\n'
97a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor               << "  Number of macros: " << LocalNumMacros << '\n';
98a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor  dumpLocalRemap("Macro ID local -> global map", MacroRemap);
99a8235d6c4093cd38dcf742909651f867de62e55bDouglas Gregor
10026ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor  llvm::errs() << "  Base submodule ID: " << BaseSubmoduleID << '\n'
10126ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor               << "  Number of submodules: " << LocalNumSubmodules << '\n';
10226ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor  dumpLocalRemap("Submodule ID local -> global map", SubmoduleRemap);
10326ced127b7d443fcf3472463c9f39c2376bd9d70Douglas Gregor
10498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base selector ID: " << BaseSelectorID << '\n'
10598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of selectors: " << LocalNumSelectors << '\n';
10698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Selector ID local -> global map", SelectorRemap);
10798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
10898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base preprocessed entity ID: " << BasePreprocessedEntityID
10998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << '\n'
11098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of preprocessed entities: "
111e24692b30adbe8144597678a0e3354912e99c747Argyrios Kyrtzidis               << NumPreprocessedEntities << '\n';
11298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Preprocessed entity ID local -> global map",
11398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor                 PreprocessedEntityRemap);
11498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
11598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base type index: " << BaseTypeIndex << '\n'
11698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of types: " << LocalNumTypes << '\n';
11798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Type index local -> global map", TypeRemap);
11898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
11998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::errs() << "  Base decl ID: " << BaseDeclID << '\n'
12098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor               << "  Number of decls: " << LocalNumDecls << '\n';
12198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  dumpLocalRemap("Decl ID local -> global map", DeclRemap);
12298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}
123