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;
67 /// \brief The number of modules we have created in total.
68 unsigned NumCreatedModules;
71 /// \brief Flags describing the role of a module header.
72 enum ModuleHeaderRole {
73 /// \brief This header is normally included in the module.
75 /// \brief This header is included but private.
77 /// \brief This header is part of the module (for layering purposes) but
78 /// should be textually included.
80 // Caution: Adding an enumerator needs other changes.
81 // Adjust the number of bits for KnownHeader::Storage.
82 // Adjust the bitfield HeaderFileInfo::HeaderRole size.
83 // Adjust the HeaderFileInfoTrait::ReadData streaming.
84 // Adjust the HeaderFileInfoTrait::EmitData streaming.
85 // Adjust ModuleMap::addHeader.
88 /// \brief A header that is known to reside within a given module,
89 /// whether it was included or excluded.
91 llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage;
94 KnownHeader() : Storage(nullptr, NormalHeader) { }
95 KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { }
97 /// \brief Retrieve the module the header is stored in.
98 Module *getModule() const { return Storage.getPointer(); }
100 /// \brief The role of this header within the module.
101 ModuleHeaderRole getRole() const { return Storage.getInt(); }
103 /// \brief Whether this header is available in the module.
104 bool isAvailable() const {
105 return getModule()->isAvailable();
108 // \brief Whether this known header is valid (i.e., it has an
109 // associated module).
110 explicit operator bool() const {
111 return Storage.getPointer() != nullptr;
115 typedef llvm::SmallPtrSet<const FileEntry *, 1> AdditionalModMapsSet;
118 typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> >
121 /// \brief Mapping from each header to the module that owns the contents of
125 /// \brief Mapping from directories with umbrella headers to the module
126 /// that is generated from the umbrella header.
128 /// This mapping is used to map headers that haven't explicitly been named
129 /// in the module map over to the module that includes them via its umbrella
131 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
133 /// \brief The set of attributes that can be attached to a module.
135 Attributes() : IsSystem(), IsExternC(), IsExhaustive() {}
137 /// \brief Whether this is a system module.
138 unsigned IsSystem : 1;
140 /// \brief Whether this is an extern "C" module.
141 unsigned IsExternC : 1;
143 /// \brief Whether this is an exhaustive set of configuration macros.
144 unsigned IsExhaustive : 1;
147 /// \brief A directory for which framework modules can be inferred.
148 struct InferredDirectory {
149 InferredDirectory() : InferModules() {}
151 /// \brief Whether to infer modules from this directory.
152 unsigned InferModules : 1;
154 /// \brief The attributes to use for inferred modules.
157 /// \brief If \c InferModules is non-zero, the module map file that allowed
158 /// inferred modules. Otherwise, nullptr.
159 const FileEntry *ModuleMapFile;
161 /// \brief The names of modules that cannot be inferred within this
163 SmallVector<std::string, 2> ExcludedModules;
166 /// \brief A mapping from directories to information about inferring
167 /// framework modules from within those directories.
168 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
170 /// A mapping from an inferred module to the module map that allowed the
172 llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy;
174 llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
176 /// \brief Describes whether we haved parsed a particular file as a module
178 llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
180 friend class ModuleMapParser;
182 /// \brief Resolve the given export declaration into an actual export
185 /// \param Mod The module in which we're resolving the export declaration.
187 /// \param Unresolved The export declaration to resolve.
189 /// \param Complain Whether this routine should complain about unresolvable
192 /// \returns The resolved export declaration, which will have a NULL pointer
193 /// if the export could not be resolved.
195 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
196 bool Complain) const;
198 /// \brief Resolve the given module id to an actual module.
200 /// \param Id The module-id to resolve.
202 /// \param Mod The module in which we're resolving the module-id.
204 /// \param Complain Whether this routine should complain about unresolvable
207 /// \returns The resolved module, or null if the module-id could not be
209 Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
211 /// \brief Looks up the modules that \p File corresponds to.
213 /// If \p File represents a builtin header within Clang's builtin include
214 /// directory, this also loads all of the module maps to see if it will get
215 /// associated with a specific module (e.g. in /usr/include).
216 HeadersMap::iterator findKnownHeader(const FileEntry *File);
218 /// \brief Searches for a module whose umbrella directory contains \p File.
220 /// \param File The header to search for.
222 /// \param IntermediateDirs On success, contains the set of directories
223 /// searched before finding \p File.
224 KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
225 SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
227 /// \brief A convenience method to determine if \p File is (possibly nested)
228 /// in an umbrella directory.
229 bool isHeaderInUmbrellaDirs(const FileEntry *File) {
230 SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
231 return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
234 Module *inferFrameworkModule(StringRef ModuleName,
235 const DirectoryEntry *FrameworkDir,
236 Attributes Attrs, Module *Parent);
239 /// \brief Construct a new module map.
241 /// \param SourceMgr The source manager used to find module files and headers.
242 /// This source manager should be shared with the header-search mechanism,
243 /// since they will refer to the same headers.
245 /// \param Diags A diagnostic engine used for diagnostics.
247 /// \param LangOpts Language options for this translation unit.
249 /// \param Target The target for this translation unit.
250 ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
251 const LangOptions &LangOpts, const TargetInfo *Target,
252 HeaderSearch &HeaderInfo);
254 /// \brief Destroy the module map.
258 /// \brief Set the target information.
259 void setTarget(const TargetInfo &Target);
261 /// \brief Set the directory that contains Clang-supplied include
262 /// files, such as our stdarg.h or tgmath.h.
263 void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
264 BuiltinIncludeDir = Dir;
267 /// \brief Retrieve the module that owns the given header file, if any.
269 /// \param File The header file that is likely to be included.
271 /// \param RequestingModule Specifies the module the header is intended to be
272 /// used from. Used to disambiguate if a header is present in multiple
275 /// \param IncludeTextualHeaders If \c true, also find textual headers. By
276 /// default, these are treated like excluded headers and result in no known
277 /// header being found.
279 /// \returns The module KnownHeader, which provides the module that owns the
280 /// given header file. The KnownHeader is default constructed to indicate
281 /// that no module owns this header file.
282 KnownHeader findModuleForHeader(const FileEntry *File,
283 Module *RequestingModule = nullptr,
284 bool IncludeTextualHeaders = false);
286 /// \brief Reports errors if a module must not include a specific file.
288 /// \param RequestingModule The module including a file.
290 /// \param FilenameLoc The location of the inclusion's filename.
292 /// \param Filename The included filename as written.
294 /// \param File The included file.
295 void diagnoseHeaderInclusion(Module *RequestingModule,
296 SourceLocation FilenameLoc, StringRef Filename,
297 const FileEntry *File);
299 /// \brief Determine whether the given header is part of a module
300 /// marked 'unavailable'.
301 bool isHeaderInUnavailableModule(const FileEntry *Header) const;
303 /// \brief Determine whether the given header is unavailable as part
304 /// of the specified module.
305 bool isHeaderUnavailableInModule(const FileEntry *Header,
306 const Module *RequestingModule) const;
308 /// \brief Retrieve a module with the given name.
310 /// \param Name The name of the module to look up.
312 /// \returns The named module, if known; otherwise, returns null.
313 Module *findModule(StringRef Name) const;
315 /// \brief Retrieve a module with the given name using lexical name lookup,
316 /// starting at the given context.
318 /// \param Name The name of the module to look up.
320 /// \param Context The module context, from which we will perform lexical
323 /// \returns The named module, if known; otherwise, returns null.
324 Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
326 /// \brief Retrieve a module with the given name within the given context,
327 /// using direct (qualified) name lookup.
329 /// \param Name The name of the module to look up.
331 /// \param Context The module for which we will look for a submodule. If
332 /// null, we will look for a top-level module.
334 /// \returns The named submodule, if known; otherwose, returns null.
335 Module *lookupModuleQualified(StringRef Name, Module *Context) const;
337 /// \brief Find a new module or submodule, or create it if it does not already
340 /// \param Name The name of the module to find or create.
342 /// \param Parent The module that will act as the parent of this submodule,
343 /// or NULL to indicate that this is a top-level module.
345 /// \param IsFramework Whether this is a framework module.
347 /// \param IsExplicit Whether this is an explicit submodule.
349 /// \returns The found or newly-created module, along with a boolean value
350 /// that will be true if the module is newly-created.
351 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
355 /// \brief Infer the contents of a framework module map from the given
356 /// framework directory.
357 Module *inferFrameworkModule(StringRef ModuleName,
358 const DirectoryEntry *FrameworkDir,
359 bool IsSystem, Module *Parent);
361 /// \brief Retrieve the module map file containing the definition of the given
364 /// \param Module The module whose module map file will be returned, if known.
366 /// \returns The file entry for the module map file containing the given
367 /// module, or NULL if the module definition was inferred.
368 const FileEntry *getContainingModuleMapFile(const Module *Module) const;
370 /// \brief Get the module map file that (along with the module name) uniquely
371 /// identifies this module.
373 /// The particular module that \c Name refers to may depend on how the module
374 /// was found in header search. However, the combination of \c Name and
375 /// this module map will be globally unique for top-level modules. In the case
376 /// of inferred modules, returns the module map that allowed the inference
377 /// (e.g. contained 'module *'). Otherwise, returns
378 /// getContainingModuleMapFile().
379 const FileEntry *getModuleMapFileForUniquing(const Module *M) const;
381 void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap);
383 /// \brief Get any module map files other than getModuleMapFileForUniquing(M)
384 /// that define submodules of a top-level module \p M. This is cheaper than
385 /// getting the module map file for each submodule individually, since the
386 /// expected number of results is very small.
387 AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) {
388 auto I = AdditionalModMaps.find(M);
389 if (I == AdditionalModMaps.end())
394 void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap) {
395 AdditionalModMaps[M].insert(ModuleMap);
398 /// \brief Resolve all of the unresolved exports in the given module.
400 /// \param Mod The module whose exports should be resolved.
402 /// \param Complain Whether to emit diagnostics for failures.
404 /// \returns true if any errors were encountered while resolving exports,
406 bool resolveExports(Module *Mod, bool Complain);
408 /// \brief Resolve all of the unresolved uses in the given module.
410 /// \param Mod The module whose uses should be resolved.
412 /// \param Complain Whether to emit diagnostics for failures.
414 /// \returns true if any errors were encountered while resolving uses,
416 bool resolveUses(Module *Mod, bool Complain);
418 /// \brief Resolve all of the unresolved conflicts in the given module.
420 /// \param Mod The module whose conflicts should be resolved.
422 /// \param Complain Whether to emit diagnostics for failures.
424 /// \returns true if any errors were encountered while resolving conflicts,
426 bool resolveConflicts(Module *Mod, bool Complain);
428 /// \brief Infers the (sub)module based on the given source location and
431 /// \param Loc The location within the source that we are querying, along
432 /// with its source manager.
434 /// \returns The module that owns this source location, or null if no
435 /// module owns this source location.
436 Module *inferModuleFromLocation(FullSourceLoc Loc);
438 /// \brief Sets the umbrella header of the given module to the given
440 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
441 Twine NameAsWritten);
443 /// \brief Sets the umbrella directory of the given module to the given
445 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
446 Twine NameAsWritten);
448 /// \brief Adds this header to the given module.
449 /// \param Role The role of the header wrt the module.
450 void addHeader(Module *Mod, Module::Header Header,
451 ModuleHeaderRole Role);
453 /// \brief Marks this header as being excluded from the given module.
454 void excludeHeader(Module *Mod, Module::Header Header);
456 /// \brief Parse the given module map file, and record any modules we
459 /// \param File The file to be parsed.
461 /// \param IsSystem Whether this module map file is in a system header
462 /// directory, and therefore should be considered a system module.
464 /// \param HomeDir The directory in which relative paths within this module
465 /// map file will be resolved.
467 /// \returns true if an error occurred, false otherwise.
468 bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
469 const DirectoryEntry *HomeDir);
471 /// \brief Dump the contents of the module map, for debugging purposes.
474 typedef llvm::StringMap<Module *>::const_iterator module_iterator;
475 module_iterator module_begin() const { return Modules.begin(); }
476 module_iterator module_end() const { return Modules.end(); }