1//===--- Module.h - Describe a module ---------------------------*- 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/// \file
11/// \brief Defines the clang::Module class, which describes a module in the
12/// source code.
13///
14//===----------------------------------------------------------------------===//
15#ifndef LLVM_CLANG_BASIC_MODULE_H
16#define LLVM_CLANG_BASIC_MODULE_H
17
18#include "clang/Basic/SourceLocation.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/PointerIntPair.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace llvm {
31  class raw_ostream;
32}
33
34namespace clang {
35
36class DirectoryEntry;
37class FileEntry;
38class FileManager;
39class LangOptions;
40class TargetInfo;
41class IdentifierInfo;
42
43/// \brief Describes the name of a module.
44typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
45
46/// \brief Describes a module or submodule.
47class Module {
48public:
49  /// \brief The name of this module.
50  std::string Name;
51
52  /// \brief The location of the module definition.
53  SourceLocation DefinitionLoc;
54
55  /// \brief The parent of this module. This will be NULL for the top-level
56  /// module.
57  Module *Parent;
58
59  /// \brief The module map file that (along with the module name) uniquely
60  /// identifies this module.
61  ///
62  /// The particular module that \c Name refers to may depend on how the module
63  /// was found in header search. However, the combination of \c Name and
64  /// \c ModuleMap will be globally unique for top-level modules. In the case of
65  /// inferred modules, \c ModuleMap will contain the module map that allowed
66  /// the inference (e.g. contained 'Module *') rather than the virtual
67  /// inferred module map file.
68  const FileEntry *ModuleMap;
69
70  /// \brief The umbrella header or directory.
71  llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
72
73private:
74  /// \brief The submodules of this module, indexed by name.
75  std::vector<Module *> SubModules;
76
77  /// \brief A mapping from the submodule name to the index into the
78  /// \c SubModules vector at which that submodule resides.
79  llvm::StringMap<unsigned> SubModuleIndex;
80
81  /// \brief The AST file if this is a top-level module which has a
82  /// corresponding serialized AST file, or null otherwise.
83  const FileEntry *ASTFile;
84
85  /// \brief The top-level headers associated with this module.
86  llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
87
88  /// \brief top-level header filenames that aren't resolved to FileEntries yet.
89  std::vector<std::string> TopHeaderNames;
90
91  /// \brief Cache of modules visible to lookup in this module.
92  mutable llvm::DenseSet<const Module*> VisibleModulesCache;
93
94public:
95  /// \brief The headers that are part of this module.
96  SmallVector<const FileEntry *, 2> NormalHeaders;
97
98  /// \brief The headers that are explicitly excluded from this module.
99  SmallVector<const FileEntry *, 2> ExcludedHeaders;
100
101  /// \brief The headers that are private to this module.
102  SmallVector<const FileEntry *, 2> PrivateHeaders;
103
104  /// \brief Information about a header directive as found in the module map
105  /// file.
106  struct HeaderDirective {
107    SourceLocation FileNameLoc;
108    std::string FileName;
109    bool IsUmbrella;
110  };
111
112  /// \brief Headers that are mentioned in the module map file but could not be
113  /// found on the file system.
114  SmallVector<HeaderDirective, 1> MissingHeaders;
115
116  /// \brief An individual requirement: a feature name and a flag indicating
117  /// the required state of that feature.
118  typedef std::pair<std::string, bool> Requirement;
119
120  /// \brief The set of language features required to use this module.
121  ///
122  /// If any of these requirements are not available, the \c IsAvailable bit
123  /// will be false to indicate that this (sub)module is not available.
124  SmallVector<Requirement, 2> Requirements;
125
126  /// \brief Whether this module is missing a feature from \c Requirements.
127  unsigned IsMissingRequirement : 1;
128
129  /// \brief Whether this module is available in the current translation unit.
130  ///
131  /// If the module is missing headers or does not meet all requirements then
132  /// this bit will be 0.
133  unsigned IsAvailable : 1;
134
135  /// \brief Whether this module was loaded from a module file.
136  unsigned IsFromModuleFile : 1;
137
138  /// \brief Whether this is a framework module.
139  unsigned IsFramework : 1;
140
141  /// \brief Whether this is an explicit submodule.
142  unsigned IsExplicit : 1;
143
144  /// \brief Whether this is a "system" module (which assumes that all
145  /// headers in it are system headers).
146  unsigned IsSystem : 1;
147
148  /// \brief Whether this is an 'extern "C"' module (which implicitly puts all
149  /// headers in it within an 'extern "C"' block, and allows the module to be
150  /// imported within such a block).
151  unsigned IsExternC : 1;
152
153  /// \brief Whether this is an inferred submodule (module * { ... }).
154  unsigned IsInferred : 1;
155
156  /// \brief Whether we should infer submodules for this module based on
157  /// the headers.
158  ///
159  /// Submodules can only be inferred for modules with an umbrella header.
160  unsigned InferSubmodules : 1;
161
162  /// \brief Whether, when inferring submodules, the inferred submodules
163  /// should be explicit.
164  unsigned InferExplicitSubmodules : 1;
165
166  /// \brief Whether, when inferring submodules, the inferr submodules should
167  /// export all modules they import (e.g., the equivalent of "export *").
168  unsigned InferExportWildcard : 1;
169
170  /// \brief Whether the set of configuration macros is exhaustive.
171  ///
172  /// When the set of configuration macros is exhaustive, meaning
173  /// that no identifier not in this list should affect how the module is
174  /// built.
175  unsigned ConfigMacrosExhaustive : 1;
176
177  /// \brief Describes the visibility of the various names within a
178  /// particular module.
179  enum NameVisibilityKind {
180    /// \brief All of the names in this module are hidden.
181    ///
182    Hidden,
183    /// \brief Only the macro names in this module are visible.
184    MacrosVisible,
185    /// \brief All of the names in this module are visible.
186    AllVisible
187  };
188
189  /// \brief The visibility of names within this particular module.
190  NameVisibilityKind NameVisibility;
191
192  /// \brief The location at which macros within this module became visible.
193  SourceLocation MacroVisibilityLoc;
194
195  /// \brief The location of the inferred submodule.
196  SourceLocation InferredSubmoduleLoc;
197
198  /// \brief The set of modules imported by this module, and on which this
199  /// module depends.
200  SmallVector<Module *, 2> Imports;
201
202  /// \brief Describes an exported module.
203  ///
204  /// The pointer is the module being re-exported, while the bit will be true
205  /// to indicate that this is a wildcard export.
206  typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
207
208  /// \brief The set of export declarations.
209  SmallVector<ExportDecl, 2> Exports;
210
211  /// \brief Describes an exported module that has not yet been resolved
212  /// (perhaps because the module it refers to has not yet been loaded).
213  struct UnresolvedExportDecl {
214    /// \brief The location of the 'export' keyword in the module map file.
215    SourceLocation ExportLoc;
216
217    /// \brief The name of the module.
218    ModuleId Id;
219
220    /// \brief Whether this export declaration ends in a wildcard, indicating
221    /// that all of its submodules should be exported (rather than the named
222    /// module itself).
223    bool Wildcard;
224  };
225
226  /// \brief The set of export declarations that have yet to be resolved.
227  SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
228
229  /// \brief The directly used modules.
230  SmallVector<Module *, 2> DirectUses;
231
232  /// \brief The set of use declarations that have yet to be resolved.
233  SmallVector<ModuleId, 2> UnresolvedDirectUses;
234
235  /// \brief A library or framework to link against when an entity from this
236  /// module is used.
237  struct LinkLibrary {
238    LinkLibrary() : IsFramework(false) { }
239    LinkLibrary(const std::string &Library, bool IsFramework)
240      : Library(Library), IsFramework(IsFramework) { }
241
242    /// \brief The library to link against.
243    ///
244    /// This will typically be a library or framework name, but can also
245    /// be an absolute path to the library or framework.
246    std::string Library;
247
248    /// \brief Whether this is a framework rather than a library.
249    bool IsFramework;
250  };
251
252  /// \brief The set of libraries or frameworks to link against when
253  /// an entity from this module is used.
254  llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
255
256  /// \brief The set of "configuration macros", which are macros that
257  /// (intentionally) change how this module is built.
258  std::vector<std::string> ConfigMacros;
259
260  /// \brief An unresolved conflict with another module.
261  struct UnresolvedConflict {
262    /// \brief The (unresolved) module id.
263    ModuleId Id;
264
265    /// \brief The message provided to the user when there is a conflict.
266    std::string Message;
267  };
268
269  /// \brief The list of conflicts for which the module-id has not yet been
270  /// resolved.
271  std::vector<UnresolvedConflict> UnresolvedConflicts;
272
273  /// \brief A conflict between two modules.
274  struct Conflict {
275    /// \brief The module that this module conflicts with.
276    Module *Other;
277
278    /// \brief The message provided to the user when there is a conflict.
279    std::string Message;
280  };
281
282  /// \brief The list of conflicts.
283  std::vector<Conflict> Conflicts;
284
285  /// \brief Construct a new module or submodule.
286  ///
287  /// For an explanation of \p ModuleMap, see Module::ModuleMap.
288  Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
289         const FileEntry *ModuleMap, bool IsFramework, bool IsExplicit);
290
291  ~Module();
292
293  /// \brief Determine whether this module is available for use within the
294  /// current translation unit.
295  bool isAvailable() const { return IsAvailable; }
296
297  /// \brief Determine whether this module is available for use within the
298  /// current translation unit.
299  ///
300  /// \param LangOpts The language options used for the current
301  /// translation unit.
302  ///
303  /// \param Target The target options used for the current translation unit.
304  ///
305  /// \param Req If this module is unavailable, this parameter
306  /// will be set to one of the requirements that is not met for use of
307  /// this module.
308  bool isAvailable(const LangOptions &LangOpts,
309                   const TargetInfo &Target,
310                   Requirement &Req,
311                   HeaderDirective &MissingHeader) const;
312
313  /// \brief Determine whether this module is a submodule.
314  bool isSubModule() const { return Parent != nullptr; }
315
316  /// \brief Determine whether this module is a submodule of the given other
317  /// module.
318  bool isSubModuleOf(const Module *Other) const;
319
320  /// \brief Determine whether this module is a part of a framework,
321  /// either because it is a framework module or because it is a submodule
322  /// of a framework module.
323  bool isPartOfFramework() const {
324    for (const Module *Mod = this; Mod; Mod = Mod->Parent)
325      if (Mod->IsFramework)
326        return true;
327
328    return false;
329  }
330
331  /// \brief Determine whether this module is a subframework of another
332  /// framework.
333  bool isSubFramework() const {
334    return IsFramework && Parent && Parent->isPartOfFramework();
335  }
336
337  /// \brief Retrieve the full name of this module, including the path from
338  /// its top-level module.
339  std::string getFullModuleName() const;
340
341  /// \brief Retrieve the top-level module for this (sub)module, which may
342  /// be this module.
343  Module *getTopLevelModule() {
344    return const_cast<Module *>(
345             const_cast<const Module *>(this)->getTopLevelModule());
346  }
347
348  /// \brief Retrieve the top-level module for this (sub)module, which may
349  /// be this module.
350  const Module *getTopLevelModule() const;
351
352  /// \brief Retrieve the name of the top-level module.
353  ///
354  StringRef getTopLevelModuleName() const {
355    return getTopLevelModule()->Name;
356  }
357
358  /// \brief The serialized AST file for this module, if one was created.
359  const FileEntry *getASTFile() const {
360    return getTopLevelModule()->ASTFile;
361  }
362
363  /// \brief Set the serialized AST file for the top-level module of this module.
364  void setASTFile(const FileEntry *File) {
365    assert((File == nullptr || getASTFile() == nullptr ||
366            getASTFile() == File) && "file path changed");
367    getTopLevelModule()->ASTFile = File;
368  }
369
370  /// \brief Retrieve the directory for which this module serves as the
371  /// umbrella.
372  const DirectoryEntry *getUmbrellaDir() const;
373
374  /// \brief Retrieve the header that serves as the umbrella header for this
375  /// module.
376  const FileEntry *getUmbrellaHeader() const {
377    return Umbrella.dyn_cast<const FileEntry *>();
378  }
379
380  /// \brief Determine whether this module has an umbrella directory that is
381  /// not based on an umbrella header.
382  bool hasUmbrellaDir() const {
383    return Umbrella && Umbrella.is<const DirectoryEntry *>();
384  }
385
386  /// \brief Add a top-level header associated with this module.
387  void addTopHeader(const FileEntry *File) {
388    assert(File);
389    TopHeaders.insert(File);
390  }
391
392  /// \brief Add a top-level header filename associated with this module.
393  void addTopHeaderFilename(StringRef Filename) {
394    TopHeaderNames.push_back(Filename);
395  }
396
397  /// \brief The top-level headers associated with this module.
398  ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
399
400  /// \brief Add the given feature requirement to the list of features
401  /// required by this module.
402  ///
403  /// \param Feature The feature that is required by this module (and
404  /// its submodules).
405  ///
406  /// \param RequiredState The required state of this feature: \c true
407  /// if it must be present, \c false if it must be absent.
408  ///
409  /// \param LangOpts The set of language options that will be used to
410  /// evaluate the availability of this feature.
411  ///
412  /// \param Target The target options that will be used to evaluate the
413  /// availability of this feature.
414  void addRequirement(StringRef Feature, bool RequiredState,
415                      const LangOptions &LangOpts,
416                      const TargetInfo &Target);
417
418  /// \brief Mark this module and all of its submodules as unavailable.
419  void markUnavailable(bool MissingRequirement = false);
420
421  /// \brief Find the submodule with the given name.
422  ///
423  /// \returns The submodule if found, or NULL otherwise.
424  Module *findSubmodule(StringRef Name) const;
425
426  /// \brief Determine whether the specified module would be visible to
427  /// a lookup at the end of this module.
428  ///
429  /// FIXME: This may return incorrect results for (submodules of) the
430  /// module currently being built, if it's queried before we see all
431  /// of its imports.
432  bool isModuleVisible(const Module *M) const {
433    if (VisibleModulesCache.empty())
434      buildVisibleModulesCache();
435    return VisibleModulesCache.count(M);
436  }
437
438  typedef std::vector<Module *>::iterator submodule_iterator;
439  typedef std::vector<Module *>::const_iterator submodule_const_iterator;
440
441  submodule_iterator submodule_begin() { return SubModules.begin(); }
442  submodule_const_iterator submodule_begin() const {return SubModules.begin();}
443  submodule_iterator submodule_end()   { return SubModules.end(); }
444  submodule_const_iterator submodule_end() const { return SubModules.end(); }
445
446  /// \brief Appends this module's list of exported modules to \p Exported.
447  ///
448  /// This provides a subset of immediately imported modules (the ones that are
449  /// directly exported), not the complete set of exported modules.
450  void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
451
452  static StringRef getModuleInputBufferName() {
453    return "<module-includes>";
454  }
455
456  /// \brief Print the module map for this module to the given stream.
457  ///
458  void print(raw_ostream &OS, unsigned Indent = 0) const;
459
460  /// \brief Dump the contents of this module to the given output stream.
461  void dump() const;
462
463private:
464  void buildVisibleModulesCache() const;
465};
466
467} // end namespace clang
468
469
470#endif // LLVM_CLANG_BASIC_MODULE_H
471