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