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