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