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