]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/Basic/Module.h
MFC r244628:
[FreeBSD/stable/9.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/PointerIntPair.h"
20 #include "llvm/ADT/PointerUnion.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/SetVector.h"
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 namespace llvm {
30   class raw_ostream;
31 }
32
33 namespace clang {
34   
35 class DirectoryEntry;
36 class FileEntry;
37 class LangOptions;
38 class TargetInfo;
39   
40 /// \brief Describes the name of a module.
41 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
42   ModuleId;
43   
44 /// \brief Describes a module or submodule.
45 class Module {
46 public:
47   /// \brief The name of this module.
48   std::string Name;
49   
50   /// \brief The location of the module definition.
51   SourceLocation DefinitionLoc;
52   
53   /// \brief The parent of this module. This will be NULL for the top-level
54   /// module.
55   Module *Parent;
56   
57   /// \brief The umbrella header or directory.
58   llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
59   
60 private:
61   /// \brief The submodules of this module, indexed by name.
62   std::vector<Module *> SubModules;
63   
64   /// \brief A mapping from the submodule name to the index into the 
65   /// \c SubModules vector at which that submodule resides.
66   llvm::StringMap<unsigned> SubModuleIndex;
67
68   /// \brief The AST file if this is a top-level module which has a
69   /// corresponding serialized AST file, or null otherwise.
70   const FileEntry *ASTFile;
71   
72 public:
73   /// \brief The headers that are part of this module.
74   llvm::SmallVector<const FileEntry *, 2> Headers;
75
76   /// \brief The headers that are explicitly excluded from this module.
77   llvm::SmallVector<const FileEntry *, 2> ExcludedHeaders;
78
79   /// \brief The top-level headers associated with this module.
80   llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
81
82   /// \brief The set of language features required to use this module.
83   ///
84   /// If any of these features is not present, the \c IsAvailable bit
85   /// will be false to indicate that this (sub)module is not
86   /// available.
87   llvm::SmallVector<std::string, 2> Requires;
88
89   /// \brief Whether this module is available in the current
90   /// translation unit.
91   unsigned IsAvailable : 1;
92
93   /// \brief Whether this module was loaded from a module file.
94   unsigned IsFromModuleFile : 1;
95   
96   /// \brief Whether this is a framework module.
97   unsigned IsFramework : 1;
98   
99   /// \brief Whether this is an explicit submodule.
100   unsigned IsExplicit : 1;
101   
102   /// \brief Whether this is a "system" module (which assumes that all
103   /// headers in it are system headers).
104   unsigned IsSystem : 1;
105   
106   /// \brief Whether we should infer submodules for this module based on 
107   /// the headers.
108   ///
109   /// Submodules can only be inferred for modules with an umbrella header.
110   unsigned InferSubmodules : 1;
111   
112   /// \brief Whether, when inferring submodules, the inferred submodules
113   /// should be explicit.
114   unsigned InferExplicitSubmodules : 1;
115   
116   /// \brief Whether, when inferring submodules, the inferr submodules should
117   /// export all modules they import (e.g., the equivalent of "export *").
118   unsigned InferExportWildcard : 1;
119   
120   /// \brief Describes the visibility of the various names within a
121   /// particular module.
122   enum NameVisibilityKind {
123     /// \brief All of the names in this module are hidden.
124     ///
125     Hidden,
126     /// \brief Only the macro names in this module are visible.
127     MacrosVisible,
128     /// \brief All of the names in this module are visible.
129     AllVisible
130   };  
131   
132   ///\ brief The visibility of names within this particular module.
133   NameVisibilityKind NameVisibility;
134
135   /// \brief The location of the inferred submodule.
136   SourceLocation InferredSubmoduleLoc;
137
138   /// \brief The set of modules imported by this module, and on which this
139   /// module depends.
140   llvm::SmallVector<Module *, 2> Imports;
141   
142   /// \brief Describes an exported module.
143   ///
144   /// The pointer is the module being re-exported, while the bit will be true
145   /// to indicate that this is a wildcard export.
146   typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
147   
148   /// \brief The set of export declarations.
149   llvm::SmallVector<ExportDecl, 2> Exports;
150   
151   /// \brief Describes an exported module that has not yet been resolved
152   /// (perhaps because the module it refers to has not yet been loaded).
153   struct UnresolvedExportDecl {
154     /// \brief The location of the 'export' keyword in the module map file.
155     SourceLocation ExportLoc;
156     
157     /// \brief The name of the module.
158     ModuleId Id;
159     
160     /// \brief Whether this export declaration ends in a wildcard, indicating
161     /// that all of its submodules should be exported (rather than the named
162     /// module itself).
163     bool Wildcard;
164   };
165   
166   /// \brief The set of export declarations that have yet to be resolved.
167   llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
168   
169   /// \brief Construct a top-level module.
170   explicit Module(StringRef Name, SourceLocation DefinitionLoc,
171                   bool IsFramework)
172     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0),Umbrella(),ASTFile(0),
173       IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), 
174       IsExplicit(false), IsSystem(false),
175       InferSubmodules(false), InferExplicitSubmodules(false),
176       InferExportWildcard(false), NameVisibility(Hidden) { }
177   
178   /// \brief Construct a new module or submodule.
179   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 
180          bool IsFramework, bool IsExplicit);
181   
182   ~Module();
183   
184   /// \brief Determine whether this module is available for use within the
185   /// current translation unit.
186   bool isAvailable() const { return IsAvailable; }
187
188   /// \brief Determine whether this module is available for use within the
189   /// current translation unit.
190   ///
191   /// \param LangOpts The language options used for the current
192   /// translation unit.
193   ///
194   /// \param Target The target options used for the current translation unit.
195   ///
196   /// \param Feature If this module is unavailable, this parameter
197   /// will be set to one of the features that is required for use of
198   /// this module (but is not available).
199   bool isAvailable(const LangOptions &LangOpts, 
200                    const TargetInfo &Target,
201                    StringRef &Feature) const;
202
203   /// \brief Determine whether this module is a submodule.
204   bool isSubModule() const { return Parent != 0; }
205   
206   /// \brief Determine whether this module is a submodule of the given other
207   /// module.
208   bool isSubModuleOf(Module *Other) const;
209   
210   /// \brief Determine whether this module is a part of a framework,
211   /// either because it is a framework module or because it is a submodule
212   /// of a framework module.
213   bool isPartOfFramework() const {
214     for (const Module *Mod = this; Mod; Mod = Mod->Parent) 
215       if (Mod->IsFramework)
216         return true;
217     
218     return false;
219   }
220   
221   /// \brief Retrieve the full name of this module, including the path from
222   /// its top-level module.
223   std::string getFullModuleName() const;
224
225   /// \brief Retrieve the top-level module for this (sub)module, which may
226   /// be this module.
227   Module *getTopLevelModule() {
228     return const_cast<Module *>(
229              const_cast<const Module *>(this)->getTopLevelModule());
230   }
231
232   /// \brief Retrieve the top-level module for this (sub)module, which may
233   /// be this module.
234   const Module *getTopLevelModule() const;
235   
236   /// \brief Retrieve the name of the top-level module.
237   ///
238   StringRef getTopLevelModuleName() const {
239     return getTopLevelModule()->Name;
240   }
241
242   /// \brief The serialized AST file for this module, if one was created.
243   const FileEntry *getASTFile() const {
244     return getTopLevelModule()->ASTFile;
245   }
246
247   /// \brief Set the serialized AST file for the top-level module of this module.
248   void setASTFile(const FileEntry *File) {
249     assert((getASTFile() == 0 || getASTFile() == File) && "file path changed");
250     getTopLevelModule()->ASTFile = File;
251   }
252
253   /// \brief Retrieve the directory for which this module serves as the
254   /// umbrella.
255   const DirectoryEntry *getUmbrellaDir() const;
256
257   /// \brief Retrieve the header that serves as the umbrella header for this
258   /// module.
259   const FileEntry *getUmbrellaHeader() const {
260     return Umbrella.dyn_cast<const FileEntry *>();
261   }
262
263   /// \brief Determine whether this module has an umbrella directory that is
264   /// not based on an umbrella header.
265   bool hasUmbrellaDir() const {
266     return Umbrella && Umbrella.is<const DirectoryEntry *>();
267   }
268
269   /// \brief Add the given feature requirement to the list of features
270   /// required by this module.
271   ///
272   /// \param Feature The feature that is required by this module (and
273   /// its submodules).
274   ///
275   /// \param LangOpts The set of language options that will be used to
276   /// evaluate the availability of this feature.
277   ///
278   /// \param Target The target options that will be used to evaluate the
279   /// availability of this feature.
280   void addRequirement(StringRef Feature, const LangOptions &LangOpts,
281                       const TargetInfo &Target);
282
283   /// \brief Find the submodule with the given name.
284   ///
285   /// \returns The submodule if found, or NULL otherwise.
286   Module *findSubmodule(StringRef Name) const;
287   
288   typedef std::vector<Module *>::iterator submodule_iterator;
289   typedef std::vector<Module *>::const_iterator submodule_const_iterator;
290   
291   submodule_iterator submodule_begin() { return SubModules.begin(); }
292   submodule_const_iterator submodule_begin() const {return SubModules.begin();}
293   submodule_iterator submodule_end()   { return SubModules.end(); }
294   submodule_const_iterator submodule_end() const { return SubModules.end(); }
295   
296   static StringRef getModuleInputBufferName() {
297     return "<module-includes>";
298   }
299
300   /// \brief Print the module map for this module to the given stream. 
301   ///
302   void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
303   
304   /// \brief Dump the contents of this module to the given output stream.
305   void dump() const;
306 };
307
308 } // end namespace clang
309
310
311 #endif // LLVM_CLANG_BASIC_MODULE_H