ModuleMap.h revision cfa88f893915ceb8ae4ce2f17c46c24a4d67502f
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/StringMap.h" 26#include "llvm/ADT/StringRef.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 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 /// \brief Describes whether we haved parsed a particular file as a module 115 /// map. 116 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap; 117 118 friend class ModuleMapParser; 119 120 /// \brief Resolve the given export declaration into an actual export 121 /// declaration. 122 /// 123 /// \param Mod The module in which we're resolving the export declaration. 124 /// 125 /// \param Unresolved The export declaration to resolve. 126 /// 127 /// \param Complain Whether this routine should complain about unresolvable 128 /// exports. 129 /// 130 /// \returns The resolved export declaration, which will have a NULL pointer 131 /// if the export could not be resolved. 132 Module::ExportDecl 133 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved, 134 bool Complain); 135 136public: 137 /// \brief Construct a new module map. 138 /// 139 /// \param FileMgr The file manager used to find module files and headers. 140 /// This file manager should be shared with the header-search mechanism, since 141 /// they will refer to the same headers. 142 /// 143 /// \param DC A diagnostic consumer that will be cloned for use in generating 144 /// diagnostics. 145 /// 146 /// \param LangOpts Language options for this translation unit. 147 /// 148 /// \param Target The target for this translation unit. 149 ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 150 const LangOptions &LangOpts, const TargetInfo *Target); 151 152 /// \brief Destroy the module map. 153 /// 154 ~ModuleMap(); 155 156 /// \brief Set the target information. 157 void setTarget(const TargetInfo &Target); 158 159 /// \brief Set the directory that contains Clang-supplied include 160 /// files, such as our stdarg.h or tgmath.h. 161 void setBuiltinIncludeDir(const DirectoryEntry *Dir) { 162 BuiltinIncludeDir = Dir; 163 } 164 165 /// \brief Retrieve the module that owns the given header file, if any. 166 /// 167 /// \param File The header file that is likely to be included. 168 /// 169 /// \returns The module that owns the given header file, or null to indicate 170 /// that no module owns this header file. 171 Module *findModuleForHeader(const FileEntry *File); 172 173 /// \brief Determine whether the given header is part of a module 174 /// marked 'unavailable'. 175 bool isHeaderInUnavailableModule(const FileEntry *Header); 176 177 /// \brief Retrieve a module with the given name. 178 /// 179 /// \param Name The name of the module to look up. 180 /// 181 /// \returns The named module, if known; otherwise, returns null. 182 Module *findModule(StringRef Name); 183 184 /// \brief Retrieve a module with the given name using lexical name lookup, 185 /// starting at the given context. 186 /// 187 /// \param Name The name of the module to look up. 188 /// 189 /// \param Context The module context, from which we will perform lexical 190 /// name lookup. 191 /// 192 /// \returns The named module, if known; otherwise, returns null. 193 Module *lookupModuleUnqualified(StringRef Name, Module *Context); 194 195 /// \brief Retrieve a module with the given name within the given context, 196 /// using direct (qualified) name lookup. 197 /// 198 /// \param Name The name of the module to look up. 199 /// 200 /// \param Context The module for which we will look for a submodule. If 201 /// null, we will look for a top-level module. 202 /// 203 /// \returns The named submodule, if known; otherwose, returns null. 204 Module *lookupModuleQualified(StringRef Name, Module *Context); 205 206 /// \brief Find a new module or submodule, or create it if it does not already 207 /// exist. 208 /// 209 /// \param Name The name of the module to find or create. 210 /// 211 /// \param Parent The module that will act as the parent of this submodule, 212 /// or NULL to indicate that this is a top-level module. 213 /// 214 /// \param IsFramework Whether this is a framework module. 215 /// 216 /// \param IsExplicit Whether this is an explicit submodule. 217 /// 218 /// \returns The found or newly-created module, along with a boolean value 219 /// that will be true if the module is newly-created. 220 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent, 221 bool IsFramework, 222 bool IsExplicit); 223 224 /// \brief Determine whether we can infer a framework module a framework 225 /// with the given name in the given 226 /// 227 /// \param ParentDir The directory that is the parent of the framework 228 /// directory. 229 /// 230 /// \param Name The name of the module. 231 /// 232 /// \param IsSystem Will be set to 'true' if the inferred module must be a 233 /// system module. 234 /// 235 /// \returns true if we are allowed to infer a framework module, and false 236 /// otherwise. 237 bool canInferFrameworkModule(const DirectoryEntry *ParentDir, 238 StringRef Name, bool &IsSystem); 239 240 /// \brief Infer the contents of a framework module map from the given 241 /// framework directory. 242 Module *inferFrameworkModule(StringRef ModuleName, 243 const DirectoryEntry *FrameworkDir, 244 bool IsSystem, Module *Parent); 245 246 /// \brief Retrieve the module map file containing the definition of the given 247 /// module. 248 /// 249 /// \param Module The module whose module map file will be returned, if known. 250 /// 251 /// \returns The file entry for the module map file containing the given 252 /// module, or NULL if the module definition was inferred. 253 const FileEntry *getContainingModuleMapFile(Module *Module); 254 255 /// \brief Resolve all of the unresolved exports in the given module. 256 /// 257 /// \param Mod The module whose exports should be resolved. 258 /// 259 /// \param Complain Whether to emit diagnostics for failures. 260 /// 261 /// \returns true if any errors were encountered while resolving exports, 262 /// false otherwise. 263 bool resolveExports(Module *Mod, bool Complain); 264 265 /// \brief Infers the (sub)module based on the given source location and 266 /// source manager. 267 /// 268 /// \param Loc The location within the source that we are querying, along 269 /// with its source manager. 270 /// 271 /// \returns The module that owns this source location, or null if no 272 /// module owns this source location. 273 Module *inferModuleFromLocation(FullSourceLoc Loc); 274 275 /// \brief Sets the umbrella header of the given module to the given 276 /// header. 277 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader); 278 279 /// \brief Sets the umbrella directory of the given module to the given 280 /// directory. 281 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir); 282 283 /// \brief Adds this header to the given module. 284 /// \param Excluded Whether this header is explicitly excluded from the 285 /// module; otherwise, it's included in the module. 286 void addHeader(Module *Mod, const FileEntry *Header, bool Excluded); 287 288 /// \brief Parse the given module map file, and record any modules we 289 /// encounter. 290 /// 291 /// \param File The file to be parsed. 292 /// 293 /// \returns true if an error occurred, false otherwise. 294 bool parseModuleMapFile(const FileEntry *File); 295 296 /// \brief Dump the contents of the module map, for debugging purposes. 297 void dump(); 298 299 typedef llvm::StringMap<Module *>::const_iterator module_iterator; 300 module_iterator module_begin() const { return Modules.begin(); } 301 module_iterator module_end() const { return Modules.end(); } 302}; 303 304} 305#endif 306