ModuleMap.h revision 2b49d1f0ad790a8a5d514af1be211591a746cb73
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/StringRef.h"
26#include "llvm/ADT/StringMap.h"
27#include <string>
28
29namespace clang {
30
31class DirectoryEntry;
32class FileEntry;
33class FileManager;
34class DiagnosticConsumer;
35class DiagnosticsEngine;
36class ModuleMapParser;
37
38class ModuleMap {
39  SourceManager *SourceMgr;
40  IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
41  const LangOptions &LangOpts;
42  const TargetInfo *Target;
43
44  /// \brief The directory used for Clang-supplied, builtin include headers,
45  /// such as "stdint.h".
46  const DirectoryEntry *BuiltinIncludeDir;
47
48  /// \brief Language options used to parse the module map itself.
49  ///
50  /// These are always simple C language options.
51  LangOptions MMapLangOpts;
52
53  /// \brief The top-level modules that are known.
54  llvm::StringMap<Module *> Modules;
55
56  /// \brief A header that is known to reside within a given module,
57  /// whether it was included or excluded.
58  class KnownHeader {
59    llvm::PointerIntPair<Module *, 1, bool> Storage;
60
61  public:
62    KnownHeader() : Storage(0, false) { }
63    KnownHeader(Module *M, bool Excluded) : Storage(M, Excluded) { }
64
65    /// \brief Retrieve the module the header is stored in.
66    Module *getModule() const { return Storage.getPointer(); }
67
68    /// \brief Whether this header is explicitly excluded from the module.
69    bool isExcluded() const { return Storage.getInt(); }
70
71    /// \brief Whether this header is available in the module.
72    bool isAvailable() const {
73      return !isExcluded() && getModule()->isAvailable();
74    }
75
76    // \brief Whether this known header is valid (i.e., it has an
77    // associated module).
78    operator bool() const { return Storage.getPointer() != 0; }
79  };
80
81  typedef llvm::DenseMap<const FileEntry *, KnownHeader> HeadersMap;
82
83  /// \brief Mapping from each header to the module that owns the contents of the
84  /// that header.
85  HeadersMap Headers;
86
87  /// \brief Mapping from directories with umbrella headers to the module
88  /// that is generated from the umbrella header.
89  ///
90  /// This mapping is used to map headers that haven't explicitly been named
91  /// in the module map over to the module that includes them via its umbrella
92  /// header.
93  llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
94
95  friend class ModuleMapParser;
96
97  /// \brief Resolve the given export declaration into an actual export
98  /// declaration.
99  ///
100  /// \param Mod The module in which we're resolving the export declaration.
101  ///
102  /// \param Unresolved The export declaration to resolve.
103  ///
104  /// \param Complain Whether this routine should complain about unresolvable
105  /// exports.
106  ///
107  /// \returns The resolved export declaration, which will have a NULL pointer
108  /// if the export could not be resolved.
109  Module::ExportDecl
110  resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
111                bool Complain);
112
113public:
114  /// \brief Construct a new module map.
115  ///
116  /// \param FileMgr The file manager used to find module files and headers.
117  /// This file manager should be shared with the header-search mechanism, since
118  /// they will refer to the same headers.
119  ///
120  /// \param DC A diagnostic consumer that will be cloned for use in generating
121  /// diagnostics.
122  ///
123  /// \param LangOpts Language options for this translation unit.
124  ///
125  /// \param Target The target for this translation unit.
126  ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
127            const LangOptions &LangOpts, const TargetInfo *Target);
128
129  /// \brief Destroy the module map.
130  ///
131  ~ModuleMap();
132
133  /// \brief Set the target information.
134  void setTarget(const TargetInfo &Target);
135
136  /// \brief Set the directory that contains Clang-supplied include
137  /// files, such as our stdarg.h or tgmath.h.
138  void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
139    BuiltinIncludeDir = Dir;
140  }
141
142  /// \brief Retrieve the module that owns the given header file, if any.
143  ///
144  /// \param File The header file that is likely to be included.
145  ///
146  /// \returns The module that owns the given header file, or null to indicate
147  /// that no module owns this header file.
148  Module *findModuleForHeader(const FileEntry *File);
149
150  /// \brief Determine whether the given header is part of a module
151  /// marked 'unavailable'.
152  bool isHeaderInUnavailableModule(const FileEntry *Header);
153
154  /// \brief Retrieve a module with the given name.
155  ///
156  /// \param Name The name of the module to look up.
157  ///
158  /// \returns The named module, if known; otherwise, returns null.
159  Module *findModule(StringRef Name);
160
161  /// \brief Retrieve a module with the given name using lexical name lookup,
162  /// starting at the given context.
163  ///
164  /// \param Name The name of the module to look up.
165  ///
166  /// \param Context The module context, from which we will perform lexical
167  /// name lookup.
168  ///
169  /// \returns The named module, if known; otherwise, returns null.
170  Module *lookupModuleUnqualified(StringRef Name, Module *Context);
171
172  /// \brief Retrieve a module with the given name within the given context,
173  /// using direct (qualified) name lookup.
174  ///
175  /// \param Name The name of the module to look up.
176  ///
177  /// \param Context The module for which we will look for a submodule. If
178  /// null, we will look for a top-level module.
179  ///
180  /// \returns The named submodule, if known; otherwose, returns null.
181  Module *lookupModuleQualified(StringRef Name, Module *Context);
182
183  /// \brief Find a new module or submodule, or create it if it does not already
184  /// exist.
185  ///
186  /// \param Name The name of the module to find or create.
187  ///
188  /// \param Parent The module that will act as the parent of this submodule,
189  /// or NULL to indicate that this is a top-level module.
190  ///
191  /// \param IsFramework Whether this is a framework module.
192  ///
193  /// \param IsExplicit Whether this is an explicit submodule.
194  ///
195  /// \returns The found or newly-created module, along with a boolean value
196  /// that will be true if the module is newly-created.
197  std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
198                                               bool IsFramework,
199                                               bool IsExplicit);
200
201  /// \brief Infer the contents of a framework module map from the given
202  /// framework directory.
203  Module *inferFrameworkModule(StringRef ModuleName,
204                               const DirectoryEntry *FrameworkDir,
205                               bool IsSystem, Module *Parent);
206
207  /// \brief Retrieve the module map file containing the definition of the given
208  /// module.
209  ///
210  /// \param Module The module whose module map file will be returned, if known.
211  ///
212  /// \returns The file entry for the module map file containing the given
213  /// module, or NULL if the module definition was inferred.
214  const FileEntry *getContainingModuleMapFile(Module *Module);
215
216  /// \brief Resolve all of the unresolved exports in the given module.
217  ///
218  /// \param Mod The module whose exports should be resolved.
219  ///
220  /// \param Complain Whether to emit diagnostics for failures.
221  ///
222  /// \returns true if any errors were encountered while resolving exports,
223  /// false otherwise.
224  bool resolveExports(Module *Mod, bool Complain);
225
226  /// \brief Infers the (sub)module based on the given source location and
227  /// source manager.
228  ///
229  /// \param Loc The location within the source that we are querying, along
230  /// with its source manager.
231  ///
232  /// \returns The module that owns this source location, or null if no
233  /// module owns this source location.
234  Module *inferModuleFromLocation(FullSourceLoc Loc);
235
236  /// \brief Sets the umbrella header of the given module to the given
237  /// header.
238  void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader);
239
240  /// \brief Sets the umbrella directory of the given module to the given
241  /// directory.
242  void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir);
243
244  /// \brief Adds this header to the given module.
245  /// \param Excluded Whether this header is explicitly excluded from the
246  /// module; otherwise, it's included in the module.
247  void addHeader(Module *Mod, const FileEntry *Header, bool Excluded);
248
249  /// \brief Parse the given module map file, and record any modules we
250  /// encounter.
251  ///
252  /// \param File The file to be parsed.
253  ///
254  /// \returns true if an error occurred, false otherwise.
255  bool parseModuleMapFile(const FileEntry *File);
256
257  /// \brief Dump the contents of the module map, for debugging purposes.
258  void dump();
259
260  typedef llvm::StringMap<Module *>::const_iterator module_iterator;
261  module_iterator module_begin() const { return Modules.begin(); }
262  module_iterator module_end()   const { return Modules.end(); }
263};
264
265}
266#endif
267