ModuleMap.h revision 82e52377bd76ed71e8c09edc5f2f452e388b16ad
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 IntrusiveRefCntPtr<DiagnosticsEngine> Diags; 41 const LangOptions &LangOpts; 42 const TargetInfo *Target; 43 44 /// \brief The directory used for Clang-supplied, builtin include headers, 45 /// such as "stdint.h". 46 const DirectoryEntry *BuiltinIncludeDir; 47 48 /// \brief Language options used to parse the module map itself. 49 /// 50 /// These are always simple C language options. 51 LangOptions MMapLangOpts; 52 53 /// \brief The top-level modules that are known. 54 llvm::StringMap<Module *> Modules; 55 56 /// \brief A header that is known to reside within a given module, 57 /// whether it was included or excluded. 58 class KnownHeader { 59 llvm::PointerIntPair<Module *, 1, bool> Storage; 60 61 public: 62 KnownHeader() : Storage(0, false) { } 63 KnownHeader(Module *M, bool Excluded) : Storage(M, Excluded) { } 64 65 /// \brief Retrieve the module the header is stored in. 66 Module *getModule() const { return Storage.getPointer(); } 67 68 /// \brief Whether this header is explicitly excluded from the module. 69 bool isExcluded() const { return Storage.getInt(); } 70 71 /// \brief Whether this header is available in the module. 72 bool isAvailable() const { 73 return !isExcluded() && getModule()->isAvailable(); 74 } 75 76 // \brief Whether this known header is valid (i.e., it has an 77 // associated module). 78 operator bool() const { return Storage.getPointer() != 0; } 79 }; 80 81 typedef llvm::DenseMap<const FileEntry *, KnownHeader> HeadersMap; 82 83 /// \brief Mapping from each header to the module that owns the contents of the 84 /// that header. 85 HeadersMap Headers; 86 87 /// \brief Mapping from directories with umbrella headers to the module 88 /// that is generated from the umbrella header. 89 /// 90 /// This mapping is used to map headers that haven't explicitly been named 91 /// in the module map over to the module that includes them via its umbrella 92 /// header. 93 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 94 95 /// \brief A directory for which framework modules can be inferred. 96 struct InferredDirectory { 97 InferredDirectory() : InferModules(), InferSystemModules() { } 98 99 /// \brief Whether to infer modules from this directory. 100 unsigned InferModules : 1; 101 102 /// \brief Whether the modules we infer are [system] modules. 103 unsigned InferSystemModules : 1; 104 105 /// \brief The names of modules that cannot be inferred within this 106 /// directory. 107 llvm::SmallVector<std::string, 2> ExcludedModules; 108 }; 109 110 /// \brief A mapping from directories to information about inferring 111 /// framework modules from within those directories. 112 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories; 113 114 friend class ModuleMapParser; 115 116 /// \brief Resolve the given export declaration into an actual export 117 /// declaration. 118 /// 119 /// \param Mod The module in which we're resolving the export declaration. 120 /// 121 /// \param Unresolved The export declaration to resolve. 122 /// 123 /// \param Complain Whether this routine should complain about unresolvable 124 /// exports. 125 /// 126 /// \returns The resolved export declaration, which will have a NULL pointer 127 /// if the export could not be resolved. 128 Module::ExportDecl 129 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved, 130 bool Complain); 131 132public: 133 /// \brief Construct a new module map. 134 /// 135 /// \param FileMgr The file manager used to find module files and headers. 136 /// This file manager should be shared with the header-search mechanism, since 137 /// they will refer to the same headers. 138 /// 139 /// \param DC A diagnostic consumer that will be cloned for use in generating 140 /// diagnostics. 141 /// 142 /// \param LangOpts Language options for this translation unit. 143 /// 144 /// \param Target The target for this translation unit. 145 ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 146 const LangOptions &LangOpts, const TargetInfo *Target); 147 148 /// \brief Destroy the module map. 149 /// 150 ~ModuleMap(); 151 152 /// \brief Set the target information. 153 void setTarget(const TargetInfo &Target); 154 155 /// \brief Set the directory that contains Clang-supplied include 156 /// files, such as our stdarg.h or tgmath.h. 157 void setBuiltinIncludeDir(const DirectoryEntry *Dir) { 158 BuiltinIncludeDir = Dir; 159 } 160 161 /// \brief Retrieve the module that owns the given header file, if any. 162 /// 163 /// \param File The header file that is likely to be included. 164 /// 165 /// \returns The module that owns the given header file, or null to indicate 166 /// that no module owns this header file. 167 Module *findModuleForHeader(const FileEntry *File); 168 169 /// \brief Determine whether the given header is part of a module 170 /// marked 'unavailable'. 171 bool isHeaderInUnavailableModule(const FileEntry *Header); 172 173 /// \brief Retrieve a module with the given name. 174 /// 175 /// \param Name The name of the module to look up. 176 /// 177 /// \returns The named module, if known; otherwise, returns null. 178 Module *findModule(StringRef Name); 179 180 /// \brief Retrieve a module with the given name using lexical name lookup, 181 /// starting at the given context. 182 /// 183 /// \param Name The name of the module to look up. 184 /// 185 /// \param Context The module context, from which we will perform lexical 186 /// name lookup. 187 /// 188 /// \returns The named module, if known; otherwise, returns null. 189 Module *lookupModuleUnqualified(StringRef Name, Module *Context); 190 191 /// \brief Retrieve a module with the given name within the given context, 192 /// using direct (qualified) name lookup. 193 /// 194 /// \param Name The name of the module to look up. 195 /// 196 /// \param Context The module for which we will look for a submodule. If 197 /// null, we will look for a top-level module. 198 /// 199 /// \returns The named submodule, if known; otherwose, returns null. 200 Module *lookupModuleQualified(StringRef Name, Module *Context); 201 202 /// \brief Find a new module or submodule, or create it if it does not already 203 /// exist. 204 /// 205 /// \param Name The name of the module to find or create. 206 /// 207 /// \param Parent The module that will act as the parent of this submodule, 208 /// or NULL to indicate that this is a top-level module. 209 /// 210 /// \param IsFramework Whether this is a framework module. 211 /// 212 /// \param IsExplicit Whether this is an explicit submodule. 213 /// 214 /// \returns The found or newly-created module, along with a boolean value 215 /// that will be true if the module is newly-created. 216 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent, 217 bool IsFramework, 218 bool IsExplicit); 219 220 /// \brief Determine whether we can infer a framework module a framework 221 /// with the given name in the given 222 /// 223 /// \param ParentDir The directory that is the parent of the framework 224 /// directory. 225 /// 226 /// \param Name The name of the module. 227 /// 228 /// \param IsSystem Will be set to 'true' if the inferred module must be a 229 /// system module. 230 /// 231 /// \returns true if we are allowed to infer a framework module, and false 232 /// otherwise. 233 bool canInferFrameworkModule(const DirectoryEntry *ParentDir, 234 StringRef Name, bool &IsSystem); 235 236 /// \brief Infer the contents of a framework module map from the given 237 /// framework directory. 238 Module *inferFrameworkModule(StringRef ModuleName, 239 const DirectoryEntry *FrameworkDir, 240 bool IsSystem, Module *Parent); 241 242 /// \brief Retrieve the module map file containing the definition of the given 243 /// module. 244 /// 245 /// \param Module The module whose module map file will be returned, if known. 246 /// 247 /// \returns The file entry for the module map file containing the given 248 /// module, or NULL if the module definition was inferred. 249 const FileEntry *getContainingModuleMapFile(Module *Module); 250 251 /// \brief Resolve all of the unresolved exports in the given module. 252 /// 253 /// \param Mod The module whose exports should be resolved. 254 /// 255 /// \param Complain Whether to emit diagnostics for failures. 256 /// 257 /// \returns true if any errors were encountered while resolving exports, 258 /// false otherwise. 259 bool resolveExports(Module *Mod, bool Complain); 260 261 /// \brief Infers the (sub)module based on the given source location and 262 /// source manager. 263 /// 264 /// \param Loc The location within the source that we are querying, along 265 /// with its source manager. 266 /// 267 /// \returns The module that owns this source location, or null if no 268 /// module owns this source location. 269 Module *inferModuleFromLocation(FullSourceLoc Loc); 270 271 /// \brief Sets the umbrella header of the given module to the given 272 /// header. 273 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader); 274 275 /// \brief Sets the umbrella directory of the given module to the given 276 /// directory. 277 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir); 278 279 /// \brief Adds this header to the given module. 280 /// \param Excluded Whether this header is explicitly excluded from the 281 /// module; otherwise, it's included in the module. 282 void addHeader(Module *Mod, const FileEntry *Header, bool Excluded); 283 284 /// \brief Parse the given module map file, and record any modules we 285 /// encounter. 286 /// 287 /// \param File The file to be parsed. 288 /// 289 /// \returns true if an error occurred, false otherwise. 290 bool parseModuleMapFile(const FileEntry *File); 291 292 /// \brief Dump the contents of the module map, for debugging purposes. 293 void dump(); 294 295 typedef llvm::StringMap<Module *>::const_iterator module_iterator; 296 module_iterator module_begin() const { return Modules.begin(); } 297 module_iterator module_end() const { return Modules.end(); } 298}; 299 300} 301#endif 302