blob: 65deac1b4af41ff28a558dede5f6a985534d9dfc [file] [log] [blame]
Chandler Carruth55fc8732012-12-04 09:13:33 +00001//===--- Module.cpp - Describe a module -----------------------------------===//
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002//
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"
Douglas Gregore727d212012-01-30 06:38:25 +000017#include "clang/Basic/TargetInfo.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringSwitch.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/raw_ostream.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000022using namespace clang;
23
Douglas Gregorb7a78192012-01-04 23:32:19 +000024Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
25 bool IsFramework, bool IsExplicit)
Argyrios Kyrtzidise2ac16b2012-09-29 01:06:04 +000026 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
27 Umbrella(), ASTFile(0), IsAvailable(true), IsFromModuleFile(false),
Douglas Gregora1f1fad2012-01-27 19:52:33 +000028 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
29 InferSubmodules(false), InferExplicitSubmodules(false),
30 InferExportWildcard(false), NameVisibility(Hidden)
Douglas Gregorb7a78192012-01-04 23:32:19 +000031{
32 if (Parent) {
33 if (!Parent->isAvailable())
34 IsAvailable = false;
Douglas Gregor2f04f182012-02-02 18:42:48 +000035 if (Parent->IsSystem)
36 IsSystem = true;
Douglas Gregorb7a78192012-01-04 23:32:19 +000037
38 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
39 Parent->SubModules.push_back(this);
40 }
41}
42
Douglas Gregor1a4761e2011-11-30 23:21:26 +000043Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000044 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000045 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000046 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000047 }
48
49}
50
Douglas Gregor51f564f2011-12-31 04:05:44 +000051/// \brief Determine whether a translation unit built using the current
52/// language options has the given feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +000053static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
54 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000055 return llvm::StringSwitch<bool>(Feature)
Douglas Gregore727d212012-01-30 06:38:25 +000056 .Case("altivec", LangOpts.AltiVec)
Douglas Gregor51f564f2011-12-31 04:05:44 +000057 .Case("blocks", LangOpts.Blocks)
58 .Case("cplusplus", LangOpts.CPlusPlus)
Richard Smith80ad52f2013-01-02 11:42:31 +000059 .Case("cplusplus11", LangOpts.CPlusPlus11)
Douglas Gregor51f564f2011-12-31 04:05:44 +000060 .Case("objc", LangOpts.ObjC1)
61 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
Douglas Gregore727d212012-01-30 06:38:25 +000062 .Case("opencl", LangOpts.OpenCL)
63 .Case("tls", Target.isTLSSupported())
64 .Default(Target.hasFeature(Feature));
Douglas Gregor51f564f2011-12-31 04:05:44 +000065}
66
67bool
Douglas Gregordc58aa72012-01-30 06:01:29 +000068Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
69 StringRef &Feature) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000070 if (IsAvailable)
71 return true;
72
73 for (const Module *Current = this; Current; Current = Current->Parent) {
74 for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
Douglas Gregordc58aa72012-01-30 06:01:29 +000075 if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000076 Feature = Current->Requires[I];
77 return false;
78 }
79 }
80 }
81
82 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000083}
84
Douglas Gregor0adaa882011-12-05 17:28:06 +000085bool Module::isSubModuleOf(Module *Other) const {
86 const Module *This = this;
87 do {
88 if (This == Other)
89 return true;
90
91 This = This->Parent;
92 } while (This);
93
94 return false;
95}
96
Douglas Gregor1e123682011-12-05 22:27:44 +000097const Module *Module::getTopLevelModule() const {
98 const Module *Result = this;
99 while (Result->Parent)
100 Result = Result->Parent;
101
102 return Result;
103}
104
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000105std::string Module::getFullModuleName() const {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000106 SmallVector<StringRef, 2> Names;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000107
108 // Build up the set of module names (from innermost to outermost).
109 for (const Module *M = this; M; M = M->Parent)
110 Names.push_back(M->Name);
111
112 std::string Result;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000113 for (SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
114 IEnd = Names.rend();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000115 I != IEnd; ++I) {
116 if (!Result.empty())
117 Result += '.';
118
119 Result += *I;
120 }
121
122 return Result;
123}
124
Douglas Gregor10694ce2011-12-08 17:39:04 +0000125const DirectoryEntry *Module::getUmbrellaDir() const {
126 if (const FileEntry *Header = getUmbrellaHeader())
127 return Header->getDir();
128
129 return Umbrella.dyn_cast<const DirectoryEntry *>();
130}
131
Douglas Gregordc58aa72012-01-30 06:01:29 +0000132void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
133 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000134 Requires.push_back(Feature);
135
136 // If this feature is currently available, we're done.
Douglas Gregordc58aa72012-01-30 06:01:29 +0000137 if (hasFeature(Feature, LangOpts, Target))
Douglas Gregor51f564f2011-12-31 04:05:44 +0000138 return;
139
140 if (!IsAvailable)
141 return;
142
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000143 SmallVector<Module *, 2> Stack;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000144 Stack.push_back(this);
145 while (!Stack.empty()) {
146 Module *Current = Stack.back();
147 Stack.pop_back();
148
149 if (!Current->IsAvailable)
150 continue;
151
152 Current->IsAvailable = false;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000153 for (submodule_iterator Sub = Current->submodule_begin(),
154 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000155 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000156 if ((*Sub)->IsAvailable)
157 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000158 }
159 }
160}
161
Douglas Gregorb7a78192012-01-04 23:32:19 +0000162Module *Module::findSubmodule(StringRef Name) const {
163 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
164 if (Pos == SubModuleIndex.end())
165 return 0;
166
167 return SubModules[Pos->getValue()];
168}
169
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000170static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000171 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
172 if (I)
173 OS << ".";
174 OS << Id[I].first;
175 }
176}
177
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000178void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
179 bool AnyWildcard = false;
180 bool UnrestrictedWildcard = false;
181 SmallVector<Module *, 4> WildcardRestrictions;
182 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
183 Module *Mod = Exports[I].getPointer();
184 if (!Exports[I].getInt()) {
185 // Export a named module directly; no wildcards involved.
186 Exported.push_back(Mod);
187
188 continue;
189 }
190
191 // Wildcard export: export all of the imported modules that match
192 // the given pattern.
193 AnyWildcard = true;
194 if (UnrestrictedWildcard)
195 continue;
196
197 if (Module *Restriction = Exports[I].getPointer())
198 WildcardRestrictions.push_back(Restriction);
199 else {
200 WildcardRestrictions.clear();
201 UnrestrictedWildcard = true;
202 }
203 }
204
205 // If there were any wildcards, push any imported modules that were
206 // re-exported by the wildcard restriction.
207 if (!AnyWildcard)
208 return;
209
210 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
211 Module *Mod = Imports[I];
212 bool Acceptable = UnrestrictedWildcard;
213 if (!Acceptable) {
214 // Check whether this module meets one of the restrictions.
215 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
216 Module *Restriction = WildcardRestrictions[R];
217 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
218 Acceptable = true;
219 break;
220 }
221 }
222 }
223
224 if (!Acceptable)
225 continue;
226
227 Exported.push_back(Mod);
228 }
229}
230
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000231void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000232 OS.indent(Indent);
233 if (IsFramework)
234 OS << "framework ";
235 if (IsExplicit)
236 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000237 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000238
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000239 if (IsSystem) {
240 OS.indent(Indent + 2);
241 OS << " [system]";
242 }
243
244 OS << " {\n";
245
Douglas Gregor51f564f2011-12-31 04:05:44 +0000246 if (!Requires.empty()) {
247 OS.indent(Indent + 2);
248 OS << "requires ";
249 for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
250 if (I)
251 OS << ", ";
252 OS << Requires[I];
253 }
254 OS << "\n";
255 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000256
Douglas Gregor10694ce2011-12-08 17:39:04 +0000257 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000258 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000259 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000260 OS.write_escaped(UmbrellaHeader->getName());
261 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000262 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
263 OS.indent(Indent + 2);
264 OS << "umbrella \"";
265 OS.write_escaped(UmbrellaDir->getName());
266 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000267 }
268
269 for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
270 OS.indent(Indent + 2);
271 OS << "header \"";
272 OS.write_escaped(Headers[I]->getName());
273 OS << "\"\n";
274 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000275
276 for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
277 OS.indent(Indent + 2);
278 OS << "exclude header \"";
279 OS.write_escaped(ExcludedHeaders[I]->getName());
280 OS << "\"\n";
281 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000282
Douglas Gregorb7a78192012-01-04 23:32:19 +0000283 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000284 MI != MIEnd; ++MI)
Douglas Gregorb7a78192012-01-04 23:32:19 +0000285 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000286
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000287 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
288 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000289 OS << "export ";
290 if (Module *Restriction = Exports[I].getPointer()) {
291 OS << Restriction->getFullModuleName();
292 if (Exports[I].getInt())
293 OS << ".*";
294 } else {
295 OS << "*";
296 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000297 OS << "\n";
298 }
299
300 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
301 OS.indent(Indent + 2);
302 OS << "export ";
303 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000304 if (UnresolvedExports[I].Wildcard) {
305 if (UnresolvedExports[I].Id.empty())
306 OS << "*";
307 else
308 OS << ".*";
309 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000310 OS << "\n";
311 }
312
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000313 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
314 OS.indent(Indent + 2);
315 OS << "link ";
316 if (LinkLibraries[I].IsFramework)
317 OS << "framework ";
318 OS << "\"";
319 OS.write_escaped(LinkLibraries[I].Library);
320 OS << "\"";
321 }
322
Douglas Gregor1e123682011-12-05 22:27:44 +0000323 if (InferSubmodules) {
324 OS.indent(Indent + 2);
325 if (InferExplicitSubmodules)
326 OS << "explicit ";
327 OS << "module * {\n";
328 if (InferExportWildcard) {
329 OS.indent(Indent + 4);
330 OS << "export *\n";
331 }
332 OS.indent(Indent + 2);
333 OS << "}\n";
334 }
335
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000336 OS.indent(Indent);
337 OS << "}\n";
338}
339
340void Module::dump() const {
341 print(llvm::errs());
342}
343
344