• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 A library or framework to link against when an entity from this
187   /// module is used.
188   struct LinkLibrary {
LinkLibraryLinkLibrary189     LinkLibrary() : IsFramework(false) { }
LinkLibraryLinkLibrary190     LinkLibrary(const std::string &Library, bool IsFramework)
191       : Library(Library), IsFramework(IsFramework) { }
192 
193     /// \brief The library to link against.
194     ///
195     /// This will typically be a library or framework name, but can also
196     /// be an absolute path to the library or framework.
197     std::string Library;
198 
199     /// \brief Whether this is a framework rather than a library.
200     bool IsFramework;
201   };
202 
203   /// \brief The set of libraries or frameworks to link against when
204   /// an entity from this module is used.
205   llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
206 
207   /// \brief The set of "configuration macros", which are macros that
208   /// (intentionally) change how this module is built.
209   std::vector<std::string> ConfigMacros;
210 
211   /// \brief An unresolved conflict with another module.
212   struct UnresolvedConflict {
213     /// \brief The (unresolved) module id.
214     ModuleId Id;
215 
216     /// \brief The message provided to the user when there is a conflict.
217     std::string Message;
218   };
219 
220   /// \brief The list of conflicts for which the module-id has not yet been
221   /// resolved.
222   std::vector<UnresolvedConflict> UnresolvedConflicts;
223 
224   /// \brief A conflict between two modules.
225   struct Conflict {
226     /// \brief The module that this module conflicts with.
227     Module *Other;
228 
229     /// \brief The message provided to the user when there is a conflict.
230     std::string Message;
231   };
232 
233   /// \brief The list of conflicts.
234   std::vector<Conflict> Conflicts;
235 
236   /// \brief Construct a top-level module.
Module(StringRef Name,SourceLocation DefinitionLoc,bool IsFramework)237   explicit Module(StringRef Name, SourceLocation DefinitionLoc,
238                   bool IsFramework)
239     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0),Umbrella(),ASTFile(0),
240       IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
241       IsExplicit(false), IsSystem(false),
242       InferSubmodules(false), InferExplicitSubmodules(false),
243       InferExportWildcard(false), ConfigMacrosExhaustive(false),
244       NameVisibility(Hidden) { }
245 
246   /// \brief Construct a new module or submodule.
247   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
248          bool IsFramework, bool IsExplicit);
249 
250   ~Module();
251 
252   /// \brief Determine whether this module is available for use within the
253   /// current translation unit.
isAvailable()254   bool isAvailable() const { return IsAvailable; }
255 
256   /// \brief Determine whether this module is available for use within the
257   /// current translation unit.
258   ///
259   /// \param LangOpts The language options used for the current
260   /// translation unit.
261   ///
262   /// \param Target The target options used for the current translation unit.
263   ///
264   /// \param Feature If this module is unavailable, this parameter
265   /// will be set to one of the features that is required for use of
266   /// this module (but is not available).
267   bool isAvailable(const LangOptions &LangOpts,
268                    const TargetInfo &Target,
269                    StringRef &Feature) const;
270 
271   /// \brief Determine whether this module is a submodule.
isSubModule()272   bool isSubModule() const { return Parent != 0; }
273 
274   /// \brief Determine whether this module is a submodule of the given other
275   /// module.
276   bool isSubModuleOf(Module *Other) const;
277 
278   /// \brief Determine whether this module is a part of a framework,
279   /// either because it is a framework module or because it is a submodule
280   /// of a framework module.
isPartOfFramework()281   bool isPartOfFramework() const {
282     for (const Module *Mod = this; Mod; Mod = Mod->Parent)
283       if (Mod->IsFramework)
284         return true;
285 
286     return false;
287   }
288 
289   /// \brief Determine whether this module is a subframework of another
290   /// framework.
isSubFramework()291   bool isSubFramework() const {
292     return IsFramework && Parent && Parent->isPartOfFramework();
293   }
294 
295   /// \brief Retrieve the full name of this module, including the path from
296   /// its top-level module.
297   std::string getFullModuleName() const;
298 
299   /// \brief Retrieve the top-level module for this (sub)module, which may
300   /// be this module.
getTopLevelModule()301   Module *getTopLevelModule() {
302     return const_cast<Module *>(
303              const_cast<const Module *>(this)->getTopLevelModule());
304   }
305 
306   /// \brief Retrieve the top-level module for this (sub)module, which may
307   /// be this module.
308   const Module *getTopLevelModule() const;
309 
310   /// \brief Retrieve the name of the top-level module.
311   ///
getTopLevelModuleName()312   StringRef getTopLevelModuleName() const {
313     return getTopLevelModule()->Name;
314   }
315 
316   /// \brief The serialized AST file for this module, if one was created.
getASTFile()317   const FileEntry *getASTFile() const {
318     return getTopLevelModule()->ASTFile;
319   }
320 
321   /// \brief Set the serialized AST file for the top-level module of this module.
setASTFile(const FileEntry * File)322   void setASTFile(const FileEntry *File) {
323     assert((getASTFile() == 0 || getASTFile() == File) && "file path changed");
324     getTopLevelModule()->ASTFile = File;
325   }
326 
327   /// \brief Retrieve the directory for which this module serves as the
328   /// umbrella.
329   const DirectoryEntry *getUmbrellaDir() const;
330 
331   /// \brief Retrieve the header that serves as the umbrella header for this
332   /// module.
getUmbrellaHeader()333   const FileEntry *getUmbrellaHeader() const {
334     return Umbrella.dyn_cast<const FileEntry *>();
335   }
336 
337   /// \brief Determine whether this module has an umbrella directory that is
338   /// not based on an umbrella header.
hasUmbrellaDir()339   bool hasUmbrellaDir() const {
340     return Umbrella && Umbrella.is<const DirectoryEntry *>();
341   }
342 
343   /// \brief Add a top-level header associated with this module.
addTopHeader(const FileEntry * File)344   void addTopHeader(const FileEntry *File) {
345     assert(File);
346     TopHeaders.insert(File);
347   }
348 
349   /// \brief Add a top-level header filename associated with this module.
addTopHeaderFilename(StringRef Filename)350   void addTopHeaderFilename(StringRef Filename) {
351     TopHeaderNames.push_back(Filename);
352   }
353 
354   /// \brief The top-level headers associated with this module.
355   ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
356 
357   /// \brief Add the given feature requirement to the list of features
358   /// required by this module.
359   ///
360   /// \param Feature The feature that is required by this module (and
361   /// its submodules).
362   ///
363   /// \param LangOpts The set of language options that will be used to
364   /// evaluate the availability of this feature.
365   ///
366   /// \param Target The target options that will be used to evaluate the
367   /// availability of this feature.
368   void addRequirement(StringRef Feature, const LangOptions &LangOpts,
369                       const TargetInfo &Target);
370 
371   /// \brief Find the submodule with the given name.
372   ///
373   /// \returns The submodule if found, or NULL otherwise.
374   Module *findSubmodule(StringRef Name) const;
375 
376   /// \brief Determine whether the specified module would be visible to
377   /// a lookup at the end of this module.
isModuleVisible(const Module * M)378   bool isModuleVisible(const Module *M) const {
379     if (VisibleModulesCache.empty())
380       buildVisibleModulesCache();
381     return VisibleModulesCache.count(M);
382   }
383 
384   typedef std::vector<Module *>::iterator submodule_iterator;
385   typedef std::vector<Module *>::const_iterator submodule_const_iterator;
386 
submodule_begin()387   submodule_iterator submodule_begin() { return SubModules.begin(); }
submodule_begin()388   submodule_const_iterator submodule_begin() const {return SubModules.begin();}
submodule_end()389   submodule_iterator submodule_end()   { return SubModules.end(); }
submodule_end()390   submodule_const_iterator submodule_end() const { return SubModules.end(); }
391 
392   /// \brief Returns the exported modules based on the wildcard restrictions.
393   void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
394 
getModuleInputBufferName()395   static StringRef getModuleInputBufferName() {
396     return "<module-includes>";
397   }
398 
399   /// \brief Print the module map for this module to the given stream.
400   ///
401   void print(raw_ostream &OS, unsigned Indent = 0) const;
402 
403   /// \brief Dump the contents of this module to the given output stream.
404   void dump() const;
405 
406 private:
407   void buildVisibleModulesCache() const;
408 };
409 
410 } // end namespace clang
411 
412 
413 #endif // LLVM_CLANG_BASIC_MODULE_H
414