ModuleMap.h revision a865405e4155e8ea83d7ff1a1d8316907c300897
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 a framework module. 63 bool IsFramework; 64 65 /// \brief Whether this is an explicit submodule. 66 bool IsExplicit; 67 68 /// \brief Construct a top-level module. 69 explicit Module(StringRef Name, SourceLocation DefinitionLoc, 70 bool IsFramework) 71 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), UmbrellaHeader(0), 72 IsFramework(IsFramework), IsExplicit(false) { } 73 74 /// \brief Construct a new module or submodule. 75 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 76 bool IsFramework, bool IsExplicit) 77 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), 78 UmbrellaHeader(0), IsFramework(IsFramework), IsExplicit(IsExplicit) { 79 } 80 81 ~Module(); 82 83 /// \brief Determine whether this module is a submodule. 84 bool isSubModule() const { return Parent != 0; } 85 86 /// \brief Determine whether this module is a part of a framework, 87 /// either because it is a framework module or because it is a submodule 88 /// of a framework module. 89 bool isPartOfFramework() const { 90 for (const Module *Mod = this; Mod; Mod = Mod->Parent) 91 if (Mod->IsFramework) 92 return true; 93 94 return false; 95 } 96 97 98 /// \brief Retrieve the full name of this module, including the path from 99 /// its top-level module. 100 std::string getFullModuleName() const; 101 102 /// \brief Retrieve the name of the top-level module. 103 StringRef getTopLevelModuleName() const; 104 }; 105 106private: 107 SourceManager *SourceMgr; 108 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; 109 LangOptions LangOpts; 110 111 /// \brief The top-level modules that are known. 112 llvm::StringMap<Module *> Modules; 113 114 /// \brief Mapping from each header to the module that owns the contents of the 115 /// that header. 116 llvm::DenseMap<const FileEntry *, Module *> Headers; 117 118 /// \brief Mapping from directories with umbrella headers to the module 119 /// that is generated from the umbrella header. 120 /// 121 /// This mapping is used to map headers that haven't explicitly been named 122 /// in the module map over to the module that includes them via its umbrella 123 /// header. 124 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 125 126 friend class ModuleMapParser; 127 128public: 129 /// \brief Construct a new module map. 130 /// 131 /// \param FileMgr The file manager used to find module files and headers. 132 /// This file manager should be shared with the header-search mechanism, since 133 /// they will refer to the same headers. 134 /// 135 /// \param DC A diagnostic consumer that will be cloned for use in generating 136 /// diagnostics. 137 ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC); 138 139 /// \brief Destroy the module map. 140 /// 141 ~ModuleMap(); 142 143 /// \brief Retrieve the module that owns the given header file, if any. 144 /// 145 /// \param File The header file that is likely to be included. 146 /// 147 /// \returns The module that owns the given header file, or null to indicate 148 /// that no module owns this header file. 149 Module *findModuleForHeader(const FileEntry *File); 150 151 /// \brief Retrieve a module with the given name. 152 /// 153 /// \param The name of the module to look up. 154 /// 155 /// \returns The named module, if known; otherwise, returns null. 156 Module *findModule(StringRef Name); 157 158 /// \brief Infer the contents of a framework module map from the given 159 /// framework directory. 160 Module *inferFrameworkModule(StringRef ModuleName, 161 const DirectoryEntry *FrameworkDir); 162 163 /// \brief Parse the given module map file, and record any modules we 164 /// encounter. 165 /// 166 /// \param File The file to be parsed. 167 /// 168 /// \returns true if an error occurred, false otherwise. 169 bool parseModuleMapFile(const FileEntry *File); 170 171 /// \brief Dump the contents of the module map, for debugging purposes. 172 void dump(); 173}; 174 175} 176#endif 177