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 29 namespace clang { 30 31 class DirectoryEntry; 32 class FileEntry; 33 class FileManager; 34 class DiagnosticConsumer; 35 class DiagnosticsEngine; 36 class HeaderSearch; 37 class ModuleMapParser; 38 39 class 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 58 public: 59 // The module that the .cc source file is associated with. 60 Module *SourceModule; 61 std::string SourceModuleName; 62 63 private: 64 /// \brief The top-level modules that are known. 65 llvm::StringMap<Module *> Modules; 66 67 public: 68 /// \brief Flags describing the role of a module header. 69 enum ModuleHeaderRole { 70 /// \brief This header is normally included in the module. 71 NormalHeader = 0x0, 72 /// \brief This header is included but private. 73 PrivateHeader = 0x1, 74 /// \brief This header is part of the module (for layering purposes) but 75 /// should be textually included. 76 TextualHeader = 0x2, 77 // Caution: Adding an enumerator needs other changes. 78 // Adjust the number of bits for KnownHeader::Storage. 79 // Adjust the bitfield HeaderFileInfo::HeaderRole size. 80 // Adjust the HeaderFileInfoTrait::ReadData streaming. 81 // Adjust the HeaderFileInfoTrait::EmitData streaming. 82 // Adjust ModuleMap::addHeader. 83 }; 84 85 /// \brief A header that is known to reside within a given module, 86 /// whether it was included or excluded. 87 class KnownHeader { 88 llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage; 89 90 public: 91 KnownHeader() : Storage(nullptr, NormalHeader) { } 92 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { } 93 94 /// \brief Retrieve the module the header is stored in. 95 Module *getModule() const { return Storage.getPointer(); } 96 97 /// \brief The role of this header within the module. 98 ModuleHeaderRole getRole() const { return Storage.getInt(); } 99 100 /// \brief Whether this header is available in the module. 101 bool isAvailable() const { 102 return getModule()->isAvailable(); 103 } 104 105 // \brief Whether this known header is valid (i.e., it has an 106 // associated module). 107 LLVM_EXPLICIT operator bool() const { 108 return Storage.getPointer() != nullptr; 109 } 110 }; 111 112 typedef llvm::SmallPtrSet<const FileEntry *, 1> AdditionalModMapsSet; 113 114 private: 115 typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> > 116 HeadersMap; 117 118 /// \brief Mapping from each header to the module that owns the contents of 119 /// that header. 120 HeadersMap Headers; 121 122 /// \brief Mapping from directories with umbrella headers to the module 123 /// that is generated from the umbrella header. 124 /// 125 /// This mapping is used to map headers that haven't explicitly been named 126 /// in the module map over to the module that includes them via its umbrella 127 /// header. 128 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 129 130 /// \brief A directory for which framework modules can be inferred. 131 struct InferredDirectory { 132 InferredDirectory() : InferModules(), InferSystemModules() { } 133 134 /// \brief Whether to infer modules from this directory. 135 unsigned InferModules : 1; 136 137 /// \brief Whether the modules we infer are [system] modules. 138 unsigned InferSystemModules : 1; 139 140 /// \brief If \c InferModules is non-zero, the module map file that allowed 141 /// inferred modules. Otherwise, nullptr. 142 const FileEntry *ModuleMapFile; 143 144 /// \brief The names of modules that cannot be inferred within this 145 /// directory. 146 SmallVector<std::string, 2> ExcludedModules; 147 }; 148 149 /// \brief A mapping from directories to information about inferring 150 /// framework modules from within those directories. 151 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories; 152 153 /// A mapping from an inferred module to the module map that allowed the 154 /// inference. 155 llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy; 156 157 llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps; 158 159 /// \brief Describes whether we haved parsed a particular file as a module 160 /// map. 161 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap; 162 163 friend class ModuleMapParser; 164 165 /// \brief Resolve the given export declaration into an actual export 166 /// declaration. 167 /// 168 /// \param Mod The module in which we're resolving the export declaration. 169 /// 170 /// \param Unresolved The export declaration to resolve. 171 /// 172 /// \param Complain Whether this routine should complain about unresolvable 173 /// exports. 174 /// 175 /// \returns The resolved export declaration, which will have a NULL pointer 176 /// if the export could not be resolved. 177 Module::ExportDecl 178 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved, 179 bool Complain) const; 180 181 /// \brief Resolve the given module id to an actual module. 182 /// 183 /// \param Id The module-id to resolve. 184 /// 185 /// \param Mod The module in which we're resolving the module-id. 186 /// 187 /// \param Complain Whether this routine should complain about unresolvable 188 /// module-ids. 189 /// 190 /// \returns The resolved module, or null if the module-id could not be 191 /// resolved. 192 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const; 193 194 /// \brief Looks up the modules that \p File corresponds to. 195 /// 196 /// If \p File represents a builtin header within Clang's builtin include 197 /// directory, this also loads all of the module maps to see if it will get 198 /// associated with a specific module (e.g. in /usr/include). 199 HeadersMap::iterator findKnownHeader(const FileEntry *File); 200 201 /// \brief Searches for a module whose umbrella directory contains \p File. 202 /// 203 /// \param File The header to search for. 204 /// 205 /// \param IntermediateDirs On success, contains the set of directories 206 /// searched before finding \p File. 207 KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File, 208 SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs); 209 210 /// \brief A convenience method to determine if \p File is (possibly nested) 211 /// in an umbrella directory. 212 bool isHeaderInUmbrellaDirs(const FileEntry *File) { 213 SmallVector<const DirectoryEntry *, 2> IntermediateDirs; 214 return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs)); 215 } 216 217 public: 218 /// \brief Construct a new module map. 219 /// 220 /// \param SourceMgr The source manager used to find module files and headers. 221 /// This source manager should be shared with the header-search mechanism, 222 /// since they will refer to the same headers. 223 /// 224 /// \param Diags A diagnostic engine used for diagnostics. 225 /// 226 /// \param LangOpts Language options for this translation unit. 227 /// 228 /// \param Target The target for this translation unit. 229 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 230 const LangOptions &LangOpts, const TargetInfo *Target, 231 HeaderSearch &HeaderInfo); 232 233 /// \brief Destroy the module map. 234 /// 235 ~ModuleMap(); 236 237 /// \brief Set the target information. 238 void setTarget(const TargetInfo &Target); 239 240 /// \brief Set the directory that contains Clang-supplied include 241 /// files, such as our stdarg.h or tgmath.h. 242 void setBuiltinIncludeDir(const DirectoryEntry *Dir) { 243 BuiltinIncludeDir = Dir; 244 } 245 246 /// \brief Retrieve the module that owns the given header file, if any. 247 /// 248 /// \param File The header file that is likely to be included. 249 /// 250 /// \param RequestingModule Specifies the module the header is intended to be 251 /// used from. Used to disambiguate if a header is present in multiple 252 /// modules. 253 /// 254 /// \param IncludeTextualHeaders If \c true, also find textual headers. By 255 /// default, these are treated like excluded headers and result in no known 256 /// header being found. 257 /// 258 /// \returns The module KnownHeader, which provides the module that owns the 259 /// given header file. The KnownHeader is default constructed to indicate 260 /// that no module owns this header file. 261 KnownHeader findModuleForHeader(const FileEntry *File, 262 Module *RequestingModule = nullptr, 263 bool IncludeTextualHeaders = false); 264 265 /// \brief Reports errors if a module must not include a specific file. 266 /// 267 /// \param RequestingModule The module including a file. 268 /// 269 /// \param FilenameLoc The location of the inclusion's filename. 270 /// 271 /// \param Filename The included filename as written. 272 /// 273 /// \param File The included file. 274 void diagnoseHeaderInclusion(Module *RequestingModule, 275 SourceLocation FilenameLoc, StringRef Filename, 276 const FileEntry *File); 277 278 /// \brief Determine whether the given header is part of a module 279 /// marked 'unavailable'. 280 bool isHeaderInUnavailableModule(const FileEntry *Header) const; 281 282 /// \brief Determine whether the given header is unavailable as part 283 /// of the specified module. 284 bool isHeaderUnavailableInModule(const FileEntry *Header, 285 const Module *RequestingModule) const; 286 287 /// \brief Retrieve a module with the given name. 288 /// 289 /// \param Name The name of the module to look up. 290 /// 291 /// \returns The named module, if known; otherwise, returns null. 292 Module *findModule(StringRef Name) const; 293 294 /// \brief Retrieve a module with the given name using lexical name lookup, 295 /// starting at the given context. 296 /// 297 /// \param Name The name of the module to look up. 298 /// 299 /// \param Context The module context, from which we will perform lexical 300 /// name lookup. 301 /// 302 /// \returns The named module, if known; otherwise, returns null. 303 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const; 304 305 /// \brief Retrieve a module with the given name within the given context, 306 /// using direct (qualified) name lookup. 307 /// 308 /// \param Name The name of the module to look up. 309 /// 310 /// \param Context The module for which we will look for a submodule. If 311 /// null, we will look for a top-level module. 312 /// 313 /// \returns The named submodule, if known; otherwose, returns null. 314 Module *lookupModuleQualified(StringRef Name, Module *Context) const; 315 316 /// \brief Find a new module or submodule, or create it if it does not already 317 /// exist. 318 /// 319 /// \param Name The name of the module to find or create. 320 /// 321 /// \param Parent The module that will act as the parent of this submodule, 322 /// or NULL to indicate that this is a top-level module. 323 /// 324 /// \param IsFramework Whether this is a framework module. 325 /// 326 /// \param IsExplicit Whether this is an explicit submodule. 327 /// 328 /// \returns The found or newly-created module, along with a boolean value 329 /// that will be true if the module is newly-created. 330 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent, 331 bool IsFramework, 332 bool IsExplicit); 333 334 /// \brief Determine whether we can infer a framework module a framework 335 /// with the given name in the given 336 /// 337 /// \param ParentDir The directory that is the parent of the framework 338 /// directory. 339 /// 340 /// \param Name The name of the module. 341 /// 342 /// \param IsSystem Will be set to 'true' if the inferred module must be a 343 /// system module. 344 /// 345 /// \returns true if we are allowed to infer a framework module, and false 346 /// otherwise. 347 bool canInferFrameworkModule(const DirectoryEntry *ParentDir, 348 StringRef Name, bool &IsSystem) const; 349 350 /// \brief Infer the contents of a framework module map from the given 351 /// framework directory. 352 Module *inferFrameworkModule(StringRef ModuleName, 353 const DirectoryEntry *FrameworkDir, 354 bool IsSystem, Module *Parent); 355 356 /// \brief Retrieve the module map file containing the definition of the given 357 /// module. 358 /// 359 /// \param Module The module whose module map file will be returned, if known. 360 /// 361 /// \returns The file entry for the module map file containing the given 362 /// module, or NULL if the module definition was inferred. 363 const FileEntry *getContainingModuleMapFile(const Module *Module) const; 364 365 /// \brief Get the module map file that (along with the module name) uniquely 366 /// identifies this module. 367 /// 368 /// The particular module that \c Name refers to may depend on how the module 369 /// was found in header search. However, the combination of \c Name and 370 /// this module map will be globally unique for top-level modules. In the case 371 /// of inferred modules, returns the module map that allowed the inference 372 /// (e.g. contained 'module *'). Otherwise, returns 373 /// getContainingModuleMapFile(). 374 const FileEntry *getModuleMapFileForUniquing(const Module *M) const; 375 376 void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap); 377 378 /// \brief Get any module map files other than getModuleMapFileForUniquing(M) 379 /// that define submodules of a top-level module \p M. This is cheaper than 380 /// getting the module map file for each submodule individually, since the 381 /// expected number of results is very small. 382 AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) { 383 auto I = AdditionalModMaps.find(M); 384 if (I == AdditionalModMaps.end()) 385 return nullptr; 386 return &I->second; 387 } 388 389 void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap) { 390 AdditionalModMaps[M].insert(ModuleMap); 391 } 392 393 /// \brief Resolve all of the unresolved exports in the given module. 394 /// 395 /// \param Mod The module whose exports should be resolved. 396 /// 397 /// \param Complain Whether to emit diagnostics for failures. 398 /// 399 /// \returns true if any errors were encountered while resolving exports, 400 /// false otherwise. 401 bool resolveExports(Module *Mod, bool Complain); 402 403 /// \brief Resolve all of the unresolved uses in the given module. 404 /// 405 /// \param Mod The module whose uses should be resolved. 406 /// 407 /// \param Complain Whether to emit diagnostics for failures. 408 /// 409 /// \returns true if any errors were encountered while resolving uses, 410 /// false otherwise. 411 bool resolveUses(Module *Mod, bool Complain); 412 413 /// \brief Resolve all of the unresolved conflicts in the given module. 414 /// 415 /// \param Mod The module whose conflicts should be resolved. 416 /// 417 /// \param Complain Whether to emit diagnostics for failures. 418 /// 419 /// \returns true if any errors were encountered while resolving conflicts, 420 /// false otherwise. 421 bool resolveConflicts(Module *Mod, bool Complain); 422 423 /// \brief Infers the (sub)module based on the given source location and 424 /// source manager. 425 /// 426 /// \param Loc The location within the source that we are querying, along 427 /// with its source manager. 428 /// 429 /// \returns The module that owns this source location, or null if no 430 /// module owns this source location. 431 Module *inferModuleFromLocation(FullSourceLoc Loc); 432 433 /// \brief Sets the umbrella header of the given module to the given 434 /// header. 435 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader); 436 437 /// \brief Sets the umbrella directory of the given module to the given 438 /// directory. 439 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir); 440 441 /// \brief Adds this header to the given module. 442 /// \param Role The role of the header wrt the module. 443 void addHeader(Module *Mod, Module::Header Header, 444 ModuleHeaderRole Role); 445 446 /// \brief Marks this header as being excluded from the given module. 447 void excludeHeader(Module *Mod, Module::Header Header); 448 449 /// \brief Parse the given module map file, and record any modules we 450 /// encounter. 451 /// 452 /// \param File The file to be parsed. 453 /// 454 /// \param IsSystem Whether this module map file is in a system header 455 /// directory, and therefore should be considered a system module. 456 /// 457 /// \param HomeDir The directory in which relative paths within this module 458 /// map file will be resolved. 459 /// 460 /// \returns true if an error occurred, false otherwise. 461 bool parseModuleMapFile(const FileEntry *File, bool IsSystem, 462 const DirectoryEntry *HomeDir); 463 464 /// \brief Dump the contents of the module map, for debugging purposes. 465 void dump(); 466 467 typedef llvm::StringMap<Module *>::const_iterator module_iterator; 468 module_iterator module_begin() const { return Modules.begin(); } 469 module_iterator module_end() const { return Modules.end(); } 470 }; 471 472 } 473 #endif 474