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