clang API Documentation
00001 //===--- ModuleMap.h - Describe the layout of modules -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the ModuleMap interface, which describes the layout of a 00011 // module as it relates to headers. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 00016 #ifndef LLVM_CLANG_LEX_MODULEMAP_H 00017 #define LLVM_CLANG_LEX_MODULEMAP_H 00018 00019 #include "clang/Basic/LangOptions.h" 00020 #include "clang/Basic/Module.h" 00021 #include "clang/Basic/SourceManager.h" 00022 #include "llvm/ADT/DenseMap.h" 00023 #include "llvm/ADT/IntrusiveRefCntPtr.h" 00024 #include "llvm/ADT/SmallVector.h" 00025 #include "llvm/ADT/StringMap.h" 00026 #include "llvm/ADT/StringRef.h" 00027 #include <string> 00028 00029 namespace clang { 00030 00031 class DirectoryEntry; 00032 class FileEntry; 00033 class FileManager; 00034 class DiagnosticConsumer; 00035 class DiagnosticsEngine; 00036 class HeaderSearch; 00037 class ModuleMapParser; 00038 00039 class ModuleMap { 00040 SourceManager &SourceMgr; 00041 DiagnosticsEngine &Diags; 00042 const LangOptions &LangOpts; 00043 const TargetInfo *Target; 00044 HeaderSearch &HeaderInfo; 00045 00046 /// \brief The directory used for Clang-supplied, builtin include headers, 00047 /// such as "stdint.h". 00048 const DirectoryEntry *BuiltinIncludeDir; 00049 00050 /// \brief Language options used to parse the module map itself. 00051 /// 00052 /// These are always simple C language options. 00053 LangOptions MMapLangOpts; 00054 00055 // The module that we are building; related to \c LangOptions::CurrentModule. 00056 Module *CompilingModule; 00057 00058 public: 00059 // The module that the .cc source file is associated with. 00060 Module *SourceModule; 00061 std::string SourceModuleName; 00062 00063 private: 00064 /// \brief The top-level modules that are known. 00065 llvm::StringMap<Module *> Modules; 00066 00067 public: 00068 /// \brief Flags describing the role of a module header. 00069 enum ModuleHeaderRole { 00070 /// \brief This header is normally included in the module. 00071 NormalHeader = 0x0, 00072 /// \brief This header is included but private. 00073 PrivateHeader = 0x1, 00074 /// \brief This header is part of the module (for layering purposes) but 00075 /// should be textually included. 00076 TextualHeader = 0x2, 00077 // Caution: Adding an enumerator needs other changes. 00078 // Adjust the number of bits for KnownHeader::Storage. 00079 // Adjust the bitfield HeaderFileInfo::HeaderRole size. 00080 // Adjust the HeaderFileInfoTrait::ReadData streaming. 00081 // Adjust the HeaderFileInfoTrait::EmitData streaming. 00082 // Adjust ModuleMap::addHeader. 00083 }; 00084 00085 /// \brief A header that is known to reside within a given module, 00086 /// whether it was included or excluded. 00087 class KnownHeader { 00088 llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage; 00089 00090 public: 00091 KnownHeader() : Storage(nullptr, NormalHeader) { } 00092 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { } 00093 00094 /// \brief Retrieve the module the header is stored in. 00095 Module *getModule() const { return Storage.getPointer(); } 00096 00097 /// \brief The role of this header within the module. 00098 ModuleHeaderRole getRole() const { return Storage.getInt(); } 00099 00100 /// \brief Whether this header is available in the module. 00101 bool isAvailable() const { 00102 return getModule()->isAvailable(); 00103 } 00104 00105 // \brief Whether this known header is valid (i.e., it has an 00106 // associated module). 00107 LLVM_EXPLICIT operator bool() const { 00108 return Storage.getPointer() != nullptr; 00109 } 00110 }; 00111 00112 typedef llvm::SmallPtrSet<const FileEntry *, 1> AdditionalModMapsSet; 00113 00114 private: 00115 typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> > 00116 HeadersMap; 00117 00118 /// \brief Mapping from each header to the module that owns the contents of 00119 /// that header. 00120 HeadersMap Headers; 00121 00122 /// \brief Mapping from directories with umbrella headers to the module 00123 /// that is generated from the umbrella header. 00124 /// 00125 /// This mapping is used to map headers that haven't explicitly been named 00126 /// in the module map over to the module that includes them via its umbrella 00127 /// header. 00128 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs; 00129 00130 /// \brief A directory for which framework modules can be inferred. 00131 struct InferredDirectory { 00132 InferredDirectory() : InferModules(), InferSystemModules() { } 00133 00134 /// \brief Whether to infer modules from this directory. 00135 unsigned InferModules : 1; 00136 00137 /// \brief Whether the modules we infer are [system] modules. 00138 unsigned InferSystemModules : 1; 00139 00140 /// \brief If \c InferModules is non-zero, the module map file that allowed 00141 /// inferred modules. Otherwise, nullptr. 00142 const FileEntry *ModuleMapFile; 00143 00144 /// \brief The names of modules that cannot be inferred within this 00145 /// directory. 00146 SmallVector<std::string, 2> ExcludedModules; 00147 }; 00148 00149 /// \brief A mapping from directories to information about inferring 00150 /// framework modules from within those directories. 00151 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories; 00152 00153 /// A mapping from an inferred module to the module map that allowed the 00154 /// inference. 00155 llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy; 00156 00157 llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps; 00158 00159 /// \brief Describes whether we haved parsed a particular file as a module 00160 /// map. 00161 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap; 00162 00163 friend class ModuleMapParser; 00164 00165 /// \brief Resolve the given export declaration into an actual export 00166 /// declaration. 00167 /// 00168 /// \param Mod The module in which we're resolving the export declaration. 00169 /// 00170 /// \param Unresolved The export declaration to resolve. 00171 /// 00172 /// \param Complain Whether this routine should complain about unresolvable 00173 /// exports. 00174 /// 00175 /// \returns The resolved export declaration, which will have a NULL pointer 00176 /// if the export could not be resolved. 00177 Module::ExportDecl 00178 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved, 00179 bool Complain) const; 00180 00181 /// \brief Resolve the given module id to an actual module. 00182 /// 00183 /// \param Id The module-id to resolve. 00184 /// 00185 /// \param Mod The module in which we're resolving the module-id. 00186 /// 00187 /// \param Complain Whether this routine should complain about unresolvable 00188 /// module-ids. 00189 /// 00190 /// \returns The resolved module, or null if the module-id could not be 00191 /// resolved. 00192 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const; 00193 00194 /// \brief Looks up the modules that \p File corresponds to. 00195 /// 00196 /// If \p File represents a builtin header within Clang's builtin include 00197 /// directory, this also loads all of the module maps to see if it will get 00198 /// associated with a specific module (e.g. in /usr/include). 00199 HeadersMap::iterator findKnownHeader(const FileEntry *File); 00200 00201 /// \brief Searches for a module whose umbrella directory contains \p File. 00202 /// 00203 /// \param File The header to search for. 00204 /// 00205 /// \param IntermediateDirs On success, contains the set of directories 00206 /// searched before finding \p File. 00207 KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File, 00208 SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs); 00209 00210 /// \brief A convenience method to determine if \p File is (possibly nested) 00211 /// in an umbrella directory. 00212 bool isHeaderInUmbrellaDirs(const FileEntry *File) { 00213 SmallVector<const DirectoryEntry *, 2> IntermediateDirs; 00214 return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs)); 00215 } 00216 00217 public: 00218 /// \brief Construct a new module map. 00219 /// 00220 /// \param SourceMgr The source manager used to find module files and headers. 00221 /// This source manager should be shared with the header-search mechanism, 00222 /// since they will refer to the same headers. 00223 /// 00224 /// \param Diags A diagnostic engine used for diagnostics. 00225 /// 00226 /// \param LangOpts Language options for this translation unit. 00227 /// 00228 /// \param Target The target for this translation unit. 00229 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 00230 const LangOptions &LangOpts, const TargetInfo *Target, 00231 HeaderSearch &HeaderInfo); 00232 00233 /// \brief Destroy the module map. 00234 /// 00235 ~ModuleMap(); 00236 00237 /// \brief Set the target information. 00238 void setTarget(const TargetInfo &Target); 00239 00240 /// \brief Set the directory that contains Clang-supplied include 00241 /// files, such as our stdarg.h or tgmath.h. 00242 void setBuiltinIncludeDir(const DirectoryEntry *Dir) { 00243 BuiltinIncludeDir = Dir; 00244 } 00245 00246 /// \brief Retrieve the module that owns the given header file, if any. 00247 /// 00248 /// \param File The header file that is likely to be included. 00249 /// 00250 /// \param RequestingModule Specifies the module the header is intended to be 00251 /// used from. Used to disambiguate if a header is present in multiple 00252 /// modules. 00253 /// 00254 /// \param IncludeTextualHeaders If \c true, also find textual headers. By 00255 /// default, these are treated like excluded headers and result in no known 00256 /// header being found. 00257 /// 00258 /// \returns The module KnownHeader, which provides the module that owns the 00259 /// given header file. The KnownHeader is default constructed to indicate 00260 /// that no module owns this header file. 00261 KnownHeader findModuleForHeader(const FileEntry *File, 00262 Module *RequestingModule = nullptr, 00263 bool IncludeTextualHeaders = false); 00264 00265 /// \brief Reports errors if a module must not include a specific file. 00266 /// 00267 /// \param RequestingModule The module including a file. 00268 /// 00269 /// \param FilenameLoc The location of the inclusion's filename. 00270 /// 00271 /// \param Filename The included filename as written. 00272 /// 00273 /// \param File The included file. 00274 void diagnoseHeaderInclusion(Module *RequestingModule, 00275 SourceLocation FilenameLoc, StringRef Filename, 00276 const FileEntry *File); 00277 00278 /// \brief Determine whether the given header is part of a module 00279 /// marked 'unavailable'. 00280 bool isHeaderInUnavailableModule(const FileEntry *Header) const; 00281 00282 /// \brief Determine whether the given header is unavailable as part 00283 /// of the specified module. 00284 bool isHeaderUnavailableInModule(const FileEntry *Header, 00285 const Module *RequestingModule) const; 00286 00287 /// \brief Retrieve a module with the given name. 00288 /// 00289 /// \param Name The name of the module to look up. 00290 /// 00291 /// \returns The named module, if known; otherwise, returns null. 00292 Module *findModule(StringRef Name) const; 00293 00294 /// \brief Retrieve a module with the given name using lexical name lookup, 00295 /// starting at the given context. 00296 /// 00297 /// \param Name The name of the module to look up. 00298 /// 00299 /// \param Context The module context, from which we will perform lexical 00300 /// name lookup. 00301 /// 00302 /// \returns The named module, if known; otherwise, returns null. 00303 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const; 00304 00305 /// \brief Retrieve a module with the given name within the given context, 00306 /// using direct (qualified) name lookup. 00307 /// 00308 /// \param Name The name of the module to look up. 00309 /// 00310 /// \param Context The module for which we will look for a submodule. If 00311 /// null, we will look for a top-level module. 00312 /// 00313 /// \returns The named submodule, if known; otherwose, returns null. 00314 Module *lookupModuleQualified(StringRef Name, Module *Context) const; 00315 00316 /// \brief Find a new module or submodule, or create it if it does not already 00317 /// exist. 00318 /// 00319 /// \param Name The name of the module to find or create. 00320 /// 00321 /// \param Parent The module that will act as the parent of this submodule, 00322 /// or NULL to indicate that this is a top-level module. 00323 /// 00324 /// \param IsFramework Whether this is a framework module. 00325 /// 00326 /// \param IsExplicit Whether this is an explicit submodule. 00327 /// 00328 /// \returns The found or newly-created module, along with a boolean value 00329 /// that will be true if the module is newly-created. 00330 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent, 00331 bool IsFramework, 00332 bool IsExplicit); 00333 00334 /// \brief Determine whether we can infer a framework module a framework 00335 /// with the given name in the given 00336 /// 00337 /// \param ParentDir The directory that is the parent of the framework 00338 /// directory. 00339 /// 00340 /// \param Name The name of the module. 00341 /// 00342 /// \param IsSystem Will be set to 'true' if the inferred module must be a 00343 /// system module. 00344 /// 00345 /// \returns true if we are allowed to infer a framework module, and false 00346 /// otherwise. 00347 bool canInferFrameworkModule(const DirectoryEntry *ParentDir, 00348 StringRef Name, bool &IsSystem) const; 00349 00350 /// \brief Infer the contents of a framework module map from the given 00351 /// framework directory. 00352 Module *inferFrameworkModule(StringRef ModuleName, 00353 const DirectoryEntry *FrameworkDir, 00354 bool IsSystem, Module *Parent); 00355 00356 /// \brief Retrieve the module map file containing the definition of the given 00357 /// module. 00358 /// 00359 /// \param Module The module whose module map file will be returned, if known. 00360 /// 00361 /// \returns The file entry for the module map file containing the given 00362 /// module, or NULL if the module definition was inferred. 00363 const FileEntry *getContainingModuleMapFile(const Module *Module) const; 00364 00365 /// \brief Get the module map file that (along with the module name) uniquely 00366 /// identifies this module. 00367 /// 00368 /// The particular module that \c Name refers to may depend on how the module 00369 /// was found in header search. However, the combination of \c Name and 00370 /// this module map will be globally unique for top-level modules. In the case 00371 /// of inferred modules, returns the module map that allowed the inference 00372 /// (e.g. contained 'module *'). Otherwise, returns 00373 /// getContainingModuleMapFile(). 00374 const FileEntry *getModuleMapFileForUniquing(const Module *M) const; 00375 00376 void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap); 00377 00378 /// \brief Get any module map files other than getModuleMapFileForUniquing(M) 00379 /// that define submodules of a top-level module \p M. This is cheaper than 00380 /// getting the module map file for each submodule individually, since the 00381 /// expected number of results is very small. 00382 AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) { 00383 auto I = AdditionalModMaps.find(M); 00384 if (I == AdditionalModMaps.end()) 00385 return nullptr; 00386 return &I->second; 00387 } 00388 00389 void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap) { 00390 AdditionalModMaps[M].insert(ModuleMap); 00391 } 00392 00393 /// \brief Resolve all of the unresolved exports in the given module. 00394 /// 00395 /// \param Mod The module whose exports should be resolved. 00396 /// 00397 /// \param Complain Whether to emit diagnostics for failures. 00398 /// 00399 /// \returns true if any errors were encountered while resolving exports, 00400 /// false otherwise. 00401 bool resolveExports(Module *Mod, bool Complain); 00402 00403 /// \brief Resolve all of the unresolved uses in the given module. 00404 /// 00405 /// \param Mod The module whose uses should be resolved. 00406 /// 00407 /// \param Complain Whether to emit diagnostics for failures. 00408 /// 00409 /// \returns true if any errors were encountered while resolving uses, 00410 /// false otherwise. 00411 bool resolveUses(Module *Mod, bool Complain); 00412 00413 /// \brief Resolve all of the unresolved conflicts in the given module. 00414 /// 00415 /// \param Mod The module whose conflicts should be resolved. 00416 /// 00417 /// \param Complain Whether to emit diagnostics for failures. 00418 /// 00419 /// \returns true if any errors were encountered while resolving conflicts, 00420 /// false otherwise. 00421 bool resolveConflicts(Module *Mod, bool Complain); 00422 00423 /// \brief Infers the (sub)module based on the given source location and 00424 /// source manager. 00425 /// 00426 /// \param Loc The location within the source that we are querying, along 00427 /// with its source manager. 00428 /// 00429 /// \returns The module that owns this source location, or null if no 00430 /// module owns this source location. 00431 Module *inferModuleFromLocation(FullSourceLoc Loc); 00432 00433 /// \brief Sets the umbrella header of the given module to the given 00434 /// header. 00435 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader); 00436 00437 /// \brief Sets the umbrella directory of the given module to the given 00438 /// directory. 00439 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir); 00440 00441 /// \brief Adds this header to the given module. 00442 /// \param Role The role of the header wrt the module. 00443 void addHeader(Module *Mod, const FileEntry *Header, 00444 ModuleHeaderRole Role); 00445 00446 /// \brief Marks this header as being excluded from the given module. 00447 void excludeHeader(Module *Mod, const FileEntry *Header); 00448 00449 /// \brief Parse the given module map file, and record any modules we 00450 /// encounter. 00451 /// 00452 /// \param File The file to be parsed. 00453 /// 00454 /// \param IsSystem Whether this module map file is in a system header 00455 /// directory, and therefore should be considered a system module. 00456 /// 00457 /// \returns true if an error occurred, false otherwise. 00458 bool parseModuleMapFile(const FileEntry *File, bool IsSystem); 00459 00460 /// \brief Dump the contents of the module map, for debugging purposes. 00461 void dump(); 00462 00463 typedef llvm::StringMap<Module *>::const_iterator module_iterator; 00464 module_iterator module_begin() const { return Modules.begin(); } 00465 module_iterator module_end() const { return Modules.end(); } 00466 }; 00467 00468 } 00469 #endif