ModuleMap.h revision 804c3bfee22076f232dddf4839439119cfdee2b6
1//===--- ModuleMap.h - Describe the layout of modules -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ModuleMap interface, which describes the layout of a
11// module as it relates to headers.
12//
13//===----------------------------------------------------------------------===//
14
15
16#ifndef LLVM_CLANG_LEX_MODULEMAP_H
17#define LLVM_CLANG_LEX_MODULEMAP_H
18
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceManager.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/IntrusiveRefCntPtr.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/StringMap.h"
26#include <string>
27
28namespace clang {
29
30class DirectoryEntry;
31class FileEntry;
32class FileManager;
33class DiagnosticConsumer;
34class DiagnosticsEngine;
35class ModuleMapParser;
36
37class ModuleMap {
38public:
39  /// \brief Describes a module or submodule.
40  struct Module {
41    /// \brief The name of this module.
42    std::string Name;
43
44    /// \brief The location of the module definition.
45    SourceLocation DefinitionLoc;
46
47    /// \brief The parent of this module. This will be NULL for the top-level
48    /// module.
49    Module *Parent;
50
51    /// \brief The umbrella header, if any.
52    ///
53    /// Only the top-level module can have an umbrella header.
54    const FileEntry *UmbrellaHeader;
55
56    /// \brief The submodules of this module, indexed by name.
57    llvm::StringMap<Module *> SubModules;
58
59    /// \brief The headers that are part of this module.
60    llvm::SmallVector<const FileEntry *, 2> Headers;
61
62    /// \brief Whether this is a framework module.
63    bool IsFramework;
64
65    /// \brief Whether this is an explicit submodule.
66    bool IsExplicit;
67
68    /// \brief Construct a top-level module.
69    explicit Module(StringRef Name, SourceLocation DefinitionLoc,
70                    bool IsFramework)
71      : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), UmbrellaHeader(0),
72        IsFramework(IsFramework), IsExplicit(false) { }
73
74    /// \brief Construct  a new module or submodule.
75    Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
76           bool IsFramework, bool IsExplicit)
77      : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
78        UmbrellaHeader(0), IsFramework(IsFramework), IsExplicit(IsExplicit) {
79    }
80
81    ~Module();
82
83    /// \brief Determine whether this module is a submodule.
84    bool isSubModule() const { return Parent != 0; }
85
86    /// \brief Determine whether this module is a part of a framework,
87    /// either because it is a framework module or because it is a submodule
88    /// of a framework module.
89    bool isPartOfFramework() const {
90      for (const Module *Mod = this; Mod; Mod = Mod->Parent)
91        if (Mod->IsFramework)
92          return true;
93
94      return false;
95    }
96
97    /// \brief Retrieve the full name of this module, including the path from
98    /// its top-level module.
99    std::string getFullModuleName() const;
100
101    /// \brief Retrieve the name of the top-level module.
102    ///
103    StringRef getTopLevelModuleName() const;
104
105    /// \brief Print the module map for this module to the given stream.
106    ///
107    void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
108
109    /// \brief Dump the contents of this module to the given output stream.
110    void dump() const;
111  };
112
113private:
114  SourceManager *SourceMgr;
115  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
116  LangOptions LangOpts;
117
118  /// \brief The top-level modules that are known.
119  llvm::StringMap<Module *> Modules;
120
121  /// \brief Mapping from each header to the module that owns the contents of the
122  /// that header.
123  llvm::DenseMap<const FileEntry *, Module *> Headers;
124
125  /// \brief Mapping from directories with umbrella headers to the module
126  /// that is generated from the umbrella header.
127  ///
128  /// This mapping is used to map headers that haven't explicitly been named
129  /// in the module map over to the module that includes them via its umbrella
130  /// header.
131  llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
132
133  friend class ModuleMapParser;
134
135public:
136  /// \brief Construct a new module map.
137  ///
138  /// \param FileMgr The file manager used to find module files and headers.
139  /// This file manager should be shared with the header-search mechanism, since
140  /// they will refer to the same headers.
141  ///
142  /// \param DC A diagnostic consumer that will be cloned for use in generating
143  /// diagnostics.
144  ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC);
145
146  /// \brief Destroy the module map.
147  ///
148  ~ModuleMap();
149
150  /// \brief Retrieve the module that owns the given header file, if any.
151  ///
152  /// \param File The header file that is likely to be included.
153  ///
154  /// \returns The module that owns the given header file, or null to indicate
155  /// that no module owns this header file.
156  Module *findModuleForHeader(const FileEntry *File);
157
158  /// \brief Retrieve a module with the given name.
159  ///
160  /// \param The name of the module to look up.
161  ///
162  /// \returns The named module, if known; otherwise, returns null.
163  Module *findModule(StringRef Name);
164
165  /// \brief Infer the contents of a framework module map from the given
166  /// framework directory.
167  Module *inferFrameworkModule(StringRef ModuleName,
168                               const DirectoryEntry *FrameworkDir);
169
170  /// \brief Parse the given module map file, and record any modules we
171  /// encounter.
172  ///
173  /// \param File The file to be parsed.
174  ///
175  /// \returns true if an error occurred, false otherwise.
176  bool parseModuleMapFile(const FileEntry *File);
177
178  /// \brief Dump the contents of the module map, for debugging purposes.
179  void dump();
180};
181
182}
183#endif
184