blob: c5bc86de81db9f5f68fe593a059cb383f1208a95 [file] [log] [blame]
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001//===--- Module.h - Describe a module ---------------------------*- 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 Module class, which describes a module in the source
11// code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/Basic/Module.h"
15#include "clang/Basic/FileManager.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000016#include "clang/Basic/LangOptions.h"
17#include "llvm/Support/ErrorHandling.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000018#include "llvm/Support/raw_ostream.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringSwitch.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000021using namespace clang;
22
Douglas Gregorb7a78192012-01-04 23:32:19 +000023Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
24 bool IsFramework, bool IsExplicit)
25 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
26 Umbrella(), IsAvailable(true), IsFromModuleFile(false),
27 IsFramework(IsFramework), IsExplicit(IsExplicit), InferSubmodules(false),
28 InferExplicitSubmodules(false), InferExportWildcard(false),
29 NameVisibility(Hidden)
30{
31 if (Parent) {
32 if (!Parent->isAvailable())
33 IsAvailable = false;
34
35 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
36 Parent->SubModules.push_back(this);
37 }
38}
39
Douglas Gregor1a4761e2011-11-30 23:21:26 +000040Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000041 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000042 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000043 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000044 }
45
46}
47
Douglas Gregor51f564f2011-12-31 04:05:44 +000048/// \brief Determine whether a translation unit built using the current
49/// language options has the given feature.
50static bool hasFeature(StringRef Feature, const LangOptions &LangOpts) {
51 return llvm::StringSwitch<bool>(Feature)
52 .Case("blocks", LangOpts.Blocks)
53 .Case("cplusplus", LangOpts.CPlusPlus)
54 .Case("cplusplus11", LangOpts.CPlusPlus0x)
55 .Case("objc", LangOpts.ObjC1)
56 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
57 .Default(false);
58}
59
60bool
61Module::isAvailable(const LangOptions &LangOpts, StringRef &Feature) const {
62 if (IsAvailable)
63 return true;
64
65 for (const Module *Current = this; Current; Current = Current->Parent) {
66 for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
67 if (!hasFeature(Current->Requires[I], LangOpts)) {
68 Feature = Current->Requires[I];
69 return false;
70 }
71 }
72 }
73
74 llvm_unreachable("could not find a reason why module is unavailable");
75 return false;
76}
77
Douglas Gregor0adaa882011-12-05 17:28:06 +000078bool Module::isSubModuleOf(Module *Other) const {
79 const Module *This = this;
80 do {
81 if (This == Other)
82 return true;
83
84 This = This->Parent;
85 } while (This);
86
87 return false;
88}
89
Douglas Gregor1e123682011-12-05 22:27:44 +000090const Module *Module::getTopLevelModule() const {
91 const Module *Result = this;
92 while (Result->Parent)
93 Result = Result->Parent;
94
95 return Result;
96}
97
Douglas Gregor1a4761e2011-11-30 23:21:26 +000098std::string Module::getFullModuleName() const {
99 llvm::SmallVector<StringRef, 2> Names;
100
101 // Build up the set of module names (from innermost to outermost).
102 for (const Module *M = this; M; M = M->Parent)
103 Names.push_back(M->Name);
104
105 std::string Result;
106 for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
107 IEnd = Names.rend();
108 I != IEnd; ++I) {
109 if (!Result.empty())
110 Result += '.';
111
112 Result += *I;
113 }
114
115 return Result;
116}
117
Douglas Gregor10694ce2011-12-08 17:39:04 +0000118const DirectoryEntry *Module::getUmbrellaDir() const {
119 if (const FileEntry *Header = getUmbrellaHeader())
120 return Header->getDir();
121
122 return Umbrella.dyn_cast<const DirectoryEntry *>();
123}
124
Douglas Gregor51f564f2011-12-31 04:05:44 +0000125void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts) {
126 Requires.push_back(Feature);
127
128 // If this feature is currently available, we're done.
129 if (hasFeature(Feature, LangOpts))
130 return;
131
132 if (!IsAvailable)
133 return;
134
135 llvm::SmallVector<Module *, 2> Stack;
136 Stack.push_back(this);
137 while (!Stack.empty()) {
138 Module *Current = Stack.back();
139 Stack.pop_back();
140
141 if (!Current->IsAvailable)
142 continue;
143
144 Current->IsAvailable = false;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000145 for (submodule_iterator Sub = Current->submodule_begin(),
146 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000147 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000148 if ((*Sub)->IsAvailable)
149 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000150 }
151 }
152}
153
Douglas Gregorb7a78192012-01-04 23:32:19 +0000154Module *Module::findSubmodule(StringRef Name) const {
155 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
156 if (Pos == SubModuleIndex.end())
157 return 0;
158
159 return SubModules[Pos->getValue()];
160}
161
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000162static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
163 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
164 if (I)
165 OS << ".";
166 OS << Id[I].first;
167 }
168}
169
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000170void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
171 OS.indent(Indent);
172 if (IsFramework)
173 OS << "framework ";
174 if (IsExplicit)
175 OS << "explicit ";
176 OS << "module " << Name << " {\n";
Douglas Gregor51f564f2011-12-31 04:05:44 +0000177
178 if (!Requires.empty()) {
179 OS.indent(Indent + 2);
180 OS << "requires ";
181 for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
182 if (I)
183 OS << ", ";
184 OS << Requires[I];
185 }
186 OS << "\n";
187 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000188
Douglas Gregor10694ce2011-12-08 17:39:04 +0000189 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000190 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000191 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000192 OS.write_escaped(UmbrellaHeader->getName());
193 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000194 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
195 OS.indent(Indent + 2);
196 OS << "umbrella \"";
197 OS.write_escaped(UmbrellaDir->getName());
198 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000199 }
200
201 for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
202 OS.indent(Indent + 2);
203 OS << "header \"";
204 OS.write_escaped(Headers[I]->getName());
205 OS << "\"\n";
206 }
207
Douglas Gregorb7a78192012-01-04 23:32:19 +0000208 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000209 MI != MIEnd; ++MI)
Douglas Gregorb7a78192012-01-04 23:32:19 +0000210 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000211
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000212 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
213 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000214 OS << "export ";
215 if (Module *Restriction = Exports[I].getPointer()) {
216 OS << Restriction->getFullModuleName();
217 if (Exports[I].getInt())
218 OS << ".*";
219 } else {
220 OS << "*";
221 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000222 OS << "\n";
223 }
224
225 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
226 OS.indent(Indent + 2);
227 OS << "export ";
228 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000229 if (UnresolvedExports[I].Wildcard) {
230 if (UnresolvedExports[I].Id.empty())
231 OS << "*";
232 else
233 OS << ".*";
234 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000235 OS << "\n";
236 }
237
Douglas Gregor1e123682011-12-05 22:27:44 +0000238 if (InferSubmodules) {
239 OS.indent(Indent + 2);
240 if (InferExplicitSubmodules)
241 OS << "explicit ";
242 OS << "module * {\n";
243 if (InferExportWildcard) {
244 OS.indent(Indent + 4);
245 OS << "export *\n";
246 }
247 OS.indent(Indent + 2);
248 OS << "}\n";
249 }
250
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000251 OS.indent(Indent);
252 OS << "}\n";
253}
254
255void Module::dump() const {
256 print(llvm::errs());
257}
258
259