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#ifndef LLVM_CLANG_LEX_MODULEMAP_H
16#define LLVM_CLANG_LEX_MODULEMAP_H
17
18#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/Module.h"
20#include "clang/Basic/SourceLocation.h"
21#include "clang/Basic/SourceManager.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/TinyPtrVector.h"
30#include "llvm/ADT/Twine.h"
31#include <algorithm>
32#include <memory>
33#include <string>
34#include <utility>
35
36namespace clang {
37
38class DirectoryEntry;
39class FileEntry;
40class FileManager;
41class DiagnosticConsumer;
42class DiagnosticsEngine;
43class HeaderSearch;
44class ModuleMapParser;
45
46/// \brief A mechanism to observe the actions of the module map parser as it
47/// reads module map files.
48class ModuleMapCallbacks {
49public:
50  virtual ~ModuleMapCallbacks() = default;
51
52  /// \brief Called when a module map file has been read.
53  ///
54  /// \param FileStart A SourceLocation referring to the start of the file's
55  /// contents.
56  /// \param File The file itself.
57  /// \param IsSystem Whether this is a module map from a system include path.
58  virtual void moduleMapFileRead(SourceLocation FileStart,
59                                 const FileEntry &File, bool IsSystem) {}
60
61  /// \brief Called when a header is added during module map parsing.
62  ///
63  /// \param Filename The header file itself.
64  virtual void moduleMapAddHeader(StringRef Filename) {}
65
66  /// \brief Called when an umbrella header is added during module map parsing.
67  ///
68  /// \param FileMgr FileManager instance
69  /// \param Header The umbrella header to collect.
70  virtual void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
71                                          const FileEntry *Header) {}
72};
73
74class ModuleMap {
75  SourceManager &SourceMgr;
76  DiagnosticsEngine &Diags;
77  const LangOptions &LangOpts;
78  const TargetInfo *Target;
79  HeaderSearch &HeaderInfo;
80
81  llvm::SmallVector<std::unique_ptr<ModuleMapCallbacks>, 1> Callbacks;
82
83  /// \brief The directory used for Clang-supplied, builtin include headers,
84  /// such as "stdint.h".
85  const DirectoryEntry *BuiltinIncludeDir = nullptr;
86
87  /// \brief Language options used to parse the module map itself.
88  ///
89  /// These are always simple C language options.
90  LangOptions MMapLangOpts;
91
92  /// The module that the main source file is associated with (the module
93  /// named LangOpts::CurrentModule, if we've loaded it).
94  Module *SourceModule = nullptr;
95
96  /// The global module for the current TU, if we still own it. (Ownership is
97  /// transferred if/when we create an enclosing module.
98  std::unique_ptr<Module> PendingGlobalModule;
99
100  /// \brief The top-level modules that are known.
101  llvm::StringMap<Module *> Modules;
102
103  /// \brief The number of modules we have created in total.
104  unsigned NumCreatedModules = 0;
105
106public:
107  /// \brief Flags describing the role of a module header.
108  enum ModuleHeaderRole {
109    /// \brief This header is normally included in the module.
110    NormalHeader  = 0x0,
111    /// \brief This header is included but private.
112    PrivateHeader = 0x1,
113    /// \brief This header is part of the module (for layering purposes) but
114    /// should be textually included.
115    TextualHeader = 0x2,
116    // Caution: Adding an enumerator needs other changes.
117    // Adjust the number of bits for KnownHeader::Storage.
118    // Adjust the bitfield HeaderFileInfo::HeaderRole size.
119    // Adjust the HeaderFileInfoTrait::ReadData streaming.
120    // Adjust the HeaderFileInfoTrait::EmitData streaming.
121    // Adjust ModuleMap::addHeader.
122  };
123
124  /// Convert a header kind to a role. Requires Kind to not be HK_Excluded.
125  static ModuleHeaderRole headerKindToRole(Module::HeaderKind Kind);
126  /// Convert a header role to a kind.
127  static Module::HeaderKind headerRoleToKind(ModuleHeaderRole Role);
128
129  /// \brief A header that is known to reside within a given module,
130  /// whether it was included or excluded.
131  class KnownHeader {
132    llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage;
133
134  public:
135    KnownHeader() : Storage(nullptr, NormalHeader) { }
136    KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { }
137
138    friend bool operator==(const KnownHeader &A, const KnownHeader &B) {
139      return A.Storage == B.Storage;
140    }
141    friend bool operator!=(const KnownHeader &A, const KnownHeader &B) {
142      return A.Storage != B.Storage;
143    }
144
145    /// \brief Retrieve the module the header is stored in.
146    Module *getModule() const { return Storage.getPointer(); }
147
148    /// \brief The role of this header within the module.
149    ModuleHeaderRole getRole() const { return Storage.getInt(); }
150
151    /// \brief Whether this header is available in the module.
152    bool isAvailable() const {
153      return getModule()->isAvailable();
154    }
155
156    /// \brief Whether this header is accessible from the specified module.
157    bool isAccessibleFrom(Module *M) const {
158      return !(getRole() & PrivateHeader) ||
159             (M && M->getTopLevelModule() == getModule()->getTopLevelModule());
160    }
161
162    // \brief Whether this known header is valid (i.e., it has an
163    // associated module).
164    explicit operator bool() const {
165      return Storage.getPointer() != nullptr;
166    }
167  };
168
169  typedef llvm::SmallPtrSet<const FileEntry *, 1> AdditionalModMapsSet;
170
171private:
172  typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1>>
173  HeadersMap;
174
175  /// \brief Mapping from each header to the module that owns the contents of
176  /// that header.
177  HeadersMap Headers;
178
179  /// Map from file sizes to modules with lazy header directives of that size.
180  mutable llvm::DenseMap<off_t, llvm::TinyPtrVector<Module*>> LazyHeadersBySize;
181  /// Map from mtimes to modules with lazy header directives with those mtimes.
182  mutable llvm::DenseMap<time_t, llvm::TinyPtrVector<Module*>>
183              LazyHeadersByModTime;
184
185  /// \brief Mapping from directories with umbrella headers to the module
186  /// that is generated from the umbrella header.
187  ///
188  /// This mapping is used to map headers that haven't explicitly been named
189  /// in the module map over to the module that includes them via its umbrella
190  /// header.
191  llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
192
193  /// \brief The set of attributes that can be attached to a module.
194  struct Attributes {
195    Attributes()
196        : IsSystem(), IsExternC(), IsExhaustive(), NoUndeclaredIncludes() {}
197
198    /// \brief Whether this is a system module.
199    unsigned IsSystem : 1;
200
201    /// \brief Whether this is an extern "C" module.
202    unsigned IsExternC : 1;
203
204    /// \brief Whether this is an exhaustive set of configuration macros.
205    unsigned IsExhaustive : 1;
206
207    /// \brief Whether files in this module can only include non-modular headers
208    /// and headers from used modules.
209    unsigned NoUndeclaredIncludes : 1;
210  };
211
212  /// \brief A directory for which framework modules can be inferred.
213  struct InferredDirectory {
214    InferredDirectory() : InferModules() {}
215
216    /// \brief Whether to infer modules from this directory.
217    unsigned InferModules : 1;
218
219    /// \brief The attributes to use for inferred modules.
220    Attributes Attrs;
221
222    /// \brief If \c InferModules is non-zero, the module map file that allowed
223    /// inferred modules.  Otherwise, nullptr.
224    const FileEntry *ModuleMapFile;
225
226    /// \brief The names of modules that cannot be inferred within this
227    /// directory.
228    SmallVector<std::string, 2> ExcludedModules;
229  };
230
231  /// \brief A mapping from directories to information about inferring
232  /// framework modules from within those directories.
233  llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
234
235  /// A mapping from an inferred module to the module map that allowed the
236  /// inference.
237  llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy;
238
239  llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
240
241  /// \brief Describes whether we haved parsed a particular file as a module
242  /// map.
243  llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
244
245  friend class ModuleMapParser;
246
247  /// \brief Resolve the given export declaration into an actual export
248  /// declaration.
249  ///
250  /// \param Mod The module in which we're resolving the export declaration.
251  ///
252  /// \param Unresolved The export declaration to resolve.
253  ///
254  /// \param Complain Whether this routine should complain about unresolvable
255  /// exports.
256  ///
257  /// \returns The resolved export declaration, which will have a NULL pointer
258  /// if the export could not be resolved.
259  Module::ExportDecl
260  resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
261                bool Complain) const;
262
263  /// \brief Resolve the given module id to an actual module.
264  ///
265  /// \param Id The module-id to resolve.
266  ///
267  /// \param Mod The module in which we're resolving the module-id.
268  ///
269  /// \param Complain Whether this routine should complain about unresolvable
270  /// module-ids.
271  ///
272  /// \returns The resolved module, or null if the module-id could not be
273  /// resolved.
274  Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
275
276  /// Add an unresolved header to a module.
277  void addUnresolvedHeader(Module *Mod,
278                           Module::UnresolvedHeaderDirective Header);
279
280  /// Look up the given header directive to find an actual header file.
281  ///
282  /// \param M The module in which we're resolving the header directive.
283  /// \param Header The header directive to resolve.
284  /// \param RelativePathName Filled in with the relative path name from the
285  ///        module to the resolved header.
286  /// \return The resolved file, if any.
287  const FileEntry *findHeader(Module *M,
288                              const Module::UnresolvedHeaderDirective &Header,
289                              SmallVectorImpl<char> &RelativePathName);
290
291  /// Resolve the given header directive.
292  void resolveHeader(Module *M,
293                     const Module::UnresolvedHeaderDirective &Header);
294
295  /// Attempt to resolve the specified header directive as naming a builtin
296  /// header.
297  /// \return \c true if a corresponding builtin header was found.
298  bool resolveAsBuiltinHeader(Module *M,
299                              const Module::UnresolvedHeaderDirective &Header);
300
301  /// \brief Looks up the modules that \p File corresponds to.
302  ///
303  /// If \p File represents a builtin header within Clang's builtin include
304  /// directory, this also loads all of the module maps to see if it will get
305  /// associated with a specific module (e.g. in /usr/include).
306  HeadersMap::iterator findKnownHeader(const FileEntry *File);
307
308  /// \brief Searches for a module whose umbrella directory contains \p File.
309  ///
310  /// \param File The header to search for.
311  ///
312  /// \param IntermediateDirs On success, contains the set of directories
313  /// searched before finding \p File.
314  KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
315                    SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
316
317  /// \brief Given that \p File is not in the Headers map, look it up within
318  /// umbrella directories and find or create a module for it.
319  KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File);
320
321  /// \brief A convenience method to determine if \p File is (possibly nested)
322  /// in an umbrella directory.
323  bool isHeaderInUmbrellaDirs(const FileEntry *File) {
324    SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
325    return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
326  }
327
328  Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
329                               Attributes Attrs, Module *Parent);
330
331public:
332  /// \brief Construct a new module map.
333  ///
334  /// \param SourceMgr The source manager used to find module files and headers.
335  /// This source manager should be shared with the header-search mechanism,
336  /// since they will refer to the same headers.
337  ///
338  /// \param Diags A diagnostic engine used for diagnostics.
339  ///
340  /// \param LangOpts Language options for this translation unit.
341  ///
342  /// \param Target The target for this translation unit.
343  ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
344            const LangOptions &LangOpts, const TargetInfo *Target,
345            HeaderSearch &HeaderInfo);
346
347  /// \brief Destroy the module map.
348  ///
349  ~ModuleMap();
350
351  /// \brief Set the target information.
352  void setTarget(const TargetInfo &Target);
353
354  /// \brief Set the directory that contains Clang-supplied include
355  /// files, such as our stdarg.h or tgmath.h.
356  void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
357    BuiltinIncludeDir = Dir;
358  }
359
360  /// \brief Get the directory that contains Clang-supplied include files.
361  const DirectoryEntry *getBuiltinDir() const {
362    return BuiltinIncludeDir;
363  }
364
365  /// \brief Is this a compiler builtin header?
366  static bool isBuiltinHeader(StringRef FileName);
367
368  /// \brief Add a module map callback.
369  void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
370    Callbacks.push_back(std::move(Callback));
371  }
372
373  /// \brief Retrieve the module that owns the given header file, if any.
374  ///
375  /// \param File The header file that is likely to be included.
376  ///
377  /// \param AllowTextual If \c true and \p File is a textual header, return
378  /// its owning module. Otherwise, no KnownHeader will be returned if the
379  /// file is only known as a textual header.
380  ///
381  /// \returns The module KnownHeader, which provides the module that owns the
382  /// given header file.  The KnownHeader is default constructed to indicate
383  /// that no module owns this header file.
384  KnownHeader findModuleForHeader(const FileEntry *File,
385                                  bool AllowTextual = false);
386
387  /// \brief Retrieve all the modules that contain the given header file. This
388  /// may not include umbrella modules, nor information from external sources,
389  /// if they have not yet been inferred / loaded.
390  ///
391  /// Typically, \ref findModuleForHeader should be used instead, as it picks
392  /// the preferred module for the header.
393  ArrayRef<KnownHeader> findAllModulesForHeader(const FileEntry *File) const;
394
395  /// Resolve all lazy header directives for the specified file.
396  ///
397  /// This ensures that the HeaderFileInfo on HeaderSearch is up to date. This
398  /// is effectively internal, but is exposed so HeaderSearch can call it.
399  void resolveHeaderDirectives(const FileEntry *File) const;
400
401  /// Resolve all lazy header directives for the specified module.
402  void resolveHeaderDirectives(Module *Mod) const;
403
404  /// \brief Reports errors if a module must not include a specific file.
405  ///
406  /// \param RequestingModule The module including a file.
407  ///
408  /// \param RequestingModuleIsModuleInterface \c true if the inclusion is in
409  ///        the interface of RequestingModule, \c false if it's in the
410  ///        implementation of RequestingModule. Value is ignored and
411  ///        meaningless if RequestingModule is nullptr.
412  ///
413  /// \param FilenameLoc The location of the inclusion's filename.
414  ///
415  /// \param Filename The included filename as written.
416  ///
417  /// \param File The included file.
418  void diagnoseHeaderInclusion(Module *RequestingModule,
419                               bool RequestingModuleIsModuleInterface,
420                               SourceLocation FilenameLoc, StringRef Filename,
421                               const FileEntry *File);
422
423  /// \brief Determine whether the given header is part of a module
424  /// marked 'unavailable'.
425  bool isHeaderInUnavailableModule(const FileEntry *Header) const;
426
427  /// \brief Determine whether the given header is unavailable as part
428  /// of the specified module.
429  bool isHeaderUnavailableInModule(const FileEntry *Header,
430                                   const Module *RequestingModule) const;
431
432  /// \brief Retrieve a module with the given name.
433  ///
434  /// \param Name The name of the module to look up.
435  ///
436  /// \returns The named module, if known; otherwise, returns null.
437  Module *findModule(StringRef Name) const;
438
439  /// \brief Retrieve a module with the given name using lexical name lookup,
440  /// starting at the given context.
441  ///
442  /// \param Name The name of the module to look up.
443  ///
444  /// \param Context The module context, from which we will perform lexical
445  /// name lookup.
446  ///
447  /// \returns The named module, if known; otherwise, returns null.
448  Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
449
450  /// \brief Retrieve a module with the given name within the given context,
451  /// using direct (qualified) name lookup.
452  ///
453  /// \param Name The name of the module to look up.
454  ///
455  /// \param Context The module for which we will look for a submodule. If
456  /// null, we will look for a top-level module.
457  ///
458  /// \returns The named submodule, if known; otherwose, returns null.
459  Module *lookupModuleQualified(StringRef Name, Module *Context) const;
460
461  /// \brief Find a new module or submodule, or create it if it does not already
462  /// exist.
463  ///
464  /// \param Name The name of the module to find or create.
465  ///
466  /// \param Parent The module that will act as the parent of this submodule,
467  /// or NULL to indicate that this is a top-level module.
468  ///
469  /// \param IsFramework Whether this is a framework module.
470  ///
471  /// \param IsExplicit Whether this is an explicit submodule.
472  ///
473  /// \returns The found or newly-created module, along with a boolean value
474  /// that will be true if the module is newly-created.
475  std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
476                                               bool IsFramework,
477                                               bool IsExplicit);
478
479  /// \brief Create a 'global module' for a C++ Modules TS module interface
480  /// unit.
481  ///
482  /// We model the global module as a submodule of the module interface unit.
483  /// Unfortunately, we can't create the module interface unit's Module until
484  /// later, because we don't know what it will be called.
485  Module *createGlobalModuleForInterfaceUnit(SourceLocation Loc);
486
487  /// \brief Create a new module for a C++ Modules TS module interface unit.
488  /// The module must not already exist, and will be configured for the current
489  /// compilation.
490  ///
491  /// Note that this also sets the current module to the newly-created module.
492  ///
493  /// \returns The newly-created module.
494  Module *createModuleForInterfaceUnit(SourceLocation Loc, StringRef Name,
495                                       Module *GlobalModule);
496
497  /// \brief Infer the contents of a framework module map from the given
498  /// framework directory.
499  Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
500                               bool IsSystem, Module *Parent);
501
502  /// \brief Retrieve the module map file containing the definition of the given
503  /// module.
504  ///
505  /// \param Module The module whose module map file will be returned, if known.
506  ///
507  /// \returns The file entry for the module map file containing the given
508  /// module, or NULL if the module definition was inferred.
509  const FileEntry *getContainingModuleMapFile(const Module *Module) const;
510
511  /// \brief Get the module map file that (along with the module name) uniquely
512  /// identifies this module.
513  ///
514  /// The particular module that \c Name refers to may depend on how the module
515  /// was found in header search. However, the combination of \c Name and
516  /// this module map will be globally unique for top-level modules. In the case
517  /// of inferred modules, returns the module map that allowed the inference
518  /// (e.g. contained 'module *'). Otherwise, returns
519  /// getContainingModuleMapFile().
520  const FileEntry *getModuleMapFileForUniquing(const Module *M) const;
521
522  void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap);
523
524  /// \brief Get any module map files other than getModuleMapFileForUniquing(M)
525  /// that define submodules of a top-level module \p M. This is cheaper than
526  /// getting the module map file for each submodule individually, since the
527  /// expected number of results is very small.
528  AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) {
529    auto I = AdditionalModMaps.find(M);
530    if (I == AdditionalModMaps.end())
531      return nullptr;
532    return &I->second;
533  }
534
535  void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap) {
536    AdditionalModMaps[M].insert(ModuleMap);
537  }
538
539  /// \brief Resolve all of the unresolved exports in the given module.
540  ///
541  /// \param Mod The module whose exports should be resolved.
542  ///
543  /// \param Complain Whether to emit diagnostics for failures.
544  ///
545  /// \returns true if any errors were encountered while resolving exports,
546  /// false otherwise.
547  bool resolveExports(Module *Mod, bool Complain);
548
549  /// \brief Resolve all of the unresolved uses in the given module.
550  ///
551  /// \param Mod The module whose uses should be resolved.
552  ///
553  /// \param Complain Whether to emit diagnostics for failures.
554  ///
555  /// \returns true if any errors were encountered while resolving uses,
556  /// false otherwise.
557  bool resolveUses(Module *Mod, bool Complain);
558
559  /// \brief Resolve all of the unresolved conflicts in the given module.
560  ///
561  /// \param Mod The module whose conflicts should be resolved.
562  ///
563  /// \param Complain Whether to emit diagnostics for failures.
564  ///
565  /// \returns true if any errors were encountered while resolving conflicts,
566  /// false otherwise.
567  bool resolveConflicts(Module *Mod, bool Complain);
568
569  /// \brief Sets the umbrella header of the given module to the given
570  /// header.
571  void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
572                         Twine NameAsWritten);
573
574  /// \brief Sets the umbrella directory of the given module to the given
575  /// directory.
576  void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
577                      Twine NameAsWritten);
578
579  /// \brief Adds this header to the given module.
580  /// \param Role The role of the header wrt the module.
581  void addHeader(Module *Mod, Module::Header Header,
582                 ModuleHeaderRole Role, bool Imported = false);
583
584  /// \brief Marks this header as being excluded from the given module.
585  void excludeHeader(Module *Mod, Module::Header Header);
586
587  /// \brief Parse the given module map file, and record any modules we
588  /// encounter.
589  ///
590  /// \param File The file to be parsed.
591  ///
592  /// \param IsSystem Whether this module map file is in a system header
593  /// directory, and therefore should be considered a system module.
594  ///
595  /// \param HomeDir The directory in which relative paths within this module
596  ///        map file will be resolved.
597  ///
598  /// \param ID The FileID of the file to process, if we've already entered it.
599  ///
600  /// \param Offset [inout] On input the offset at which to start parsing. On
601  ///        output, the offset at which the module map terminated.
602  ///
603  /// \param ExternModuleLoc The location of the "extern module" declaration
604  ///        that caused us to load this module map file, if any.
605  ///
606  /// \returns true if an error occurred, false otherwise.
607  bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
608                          const DirectoryEntry *HomeDir, FileID ID = FileID(),
609                          unsigned *Offset = nullptr,
610                          SourceLocation ExternModuleLoc = SourceLocation());
611
612  /// \brief Dump the contents of the module map, for debugging purposes.
613  void dump();
614
615  typedef llvm::StringMap<Module *>::const_iterator module_iterator;
616  module_iterator module_begin() const { return Modules.begin(); }
617  module_iterator module_end()   const { return Modules.end(); }
618};
619
620} // end namespace clang
621
622#endif // LLVM_CLANG_LEX_MODULEMAP_H
623