1 //===--- ModuleMap.h - Describe the layout of modules -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ModuleMap interface, which describes the layout of a
11 // module as it relates to headers.
13 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LEX_MODULEMAP_H
17 #define LLVM_CLANG_LEX_MODULEMAP_H
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"
34 class DiagnosticConsumer;
35 class DiagnosticsEngine;
37 class ModuleMapParser;
40 SourceManager &SourceMgr;
41 DiagnosticsEngine &Diags;
42 const LangOptions &LangOpts;
43 const TargetInfo *Target;
44 HeaderSearch &HeaderInfo;
46 /// \brief The directory used for Clang-supplied, builtin include headers,
47 /// such as "stdint.h".
48 const DirectoryEntry *BuiltinIncludeDir;
50 /// \brief Language options used to parse the module map itself.
52 /// These are always simple C language options.
53 LangOptions MMapLangOpts;
55 // The module that we are building; related to \c LangOptions::CurrentModule.
56 Module *CompilingModule;
59 // The module that the .cc source file is associated with.
61 std::string SourceModuleName;
64 /// \brief The top-level modules that are known.
65 llvm::StringMap<Module *> Modules;
68 /// \brief Flags describing the role of a module header.
69 enum ModuleHeaderRole {
70 /// \brief This header is normally included in the module.
72 /// \brief This header is included but private.
74 /// \brief This header is part of the module (for layering purposes) but
75 /// should be textually included.
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.
85 /// \brief A header that is known to reside within a given module,
86 /// whether it was included or excluded.
88 llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage;
91 KnownHeader() : Storage(nullptr, NormalHeader) { }
92 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { }
94 /// \brief Retrieve the module the header is stored in.
95 Module *getModule() const { return Storage.getPointer(); }
97 /// \brief The role of this header within the module.
98 ModuleHeaderRole getRole() const { return Storage.getInt(); }
100 /// \brief Whether this header is available in the module.
101 bool isAvailable() const {
102 return getModule()->isAvailable();
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;
112 typedef llvm::SmallPtrSet<const FileEntry *, 1> AdditionalModMapsSet;
115 typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> >
118 /// \brief Mapping from each header to the module that owns the contents of
122 /// \brief Mapping from directories with umbrella headers to the module
123 /// that is generated from the umbrella header.
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
128 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
130 /// \brief The set of attributes that can be attached to a module.
132 Attributes() : IsSystem(), IsExternC(), IsExhaustive() {}
134 /// \brief Whether this is a system module.
135 unsigned IsSystem : 1;
137 /// \brief Whether this is an extern "C" module.
138 unsigned IsExternC : 1;
140 /// \brief Whether this is an exhaustive set of configuration macros.
141 unsigned IsExhaustive : 1;
144 /// \brief A directory for which framework modules can be inferred.
145 struct InferredDirectory {
146 InferredDirectory() : InferModules() {}
148 /// \brief Whether to infer modules from this directory.
149 unsigned InferModules : 1;
151 /// \brief The attributes to use for inferred modules.
154 /// \brief If \c InferModules is non-zero, the module map file that allowed
155 /// inferred modules. Otherwise, nullptr.
156 const FileEntry *ModuleMapFile;
158 /// \brief The names of modules that cannot be inferred within this
160 SmallVector<std::string, 2> ExcludedModules;
163 /// \brief A mapping from directories to information about inferring
164 /// framework modules from within those directories.
165 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
167 /// A mapping from an inferred module to the module map that allowed the
169 llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy;
171 llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
173 /// \brief Describes whether we haved parsed a particular file as a module
175 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
177 friend class ModuleMapParser;
179 /// \brief Resolve the given export declaration into an actual export
182 /// \param Mod The module in which we're resolving the export declaration.
184 /// \param Unresolved The export declaration to resolve.
186 /// \param Complain Whether this routine should complain about unresolvable
189 /// \returns The resolved export declaration, which will have a NULL pointer
190 /// if the export could not be resolved.
192 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
193 bool Complain) const;
195 /// \brief Resolve the given module id to an actual module.
197 /// \param Id The module-id to resolve.
199 /// \param Mod The module in which we're resolving the module-id.
201 /// \param Complain Whether this routine should complain about unresolvable
204 /// \returns The resolved module, or null if the module-id could not be
206 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
208 /// \brief Looks up the modules that \p File corresponds to.
210 /// If \p File represents a builtin header within Clang's builtin include
211 /// directory, this also loads all of the module maps to see if it will get
212 /// associated with a specific module (e.g. in /usr/include).
213 HeadersMap::iterator findKnownHeader(const FileEntry *File);
215 /// \brief Searches for a module whose umbrella directory contains \p File.
217 /// \param File The header to search for.
219 /// \param IntermediateDirs On success, contains the set of directories
220 /// searched before finding \p File.
221 KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
222 SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
224 /// \brief A convenience method to determine if \p File is (possibly nested)
225 /// in an umbrella directory.
226 bool isHeaderInUmbrellaDirs(const FileEntry *File) {
227 SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
228 return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
231 Module *inferFrameworkModule(StringRef ModuleName,
232 const DirectoryEntry *FrameworkDir,
233 Attributes Attrs, Module *Parent);
236 /// \brief Construct a new module map.
238 /// \param SourceMgr The source manager used to find module files and headers.
239 /// This source manager should be shared with the header-search mechanism,
240 /// since they will refer to the same headers.
242 /// \param Diags A diagnostic engine used for diagnostics.
244 /// \param LangOpts Language options for this translation unit.
246 /// \param Target The target for this translation unit.
247 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
248 const LangOptions &LangOpts, const TargetInfo *Target,
249 HeaderSearch &HeaderInfo);
251 /// \brief Destroy the module map.
255 /// \brief Set the target information.
256 void setTarget(const TargetInfo &Target);
258 /// \brief Set the directory that contains Clang-supplied include
259 /// files, such as our stdarg.h or tgmath.h.
260 void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
261 BuiltinIncludeDir = Dir;
264 /// \brief Retrieve the module that owns the given header file, if any.
266 /// \param File The header file that is likely to be included.
268 /// \param RequestingModule Specifies the module the header is intended to be
269 /// used from. Used to disambiguate if a header is present in multiple
272 /// \param IncludeTextualHeaders If \c true, also find textual headers. By
273 /// default, these are treated like excluded headers and result in no known
274 /// header being found.
276 /// \returns The module KnownHeader, which provides the module that owns the
277 /// given header file. The KnownHeader is default constructed to indicate
278 /// that no module owns this header file.
279 KnownHeader findModuleForHeader(const FileEntry *File,
280 Module *RequestingModule = nullptr,
281 bool IncludeTextualHeaders = false);
283 /// \brief Reports errors if a module must not include a specific file.
285 /// \param RequestingModule The module including a file.
287 /// \param FilenameLoc The location of the inclusion's filename.
289 /// \param Filename The included filename as written.
291 /// \param File The included file.
292 void diagnoseHeaderInclusion(Module *RequestingModule,
293 SourceLocation FilenameLoc, StringRef Filename,
294 const FileEntry *File);
296 /// \brief Determine whether the given header is part of a module
297 /// marked 'unavailable'.
298 bool isHeaderInUnavailableModule(const FileEntry *Header) const;
300 /// \brief Determine whether the given header is unavailable as part
301 /// of the specified module.
302 bool isHeaderUnavailableInModule(const FileEntry *Header,
303 const Module *RequestingModule) const;
305 /// \brief Retrieve a module with the given name.
307 /// \param Name The name of the module to look up.
309 /// \returns The named module, if known; otherwise, returns null.
310 Module *findModule(StringRef Name) const;
312 /// \brief Retrieve a module with the given name using lexical name lookup,
313 /// starting at the given context.
315 /// \param Name The name of the module to look up.
317 /// \param Context The module context, from which we will perform lexical
320 /// \returns The named module, if known; otherwise, returns null.
321 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
323 /// \brief Retrieve a module with the given name within the given context,
324 /// using direct (qualified) name lookup.
326 /// \param Name The name of the module to look up.
328 /// \param Context The module for which we will look for a submodule. If
329 /// null, we will look for a top-level module.
331 /// \returns The named submodule, if known; otherwose, returns null.
332 Module *lookupModuleQualified(StringRef Name, Module *Context) const;
334 /// \brief Find a new module or submodule, or create it if it does not already
337 /// \param Name The name of the module to find or create.
339 /// \param Parent The module that will act as the parent of this submodule,
340 /// or NULL to indicate that this is a top-level module.
342 /// \param IsFramework Whether this is a framework module.
344 /// \param IsExplicit Whether this is an explicit submodule.
346 /// \returns The found or newly-created module, along with a boolean value
347 /// that will be true if the module is newly-created.
348 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
352 /// \brief Infer the contents of a framework module map from the given
353 /// framework directory.
354 Module *inferFrameworkModule(StringRef ModuleName,
355 const DirectoryEntry *FrameworkDir,
356 bool IsSystem, Module *Parent);
358 /// \brief Retrieve the module map file containing the definition of the given
361 /// \param Module The module whose module map file will be returned, if known.
363 /// \returns The file entry for the module map file containing the given
364 /// module, or NULL if the module definition was inferred.
365 const FileEntry *getContainingModuleMapFile(const Module *Module) const;
367 /// \brief Get the module map file that (along with the module name) uniquely
368 /// identifies this module.
370 /// The particular module that \c Name refers to may depend on how the module
371 /// was found in header search. However, the combination of \c Name and
372 /// this module map will be globally unique for top-level modules. In the case
373 /// of inferred modules, returns the module map that allowed the inference
374 /// (e.g. contained 'module *'). Otherwise, returns
375 /// getContainingModuleMapFile().
376 const FileEntry *getModuleMapFileForUniquing(const Module *M) const;
378 void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap);
380 /// \brief Get any module map files other than getModuleMapFileForUniquing(M)
381 /// that define submodules of a top-level module \p M. This is cheaper than
382 /// getting the module map file for each submodule individually, since the
383 /// expected number of results is very small.
384 AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) {
385 auto I = AdditionalModMaps.find(M);
386 if (I == AdditionalModMaps.end())
391 void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap) {
392 AdditionalModMaps[M].insert(ModuleMap);
395 /// \brief Resolve all of the unresolved exports in the given module.
397 /// \param Mod The module whose exports should be resolved.
399 /// \param Complain Whether to emit diagnostics for failures.
401 /// \returns true if any errors were encountered while resolving exports,
403 bool resolveExports(Module *Mod, bool Complain);
405 /// \brief Resolve all of the unresolved uses in the given module.
407 /// \param Mod The module whose uses should be resolved.
409 /// \param Complain Whether to emit diagnostics for failures.
411 /// \returns true if any errors were encountered while resolving uses,
413 bool resolveUses(Module *Mod, bool Complain);
415 /// \brief Resolve all of the unresolved conflicts in the given module.
417 /// \param Mod The module whose conflicts should be resolved.
419 /// \param Complain Whether to emit diagnostics for failures.
421 /// \returns true if any errors were encountered while resolving conflicts,
423 bool resolveConflicts(Module *Mod, bool Complain);
425 /// \brief Infers the (sub)module based on the given source location and
428 /// \param Loc The location within the source that we are querying, along
429 /// with its source manager.
431 /// \returns The module that owns this source location, or null if no
432 /// module owns this source location.
433 Module *inferModuleFromLocation(FullSourceLoc Loc);
435 /// \brief Sets the umbrella header of the given module to the given
437 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader);
439 /// \brief Sets the umbrella directory of the given module to the given
441 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir);
443 /// \brief Adds this header to the given module.
444 /// \param Role The role of the header wrt the module.
445 void addHeader(Module *Mod, Module::Header Header,
446 ModuleHeaderRole Role);
448 /// \brief Marks this header as being excluded from the given module.
449 void excludeHeader(Module *Mod, Module::Header Header);
451 /// \brief Parse the given module map file, and record any modules we
454 /// \param File The file to be parsed.
456 /// \param IsSystem Whether this module map file is in a system header
457 /// directory, and therefore should be considered a system module.
459 /// \param HomeDir The directory in which relative paths within this module
460 /// map file will be resolved.
462 /// \returns true if an error occurred, false otherwise.
463 bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
464 const DirectoryEntry *HomeDir);
466 /// \brief Dump the contents of the module map, for debugging purposes.
469 typedef llvm::StringMap<Module *>::const_iterator module_iterator;
470 module_iterator module_begin() const { return Modules.begin(); }
471 module_iterator module_end() const { return Modules.end(); }