ModuleMap.h revision 2821c7f8870629b56b9c41e1c50c7a091edd544d
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 an explicit submodule.
63    bool IsExplicit;
64
65    /// \brief Construct a top-level module.
66    explicit Module(StringRef Name, SourceLocation DefinitionLoc)
67      : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), UmbrellaHeader(0),
68        IsExplicit(false) { }
69
70    /// \brief Construct  a new module or submodule.
71    Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
72           bool IsExplicit)
73      : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
74        UmbrellaHeader(0), IsExplicit(IsExplicit) {
75    }
76
77    /// \brief Determine whether this module is a submodule.
78    bool isSubModule() const { return Parent != 0; }
79
80    /// \brief Retrieve the full name of this module, including the path from
81    /// its top-level module.
82    std::string getFullModuleName() const;
83
84    /// \brief Retrieve the name of the top-level module.
85    StringRef getTopLevelModuleName() const;
86  };
87
88private:
89  SourceManager *SourceMgr;
90  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
91  LangOptions LangOpts;
92
93  /// \brief The top-level modules that are known.
94  llvm::StringMap<Module *> Modules;
95
96  /// \brief Mapping from each header to the module that owns the contents of the
97  /// that header.
98  llvm::DenseMap<const FileEntry *, Module *> Headers;
99
100  /// \brief Mapping from directories with umbrella headers to the module
101  /// that is generated from the umbrella header.
102  ///
103  /// This mapping is used to map headers that haven't explicitly been named
104  /// in the module map over to the module that includes them via its umbrella
105  /// header.
106  llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
107
108  friend class ModuleMapParser;
109
110public:
111  /// \brief Construct a new module map.
112  ///
113  /// \param FileMgr The file manager used to find module files and headers.
114  /// This file manager should be shared with the header-search mechanism, since
115  /// they will refer to the same headers.
116  ///
117  /// \param DC A diagnostic consumer that will be cloned for use in generating
118  /// diagnostics.
119  ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC);
120
121  /// \brief Destroy the module map.
122  ///
123  ~ModuleMap();
124
125  /// \brief Retrieve the module that owns the given header file, if any.
126  ///
127  /// \param File The header file that is likely to be included.
128  ///
129  /// \returns The module that owns the given header file, or null to indicate
130  /// that no module owns this header file.
131  Module *findModuleForHeader(const FileEntry *File);
132
133  /// \brief Retrieve a module with the given name.
134  ///
135  /// \param The name of the module to look up.
136  ///
137  /// \returns The named module, if known; otherwise, returns null.
138  Module *findModule(StringRef Name);
139
140  /// \brief Infer the contents of a framework module map from the given
141  /// framework directory.
142  Module *inferFrameworkModule(StringRef ModuleName,
143                               const DirectoryEntry *FrameworkDir);
144
145  /// \brief Parse the given module map file, and record any modules we
146  /// encounter.
147  ///
148  /// \param File The file to be parsed.
149  ///
150  /// \returns true if an error occurred, false otherwise.
151  bool parseModuleMapFile(const FileEntry *File);
152
153  /// \brief Dump the contents of the module map, for debugging purposes.
154  void dump();
155};
156
157}
158#endif
159