ModuleMap.h revision 09fe1bb696847e6f1b482e5ac40029d53a2402df
1//===--- ModuleMap.h - Describe the layout of modules -----------*- C++ -*-===// 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 ModuleMap interface, which describes the layout of a 11// module as it relates to headers. 12// 13//===----------------------------------------------------------------------===// 14 15 16#ifndef LLVM_CLANG_LEX_MODULEMAP_H 17#define LLVM_CLANG_LEX_MODULEMAP_H 18 19#include "clang/Basic/LangOptions.h" 20#include "clang/Basic/SourceManager.h" 21#include "llvm/ADT/DenseMap.h" 22#include "llvm/ADT/IntrusiveRefCntPtr.h" 23#include "llvm/ADT/SmallVector.h" 24#include "llvm/ADT/StringRef.h" 25#include "llvm/ADT/StringMap.h" 26#include <string> 27 28namespace clang { 29 30class DirectoryEntry; 31class FileEntry; 32class FileManager; 33class DiagnosticConsumer; 34class DiagnosticsEngine; 35class ModuleMapParser; 36 37class ModuleMap { 38public: 39 /// \brief Describes a module or submodule. 40 struct Module { 41 /// \brief The name of this module. 42 std::string Name; 43 44 /// \brief The location of the module definition. 45 SourceLocation DefinitionLoc; 46 47 /// \brief The parent of this module. This will be NULL for the top-level 48 /// module. 49 Module *Parent; 50 51 /// \brief The umbrella header, if any. 52 /// 53 /// Only the top-level module can have an umbrella header. 54 const FileEntry *UmbrellaHeader; 55 56 /// \brief The submodules of this module, indexed by name. 57 llvm::StringMap<Module *> SubModules; 58 59 /// \brief The headers that are part of this module. 60 llvm::SmallVector<const FileEntry *, 2> Headers; 61 62 /// \brief Whether this is an explicit submodule. 63 bool IsExplicit; 64 65 /// \brief Construct a top-level module. 66 explicit Module(StringRef Name, SourceLocation DefinitionLoc) 67 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), UmbrellaHeader(0), 68 IsExplicit(false) { } 69 70 /// \brief Construct a new module or submodule. 71 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 72 bool IsExplicit) 73 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), 74 UmbrellaHeader(0), IsExplicit(IsExplicit) { 75 } 76 77 ~Module(); 78 79 /// \brief Determine whether this module is a submodule. 80 bool isSubModule() const { return Parent != 0; } 81 82 /// \brief Retrieve the full name of this module, including the path from 83 /// its top-level module. 84 std::string getFullModuleName() const; 85 86 /// \brief Retrieve the name of the top-level module. 87 StringRef getTopLevelModuleName() const; 88 }; 89 90private: 91 SourceManager *SourceMgr; 92 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; 93 LangOptions LangOpts; 94 95 /// \brief The top-level modules that are known. 96 llvm::StringMap<Module *> Modules; 97 98 /// \brief Mapping from each header to the module that owns the contents of the 99 /// that header. 100 llvm::DenseMap<const FileEntry *, Module *> Headers; 101 102 /// \brief Mapping from directories with umbrella headers to the module 103 /// that is generated from the umbrella header. 104 /// 105 /// This mapping is used to map headers that haven't explicitly been named 106 /// in the module map over to the module that includes them via its umbrella 107 /// header. 108 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 109 110 friend class ModuleMapParser; 111 112public: 113 /// \brief Construct a new module map. 114 /// 115 /// \param FileMgr The file manager used to find module files and headers. 116 /// This file manager should be shared with the header-search mechanism, since 117 /// they will refer to the same headers. 118 /// 119 /// \param DC A diagnostic consumer that will be cloned for use in generating 120 /// diagnostics. 121 ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC); 122 123 /// \brief Destroy the module map. 124 /// 125 ~ModuleMap(); 126 127 /// \brief Retrieve the module that owns the given header file, if any. 128 /// 129 /// \param File The header file that is likely to be included. 130 /// 131 /// \returns The module that owns the given header file, or null to indicate 132 /// that no module owns this header file. 133 Module *findModuleForHeader(const FileEntry *File); 134 135 /// \brief Retrieve a module with the given name. 136 /// 137 /// \param The name of the module to look up. 138 /// 139 /// \returns The named module, if known; otherwise, returns null. 140 Module *findModule(StringRef Name); 141 142 /// \brief Infer the contents of a framework module map from the given 143 /// framework directory. 144 Module *inferFrameworkModule(StringRef ModuleName, 145 const DirectoryEntry *FrameworkDir); 146 147 /// \brief Parse the given module map file, and record any modules we 148 /// encounter. 149 /// 150 /// \param File The file to be parsed. 151 /// 152 /// \returns true if an error occurred, false otherwise. 153 bool parseModuleMapFile(const FileEntry *File); 154 155 /// \brief Dump the contents of the module map, for debugging purposes. 156 void dump(); 157}; 158 159} 160#endif 161