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 //===----------------------------------------------------------------------===//
10 // This file defines the Module class, which describes a module in the source
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_MODULE_H
15 #define LLVM_CLANG_BASIC_MODULE_H
17 #include "clang/Basic/SourceLocation.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/PointerUnion.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
38 /// \brief Describes the name of a module.
39 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
42 /// \brief Describes a module or submodule.
45 /// \brief The name of this module.
48 /// \brief The location of the module definition.
49 SourceLocation DefinitionLoc;
51 /// \brief The parent of this module. This will be NULL for the top-level
55 /// \brief The umbrella header or directory.
56 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
59 /// \brief The submodules of this module, indexed by name.
60 std::vector<Module *> SubModules;
62 /// \brief A mapping from the submodule name to the index into the
63 /// \c SubModules vector at which that submodule resides.
64 llvm::StringMap<unsigned> SubModuleIndex;
67 /// \brief The headers that are part of this module.
68 llvm::SmallVector<const FileEntry *, 2> Headers;
70 /// \brief The set of language features required to use this module.
72 /// If any of these features is not present, the \c IsAvailable bit
73 /// will be false to indicate that this (sub)module is not
75 llvm::SmallVector<std::string, 2> Requires;
77 /// \brief Whether this module is available in the current
79 unsigned IsAvailable : 1;
81 /// \brief Whether this module was loaded from a module file.
82 unsigned IsFromModuleFile : 1;
84 /// \brief Whether this is a framework module.
85 unsigned IsFramework : 1;
87 /// \brief Whether this is an explicit submodule.
88 unsigned IsExplicit : 1;
90 /// \brief Whether this is a "system" module (which assumes that all
91 /// headers in it are system headers).
92 unsigned IsSystem : 1;
94 /// \brief Whether we should infer submodules for this module based on
97 /// Submodules can only be inferred for modules with an umbrella header.
98 unsigned InferSubmodules : 1;
100 /// \brief Whether, when inferring submodules, the inferred submodules
101 /// should be explicit.
102 unsigned InferExplicitSubmodules : 1;
104 /// \brief Whether, when inferring submodules, the inferr submodules should
105 /// export all modules they import (e.g., the equivalent of "export *").
106 unsigned InferExportWildcard : 1;
108 /// \brief Describes the visibility of the various names within a
109 /// particular module.
110 enum NameVisibilityKind {
111 /// \brief All of the names in this module are hidden.
114 /// \brief Only the macro names in this module are visible.
116 /// \brief All of the names in this module are visible.
120 ///\ brief The visibility of names within this particular module.
121 NameVisibilityKind NameVisibility;
123 /// \brief The location of the inferred submodule.
124 SourceLocation InferredSubmoduleLoc;
126 /// \brief The set of modules imported by this module, and on which this
128 llvm::SmallVector<Module *, 2> Imports;
130 /// \brief Describes an exported module.
132 /// The pointer is the module being re-exported, while the bit will be true
133 /// to indicate that this is a wildcard export.
134 typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
136 /// \brief The set of export declarations.
137 llvm::SmallVector<ExportDecl, 2> Exports;
139 /// \brief Describes an exported module that has not yet been resolved
140 /// (perhaps because tASThe module it refers to has not yet been loaded).
141 struct UnresolvedExportDecl {
142 /// \brief The location of the 'export' keyword in the module map file.
143 SourceLocation ExportLoc;
145 /// \brief The name of the module.
148 /// \brief Whether this export declaration ends in a wildcard, indicating
149 /// that all of its submodules should be exported (rather than the named
154 /// \brief The set of export declarations that have yet to be resolved.
155 llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
157 /// \brief Construct a top-level module.
158 explicit Module(StringRef Name, SourceLocation DefinitionLoc,
160 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), Umbrella(),
161 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
162 IsExplicit(false), IsSystem(false),
163 InferSubmodules(false), InferExplicitSubmodules(false),
164 InferExportWildcard(false), NameVisibility(Hidden) { }
166 /// \brief Construct a new module or submodule.
167 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
168 bool IsFramework, bool IsExplicit);
172 /// \brief Determine whether this module is available for use within the
173 /// current translation unit.
174 bool isAvailable() const { return IsAvailable; }
176 /// \brief Determine whether this module is available for use within the
177 /// current translation unit.
179 /// \param LangOpts The language options used for the current
180 /// translation unit.
182 /// \param Target The target options used for the current translation unit.
184 /// \param Feature If this module is unavailable, this parameter
185 /// will be set to one of the features that is required for use of
186 /// this module (but is not available).
187 bool isAvailable(const LangOptions &LangOpts,
188 const TargetInfo &Target,
189 StringRef &Feature) const;
191 /// \brief Determine whether this module is a submodule.
192 bool isSubModule() const { return Parent != 0; }
194 /// \brief Determine whether this module is a submodule of the given other
196 bool isSubModuleOf(Module *Other) const;
198 /// \brief Determine whether this module is a part of a framework,
199 /// either because it is a framework module or because it is a submodule
200 /// of a framework module.
201 bool isPartOfFramework() const {
202 for (const Module *Mod = this; Mod; Mod = Mod->Parent)
203 if (Mod->IsFramework)
209 /// \brief Retrieve the full name of this module, including the path from
210 /// its top-level module.
211 std::string getFullModuleName() const;
213 /// \brief Retrieve the top-level module for this (sub)module, which may
215 Module *getTopLevelModule() {
216 return const_cast<Module *>(
217 const_cast<const Module *>(this)->getTopLevelModule());
220 /// \brief Retrieve the top-level module for this (sub)module, which may
222 const Module *getTopLevelModule() const;
224 /// \brief Retrieve the name of the top-level module.
226 StringRef getTopLevelModuleName() const {
227 return getTopLevelModule()->Name;
230 /// \brief Retrieve the directory for which this module serves as the
232 const DirectoryEntry *getUmbrellaDir() const;
234 /// \brief Retrieve the header that serves as the umbrella header for this
236 const FileEntry *getUmbrellaHeader() const {
237 return Umbrella.dyn_cast<const FileEntry *>();
240 /// \brief Determine whether this module has an umbrella directory that is
241 /// not based on an umbrella header.
242 bool hasUmbrellaDir() const {
243 return Umbrella && Umbrella.is<const DirectoryEntry *>();
246 /// \briaf Add the given feature requirement to the list of features
247 /// required by this module.
249 /// \param Feature The feature that is required by this module (and
252 /// \param LangOpts The set of language options that will be used to
253 /// evaluate the availability of this feature.
255 /// \param Target The target options that will be used to evaluate the
256 /// availability of this feature.
257 void addRequirement(StringRef Feature, const LangOptions &LangOpts,
258 const TargetInfo &Target);
260 /// \brief Find the submodule with the given name.
262 /// \returns The submodule if found, or NULL otherwise.
263 Module *findSubmodule(StringRef Name) const;
265 typedef std::vector<Module *>::iterator submodule_iterator;
266 typedef std::vector<Module *>::const_iterator submodule_const_iterator;
268 submodule_iterator submodule_begin() { return SubModules.begin(); }
269 submodule_const_iterator submodule_begin() const {return SubModules.begin();}
270 submodule_iterator submodule_end() { return SubModules.end(); }
271 submodule_const_iterator submodule_end() const { return SubModules.end(); }
273 /// \brief Print the module map for this module to the given stream.
275 void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
277 /// \brief Dump the contents of this module to the given output stream.
281 } // end namespace clang
284 #endif // LLVM_CLANG_BASIC_MODULE_H