ModuleMap.h revision c5b2e58840748145d1706c1d1481369d1863fabf
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/Module.h" 21#include "clang/Basic/SourceManager.h" 22#include "llvm/ADT/DenseMap.h" 23#include "llvm/ADT/IntrusiveRefCntPtr.h" 24#include "llvm/ADT/SmallVector.h" 25#include "llvm/ADT/StringRef.h" 26#include "llvm/ADT/StringMap.h" 27#include <string> 28 29namespace clang { 30 31class DirectoryEntry; 32class FileEntry; 33class FileManager; 34class DiagnosticConsumer; 35class DiagnosticsEngine; 36class ModuleMapParser; 37 38class ModuleMap { 39 SourceManager *SourceMgr; 40 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; 41 const LangOptions &LangOpts; 42 43 /// \brief Language options used to parse the module map itself. 44 /// 45 /// These are always simple C language options. 46 LangOptions MMapLangOpts; 47 48 /// \brief The top-level modules that are known. 49 llvm::StringMap<Module *> Modules; 50 51 /// \brief Mapping from each header to the module that owns the contents of the 52 /// that header. 53 llvm::DenseMap<const FileEntry *, Module *> Headers; 54 55 /// \brief Mapping from directories with umbrella headers to the module 56 /// that is generated from the umbrella header. 57 /// 58 /// This mapping is used to map headers that haven't explicitly been named 59 /// in the module map over to the module that includes them via its umbrella 60 /// header. 61 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 62 63 friend class ModuleMapParser; 64 65 /// \brief Resolve the given export declaration into an actual export 66 /// declaration. 67 /// 68 /// \param Mod The module in which we're resolving the export declaration. 69 /// 70 /// \param Unresolved The export declaration to resolve. 71 /// 72 /// \param Complain Whether this routine should complain about unresolvable 73 /// exports. 74 /// 75 /// \returns The resolved export declaration, which will have a NULL pointer 76 /// if the export could not be resolved. 77 Module::ExportDecl 78 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved, 79 bool Complain); 80 81public: 82 /// \brief Construct a new module map. 83 /// 84 /// \param FileMgr The file manager used to find module files and headers. 85 /// This file manager should be shared with the header-search mechanism, since 86 /// they will refer to the same headers. 87 /// 88 /// \param DC A diagnostic consumer that will be cloned for use in generating 89 /// diagnostics. 90 /// 91 /// \param LangOpts Language options for this translation unit. 92 ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 93 const LangOptions &LangOpts); 94 95 /// \brief Destroy the module map. 96 /// 97 ~ModuleMap(); 98 99 /// \brief Retrieve the module that owns the given header file, if any. 100 /// 101 /// \param File The header file that is likely to be included. 102 /// 103 /// \returns The module that owns the given header file, or null to indicate 104 /// that no module owns this header file. 105 Module *findModuleForHeader(const FileEntry *File); 106 107 /// \brief Determine whether the given header is part of a module 108 /// marked 'unavailable'. 109 bool isHeaderInUnavailableModule(const FileEntry *Header); 110 111 /// \brief Retrieve a module with the given name. 112 /// 113 /// \param The name of the module to look up. 114 /// 115 /// \returns The named module, if known; otherwise, returns null. 116 Module *findModule(StringRef Name); 117 118 /// \brief Retrieve a module with the given name using lexical name lookup, 119 /// starting at the given context. 120 /// 121 /// \param The name of the module to look up. 122 /// 123 /// \param Context The module context, from which we will perform lexical 124 /// name lookup. 125 /// 126 /// \returns The named module, if known; otherwise, returns null. 127 Module *lookupModuleUnqualified(StringRef Name, Module *Context); 128 129 /// \brief Retrieve a module with the given name within the given context, 130 /// using direct (qualified) name lookup. 131 /// 132 /// \param The name of the module to look up. 133 /// 134 /// \param Context The module for which we will look for a submodule. If 135 /// null, we will look for a top-level module. 136 /// 137 /// \returns The named submodule, if known; otherwose, returns null. 138 Module *lookupModuleQualified(StringRef Name, Module *Context); 139 140 /// \brief Find a new module or submodule, or create it if it does not already 141 /// exist. 142 /// 143 /// \param Name The name of the module to find or create. 144 /// 145 /// \param Parent The module that will act as the parent of this submodule, 146 /// or NULL to indicate that this is a top-level module. 147 /// 148 /// \param IsFramework Whether this is a framework module. 149 /// 150 /// \param IsExplicit Whether this is an explicit submodule. 151 /// 152 /// \returns The found or newly-created module, along with a boolean value 153 /// that will be true if the module is newly-created. 154 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent, 155 bool IsFramework, 156 bool IsExplicit); 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 bool IsSystem, Module *Parent); 163 164 /// \brief Retrieve the module map file containing the definition of the given 165 /// module. 166 /// 167 /// \param Module The module whose module map file will be returned, if known. 168 /// 169 /// \returns The file entry for the module map file containing the given 170 /// module, or NULL if the module definition was inferred. 171 const FileEntry *getContainingModuleMapFile(Module *Module); 172 173 /// \brief Resolve all of the unresolved exports in the given module. 174 /// 175 /// \param Mod The module whose exports should be resolved. 176 /// 177 /// \param Complain Whether to emit diagnostics for failures. 178 /// 179 /// \returns true if any errors were encountered while resolving exports, 180 /// false otherwise. 181 bool resolveExports(Module *Mod, bool Complain); 182 183 /// \brief Infers the (sub)module based on the given source location and 184 /// source manager. 185 /// 186 /// \param Loc The location within the source that we are querying, along 187 /// with its source manager. 188 /// 189 /// \returns The module that owns this source location, or null if no 190 /// module owns this source location. 191 Module *inferModuleFromLocation(FullSourceLoc Loc); 192 193 /// \brief Sets the umbrella header of the given module to the given 194 /// header. 195 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader); 196 197 /// \brief Sets the umbrella directory of the given module to the given 198 /// directory. 199 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir); 200 201 /// \brief Adds this header to the given module. 202 void addHeader(Module *Mod, const FileEntry *Header); 203 204 /// \brief Parse the given module map file, and record any modules we 205 /// encounter. 206 /// 207 /// \param File The file to be parsed. 208 /// 209 /// \returns true if an error occurred, false otherwise. 210 bool parseModuleMapFile(const FileEntry *File); 211 212 /// \brief Dump the contents of the module map, for debugging purposes. 213 void dump(); 214 215 typedef llvm::StringMap<Module *>::const_iterator module_iterator; 216 module_iterator module_begin() const { return Modules.begin(); } 217 module_iterator module_end() const { return Modules.end(); } 218}; 219 220} 221#endif 222