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/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"
43 /// \brief Describes the name of a module.
44 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
46 /// \brief Describes a module or submodule.
49 /// \brief The name of this module.
52 /// \brief The location of the module definition.
53 SourceLocation DefinitionLoc;
55 /// \brief The parent of this module. This will be NULL for the top-level
59 /// \brief The umbrella header or directory.
60 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
63 /// \brief The submodules of this module, indexed by name.
64 std::vector<Module *> SubModules;
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;
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;
74 /// \brief The top-level headers associated with this module.
75 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
77 /// \brief top-level header filenames that aren't resolved to FileEntries yet.
78 std::vector<std::string> TopHeaderNames;
80 /// \brief Cache of modules visible to lookup in this module.
81 mutable llvm::DenseSet<const Module*> VisibleModulesCache;
84 /// \brief The headers that are part of this module.
85 SmallVector<const FileEntry *, 2> NormalHeaders;
87 /// \brief The headers that are explicitly excluded from this module.
88 SmallVector<const FileEntry *, 2> ExcludedHeaders;
90 /// \brief The headers that are private to this module.
91 llvm::SmallVector<const FileEntry *, 2> PrivateHeaders;
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;
97 /// \brief The set of language features required to use this module.
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;
103 /// \brief Whether this module is available in the current
104 /// translation unit.
105 unsigned IsAvailable : 1;
107 /// \brief Whether this module was loaded from a module file.
108 unsigned IsFromModuleFile : 1;
110 /// \brief Whether this is a framework module.
111 unsigned IsFramework : 1;
113 /// \brief Whether this is an explicit submodule.
114 unsigned IsExplicit : 1;
116 /// \brief Whether this is a "system" module (which assumes that all
117 /// headers in it are system headers).
118 unsigned IsSystem : 1;
120 /// \brief Whether we should infer submodules for this module based on
123 /// Submodules can only be inferred for modules with an umbrella header.
124 unsigned InferSubmodules : 1;
126 /// \brief Whether, when inferring submodules, the inferred submodules
127 /// should be explicit.
128 unsigned InferExplicitSubmodules : 1;
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;
134 /// \brief Whether the set of configuration macros is exhaustive.
136 /// When the set of configuration macros is exhaustive, meaning
137 /// that no identifier not in this list should affect how the module is
139 unsigned ConfigMacrosExhaustive : 1;
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.
147 /// \brief Only the macro names in this module are visible.
149 /// \brief All of the names in this module are visible.
153 ///\ brief The visibility of names within this particular module.
154 NameVisibilityKind NameVisibility;
156 /// \brief The location of the inferred submodule.
157 SourceLocation InferredSubmoduleLoc;
159 /// \brief The set of modules imported by this module, and on which this
161 SmallVector<Module *, 2> Imports;
163 /// \brief Describes an exported module.
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;
169 /// \brief The set of export declarations.
170 SmallVector<ExportDecl, 2> Exports;
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;
178 /// \brief The name of the module.
181 /// \brief Whether this export declaration ends in a wildcard, indicating
182 /// that all of its submodules should be exported (rather than the named
187 /// \brief The set of export declarations that have yet to be resolved.
188 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
190 /// \brief The directly used modules.
191 SmallVector<Module *, 2> DirectUses;
193 /// \brief The set of use declarations that have yet to be resolved.
194 SmallVector<ModuleId, 2> UnresolvedDirectUses;
196 /// \brief A library or framework to link against when an entity from this
199 LinkLibrary() : IsFramework(false) { }
200 LinkLibrary(const std::string &Library, bool IsFramework)
201 : Library(Library), IsFramework(IsFramework) { }
203 /// \brief The library to link against.
205 /// This will typically be a library or framework name, but can also
206 /// be an absolute path to the library or framework.
209 /// \brief Whether this is a framework rather than a library.
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;
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;
221 /// \brief An unresolved conflict with another module.
222 struct UnresolvedConflict {
223 /// \brief The (unresolved) module id.
226 /// \brief The message provided to the user when there is a conflict.
230 /// \brief The list of conflicts for which the module-id has not yet been
232 std::vector<UnresolvedConflict> UnresolvedConflicts;
234 /// \brief A conflict between two modules.
236 /// \brief The module that this module conflicts with.
239 /// \brief The message provided to the user when there is a conflict.
243 /// \brief The list of conflicts.
244 std::vector<Conflict> Conflicts;
246 /// \brief Construct a top-level module.
247 explicit Module(StringRef Name, SourceLocation DefinitionLoc,
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) { }
256 /// \brief Construct a new module or submodule.
257 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
258 bool IsFramework, bool IsExplicit);
262 /// \brief Determine whether this module is available for use within the
263 /// current translation unit.
264 bool isAvailable() const { return IsAvailable; }
266 /// \brief Determine whether this module is available for use within the
267 /// current translation unit.
269 /// \param LangOpts The language options used for the current
270 /// translation unit.
272 /// \param Target The target options used for the current translation unit.
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
277 bool isAvailable(const LangOptions &LangOpts,
278 const TargetInfo &Target,
279 Requirement &Req) const;
281 /// \brief Determine whether this module is a submodule.
282 bool isSubModule() const { return Parent != 0; }
284 /// \brief Determine whether this module is a submodule of the given other
286 bool isSubModuleOf(Module *Other) const;
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)
299 /// \brief Determine whether this module is a subframework of another
301 bool isSubFramework() const {
302 return IsFramework && Parent && Parent->isPartOfFramework();
305 /// \brief Retrieve the full name of this module, including the path from
306 /// its top-level module.
307 std::string getFullModuleName() const;
309 /// \brief Retrieve the top-level module for this (sub)module, which may
311 Module *getTopLevelModule() {
312 return const_cast<Module *>(
313 const_cast<const Module *>(this)->getTopLevelModule());
316 /// \brief Retrieve the top-level module for this (sub)module, which may
318 const Module *getTopLevelModule() const;
320 /// \brief Retrieve the name of the top-level module.
322 StringRef getTopLevelModuleName() const {
323 return getTopLevelModule()->Name;
326 /// \brief The serialized AST file for this module, if one was created.
327 const FileEntry *getASTFile() const {
328 return getTopLevelModule()->ASTFile;
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;
337 /// \brief Retrieve the directory for which this module serves as the
339 const DirectoryEntry *getUmbrellaDir() const;
341 /// \brief Retrieve the header that serves as the umbrella header for this
343 const FileEntry *getUmbrellaHeader() const {
344 return Umbrella.dyn_cast<const FileEntry *>();
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 *>();
353 /// \brief Add a top-level header associated with this module.
354 void addTopHeader(const FileEntry *File) {
356 TopHeaders.insert(File);
359 /// \brief Add a top-level header filename associated with this module.
360 void addTopHeaderFilename(StringRef Filename) {
361 TopHeaderNames.push_back(Filename);
364 /// \brief The top-level headers associated with this module.
365 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
367 /// \brief Add the given feature requirement to the list of features
368 /// required by this module.
370 /// \param Feature The feature that is required by this module (and
373 /// \param RequiredState The required state of this feature: \c true
374 /// if it must be present, \c false if it must be absent.
376 /// \param LangOpts The set of language options that will be used to
377 /// evaluate the availability of this feature.
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);
385 /// \brief Find the submodule with the given name.
387 /// \returns The submodule if found, or NULL otherwise.
388 Module *findSubmodule(StringRef Name) const;
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);
398 typedef std::vector<Module *>::iterator submodule_iterator;
399 typedef std::vector<Module *>::const_iterator submodule_const_iterator;
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(); }
406 /// \brief Appends this module's list of exported modules to \p Exported.
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;
412 static StringRef getModuleInputBufferName() {
413 return "<module-includes>";
416 /// \brief Print the module map for this module to the given stream.
418 void print(raw_ostream &OS, unsigned Indent = 0) const;
420 /// \brief Dump the contents of this module to the given output stream.
424 void buildVisibleModulesCache() const;
427 } // end namespace clang
430 #endif // LLVM_CLANG_BASIC_MODULE_H