1 //===--- Module.h - Describe a module ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the clang::Module class, which describes a module in the
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_MODULE_H
16 #define LLVM_CLANG_BASIC_MODULE_H
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"
40 /// \brief Describes the name of a module.
41 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
44 /// \brief Describes a module or submodule.
47 /// \brief The name of this module.
50 /// \brief The location of the module definition.
51 SourceLocation DefinitionLoc;
53 /// \brief The parent of this module. This will be NULL for the top-level
57 /// \brief The umbrella header or directory.
58 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
61 /// \brief The submodules of this module, indexed by name.
62 std::vector<Module *> SubModules;
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;
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;
73 /// \brief The headers that are part of this module.
74 llvm::SmallVector<const FileEntry *, 2> Headers;
76 /// \brief The headers that are explicitly excluded from this module.
77 llvm::SmallVector<const FileEntry *, 2> ExcludedHeaders;
79 /// \brief The top-level headers associated with this module.
80 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
82 /// \brief The set of language features required to use this module.
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
87 llvm::SmallVector<std::string, 2> Requires;
89 /// \brief Whether this module is available in the current
91 unsigned IsAvailable : 1;
93 /// \brief Whether this module was loaded from a module file.
94 unsigned IsFromModuleFile : 1;
96 /// \brief Whether this is a framework module.
97 unsigned IsFramework : 1;
99 /// \brief Whether this is an explicit submodule.
100 unsigned IsExplicit : 1;
102 /// \brief Whether this is a "system" module (which assumes that all
103 /// headers in it are system headers).
104 unsigned IsSystem : 1;
106 /// \brief Whether we should infer submodules for this module based on
109 /// Submodules can only be inferred for modules with an umbrella header.
110 unsigned InferSubmodules : 1;
112 /// \brief Whether, when inferring submodules, the inferred submodules
113 /// should be explicit.
114 unsigned InferExplicitSubmodules : 1;
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;
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.
126 /// \brief Only the macro names in this module are visible.
128 /// \brief All of the names in this module are visible.
132 ///\ brief The visibility of names within this particular module.
133 NameVisibilityKind NameVisibility;
135 /// \brief The location of the inferred submodule.
136 SourceLocation InferredSubmoduleLoc;
138 /// \brief The set of modules imported by this module, and on which this
140 llvm::SmallVector<Module *, 2> Imports;
142 /// \brief Describes an exported module.
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;
148 /// \brief The set of export declarations.
149 llvm::SmallVector<ExportDecl, 2> Exports;
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;
157 /// \brief The name of the module.
160 /// \brief Whether this export declaration ends in a wildcard, indicating
161 /// that all of its submodules should be exported (rather than the named
166 /// \brief The set of export declarations that have yet to be resolved.
167 llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
169 /// \brief Construct a top-level module.
170 explicit Module(StringRef Name, SourceLocation DefinitionLoc,
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) { }
178 /// \brief Construct a new module or submodule.
179 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
180 bool IsFramework, bool IsExplicit);
184 /// \brief Determine whether this module is available for use within the
185 /// current translation unit.
186 bool isAvailable() const { return IsAvailable; }
188 /// \brief Determine whether this module is available for use within the
189 /// current translation unit.
191 /// \param LangOpts The language options used for the current
192 /// translation unit.
194 /// \param Target The target options used for the current translation unit.
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;
203 /// \brief Determine whether this module is a submodule.
204 bool isSubModule() const { return Parent != 0; }
206 /// \brief Determine whether this module is a submodule of the given other
208 bool isSubModuleOf(Module *Other) const;
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)
221 /// \brief Retrieve the full name of this module, including the path from
222 /// its top-level module.
223 std::string getFullModuleName() const;
225 /// \brief Retrieve the top-level module for this (sub)module, which may
227 Module *getTopLevelModule() {
228 return const_cast<Module *>(
229 const_cast<const Module *>(this)->getTopLevelModule());
232 /// \brief Retrieve the top-level module for this (sub)module, which may
234 const Module *getTopLevelModule() const;
236 /// \brief Retrieve the name of the top-level module.
238 StringRef getTopLevelModuleName() const {
239 return getTopLevelModule()->Name;
242 /// \brief The serialized AST file for this module, if one was created.
243 const FileEntry *getASTFile() const {
244 return getTopLevelModule()->ASTFile;
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;
253 /// \brief Retrieve the directory for which this module serves as the
255 const DirectoryEntry *getUmbrellaDir() const;
257 /// \brief Retrieve the header that serves as the umbrella header for this
259 const FileEntry *getUmbrellaHeader() const {
260 return Umbrella.dyn_cast<const FileEntry *>();
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 *>();
269 /// \brief Add the given feature requirement to the list of features
270 /// required by this module.
272 /// \param Feature The feature that is required by this module (and
275 /// \param LangOpts The set of language options that will be used to
276 /// evaluate the availability of this feature.
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);
283 /// \brief Find the submodule with the given name.
285 /// \returns The submodule if found, or NULL otherwise.
286 Module *findSubmodule(StringRef Name) const;
288 typedef std::vector<Module *>::iterator submodule_iterator;
289 typedef std::vector<Module *>::const_iterator submodule_const_iterator;
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(); }
296 static StringRef getModuleInputBufferName() {
297 return "<module-includes>";
300 /// \brief Print the module map for this module to the given stream.
302 void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
304 /// \brief Dump the contents of this module to the given output stream.
308 } // end namespace clang
311 #endif // LLVM_CLANG_BASIC_MODULE_H