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
29namespace clang {
30
31class DirectoryEntry;
32class FileEntry;
33class FileManager;
34class DiagnosticConsumer;
35class DiagnosticsEngine;
36class HeaderSearch;
37class ModuleMapParser;
38
39class ModuleMap {
40  SourceManager *SourceMgr;
41  IntrusiveRefCntPtr<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  /// \brief The top-level modules that are known.
59  llvm::StringMap<Module *> Modules;
60
61public:
62  /// \brief Describes the role of a module header.
63  enum ModuleHeaderRole {
64    /// \brief This header is normally included in the module.
65    NormalHeader,
66    /// \brief This header is included but private.
67    PrivateHeader,
68    /// \brief This header is explicitly excluded from the module.
69    ExcludedHeader
70    // Caution: Adding an enumerator needs other changes.
71    // Adjust the number of bits for KnownHeader::Storage.
72    // Adjust the bitfield HeaderFileInfo::HeaderRole size.
73    // Adjust the HeaderFileInfoTrait::ReadData streaming.
74    // Adjust the HeaderFileInfoTrait::EmitData streaming.
75  };
76
77  /// \brief A header that is known to reside within a given module,
78  /// whether it was included or excluded.
79  class KnownHeader {
80    llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage;
81
82  public:
83    KnownHeader() : Storage(0, NormalHeader) { }
84    KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { }
85
86    /// \brief Retrieve the module the header is stored in.
87    Module *getModule() const { return Storage.getPointer(); }
88
89    /// \brief The role of this header within the module.
90    ModuleHeaderRole getRole() const { return Storage.getInt(); }
91
92    /// \brief Whether this header is available in the module.
93    bool isAvailable() const {
94      return getRole() != ExcludedHeader && getModule()->isAvailable();
95    }
96
97    // \brief Whether this known header is valid (i.e., it has an
98    // associated module).
99    LLVM_EXPLICIT operator bool() const { return Storage.getPointer() != 0; }
100  };
101
102private:
103  typedef llvm::DenseMap<const FileEntry *, KnownHeader> HeadersMap;
104
105  /// \brief Mapping from each header to the module that owns the contents of
106  /// that header.
107  HeadersMap Headers;
108
109  /// \brief Mapping from directories with umbrella headers to the module
110  /// that is generated from the umbrella header.
111  ///
112  /// This mapping is used to map headers that haven't explicitly been named
113  /// in the module map over to the module that includes them via its umbrella
114  /// header.
115  llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
116
117  /// \brief A directory for which framework modules can be inferred.
118  struct InferredDirectory {
119    InferredDirectory() : InferModules(), InferSystemModules() { }
120
121    /// \brief Whether to infer modules from this directory.
122    unsigned InferModules : 1;
123
124    /// \brief Whether the modules we infer are [system] modules.
125    unsigned InferSystemModules : 1;
126
127    /// \brief The names of modules that cannot be inferred within this
128    /// directory.
129    SmallVector<std::string, 2> ExcludedModules;
130  };
131
132  /// \brief A mapping from directories to information about inferring
133  /// framework modules from within those directories.
134  llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
135
136  /// \brief Describes whether we haved parsed a particular file as a module
137  /// map.
138  llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
139
140  friend class ModuleMapParser;
141
142  /// \brief Resolve the given export declaration into an actual export
143  /// declaration.
144  ///
145  /// \param Mod The module in which we're resolving the export declaration.
146  ///
147  /// \param Unresolved The export declaration to resolve.
148  ///
149  /// \param Complain Whether this routine should complain about unresolvable
150  /// exports.
151  ///
152  /// \returns The resolved export declaration, which will have a NULL pointer
153  /// if the export could not be resolved.
154  Module::ExportDecl
155  resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
156                bool Complain) const;
157
158  /// \brief Resolve the given module id to an actual module.
159  ///
160  /// \param Id The module-id to resolve.
161  ///
162  /// \param Mod The module in which we're resolving the module-id.
163  ///
164  /// \param Complain Whether this routine should complain about unresolvable
165  /// module-ids.
166  ///
167  /// \returns The resolved module, or null if the module-id could not be
168  /// resolved.
169  Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
170
171public:
172  /// \brief Construct a new module map.
173  ///
174  /// \param FileMgr The file manager used to find module files and headers.
175  /// This file manager should be shared with the header-search mechanism, since
176  /// they will refer to the same headers.
177  ///
178  /// \param DC A diagnostic consumer that will be cloned for use in generating
179  /// diagnostics.
180  ///
181  /// \param LangOpts Language options for this translation unit.
182  ///
183  /// \param Target The target for this translation unit.
184  ModuleMap(FileManager &FileMgr, DiagnosticConsumer &DC,
185            const LangOptions &LangOpts, const TargetInfo *Target,
186            HeaderSearch &HeaderInfo);
187
188  /// \brief Destroy the module map.
189  ///
190  ~ModuleMap();
191
192  /// \brief Set the target information.
193  void setTarget(const TargetInfo &Target);
194
195  /// \brief Set the directory that contains Clang-supplied include
196  /// files, such as our stdarg.h or tgmath.h.
197  void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
198    BuiltinIncludeDir = Dir;
199  }
200
201  /// \brief Retrieve the module that owns the given header file, if any.
202  ///
203  /// \param File The header file that is likely to be included.
204  ///
205  /// \returns The module KnownHeader, which provides the module that owns the
206  /// given header file.  The KnownHeader is default constructed to indicate
207  /// that no module owns this header file.
208  KnownHeader findModuleForHeader(const FileEntry *File);
209
210  /// \brief Determine whether the given header is part of a module
211  /// marked 'unavailable'.
212  bool isHeaderInUnavailableModule(const FileEntry *Header) const;
213
214  /// \brief Retrieve a module with the given name.
215  ///
216  /// \param Name The name of the module to look up.
217  ///
218  /// \returns The named module, if known; otherwise, returns null.
219  Module *findModule(StringRef Name) const;
220
221  /// \brief Retrieve a module with the given name using lexical name lookup,
222  /// starting at the given context.
223  ///
224  /// \param Name The name of the module to look up.
225  ///
226  /// \param Context The module context, from which we will perform lexical
227  /// name lookup.
228  ///
229  /// \returns The named module, if known; otherwise, returns null.
230  Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
231
232  /// \brief Retrieve a module with the given name within the given context,
233  /// using direct (qualified) name lookup.
234  ///
235  /// \param Name The name of the module to look up.
236  ///
237  /// \param Context The module for which we will look for a submodule. If
238  /// null, we will look for a top-level module.
239  ///
240  /// \returns The named submodule, if known; otherwose, returns null.
241  Module *lookupModuleQualified(StringRef Name, Module *Context) const;
242
243  /// \brief Find a new module or submodule, or create it if it does not already
244  /// exist.
245  ///
246  /// \param Name The name of the module to find or create.
247  ///
248  /// \param Parent The module that will act as the parent of this submodule,
249  /// or NULL to indicate that this is a top-level module.
250  ///
251  /// \param IsFramework Whether this is a framework module.
252  ///
253  /// \param IsExplicit Whether this is an explicit submodule.
254  ///
255  /// \returns The found or newly-created module, along with a boolean value
256  /// that will be true if the module is newly-created.
257  std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
258                                               bool IsFramework,
259                                               bool IsExplicit);
260
261  /// \brief Determine whether we can infer a framework module a framework
262  /// with the given name in the given
263  ///
264  /// \param ParentDir The directory that is the parent of the framework
265  /// directory.
266  ///
267  /// \param Name The name of the module.
268  ///
269  /// \param IsSystem Will be set to 'true' if the inferred module must be a
270  /// system module.
271  ///
272  /// \returns true if we are allowed to infer a framework module, and false
273  /// otherwise.
274  bool canInferFrameworkModule(const DirectoryEntry *ParentDir,
275                               StringRef Name, bool &IsSystem) const;
276
277  /// \brief Infer the contents of a framework module map from the given
278  /// framework directory.
279  Module *inferFrameworkModule(StringRef ModuleName,
280                               const DirectoryEntry *FrameworkDir,
281                               bool IsSystem, Module *Parent);
282
283  /// \brief Retrieve the module map file containing the definition of the given
284  /// module.
285  ///
286  /// \param Module The module whose module map file will be returned, if known.
287  ///
288  /// \returns The file entry for the module map file containing the given
289  /// module, or NULL if the module definition was inferred.
290  const FileEntry *getContainingModuleMapFile(Module *Module) const;
291
292  /// \brief Resolve all of the unresolved exports in the given module.
293  ///
294  /// \param Mod The module whose exports should be resolved.
295  ///
296  /// \param Complain Whether to emit diagnostics for failures.
297  ///
298  /// \returns true if any errors were encountered while resolving exports,
299  /// false otherwise.
300  bool resolveExports(Module *Mod, bool Complain);
301
302  /// \brief Resolve all of the unresolved conflicts in the given module.
303  ///
304  /// \param Mod The module whose conflicts should be resolved.
305  ///
306  /// \param Complain Whether to emit diagnostics for failures.
307  ///
308  /// \returns true if any errors were encountered while resolving conflicts,
309  /// false otherwise.
310  bool resolveConflicts(Module *Mod, bool Complain);
311
312  /// \brief Infers the (sub)module based on the given source location and
313  /// source manager.
314  ///
315  /// \param Loc The location within the source that we are querying, along
316  /// with its source manager.
317  ///
318  /// \returns The module that owns this source location, or null if no
319  /// module owns this source location.
320  Module *inferModuleFromLocation(FullSourceLoc Loc);
321
322  /// \brief Sets the umbrella header of the given module to the given
323  /// header.
324  void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader);
325
326  /// \brief Sets the umbrella directory of the given module to the given
327  /// directory.
328  void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir);
329
330  /// \brief Adds this header to the given module.
331  /// \param Role The role of the header wrt the module.
332  void addHeader(Module *Mod, const FileEntry *Header,
333                 ModuleHeaderRole Role);
334
335  /// \brief Parse the given module map file, and record any modules we
336  /// encounter.
337  ///
338  /// \param File The file to be parsed.
339  ///
340  /// \param IsSystem Whether this module map file is in a system header
341  /// directory, and therefore should be considered a system module.
342  ///
343  /// \returns true if an error occurred, false otherwise.
344  bool parseModuleMapFile(const FileEntry *File, bool IsSystem);
345
346  /// \brief Dump the contents of the module map, for debugging purposes.
347  void dump();
348
349  typedef llvm::StringMap<Module *>::const_iterator module_iterator;
350  module_iterator module_begin() const { return Modules.begin(); }
351  module_iterator module_end()   const { return Modules.end(); }
352};
353
354}
355#endif
356