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/SetVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
41 /// \brief Describes the name of a module.
42 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
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;
72 /// \brief The top-level headers associated with this module.
73 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
75 /// \brief top-level header filenames that aren't resolved to FileEntries yet.
76 std::vector<std::string> TopHeaderNames;
79 /// \brief The headers that are part of this module.
80 SmallVector<const FileEntry *, 2> Headers;
82 /// \brief The headers that are explicitly excluded from this module.
83 SmallVector<const FileEntry *, 2> ExcludedHeaders;
85 /// \brief The set of language features required to use this module.
87 /// If any of these features is not present, the \c IsAvailable bit
88 /// will be false to indicate that this (sub)module is not
90 SmallVector<std::string, 2> Requires;
92 /// \brief Whether this module is available in the current
94 unsigned IsAvailable : 1;
96 /// \brief Whether this module was loaded from a module file.
97 unsigned IsFromModuleFile : 1;
99 /// \brief Whether this is a framework module.
100 unsigned IsFramework : 1;
102 /// \brief Whether this is an explicit submodule.
103 unsigned IsExplicit : 1;
105 /// \brief Whether this is a "system" module (which assumes that all
106 /// headers in it are system headers).
107 unsigned IsSystem : 1;
109 /// \brief Whether we should infer submodules for this module based on
112 /// Submodules can only be inferred for modules with an umbrella header.
113 unsigned InferSubmodules : 1;
115 /// \brief Whether, when inferring submodules, the inferred submodules
116 /// should be explicit.
117 unsigned InferExplicitSubmodules : 1;
119 /// \brief Whether, when inferring submodules, the inferr submodules should
120 /// export all modules they import (e.g., the equivalent of "export *").
121 unsigned InferExportWildcard : 1;
123 /// \brief Whether the set of configuration macros is exhaustive.
125 /// When the set of configuration macros is exhaustive, meaning
126 /// that no identifier not in this list should affect how the module is
128 unsigned ConfigMacrosExhaustive : 1;
130 /// \brief Describes the visibility of the various names within a
131 /// particular module.
132 enum NameVisibilityKind {
133 /// \brief All of the names in this module are hidden.
136 /// \brief Only the macro names in this module are visible.
138 /// \brief All of the names in this module are visible.
142 ///\ brief The visibility of names within this particular module.
143 NameVisibilityKind NameVisibility;
145 /// \brief The location of the inferred submodule.
146 SourceLocation InferredSubmoduleLoc;
148 /// \brief The set of modules imported by this module, and on which this
150 SmallVector<Module *, 2> Imports;
152 /// \brief Describes an exported module.
154 /// The pointer is the module being re-exported, while the bit will be true
155 /// to indicate that this is a wildcard export.
156 typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
158 /// \brief The set of export declarations.
159 SmallVector<ExportDecl, 2> Exports;
161 /// \brief Describes an exported module that has not yet been resolved
162 /// (perhaps because the module it refers to has not yet been loaded).
163 struct UnresolvedExportDecl {
164 /// \brief The location of the 'export' keyword in the module map file.
165 SourceLocation ExportLoc;
167 /// \brief The name of the module.
170 /// \brief Whether this export declaration ends in a wildcard, indicating
171 /// that all of its submodules should be exported (rather than the named
176 /// \brief The set of export declarations that have yet to be resolved.
177 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
179 /// \brief A library or framework to link against when an entity from this
182 LinkLibrary() : IsFramework(false) { }
183 LinkLibrary(const std::string &Library, bool IsFramework)
184 : Library(Library), IsFramework(IsFramework) { }
186 /// \brief The library to link against.
188 /// This will typically be a library or framework name, but can also
189 /// be an absolute path to the library or framework.
192 /// \brief Whether this is a framework rather than a library.
196 /// \brief The set of libraries or frameworks to link against when
197 /// an entity from this module is used.
198 llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
200 /// \brief The set of "configuration macros", which are macros that
201 /// (intentionally) change how this module is built.
202 std::vector<std::string> ConfigMacros;
204 /// \brief An unresolved conflict with another module.
205 struct UnresolvedConflict {
206 /// \brief The (unresolved) module id.
209 /// \brief The message provided to the user when there is a conflict.
213 /// \brief The list of conflicts for which the module-id has not yet been
215 std::vector<UnresolvedConflict> UnresolvedConflicts;
217 /// \brief A conflict between two modules.
219 /// \brief The module that this module conflicts with.
222 /// \brief The message provided to the user when there is a conflict.
226 /// \brief The list of conflicts.
227 std::vector<Conflict> Conflicts;
229 /// \brief Construct a top-level module.
230 explicit Module(StringRef Name, SourceLocation DefinitionLoc,
232 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0),Umbrella(),ASTFile(0),
233 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
234 IsExplicit(false), IsSystem(false),
235 InferSubmodules(false), InferExplicitSubmodules(false),
236 InferExportWildcard(false), ConfigMacrosExhaustive(false),
237 NameVisibility(Hidden) { }
239 /// \brief Construct a new module or submodule.
240 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
241 bool IsFramework, bool IsExplicit);
245 /// \brief Determine whether this module is available for use within the
246 /// current translation unit.
247 bool isAvailable() const { return IsAvailable; }
249 /// \brief Determine whether this module is available for use within the
250 /// current translation unit.
252 /// \param LangOpts The language options used for the current
253 /// translation unit.
255 /// \param Target The target options used for the current translation unit.
257 /// \param Feature If this module is unavailable, this parameter
258 /// will be set to one of the features that is required for use of
259 /// this module (but is not available).
260 bool isAvailable(const LangOptions &LangOpts,
261 const TargetInfo &Target,
262 StringRef &Feature) const;
264 /// \brief Determine whether this module is a submodule.
265 bool isSubModule() const { return Parent != 0; }
267 /// \brief Determine whether this module is a submodule of the given other
269 bool isSubModuleOf(Module *Other) const;
271 /// \brief Determine whether this module is a part of a framework,
272 /// either because it is a framework module or because it is a submodule
273 /// of a framework module.
274 bool isPartOfFramework() const {
275 for (const Module *Mod = this; Mod; Mod = Mod->Parent)
276 if (Mod->IsFramework)
282 /// \brief Determine whether this module is a subframework of another
284 bool isSubFramework() const {
285 return IsFramework && Parent && Parent->isPartOfFramework();
288 /// \brief Retrieve the full name of this module, including the path from
289 /// its top-level module.
290 std::string getFullModuleName() const;
292 /// \brief Retrieve the top-level module for this (sub)module, which may
294 Module *getTopLevelModule() {
295 return const_cast<Module *>(
296 const_cast<const Module *>(this)->getTopLevelModule());
299 /// \brief Retrieve the top-level module for this (sub)module, which may
301 const Module *getTopLevelModule() const;
303 /// \brief Retrieve the name of the top-level module.
305 StringRef getTopLevelModuleName() const {
306 return getTopLevelModule()->Name;
309 /// \brief The serialized AST file for this module, if one was created.
310 const FileEntry *getASTFile() const {
311 return getTopLevelModule()->ASTFile;
314 /// \brief Set the serialized AST file for the top-level module of this module.
315 void setASTFile(const FileEntry *File) {
316 assert((getASTFile() == 0 || getASTFile() == File) && "file path changed");
317 getTopLevelModule()->ASTFile = File;
320 /// \brief Retrieve the directory for which this module serves as the
322 const DirectoryEntry *getUmbrellaDir() const;
324 /// \brief Retrieve the header that serves as the umbrella header for this
326 const FileEntry *getUmbrellaHeader() const {
327 return Umbrella.dyn_cast<const FileEntry *>();
330 /// \brief Determine whether this module has an umbrella directory that is
331 /// not based on an umbrella header.
332 bool hasUmbrellaDir() const {
333 return Umbrella && Umbrella.is<const DirectoryEntry *>();
336 /// \brief Add a top-level header associated with this module.
337 void addTopHeader(const FileEntry *File) {
339 TopHeaders.insert(File);
342 /// \brief Add a top-level header filename associated with this module.
343 void addTopHeaderFilename(StringRef Filename) {
344 TopHeaderNames.push_back(Filename);
347 /// \brief The top-level headers associated with this module.
348 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
350 /// \brief Add the given feature requirement to the list of features
351 /// required by this module.
353 /// \param Feature The feature that is required by this module (and
356 /// \param LangOpts The set of language options that will be used to
357 /// evaluate the availability of this feature.
359 /// \param Target The target options that will be used to evaluate the
360 /// availability of this feature.
361 void addRequirement(StringRef Feature, const LangOptions &LangOpts,
362 const TargetInfo &Target);
364 /// \brief Find the submodule with the given name.
366 /// \returns The submodule if found, or NULL otherwise.
367 Module *findSubmodule(StringRef Name) const;
369 typedef std::vector<Module *>::iterator submodule_iterator;
370 typedef std::vector<Module *>::const_iterator submodule_const_iterator;
372 submodule_iterator submodule_begin() { return SubModules.begin(); }
373 submodule_const_iterator submodule_begin() const {return SubModules.begin();}
374 submodule_iterator submodule_end() { return SubModules.end(); }
375 submodule_const_iterator submodule_end() const { return SubModules.end(); }
377 /// \brief Returns the exported modules based on the wildcard restrictions.
378 void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
380 static StringRef getModuleInputBufferName() {
381 return "<module-includes>";
384 /// \brief Print the module map for this module to the given stream.
386 void print(raw_ostream &OS, unsigned Indent = 0) const;
388 /// \brief Dump the contents of this module to the given output stream.
392 } // end namespace clang
395 #endif // LLVM_CLANG_BASIC_MODULE_H