ModuleMap.h revision 651f13cea278ec967336033dd032faef0e9fc2ec
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 HeaderSearch; 37class ModuleMapParser; 38 39class ModuleMap { 40 SourceManager &SourceMgr; 41 DiagnosticsEngine &Diags; 42 const LangOptions &LangOpts; 43 const TargetInfo *Target; 44 HeaderSearch &HeaderInfo; 45 46 /// \brief The directory used for Clang-supplied, builtin include headers, 47 /// such as "stdint.h". 48 const DirectoryEntry *BuiltinIncludeDir; 49 50 /// \brief Language options used to parse the module map itself. 51 /// 52 /// These are always simple C language options. 53 LangOptions MMapLangOpts; 54 55 // The module that we are building; related to \c LangOptions::CurrentModule. 56 Module *CompilingModule; 57 58public: 59 // The module that the .cc source file is associated with. 60 Module *SourceModule; 61 std::string SourceModuleName; 62 63private: 64 /// \brief The top-level modules that are known. 65 llvm::StringMap<Module *> Modules; 66 67public: 68 /// \brief Describes the role of a module header. 69 enum ModuleHeaderRole { 70 /// \brief This header is normally included in the module. 71 NormalHeader, 72 /// \brief This header is included but private. 73 PrivateHeader, 74 /// \brief This header is explicitly excluded from the module. 75 ExcludedHeader 76 // Caution: Adding an enumerator needs other changes. 77 // Adjust the number of bits for KnownHeader::Storage. 78 // Adjust the bitfield HeaderFileInfo::HeaderRole size. 79 // Adjust the HeaderFileInfoTrait::ReadData streaming. 80 // Adjust the HeaderFileInfoTrait::EmitData streaming. 81 }; 82 83 /// \brief A header that is known to reside within a given module, 84 /// whether it was included or excluded. 85 class KnownHeader { 86 llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage; 87 88 public: 89 KnownHeader() : Storage(0, NormalHeader) { } 90 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { } 91 92 /// \brief Retrieve the module the header is stored in. 93 Module *getModule() const { return Storage.getPointer(); } 94 95 /// \brief The role of this header within the module. 96 ModuleHeaderRole getRole() const { return Storage.getInt(); } 97 98 /// \brief Whether this header is available in the module. 99 bool isAvailable() const { 100 return getRole() != ExcludedHeader && getModule()->isAvailable(); 101 } 102 103 // \brief Whether this known header is valid (i.e., it has an 104 // associated module). 105 LLVM_EXPLICIT operator bool() const { return Storage.getPointer() != 0; } 106 }; 107 108private: 109 typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> > 110 HeadersMap; 111 112 /// \brief Mapping from each header to the module that owns the contents of 113 /// that header. 114 HeadersMap Headers; 115 116 /// \brief Mapping from directories with umbrella headers to the module 117 /// that is generated from the umbrella header. 118 /// 119 /// This mapping is used to map headers that haven't explicitly been named 120 /// in the module map over to the module that includes them via its umbrella 121 /// header. 122 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 123 124 /// \brief A directory for which framework modules can be inferred. 125 struct InferredDirectory { 126 InferredDirectory() : InferModules(), InferSystemModules() { } 127 128 /// \brief Whether to infer modules from this directory. 129 unsigned InferModules : 1; 130 131 /// \brief Whether the modules we infer are [system] modules. 132 unsigned InferSystemModules : 1; 133 134 /// \brief The names of modules that cannot be inferred within this 135 /// directory. 136 SmallVector<std::string, 2> ExcludedModules; 137 }; 138 139 /// \brief A mapping from directories to information about inferring 140 /// framework modules from within those directories. 141 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories; 142 143 /// \brief Describes whether we haved parsed a particular file as a module 144 /// map. 145 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap; 146 147 friend class ModuleMapParser; 148 149 /// \brief Resolve the given export declaration into an actual export 150 /// declaration. 151 /// 152 /// \param Mod The module in which we're resolving the export declaration. 153 /// 154 /// \param Unresolved The export declaration to resolve. 155 /// 156 /// \param Complain Whether this routine should complain about unresolvable 157 /// exports. 158 /// 159 /// \returns The resolved export declaration, which will have a NULL pointer 160 /// if the export could not be resolved. 161 Module::ExportDecl 162 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved, 163 bool Complain) const; 164 165 /// \brief Resolve the given module id to an actual module. 166 /// 167 /// \param Id The module-id to resolve. 168 /// 169 /// \param Mod The module in which we're resolving the module-id. 170 /// 171 /// \param Complain Whether this routine should complain about unresolvable 172 /// module-ids. 173 /// 174 /// \returns The resolved module, or null if the module-id could not be 175 /// resolved. 176 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const; 177 178 /// \brief Looks up the modules that \p File corresponds to. 179 /// 180 /// If \p File represents a builtin header within Clang's builtin include 181 /// directory, this also loads all of the module maps to see if it will get 182 /// associated with a specific module (e.g. in /usr/include). 183 HeadersMap::iterator findKnownHeader(const FileEntry *File); 184 185public: 186 /// \brief Construct a new module map. 187 /// 188 /// \param SourceMgr The source manager used to find module files and headers. 189 /// This source manager should be shared with the header-search mechanism, 190 /// since they will refer to the same headers. 191 /// 192 /// \param Diags A diagnostic engine used for diagnostics. 193 /// 194 /// \param LangOpts Language options for this translation unit. 195 /// 196 /// \param Target The target for this translation unit. 197 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 198 const LangOptions &LangOpts, const TargetInfo *Target, 199 HeaderSearch &HeaderInfo); 200 201 /// \brief Destroy the module map. 202 /// 203 ~ModuleMap(); 204 205 /// \brief Set the target information. 206 void setTarget(const TargetInfo &Target); 207 208 /// \brief Set the directory that contains Clang-supplied include 209 /// files, such as our stdarg.h or tgmath.h. 210 void setBuiltinIncludeDir(const DirectoryEntry *Dir) { 211 BuiltinIncludeDir = Dir; 212 } 213 214 /// \brief Retrieve the module that owns the given header file, if any. 215 /// 216 /// \param File The header file that is likely to be included. 217 /// 218 /// \param RequestingModule Specifies the module the header is intended to be 219 /// used from. Used to disambiguate if a header is present in multiple 220 /// modules. 221 /// 222 /// \returns The module KnownHeader, which provides the module that owns the 223 /// given header file. The KnownHeader is default constructed to indicate 224 /// that no module owns this header file. 225 KnownHeader findModuleForHeader(const FileEntry *File, 226 Module *RequestingModule = NULL); 227 228 /// \brief Reports errors if a module must not include a specific file. 229 /// 230 /// \param RequestingModule The module including a file. 231 /// 232 /// \param FilenameLoc The location of the inclusion's filename. 233 /// 234 /// \param Filename The included filename as written. 235 /// 236 /// \param File The included file. 237 void diagnoseHeaderInclusion(Module *RequestingModule, 238 SourceLocation FilenameLoc, StringRef Filename, 239 const FileEntry *File); 240 241 /// \brief Determine whether the given header is part of a module 242 /// marked 'unavailable'. 243 bool isHeaderInUnavailableModule(const FileEntry *Header) const; 244 245 /// \brief Retrieve a module with the given name. 246 /// 247 /// \param Name The name of the module to look up. 248 /// 249 /// \returns The named module, if known; otherwise, returns null. 250 Module *findModule(StringRef Name) const; 251 252 /// \brief Retrieve a module with the given name using lexical name lookup, 253 /// starting at the given context. 254 /// 255 /// \param Name The name of the module to look up. 256 /// 257 /// \param Context The module context, from which we will perform lexical 258 /// name lookup. 259 /// 260 /// \returns The named module, if known; otherwise, returns null. 261 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const; 262 263 /// \brief Retrieve a module with the given name within the given context, 264 /// using direct (qualified) name lookup. 265 /// 266 /// \param Name The name of the module to look up. 267 /// 268 /// \param Context The module for which we will look for a submodule. If 269 /// null, we will look for a top-level module. 270 /// 271 /// \returns The named submodule, if known; otherwose, returns null. 272 Module *lookupModuleQualified(StringRef Name, Module *Context) const; 273 274 /// \brief Find a new module or submodule, or create it if it does not already 275 /// exist. 276 /// 277 /// \param Name The name of the module to find or create. 278 /// 279 /// \param Parent The module that will act as the parent of this submodule, 280 /// or NULL to indicate that this is a top-level module. 281 /// 282 /// \param IsFramework Whether this is a framework module. 283 /// 284 /// \param IsExplicit Whether this is an explicit submodule. 285 /// 286 /// \returns The found or newly-created module, along with a boolean value 287 /// that will be true if the module is newly-created. 288 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent, 289 bool IsFramework, 290 bool IsExplicit); 291 292 /// \brief Determine whether we can infer a framework module a framework 293 /// with the given name in the given 294 /// 295 /// \param ParentDir The directory that is the parent of the framework 296 /// directory. 297 /// 298 /// \param Name The name of the module. 299 /// 300 /// \param IsSystem Will be set to 'true' if the inferred module must be a 301 /// system module. 302 /// 303 /// \returns true if we are allowed to infer a framework module, and false 304 /// otherwise. 305 bool canInferFrameworkModule(const DirectoryEntry *ParentDir, 306 StringRef Name, bool &IsSystem) const; 307 308 /// \brief Infer the contents of a framework module map from the given 309 /// framework directory. 310 Module *inferFrameworkModule(StringRef ModuleName, 311 const DirectoryEntry *FrameworkDir, 312 bool IsSystem, Module *Parent); 313 314 /// \brief Retrieve the module map file containing the definition of the given 315 /// module. 316 /// 317 /// \param Module The module whose module map file will be returned, if known. 318 /// 319 /// \returns The file entry for the module map file containing the given 320 /// module, or NULL if the module definition was inferred. 321 const FileEntry *getContainingModuleMapFile(Module *Module) const; 322 323 /// \brief Resolve all of the unresolved exports in the given module. 324 /// 325 /// \param Mod The module whose exports should be resolved. 326 /// 327 /// \param Complain Whether to emit diagnostics for failures. 328 /// 329 /// \returns true if any errors were encountered while resolving exports, 330 /// false otherwise. 331 bool resolveExports(Module *Mod, bool Complain); 332 333 /// \brief Resolve all of the unresolved uses in the given module. 334 /// 335 /// \param Mod The module whose uses should be resolved. 336 /// 337 /// \param Complain Whether to emit diagnostics for failures. 338 /// 339 /// \returns true if any errors were encountered while resolving uses, 340 /// false otherwise. 341 bool resolveUses(Module *Mod, bool Complain); 342 343 /// \brief Resolve all of the unresolved conflicts in the given module. 344 /// 345 /// \param Mod The module whose conflicts should be resolved. 346 /// 347 /// \param Complain Whether to emit diagnostics for failures. 348 /// 349 /// \returns true if any errors were encountered while resolving conflicts, 350 /// false otherwise. 351 bool resolveConflicts(Module *Mod, bool Complain); 352 353 /// \brief Infers the (sub)module based on the given source location and 354 /// source manager. 355 /// 356 /// \param Loc The location within the source that we are querying, along 357 /// with its source manager. 358 /// 359 /// \returns The module that owns this source location, or null if no 360 /// module owns this source location. 361 Module *inferModuleFromLocation(FullSourceLoc Loc); 362 363 /// \brief Sets the umbrella header of the given module to the given 364 /// header. 365 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader); 366 367 /// \brief Sets the umbrella directory of the given module to the given 368 /// directory. 369 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir); 370 371 /// \brief Adds this header to the given module. 372 /// \param Role The role of the header wrt the module. 373 void addHeader(Module *Mod, const FileEntry *Header, 374 ModuleHeaderRole Role); 375 376 /// \brief Parse the given module map file, and record any modules we 377 /// encounter. 378 /// 379 /// \param File The file to be parsed. 380 /// 381 /// \param IsSystem Whether this module map file is in a system header 382 /// directory, and therefore should be considered a system module. 383 /// 384 /// \returns true if an error occurred, false otherwise. 385 bool parseModuleMapFile(const FileEntry *File, bool IsSystem); 386 387 /// \brief Dump the contents of the module map, for debugging purposes. 388 void dump(); 389 390 typedef llvm::StringMap<Module *>::const_iterator module_iterator; 391 module_iterator module_begin() const { return Modules.begin(); } 392 module_iterator module_end() const { return Modules.end(); } 393}; 394 395} 396#endif 397