1 //===--- ModuleMap.h - Describe the layout of modules -----------*- 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 ModuleMap interface, which describes the layout of a
11 // module as it relates to headers.
13 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LEX_MODULEMAP_H
17 #define LLVM_CLANG_LEX_MODULEMAP_H
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Basic/Module.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/StringMap.h"
34 class DiagnosticConsumer;
35 class DiagnosticsEngine;
36 class ModuleMapParser;
39 SourceManager *SourceMgr;
40 IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
41 const LangOptions &LangOpts;
42 const TargetInfo *Target;
44 /// \brief The directory used for Clang-supplied, builtin include headers,
45 /// such as "stdint.h".
46 const DirectoryEntry *BuiltinIncludeDir;
48 /// \brief Language options used to parse the module map itself.
50 /// These are always simple C language options.
51 LangOptions MMapLangOpts;
53 /// \brief The top-level modules that are known.
54 llvm::StringMap<Module *> Modules;
56 /// \brief A header that is known to reside within a given module,
57 /// whether it was included or excluded.
59 llvm::PointerIntPair<Module *, 1, bool> Storage;
62 KnownHeader() : Storage(0, false) { }
63 KnownHeader(Module *M, bool Excluded) : Storage(M, Excluded) { }
65 /// \brief Retrieve the module the header is stored in.
66 Module *getModule() const { return Storage.getPointer(); }
68 /// \brief Whether this header is explicitly excluded from the module.
69 bool isExcluded() const { return Storage.getInt(); }
71 /// \brief Whether this header is available in the module.
72 bool isAvailable() const {
73 return !isExcluded() && getModule()->isAvailable();
76 // \brief Whether this known header is valid (i.e., it has an
77 // associated module).
78 operator bool() const { return Storage.getPointer() != 0; }
81 typedef llvm::DenseMap<const FileEntry *, KnownHeader> HeadersMap;
83 /// \brief Mapping from each header to the module that owns the contents of the
87 /// \brief Mapping from directories with umbrella headers to the module
88 /// that is generated from the umbrella header.
90 /// This mapping is used to map headers that haven't explicitly been named
91 /// in the module map over to the module that includes them via its umbrella
93 llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
95 /// \brief A directory for which framework modules can be inferred.
96 struct InferredDirectory {
97 InferredDirectory() : InferModules(), InferSystemModules() { }
99 /// \brief Whether to infer modules from this directory.
100 unsigned InferModules : 1;
102 /// \brief Whether the modules we infer are [system] modules.
103 unsigned InferSystemModules : 1;
105 /// \brief The names of modules that cannot be inferred within this
107 llvm::SmallVector<std::string, 2> ExcludedModules;
110 /// \brief A mapping from directories to information about inferring
111 /// framework modules from within those directories.
112 llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
114 friend class ModuleMapParser;
116 /// \brief Resolve the given export declaration into an actual export
119 /// \param Mod The module in which we're resolving the export declaration.
121 /// \param Unresolved The export declaration to resolve.
123 /// \param Complain Whether this routine should complain about unresolvable
126 /// \returns The resolved export declaration, which will have a NULL pointer
127 /// if the export could not be resolved.
129 resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
133 /// \brief Construct a new module map.
135 /// \param FileMgr The file manager used to find module files and headers.
136 /// This file manager should be shared with the header-search mechanism, since
137 /// they will refer to the same headers.
139 /// \param DC A diagnostic consumer that will be cloned for use in generating
142 /// \param LangOpts Language options for this translation unit.
144 /// \param Target The target for this translation unit.
145 ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
146 const LangOptions &LangOpts, const TargetInfo *Target);
148 /// \brief Destroy the module map.
152 /// \brief Set the target information.
153 void setTarget(const TargetInfo &Target);
155 /// \brief Set the directory that contains Clang-supplied include
156 /// files, such as our stdarg.h or tgmath.h.
157 void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
158 BuiltinIncludeDir = Dir;
161 /// \brief Retrieve the module that owns the given header file, if any.
163 /// \param File The header file that is likely to be included.
165 /// \returns The module that owns the given header file, or null to indicate
166 /// that no module owns this header file.
167 Module *findModuleForHeader(const FileEntry *File);
169 /// \brief Determine whether the given header is part of a module
170 /// marked 'unavailable'.
171 bool isHeaderInUnavailableModule(const FileEntry *Header);
173 /// \brief Retrieve a module with the given name.
175 /// \param Name The name of the module to look up.
177 /// \returns The named module, if known; otherwise, returns null.
178 Module *findModule(StringRef Name);
180 /// \brief Retrieve a module with the given name using lexical name lookup,
181 /// starting at the given context.
183 /// \param Name The name of the module to look up.
185 /// \param Context The module context, from which we will perform lexical
188 /// \returns The named module, if known; otherwise, returns null.
189 Module *lookupModuleUnqualified(StringRef Name, Module *Context);
191 /// \brief Retrieve a module with the given name within the given context,
192 /// using direct (qualified) name lookup.
194 /// \param Name The name of the module to look up.
196 /// \param Context The module for which we will look for a submodule. If
197 /// null, we will look for a top-level module.
199 /// \returns The named submodule, if known; otherwose, returns null.
200 Module *lookupModuleQualified(StringRef Name, Module *Context);
202 /// \brief Find a new module or submodule, or create it if it does not already
205 /// \param Name The name of the module to find or create.
207 /// \param Parent The module that will act as the parent of this submodule,
208 /// or NULL to indicate that this is a top-level module.
210 /// \param IsFramework Whether this is a framework module.
212 /// \param IsExplicit Whether this is an explicit submodule.
214 /// \returns The found or newly-created module, along with a boolean value
215 /// that will be true if the module is newly-created.
216 std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
220 /// \brief Determine whether we can infer a framework module a framework
221 /// with the given name in the given
223 /// \param ParentDir The directory that is the parent of the framework
226 /// \param Name The name of the module.
228 /// \param IsSystem Will be set to 'true' if the inferred module must be a
231 /// \returns true if we are allowed to infer a framework module, and false
233 bool canInferFrameworkModule(const DirectoryEntry *ParentDir,
234 StringRef Name, bool &IsSystem);
236 /// \brief Infer the contents of a framework module map from the given
237 /// framework directory.
238 Module *inferFrameworkModule(StringRef ModuleName,
239 const DirectoryEntry *FrameworkDir,
240 bool IsSystem, Module *Parent);
242 /// \brief Retrieve the module map file containing the definition of the given
245 /// \param Module The module whose module map file will be returned, if known.
247 /// \returns The file entry for the module map file containing the given
248 /// module, or NULL if the module definition was inferred.
249 const FileEntry *getContainingModuleMapFile(Module *Module);
251 /// \brief Resolve all of the unresolved exports in the given module.
253 /// \param Mod The module whose exports should be resolved.
255 /// \param Complain Whether to emit diagnostics for failures.
257 /// \returns true if any errors were encountered while resolving exports,
259 bool resolveExports(Module *Mod, bool Complain);
261 /// \brief Infers the (sub)module based on the given source location and
264 /// \param Loc The location within the source that we are querying, along
265 /// with its source manager.
267 /// \returns The module that owns this source location, or null if no
268 /// module owns this source location.
269 Module *inferModuleFromLocation(FullSourceLoc Loc);
271 /// \brief Sets the umbrella header of the given module to the given
273 void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader);
275 /// \brief Sets the umbrella directory of the given module to the given
277 void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir);
279 /// \brief Adds this header to the given module.
280 /// \param Excluded Whether this header is explicitly excluded from the
281 /// module; otherwise, it's included in the module.
282 void addHeader(Module *Mod, const FileEntry *Header, bool Excluded);
284 /// \brief Parse the given module map file, and record any modules we
287 /// \param File The file to be parsed.
289 /// \returns true if an error occurred, false otherwise.
290 bool parseModuleMapFile(const FileEntry *File);
292 /// \brief Dump the contents of the module map, for debugging purposes.
295 typedef llvm::StringMap<Module *>::const_iterator module_iterator;
296 module_iterator module_begin() const { return Modules.begin(); }
297 module_iterator module_end() const { return Modules.end(); }